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