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