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