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