]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/screen.c
dont ignore topleft layouts
[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     screen_update_areas();
357     dock_configure();
358
359     for (it = client_list; it; it = it->next)
360         client_move_onscreen(it->data, FALSE);
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_topleft)
491                     corner = OB_CORNER_TOPLEFT;
492                 else if (data[3] == prop_atoms.net_wm_topright)
493                     corner = OB_CORNER_TOPRIGHT;
494                 else if (data[3] == prop_atoms.net_wm_bottomright)
495                     corner = OB_CORNER_BOTTOMRIGHT;
496                 else if (data[3] == prop_atoms.net_wm_bottomleft)
497                     corner = OB_CORNER_BOTTOMLEFT;
498                 else
499                     goto screen_update_layout_bail;
500             }
501
502             /* fill in a zero rows/columns */
503             if ((data[1] == 0 && data[2] == 0) || /* both 0's is bad data.. */
504                 (data[1] != 0 && data[2] != 0)) { /* no 0's is bad data.. */
505                 goto screen_update_layout_bail;
506             } else {
507                 if (data[1] == 0) {
508                     data[1] = (screen_num_desktops +
509                                screen_num_desktops % data[2]) / data[2];
510                 } else if (data[2] == 0) {
511                     data[2] = (screen_num_desktops +
512                                screen_num_desktops % data[1]) / data[1];
513                 }
514                 cols = data[1];
515                 rows = data[2];
516             }
517
518             /* bounds checking */
519             if (orient == OB_ORIENTATION_HORZ) {
520                 rows = MIN(rows, screen_num_desktops);
521                 cols = MIN(cols, ((screen_num_desktops +
522                                      (screen_num_desktops % rows)) / rows));
523             } else {
524                 cols = MIN(cols, screen_num_desktops);
525                 rows = MIN(rows, ((screen_num_desktops +
526                                      (screen_num_desktops % cols)) / cols));
527             }
528
529             valid = TRUE;
530         }
531     screen_update_layout_bail:
532         g_free(data);
533     }
534
535     if (!valid) {
536         /* defaults */
537         orient = OB_ORIENTATION_HORZ;
538         corner = OB_CORNER_TOPLEFT;
539         rows = 1;
540         cols = screen_num_desktops;
541     }
542
543     screen_desktop_layout.orientation = orient;
544     screen_desktop_layout.start_corner = corner;
545     screen_desktop_layout.rows = rows;
546     screen_desktop_layout.columns = cols;
547 }
548
549 void screen_update_desktop_names()
550 {
551     guint i;
552
553     /* empty the array */
554     g_strfreev(screen_desktop_names);
555     screen_desktop_names = NULL;
556
557     if (PROP_GETSS(RootWindow(ob_display, ob_screen),
558                    net_desktop_names, utf8, &screen_desktop_names))
559         for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
560     else
561         i = 0;
562     if (i <= screen_num_desktops) {
563         screen_desktop_names = g_renew(char*, screen_desktop_names,
564                                        screen_num_desktops + 1);
565         screen_desktop_names[screen_num_desktops] = NULL;
566         for (; i < screen_num_desktops; ++i)
567             screen_desktop_names[i] = g_strdup("Unnamed Desktop");
568     }
569 }
570
571 void screen_show_desktop(gboolean show)
572 {
573     GList *it;
574      
575     if (show == screen_showing_desktop) return; /* no change */
576
577     screen_showing_desktop = show;
578
579     if (show) {
580         /* bottom to top */
581         for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
582             if (WINDOW_IS_CLIENT(it->data)) {
583                 ObClient *client = it->data;
584                 if (client->frame->visible && !client_should_show(client))
585                     frame_hide(client->frame);
586             }
587         }
588     } else {
589         /* top to bottom */
590         for (it = stacking_list; it != NULL; it = it->next) {
591             if (WINDOW_IS_CLIENT(it->data)) {
592                 ObClient *client = it->data;
593                 if (!client->frame->visible && client_should_show(client))
594                     frame_show(client->frame);
595             }
596         }
597     }
598
599     if (show) {
600         /* focus desktop */
601         for (it = focus_order[screen_desktop]; it; it = it->next)
602             if (((ObClient*)it->data)->type == OB_CLIENT_TYPE_DESKTOP &&
603                 client_focus(it->data))
604                 break;
605     } else {
606         focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
607     }
608
609     show = !!show; /* make it boolean */
610     PROP_SET32(RootWindow(ob_display, ob_screen),
611                net_showing_desktop, cardinal, show);
612
613     dispatch_ob(Event_Ob_ShowDesktop, show, 0);
614 }
615
616 void screen_install_colormap(ObClient *client, gboolean install)
617 {
618     XWindowAttributes wa;
619
620     if (client == NULL) {
621         if (install)
622             XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
623         else
624             XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
625     } else {
626         if (XGetWindowAttributes(ob_display, client->window, &wa) &&
627             wa.colormap != None) {
628             xerror_set_ignore(TRUE);
629             if (install)
630                 XInstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
631             else
632                 XUninstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
633             xerror_set_ignore(FALSE);
634         }
635     }
636 }
637
638 void screen_update_areas()
639 {
640     guint i, x;
641     guint32 *dims;
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     for (i = 0; i < screen_num_desktops + 1; ++i) {
661         Strut s;
662         int l, r, t, b;
663
664         /* calc the xinerama areas */
665         for (x = 0; x < screen_num_monitors; ++x) {
666             area[i][x] = monitor_area[x];
667             if (x == 0) {
668                 l = monitor_area[x].x;
669                 t = monitor_area[x].y;
670                 r = monitor_area[x].x + monitor_area[x].width - 1;
671                 b = monitor_area[x].y + monitor_area[x].height - 1;
672             } else {
673                 l = MIN(l, monitor_area[x].x);
674                 t = MIN(t, monitor_area[x].y);
675                 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
676                 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
677             }
678         }
679         RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
680
681         /* apply struts */
682         STRUT_SET(s, 0, 0, 0, 0);
683         for (it = client_list; it; it = it->next)
684             STRUT_ADD(s, ((ObClient*)it->data)->strut);
685         STRUT_ADD(s, dock_strut);
686
687         if (s.left) {
688             int o;
689
690             /* find the left-most xin heads, i do this in 2 loops :| */
691             o = area[i][0].x;
692             for (x = 1; x < screen_num_monitors; ++x)
693                 o = MIN(o, area[i][x].x);
694
695             for (x = 0; x < screen_num_monitors; ++x) {
696                 int edge = o + s.left - area[i][x].x;
697                 if (edge > 0) {
698                     area[i][x].x += edge;
699                     area[i][x].width -= edge;
700                 }
701             }
702
703             area[i][screen_num_monitors].x += s.left;
704             area[i][screen_num_monitors].width -= s.left;
705         }
706         if (s.top) {
707             int o;
708
709             /* find the left-most xin heads, i do this in 2 loops :| */
710             o = area[i][0].y;
711             for (x = 1; x < screen_num_monitors; ++x)
712                 o = MIN(o, area[i][x].y);
713
714             for (x = 0; x < screen_num_monitors; ++x) {
715                 int edge = o + s.top - area[i][x].y;
716                 if (edge > 0) {
717                     area[i][x].y += edge;
718                     area[i][x].height -= edge;
719                 }
720             }
721
722             area[i][screen_num_monitors].y += s.top;
723             area[i][screen_num_monitors].height -= s.top;
724         }
725         if (s.right) {
726             int o;
727
728             /* find the bottom-most xin heads, i do this in 2 loops :| */
729             o = area[i][0].x + area[i][0].width - 1;
730             for (x = 1; x < screen_num_monitors; ++x)
731                 o = MAX(o, area[i][x].x + area[i][x].width - 1);
732
733             for (x = 0; x < screen_num_monitors; ++x) {
734                 int edge = (area[i][x].x + area[i][x].width - 1) -
735                     (o - s.right);
736                 if (edge > 0)
737                     area[i][x].width -= edge;
738             }
739
740             area[i][screen_num_monitors].width -= s.right;
741         }
742         if (s.bottom) {
743             int o;
744
745             /* find the bottom-most xin heads, i do this in 2 loops :| */
746             o = area[i][0].y + area[i][0].height - 1;
747             for (x = 1; x < screen_num_monitors; ++x)
748                 o = MAX(o, area[i][x].y + area[i][x].height - 1);
749
750             for (x = 0; x < screen_num_monitors; ++x) {
751                 int edge = (area[i][x].y + area[i][x].height - 1) -
752                     (o - s.bottom);
753                 if (edge > 0)
754                     area[i][x].height -= edge;
755             }
756
757             area[i][screen_num_monitors].height -= s.bottom;
758         }
759
760         /* XXX when dealing with partial struts, if its in a single
761            xinerama area, then only subtract it from that area's space
762         for (x = 0; x < screen_num_monitors; ++x) {
763             GList *it;
764
765
766                do something smart with it for the 'all xinerama areas' one...
767
768             for (it = client_list; it; it = it->next) {
769
770                 XXX if gunna test this shit, then gotta worry about when
771                 the client moves between xinerama heads..
772
773                 if (RECT_CONTAINS_RECT(((ObClient*)it->data)->frame->area,
774                                        area[i][x])) {
775
776                 }            
777             }
778         }
779         */
780
781         /* XXX optimize when this is run? */
782
783         /* the area has changed, adjust all the maximized 
784            windows */
785         for (it = client_list; it; it = it->next) {
786             ObClient *c = it->data; 
787             if (i < screen_num_desktops) {
788                 if (c->desktop == i)
789                     client_reconfigure(c);
790             } else if (c->desktop == DESKTOP_ALL)
791                 client_reconfigure(c);
792         }
793         if (i < screen_num_desktops) {
794             /* don't set these for the 'all desktops' area */
795             dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
796             dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
797             dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
798             dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
799         }
800     }
801     PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
802                 dims, 4 * screen_num_desktops);
803
804     g_free(dims);
805 }
806
807 Rect *screen_area(guint desktop)
808 {
809     return screen_area_monitor(desktop, screen_num_monitors);
810 }
811
812 Rect *screen_area_monitor(guint desktop, guint head)
813 {
814     if (head > screen_num_monitors)
815         return NULL;
816     if (desktop >= screen_num_desktops) {
817         if (desktop == DESKTOP_ALL)
818             return &area[screen_num_desktops][head];
819         return NULL;
820     }
821     return &area[desktop][head];
822 }
823
824 Rect *screen_physical_area()
825 {
826     return screen_physical_area_monitor(screen_num_monitors);
827 }
828
829 Rect *screen_physical_area_monitor(guint head)
830 {
831     if (head > screen_num_monitors)
832         return NULL;
833     return &monitor_area[head];
834 }
835
836 static void set_root_cursor()
837 {
838 #ifdef USE_LIBSN
839         if (sn_busy_cnt)
840             XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
841                           ob_cursor(OB_CURSOR_BUSY));
842         else
843 #endif
844             XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
845                           ob_cursor(OB_CURSOR_POINTER));
846 }
847
848 #ifdef USE_LIBSN
849 static void sn_timeout(void *data)
850 {
851     timer_stop(sn_timer);
852     sn_timer = NULL;
853     sn_busy_cnt = 0;
854
855     set_root_cursor();
856 }
857
858 static void sn_event_func(SnMonitorEvent *ev, void *data)
859 {
860     SnStartupSequence *seq;
861     const char *seq_id, *bin_name;
862     int cnt = sn_busy_cnt;
863
864     if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
865         return;
866
867     seq_id = sn_startup_sequence_get_id(seq);
868     bin_name = sn_startup_sequence_get_binary_name(seq);
869     
870     if (!(seq_id && bin_name))
871         return;
872
873     switch (sn_monitor_event_get_type(ev)) {
874     case SN_MONITOR_EVENT_INITIATED:
875         ++sn_busy_cnt;
876         if (sn_timer)
877             timer_stop(sn_timer);
878         /* 30 second timeout for apps to start */
879         sn_timer = timer_start(30 * 1000000, sn_timeout, NULL);
880         break;
881     case SN_MONITOR_EVENT_CHANGED:
882         break;
883     case SN_MONITOR_EVENT_COMPLETED:
884         if (sn_busy_cnt) --sn_busy_cnt;
885         if (sn_timer) {
886             timer_stop(sn_timer);
887             sn_timer = NULL;
888         }
889         break;
890     case SN_MONITOR_EVENT_CANCELED:
891         if (sn_busy_cnt) --sn_busy_cnt;
892         if (sn_timer) {
893             timer_stop(sn_timer);
894             sn_timer = NULL;
895         }
896     };
897
898     if (sn_busy_cnt != cnt)
899         set_root_cursor();
900 }
901 #endif
902
903 gboolean screen_pointer_pos(int *x, int *y)
904 {
905     Window w;
906     int i;
907     guint u;
908
909     return !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
910                            &w, &w, x, y, &i, &i, &u);
911 }