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