Merge branch 'backport' into work
[mikachu/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 "grab.h"
24 #include "startupnotify.h"
25 #include "moveresize.h"
26 #include "config.h"
27 #include "screen.h"
28 #include "client.h"
29 #include "session.h"
30 #include "frame.h"
31 #include "event.h"
32 #include "focus.h"
33 #include "focus_cycle.h"
34 #include "popup.h"
35 #include "obrender/render.h"
36 #include "gettext.h"
37 #include "obt/display.h"
38 #include "obt/prop.h"
39 #include "obt/mainloop.h"
40
41 #include <X11/Xlib.h>
42 #ifdef HAVE_UNISTD_H
43 #  include <sys/types.h>
44 #  include <unistd.h>
45 #endif
46 #include <assert.h>
47
48 /*! The event mask to grab on the root window */
49 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
50                         EnterWindowMask | LeaveWindowMask | \
51                         SubstructureRedirectMask | FocusChangeMask | \
52                         ButtonPressMask | ButtonReleaseMask)
53
54 static gboolean screen_validate_layout(ObDesktopLayout *l);
55 static gboolean replace_wm(void);
56 static void     screen_tell_ksplash(void);
57 static void     screen_fallback_focus(void);
58
59 guint           screen_num_desktops;
60 guint           screen_num_monitors;
61 guint           screen_desktop;
62 guint           screen_last_desktop;
63 gboolean        screen_showing_desktop;
64 ObDesktopLayout screen_desktop_layout;
65 gchar         **screen_desktop_names;
66 Window          screen_support_win;
67 Time            screen_desktop_user_time = CurrentTime;
68
69 static Size     screen_physical_size;
70 static guint    screen_old_desktop;
71 static gboolean screen_desktop_timeout = TRUE;
72 /*! An array of desktops, holding an array of areas per monitor */
73 static Rect  *monitor_area = NULL;
74 /*! An array of desktops, holding an array of struts */
75 static GSList *struts_top = NULL;
76 static GSList *struts_left = NULL;
77 static GSList *struts_right = NULL;
78 static GSList *struts_bottom = NULL;
79
80 static ObPagerPopup *desktop_popup;
81 static gboolean      desktop_popup_perm;
82
83 /*! The number of microseconds that you need to be on a desktop before it will
84   replace the remembered "last desktop" */
85 #define REMEMBER_LAST_DESKTOP_TIME 750000
86
87 static gboolean replace_wm(void)
88 {
89     gchar *wm_sn;
90     Atom wm_sn_atom;
91     Window current_wm_sn_owner;
92     Time timestamp;
93
94     wm_sn = g_strdup_printf("WM_S%d", ob_screen);
95     wm_sn_atom = XInternAtom(obt_display, wm_sn, FALSE);
96     g_free(wm_sn);
97
98     current_wm_sn_owner = XGetSelectionOwner(obt_display, wm_sn_atom);
99     if (current_wm_sn_owner == screen_support_win)
100         current_wm_sn_owner = None;
101     if (current_wm_sn_owner) {
102         if (!ob_replace_wm) {
103             g_message(_("A window manager is already running on screen %d"),
104                       ob_screen);
105             return FALSE;
106         }
107         obt_display_ignore_errors(TRUE);
108
109         /* We want to find out when the current selection owner dies */
110         XSelectInput(obt_display, current_wm_sn_owner, StructureNotifyMask);
111         XSync(obt_display, FALSE);
112
113         obt_display_ignore_errors(FALSE);
114         if (obt_display_error_occured)
115             current_wm_sn_owner = None;
116     }
117
118     timestamp = event_get_server_time();
119
120     XSetSelectionOwner(obt_display, wm_sn_atom, screen_support_win,
121                        timestamp);
122
123     if (XGetSelectionOwner(obt_display, wm_sn_atom) != screen_support_win) {
124         g_message(_("Could not acquire window manager selection on screen %d"),
125                   ob_screen);
126         return FALSE;
127     }
128
129     /* Wait for old window manager to go away */
130     if (current_wm_sn_owner) {
131       XEvent event;
132       gulong wait = 0;
133       const gulong timeout = G_USEC_PER_SEC * 15; /* wait for 15s max */
134
135       while (wait < timeout) {
136           if (XCheckWindowEvent(obt_display, current_wm_sn_owner,
137                                 StructureNotifyMask, &event) &&
138               event.type == DestroyNotify)
139               break;
140           g_usleep(G_USEC_PER_SEC / 10);
141           wait += G_USEC_PER_SEC / 10;
142       }
143
144       if (wait >= timeout) {
145           g_message(_("The WM on screen %d is not exiting"), ob_screen);
146           return FALSE;
147       }
148     }
149
150     /* Send client message indicating that we are now the WM */
151     obt_prop_message(ob_screen, obt_root(ob_screen), OBT_PROP_ATOM(MANAGER),
152                      timestamp, wm_sn_atom, screen_support_win, 0, 0,
153                      SubstructureNotifyMask);
154
155     return TRUE;
156 }
157
158 gboolean screen_annex(void)
159 {
160     XSetWindowAttributes attrib;
161     pid_t pid;
162     gint i, num_support;
163     gulong *supported;
164
165     /* create the netwm support window */
166     attrib.override_redirect = TRUE;
167     attrib.event_mask = PropertyChangeMask;
168     screen_support_win = XCreateWindow(obt_display, obt_root(ob_screen),
169                                        -100, -100, 1, 1, 0,
170                                        CopyFromParent, InputOutput,
171                                        CopyFromParent,
172                                        CWEventMask | CWOverrideRedirect,
173                                        &attrib);
174     XMapWindow(obt_display, screen_support_win);
175     XLowerWindow(obt_display, screen_support_win);
176
177     if (!replace_wm()) {
178         XDestroyWindow(obt_display, screen_support_win);
179         return FALSE;
180     }
181
182     obt_display_ignore_errors(TRUE);
183     XSelectInput(obt_display, obt_root(ob_screen), ROOT_EVENTMASK);
184     obt_display_ignore_errors(FALSE);
185     if (obt_display_error_occured) {
186         g_message(_("A window manager is already running on screen %d"),
187                   ob_screen);
188
189         XDestroyWindow(obt_display, screen_support_win);
190         return FALSE;
191     }
192
193     screen_set_root_cursor();
194
195     /* set the OPENBOX_PID hint */
196     pid = getpid();
197     OBT_PROP_SET32(obt_root(ob_screen), OPENBOX_PID, CARDINAL, pid);
198
199     /* set supporting window */
200     OBT_PROP_SET32(obt_root(ob_screen),
201                    NET_SUPPORTING_WM_CHECK, WINDOW, screen_support_win);
202
203     /* set properties on the supporting window */
204     OBT_PROP_SETS(screen_support_win, NET_WM_NAME, utf8, "Openbox");
205     OBT_PROP_SET32(screen_support_win, NET_SUPPORTING_WM_CHECK,
206                    WINDOW, screen_support_win);
207
208     /* set the _NET_SUPPORTED_ATOMS hint */
209
210     /* this is all the atoms after NET_SUPPORTED in the ObtPropAtoms enum */
211     num_support = OBT_PROP_NUM_ATOMS - OBT_PROP_NET_SUPPORTED - 1;
212     i = 0;
213     supported = g_new(gulong, num_support);
214     supported[i++] = OBT_PROP_ATOM(NET_SUPPORTING_WM_CHECK);
215     supported[i++] = OBT_PROP_ATOM(NET_WM_FULL_PLACEMENT);
216     supported[i++] = OBT_PROP_ATOM(NET_CURRENT_DESKTOP);
217     supported[i++] = OBT_PROP_ATOM(NET_NUMBER_OF_DESKTOPS);
218     supported[i++] = OBT_PROP_ATOM(NET_DESKTOP_GEOMETRY);
219     supported[i++] = OBT_PROP_ATOM(NET_DESKTOP_VIEWPORT);
220     supported[i++] = OBT_PROP_ATOM(NET_ACTIVE_WINDOW);
221     supported[i++] = OBT_PROP_ATOM(NET_WORKAREA);
222     supported[i++] = OBT_PROP_ATOM(NET_CLIENT_LIST);
223     supported[i++] = OBT_PROP_ATOM(NET_CLIENT_LIST_STACKING);
224     supported[i++] = OBT_PROP_ATOM(NET_DESKTOP_NAMES);
225     supported[i++] = OBT_PROP_ATOM(NET_CLOSE_WINDOW);
226     supported[i++] = OBT_PROP_ATOM(NET_DESKTOP_LAYOUT);
227     supported[i++] = OBT_PROP_ATOM(NET_SHOWING_DESKTOP);
228     supported[i++] = OBT_PROP_ATOM(NET_WM_NAME);
229     supported[i++] = OBT_PROP_ATOM(NET_WM_VISIBLE_NAME);
230     supported[i++] = OBT_PROP_ATOM(NET_WM_ICON_NAME);
231     supported[i++] = OBT_PROP_ATOM(NET_WM_VISIBLE_ICON_NAME);
232     supported[i++] = OBT_PROP_ATOM(NET_WM_DESKTOP);
233     supported[i++] = OBT_PROP_ATOM(NET_WM_STRUT);
234     supported[i++] = OBT_PROP_ATOM(NET_WM_STRUT_PARTIAL);
235     supported[i++] = OBT_PROP_ATOM(NET_WM_ICON);
236     supported[i++] = OBT_PROP_ATOM(NET_WM_ICON_GEOMETRY);
237     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE);
238     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DESKTOP);
239     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DOCK);
240     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_TOOLBAR);
241     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_MENU);
242     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_UTILITY);
243     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_SPLASH);
244     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_DIALOG);
245     supported[i++] = OBT_PROP_ATOM(NET_WM_WINDOW_TYPE_NORMAL);
246     supported[i++] = OBT_PROP_ATOM(NET_WM_ALLOWED_ACTIONS);
247     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_MOVE);
248     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_RESIZE);
249     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_MINIMIZE);
250     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_SHADE);
251     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_HORZ);
252     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_MAXIMIZE_VERT);
253     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_FULLSCREEN);
254     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_CHANGE_DESKTOP);
255     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_CLOSE);
256     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_ABOVE);
257     supported[i++] = OBT_PROP_ATOM(NET_WM_ACTION_BELOW);
258     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE);
259     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_MODAL);
260     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_VERT);
261     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_MAXIMIZED_HORZ);
262     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_SHADED);
263     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_TASKBAR);
264     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_SKIP_PAGER);
265     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_HIDDEN);
266     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_FULLSCREEN);
267     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_ABOVE);
268     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_BELOW);
269     supported[i++] = OBT_PROP_ATOM(NET_WM_STATE_DEMANDS_ATTENTION);
270     supported[i++] = OBT_PROP_ATOM(NET_MOVERESIZE_WINDOW);
271     supported[i++] = OBT_PROP_ATOM(NET_WM_MOVERESIZE);
272     supported[i++] = OBT_PROP_ATOM(NET_WM_USER_TIME);
273 /*
274     supported[i++] = OBT_PROP_ATOM(NET_WM_USER_TIME_WINDOW);
275 */
276     supported[i++] = OBT_PROP_ATOM(NET_FRAME_EXTENTS);
277     supported[i++] = OBT_PROP_ATOM(NET_REQUEST_FRAME_EXTENTS);
278     supported[i++] = OBT_PROP_ATOM(NET_RESTACK_WINDOW);
279     supported[i++] = OBT_PROP_ATOM(NET_STARTUP_ID);
280 #ifdef SYNC
281     supported[i++] = OBT_PROP_ATOM(NET_WM_SYNC_REQUEST);
282     supported[i++] = OBT_PROP_ATOM(NET_WM_SYNC_REQUEST_COUNTER);
283 #endif
284     supported[i++] = OBT_PROP_ATOM(NET_WM_PID);
285     supported[i++] = OBT_PROP_ATOM(NET_WM_PING);
286
287     supported[i++] = OBT_PROP_ATOM(KDE_WM_CHANGE_STATE);
288     supported[i++] = OBT_PROP_ATOM(KDE_NET_WM_FRAME_STRUT);
289     supported[i++] = OBT_PROP_ATOM(KDE_NET_WM_WINDOW_TYPE_OVERRIDE);
290
291     supported[i++] = OBT_PROP_ATOM(OB_WM_ACTION_UNDECORATE);
292     supported[i++] = OBT_PROP_ATOM(OB_WM_STATE_UNDECORATED);
293     supported[i++] = OBT_PROP_ATOM(OPENBOX_PID);
294     supported[i++] = OBT_PROP_ATOM(OB_THEME);
295     supported[i++] = OBT_PROP_ATOM(OB_CONFIG_FILE);
296     supported[i++] = OBT_PROP_ATOM(OB_CONTROL);
297     supported[i++] = OBT_PROP_ATOM(OB_ROLE);
298     supported[i++] = OBT_PROP_ATOM(OB_NAME);
299     supported[i++] = OBT_PROP_ATOM(OB_CLASS);
300     g_assert(i == num_support);
301
302     OBT_PROP_SETA32(obt_root(ob_screen),
303                     NET_SUPPORTED, ATOM, supported, num_support);
304     g_free(supported);
305
306     screen_tell_ksplash();
307
308     return TRUE;
309 }
310
311 static void screen_tell_ksplash(void)
312 {
313     XEvent e;
314     char **argv;
315
316     argv = g_new(gchar*, 6);
317     argv[0] = g_strdup("dcop");
318     argv[1] = g_strdup("ksplash");
319     argv[2] = g_strdup("ksplash");
320     argv[3] = g_strdup("upAndRunning(QString)");
321     argv[4] = g_strdup("wm started");
322     argv[5] = NULL;
323
324     /* tell ksplash through the dcop server command line interface */
325     g_spawn_async(NULL, argv, NULL,
326                   G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD |
327                   G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_STDOUT_TO_DEV_NULL,
328                   NULL, NULL, NULL, NULL);
329     g_strfreev(argv);
330
331     /* i'm not sure why we do this, kwin does it, but ksplash doesn't seem to
332        hear it anyways. perhaps it is for old ksplash. or new ksplash. or
333        something. oh well. */
334     e.xclient.type = ClientMessage;
335     e.xclient.display = obt_display;
336     e.xclient.window = obt_root(ob_screen);
337     e.xclient.message_type =
338         XInternAtom(obt_display, "_KDE_SPLASH_PROGRESS", False);
339     e.xclient.format = 8;
340     strcpy(e.xclient.data.b, "wm started");
341     XSendEvent(obt_display, obt_root(ob_screen),
342                False, SubstructureNotifyMask, &e);
343 }
344
345 void screen_startup(gboolean reconfig)
346 {
347     gchar **names = NULL;
348     guint32 d;
349     gboolean namesexist = FALSE;
350
351     desktop_popup = pager_popup_new();
352     desktop_popup_perm = FALSE;
353     pager_popup_height(desktop_popup, POPUP_HEIGHT);
354
355     if (reconfig) {
356         /* update the pager popup's width */
357         pager_popup_text_width_to_strings(desktop_popup,
358                                           screen_desktop_names,
359                                           screen_num_desktops);
360         return;
361     }
362
363     /* get the initial size */
364     screen_resize();
365
366     /* have names already been set for the desktops? */
367     if (OBT_PROP_GETSS(obt_root(ob_screen), NET_DESKTOP_NAMES, utf8, &names)) {
368         g_strfreev(names);
369         namesexist = TRUE;
370     }
371
372     /* if names don't exist and we have session names, set those.
373        do this stuff BEFORE setting the number of desktops, because that
374        will create default names for them
375     */
376     if (!namesexist && session_desktop_names != NULL) {
377         guint i, numnames;
378         GSList *it;
379
380         /* get the desktop names */
381         numnames = g_slist_length(session_desktop_names);
382         names = g_new(gchar*, numnames + 1);
383         names[numnames] = NULL;
384         for (i = 0, it = session_desktop_names; it; ++i, it = g_slist_next(it))
385             names[i] = g_strdup(it->data);
386
387         /* set the root window property */
388         OBT_PROP_SETSS(obt_root(ob_screen),
389                        NET_DESKTOP_NAMES, utf8, (const gchar**)names);
390
391         g_strfreev(names);
392     }
393
394     /* set the number of desktops, if it's not already set.
395
396        this will also set the default names from the config file up for
397        desktops that don't have names yet */
398     screen_num_desktops = 0;
399     if (OBT_PROP_GET32(obt_root(ob_screen),
400                        NET_NUMBER_OF_DESKTOPS, CARDINAL, &d))
401     {
402         if (d != config_desktops_num) {
403             /* TRANSLATORS: If you need to specify a different order of the
404                arguments, you can use %1$d for the first one and %2$d for the
405                second one. For example,
406                "The current session has %2$d desktops, but Openbox is configured for %1$d ..." */
407             g_warning(ngettext("Openbox is configured for %d desktop, but the current session has %d.  Overriding the Openbox configuration.", "Openbox is configured for %d desktops, but the current session has %d.  Overriding the Openbox configuration.", config_desktops_num),
408                       config_desktops_num, d);
409         }
410         screen_set_num_desktops(d);
411     }
412     /* restore from session if possible */
413     else if (session_num_desktops)
414         screen_set_num_desktops(session_num_desktops);
415     else
416         screen_set_num_desktops(config_desktops_num);
417
418     screen_desktop = screen_num_desktops;  /* something invalid */
419     /* start on the current desktop when a wm was already running */
420     if (OBT_PROP_GET32(obt_root(ob_screen),
421                        NET_CURRENT_DESKTOP, CARDINAL, &d) &&
422         d < screen_num_desktops)
423     {
424         screen_set_desktop(d, FALSE);
425     } else if (session_desktop >= 0)
426         screen_set_desktop(MIN((guint)session_desktop,
427                                screen_num_desktops), FALSE);
428     else
429         screen_set_desktop(MIN(config_screen_firstdesk,
430                                screen_num_desktops) - 1, FALSE);
431     screen_last_desktop = screen_desktop;
432
433     /* don't start in showing-desktop mode */
434     screen_showing_desktop = FALSE;
435     OBT_PROP_SET32(obt_root(ob_screen),
436                    NET_SHOWING_DESKTOP, CARDINAL, screen_showing_desktop);
437
438     if (session_desktop_layout_present &&
439         screen_validate_layout(&session_desktop_layout))
440     {
441         screen_desktop_layout = session_desktop_layout;
442     }
443     else
444         screen_update_layout();
445 }
446
447 void screen_shutdown(gboolean reconfig)
448 {
449     pager_popup_free(desktop_popup);
450
451     if (reconfig)
452         return;
453
454     XSelectInput(obt_display, obt_root(ob_screen), NoEventMask);
455
456     /* we're not running here no more! */
457     OBT_PROP_ERASE(obt_root(ob_screen), OPENBOX_PID);
458     /* not without us */
459     OBT_PROP_ERASE(obt_root(ob_screen), NET_SUPPORTED);
460     /* don't keep this mode */
461     OBT_PROP_ERASE(obt_root(ob_screen), NET_SHOWING_DESKTOP);
462
463     XDestroyWindow(obt_display, screen_support_win);
464
465     g_strfreev(screen_desktop_names);
466     screen_desktop_names = NULL;
467 }
468
469 void screen_resize(void)
470 {
471     static gint oldw = 0, oldh = 0;
472     gint w, h;
473     GList *it;
474     gulong geometry[2];
475
476     w = WidthOfScreen(ScreenOfDisplay(obt_display, ob_screen));
477     h = HeightOfScreen(ScreenOfDisplay(obt_display, ob_screen));
478
479     if (w == oldw && h == oldh) return;
480
481     oldw = w; oldh = h;
482
483     /* Set the _NET_DESKTOP_GEOMETRY hint */
484     screen_physical_size.width = geometry[0] = w;
485     screen_physical_size.height = geometry[1] = h;
486     OBT_PROP_SETA32(obt_root(ob_screen),
487                     NET_DESKTOP_GEOMETRY, CARDINAL, geometry, 2);
488
489     if (ob_state() != OB_STATE_RUNNING)
490         return;
491
492     screen_update_areas();
493     dock_configure();
494
495     for (it = client_list; it; it = g_list_next(it))
496         client_move_onscreen(it->data, FALSE);
497 }
498
499 void screen_set_num_desktops(guint num)
500 {
501     gulong *viewport;
502     GList *it, *stacking_copy;
503
504     g_assert(num > 0);
505
506     if (screen_num_desktops == num) return;
507
508     screen_num_desktops = num;
509     OBT_PROP_SET32(obt_root(ob_screen), NET_NUMBER_OF_DESKTOPS, CARDINAL, num);
510
511     /* set the viewport hint */
512     viewport = g_new0(gulong, num * 2);
513     OBT_PROP_SETA32(obt_root(ob_screen),
514                     NET_DESKTOP_VIEWPORT, CARDINAL, viewport, num * 2);
515     g_free(viewport);
516
517     /* the number of rows/columns will differ */
518     screen_update_layout();
519
520     /* move windows on desktops that will no longer exist!
521        make a copy of the list cuz we're changing it */
522     stacking_copy = g_list_copy(stacking_list);
523     for (it = g_list_last(stacking_copy); it; it = g_list_previous(it)) {
524         if (WINDOW_IS_CLIENT(it->data)) {
525             ObClient *c = it->data;
526             if (c->desktop != DESKTOP_ALL && c->desktop >= num)
527                 client_set_desktop(c, num - 1, FALSE, TRUE);
528             /* raise all the windows that are on the current desktop which
529                is being merged */
530             else if (screen_desktop == num - 1 &&
531                      (c->desktop == DESKTOP_ALL ||
532                       c->desktop == screen_desktop))
533                 stacking_raise(CLIENT_AS_WINDOW(c));
534         }
535     }
536     g_list_free(stacking_copy);
537
538     /* change our struts/area to match (after moving windows) */
539     screen_update_areas();
540
541     /* may be some unnamed desktops that we need to fill in with names
542        (after updating the areas so the popup can resize) */
543     screen_update_desktop_names();
544
545     /* change our desktop if we're on one that no longer exists! */
546     if (screen_desktop >= screen_num_desktops)
547         screen_set_desktop(num - 1, TRUE);
548 }
549
550 static void screen_fallback_focus(void)
551 {
552     ObClient *c;
553     gboolean allow_omni;
554
555     /* only allow omnipresent windows to get focus on desktop change if
556        an omnipresent window is already focused (it'll keep focus probably, but
557        maybe not depending on mouse-focus options) */
558     allow_omni = focus_client && (client_normal(focus_client) &&
559                                   focus_client->desktop == DESKTOP_ALL);
560
561     /* the client moved there already so don't move focus. prevent flicker
562        on sendtodesktop + follow */
563     if (focus_client && focus_client->desktop == screen_desktop)
564         return;
565
566     /* have to try focus here because when you leave an empty desktop
567        there is no focus out to watch for. also, we have different rules
568        here. we always allow it to look under the mouse pointer if
569        config_focus_last is FALSE
570
571        do this before hiding the windows so if helper windows are coming
572        with us, they don't get hidden
573     */
574     if ((c = focus_fallback(TRUE, !config_focus_last, allow_omni,
575                             !allow_omni)))
576     {
577         /* only do the flicker reducing stuff ahead of time if we are going
578            to call xsetinputfocus on the window ourselves. otherwise there is
579            no guarantee the window will actually take focus.. */
580         if (c->can_focus) {
581             /* reduce flicker by hiliting now rather than waiting for the
582                server FocusIn event */
583             frame_adjust_focus(c->frame, TRUE);
584             /* do this here so that if you switch desktops to a window with
585                helper windows then the helper windows won't flash */
586             client_bring_helper_windows(c);
587         }
588     }
589 }
590
591 static gboolean last_desktop_func(gpointer data)
592 {
593     screen_desktop_timeout = TRUE;
594     return FALSE;
595 }
596
597 void screen_set_desktop(guint num, gboolean dofocus)
598 {
599     GList *it;
600     guint previous;
601     gulong ignore_start;
602
603     g_assert(num < screen_num_desktops);
604
605     previous = screen_desktop;
606     screen_desktop = num;
607
608     if (previous == num) return;
609
610     OBT_PROP_SET32(obt_root(ob_screen), NET_CURRENT_DESKTOP, CARDINAL, num);
611
612     /* This whole thing decides when/how to save the screen_last_desktop so
613        that it can be restored later if you want */
614     if (screen_desktop_timeout) {
615         /* If screen_desktop_timeout is true, then we've been on this desktop
616            long enough and we can save it as the last desktop. */
617
618         if (screen_last_desktop == previous)
619             /* this is the startup state only */
620             screen_old_desktop = screen_desktop;
621         else {
622             /* save the "last desktop" as the "old desktop" */
623             screen_old_desktop = screen_last_desktop;
624             /* save the desktop we're coming from as the "last desktop" */
625             screen_last_desktop = previous;
626         }
627     }
628     else {
629         /* If screen_desktop_timeout is false, then we just got to this desktop
630            and we are moving away again. */
631
632         if (screen_desktop == screen_last_desktop) {
633             /* If we are moving to the "last desktop" .. */
634             if (previous == screen_old_desktop) {
635                 /* .. from the "old desktop", change the last desktop to
636                    be where we are coming from */
637                 screen_last_desktop = screen_old_desktop;
638             }
639             else if (screen_last_desktop == screen_old_desktop) {
640                 /* .. and also to the "old desktop", change the "last
641                    desktop" to be where we are coming from */
642                 screen_last_desktop = previous;
643             }
644             else {
645                 /* .. from some other desktop, then set the "last desktop" to
646                    be the saved "old desktop", i.e. where we were before the
647                    "last desktop" */
648                 screen_last_desktop = screen_old_desktop;
649             }
650         }
651         else {
652             /* If we are moving to any desktop besides the "last desktop"..
653                (this is the normal case) */
654             if (screen_desktop == screen_old_desktop) {
655                 /* If moving to the "old desktop", which is not the
656                    "last desktop", don't save anything */
657             }
658             else if (previous == screen_old_desktop) {
659                 /* If moving from the "old desktop", and not to the
660                    "last desktop", don't save anything */
661             }
662             else if (screen_last_desktop == screen_old_desktop) {
663                 /* If the "last desktop" is the same as "old desktop" and
664                    you're not moving to the "last desktop" then save where
665                    we're coming from as the "last desktop" */
666                 screen_last_desktop = previous;
667             }
668             else {
669                 /* If the "last desktop" is different from the "old desktop"
670                    and you're not moving to the "last desktop", then don't save
671                    anything */
672             }
673         }
674     }
675     screen_desktop_timeout = FALSE;
676     obt_main_loop_timeout_remove(ob_main_loop, last_desktop_func);
677     obt_main_loop_timeout_add(ob_main_loop, REMEMBER_LAST_DESKTOP_TIME,
678                               last_desktop_func, NULL, NULL, NULL);
679
680     ob_debug("Moving to desktop %d", num+1);
681
682     if (ob_state() == OB_STATE_RUNNING)
683         screen_show_desktop_popup(screen_desktop, FALSE);
684
685     /* ignore enter events caused by the move */
686     ignore_start = event_start_ignore_all_enters();
687
688     if (moveresize_client)
689         client_set_desktop(moveresize_client, num, TRUE, FALSE);
690
691     /* show windows before hiding the rest to lessen the enter/leave events */
692
693     /* show windows from top to bottom */
694     for (it = stacking_list; it; it = g_list_next(it)) {
695         if (WINDOW_IS_CLIENT(it->data)) {
696             ObClient *c = it->data;
697             client_show(c);
698         }
699     }
700
701     if (dofocus) screen_fallback_focus();
702
703     /* hide windows from bottom to top */
704     for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
705         if (WINDOW_IS_CLIENT(it->data)) {
706             ObClient *c = it->data;
707             if (client_hide(c)) {
708                 /* in the middle of cycling..? kill it. */
709                 focus_cycle_stop(c);
710
711                 if (c == focus_client) {
712                     /* c was focused and we didn't do fallback clearly so make
713                        sure openbox doesnt still consider the window focused.
714                        this happens when using NextWindow with allDesktops,
715                        since it doesnt want to move focus on desktop change,
716                        but the focus is not going to stay with the current
717                        window, which has now disappeared.
718                        only do this if the client was actually hidden,
719                        otherwise it can keep focus. */
720                     focus_set_client(NULL);
721                 }
722             }
723         }
724     }
725
726     event_end_ignore_all_enters(ignore_start);
727
728     if (event_curtime != CurrentTime)
729         screen_desktop_user_time = event_curtime;
730 }
731
732 void screen_add_desktop(gboolean current)
733 {
734     gulong ignore_start;
735
736     /* ignore enter events caused by this */
737     ignore_start = event_start_ignore_all_enters();
738
739     screen_set_num_desktops(screen_num_desktops+1);
740
741     /* move all the clients over */
742     if (current) {
743         GList *it;
744
745         for (it = client_list; it; it = g_list_next(it)) {
746             ObClient *c = it->data;
747             if (c->desktop != DESKTOP_ALL && c->desktop >= screen_desktop &&
748                 /* don't move direct children, they'll be moved with their
749                    parent - which will have to be on the same desktop */
750                 !client_direct_parent(c))
751             {
752                 ob_debug("moving window %s", c->title);
753                 client_set_desktop(c, c->desktop+1, FALSE, TRUE);
754             }
755         }
756     }
757
758     event_end_ignore_all_enters(ignore_start);
759 }
760
761 void screen_remove_desktop(gboolean current)
762 {
763     guint rmdesktop, movedesktop;
764     GList *it, *stacking_copy;
765     gulong ignore_start;
766
767     if (screen_num_desktops <= 1) return;
768
769     /* ignore enter events caused by this */
770     ignore_start = event_start_ignore_all_enters();
771
772     /* what desktop are we removing and moving to? */
773     if (current)
774         rmdesktop = screen_desktop;
775     else
776         rmdesktop = screen_num_desktops - 1;
777     if (rmdesktop < screen_num_desktops - 1)
778         movedesktop = rmdesktop + 1;
779     else
780         movedesktop = rmdesktop;
781
782     /* make a copy of the list cuz we're changing it */
783     stacking_copy = g_list_copy(stacking_list);
784     for (it = g_list_last(stacking_copy); it; it = g_list_previous(it)) {
785         if (WINDOW_IS_CLIENT(it->data)) {
786             ObClient *c = it->data;
787             guint d = c->desktop;
788             if (d != DESKTOP_ALL && d >= movedesktop &&
789                 /* don't move direct children, they'll be moved with their
790                    parent - which will have to be on the same desktop */
791                 !client_direct_parent(c))
792             {
793                 ob_debug("moving window %s", c->title);
794                 client_set_desktop(c, c->desktop - 1, TRUE, TRUE);
795             }
796             /* raise all the windows that are on the current desktop which
797                is being merged */
798             if ((screen_desktop == rmdesktop - 1 ||
799                  screen_desktop == rmdesktop) &&
800                 (d == DESKTOP_ALL || d == screen_desktop))
801             {
802                 stacking_raise(CLIENT_AS_WINDOW(c));
803                 ob_debug("raising window %s", c->title);
804             }
805         }
806     }
807     g_list_free(stacking_copy);
808
809     /* fallback focus like we're changing desktops */
810     if (screen_desktop < screen_num_desktops - 1) {
811         screen_fallback_focus();
812         ob_debug("fake desktop change");
813     }
814
815     screen_set_num_desktops(screen_num_desktops-1);
816
817     event_end_ignore_all_enters(ignore_start);
818 }
819
820 static void get_row_col(guint d, guint *r, guint *c)
821 {
822     switch (screen_desktop_layout.orientation) {
823     case OB_ORIENTATION_HORZ:
824         switch (screen_desktop_layout.start_corner) {
825         case OB_CORNER_TOPLEFT:
826             *r = d / screen_desktop_layout.columns;
827             *c = d % screen_desktop_layout.columns;
828             break;
829         case OB_CORNER_BOTTOMLEFT:
830             *r = screen_desktop_layout.rows - 1 -
831                 d / screen_desktop_layout.columns;
832             *c = d % screen_desktop_layout.columns;
833             break;
834         case OB_CORNER_TOPRIGHT:
835             *r = d / screen_desktop_layout.columns;
836             *c = screen_desktop_layout.columns - 1 -
837                 d % screen_desktop_layout.columns;
838             break;
839         case OB_CORNER_BOTTOMRIGHT:
840             *r = screen_desktop_layout.rows - 1 -
841                 d / screen_desktop_layout.columns;
842             *c = screen_desktop_layout.columns - 1 -
843                 d % screen_desktop_layout.columns;
844             break;
845         }
846         break;
847     case OB_ORIENTATION_VERT:
848         switch (screen_desktop_layout.start_corner) {
849         case OB_CORNER_TOPLEFT:
850             *r = d % screen_desktop_layout.rows;
851             *c = d / screen_desktop_layout.rows;
852             break;
853         case OB_CORNER_BOTTOMLEFT:
854             *r = screen_desktop_layout.rows - 1 -
855                 d % screen_desktop_layout.rows;
856             *c = d / screen_desktop_layout.rows;
857             break;
858         case OB_CORNER_TOPRIGHT:
859             *r = d % screen_desktop_layout.rows;
860             *c = screen_desktop_layout.columns - 1 -
861                 d / screen_desktop_layout.rows;
862             break;
863         case OB_CORNER_BOTTOMRIGHT:
864             *r = screen_desktop_layout.rows - 1 -
865                 d % screen_desktop_layout.rows;
866             *c = screen_desktop_layout.columns - 1 -
867                 d / screen_desktop_layout.rows;
868             break;
869         }
870         break;
871     }
872 }
873
874 static guint translate_row_col(guint r, guint c)
875 {
876     switch (screen_desktop_layout.orientation) {
877     case OB_ORIENTATION_HORZ:
878         switch (screen_desktop_layout.start_corner) {
879         case OB_CORNER_TOPLEFT:
880             return r % screen_desktop_layout.rows *
881                 screen_desktop_layout.columns +
882                 c % screen_desktop_layout.columns;
883         case OB_CORNER_BOTTOMLEFT:
884             return (screen_desktop_layout.rows - 1 -
885                     r % screen_desktop_layout.rows) *
886                 screen_desktop_layout.columns +
887                 c % screen_desktop_layout.columns;
888         case OB_CORNER_TOPRIGHT:
889             return r % screen_desktop_layout.rows *
890                 screen_desktop_layout.columns +
891                 (screen_desktop_layout.columns - 1 -
892                  c % screen_desktop_layout.columns);
893         case OB_CORNER_BOTTOMRIGHT:
894             return (screen_desktop_layout.rows - 1 -
895                     r % screen_desktop_layout.rows) *
896                 screen_desktop_layout.columns +
897                 (screen_desktop_layout.columns - 1 -
898                  c % screen_desktop_layout.columns);
899         }
900     case OB_ORIENTATION_VERT:
901         switch (screen_desktop_layout.start_corner) {
902         case OB_CORNER_TOPLEFT:
903             return c % screen_desktop_layout.columns *
904                 screen_desktop_layout.rows +
905                 r % screen_desktop_layout.rows;
906         case OB_CORNER_BOTTOMLEFT:
907             return c % screen_desktop_layout.columns *
908                 screen_desktop_layout.rows +
909                 (screen_desktop_layout.rows - 1 -
910                  r % screen_desktop_layout.rows);
911         case OB_CORNER_TOPRIGHT:
912             return (screen_desktop_layout.columns - 1 -
913                     c % screen_desktop_layout.columns) *
914                 screen_desktop_layout.rows +
915                 r % screen_desktop_layout.rows;
916         case OB_CORNER_BOTTOMRIGHT:
917             return (screen_desktop_layout.columns - 1 -
918                     c % screen_desktop_layout.columns) *
919                 screen_desktop_layout.rows +
920                 (screen_desktop_layout.rows - 1 -
921                  r % screen_desktop_layout.rows);
922         }
923     }
924     g_assert_not_reached();
925     return 0;
926 }
927
928 static gboolean hide_desktop_popup_func(gpointer data)
929 {
930     pager_popup_hide(desktop_popup);
931     return FALSE; /* don't repeat */
932 }
933
934 void screen_show_desktop_popup(guint d, gboolean perm)
935 {
936     Rect *a;
937
938     /* 0 means don't show the popup */
939     if (!config_desktop_popup_time) return;
940
941     a = screen_physical_area_primary(FALSE);
942     pager_popup_position(desktop_popup, CenterGravity,
943                          a->x + a->width / 2, a->y + a->height / 2);
944     pager_popup_icon_size_multiplier(desktop_popup,
945                                      (screen_desktop_layout.columns /
946                                       screen_desktop_layout.rows) / 2,
947                                      (screen_desktop_layout.rows/
948                                       screen_desktop_layout.columns) / 2);
949     pager_popup_max_width(desktop_popup,
950                           MAX(a->width/3, POPUP_WIDTH));
951     pager_popup_show(desktop_popup, screen_desktop_names[d], d);
952
953     obt_main_loop_timeout_remove(ob_main_loop, hide_desktop_popup_func);
954     if (!perm && !desktop_popup_perm)
955         /* only hide if its not already being show permanently */
956         obt_main_loop_timeout_add(ob_main_loop,
957                                   config_desktop_popup_time * 1000,
958                                   hide_desktop_popup_func, desktop_popup,
959                                   g_direct_equal, NULL);
960     if (perm)
961         desktop_popup_perm = TRUE;
962
963     g_free(a);
964 }
965
966 void screen_hide_desktop_popup(void)
967 {
968     obt_main_loop_timeout_remove_data(ob_main_loop, hide_desktop_popup_func,
969                                       desktop_popup, FALSE);
970     pager_popup_hide(desktop_popup);
971     desktop_popup_perm = FALSE;
972 }
973
974 guint screen_find_desktop(guint from, ObDirection dir,
975                           gboolean wrap, gboolean linear)
976 {
977     guint r, c;
978     guint d;
979
980     d = from;
981     get_row_col(d, &r, &c);
982     if (linear) {
983         switch (dir) {
984         case OB_DIRECTION_EAST:
985             if (d < screen_num_desktops - 1)
986                 ++d;
987             else if (wrap)
988                 d = 0;
989             else
990                 return from;
991             break;
992         case OB_DIRECTION_WEST:
993             if (d > 0)
994                 --d;
995             else if (wrap)
996                 d = screen_num_desktops - 1;
997             else
998                 return from;
999             break;
1000         default:
1001             g_assert_not_reached();
1002             return from;
1003         }
1004     } else {
1005         switch (dir) {
1006         case OB_DIRECTION_EAST:
1007             ++c;
1008             if (c >= screen_desktop_layout.columns) {
1009                 if (wrap)
1010                     c = 0;
1011                 else
1012                     return from;
1013             }
1014             d = translate_row_col(r, c);
1015             if (d >= screen_num_desktops) {
1016                 if (wrap)
1017                     ++c;
1018                 else
1019                     return from;
1020             }
1021             break;
1022         case OB_DIRECTION_WEST:
1023             --c;
1024             if (c >= screen_desktop_layout.columns) {
1025                 if (wrap)
1026                     c = screen_desktop_layout.columns - 1;
1027                 else
1028                     return from;
1029             }
1030             d = translate_row_col(r, c);
1031             if (d >= screen_num_desktops) {
1032                 if (wrap)
1033                     --c;
1034                 else
1035                     return from;
1036             }
1037             break;
1038         case OB_DIRECTION_SOUTH:
1039             ++r;
1040             if (r >= screen_desktop_layout.rows) {
1041                 if (wrap)
1042                     r = 0;
1043                 else
1044                     return from;
1045             }
1046             d = translate_row_col(r, c);
1047             if (d >= screen_num_desktops) {
1048                 if (wrap)
1049                     ++r;
1050                 else
1051                     return from;
1052             }
1053             break;
1054         case OB_DIRECTION_NORTH:
1055             --r;
1056             if (r >= screen_desktop_layout.rows) {
1057                 if (wrap)
1058                     r = screen_desktop_layout.rows - 1;
1059                 else
1060                     return from;
1061             }
1062             d = translate_row_col(r, c);
1063             if (d >= screen_num_desktops) {
1064                 if (wrap)
1065                     --r;
1066                 else
1067                     return from;
1068             }
1069             break;
1070         default:
1071             g_assert_not_reached();
1072             return from;
1073         }
1074
1075         d = translate_row_col(r, c);
1076     }
1077     return d;
1078 }
1079
1080 static gboolean screen_validate_layout(ObDesktopLayout *l)
1081 {
1082     if (l->columns == 0 && l->rows == 0) /* both 0's is bad data.. */
1083         return FALSE;
1084
1085     /* fill in a zero rows/columns */
1086     if (l->columns == 0) {
1087         l->columns = screen_num_desktops / l->rows;
1088         if (l->rows * l->columns < screen_num_desktops)
1089             l->columns++;
1090         if (l->rows * l->columns >= screen_num_desktops + l->columns)
1091             l->rows--;
1092     } else if (l->rows == 0) {
1093         l->rows = screen_num_desktops / l->columns;
1094         if (l->columns * l->rows < screen_num_desktops)
1095             l->rows++;
1096         if (l->columns * l->rows >= screen_num_desktops + l->rows)
1097             l->columns--;
1098     }
1099
1100     /* bounds checking */
1101     if (l->orientation == OB_ORIENTATION_HORZ) {
1102         l->columns = MIN(screen_num_desktops, l->columns);
1103         l->rows = MIN(l->rows,
1104                       (screen_num_desktops + l->columns - 1) / l->columns);
1105         l->columns = screen_num_desktops / l->rows +
1106             !!(screen_num_desktops % l->rows);
1107     } else {
1108         l->rows = MIN(screen_num_desktops, l->rows);
1109         l->columns = MIN(l->columns,
1110                          (screen_num_desktops + l->rows - 1) / l->rows);
1111         l->rows = screen_num_desktops / l->columns +
1112             !!(screen_num_desktops % l->columns);
1113     }
1114     return TRUE;
1115 }
1116
1117 void screen_update_layout(void)
1118
1119 {
1120     ObDesktopLayout l;
1121     guint32 *data;
1122     guint num;
1123
1124     screen_desktop_layout.orientation = OB_ORIENTATION_HORZ;
1125     screen_desktop_layout.start_corner = OB_CORNER_TOPLEFT;
1126     screen_desktop_layout.rows = 1;
1127     screen_desktop_layout.columns = screen_num_desktops;
1128
1129     if (OBT_PROP_GETA32(obt_root(ob_screen),
1130                         NET_DESKTOP_LAYOUT, CARDINAL, &data, &num)) {
1131         if (num == 3 || num == 4) {
1132
1133             if (data[0] == OBT_PROP_ATOM(NET_WM_ORIENTATION_VERT))
1134                 l.orientation = OB_ORIENTATION_VERT;
1135             else if (data[0] == OBT_PROP_ATOM(NET_WM_ORIENTATION_HORZ))
1136                 l.orientation = OB_ORIENTATION_HORZ;
1137             else
1138                 return;
1139
1140             if (num < 4)
1141                 l.start_corner = OB_CORNER_TOPLEFT;
1142             else {
1143                 if (data[3] == OBT_PROP_ATOM(NET_WM_TOPLEFT))
1144                     l.start_corner = OB_CORNER_TOPLEFT;
1145                 else if (data[3] == OBT_PROP_ATOM(NET_WM_TOPRIGHT))
1146                     l.start_corner = OB_CORNER_TOPRIGHT;
1147                 else if (data[3] == OBT_PROP_ATOM(NET_WM_BOTTOMRIGHT))
1148                     l.start_corner = OB_CORNER_BOTTOMRIGHT;
1149                 else if (data[3] == OBT_PROP_ATOM(NET_WM_BOTTOMLEFT))
1150                     l.start_corner = OB_CORNER_BOTTOMLEFT;
1151                 else
1152                     return;
1153             }
1154
1155             l.columns = data[1];
1156             l.rows = data[2];
1157
1158             if (screen_validate_layout(&l))
1159                 screen_desktop_layout = l;
1160
1161             g_free(data);
1162         }
1163     }
1164 }
1165
1166 void screen_update_desktop_names(void)
1167 {
1168     guint i;
1169
1170     /* empty the array */
1171     g_strfreev(screen_desktop_names);
1172     screen_desktop_names = NULL;
1173
1174     if (OBT_PROP_GETSS(obt_root(ob_screen),
1175                        NET_DESKTOP_NAMES, utf8, &screen_desktop_names))
1176         for (i = 0; screen_desktop_names[i] && i < screen_num_desktops; ++i);
1177     else
1178         i = 0;
1179     if (i < screen_num_desktops) {
1180         GSList *it;
1181
1182         screen_desktop_names = g_renew(gchar*, screen_desktop_names,
1183                                        screen_num_desktops + 1);
1184         screen_desktop_names[screen_num_desktops] = NULL;
1185
1186         it = g_slist_nth(config_desktops_names, i);
1187
1188         for (; i < screen_num_desktops; ++i) {
1189             if (it && ((char*)it->data)[0]) /* not empty */
1190                 /* use the names from the config file when possible */
1191                 screen_desktop_names[i] = g_strdup(it->data);
1192             else
1193                 /* make up a nice name if it's not though */
1194                 screen_desktop_names[i] = g_strdup_printf(_("desktop %i"),
1195                                                           i + 1);
1196             if (it) it = g_slist_next(it);
1197         }
1198
1199         /* if we changed any names, then set the root property so we can
1200            all agree on the names */
1201         OBT_PROP_SETSS(obt_root(ob_screen), NET_DESKTOP_NAMES,
1202                        utf8, (const gchar**)screen_desktop_names);
1203     }
1204
1205     /* resize the pager for these names */
1206     pager_popup_text_width_to_strings(desktop_popup,
1207                                       screen_desktop_names,
1208                                       screen_num_desktops);
1209 }
1210
1211 void screen_show_desktop(gboolean show, ObClient *show_only)
1212 {
1213     GList *it;
1214
1215     if (show == screen_showing_desktop) return; /* no change */
1216
1217     screen_showing_desktop = show;
1218
1219     if (show) {
1220         /* hide windows bottom to top */
1221         for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
1222             if (WINDOW_IS_CLIENT(it->data)) {
1223                 ObClient *client = it->data;
1224                 client_showhide(client);
1225             }
1226         }
1227     }
1228     else {
1229         /* restore windows top to bottom */
1230         for (it = stacking_list; it; it = g_list_next(it)) {
1231             if (WINDOW_IS_CLIENT(it->data)) {
1232                 ObClient *client = it->data;
1233                 if (client_should_show(client)) {
1234                     if (!show_only || client == show_only)
1235                         client_show(client);
1236                     else
1237                         client_iconify(client, TRUE, FALSE, TRUE);
1238                 }
1239             }
1240         }
1241     }
1242
1243     if (show) {
1244         /* focus the desktop */
1245         for (it = focus_order; it; it = g_list_next(it)) {
1246             ObClient *c = it->data;
1247             if (c->type == OB_CLIENT_TYPE_DESKTOP &&
1248                 (c->desktop == screen_desktop || c->desktop == DESKTOP_ALL) &&
1249                 client_focus(it->data))
1250                 break;
1251         }
1252     }
1253     else if (!show_only) {
1254         ObClient *c;
1255
1256         if ((c = focus_fallback(TRUE, FALSE, TRUE, FALSE))) {
1257             /* only do the flicker reducing stuff ahead of time if we are going
1258                to call xsetinputfocus on the window ourselves. otherwise there
1259                is no guarantee the window will actually take focus.. */
1260             if (c->can_focus) {
1261                 /* reduce flicker by hiliting now rather than waiting for the
1262                    server FocusIn event */
1263                 frame_adjust_focus(c->frame, TRUE);
1264             }
1265         }
1266     }
1267
1268     show = !!show; /* make it boolean */
1269     OBT_PROP_SET32(obt_root(ob_screen), NET_SHOWING_DESKTOP, CARDINAL, show);
1270 }
1271
1272 void screen_install_colormap(ObClient *client, gboolean install)
1273 {
1274     if (client == NULL || client->colormap == None) {
1275         if (install)
1276             XInstallColormap(obt_display, RrColormap(ob_rr_inst));
1277         else
1278             XUninstallColormap(obt_display, RrColormap(ob_rr_inst));
1279     } else {
1280         obt_display_ignore_errors(TRUE);
1281         if (install)
1282             XInstallColormap(obt_display, client->colormap);
1283         else
1284             XUninstallColormap(obt_display, client->colormap);
1285         obt_display_ignore_errors(FALSE);
1286     }
1287 }
1288
1289 typedef struct {
1290     guint desktop;
1291     StrutPartial *strut;
1292 } ObScreenStrut;
1293
1294 #define RESET_STRUT_LIST(sl) \
1295     (g_slist_free(sl), sl = NULL)
1296
1297 #define ADD_STRUT_TO_LIST(sl, d, s) \
1298 { \
1299     ObScreenStrut *ss = g_new(ObScreenStrut, 1); \
1300     ss->desktop = d; \
1301     ss->strut = s;  \
1302     sl = g_slist_prepend(sl, ss); \
1303 }
1304
1305 #define VALIDATE_STRUTS(sl, side, max) \
1306 { \
1307     GSList *it; \
1308     for (it = sl; it; it = g_slist_next(it)) { \
1309       ObScreenStrut *ss = it->data; \
1310       ss->strut->side = MIN(max, ss->strut->side); \
1311     } \
1312 }
1313
1314 static void get_xinerama_screens(Rect **xin_areas, guint *nxin)
1315 {
1316     guint i;
1317     gint n, l, r, t, b;
1318 #ifdef XINERAMA
1319     XineramaScreenInfo *info;
1320 #endif
1321
1322     if (ob_debug_xinerama) {
1323         gint w = WidthOfScreen(ScreenOfDisplay(obt_display, ob_screen));
1324         gint h = HeightOfScreen(ScreenOfDisplay(obt_display, ob_screen));
1325         *nxin = 2;
1326         *xin_areas = g_new(Rect, *nxin + 1);
1327         RECT_SET((*xin_areas)[0], 0, 0, w/2, h);
1328         RECT_SET((*xin_areas)[1], w/2, 0, w-(w/2), h);
1329     }
1330 #ifdef XINERAMA
1331     else if (obt_display_extension_xinerama &&
1332              (info = XineramaQueryScreens(obt_display, &n))) {
1333         *nxin = n;
1334         *xin_areas = g_new(Rect, *nxin + 1);
1335         for (i = 0; i < *nxin; ++i)
1336             RECT_SET((*xin_areas)[i], info[i].x_org, info[i].y_org,
1337                      info[i].width, info[i].height);
1338         XFree(info);
1339     }
1340 #endif
1341     else {
1342         *nxin = 1;
1343         *xin_areas = g_new(Rect, *nxin + 1);
1344         RECT_SET((*xin_areas)[0], 0, 0,
1345                  WidthOfScreen(ScreenOfDisplay(obt_display, ob_screen)),
1346                  HeightOfScreen(ScreenOfDisplay(obt_display, ob_screen)));
1347     }
1348
1349     /* returns one extra with the total area in it */
1350     l = (*xin_areas)[0].x;
1351     t = (*xin_areas)[0].y;
1352     r = (*xin_areas)[0].x + (*xin_areas)[0].width - 1;
1353     b = (*xin_areas)[0].y + (*xin_areas)[0].height - 1;
1354     for (i = 1; i < *nxin; ++i) {
1355         l = MIN(l, (*xin_areas)[i].x);
1356         t = MIN(l, (*xin_areas)[i].y);
1357         r = MAX(r, (*xin_areas)[i].x + (*xin_areas)[i].width - 1);
1358         b = MAX(b, (*xin_areas)[i].y + (*xin_areas)[i].height - 1);
1359     }
1360     RECT_SET((*xin_areas)[*nxin], l, t, r - l + 1, b - t + 1);
1361 }
1362
1363 void screen_update_areas(void)
1364 {
1365     guint i;
1366     gulong *dims;
1367     GList *it;
1368
1369     g_free(monitor_area);
1370     get_xinerama_screens(&monitor_area, &screen_num_monitors);
1371
1372     /* set up the user-specified margins */
1373     config_margins.top_start = RECT_LEFT(monitor_area[screen_num_monitors]);
1374     config_margins.top_end = RECT_RIGHT(monitor_area[screen_num_monitors]);
1375     config_margins.bottom_start = RECT_LEFT(monitor_area[screen_num_monitors]);
1376     config_margins.bottom_end = RECT_RIGHT(monitor_area[screen_num_monitors]);
1377     config_margins.left_start = RECT_TOP(monitor_area[screen_num_monitors]);
1378     config_margins.left_end = RECT_BOTTOM(monitor_area[screen_num_monitors]);
1379     config_margins.right_start = RECT_TOP(monitor_area[screen_num_monitors]);
1380     config_margins.right_end = RECT_BOTTOM(monitor_area[screen_num_monitors]);
1381
1382     RESET_STRUT_LIST(struts_left);
1383     RESET_STRUT_LIST(struts_top);
1384     RESET_STRUT_LIST(struts_right);
1385     RESET_STRUT_LIST(struts_bottom);
1386
1387     /* collect the struts */
1388     for (it = client_list; it; it = g_list_next(it)) {
1389         ObClient *c = it->data;
1390         if (c->strut.left)
1391             ADD_STRUT_TO_LIST(struts_left, c->desktop, &c->strut);
1392         if (c->strut.top)
1393             ADD_STRUT_TO_LIST(struts_top, c->desktop, &c->strut);
1394         if (c->strut.right)
1395             ADD_STRUT_TO_LIST(struts_right, c->desktop, &c->strut);
1396         if (c->strut.bottom)
1397             ADD_STRUT_TO_LIST(struts_bottom, c->desktop, &c->strut);
1398     }
1399     if (dock_strut.left)
1400         ADD_STRUT_TO_LIST(struts_left, DESKTOP_ALL, &dock_strut);
1401     if (dock_strut.top)
1402         ADD_STRUT_TO_LIST(struts_top, DESKTOP_ALL, &dock_strut);
1403     if (dock_strut.right)
1404         ADD_STRUT_TO_LIST(struts_right, DESKTOP_ALL, &dock_strut);
1405     if (dock_strut.bottom)
1406         ADD_STRUT_TO_LIST(struts_bottom, DESKTOP_ALL, &dock_strut);
1407
1408     if (config_margins.left)
1409         ADD_STRUT_TO_LIST(struts_left, DESKTOP_ALL, &config_margins);
1410     if (config_margins.top)
1411         ADD_STRUT_TO_LIST(struts_top, DESKTOP_ALL, &config_margins);
1412     if (config_margins.right)
1413         ADD_STRUT_TO_LIST(struts_right, DESKTOP_ALL, &config_margins);
1414     if (config_margins.bottom)
1415         ADD_STRUT_TO_LIST(struts_bottom, DESKTOP_ALL, &config_margins);
1416
1417     VALIDATE_STRUTS(struts_left, left,
1418                     monitor_area[screen_num_monitors].width / 2);
1419     VALIDATE_STRUTS(struts_right, right,
1420                     monitor_area[screen_num_monitors].width / 2);
1421     VALIDATE_STRUTS(struts_top, top,
1422                     monitor_area[screen_num_monitors].height / 2);
1423     VALIDATE_STRUTS(struts_bottom, bottom,
1424                     monitor_area[screen_num_monitors].height / 2);
1425
1426     dims = g_new(gulong, 4 * screen_num_desktops);
1427     for (i = 0; i < screen_num_desktops; ++i) {
1428         Rect *area = screen_area(i, SCREEN_AREA_ALL_MONITORS, NULL);
1429         dims[i*4+0] = area->x;
1430         dims[i*4+1] = area->y;
1431         dims[i*4+2] = area->width;
1432         dims[i*4+3] = area->height;
1433         g_free(area);
1434     }
1435
1436     /* set the legacy workarea hint to the union of all the monitors */
1437     OBT_PROP_SETA32(obt_root(ob_screen), NET_WORKAREA, CARDINAL,
1438                     dims, 4 * screen_num_desktops);
1439
1440     /* the area has changed, adjust all the windows if they need it */
1441     for (it = client_list; it; it = g_list_next(it))
1442         client_reconfigure(it->data, FALSE);
1443
1444     g_free(dims);
1445 }
1446
1447 #if 0
1448 Rect* screen_area_all_monitors(guint desktop)
1449 {
1450     guint i;
1451     Rect *a;
1452
1453     a = screen_area_monitor(desktop, 0);
1454
1455     /* combine all the monitors together */
1456     for (i = 1; i < screen_num_monitors; ++i) {
1457         Rect *m = screen_area_monitor(desktop, i);
1458         gint l, r, t, b;
1459
1460         l = MIN(RECT_LEFT(*a), RECT_LEFT(*m));
1461         t = MIN(RECT_TOP(*a), RECT_TOP(*m));
1462         r = MAX(RECT_RIGHT(*a), RECT_RIGHT(*m));
1463         b = MAX(RECT_BOTTOM(*a), RECT_BOTTOM(*m));
1464
1465         RECT_SET(*a, l, t, r - l + 1, b - t + 1);
1466
1467         g_free(m);
1468     }
1469
1470     return a;
1471 }
1472 #endif
1473
1474 #define STRUT_LEFT_IN_SEARCH(s, search) \
1475     (RANGES_INTERSECT(search->y, search->height, \
1476                       s->left_start, s->left_end - s->left_start + 1))
1477 #define STRUT_RIGHT_IN_SEARCH(s, search) \
1478     (RANGES_INTERSECT(search->y, search->height, \
1479                       s->right_start, s->right_end - s->right_start + 1))
1480 #define STRUT_TOP_IN_SEARCH(s, search) \
1481     (RANGES_INTERSECT(search->x, search->width, \
1482                       s->top_start, s->top_end - s->top_start + 1))
1483 #define STRUT_BOTTOM_IN_SEARCH(s, search) \
1484     (RANGES_INTERSECT(search->x, search->width, \
1485                       s->bottom_start, s->bottom_end - s->bottom_start + 1))
1486
1487 #define STRUT_LEFT_IGNORE(s, us, search) \
1488     (head == SCREEN_AREA_ALL_MONITORS && us && \
1489      RECT_LEFT(monitor_area[i]) + s->left > RECT_LEFT(*search))
1490 #define STRUT_RIGHT_IGNORE(s, us, search) \
1491     (head == SCREEN_AREA_ALL_MONITORS && us && \
1492      RECT_RIGHT(monitor_area[i]) - s->right < RECT_RIGHT(*search))
1493 #define STRUT_TOP_IGNORE(s, us, search) \
1494     (head == SCREEN_AREA_ALL_MONITORS && us && \
1495      RECT_TOP(monitor_area[i]) + s->top > RECT_TOP(*search))
1496 #define STRUT_BOTTOM_IGNORE(s, us, search) \
1497     (head == SCREEN_AREA_ALL_MONITORS && us && \
1498      RECT_BOTTOM(monitor_area[i]) - s->bottom < RECT_BOTTOM(*search))
1499
1500 Rect* screen_area(guint desktop, guint head, Rect *search)
1501 {
1502     Rect *a;
1503     GSList *it;
1504     gint l, r, t, b;
1505     guint i, d;
1506     gboolean us = search != NULL; /* user provided search */
1507
1508     g_assert(desktop < screen_num_desktops || desktop == DESKTOP_ALL);
1509     g_assert(head < screen_num_monitors || head == SCREEN_AREA_ONE_MONITOR ||
1510              head == SCREEN_AREA_ALL_MONITORS);
1511     g_assert(!(head == SCREEN_AREA_ONE_MONITOR && search == NULL));
1512
1513     /* find any struts for this monitor
1514        which will be affecting the search area.
1515     */
1516
1517     /* search everything if search is null */
1518     if (!search) {
1519         if (head < screen_num_monitors) search = &monitor_area[head];
1520         else search = &monitor_area[screen_num_monitors];
1521     }
1522     if (head == SCREEN_AREA_ONE_MONITOR) head = screen_find_monitor(search);
1523
1524     /* al is "all left" meaning the furthest left you can get, l is our
1525        "working left" meaning our current strut edge which we're calculating
1526     */
1527
1528     /* only include monitors which the search area lines up with */
1529     if (RECT_INTERSECTS_RECT(monitor_area[screen_num_monitors], *search)) {
1530         l = RECT_RIGHT(monitor_area[screen_num_monitors]);
1531         t = RECT_BOTTOM(monitor_area[screen_num_monitors]);
1532         r = RECT_LEFT(monitor_area[screen_num_monitors]);
1533         b = RECT_TOP(monitor_area[screen_num_monitors]);
1534         for (i = 0; i < screen_num_monitors; ++i) {
1535             /* add the monitor if applicable */
1536             if (RANGES_INTERSECT(search->x, search->width,
1537                                  monitor_area[i].x, monitor_area[i].width))
1538             {
1539                 t = MIN(t, RECT_TOP(monitor_area[i]));
1540                 b = MAX(b, RECT_BOTTOM(monitor_area[i]));
1541             }
1542             if (RANGES_INTERSECT(search->y, search->height,
1543                                  monitor_area[i].y, monitor_area[i].height))
1544             {
1545                 l = MIN(l, RECT_LEFT(monitor_area[i]));
1546                 r = MAX(r, RECT_RIGHT(monitor_area[i]));
1547             }
1548         }
1549     } else {
1550         l = RECT_LEFT(monitor_area[screen_num_monitors]);
1551         t = RECT_TOP(monitor_area[screen_num_monitors]);
1552         r = RECT_RIGHT(monitor_area[screen_num_monitors]);
1553         b = RECT_BOTTOM(monitor_area[screen_num_monitors]);
1554     }
1555
1556     for (d = 0; d < screen_num_desktops; ++d) {
1557         if (d != desktop && desktop != DESKTOP_ALL) continue;
1558
1559         for (i = 0; i < screen_num_monitors; ++i) {
1560             if (head != SCREEN_AREA_ALL_MONITORS && head != i) continue;
1561
1562             for (it = struts_left; it; it = g_slist_next(it)) {
1563                 ObScreenStrut *s = it->data;
1564                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1565                     STRUT_LEFT_IN_SEARCH(s->strut, search) &&
1566                     !STRUT_LEFT_IGNORE(s->strut, us, search))
1567                     l = MAX(l, RECT_LEFT(monitor_area[screen_num_monitors])
1568                                + s->strut->left);
1569             }
1570             for (it = struts_top; it; it = g_slist_next(it)) {
1571                 ObScreenStrut *s = it->data;
1572                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1573                     STRUT_TOP_IN_SEARCH(s->strut, search) &&
1574                     !STRUT_TOP_IGNORE(s->strut, us, search))
1575                     t = MAX(t, RECT_TOP(monitor_area[screen_num_monitors])
1576                                + s->strut->top);
1577             }
1578             for (it = struts_right; it; it = g_slist_next(it)) {
1579                 ObScreenStrut *s = it->data;
1580                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1581                     STRUT_RIGHT_IN_SEARCH(s->strut, search) &&
1582                     !STRUT_RIGHT_IGNORE(s->strut, us, search))
1583                     r = MIN(r, RECT_RIGHT(monitor_area[screen_num_monitors])
1584                                - s->strut->right);
1585             }
1586             for (it = struts_bottom; it; it = g_slist_next(it)) {
1587                 ObScreenStrut *s = it->data;
1588                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1589                     STRUT_BOTTOM_IN_SEARCH(s->strut, search) &&
1590                     !STRUT_BOTTOM_IGNORE(s->strut, us, search))
1591                     b = MIN(b, RECT_BOTTOM(monitor_area[screen_num_monitors])
1592                                - s->strut->bottom);
1593             }
1594
1595             /* limit to this monitor */
1596             if (head == i) {
1597                 l = MAX(l, RECT_LEFT(monitor_area[i]));
1598                 t = MAX(t, RECT_TOP(monitor_area[i]));
1599                 r = MIN(r, RECT_RIGHT(monitor_area[i]));
1600                 b = MIN(b, RECT_BOTTOM(monitor_area[i]));
1601             }
1602         }
1603     }
1604
1605     a = g_new(Rect, 1);
1606     a->x = l;
1607     a->y = t;
1608     a->width = r - l + 1;
1609     a->height = b - t + 1;
1610     return a;
1611 }
1612
1613 guint screen_find_monitor(Rect *search)
1614 {
1615     guint i;
1616     guint most = screen_num_monitors;
1617     guint mostv = 0;
1618
1619     for (i = 0; i < screen_num_monitors; ++i) {
1620         Rect *area = screen_physical_area_monitor(i);
1621         if (RECT_INTERSECTS_RECT(*area, *search)) {
1622             Rect r;
1623             guint v;
1624
1625             RECT_SET_INTERSECTION(r, *area, *search);
1626             v = r.width * r.height;
1627
1628             if (v > mostv) {
1629                 mostv = v;
1630                 most = i;
1631             }
1632         }
1633         g_free(area);
1634     }
1635     return most;
1636 }
1637
1638 Rect* screen_physical_area_all_monitors(void)
1639 {
1640     return screen_physical_area_monitor(screen_num_monitors);
1641 }
1642
1643 Rect* screen_physical_area_monitor(guint head)
1644 {
1645     Rect *a;
1646     g_assert(head <= screen_num_monitors);
1647
1648     a = g_new(Rect, 1);
1649     *a = monitor_area[head];
1650     return a;
1651 }
1652
1653 gboolean screen_physical_area_monitor_contains(guint head, Rect *search)
1654 {
1655     g_assert(head <= screen_num_monitors);
1656     g_assert(search);
1657     return RECT_INTERSECTS_RECT(monitor_area[head], *search);
1658 }
1659
1660 guint screen_monitor_active(void)
1661 {
1662     if (moveresize_client)
1663         return client_monitor(moveresize_client);
1664     else if (focus_client)
1665         return client_monitor(focus_client);
1666     else
1667         return screen_monitor_pointer();
1668 }
1669
1670 Rect* screen_physical_area_active(void)
1671 {
1672     return screen_physical_area_monitor(screen_monitor_active());
1673 }
1674
1675 guint screen_monitor_primary(gboolean fixed)
1676 {
1677     if (config_primary_monitor_index > 0) {
1678         if (config_primary_monitor_index-1 < screen_num_monitors)
1679             return config_primary_monitor_index - 1;
1680         else
1681             return 0;
1682     }
1683     else if (fixed)
1684         return 0;
1685     else if (config_primary_monitor == OB_PLACE_MONITOR_ACTIVE)
1686         return screen_monitor_active();
1687     else /* config_primary_monitor == OB_PLACE_MONITOR_MOUSE */
1688         return screen_monitor_pointer();
1689 }
1690
1691 Rect *screen_physical_area_primary(gboolean fixed)
1692 {
1693     return screen_physical_area_monitor(screen_monitor_primary(fixed));
1694 }
1695
1696 void screen_set_root_cursor(void)
1697 {
1698     if (sn_app_starting())
1699         XDefineCursor(obt_display, obt_root(ob_screen),
1700                       ob_cursor(OB_CURSOR_BUSYPOINTER));
1701     else
1702         XDefineCursor(obt_display, obt_root(ob_screen),
1703                       ob_cursor(OB_CURSOR_POINTER));
1704 }
1705
1706 guint screen_find_monitor_point(guint x, guint y)
1707 {
1708     Rect mon;
1709     RECT_SET(mon, x, y, 1, 1);
1710     return screen_find_monitor(&mon);
1711 }
1712
1713 guint screen_monitor_pointer()
1714 {
1715     gint x, y;
1716     if (!screen_pointer_pos(&x, &y))
1717         x = y = 0;
1718     return screen_find_monitor_point(x, y);
1719 }
1720
1721 gboolean screen_pointer_pos(gint *x, gint *y)
1722 {
1723     Window w;
1724     gint i;
1725     guint u;
1726     gboolean ret;
1727
1728     ret = !!XQueryPointer(obt_display, obt_root(ob_screen),
1729                           &w, &w, x, y, &i, &i, &u);
1730     if (!ret) {
1731         for (i = 0; i < ScreenCount(obt_display); ++i)
1732             if (i != ob_screen)
1733                 if (XQueryPointer(obt_display, obt_root(i),
1734                                   &w, &w, x, y, &i, &i, &u))
1735                     break;
1736     }
1737     return ret;
1738 }