improve the add/remove desktop actions a bunch.
[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 = NULL;
70 /*! An array of desktops, holding an array of struts */
71 static GSList *struts_top = NULL;
72 static GSList *struts_left = NULL;
73 static GSList *struts_right = NULL;
74 static GSList *struts_bottom = NULL;
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, *stacking_copy;
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        make a copy of the list cuz we're changing it */
521     stacking_copy = g_list_copy(stacking_list);
522     for (it = g_list_last(stacking_copy); it; it = g_list_previous(it)) {
523         if (WINDOW_IS_CLIENT(it->data)) {
524             ObClient *c = it->data;
525             if (c->desktop != DESKTOP_ALL && c->desktop >= num)
526                 client_set_desktop(c, num - 1, FALSE, TRUE);
527             /* raise all the windows that are on the current desktop which
528                is being merged */
529             else if (c->desktop == DESKTOP_ALL ||
530                      c->desktop == num - 1)
531                 stacking_raise(WINDOW_AS_CLIENT(c));
532         }
533     }
534  
535     /* change our struts/area to match (after moving windows) */
536     screen_update_areas();
537
538     /* may be some unnamed desktops that we need to fill in with names
539      (after updating the areas so the popup can resize) */
540     screen_update_desktop_names();
541
542     /* change our desktop if we're on one that no longer exists! */
543     if (screen_desktop >= screen_num_desktops)
544         screen_set_desktop(num - 1, TRUE);
545 }
546
547 void screen_set_desktop(guint num, gboolean dofocus)
548 {
549     ObClient *c;
550     GList *it;
551     guint old;
552     gulong ignore_start;
553     gboolean allow_omni;
554      
555     g_assert(num < screen_num_desktops);
556
557     old = screen_desktop;
558     screen_desktop = num;
559
560     if (old == num) return;
561
562     PROP_SET32(RootWindow(ob_display, ob_screen),
563                net_current_desktop, cardinal, num);
564
565     screen_last_desktop = old;
566
567     ob_debug("Moving to desktop %d\n", num+1);
568
569     /* ignore enter events caused by the move */
570     ignore_start = event_start_ignore_all_enters();
571
572     if (moveresize_client)
573         client_set_desktop(moveresize_client, num, TRUE, FALSE);
574
575     /* show windows before hiding the rest to lessen the enter/leave events */
576
577     /* show windows from top to bottom */
578     for (it = stacking_list; it; it = g_list_next(it)) {
579         if (WINDOW_IS_CLIENT(it->data)) {
580             ObClient *c = it->data;
581             client_show(c);
582         }
583     }
584
585     /* only allow omnipresent windows to get focus on desktop change if
586        an omnipresent window is already focused (it'll keep focus probably, but
587        maybe not depending on mouse-focus options) */
588     allow_omni = focus_client && (client_normal(focus_client) &&
589                                   focus_client->desktop == DESKTOP_ALL);
590
591     /* the client moved there already so don't move focus. prevent flicker
592        on sendtodesktop + follow */
593     if (focus_client && focus_client->desktop == screen_desktop)
594         dofocus = FALSE;
595
596     /* have to try focus here because when you leave an empty desktop
597        there is no focus out to watch for. also, we have different rules
598        here. we always allow it to look under the mouse pointer if
599        config_focus_last is FALSE
600
601        do this before hiding the windows so if helper windows are coming
602        with us, they don't get hidden
603     */
604     if (dofocus && (c = focus_fallback(TRUE, !config_focus_last, allow_omni)))
605     {
606         /* only do the flicker reducing stuff ahead of time if we are going
607            to call xsetinputfocus on the window ourselves. otherwise there is
608            no guarantee the window will actually take focus.. */
609         if (c->can_focus) {
610             /* reduce flicker by hiliting now rather than waiting for the
611                server FocusIn event */
612             frame_adjust_focus(c->frame, TRUE);
613             /* do this here so that if you switch desktops to a window with
614                helper windows then the helper windows won't flash */
615             client_bring_helper_windows(c);
616         }
617     }
618
619     /* hide windows from bottom to top */
620     for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
621         if (WINDOW_IS_CLIENT(it->data)) {
622             ObClient *c = it->data;
623             client_hide(c);
624         }
625     }
626
627     event_end_ignore_all_enters(ignore_start);
628
629     if (event_curtime != CurrentTime)
630         screen_desktop_user_time = event_curtime;
631 }
632
633 static void get_row_col(guint d, guint *r, guint *c)
634 {
635     switch (screen_desktop_layout.orientation) {
636     case OB_ORIENTATION_HORZ:
637         switch (screen_desktop_layout.start_corner) {
638         case OB_CORNER_TOPLEFT:
639             *r = d / screen_desktop_layout.columns;
640             *c = d % screen_desktop_layout.columns;
641             break;
642         case OB_CORNER_BOTTOMLEFT:
643             *r = screen_desktop_layout.rows - 1 -
644                 d / screen_desktop_layout.columns;
645             *c = d % screen_desktop_layout.columns;
646             break;
647         case OB_CORNER_TOPRIGHT:
648             *r = d / screen_desktop_layout.columns;
649             *c = screen_desktop_layout.columns - 1 -
650                 d % screen_desktop_layout.columns;
651             break;
652         case OB_CORNER_BOTTOMRIGHT:
653             *r = screen_desktop_layout.rows - 1 -
654                 d / screen_desktop_layout.columns;
655             *c = screen_desktop_layout.columns - 1 -
656                 d % screen_desktop_layout.columns;
657             break;
658         }
659         break;
660     case OB_ORIENTATION_VERT:
661         switch (screen_desktop_layout.start_corner) {
662         case OB_CORNER_TOPLEFT:
663             *r = d % screen_desktop_layout.rows;
664             *c = d / screen_desktop_layout.rows;
665             break;
666         case OB_CORNER_BOTTOMLEFT:
667             *r = screen_desktop_layout.rows - 1 -
668                 d % screen_desktop_layout.rows;
669             *c = d / screen_desktop_layout.rows;
670             break;
671         case OB_CORNER_TOPRIGHT:
672             *r = d % screen_desktop_layout.rows;
673             *c = screen_desktop_layout.columns - 1 -
674                 d / screen_desktop_layout.rows;
675             break;
676         case OB_CORNER_BOTTOMRIGHT:
677             *r = screen_desktop_layout.rows - 1 -
678                 d % screen_desktop_layout.rows;
679             *c = screen_desktop_layout.columns - 1 -
680                 d / screen_desktop_layout.rows;
681             break;
682         }
683         break;
684     }
685 }
686
687 static guint translate_row_col(guint r, guint c)
688 {
689     switch (screen_desktop_layout.orientation) {
690     case OB_ORIENTATION_HORZ:
691         switch (screen_desktop_layout.start_corner) {
692         case OB_CORNER_TOPLEFT:
693             return r % screen_desktop_layout.rows *
694                 screen_desktop_layout.columns +
695                 c % screen_desktop_layout.columns;
696         case OB_CORNER_BOTTOMLEFT:
697             return (screen_desktop_layout.rows - 1 -
698                     r % screen_desktop_layout.rows) *
699                 screen_desktop_layout.columns +
700                 c % screen_desktop_layout.columns;
701         case OB_CORNER_TOPRIGHT:
702             return r % screen_desktop_layout.rows *
703                 screen_desktop_layout.columns +
704                 (screen_desktop_layout.columns - 1 -
705                  c % screen_desktop_layout.columns);
706         case OB_CORNER_BOTTOMRIGHT:
707             return (screen_desktop_layout.rows - 1 -
708                     r % screen_desktop_layout.rows) *
709                 screen_desktop_layout.columns +
710                 (screen_desktop_layout.columns - 1 -
711                  c % screen_desktop_layout.columns);
712         }
713     case OB_ORIENTATION_VERT:
714         switch (screen_desktop_layout.start_corner) {
715         case OB_CORNER_TOPLEFT:
716             return c % screen_desktop_layout.columns *
717                 screen_desktop_layout.rows +
718                 r % screen_desktop_layout.rows;
719         case OB_CORNER_BOTTOMLEFT:
720             return c % screen_desktop_layout.columns *
721                 screen_desktop_layout.rows +
722                 (screen_desktop_layout.rows - 1 -
723                  r % screen_desktop_layout.rows);
724         case OB_CORNER_TOPRIGHT:
725             return (screen_desktop_layout.columns - 1 -
726                     c % screen_desktop_layout.columns) *
727                 screen_desktop_layout.rows +
728                 r % screen_desktop_layout.rows;
729         case OB_CORNER_BOTTOMRIGHT:
730             return (screen_desktop_layout.columns - 1 -
731                     c % screen_desktop_layout.columns) *
732                 screen_desktop_layout.rows +
733                 (screen_desktop_layout.rows - 1 -
734                  r % screen_desktop_layout.rows);
735         }
736     }
737     g_assert_not_reached();
738     return 0;
739 }
740
741 void screen_desktop_popup(guint d, gboolean show)
742 {
743     Rect *a;
744
745     if (!show) {
746         pager_popup_hide(desktop_cycle_popup);
747     } else {
748         a = screen_physical_area_active();
749         pager_popup_position(desktop_cycle_popup, CenterGravity,
750                              a->x + a->width / 2, a->y + a->height / 2);
751         pager_popup_icon_size_multiplier(desktop_cycle_popup,
752                                          (screen_desktop_layout.columns /
753                                           screen_desktop_layout.rows) / 2,
754                                          (screen_desktop_layout.rows/
755                                           screen_desktop_layout.columns) / 2);
756         pager_popup_max_width(desktop_cycle_popup,
757                               MAX(a->width/3, POPUP_WIDTH));
758         pager_popup_show(desktop_cycle_popup, screen_desktop_names[d], d);
759     }
760 }
761
762 guint screen_cycle_desktop(ObDirection dir, gboolean wrap, gboolean linear,
763                            gboolean dialog, gboolean done, gboolean cancel)
764 {
765     guint r, c;
766     static guint d = (guint)-1;
767     guint ret, oldd;
768
769     if (d == (guint)-1)
770         d = screen_desktop;
771
772     if ((cancel || done) && dialog)
773         goto show_cycle_dialog;
774
775     oldd = d;
776     get_row_col(d, &r, &c);
777
778     if (linear) {
779         switch (dir) {
780         case OB_DIRECTION_EAST:
781             if (d < screen_num_desktops - 1)
782                 ++d;
783             else if (wrap)
784                 d = 0;
785             break;
786         case OB_DIRECTION_WEST:
787             if (d > 0)
788                 --d;
789             else if (wrap)
790                 d = screen_num_desktops - 1;
791             break;
792         default:
793             assert(0);
794             return screen_desktop;
795         }
796     } else {
797         switch (dir) {
798         case OB_DIRECTION_EAST:
799             ++c;
800             if (c >= screen_desktop_layout.columns) {
801                 if (wrap)
802                     c = 0;
803                 else
804                     goto show_cycle_dialog;
805             }
806             d = translate_row_col(r, c);
807             if (d >= screen_num_desktops) {
808                 if (wrap) {
809                     ++c;
810                 } else {
811                     d = oldd;
812                     goto show_cycle_dialog;
813                 }
814             }
815             break;
816         case OB_DIRECTION_WEST:
817             --c;
818             if (c >= screen_desktop_layout.columns) {
819                 if (wrap)
820                     c = screen_desktop_layout.columns - 1;
821                 else
822                     goto show_cycle_dialog;
823             }
824             d = translate_row_col(r, c);
825             if (d >= screen_num_desktops) {
826                 if (wrap) {
827                     --c;
828                 } else {
829                     d = oldd;
830                     goto show_cycle_dialog;
831                 }
832             }
833             break;
834         case OB_DIRECTION_SOUTH:
835             ++r;
836             if (r >= screen_desktop_layout.rows) {
837                 if (wrap)
838                     r = 0;
839                 else
840                     goto show_cycle_dialog;
841             }
842             d = translate_row_col(r, c);
843             if (d >= screen_num_desktops) {
844                 if (wrap) {
845                     ++r;
846                 } else {
847                     d = oldd;
848                     goto show_cycle_dialog;
849                 }
850             }
851             break;
852         case OB_DIRECTION_NORTH:
853             --r;
854             if (r >= screen_desktop_layout.rows) {
855                 if (wrap)
856                     r = screen_desktop_layout.rows - 1;
857                 else
858                     goto show_cycle_dialog;
859             }
860             d = translate_row_col(r, c);
861             if (d >= screen_num_desktops) {
862                 if (wrap) {
863                     --r;
864                 } else {
865                     d = oldd;
866                     goto show_cycle_dialog;
867                 }
868             }
869             break;
870         default:
871             assert(0);
872             return d = screen_desktop;
873         }
874
875         d = translate_row_col(r, c);
876     }
877
878 show_cycle_dialog:
879     if (dialog && !cancel && !done) {
880         screen_desktop_popup(d, TRUE);
881     } else
882         screen_desktop_popup(0, FALSE);
883     ret = d;
884
885     if (!dialog || cancel || done)
886         d = (guint)-1;
887
888     return ret;
889 }
890
891 static gboolean screen_validate_layout(ObDesktopLayout *l)
892 {
893     if (l->columns == 0 && l->rows == 0) /* both 0's is bad data.. */
894         return FALSE;
895
896     /* fill in a zero rows/columns */
897     if (l->columns == 0) {
898         l->columns = screen_num_desktops / l->rows;
899         if (l->rows * l->columns < screen_num_desktops)
900             l->columns++;
901         if (l->rows * l->columns >= screen_num_desktops + l->columns)
902             l->rows--;
903     } else if (l->rows == 0) {
904         l->rows = screen_num_desktops / l->columns;
905         if (l->columns * l->rows < screen_num_desktops)
906             l->rows++;
907         if (l->columns * l->rows >= screen_num_desktops + l->rows)
908             l->columns--;
909     }
910
911     /* bounds checking */
912     if (l->orientation == OB_ORIENTATION_HORZ) {
913         l->columns = MIN(screen_num_desktops, l->columns);
914         l->rows = MIN(l->rows,
915                       (screen_num_desktops + l->columns - 1) / l->columns);
916         l->columns = screen_num_desktops / l->rows +
917             !!(screen_num_desktops % l->rows);
918     } else {
919         l->rows = MIN(screen_num_desktops, l->rows);
920         l->columns = MIN(l->columns,
921                          (screen_num_desktops + l->rows - 1) / l->rows);
922         l->rows = screen_num_desktops / l->columns +
923             !!(screen_num_desktops % l->columns);
924     }
925     return TRUE;
926 }
927
928 void screen_update_layout()
929
930 {
931     ObDesktopLayout l;
932     guint32 *data;
933     guint num;
934
935     screen_desktop_layout.orientation = OB_ORIENTATION_HORZ;
936     screen_desktop_layout.start_corner = OB_CORNER_TOPLEFT;
937     screen_desktop_layout.rows = 1;
938     screen_desktop_layout.columns = screen_num_desktops;
939
940     if (PROP_GETA32(RootWindow(ob_display, ob_screen),
941                     net_desktop_layout, cardinal, &data, &num)) {
942         if (num == 3 || num == 4) {
943             
944             if (data[0] == prop_atoms.net_wm_orientation_vert)
945                 l.orientation = OB_ORIENTATION_VERT;
946             else if (data[0] == prop_atoms.net_wm_orientation_horz)
947                 l.orientation = OB_ORIENTATION_HORZ;
948             else
949                 return;
950
951             if (num < 4)
952                 l.start_corner = OB_CORNER_TOPLEFT;
953             else {
954                 if (data[3] == prop_atoms.net_wm_topleft)
955                     l.start_corner = OB_CORNER_TOPLEFT;
956                 else if (data[3] == prop_atoms.net_wm_topright)
957                     l.start_corner = OB_CORNER_TOPRIGHT;
958                 else if (data[3] == prop_atoms.net_wm_bottomright)
959                     l.start_corner = OB_CORNER_BOTTOMRIGHT;
960                 else if (data[3] == prop_atoms.net_wm_bottomleft)
961                     l.start_corner = OB_CORNER_BOTTOMLEFT;
962                 else
963                     return;
964             }
965
966             l.columns = data[1];
967             l.rows = data[2];
968
969             if (screen_validate_layout(&l))
970                 screen_desktop_layout = l;
971
972             g_free(data);
973         }
974     }
975 }
976
977 void screen_update_desktop_names()
978 {
979     guint i;
980
981     /* empty the array */
982     g_strfreev(screen_desktop_names);
983     screen_desktop_names = NULL;
984
985     if (PROP_GETSS(RootWindow(ob_display, ob_screen),
986                    net_desktop_names, utf8, &screen_desktop_names))
987         for (i = 0; screen_desktop_names[i] && i < screen_num_desktops; ++i);
988     else
989         i = 0;
990     if (i < screen_num_desktops) {
991         GSList *it;
992
993         screen_desktop_names = g_renew(gchar*, screen_desktop_names,
994                                        screen_num_desktops + 1);
995         screen_desktop_names[screen_num_desktops] = NULL;
996
997         it = g_slist_nth(config_desktops_names, i);
998
999         for (; i < screen_num_desktops; ++i) {
1000             if (it && ((char*)it->data)[0]) /* not empty */
1001                 /* use the names from the config file when possible */
1002                 screen_desktop_names[i] = g_strdup(it->data);
1003             else
1004                 /* make up a nice name if it's not though */
1005                 screen_desktop_names[i] = g_strdup_printf(_("desktop %i"),
1006                                                           i + 1);
1007             if (it) it = g_slist_next(it);
1008         }
1009
1010         /* if we changed any names, then set the root property so we can
1011            all agree on the names */
1012         PROP_SETSS(RootWindow(ob_display, ob_screen), net_desktop_names,
1013                    screen_desktop_names);
1014     }
1015
1016     /* resize the pager for these names */
1017     pager_popup_text_width_to_strings(desktop_cycle_popup,
1018                                       screen_desktop_names,
1019                                       screen_num_desktops);
1020 }
1021
1022 void screen_show_desktop(gboolean show, ObClient *show_only)
1023 {
1024     GList *it;
1025      
1026     if (show == screen_showing_desktop) return; /* no change */
1027
1028     screen_showing_desktop = show;
1029
1030     if (show) {
1031         /* hide windows bottom to top */
1032         for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
1033             if (WINDOW_IS_CLIENT(it->data)) {
1034                 ObClient *client = it->data;
1035                 client_showhide(client);
1036             }
1037         }
1038     }
1039     else {
1040         /* restore windows top to bottom */
1041         for (it = stacking_list; it; it = g_list_next(it)) {
1042             if (WINDOW_IS_CLIENT(it->data)) {
1043                 ObClient *client = it->data;
1044                 if (client_should_show(client)) {
1045                     if (!show_only || client == show_only)
1046                         client_show(client);
1047                     else
1048                         client_iconify(client, TRUE, FALSE, TRUE);
1049                 }
1050             }
1051         }
1052     }
1053
1054     if (show) {
1055         /* focus the desktop */
1056         for (it = focus_order; it; it = g_list_next(it)) {
1057             ObClient *c = it->data;
1058             if (c->type == OB_CLIENT_TYPE_DESKTOP &&
1059                 (c->desktop == screen_desktop || c->desktop == DESKTOP_ALL) &&
1060                 client_focus(it->data))
1061                 break;
1062         }
1063     }
1064     else if (!show_only) {
1065         ObClient *c;
1066
1067         if ((c = focus_fallback(TRUE, FALSE, TRUE))) {
1068             /* only do the flicker reducing stuff ahead of time if we are going
1069                to call xsetinputfocus on the window ourselves. otherwise there
1070                is no guarantee the window will actually take focus.. */
1071             if (c->can_focus) {
1072                 /* reduce flicker by hiliting now rather than waiting for the
1073                    server FocusIn event */
1074                 frame_adjust_focus(c->frame, TRUE);
1075             }
1076         }
1077     }
1078
1079     show = !!show; /* make it boolean */
1080     PROP_SET32(RootWindow(ob_display, ob_screen),
1081                net_showing_desktop, cardinal, show);
1082 }
1083
1084 void screen_install_colormap(ObClient *client, gboolean install)
1085 {
1086     if (client == NULL || client->colormap == None) {
1087         if (install)
1088             XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
1089         else
1090             XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
1091     } else {
1092         xerror_set_ignore(TRUE);
1093         if (install)
1094             XInstallColormap(RrDisplay(ob_rr_inst), client->colormap);
1095         else
1096             XUninstallColormap(RrDisplay(ob_rr_inst), client->colormap);
1097         xerror_set_ignore(FALSE);
1098     }
1099 }
1100
1101 #define STRUT_LEFT_ON_MONITOR(s, i) \
1102     (RANGES_INTERSECT(s->left_start, s->left_end - s->left_start + 1, \
1103                       monitor_area[i].y, monitor_area[i].height))
1104 #define STRUT_RIGHT_ON_MONITOR(s, i) \
1105     (RANGES_INTERSECT(s->right_start, s->right_end - s->right_start + 1, \
1106                       monitor_area[i].y, monitor_area[i].height))
1107 #define STRUT_TOP_ON_MONITOR(s, i) \
1108     (RANGES_INTERSECT(s->top_start, s->top_end - s->top_start + 1, \
1109                       monitor_area[i].x, monitor_area[i].width))
1110 #define STRUT_BOTTOM_ON_MONITOR(s, i) \
1111     (RANGES_INTERSECT(s->bottom_start, s->bottom_end - s->bottom_start + 1, \
1112                       monitor_area[i].x, monitor_area[i].width))
1113
1114 typedef struct {
1115     guint desktop;
1116     StrutPartial *strut;
1117 } ObScreenStrut;
1118
1119 #define RESET_STRUT_LIST(sl) \
1120     (g_slist_free(sl), sl = NULL)
1121
1122 #define ADD_STRUT_TO_LIST(sl, d, s) \
1123 { \
1124     ObScreenStrut *ss = g_new(ObScreenStrut, 1); \
1125     ss->desktop = d; \
1126     ss->strut = s;  \
1127     sl = g_slist_prepend(sl, ss); \
1128 }
1129
1130 void screen_update_areas()
1131 {
1132     guint i, j;
1133     gulong *dims;
1134     GList *it;
1135     GSList *sit;
1136
1137     ob_debug("updating screen areas\n");
1138
1139     g_free(monitor_area);
1140     extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
1141
1142     dims = g_new(gulong, 4 * screen_num_desktops * screen_num_monitors);
1143
1144     RESET_STRUT_LIST(struts_left);
1145     RESET_STRUT_LIST(struts_top);
1146     RESET_STRUT_LIST(struts_right);
1147     RESET_STRUT_LIST(struts_bottom);
1148
1149     /* collect the struts */
1150     for (it = client_list; it; it = g_list_next(it)) {
1151         ObClient *c = it->data;
1152         if (c->strut.left)
1153             ADD_STRUT_TO_LIST(struts_left, c->desktop, &c->strut);
1154         if (c->strut.top)
1155             ADD_STRUT_TO_LIST(struts_top, c->desktop, &c->strut);
1156         if (c->strut.right)
1157             ADD_STRUT_TO_LIST(struts_right, c->desktop, &c->strut);
1158         if (c->strut.bottom)
1159             ADD_STRUT_TO_LIST(struts_bottom, c->desktop, &c->strut);
1160     }
1161     if (dock_strut.left)
1162         ADD_STRUT_TO_LIST(struts_left, DESKTOP_ALL, &dock_strut);
1163     if (dock_strut.top)
1164         ADD_STRUT_TO_LIST(struts_top, DESKTOP_ALL, &dock_strut);
1165     if (dock_strut.right)
1166         ADD_STRUT_TO_LIST(struts_right, DESKTOP_ALL, &dock_strut);
1167     if (dock_strut.bottom)
1168         ADD_STRUT_TO_LIST(struts_bottom, DESKTOP_ALL, &dock_strut);
1169
1170     /* set up the work areas to be full screen */
1171     for (i = 0; i < screen_num_monitors; ++i)
1172         for (j = 0; j < screen_num_desktops; ++j) {
1173             dims[(i * screen_num_desktops + j) * 4+0] = monitor_area[i].x;
1174             dims[(i * screen_num_desktops + j) * 4+1] = monitor_area[i].y;
1175             dims[(i * screen_num_desktops + j) * 4+2] = monitor_area[i].width;
1176             dims[(i * screen_num_desktops + j) * 4+3] = monitor_area[i].height;
1177         }
1178
1179     /* calculate the work areas from the struts */
1180     for (i = 0; i < screen_num_monitors; ++i)
1181         for (j = 0; j < screen_num_desktops; ++j) {
1182             gint l = 0, r = 0, t = 0, b = 0;
1183
1184             /* only add the strut to the area if it touches the monitor */
1185
1186             for (sit = struts_left; sit; sit = g_slist_next(sit)) {
1187                 ObScreenStrut *s = sit->data;
1188                 if ((s->desktop == j || s->desktop == DESKTOP_ALL) &&
1189                     STRUT_LEFT_ON_MONITOR(s->strut, i))
1190                     l = MAX(l, s->strut->left);
1191             }
1192             for (sit = struts_top; sit; sit = g_slist_next(sit)) {
1193                 ObScreenStrut *s = sit->data;
1194                 if ((s->desktop == j || s->desktop == DESKTOP_ALL) &&
1195                     STRUT_TOP_ON_MONITOR(s->strut, i))
1196                     t = MAX(t, s->strut->top);
1197             }
1198             for (sit = struts_right; sit; sit = g_slist_next(sit)) {
1199                 ObScreenStrut *s = sit->data;
1200                 if ((s->desktop == j || s->desktop == DESKTOP_ALL) &&
1201                     STRUT_RIGHT_ON_MONITOR(s->strut, i))
1202                     r = MAX(r, s->strut->right);
1203             }
1204             for (sit = struts_bottom; sit; sit = g_slist_next(sit)) {
1205                 ObScreenStrut *s = sit->data;
1206                 if ((s->desktop == j || s->desktop == DESKTOP_ALL) &&
1207                     STRUT_BOTTOM_ON_MONITOR(s->strut, i))
1208                     b = MAX(b, s->strut->bottom);
1209             }
1210
1211             /* based on these margins, set the work area for the
1212                monitor/desktop */
1213             dims[(i * screen_num_desktops + j) * 4 + 0] += l;
1214             dims[(i * screen_num_desktops + j) * 4 + 1] += t;
1215             dims[(i * screen_num_desktops + j) * 4 + 2] -= l + r;
1216             dims[(i * screen_num_desktops + j) * 4 + 3] -= t + b;
1217         }
1218
1219     PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
1220                 dims, 4 * screen_num_desktops * screen_num_monitors);
1221
1222     /* the area has changed, adjust all the windows if they need it */
1223     for (it = client_list; it; it = g_list_next(it))
1224         client_reconfigure(it->data, FALSE);
1225
1226     g_free(dims);
1227 }
1228
1229 #if 0
1230 Rect* screen_area_all_monitors(guint desktop)
1231 {
1232     guint i;
1233     Rect *a;
1234
1235     a = screen_area_monitor(desktop, 0);
1236
1237     /* combine all the monitors together */
1238     for (i = 1; i < screen_num_monitors; ++i) {
1239         Rect *m = screen_area_monitor(desktop, i);
1240         gint l, r, t, b;
1241
1242         l = MIN(RECT_LEFT(*a), RECT_LEFT(*m));
1243         t = MIN(RECT_TOP(*a), RECT_TOP(*m));
1244         r = MAX(RECT_RIGHT(*a), RECT_RIGHT(*m));
1245         b = MAX(RECT_BOTTOM(*a), RECT_BOTTOM(*m));
1246
1247         RECT_SET(*a, l, t, r - l + 1, b - t + 1);
1248
1249         g_free(m);
1250     }
1251         
1252     return a;
1253 }
1254 #endif
1255
1256 #define STRUT_LEFT_IN_SEARCH(s, search) \
1257     (RANGES_INTERSECT(search->y, search->height, \
1258                       s->left_start, s->left_end - s->left_start + 1))
1259 #define STRUT_RIGHT_IN_SEARCH(s, search) \
1260     (RANGES_INTERSECT(search->y, search->height, \
1261                       s->right_start, s->right_end - s->right_start + 1))
1262 #define STRUT_TOP_IN_SEARCH(s, search) \
1263     (RANGES_INTERSECT(search->x, search->width, \
1264                       s->top_start, s->top_end - s->top_start + 1))
1265 #define STRUT_BOTTOM_IN_SEARCH(s, search) \
1266     (RANGES_INTERSECT(search->x, search->width, \
1267                       s->bottom_start, s->bottom_end - s->bottom_start + 1))
1268
1269 #define STRUT_LEFT_IGNORE(s, us, search) \
1270     (head == SCREEN_AREA_ALL_MONITORS && us && \
1271      RECT_LEFT(monitor_area[i]) + s->left > RECT_LEFT(*search))
1272 #define STRUT_RIGHT_IGNORE(s, us, search) \
1273     (head == SCREEN_AREA_ALL_MONITORS && us && \
1274      RECT_RIGHT(monitor_area[i]) - s->right < RECT_RIGHT(*search))
1275 #define STRUT_TOP_IGNORE(s, us, search) \
1276     (head == SCREEN_AREA_ALL_MONITORS && us && \
1277      RECT_TOP(monitor_area[i]) + s->top > RECT_TOP(*search))
1278 #define STRUT_BOTTOM_IGNORE(s, us, search) \
1279     (head == SCREEN_AREA_ALL_MONITORS && us && \
1280      RECT_BOTTOM(monitor_area[i]) - s->bottom < RECT_BOTTOM(*search))
1281
1282 Rect* screen_area(guint desktop, guint head, Rect *search)
1283 {
1284     Rect *a;
1285     GSList *it;
1286     gint l, r, t, b, al, ar, at, ab;
1287     guint i, d;
1288     gboolean us = search != NULL; /* user provided search */
1289
1290     g_assert(desktop < screen_num_desktops || desktop == DESKTOP_ALL);
1291     g_assert(head < screen_num_monitors || head == SCREEN_AREA_ONE_MONITOR ||
1292              head == SCREEN_AREA_ALL_MONITORS);
1293     g_assert(!(head == SCREEN_AREA_ONE_MONITOR && search == NULL));
1294
1295     /* find any struts for this monitor
1296        which will be affecting the search area.
1297     */
1298
1299     /* search everything if search is null */
1300     if (!search) {
1301         if (head < screen_num_monitors) search = &monitor_area[head];
1302         else search = &monitor_area[screen_num_monitors];
1303     }
1304     if (head == SCREEN_AREA_ONE_MONITOR) head = screen_find_monitor(search);
1305
1306     /* al is "all left" meaning the furthest left you can get, l is our
1307        "working left" meaning our current strut edge which we're calculating
1308     */
1309
1310     /* only include monitors which the search area lines up with */
1311     if (RECT_INTERSECTS_RECT(monitor_area[screen_num_monitors], *search)) {
1312         al = l = RECT_RIGHT(monitor_area[screen_num_monitors]);
1313         at = t = RECT_BOTTOM(monitor_area[screen_num_monitors]);
1314         ar = r = RECT_LEFT(monitor_area[screen_num_monitors]);
1315         ab = b = RECT_TOP(monitor_area[screen_num_monitors]);
1316         for (i = 0; i < screen_num_monitors; ++i) {
1317             /* add the monitor if applicable */
1318             if (RANGES_INTERSECT(search->x, search->width,
1319                                  monitor_area[i].x, monitor_area[i].width))
1320             {
1321                 at = t = MIN(t, RECT_TOP(monitor_area[i]));
1322                 ab = b = MAX(b, RECT_BOTTOM(monitor_area[i]));
1323             }
1324             if (RANGES_INTERSECT(search->y, search->height,
1325                                  monitor_area[i].y, monitor_area[i].height))
1326             {
1327                 al = l = MIN(l, RECT_LEFT(monitor_area[i]));
1328                 ar = r = MAX(r, RECT_RIGHT(monitor_area[i]));
1329             }
1330         }
1331     } else {
1332         al = l = RECT_LEFT(monitor_area[screen_num_monitors]);
1333         at = t = RECT_TOP(monitor_area[screen_num_monitors]);
1334         ar = r = RECT_RIGHT(monitor_area[screen_num_monitors]);
1335         ab = b = RECT_BOTTOM(monitor_area[screen_num_monitors]);
1336     }
1337
1338     for (d = 0; d < screen_num_desktops; ++d) {
1339         if (d != desktop && desktop != DESKTOP_ALL) continue;
1340
1341         for (i = 0; i < screen_num_monitors; ++i) {
1342             if (head != SCREEN_AREA_ALL_MONITORS && head != i) continue;
1343
1344             for (it = struts_left; it; it = g_slist_next(it)) {
1345                 ObScreenStrut *s = it->data;
1346                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1347                     STRUT_LEFT_IN_SEARCH(s->strut, search) &&
1348                     !STRUT_LEFT_IGNORE(s->strut, us, search))
1349                     l = MAX(l, al + s->strut->left);
1350             }
1351             for (it = struts_top; it; it = g_slist_next(it)) {
1352                 ObScreenStrut *s = it->data;
1353                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1354                     STRUT_TOP_IN_SEARCH(s->strut, search) &&
1355                     !STRUT_TOP_IGNORE(s->strut, us, search))
1356                     t = MAX(t, at + s->strut->top);
1357             }
1358             for (it = struts_right; it; it = g_slist_next(it)) {
1359                 ObScreenStrut *s = it->data;
1360                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1361                     STRUT_RIGHT_IN_SEARCH(s->strut, search) &&
1362                     !STRUT_RIGHT_IGNORE(s->strut, us, search))
1363                     r = MIN(r, ar - s->strut->right);
1364             }
1365             for (it = struts_bottom; it; it = g_slist_next(it)) {
1366                 ObScreenStrut *s = it->data;
1367                 if ((s->desktop == d || s->desktop == DESKTOP_ALL) &&
1368                     STRUT_BOTTOM_IN_SEARCH(s->strut, search) &&
1369                     !STRUT_BOTTOM_IGNORE(s->strut, us, search))
1370                     b = MIN(b, ab - s->strut->bottom);
1371             }
1372
1373             /* limit to this monitor */
1374             if (head == i) {
1375                 l = MAX(l, RECT_LEFT(monitor_area[i]));
1376                 t = MAX(t, RECT_TOP(monitor_area[i]));
1377                 r = MIN(r, RECT_RIGHT(monitor_area[i]));
1378                 b = MIN(b, RECT_BOTTOM(monitor_area[i]));
1379             }
1380         }
1381     }
1382
1383     a = g_new(Rect, 1);
1384     a->x = l;
1385     a->y = t;
1386     a->width = r - l + 1;
1387     a->height = b - t + 1;
1388     return a;
1389 }
1390
1391 guint screen_find_monitor(Rect *search)
1392 {
1393     guint i;
1394     guint most = 0;
1395     guint mostv = 0;
1396
1397     for (i = 0; i < screen_num_monitors; ++i) {
1398         Rect *area = screen_physical_area_monitor(i);
1399         if (RECT_INTERSECTS_RECT(*area, *search)) {
1400             Rect r;
1401             guint v;
1402
1403             RECT_SET_INTERSECTION(r, *area, *search);
1404             v = r.width * r.height;
1405
1406             if (v > mostv) {
1407                 mostv = v;
1408                 most = i;
1409             }
1410         }
1411         g_free(area);
1412     }
1413     return most;
1414 }
1415
1416 Rect* screen_physical_area_all_monitors()
1417 {
1418     return screen_physical_area_monitor(screen_num_monitors);
1419 }
1420
1421 Rect* screen_physical_area_monitor(guint head)
1422 {
1423     Rect *a;
1424     g_assert(head <= screen_num_monitors);
1425
1426     a = g_new(Rect, 1);
1427     *a = monitor_area[head];
1428     return a;
1429 }
1430
1431 gboolean screen_physical_area_monitor_contains(guint head, Rect *search)
1432 {
1433     g_assert(head <= screen_num_monitors);
1434     g_assert(search);
1435     return RECT_INTERSECTS_RECT(monitor_area[head], *search);
1436 }
1437
1438 Rect* screen_physical_area_active()
1439 {
1440     Rect *a;
1441     gint x, y;
1442
1443     if (focus_client)
1444         a = screen_physical_area_monitor(client_monitor(focus_client));
1445     else {
1446         Rect mon;
1447         if (screen_pointer_pos(&x, &y))
1448             RECT_SET(mon, x, y, 1, 1);
1449         else
1450             RECT_SET(mon, 0, 0, 1, 1);
1451         a = screen_physical_area_monitor(screen_find_monitor(&mon));
1452     }
1453     return a;
1454 }
1455
1456 void screen_set_root_cursor()
1457 {
1458     if (sn_app_starting())
1459         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1460                       ob_cursor(OB_CURSOR_BUSYPOINTER));
1461     else
1462         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1463                       ob_cursor(OB_CURSOR_POINTER));
1464 }
1465
1466 gboolean screen_pointer_pos(gint *x, gint *y)
1467 {
1468     Window w;
1469     gint i;
1470     guint u;
1471     gboolean ret;
1472
1473     ret = !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
1474                           &w, &w, x, y, &i, &i, &u);
1475     if (!ret) {
1476         for (i = 0; i < ScreenCount(ob_display); ++i)
1477             if (i != ob_screen)
1478                 if (XQueryPointer(ob_display, RootWindow(ob_display, i),
1479                                   &w, &w, x, y, &i, &i, &u))
1480                     break;
1481     }
1482     return ret;
1483 }