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