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