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