make interactive actions a type and not special cases.
[mikachu/openbox.git] / openbox / screen.c
1 #include "debug.h"
2 #include "openbox.h"
3 #include "mainloop.h"
4 #include "dock.h"
5 #include "xerror.h"
6 #include "prop.h"
7 #include "startup.h"
8 #include "grab.h"
9 #include "config.h"
10 #include "screen.h"
11 #include "client.h"
12 #include "frame.h"
13 #include "focus.h"
14 #include "popup.h"
15 #include "extensions.h"
16 #include "render/render.h"
17
18 #ifdef USE_LIBSN
19 #  define SN_API_NOT_YET_FROZEN
20 #  include <libsn/sn.h>
21 #endif
22
23 #include <X11/Xlib.h>
24 #ifdef HAVE_UNISTD_H
25 #  include <sys/types.h>
26 #  include <unistd.h>
27 #endif
28 #include <assert.h>
29
30 /*! The event mask to grab on the root window */
31 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
32                         EnterWindowMask | LeaveWindowMask | \
33                         SubstructureNotifyMask | SubstructureRedirectMask | \
34                         ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
35
36 guint    screen_num_desktops;
37 guint    screen_num_monitors;
38 guint    screen_desktop;
39 Size     screen_physical_size;
40 gboolean screen_showing_desktop;
41 DesktopLayout screen_desktop_layout;
42 char   **screen_desktop_names;
43 Window   screen_support_win;
44
45 static Rect  **area; /* array of desktop holding array of xinerama areas */
46 static Rect  *monitor_area;
47
48 static Popup *desktop_cycle_popup;
49
50 #ifdef USE_LIBSN
51 static SnMonitorContext *sn_context;
52 static int sn_busy_cnt;
53
54 static void sn_event_func(SnMonitorEvent *event, void *data);
55 #endif
56
57 static void set_root_cursor();
58
59 static gboolean replace_wm()
60 {
61     char *wm_sn;
62     Atom wm_sn_atom;
63     Window current_wm_sn_owner;
64     Time timestamp;
65
66     wm_sn = g_strdup_printf("WM_S%d", ob_screen);
67     wm_sn_atom = XInternAtom(ob_display, wm_sn, FALSE);
68     g_free(wm_sn);
69
70     current_wm_sn_owner = XGetSelectionOwner(ob_display, wm_sn_atom);
71     if (current_wm_sn_owner) {
72         if (!ob_replace_wm) {
73             g_warning("A window manager is already running on screen %d",
74                       ob_screen);
75             return FALSE;
76         }
77         xerror_set_ignore(TRUE);
78         xerror_occured = FALSE;
79
80         /* We want to find out when the current selection owner dies */
81         XSelectInput(ob_display, current_wm_sn_owner, StructureNotifyMask);
82         XSync(ob_display, FALSE);
83
84         xerror_set_ignore(FALSE);
85         if (xerror_occured)
86             current_wm_sn_owner = None;
87     }
88
89     {
90         /* Generate a timestamp */
91         XEvent event;
92
93         XSelectInput(ob_display, screen_support_win, PropertyChangeMask);
94
95         XChangeProperty(ob_display, screen_support_win,
96                         prop_atoms.wm_class, prop_atoms.string,
97                         8, PropModeAppend, NULL, 0);
98         XWindowEvent(ob_display, screen_support_win,
99                      PropertyChangeMask, &event);
100
101         XSelectInput(ob_display, screen_support_win, NoEventMask);
102
103         timestamp = event.xproperty.time;
104     }
105
106     XSetSelectionOwner(ob_display, wm_sn_atom, screen_support_win,
107                        timestamp);
108
109     if (XGetSelectionOwner(ob_display, wm_sn_atom) != screen_support_win) {
110         g_warning("Could not acquire window manager selection on screen %d",
111                   ob_screen);
112         return FALSE;
113     }
114
115     /* Wait for old window manager to go away */
116     if (current_wm_sn_owner) {
117       XEvent event;
118       gulong wait = 0;
119       const gulong timeout = G_USEC_PER_SEC * 15; /* wait for 15s max */
120
121       while (wait < timeout) {
122           if (XCheckWindowEvent(ob_display, current_wm_sn_owner,
123                                 StructureNotifyMask, &event) &&
124               event.type == DestroyNotify)
125               break;
126           g_usleep(G_USEC_PER_SEC / 10);
127           wait += G_USEC_PER_SEC / 10;
128       }
129
130       if (wait >= timeout) {
131           g_warning("Timeout expired while waiting for the current WM to die "
132                     "on screen %d", ob_screen);
133           return FALSE;
134       }
135     }
136
137     /* Send client message indicating that we are now the WM */
138     prop_message(RootWindow(ob_display, ob_screen), prop_atoms.manager,
139                  timestamp, wm_sn_atom, 0, 0, SubstructureNotifyMask);
140
141
142     return TRUE;
143 }
144
145 gboolean screen_annex()
146 {
147     XSetWindowAttributes attrib;
148     pid_t pid;
149     gint i, num_support;
150     guint32 *supported;
151
152     /* create the netwm support window */
153     attrib.override_redirect = TRUE;
154     screen_support_win = XCreateWindow(ob_display,
155                                        RootWindow(ob_display, ob_screen),
156                                        -100, -100, 1, 1, 0,
157                                        CopyFromParent, InputOutput,
158                                        CopyFromParent,
159                                        CWOverrideRedirect, &attrib);
160     XMapRaised(ob_display, screen_support_win);
161
162     if (!replace_wm()) {
163         XDestroyWindow(ob_display, screen_support_win);
164         return FALSE;
165     }
166
167     xerror_set_ignore(TRUE);
168     xerror_occured = FALSE;
169     XSelectInput(ob_display, RootWindow(ob_display, ob_screen),
170                  ROOT_EVENTMASK);
171     xerror_set_ignore(FALSE);
172     if (xerror_occured) {
173         g_warning("A window manager is already running on screen %d",
174                   ob_screen);
175
176         XDestroyWindow(ob_display, screen_support_win);
177         return FALSE;
178     }
179
180
181     set_root_cursor();
182
183     /* set the OPENBOX_PID hint */
184     pid = getpid();
185     PROP_SET32(RootWindow(ob_display, ob_screen),
186                openbox_pid, cardinal, pid);
187
188     /* set supporting window */
189     PROP_SET32(RootWindow(ob_display, ob_screen),
190                net_supporting_wm_check, window, screen_support_win);
191
192     /* set properties on the supporting window */
193     PROP_SETS(screen_support_win, net_wm_name, "Openbox");
194     PROP_SET32(screen_support_win, net_supporting_wm_check,
195                window, screen_support_win);
196
197     /* set the _NET_SUPPORTED_ATOMS hint */
198     num_support = 50;
199     i = 0;
200     supported = g_new(guint32, num_support);
201     supported[i++] = prop_atoms.net_current_desktop;
202     supported[i++] = prop_atoms.net_number_of_desktops;
203     supported[i++] = prop_atoms.net_desktop_geometry;
204     supported[i++] = prop_atoms.net_desktop_viewport;
205     supported[i++] = prop_atoms.net_active_window;
206     supported[i++] = prop_atoms.net_workarea;
207     supported[i++] = prop_atoms.net_client_list;
208     supported[i++] = prop_atoms.net_client_list_stacking;
209     supported[i++] = prop_atoms.net_desktop_names;
210     supported[i++] = prop_atoms.net_close_window;
211     supported[i++] = prop_atoms.net_desktop_layout;
212     supported[i++] = prop_atoms.net_showing_desktop;
213     supported[i++] = prop_atoms.net_wm_name;
214     supported[i++] = prop_atoms.net_wm_visible_name;
215     supported[i++] = prop_atoms.net_wm_icon_name;
216     supported[i++] = prop_atoms.net_wm_visible_icon_name;
217     supported[i++] = prop_atoms.net_wm_desktop;
218     supported[i++] = prop_atoms.net_wm_strut;
219     supported[i++] = prop_atoms.net_wm_window_type;
220     supported[i++] = prop_atoms.net_wm_window_type_desktop;
221     supported[i++] = prop_atoms.net_wm_window_type_dock;
222     supported[i++] = prop_atoms.net_wm_window_type_toolbar;
223     supported[i++] = prop_atoms.net_wm_window_type_menu;
224     supported[i++] = prop_atoms.net_wm_window_type_utility;
225     supported[i++] = prop_atoms.net_wm_window_type_splash;
226     supported[i++] = prop_atoms.net_wm_window_type_dialog;
227     supported[i++] = prop_atoms.net_wm_window_type_normal;
228     supported[i++] = prop_atoms.net_wm_allowed_actions;
229     supported[i++] = prop_atoms.net_wm_action_move;
230     supported[i++] = prop_atoms.net_wm_action_resize;
231     supported[i++] = prop_atoms.net_wm_action_minimize;
232     supported[i++] = prop_atoms.net_wm_action_shade;
233     supported[i++] = prop_atoms.net_wm_action_maximize_horz;
234     supported[i++] = prop_atoms.net_wm_action_maximize_vert;
235     supported[i++] = prop_atoms.net_wm_action_fullscreen;
236     supported[i++] = prop_atoms.net_wm_action_change_desktop;
237     supported[i++] = prop_atoms.net_wm_action_close;
238     supported[i++] = prop_atoms.net_wm_state;
239     supported[i++] = prop_atoms.net_wm_state_modal;
240     supported[i++] = prop_atoms.net_wm_state_maximized_vert;
241     supported[i++] = prop_atoms.net_wm_state_maximized_horz;
242     supported[i++] = prop_atoms.net_wm_state_shaded;
243     supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
244     supported[i++] = prop_atoms.net_wm_state_skip_pager;
245     supported[i++] = prop_atoms.net_wm_state_hidden;
246     supported[i++] = prop_atoms.net_wm_state_fullscreen;
247     supported[i++] = prop_atoms.net_wm_state_above;
248     supported[i++] = prop_atoms.net_wm_state_below;
249     supported[i++] = prop_atoms.net_moveresize_window;
250     supported[i++] = prop_atoms.net_wm_moveresize;
251     g_assert(i == num_support);
252 /*
253   supported[] = prop_atoms.net_wm_action_stick;
254 */
255
256     PROP_SETA32(RootWindow(ob_display, ob_screen),
257                 net_supported, atom, supported, num_support);
258     g_free(supported);
259
260     return TRUE;
261 }
262
263 void screen_startup()
264 {
265     GSList *it;
266     guint i;
267
268     desktop_cycle_popup = popup_new(FALSE);
269
270     /* get the initial size */
271     screen_resize();
272
273     /* set the names */
274     screen_desktop_names = g_new(char*,
275                                  g_slist_length(config_desktops_names) + 1);
276     for (i = 0, it = config_desktops_names; it; ++i, it = it->next)
277         screen_desktop_names[i] = it->data; /* dont strdup */
278     screen_desktop_names[i] = NULL;
279     PROP_SETSS(RootWindow(ob_display, ob_screen),
280                net_desktop_names, screen_desktop_names);
281     g_free(screen_desktop_names); /* dont free the individual strings */
282     screen_desktop_names = NULL;
283
284     screen_num_desktops = 0;
285     screen_set_num_desktops(config_desktops_num);
286     if (startup_desktop >= screen_num_desktops)
287         startup_desktop = 0;
288     screen_desktop = startup_desktop;
289     screen_set_desktop(startup_desktop);
290
291     /* don't start in showing-desktop mode */
292     screen_showing_desktop = FALSE;
293     PROP_SET32(RootWindow(ob_display, ob_screen),
294                net_showing_desktop, cardinal, screen_showing_desktop);
295
296     screen_update_layout();
297
298 #ifdef USE_LIBSN
299     sn_context = sn_monitor_context_new(ob_sn_display, ob_screen,
300                                         sn_event_func, NULL, NULL);
301     sn_busy_cnt = 0;
302 #endif
303 }
304
305 void screen_shutdown()
306 {
307     Rect **r;
308
309     popup_free(desktop_cycle_popup);
310
311     XSelectInput(ob_display, RootWindow(ob_display, ob_screen), NoEventMask);
312
313     /* we're not running here no more! */
314     PROP_ERASE(RootWindow(ob_display, ob_screen), openbox_pid);
315     /* not without us */
316     PROP_ERASE(RootWindow(ob_display, ob_screen), net_supported);
317     /* don't keep this mode */
318     PROP_ERASE(RootWindow(ob_display, ob_screen), net_showing_desktop);
319
320     XDestroyWindow(ob_display, screen_support_win);
321
322     g_strfreev(screen_desktop_names);
323     for (r = area; *r; ++r)
324         g_free(*r);
325     g_free(area);
326 }
327
328 void screen_resize()
329 {
330     static int oldw = 0, oldh = 0;
331     int w, h;
332     GList *it;
333     guint32 geometry[2];
334
335     w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
336     h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
337
338     if (w == oldw && h == oldh) return;
339
340     oldw = w; oldh = h;
341
342     /* Set the _NET_DESKTOP_GEOMETRY hint */
343     screen_physical_size.width = geometry[0] = w;
344     screen_physical_size.height = geometry[1] = h;
345     PROP_SETA32(RootWindow(ob_display, ob_screen),
346                 net_desktop_geometry, cardinal, geometry, 2);
347
348     if (ob_state() == OB_STATE_STARTING)
349         return;
350
351     screen_update_areas();
352     dock_configure();
353
354     for (it = client_list; it; it = it->next)
355         client_move_onscreen(it->data, FALSE);
356 }
357
358 void screen_set_num_desktops(guint num)
359 {
360     guint i, old;
361     guint32 *viewport;
362     GList *it;
363
364     g_assert(num > 0);
365
366     old = screen_num_desktops;
367     screen_num_desktops = num;
368     PROP_SET32(RootWindow(ob_display, ob_screen),
369                net_number_of_desktops, cardinal, num);
370
371     /* set the viewport hint */
372     viewport = g_new0(guint32, num * 2);
373     PROP_SETA32(RootWindow(ob_display, ob_screen),
374                 net_desktop_viewport, cardinal, viewport, num * 2);
375     g_free(viewport);
376
377     /* the number of rows/columns will differ */
378     screen_update_layout();
379
380     /* may be some unnamed desktops that we need to fill in with names */
381     screen_update_desktop_names();
382
383     /* update the focus lists */
384     /* free our lists for the desktops which have disappeared */
385     for (i = num; i < old; ++i)
386         g_list_free(focus_order[i]);
387     /* realloc the array */
388     focus_order = g_renew(GList*, focus_order, num);
389     /* set the new lists to be empty */
390     for (i = old; i < num; ++i)
391         focus_order[i] = NULL;
392
393     /* move windows on desktops that will no longer exist! */
394     for (it = client_list; it != NULL; it = it->next) {
395         ObClient *c = it->data;
396         if (c->desktop >= num && c->desktop != DESKTOP_ALL)
397             client_set_desktop(c, num - 1, FALSE);
398     }
399
400     /* change our struts/area to match (after moving windows) */
401     screen_update_areas();
402
403     /* change our desktop if we're on one that no longer exists! */
404     if (screen_desktop >= screen_num_desktops)
405         screen_set_desktop(num - 1);
406 }
407
408 void screen_set_desktop(guint num)
409 {
410     GList *it;
411     guint old;
412     XEvent e;
413      
414     g_assert(num < screen_num_desktops);
415
416     old = screen_desktop;
417     screen_desktop = num;
418     PROP_SET32(RootWindow(ob_display, ob_screen),
419                net_current_desktop, cardinal, num);
420
421     if (old == num) return;
422
423     ob_debug("Moving to desktop %d\n", num+1);
424
425     /* show windows before hiding the rest to lessen the enter/leave events */
426
427     /* show windows from top to bottom */
428     for (it = stacking_list; it != NULL; it = it->next) {
429         if (WINDOW_IS_CLIENT(it->data)) {
430             ObClient *c = it->data;
431             if (!c->frame->visible && client_should_show(c))
432                 frame_show(c->frame);
433         }
434     }
435
436     /* hide windows from bottom to top */
437     for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
438         if (WINDOW_IS_CLIENT(it->data)) {
439             ObClient *c = it->data;
440             if (c->frame->visible && !client_should_show(c))
441                 frame_hide(c->frame);
442         }
443     }
444
445     /* focus the last focused window on the desktop, and ignore enter events
446        from the switch so it doesnt mess with the focus */
447     while (XCheckTypedEvent(ob_display, EnterNotify, &e));
448 #ifdef DEBUG_FOCUS
449     ob_debug("switch fallback\n");
450 #endif
451     focus_fallback(OB_FOCUS_FALLBACK_DESKTOP);
452 #ifdef DEBUG_FOCUS
453     ob_debug("/switch fallback\n");
454 #endif
455 }
456
457 static void get_row_col(guint d, guint *r, guint *c)
458 {
459     switch (screen_desktop_layout.orientation) {
460     case OB_ORIENTATION_HORZ:
461         switch (screen_desktop_layout.start_corner) {
462         case OB_CORNER_TOPLEFT:
463             *r = d / screen_desktop_layout.columns;
464             *c = d % screen_desktop_layout.columns;
465             break;
466         case OB_CORNER_BOTTOMLEFT:
467             *r = screen_desktop_layout.rows - 1 -
468                 d / screen_desktop_layout.columns;
469             *c = d % screen_desktop_layout.columns;
470             break;
471         case OB_CORNER_TOPRIGHT:
472             *r = d / screen_desktop_layout.columns;
473             *c = screen_desktop_layout.columns - 1 -
474                 d % screen_desktop_layout.columns;
475             break;
476         case OB_CORNER_BOTTOMRIGHT:
477             *r = screen_desktop_layout.rows - 1 -
478                 d / screen_desktop_layout.columns;
479             *c = screen_desktop_layout.columns - 1 -
480                 d % screen_desktop_layout.columns;
481             break;
482         }
483         break;
484     case OB_ORIENTATION_VERT:
485         switch (screen_desktop_layout.start_corner) {
486         case OB_CORNER_TOPLEFT:
487             *r = d % screen_desktop_layout.rows;
488             *c = d / screen_desktop_layout.rows;
489             break;
490         case OB_CORNER_BOTTOMLEFT:
491             *r = screen_desktop_layout.rows - 1 -
492                 d % screen_desktop_layout.rows;
493             *c = d / screen_desktop_layout.rows;
494             break;
495         case OB_CORNER_TOPRIGHT:
496             *r = d % screen_desktop_layout.rows;
497             *c = screen_desktop_layout.columns - 1 -
498                 d / screen_desktop_layout.rows;
499             break;
500         case OB_CORNER_BOTTOMRIGHT:
501             *r = screen_desktop_layout.rows - 1 -
502                 d % screen_desktop_layout.rows;
503             *c = screen_desktop_layout.columns - 1 -
504                 d / screen_desktop_layout.rows;
505             break;
506         }
507         break;
508     }
509 }
510
511 static guint translate_row_col(guint r, guint c)
512 {
513     switch (screen_desktop_layout.orientation) {
514     case OB_ORIENTATION_HORZ:
515         switch (screen_desktop_layout.start_corner) {
516         case OB_CORNER_TOPLEFT:
517             return r % screen_desktop_layout.rows *
518                 screen_desktop_layout.columns +
519                 c % screen_desktop_layout.columns;
520         case OB_CORNER_BOTTOMLEFT:
521             return (screen_desktop_layout.rows - 1 -
522                     r % screen_desktop_layout.rows) *
523                 screen_desktop_layout.columns +
524                 c % screen_desktop_layout.columns;
525         case OB_CORNER_TOPRIGHT:
526             return r % screen_desktop_layout.rows *
527                 screen_desktop_layout.columns +
528                 (screen_desktop_layout.columns - 1 -
529                  c % screen_desktop_layout.columns);
530         case OB_CORNER_BOTTOMRIGHT:
531             return (screen_desktop_layout.rows - 1 -
532                     r % screen_desktop_layout.rows) *
533                 screen_desktop_layout.columns +
534                 (screen_desktop_layout.columns - 1 -
535                  c % screen_desktop_layout.columns);
536         }
537     case OB_ORIENTATION_VERT:
538         switch (screen_desktop_layout.start_corner) {
539         case OB_CORNER_TOPLEFT:
540             return c % screen_desktop_layout.columns *
541                 screen_desktop_layout.rows +
542                 r % screen_desktop_layout.rows;
543         case OB_CORNER_BOTTOMLEFT:
544             return c % screen_desktop_layout.columns *
545                 screen_desktop_layout.rows +
546                 (screen_desktop_layout.rows - 1 -
547                  r % screen_desktop_layout.rows);
548         case OB_CORNER_TOPRIGHT:
549             return (screen_desktop_layout.columns - 1 -
550                     c % screen_desktop_layout.columns) *
551                 screen_desktop_layout.rows +
552                 r % screen_desktop_layout.rows;
553         case OB_CORNER_BOTTOMRIGHT:
554             return (screen_desktop_layout.columns - 1 -
555                     c % screen_desktop_layout.columns) *
556                 screen_desktop_layout.rows +
557                 (screen_desktop_layout.rows - 1 -
558                  r % screen_desktop_layout.rows);
559         }
560     }
561     g_assert_not_reached();
562     return 0;
563 }
564
565 static void popup_cycle(guint d, gboolean show)
566 {
567     Rect *a;
568
569     if (!show) {
570         popup_hide(desktop_cycle_popup);
571     } else {
572         a = screen_physical_area_monitor(0);
573         popup_position(desktop_cycle_popup, CenterGravity,
574                        a->x + a->width / 2, a->y + a->height / 2);
575         /* XXX the size and the font extents need to be related on some level
576          */
577         popup_size(desktop_cycle_popup, POPUP_WIDTH, POPUP_HEIGHT);
578
579         popup_set_text_align(desktop_cycle_popup, RR_JUSTIFY_CENTER);
580
581         popup_show(desktop_cycle_popup,
582                    screen_desktop_names[d], NULL);
583     }
584 }
585
586 guint screen_cycle_desktop(ObDirection dir, gboolean wrap, gboolean linear,
587                            gboolean dialog, gboolean done, gboolean cancel)
588 {
589     static gboolean first = TRUE;
590     static gboolean lin;
591     static guint origd, d;
592     guint r, c;
593
594     if (cancel) {
595         d = origd;
596         goto done_cycle;
597     } else if (done) {
598         screen_set_desktop(d);
599         goto done_cycle;
600     }
601     if (first) {
602         first = FALSE;
603         lin = linear;
604         d = origd = screen_desktop;
605     }
606
607     get_row_col(d, &r, &c);
608
609     if (lin) {
610         switch (dir) {
611         case OB_DIRECTION_EAST:
612             if (d < screen_num_desktops - 1)
613                 ++d;
614             else if (wrap)
615                 d = 0;
616             break;
617         case OB_DIRECTION_WEST:
618             if (d > 0)
619                 --d;
620             else if (wrap)
621                 d = screen_num_desktops - 1;
622             break;
623         default:
624             assert(0);
625             return screen_desktop;
626         }
627     } else {
628         switch (dir) {
629         case OB_DIRECTION_EAST:
630             ++c;
631             if (c >= screen_desktop_layout.columns) {
632                 if (!wrap) return d = screen_desktop;
633                 c = 0;
634             }
635             d = translate_row_col(r, c);
636             if (d >= screen_num_desktops) {
637                 if (!wrap) return d = screen_desktop;
638                 ++c;
639             }
640             break;
641         case OB_DIRECTION_WEST:
642             --c;
643             if (c >= screen_desktop_layout.columns) {
644                 if (!wrap) return d = screen_desktop;
645                 c = screen_desktop_layout.columns - 1;
646             }
647             d = translate_row_col(r, c);
648             if (d >= screen_num_desktops) {
649                 if (!wrap) return d = screen_desktop;
650                 --c;
651             }
652             break;
653         case OB_DIRECTION_SOUTH:
654             ++r;
655             if (r >= screen_desktop_layout.rows) {
656                 if (!wrap) return d = screen_desktop;
657                 r = 0;
658             }
659             d = translate_row_col(r, c);
660             if (d >= screen_num_desktops) {
661                 if (!wrap) return d = screen_desktop;
662                 ++r;
663             }
664             break;
665         case OB_DIRECTION_NORTH:
666             --r;
667             if (r >= screen_desktop_layout.rows) {
668                 if (!wrap) return d = screen_desktop;
669                 r = screen_desktop_layout.rows - 1;
670             }
671             d = translate_row_col(r, c);
672             if (d >= screen_num_desktops) {
673                 if (!wrap) return d = screen_desktop;
674                 --r;
675             }
676             break;
677         default:
678             assert(0);
679             return d = screen_desktop;
680         }
681
682         d = translate_row_col(r, c);
683     }
684
685     if (dialog) {
686         popup_cycle(d, TRUE);
687         return d;
688     }
689
690 done_cycle:
691     first = TRUE;
692
693     popup_cycle(0, FALSE);
694
695     return d;
696 }
697
698 void screen_update_layout()
699 {
700     ObOrientation orient;
701     ObCorner corner;
702     guint rows;
703     guint cols;
704     guint32 *data;
705     guint num;
706     gboolean valid = FALSE;
707
708     if (PROP_GETA32(RootWindow(ob_display, ob_screen),
709                     net_desktop_layout, cardinal, &data, &num)) {
710         if (num == 3 || num == 4) {
711
712             if (data[0] == prop_atoms.net_wm_orientation_vert)
713                 orient = OB_ORIENTATION_VERT;
714             else if (data[0] == prop_atoms.net_wm_orientation_horz)
715                 orient = OB_ORIENTATION_HORZ;
716             else
717                 goto screen_update_layout_bail;
718
719             if (num < 4)
720                 corner = OB_CORNER_TOPLEFT;
721             else {
722                 if (data[3] == prop_atoms.net_wm_topleft)
723                     corner = OB_CORNER_TOPLEFT;
724                 else if (data[3] == prop_atoms.net_wm_topright)
725                     corner = OB_CORNER_TOPRIGHT;
726                 else if (data[3] == prop_atoms.net_wm_bottomright)
727                     corner = OB_CORNER_BOTTOMRIGHT;
728                 else if (data[3] == prop_atoms.net_wm_bottomleft)
729                     corner = OB_CORNER_BOTTOMLEFT;
730                 else
731                     goto screen_update_layout_bail;
732             }
733
734             /* fill in a zero rows/columns */
735             if ((data[1] == 0 && data[2] == 0) || /* both 0's is bad data.. */
736                 (data[1] != 0 && data[2] != 0)) { /* no 0's is bad data.. */
737                 goto screen_update_layout_bail;
738             } else {
739                 if (data[1] == 0) {
740                     data[1] = (screen_num_desktops +
741                                screen_num_desktops % data[2]) / data[2];
742                 } else if (data[2] == 0) {
743                     data[2] = (screen_num_desktops +
744                                screen_num_desktops % data[1]) / data[1];
745                 }
746                 cols = data[1];
747                 rows = data[2];
748             }
749
750             /* bounds checking */
751             if (orient == OB_ORIENTATION_HORZ) {
752                 rows = MIN(rows, screen_num_desktops);
753                 cols = MIN(cols, ((screen_num_desktops +
754                                      (screen_num_desktops % rows)) / rows));
755             } else {
756                 cols = MIN(cols, screen_num_desktops);
757                 rows = MIN(rows, ((screen_num_desktops +
758                                      (screen_num_desktops % cols)) / cols));
759             }
760
761             valid = TRUE;
762         }
763     screen_update_layout_bail:
764         g_free(data);
765     }
766
767     if (!valid) {
768         /* defaults */
769         orient = OB_ORIENTATION_HORZ;
770         corner = OB_CORNER_TOPLEFT;
771         rows = 1;
772         cols = screen_num_desktops;
773     }
774
775     screen_desktop_layout.orientation = orient;
776     screen_desktop_layout.start_corner = corner;
777     screen_desktop_layout.rows = rows;
778     screen_desktop_layout.columns = cols;
779 }
780
781 void screen_update_desktop_names()
782 {
783     guint i;
784
785     /* empty the array */
786     g_strfreev(screen_desktop_names);
787     screen_desktop_names = NULL;
788
789     if (PROP_GETSS(RootWindow(ob_display, ob_screen),
790                    net_desktop_names, utf8, &screen_desktop_names))
791         for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
792     else
793         i = 0;
794     if (i <= screen_num_desktops) {
795         screen_desktop_names = g_renew(char*, screen_desktop_names,
796                                        screen_num_desktops + 1);
797         screen_desktop_names[screen_num_desktops] = NULL;
798         for (; i < screen_num_desktops; ++i)
799             screen_desktop_names[i] = g_strdup("Unnamed Desktop");
800     }
801 }
802
803 void screen_show_desktop(gboolean show)
804 {
805     GList *it;
806      
807     if (show == screen_showing_desktop) return; /* no change */
808
809     screen_showing_desktop = show;
810
811     if (show) {
812         /* bottom to top */
813         for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
814             if (WINDOW_IS_CLIENT(it->data)) {
815                 ObClient *client = it->data;
816                 if (client->frame->visible && !client_should_show(client))
817                     frame_hide(client->frame);
818             }
819         }
820     } else {
821         /* top to bottom */
822         for (it = stacking_list; it != NULL; it = it->next) {
823             if (WINDOW_IS_CLIENT(it->data)) {
824                 ObClient *client = it->data;
825                 if (!client->frame->visible && client_should_show(client))
826                     frame_show(client->frame);
827             }
828         }
829     }
830
831     if (show) {
832         /* focus desktop */
833         for (it = focus_order[screen_desktop]; it; it = it->next)
834             if (((ObClient*)it->data)->type == OB_CLIENT_TYPE_DESKTOP &&
835                 client_focus(it->data))
836                 break;
837     } else {
838         focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
839     }
840
841     show = !!show; /* make it boolean */
842     PROP_SET32(RootWindow(ob_display, ob_screen),
843                net_showing_desktop, cardinal, show);
844 }
845
846 void screen_install_colormap(ObClient *client, gboolean install)
847 {
848     XWindowAttributes wa;
849
850     if (client == NULL) {
851         if (install)
852             XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
853         else
854             XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
855     } else {
856         if (XGetWindowAttributes(ob_display, client->window, &wa) &&
857             wa.colormap != None) {
858             xerror_set_ignore(TRUE);
859             if (install)
860                 XInstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
861             else
862                 XUninstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
863             xerror_set_ignore(FALSE);
864         }
865     }
866 }
867
868 void screen_update_areas()
869 {
870     guint i, x;
871     guint32 *dims;
872     GList *it;
873
874     g_free(monitor_area);
875     extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
876
877     if (area) {
878         for (i = 0; area[i]; ++i)
879             g_free(area[i]);
880         g_free(area);
881     }
882
883     area = g_new(Rect*, screen_num_desktops + 2);
884     for (i = 0; i < screen_num_desktops + 1; ++i)
885         area[i] = g_new(Rect, screen_num_monitors + 1);
886     area[i] = NULL;
887      
888     dims = g_new(guint32, 4 * screen_num_desktops);
889
890     for (i = 0; i < screen_num_desktops + 1; ++i) {
891         Strut s;
892         int l, r, t, b;
893
894         /* calc the xinerama areas */
895         for (x = 0; x < screen_num_monitors; ++x) {
896             area[i][x] = monitor_area[x];
897             if (x == 0) {
898                 l = monitor_area[x].x;
899                 t = monitor_area[x].y;
900                 r = monitor_area[x].x + monitor_area[x].width - 1;
901                 b = monitor_area[x].y + monitor_area[x].height - 1;
902             } else {
903                 l = MIN(l, monitor_area[x].x);
904                 t = MIN(t, monitor_area[x].y);
905                 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
906                 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
907             }
908         }
909         RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
910
911         /* apply struts */
912         STRUT_SET(s, 0, 0, 0, 0);
913         for (it = client_list; it; it = it->next)
914             STRUT_ADD(s, ((ObClient*)it->data)->strut);
915         STRUT_ADD(s, dock_strut);
916
917         if (s.left) {
918             int o;
919
920             /* find the left-most xin heads, i do this in 2 loops :| */
921             o = area[i][0].x;
922             for (x = 1; x < screen_num_monitors; ++x)
923                 o = MIN(o, area[i][x].x);
924
925             for (x = 0; x < screen_num_monitors; ++x) {
926                 int edge = o + s.left - area[i][x].x;
927                 if (edge > 0) {
928                     area[i][x].x += edge;
929                     area[i][x].width -= edge;
930                 }
931             }
932
933             area[i][screen_num_monitors].x += s.left;
934             area[i][screen_num_monitors].width -= s.left;
935         }
936         if (s.top) {
937             int o;
938
939             /* find the left-most xin heads, i do this in 2 loops :| */
940             o = area[i][0].y;
941             for (x = 1; x < screen_num_monitors; ++x)
942                 o = MIN(o, area[i][x].y);
943
944             for (x = 0; x < screen_num_monitors; ++x) {
945                 int edge = o + s.top - area[i][x].y;
946                 if (edge > 0) {
947                     area[i][x].y += edge;
948                     area[i][x].height -= edge;
949                 }
950             }
951
952             area[i][screen_num_monitors].y += s.top;
953             area[i][screen_num_monitors].height -= s.top;
954         }
955         if (s.right) {
956             int o;
957
958             /* find the bottom-most xin heads, i do this in 2 loops :| */
959             o = area[i][0].x + area[i][0].width - 1;
960             for (x = 1; x < screen_num_monitors; ++x)
961                 o = MAX(o, area[i][x].x + area[i][x].width - 1);
962
963             for (x = 0; x < screen_num_monitors; ++x) {
964                 int edge = (area[i][x].x + area[i][x].width - 1) -
965                     (o - s.right);
966                 if (edge > 0)
967                     area[i][x].width -= edge;
968             }
969
970             area[i][screen_num_monitors].width -= s.right;
971         }
972         if (s.bottom) {
973             int o;
974
975             /* find the bottom-most xin heads, i do this in 2 loops :| */
976             o = area[i][0].y + area[i][0].height - 1;
977             for (x = 1; x < screen_num_monitors; ++x)
978                 o = MAX(o, area[i][x].y + area[i][x].height - 1);
979
980             for (x = 0; x < screen_num_monitors; ++x) {
981                 int edge = (area[i][x].y + area[i][x].height - 1) -
982                     (o - s.bottom);
983                 if (edge > 0)
984                     area[i][x].height -= edge;
985             }
986
987             area[i][screen_num_monitors].height -= s.bottom;
988         }
989
990         /* XXX when dealing with partial struts, if its in a single
991            xinerama area, then only subtract it from that area's space
992         for (x = 0; x < screen_num_monitors; ++x) {
993             GList *it;
994
995
996                do something smart with it for the 'all xinerama areas' one...
997
998             for (it = client_list; it; it = it->next) {
999
1000                 XXX if gunna test this shit, then gotta worry about when
1001                 the client moves between xinerama heads..
1002
1003                 if (RECT_CONTAINS_RECT(((ObClient*)it->data)->frame->area,
1004                                        area[i][x])) {
1005
1006                 }            
1007             }
1008         }
1009         */
1010
1011         /* XXX optimize when this is run? */
1012
1013         /* the area has changed, adjust all the maximized 
1014            windows */
1015         for (it = client_list; it; it = it->next) {
1016             ObClient *c = it->data; 
1017             if (i < screen_num_desktops) {
1018                 if (c->desktop == i)
1019                     client_reconfigure(c);
1020             } else if (c->desktop == DESKTOP_ALL)
1021                 client_reconfigure(c);
1022         }
1023         if (i < screen_num_desktops) {
1024             /* don't set these for the 'all desktops' area */
1025             dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
1026             dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
1027             dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
1028             dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
1029         }
1030     }
1031     PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
1032                 dims, 4 * screen_num_desktops);
1033
1034     g_free(dims);
1035 }
1036
1037 Rect *screen_area(guint desktop)
1038 {
1039     return screen_area_monitor(desktop, screen_num_monitors);
1040 }
1041
1042 Rect *screen_area_monitor(guint desktop, guint head)
1043 {
1044     if (head > screen_num_monitors)
1045         return NULL;
1046     if (desktop >= screen_num_desktops) {
1047         if (desktop == DESKTOP_ALL)
1048             return &area[screen_num_desktops][head];
1049         return NULL;
1050     }
1051     return &area[desktop][head];
1052 }
1053
1054 Rect *screen_physical_area()
1055 {
1056     return screen_physical_area_monitor(screen_num_monitors);
1057 }
1058
1059 Rect *screen_physical_area_monitor(guint head)
1060 {
1061     if (head > screen_num_monitors)
1062         return NULL;
1063     return &monitor_area[head];
1064 }
1065
1066 static void set_root_cursor()
1067 {
1068 #ifdef USE_LIBSN
1069         if (sn_busy_cnt)
1070             XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1071                           ob_cursor(OB_CURSOR_BUSY));
1072         else
1073 #endif
1074             XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1075                           ob_cursor(OB_CURSOR_POINTER));
1076 }
1077
1078 #ifdef USE_LIBSN
1079 static gboolean sn_timeout(gpointer data)
1080 {
1081     sn_busy_cnt = 0;
1082
1083     set_root_cursor();
1084
1085     return FALSE; /* don't repeat */
1086 }
1087
1088 static void sn_event_func(SnMonitorEvent *ev, void *data)
1089 {
1090     SnStartupSequence *seq;
1091     const char *seq_id, *bin_name;
1092     int cnt = sn_busy_cnt;
1093
1094     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
1095         return;
1096
1097     seq_id = sn_startup_sequence_get_id(seq);
1098     bin_name = sn_startup_sequence_get_binary_name(seq);
1099     
1100     if (!(seq_id && bin_name))
1101         return;
1102
1103     switch (sn_monitor_event_get_type(ev)) {
1104     case SN_MONITOR_EVENT_INITIATED:
1105         ++sn_busy_cnt;
1106         ob_main_loop_timeout_remove(ob_main_loop, sn_timeout);
1107         /* 30 second timeout for apps to start */
1108         ob_main_loop_timeout_add(ob_main_loop, 30 * G_USEC_PER_SEC,
1109                                  sn_timeout, NULL, NULL);
1110         break;
1111     case SN_MONITOR_EVENT_CHANGED:
1112         break;
1113     case SN_MONITOR_EVENT_COMPLETED:
1114         if (sn_busy_cnt) --sn_busy_cnt;
1115         ob_main_loop_timeout_remove(ob_main_loop, sn_timeout);
1116         break;
1117     case SN_MONITOR_EVENT_CANCELED:
1118         if (sn_busy_cnt) --sn_busy_cnt;
1119         ob_main_loop_timeout_remove(ob_main_loop, sn_timeout);
1120     };
1121
1122     if (sn_busy_cnt != cnt)
1123         set_root_cursor();
1124 }
1125 #endif
1126
1127 gboolean screen_pointer_pos(int *x, int *y)
1128 {
1129     Window w;
1130     int i;
1131     guint u;
1132
1133     return !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
1134                            &w, &w, x, y, &i, &i, &u);
1135 }