make openbox base-dir spec compliant, and change the theme dir structure, so that...
[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
439 static void get_row_col(guint d, guint *r, guint *c)
440 {
441     switch (screen_desktop_layout.orientation) {
442     case OB_ORIENTATION_HORZ:
443         switch (screen_desktop_layout.start_corner) {
444         case OB_CORNER_TOPLEFT:
445             *r = d / screen_desktop_layout.columns;
446             *c = d % screen_desktop_layout.columns;
447             break;
448         case OB_CORNER_BOTTOMLEFT:
449             *r = screen_desktop_layout.rows - 1 -
450                 d / screen_desktop_layout.columns;
451             *c = d % screen_desktop_layout.columns;
452             break;
453         case OB_CORNER_TOPRIGHT:
454             *r = d / screen_desktop_layout.columns;
455             *c = screen_desktop_layout.columns - 1 -
456                 d % screen_desktop_layout.columns;
457             break;
458         case OB_CORNER_BOTTOMRIGHT:
459             *r = screen_desktop_layout.rows - 1 -
460                 d / screen_desktop_layout.columns;
461             *c = screen_desktop_layout.columns - 1 -
462                 d % screen_desktop_layout.columns;
463             break;
464         }
465         break;
466     case OB_ORIENTATION_VERT:
467         switch (screen_desktop_layout.start_corner) {
468         case OB_CORNER_TOPLEFT:
469             *r = d % screen_desktop_layout.rows;
470             *c = d / screen_desktop_layout.rows;
471             break;
472         case OB_CORNER_BOTTOMLEFT:
473             *r = screen_desktop_layout.rows - 1 -
474                 d % screen_desktop_layout.rows;
475             *c = d / screen_desktop_layout.rows;
476             break;
477         case OB_CORNER_TOPRIGHT:
478             *r = d % screen_desktop_layout.rows;
479             *c = screen_desktop_layout.columns - 1 -
480                 d / screen_desktop_layout.rows;
481             break;
482         case OB_CORNER_BOTTOMRIGHT:
483             *r = screen_desktop_layout.rows - 1 -
484                 d % screen_desktop_layout.rows;
485             *c = screen_desktop_layout.columns - 1 -
486                 d / screen_desktop_layout.rows;
487             break;
488         }
489         break;
490     }
491 }
492
493 static guint translate_row_col(guint r, guint c)
494 {
495     switch (screen_desktop_layout.orientation) {
496     case OB_ORIENTATION_HORZ:
497         switch (screen_desktop_layout.start_corner) {
498         case OB_CORNER_TOPLEFT:
499             return r % screen_desktop_layout.rows *
500                 screen_desktop_layout.columns +
501                 c % screen_desktop_layout.columns;
502         case OB_CORNER_BOTTOMLEFT:
503             return (screen_desktop_layout.rows - 1 -
504                     r % screen_desktop_layout.rows) *
505                 screen_desktop_layout.columns +
506                 c % screen_desktop_layout.columns;
507         case OB_CORNER_TOPRIGHT:
508             return r % screen_desktop_layout.rows *
509                 screen_desktop_layout.columns +
510                 (screen_desktop_layout.columns - 1 -
511                  c % screen_desktop_layout.columns);
512         case OB_CORNER_BOTTOMRIGHT:
513             return (screen_desktop_layout.rows - 1 -
514                     r % screen_desktop_layout.rows) *
515                 screen_desktop_layout.columns +
516                 (screen_desktop_layout.columns - 1 -
517                  c % screen_desktop_layout.columns);
518         }
519     case OB_ORIENTATION_VERT:
520         switch (screen_desktop_layout.start_corner) {
521         case OB_CORNER_TOPLEFT:
522             return c % screen_desktop_layout.columns *
523                 screen_desktop_layout.rows +
524                 r % screen_desktop_layout.rows;
525         case OB_CORNER_BOTTOMLEFT:
526             return c % screen_desktop_layout.columns *
527                 screen_desktop_layout.rows +
528                 (screen_desktop_layout.rows - 1 -
529                  r % screen_desktop_layout.rows);
530         case OB_CORNER_TOPRIGHT:
531             return (screen_desktop_layout.columns - 1 -
532                     c % screen_desktop_layout.columns) *
533                 screen_desktop_layout.rows +
534                 r % screen_desktop_layout.rows;
535         case OB_CORNER_BOTTOMRIGHT:
536             return (screen_desktop_layout.columns - 1 -
537                     c % screen_desktop_layout.columns) *
538                 screen_desktop_layout.rows +
539                 (screen_desktop_layout.rows - 1 -
540                  r % screen_desktop_layout.rows);
541         }
542     }
543     g_assert_not_reached();
544     return 0;
545 }
546
547 static void popup_cycle(guint d, gboolean show)
548 {
549     Rect *a;
550
551     if (!show) {
552         pager_popup_hide(desktop_cycle_popup);
553     } else {
554         a = screen_physical_area_monitor(0);
555         pager_popup_position(desktop_cycle_popup, CenterGravity,
556                              a->x + a->width / 2, a->y + a->height / 2);
557         /* XXX the size and the font extents need to be related on some level
558          */
559         pager_popup_size(desktop_cycle_popup, POPUP_WIDTH, POPUP_HEIGHT);
560
561         pager_popup_set_text_align(desktop_cycle_popup, RR_JUSTIFY_CENTER);
562
563         pager_popup_show(desktop_cycle_popup, screen_desktop_names[d], d);
564     }
565 }
566
567 guint screen_cycle_desktop(ObDirection dir, gboolean wrap, gboolean linear,
568                            gboolean dialog, gboolean done, gboolean cancel)
569 {
570     static gboolean first = TRUE;
571     static gboolean lin;
572     static guint origd, d;
573     guint r, c;
574
575     if (cancel) {
576         d = origd;
577         goto done_cycle;
578     } else if (done && dialog) {
579         goto done_cycle;
580     }
581     if (first) {
582         first = FALSE;
583         lin = linear;
584         d = origd = screen_desktop;
585     }
586
587     get_row_col(d, &r, &c);
588
589     if (lin) {
590         switch (dir) {
591         case OB_DIRECTION_EAST:
592             if (d < screen_num_desktops - 1)
593                 ++d;
594             else if (wrap)
595                 d = 0;
596             break;
597         case OB_DIRECTION_WEST:
598             if (d > 0)
599                 --d;
600             else if (wrap)
601                 d = screen_num_desktops - 1;
602             break;
603         default:
604             assert(0);
605             return screen_desktop;
606         }
607     } else {
608         switch (dir) {
609         case OB_DIRECTION_EAST:
610             ++c;
611             if (c >= screen_desktop_layout.columns) {
612                 if (!wrap) return d = screen_desktop;
613                 c = 0;
614             }
615             d = translate_row_col(r, c);
616             if (d >= screen_num_desktops) {
617                 if (!wrap) return d = screen_desktop;
618                 ++c;
619             }
620             break;
621         case OB_DIRECTION_WEST:
622             --c;
623             if (c >= screen_desktop_layout.columns) {
624                 if (!wrap) return d = screen_desktop;
625                 c = screen_desktop_layout.columns - 1;
626             }
627             d = translate_row_col(r, c);
628             if (d >= screen_num_desktops) {
629                 if (!wrap) return d = screen_desktop;
630                 --c;
631             }
632             break;
633         case OB_DIRECTION_SOUTH:
634             ++r;
635             if (r >= screen_desktop_layout.rows) {
636                 if (!wrap) return d = screen_desktop;
637                 r = 0;
638             }
639             d = translate_row_col(r, c);
640             if (d >= screen_num_desktops) {
641                 if (!wrap) return d = screen_desktop;
642                 ++r;
643             }
644             break;
645         case OB_DIRECTION_NORTH:
646             --r;
647             if (r >= screen_desktop_layout.rows) {
648                 if (!wrap) return d = screen_desktop;
649                 r = screen_desktop_layout.rows - 1;
650             }
651             d = translate_row_col(r, c);
652             if (d >= screen_num_desktops) {
653                 if (!wrap) return d = screen_desktop;
654                 --r;
655             }
656             break;
657         default:
658             assert(0);
659             return d = screen_desktop;
660         }
661
662         d = translate_row_col(r, c);
663     }
664
665     if (dialog) {
666         popup_cycle(d, TRUE);
667         return d;
668     }
669
670 done_cycle:
671     first = TRUE;
672
673     popup_cycle(0, FALSE);
674
675     return d;
676 }
677
678 void screen_update_layout()
679 {
680     ObOrientation orient;
681     ObCorner corner;
682     guint rows;
683     guint cols;
684     guint32 *data;
685     guint num;
686     gboolean valid = FALSE;
687
688     if (PROP_GETA32(RootWindow(ob_display, ob_screen),
689                     net_desktop_layout, cardinal, &data, &num)) {
690         if (num == 3 || num == 4) {
691
692             if (data[0] == prop_atoms.net_wm_orientation_vert)
693                 orient = OB_ORIENTATION_VERT;
694             else if (data[0] == prop_atoms.net_wm_orientation_horz)
695                 orient = OB_ORIENTATION_HORZ;
696             else
697                 goto screen_update_layout_bail;
698
699             if (num < 4)
700                 corner = OB_CORNER_TOPLEFT;
701             else {
702                 if (data[3] == prop_atoms.net_wm_topleft)
703                     corner = OB_CORNER_TOPLEFT;
704                 else if (data[3] == prop_atoms.net_wm_topright)
705                     corner = OB_CORNER_TOPRIGHT;
706                 else if (data[3] == prop_atoms.net_wm_bottomright)
707                     corner = OB_CORNER_BOTTOMRIGHT;
708                 else if (data[3] == prop_atoms.net_wm_bottomleft)
709                     corner = OB_CORNER_BOTTOMLEFT;
710                 else
711                     goto screen_update_layout_bail;
712             }
713
714             cols = data[1];
715             rows = data[2];
716
717             /* fill in a zero rows/columns */
718             if ((cols == 0 && rows == 0)) { /* both 0's is bad data.. */
719                 goto screen_update_layout_bail;
720             } else {
721                 if (cols == 0) {
722                     cols = screen_num_desktops / rows;
723                     if (rows * cols < screen_num_desktops)
724                         cols++;
725                     if (rows * cols >= screen_num_desktops + cols)
726                         rows--;
727                 } else if (rows == 0) {
728                     rows = screen_num_desktops / rows;
729                     if (cols * rows < screen_num_desktops)
730                         rows++;
731                     if (cols * rows >= screen_num_desktops + rows)
732                         cols--;
733                 }
734             }
735
736             /* bounds checking */
737             if (orient == OB_ORIENTATION_HORZ) {
738                 cols = MIN(screen_num_desktops, cols);
739                 rows = MIN(rows, (screen_num_desktops + cols - 1) / cols);
740                 cols = screen_num_desktops / rows +
741                     !!(screen_num_desktops % rows);
742             } else {
743                 rows = MIN(screen_num_desktops, rows);
744                 cols = MIN(cols, (screen_num_desktops + rows - 1) / rows);
745                 rows = screen_num_desktops / cols +
746                     !!(screen_num_desktops % cols);
747             }
748
749             valid = TRUE;
750         }
751     screen_update_layout_bail:
752         g_free(data);
753     }
754
755     if (!valid) {
756         /* defaults */
757         orient = OB_ORIENTATION_HORZ;
758         corner = OB_CORNER_TOPLEFT;
759         rows = 1;
760         cols = screen_num_desktops;
761     }
762
763     screen_desktop_layout.orientation = orient;
764     screen_desktop_layout.start_corner = corner;
765     screen_desktop_layout.rows = rows;
766     screen_desktop_layout.columns = cols;
767 }
768
769 void screen_update_desktop_names()
770 {
771     guint i;
772
773     /* empty the array */
774     g_strfreev(screen_desktop_names);
775     screen_desktop_names = NULL;
776
777     if (PROP_GETSS(RootWindow(ob_display, ob_screen),
778                    net_desktop_names, utf8, &screen_desktop_names))
779         for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
780     else
781         i = 0;
782     if (i <= screen_num_desktops) {
783         screen_desktop_names = g_renew(char*, screen_desktop_names,
784                                        screen_num_desktops + 1);
785         screen_desktop_names[screen_num_desktops] = NULL;
786         for (; i < screen_num_desktops; ++i)
787             screen_desktop_names[i] = g_strdup("Unnamed Desktop");
788     }
789 }
790
791 void screen_show_desktop(gboolean show)
792 {
793     GList *it;
794      
795     if (show == screen_showing_desktop) return; /* no change */
796
797     screen_showing_desktop = show;
798
799     if (show) {
800         /* bottom to top */
801         for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
802             if (WINDOW_IS_CLIENT(it->data)) {
803                 ObClient *client = it->data;
804                 if (client->frame->visible && !client_should_show(client))
805                     frame_hide(client->frame);
806             }
807         }
808     } else {
809         /* top to bottom */
810         for (it = stacking_list; it != NULL; it = it->next) {
811             if (WINDOW_IS_CLIENT(it->data)) {
812                 ObClient *client = it->data;
813                 if (!client->frame->visible && client_should_show(client))
814                     frame_show(client->frame);
815             }
816         }
817     }
818
819     if (show) {
820         /* focus desktop */
821         for (it = focus_order[screen_desktop]; it; it = it->next)
822             if (((ObClient*)it->data)->type == OB_CLIENT_TYPE_DESKTOP &&
823                 client_focus(it->data))
824                 break;
825     } else {
826         focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
827     }
828
829     show = !!show; /* make it boolean */
830     PROP_SET32(RootWindow(ob_display, ob_screen),
831                net_showing_desktop, cardinal, show);
832 }
833
834 void screen_install_colormap(ObClient *client, gboolean install)
835 {
836     XWindowAttributes wa;
837
838     if (client == NULL) {
839         if (install)
840             XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
841         else
842             XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
843     } else {
844         if (XGetWindowAttributes(ob_display, client->window, &wa) &&
845             wa.colormap != None) {
846             xerror_set_ignore(TRUE);
847             if (install)
848                 XInstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
849             else
850                 XUninstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
851             xerror_set_ignore(FALSE);
852         }
853     }
854 }
855
856 void screen_update_areas()
857 {
858     guint i, x;
859     guint32 *dims;
860     GList *it;
861
862     g_free(monitor_area);
863     extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
864
865     if (area) {
866         for (i = 0; area[i]; ++i)
867             g_free(area[i]);
868         g_free(area);
869     }
870
871     area = g_new(Rect*, screen_num_desktops + 2);
872     for (i = 0; i < screen_num_desktops + 1; ++i)
873         area[i] = g_new(Rect, screen_num_monitors + 1);
874     area[i] = NULL;
875      
876     dims = g_new(guint32, 4 * screen_num_desktops);
877
878     for (i = 0; i < screen_num_desktops + 1; ++i) {
879         Strut s;
880         int l, r, t, b;
881
882         /* calc the xinerama areas */
883         for (x = 0; x < screen_num_monitors; ++x) {
884             area[i][x] = monitor_area[x];
885             if (x == 0) {
886                 l = monitor_area[x].x;
887                 t = monitor_area[x].y;
888                 r = monitor_area[x].x + monitor_area[x].width - 1;
889                 b = monitor_area[x].y + monitor_area[x].height - 1;
890             } else {
891                 l = MIN(l, monitor_area[x].x);
892                 t = MIN(t, monitor_area[x].y);
893                 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
894                 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
895             }
896         }
897         RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
898
899         /* apply struts */
900         STRUT_SET(s, 0, 0, 0, 0);
901         for (it = client_list; it; it = it->next)
902             STRUT_ADD(s, ((ObClient*)it->data)->strut);
903         STRUT_ADD(s, dock_strut);
904
905         if (s.left) {
906             int o;
907
908             /* find the left-most xin heads, i do this in 2 loops :| */
909             o = area[i][0].x;
910             for (x = 1; x < screen_num_monitors; ++x)
911                 o = MIN(o, area[i][x].x);
912
913             for (x = 0; x < screen_num_monitors; ++x) {
914                 int edge = o + s.left - area[i][x].x;
915                 if (edge > 0) {
916                     area[i][x].x += edge;
917                     area[i][x].width -= edge;
918                 }
919             }
920
921             area[i][screen_num_monitors].x += s.left;
922             area[i][screen_num_monitors].width -= s.left;
923         }
924         if (s.top) {
925             int o;
926
927             /* find the left-most xin heads, i do this in 2 loops :| */
928             o = area[i][0].y;
929             for (x = 1; x < screen_num_monitors; ++x)
930                 o = MIN(o, area[i][x].y);
931
932             for (x = 0; x < screen_num_monitors; ++x) {
933                 int edge = o + s.top - area[i][x].y;
934                 if (edge > 0) {
935                     area[i][x].y += edge;
936                     area[i][x].height -= edge;
937                 }
938             }
939
940             area[i][screen_num_monitors].y += s.top;
941             area[i][screen_num_monitors].height -= s.top;
942         }
943         if (s.right) {
944             int o;
945
946             /* find the bottom-most xin heads, i do this in 2 loops :| */
947             o = area[i][0].x + area[i][0].width - 1;
948             for (x = 1; x < screen_num_monitors; ++x)
949                 o = MAX(o, area[i][x].x + area[i][x].width - 1);
950
951             for (x = 0; x < screen_num_monitors; ++x) {
952                 int edge = (area[i][x].x + area[i][x].width - 1) -
953                     (o - s.right);
954                 if (edge > 0)
955                     area[i][x].width -= edge;
956             }
957
958             area[i][screen_num_monitors].width -= s.right;
959         }
960         if (s.bottom) {
961             int o;
962
963             /* find the bottom-most xin heads, i do this in 2 loops :| */
964             o = area[i][0].y + area[i][0].height - 1;
965             for (x = 1; x < screen_num_monitors; ++x)
966                 o = MAX(o, area[i][x].y + area[i][x].height - 1);
967
968             for (x = 0; x < screen_num_monitors; ++x) {
969                 int edge = (area[i][x].y + area[i][x].height - 1) -
970                     (o - s.bottom);
971                 if (edge > 0)
972                     area[i][x].height -= edge;
973             }
974
975             area[i][screen_num_monitors].height -= s.bottom;
976         }
977
978         /* XXX when dealing with partial struts, if its in a single
979            xinerama area, then only subtract it from that area's space
980         for (x = 0; x < screen_num_monitors; ++x) {
981             GList *it;
982
983
984                do something smart with it for the 'all xinerama areas' one...
985
986             for (it = client_list; it; it = it->next) {
987
988                 XXX if gunna test this shit, then gotta worry about when
989                 the client moves between xinerama heads..
990
991                 if (RECT_CONTAINS_RECT(((ObClient*)it->data)->frame->area,
992                                        area[i][x])) {
993
994                 }            
995             }
996         }
997         */
998
999         /* XXX optimize when this is run? */
1000
1001         /* the area has changed, adjust all the maximized 
1002            windows */
1003         for (it = client_list; it; it = it->next) {
1004             ObClient *c = it->data; 
1005             if (i < screen_num_desktops) {
1006                 if (c->desktop == i)
1007                     client_reconfigure(c);
1008             } else if (c->desktop == DESKTOP_ALL)
1009                 client_reconfigure(c);
1010         }
1011         if (i < screen_num_desktops) {
1012             /* don't set these for the 'all desktops' area */
1013             dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
1014             dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
1015             dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
1016             dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
1017         }
1018     }
1019     PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
1020                 dims, 4 * screen_num_desktops);
1021
1022     g_free(dims);
1023 }
1024
1025 Rect *screen_area(guint desktop)
1026 {
1027     return screen_area_monitor(desktop, screen_num_monitors);
1028 }
1029
1030 Rect *screen_area_monitor(guint desktop, guint head)
1031 {
1032     if (head > screen_num_monitors)
1033         return NULL;
1034     if (desktop >= screen_num_desktops) {
1035         if (desktop == DESKTOP_ALL)
1036             return &area[screen_num_desktops][head];
1037         return NULL;
1038     }
1039     return &area[desktop][head];
1040 }
1041
1042 Rect *screen_physical_area()
1043 {
1044     return screen_physical_area_monitor(screen_num_monitors);
1045 }
1046
1047 Rect *screen_physical_area_monitor(guint head)
1048 {
1049     if (head > screen_num_monitors)
1050         return NULL;
1051     return &monitor_area[head];
1052 }
1053
1054 void screen_set_root_cursor()
1055 {
1056     if (sn_app_starting())
1057         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1058                       ob_cursor(OB_CURSOR_BUSY));
1059     else
1060         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1061                       ob_cursor(OB_CURSOR_POINTER));
1062 }
1063
1064 gboolean screen_pointer_pos(int *x, int *y)
1065 {
1066     Window w;
1067     int i;
1068     guint u;
1069
1070     return !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
1071                            &w, &w, x, y, &i, &i, &u);
1072 }