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