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