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