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