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