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