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