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