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