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