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