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