]> icculus.org git repositories - dana/openbox.git/blob - openbox/screen.c
refactor all the session stuff. yay, it works properly now.
[dana/openbox.git] / openbox / screen.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    screen.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "openbox.h"
22 #include "dock.h"
23 #include "xerror.h"
24 #include "prop.h"
25 #include "grab.h"
26 #include "startupnotify.h"
27 #include "moveresize.h"
28 #include "config.h"
29 #include "screen.h"
30 #include "client.h"
31 #include "session.h"
32 #include "frame.h"
33 #include "event.h"
34 #include "focus.h"
35 #include "popup.h"
36 #include "extensions.h"
37 #include "render/render.h"
38 #include "gettext.h"
39
40 #include <X11/Xlib.h>
41 #ifdef HAVE_UNISTD_H
42 #  include <sys/types.h>
43 #  include <unistd.h>
44 #endif
45 #include <assert.h>
46
47 /*! The event mask to grab on the root window */
48 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
49                         EnterWindowMask | LeaveWindowMask | \
50                         SubstructureRedirectMask | FocusChangeMask | \
51                         ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
52
53 guint    screen_num_desktops;
54 guint    screen_num_monitors;
55 guint    screen_desktop;
56 guint    screen_last_desktop;
57 Size     screen_physical_size;
58 gboolean screen_showing_desktop;
59 DesktopLayout screen_desktop_layout;
60 gchar  **screen_desktop_names;
61 Window   screen_support_win;
62 Time     screen_desktop_user_time = CurrentTime;
63
64 static Rect  **area; /* array of desktop holding array of xinerama areas */
65 static Rect  *monitor_area;
66
67 static ObPagerPopup *desktop_cycle_popup;
68
69 static gboolean replace_wm()
70 {
71     gchar *wm_sn;
72     Atom wm_sn_atom;
73     Window current_wm_sn_owner;
74     Time timestamp;
75
76     wm_sn = g_strdup_printf("WM_S%d", ob_screen);
77     wm_sn_atom = XInternAtom(ob_display, wm_sn, FALSE);
78     g_free(wm_sn);
79
80     current_wm_sn_owner = XGetSelectionOwner(ob_display, wm_sn_atom);
81     if (current_wm_sn_owner == screen_support_win)
82         current_wm_sn_owner = None;
83     if (current_wm_sn_owner) {
84         if (!ob_replace_wm) {
85             g_message(_("A window manager is already running on screen %d"),
86                       ob_screen);
87             return FALSE;
88         }
89         xerror_set_ignore(TRUE);
90         xerror_occured = FALSE;
91
92         /* We want to find out when the current selection owner dies */
93         XSelectInput(ob_display, current_wm_sn_owner, StructureNotifyMask);
94         XSync(ob_display, FALSE);
95
96         xerror_set_ignore(FALSE);
97         if (xerror_occured)
98             current_wm_sn_owner = None;
99     }
100
101     {
102         /* Generate a timestamp */
103         XEvent event;
104
105         XSelectInput(ob_display, screen_support_win, PropertyChangeMask);
106
107         XChangeProperty(ob_display, screen_support_win,
108                         prop_atoms.wm_class, prop_atoms.string,
109                         8, PropModeAppend, NULL, 0);
110         XWindowEvent(ob_display, screen_support_win,
111                      PropertyChangeMask, &event);
112
113         XSelectInput(ob_display, screen_support_win, NoEventMask);
114
115         timestamp = event.xproperty.time;
116     }
117
118     XSetSelectionOwner(ob_display, wm_sn_atom, screen_support_win,
119                        timestamp);
120
121     if (XGetSelectionOwner(ob_display, wm_sn_atom) != screen_support_win) {
122         g_message(_("Could not acquire window manager selection on screen %d"),
123                   ob_screen);
124         return FALSE;
125     }
126
127     /* Wait for old window manager to go away */
128     if (current_wm_sn_owner) {
129       XEvent event;
130       gulong wait = 0;
131       const gulong timeout = G_USEC_PER_SEC * 15; /* wait for 15s max */
132
133       while (wait < timeout) {
134           if (XCheckWindowEvent(ob_display, current_wm_sn_owner,
135                                 StructureNotifyMask, &event) &&
136               event.type == DestroyNotify)
137               break;
138           g_usleep(G_USEC_PER_SEC / 10);
139           wait += G_USEC_PER_SEC / 10;
140       }
141
142       if (wait >= timeout) {
143           g_message(_("The WM on screen %d is not exiting"), ob_screen);
144           return FALSE;
145       }
146     }
147
148     /* Send client message indicating that we are now the WM */
149     prop_message(RootWindow(ob_display, ob_screen), prop_atoms.manager,
150                  timestamp, wm_sn_atom, screen_support_win, 0,
151                  SubstructureNotifyMask);
152
153     return TRUE;
154 }
155
156 gboolean screen_annex()
157 {
158     XSetWindowAttributes attrib;
159     pid_t pid;
160     gint i, num_support;
161     Atom *prop_atoms_start, *wm_supported_pos;
162     gulong *supported;
163
164     /* create the netwm support window */
165     attrib.override_redirect = TRUE;
166     screen_support_win = XCreateWindow(ob_display,
167                                        RootWindow(ob_display, ob_screen),
168                                        -100, -100, 1, 1, 0,
169                                        CopyFromParent, InputOutput,
170                                        CopyFromParent,
171                                        CWOverrideRedirect, &attrib);
172     XMapWindow(ob_display, screen_support_win);
173
174     if (!replace_wm()) {
175         XDestroyWindow(ob_display, screen_support_win);
176         return FALSE;
177     }
178
179     xerror_set_ignore(TRUE);
180     xerror_occured = FALSE;
181     XSelectInput(ob_display, RootWindow(ob_display, ob_screen),
182                  ROOT_EVENTMASK);
183     xerror_set_ignore(FALSE);
184     if (xerror_occured) {
185         g_message(_("A window manager is already running on screen %d"),
186                   ob_screen);
187
188         XDestroyWindow(ob_display, screen_support_win);
189         return FALSE;
190     }
191
192
193     screen_set_root_cursor();
194
195     /* set the OPENBOX_PID hint */
196     pid = getpid();
197     PROP_SET32(RootWindow(ob_display, ob_screen),
198                openbox_pid, cardinal, pid);
199
200     /* set supporting window */
201     PROP_SET32(RootWindow(ob_display, ob_screen),
202                net_supporting_wm_check, window, screen_support_win);
203
204     /* set properties on the supporting window */
205     PROP_SETS(screen_support_win, net_wm_name, "Openbox");
206     PROP_SET32(screen_support_win, net_supporting_wm_check,
207                window, screen_support_win);
208
209     /* set the _NET_SUPPORTED_ATOMS hint */
210
211     /* this is all the atoms after net_supported in the prop_atoms struct */
212     prop_atoms_start = (Atom*)&prop_atoms;
213     wm_supported_pos = (Atom*)&(prop_atoms.net_supported);
214     num_support = sizeof(prop_atoms) / sizeof(Atom) -
215         (wm_supported_pos - prop_atoms_start) - 1;
216     i = 0;
217     supported = g_new(gulong, num_support);
218     supported[i++] = prop_atoms.net_supporting_wm_check;
219     supported[i++] = prop_atoms.net_wm_full_placement;
220     supported[i++] = prop_atoms.net_current_desktop;
221     supported[i++] = prop_atoms.net_number_of_desktops;
222     supported[i++] = prop_atoms.net_desktop_geometry;
223     supported[i++] = prop_atoms.net_desktop_viewport;
224     supported[i++] = prop_atoms.net_active_window;
225     supported[i++] = prop_atoms.net_workarea;
226     supported[i++] = prop_atoms.net_client_list;
227     supported[i++] = prop_atoms.net_client_list_stacking;
228     supported[i++] = prop_atoms.net_desktop_names;
229     supported[i++] = prop_atoms.net_close_window;
230     supported[i++] = prop_atoms.net_desktop_layout;
231     supported[i++] = prop_atoms.net_showing_desktop;
232     supported[i++] = prop_atoms.net_wm_name;
233     supported[i++] = prop_atoms.net_wm_visible_name;
234     supported[i++] = prop_atoms.net_wm_icon_name;
235     supported[i++] = prop_atoms.net_wm_visible_icon_name;
236     supported[i++] = prop_atoms.net_wm_desktop;
237     supported[i++] = prop_atoms.net_wm_strut;
238     supported[i++] = prop_atoms.net_wm_strut_partial;
239     supported[i++] = prop_atoms.net_wm_icon;
240     supported[i++] = prop_atoms.net_wm_icon_geometry;
241     supported[i++] = prop_atoms.net_wm_window_type;
242     supported[i++] = prop_atoms.net_wm_window_type_desktop;
243     supported[i++] = prop_atoms.net_wm_window_type_dock;
244     supported[i++] = prop_atoms.net_wm_window_type_toolbar;
245     supported[i++] = prop_atoms.net_wm_window_type_menu;
246     supported[i++] = prop_atoms.net_wm_window_type_utility;
247     supported[i++] = prop_atoms.net_wm_window_type_splash;
248     supported[i++] = prop_atoms.net_wm_window_type_dialog;
249     supported[i++] = prop_atoms.net_wm_window_type_normal;
250     supported[i++] = prop_atoms.net_wm_allowed_actions;
251     supported[i++] = prop_atoms.net_wm_action_move;
252     supported[i++] = prop_atoms.net_wm_action_resize;
253     supported[i++] = prop_atoms.net_wm_action_minimize;
254     supported[i++] = prop_atoms.net_wm_action_shade;
255     supported[i++] = prop_atoms.net_wm_action_maximize_horz;
256     supported[i++] = prop_atoms.net_wm_action_maximize_vert;
257     supported[i++] = prop_atoms.net_wm_action_fullscreen;
258     supported[i++] = prop_atoms.net_wm_action_change_desktop;
259     supported[i++] = prop_atoms.net_wm_action_close;
260     supported[i++] = prop_atoms.net_wm_state;
261     supported[i++] = prop_atoms.net_wm_state_modal;
262     supported[i++] = prop_atoms.net_wm_state_maximized_vert;
263     supported[i++] = prop_atoms.net_wm_state_maximized_horz;
264     supported[i++] = prop_atoms.net_wm_state_shaded;
265     supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
266     supported[i++] = prop_atoms.net_wm_state_skip_pager;
267     supported[i++] = prop_atoms.net_wm_state_hidden;
268     supported[i++] = prop_atoms.net_wm_state_fullscreen;
269     supported[i++] = prop_atoms.net_wm_state_above;
270     supported[i++] = prop_atoms.net_wm_state_below;
271     supported[i++] = prop_atoms.net_wm_state_demands_attention;
272     supported[i++] = prop_atoms.net_moveresize_window;
273     supported[i++] = prop_atoms.net_wm_moveresize;
274     supported[i++] = prop_atoms.net_wm_user_time;
275     supported[i++] = prop_atoms.net_frame_extents;
276     supported[i++] = prop_atoms.net_startup_id;
277 #ifdef SYNC
278     supported[i++] = prop_atoms.net_wm_sync_request;
279     supported[i++] = prop_atoms.net_wm_sync_request_counter;
280 #endif
281
282     supported[i++] = prop_atoms.kde_wm_change_state;
283     supported[i++] = prop_atoms.kde_net_wm_frame_strut;
284     supported[i++] = prop_atoms.kde_net_wm_window_type_override;
285
286     supported[i++] = prop_atoms.openbox_wm_state_undecorated;
287     supported[i++] = prop_atoms.openbox_pid;
288     supported[i++] = prop_atoms.openbox_config;
289     supported[i++] = prop_atoms.openbox_control;
290     g_assert(i == num_support);
291
292     PROP_SETA32(RootWindow(ob_display, ob_screen),
293                 net_supported, atom, supported, num_support);
294     g_free(supported);
295
296     return TRUE;
297 }
298
299 void screen_startup(gboolean reconfig)
300 {
301     GSList *it;
302     guint i;
303
304     desktop_cycle_popup = pager_popup_new(FALSE);
305
306     if (!reconfig)
307         /* get the initial size */
308         screen_resize();
309
310     /* set the names */
311     screen_desktop_names = g_new(gchar*,
312                                  g_slist_length(config_desktops_names) + 1);
313     for (i = 0, it = config_desktops_names; it; ++i, it = g_slist_next(it))
314         screen_desktop_names[i] = it->data; /* dont strdup */
315     screen_desktop_names[i] = NULL;
316     PROP_SETSS(RootWindow(ob_display, ob_screen),
317                net_desktop_names, screen_desktop_names);
318     g_free(screen_desktop_names); /* dont free the individual strings */
319     screen_desktop_names = NULL;
320
321     if (!reconfig)
322         screen_num_desktops = 0;
323     screen_set_num_desktops(config_desktops_num);
324     if (!reconfig) {
325         guint32 d;
326         /* start on the current desktop when a wm was already running */
327         if (PROP_GET32(RootWindow(ob_display, ob_screen),
328                        net_current_desktop, cardinal, &d) &&
329             d < screen_num_desktops)
330         {
331             screen_set_desktop(d);
332         } else if (session_desktop >= 0)
333             screen_set_desktop(MIN((guint)session_desktop,
334                                    screen_num_desktops));
335         else
336             screen_set_desktop(MIN(config_screen_firstdesk,
337                                    screen_num_desktops) - 1);
338
339         /* don't start in showing-desktop mode */
340         screen_showing_desktop = FALSE;
341         PROP_SET32(RootWindow(ob_display, ob_screen),
342                    net_showing_desktop, cardinal, screen_showing_desktop);
343
344         screen_update_layout();
345     }
346 }
347
348 void screen_shutdown(gboolean reconfig)
349 {
350     Rect **r;
351
352     pager_popup_free(desktop_cycle_popup);
353
354     if (!reconfig) {
355         XSelectInput(ob_display, RootWindow(ob_display, ob_screen),
356                      NoEventMask);
357
358         /* we're not running here no more! */
359         PROP_ERASE(RootWindow(ob_display, ob_screen), openbox_pid);
360         /* not without us */
361         PROP_ERASE(RootWindow(ob_display, ob_screen), net_supported);
362         /* don't keep this mode */
363         PROP_ERASE(RootWindow(ob_display, ob_screen), net_showing_desktop);
364
365         XDestroyWindow(ob_display, screen_support_win);
366     }
367
368     g_strfreev(screen_desktop_names);
369     screen_desktop_names = NULL;
370     for (r = area; *r; ++r)
371         g_free(*r);
372     g_free(area);
373     area = NULL;
374 }
375
376 void screen_resize()
377 {
378     static gint oldw = 0, oldh = 0;
379     gint w, h;
380     GList *it;
381     gulong geometry[2];
382
383     w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
384     h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
385
386     if (w == oldw && h == oldh) return;
387
388     oldw = w; oldh = h;
389
390     /* Set the _NET_DESKTOP_GEOMETRY hint */
391     screen_physical_size.width = geometry[0] = w;
392     screen_physical_size.height = geometry[1] = h;
393     PROP_SETA32(RootWindow(ob_display, ob_screen),
394                 net_desktop_geometry, cardinal, geometry, 2);
395
396     if (ob_state() == OB_STATE_STARTING)
397         return;
398
399     screen_update_areas();
400     dock_configure();
401
402     for (it = client_list; it; it = g_list_next(it))
403         client_move_onscreen(it->data, FALSE);
404 }
405
406 void screen_set_num_desktops(guint num)
407 {
408     guint old;
409     gulong *viewport;
410     GList *it;
411
412     g_assert(num > 0);
413
414     if (screen_num_desktops == num) return;
415
416     old = screen_num_desktops;
417     screen_num_desktops = num;
418     PROP_SET32(RootWindow(ob_display, ob_screen),
419                net_number_of_desktops, cardinal, num);
420
421     /* set the viewport hint */
422     viewport = g_new0(gulong, num * 2);
423     PROP_SETA32(RootWindow(ob_display, ob_screen),
424                 net_desktop_viewport, cardinal, viewport, num * 2);
425     g_free(viewport);
426
427     /* the number of rows/columns will differ */
428     screen_update_layout();
429
430     /* may be some unnamed desktops that we need to fill in with names */
431     screen_update_desktop_names();
432
433     /* move windows on desktops that will no longer exist! */
434     for (it = client_list; it; it = g_list_next(it)) {
435         ObClient *c = it->data;
436         if (c->desktop >= num && c->desktop != DESKTOP_ALL)
437             client_set_desktop(c, num - 1, FALSE);
438     }
439  
440     /* change our struts/area to match (after moving windows) */
441     screen_update_areas();
442
443     /* change our desktop if we're on one that no longer exists! */
444     if (screen_desktop >= screen_num_desktops)
445         screen_set_desktop(num - 1);
446 }
447
448 void screen_set_desktop(guint num)
449 {
450     ObClient *c;
451     GList *it;
452     guint old;
453      
454     g_assert(num < screen_num_desktops);
455
456     old = screen_desktop;
457     screen_desktop = num;
458     PROP_SET32(RootWindow(ob_display, ob_screen),
459                net_current_desktop, cardinal, num);
460
461     if (old == num) return;
462
463     screen_last_desktop = old;
464
465     ob_debug("Moving to desktop %d\n", num+1);
466
467     if (moveresize_client)
468         client_set_desktop(moveresize_client, num, TRUE);
469
470     /* show windows before hiding the rest to lessen the enter/leave events */
471
472     /* show/hide windows from top to bottom */
473     for (it = stacking_list; it; it = g_list_next(it)) {
474         if (WINDOW_IS_CLIENT(it->data)) {
475             ObClient *c = it->data;
476             client_show(c);
477         }
478     }
479
480     /* hide windows from bottom to top */
481     for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
482         if (WINDOW_IS_CLIENT(it->data)) {
483             ObClient *c = it->data;
484             client_hide(c);
485         }
486     }
487
488     /* have to try focus here because when you leave an empty desktop
489        there is no focus out to watch for */
490     if ((c = focus_fallback_target(TRUE, focus_client))) {
491         /* reduce flicker by hiliting now rather than waiting for the server
492            FocusIn event */
493         frame_adjust_focus(c->frame, TRUE);
494         client_focus(c);
495     }
496
497     event_ignore_queued_enters();
498
499     if (event_curtime != CurrentTime)
500         screen_desktop_user_time = event_curtime;
501 }
502
503 static void get_row_col(guint d, guint *r, guint *c)
504 {
505     switch (screen_desktop_layout.orientation) {
506     case OB_ORIENTATION_HORZ:
507         switch (screen_desktop_layout.start_corner) {
508         case OB_CORNER_TOPLEFT:
509             *r = d / screen_desktop_layout.columns;
510             *c = d % screen_desktop_layout.columns;
511             break;
512         case OB_CORNER_BOTTOMLEFT:
513             *r = screen_desktop_layout.rows - 1 -
514                 d / screen_desktop_layout.columns;
515             *c = d % screen_desktop_layout.columns;
516             break;
517         case OB_CORNER_TOPRIGHT:
518             *r = d / screen_desktop_layout.columns;
519             *c = screen_desktop_layout.columns - 1 -
520                 d % screen_desktop_layout.columns;
521             break;
522         case OB_CORNER_BOTTOMRIGHT:
523             *r = screen_desktop_layout.rows - 1 -
524                 d / screen_desktop_layout.columns;
525             *c = screen_desktop_layout.columns - 1 -
526                 d % screen_desktop_layout.columns;
527             break;
528         }
529         break;
530     case OB_ORIENTATION_VERT:
531         switch (screen_desktop_layout.start_corner) {
532         case OB_CORNER_TOPLEFT:
533             *r = d % screen_desktop_layout.rows;
534             *c = d / screen_desktop_layout.rows;
535             break;
536         case OB_CORNER_BOTTOMLEFT:
537             *r = screen_desktop_layout.rows - 1 -
538                 d % screen_desktop_layout.rows;
539             *c = d / screen_desktop_layout.rows;
540             break;
541         case OB_CORNER_TOPRIGHT:
542             *r = d % screen_desktop_layout.rows;
543             *c = screen_desktop_layout.columns - 1 -
544                 d / screen_desktop_layout.rows;
545             break;
546         case OB_CORNER_BOTTOMRIGHT:
547             *r = screen_desktop_layout.rows - 1 -
548                 d % screen_desktop_layout.rows;
549             *c = screen_desktop_layout.columns - 1 -
550                 d / screen_desktop_layout.rows;
551             break;
552         }
553         break;
554     }
555 }
556
557 static guint translate_row_col(guint r, guint c)
558 {
559     switch (screen_desktop_layout.orientation) {
560     case OB_ORIENTATION_HORZ:
561         switch (screen_desktop_layout.start_corner) {
562         case OB_CORNER_TOPLEFT:
563             return r % screen_desktop_layout.rows *
564                 screen_desktop_layout.columns +
565                 c % screen_desktop_layout.columns;
566         case OB_CORNER_BOTTOMLEFT:
567             return (screen_desktop_layout.rows - 1 -
568                     r % screen_desktop_layout.rows) *
569                 screen_desktop_layout.columns +
570                 c % screen_desktop_layout.columns;
571         case OB_CORNER_TOPRIGHT:
572             return r % screen_desktop_layout.rows *
573                 screen_desktop_layout.columns +
574                 (screen_desktop_layout.columns - 1 -
575                  c % screen_desktop_layout.columns);
576         case OB_CORNER_BOTTOMRIGHT:
577             return (screen_desktop_layout.rows - 1 -
578                     r % screen_desktop_layout.rows) *
579                 screen_desktop_layout.columns +
580                 (screen_desktop_layout.columns - 1 -
581                  c % screen_desktop_layout.columns);
582         }
583     case OB_ORIENTATION_VERT:
584         switch (screen_desktop_layout.start_corner) {
585         case OB_CORNER_TOPLEFT:
586             return c % screen_desktop_layout.columns *
587                 screen_desktop_layout.rows +
588                 r % screen_desktop_layout.rows;
589         case OB_CORNER_BOTTOMLEFT:
590             return c % screen_desktop_layout.columns *
591                 screen_desktop_layout.rows +
592                 (screen_desktop_layout.rows - 1 -
593                  r % screen_desktop_layout.rows);
594         case OB_CORNER_TOPRIGHT:
595             return (screen_desktop_layout.columns - 1 -
596                     c % screen_desktop_layout.columns) *
597                 screen_desktop_layout.rows +
598                 r % screen_desktop_layout.rows;
599         case OB_CORNER_BOTTOMRIGHT:
600             return (screen_desktop_layout.columns - 1 -
601                     c % screen_desktop_layout.columns) *
602                 screen_desktop_layout.rows +
603                 (screen_desktop_layout.rows - 1 -
604                  r % screen_desktop_layout.rows);
605         }
606     }
607     g_assert_not_reached();
608     return 0;
609 }
610
611 void screen_desktop_popup(guint d, gboolean show)
612 {
613     Rect *a;
614
615     if (!show) {
616         pager_popup_hide(desktop_cycle_popup);
617     } else {
618         a = screen_physical_area_monitor(0);
619         pager_popup_position(desktop_cycle_popup, CenterGravity,
620                              a->x + a->width / 2, a->y + a->height / 2);
621         pager_popup_width(desktop_cycle_popup, MAX(a->width/3, POPUP_WIDTH));
622         pager_popup_height(desktop_cycle_popup, POPUP_HEIGHT);
623
624         pager_popup_show(desktop_cycle_popup, screen_desktop_names[d], d);
625     }
626 }
627
628 guint screen_cycle_desktop(ObDirection dir, gboolean wrap, gboolean linear,
629                            gboolean dialog, gboolean done, gboolean cancel)
630 {
631     static gboolean first = TRUE;
632     static guint origd, d;
633     guint r, c;
634
635     if (cancel) {
636         d = origd;
637         goto done_cycle;
638     } else if (done && dialog) {
639         goto done_cycle;
640     }
641     if (first) {
642         first = FALSE;
643         d = origd = screen_desktop;
644     }
645
646     get_row_col(d, &r, &c);
647
648     if (linear) {
649         switch (dir) {
650         case OB_DIRECTION_EAST:
651             if (d < screen_num_desktops - 1)
652                 ++d;
653             else if (wrap)
654                 d = 0;
655             break;
656         case OB_DIRECTION_WEST:
657             if (d > 0)
658                 --d;
659             else if (wrap)
660                 d = screen_num_desktops - 1;
661             break;
662         default:
663             assert(0);
664             return screen_desktop;
665         }
666     } else {
667         switch (dir) {
668         case OB_DIRECTION_EAST:
669             ++c;
670             if (c >= screen_desktop_layout.columns) {
671                 if (wrap) {
672                     c = 0;
673                 } else {
674                     d = screen_desktop;
675                     goto show_cycle_dialog;
676                 }
677             }
678             d = translate_row_col(r, c);
679             if (d >= screen_num_desktops) {
680                 if (wrap) {
681                     ++c;
682                 } else {
683                     d = screen_desktop;
684                     goto show_cycle_dialog;
685                 }
686             }
687             break;
688         case OB_DIRECTION_WEST:
689             --c;
690             if (c >= screen_desktop_layout.columns) {
691                 if (wrap) {
692                     c = screen_desktop_layout.columns - 1;
693                 } else {
694                     d = screen_desktop;
695                     goto show_cycle_dialog;
696                 }
697             }
698             d = translate_row_col(r, c);
699             if (d >= screen_num_desktops) {
700                 if (wrap) {
701                     --c;
702                 } else {
703                     d = screen_desktop;
704                     goto show_cycle_dialog;
705                 }
706             }
707             break;
708         case OB_DIRECTION_SOUTH:
709             ++r;
710             if (r >= screen_desktop_layout.rows) {
711                 if (wrap) {
712                     r = 0;
713                 } else {
714                     d = screen_desktop;
715                     goto show_cycle_dialog;
716                 }
717             }
718             d = translate_row_col(r, c);
719             if (d >= screen_num_desktops) {
720                 if (wrap) {
721                     ++r;
722                 } else {
723                     d = screen_desktop;
724                     goto show_cycle_dialog;
725                 }
726             }
727             break;
728         case OB_DIRECTION_NORTH:
729             --r;
730             if (r >= screen_desktop_layout.rows) {
731                 if (wrap) {
732                     r = screen_desktop_layout.rows - 1;
733                 } else {
734                     d = screen_desktop;
735                     goto show_cycle_dialog;
736                 }
737             }
738             d = translate_row_col(r, c);
739             if (d >= screen_num_desktops) {
740                 if (wrap) {
741                     --r;
742                 } else {
743                     d = screen_desktop;
744                     goto show_cycle_dialog;
745                 }
746             }
747             break;
748         default:
749             assert(0);
750             return d = screen_desktop;
751         }
752
753         d = translate_row_col(r, c);
754     }
755
756 show_cycle_dialog:
757     if (dialog) {
758         screen_desktop_popup(d, TRUE);
759         return d;
760     }
761
762 done_cycle:
763     first = TRUE;
764
765     screen_desktop_popup(0, FALSE);
766
767     return d;
768 }
769
770 void screen_update_layout()
771 {
772     ObOrientation orient;
773     ObCorner corner;
774     guint rows;
775     guint cols;
776     guint32 *data;
777     guint num;
778     gboolean valid = FALSE;
779
780     if (PROP_GETA32(RootWindow(ob_display, ob_screen),
781                     net_desktop_layout, cardinal, &data, &num)) {
782         if (num == 3 || num == 4) {
783
784             if (data[0] == prop_atoms.net_wm_orientation_vert)
785                 orient = OB_ORIENTATION_VERT;
786             else if (data[0] == prop_atoms.net_wm_orientation_horz)
787                 orient = OB_ORIENTATION_HORZ;
788             else
789                 goto screen_update_layout_bail;
790
791             if (num < 4)
792                 corner = OB_CORNER_TOPLEFT;
793             else {
794                 if (data[3] == prop_atoms.net_wm_topleft)
795                     corner = OB_CORNER_TOPLEFT;
796                 else if (data[3] == prop_atoms.net_wm_topright)
797                     corner = OB_CORNER_TOPRIGHT;
798                 else if (data[3] == prop_atoms.net_wm_bottomright)
799                     corner = OB_CORNER_BOTTOMRIGHT;
800                 else if (data[3] == prop_atoms.net_wm_bottomleft)
801                     corner = OB_CORNER_BOTTOMLEFT;
802                 else
803                     goto screen_update_layout_bail;
804             }
805
806             cols = data[1];
807             rows = data[2];
808
809             /* fill in a zero rows/columns */
810             if ((cols == 0 && rows == 0)) { /* both 0's is bad data.. */
811                 goto screen_update_layout_bail;
812             } else {
813                 if (cols == 0) {
814                     cols = screen_num_desktops / rows;
815                     if (rows * cols < screen_num_desktops)
816                         cols++;
817                     if (rows * cols >= screen_num_desktops + cols)
818                         rows--;
819                 } else if (rows == 0) {
820                     rows = screen_num_desktops / cols;
821                     if (cols * rows < screen_num_desktops)
822                         rows++;
823                     if (cols * rows >= screen_num_desktops + rows)
824                         cols--;
825                 }
826             }
827
828             /* bounds checking */
829             if (orient == OB_ORIENTATION_HORZ) {
830                 cols = MIN(screen_num_desktops, cols);
831                 rows = MIN(rows, (screen_num_desktops + cols - 1) / cols);
832                 cols = screen_num_desktops / rows +
833                     !!(screen_num_desktops % rows);
834             } else {
835                 rows = MIN(screen_num_desktops, rows);
836                 cols = MIN(cols, (screen_num_desktops + rows - 1) / rows);
837                 rows = screen_num_desktops / cols +
838                     !!(screen_num_desktops % cols);
839             }
840
841             valid = TRUE;
842         }
843     screen_update_layout_bail:
844         g_free(data);
845     }
846
847     if (!valid) {
848         /* defaults */
849         orient = OB_ORIENTATION_HORZ;
850         corner = OB_CORNER_TOPLEFT;
851         rows = 1;
852         cols = screen_num_desktops;
853     }
854
855     screen_desktop_layout.orientation = orient;
856     screen_desktop_layout.start_corner = corner;
857     screen_desktop_layout.rows = rows;
858     screen_desktop_layout.columns = cols;
859 }
860
861 void screen_update_desktop_names()
862 {
863     guint i;
864
865     /* empty the array */
866     g_strfreev(screen_desktop_names);
867     screen_desktop_names = NULL;
868
869     if (PROP_GETSS(RootWindow(ob_display, ob_screen),
870                    net_desktop_names, utf8, &screen_desktop_names))
871         for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
872     else
873         i = 0;
874     if (i <= screen_num_desktops) {
875         screen_desktop_names = g_renew(gchar*, screen_desktop_names,
876                                        screen_num_desktops + 1);
877         screen_desktop_names[screen_num_desktops] = NULL;
878         for (; i < screen_num_desktops; ++i)
879             screen_desktop_names[i] = g_strdup_printf("Desktop %i", i + 1);
880     }
881 }
882
883 void screen_show_desktop(gboolean show, gboolean restore_focus)
884 {
885     GList *it;
886      
887     if (show == screen_showing_desktop) return; /* no change */
888
889     screen_showing_desktop = show;
890
891     if (show) {
892         /* bottom to top */
893         for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
894             if (WINDOW_IS_CLIENT(it->data)) {
895                 ObClient *client = it->data;
896                 client_showhide(client);
897             }
898         }
899     } else {
900         /* top to bottom */
901         for (it = stacking_list; it; it = g_list_next(it)) {
902             if (WINDOW_IS_CLIENT(it->data)) {
903                 ObClient *client = it->data;
904                 client_showhide(client);
905             }
906         }
907     }
908
909     if (show) {
910         /* focus desktop */
911         for (it = focus_order; it; it = g_list_next(it)) {
912             ObClient *c = it->data;
913             if (c->type == OB_CLIENT_TYPE_DESKTOP &&
914                 (c->desktop == screen_desktop || c->desktop == DESKTOP_ALL) &&
915                 client_focus(it->data))
916                 break;
917         }
918     } else if (restore_focus) {
919         ObClient *c;
920
921         /* use NULL for the "old" argument because the desktop was focused
922            and we don't want to fallback to the desktop by default */
923         if ((c = focus_fallback_target(TRUE, NULL)))
924             client_focus(c);
925     }
926
927     show = !!show; /* make it boolean */
928     PROP_SET32(RootWindow(ob_display, ob_screen),
929                net_showing_desktop, cardinal, show);
930 }
931
932 void screen_install_colormap(ObClient *client, gboolean install)
933 {
934     if (client == NULL) {
935         if (install)
936             XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
937         else
938             XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
939     } else {
940         xerror_set_ignore(TRUE);
941         if (install) {
942             if (client->colormap != None)
943                 XInstallColormap(RrDisplay(ob_rr_inst), client->colormap);
944         } else
945             XUninstallColormap(RrDisplay(ob_rr_inst), client->colormap);
946         xerror_set_ignore(FALSE);
947     }
948 }
949
950 static inline void
951 screen_area_add_strut_left(const StrutPartial *s, const Rect *monitor_area,
952                            gint edge, Strut *ret)
953 {
954     if (s->left &&
955         ((s->left_end <= s->left_start) ||
956          (RECT_TOP(*monitor_area) < s->left_end &&
957           RECT_BOTTOM(*monitor_area) > s->left_start)))
958         ret->left = MAX(ret->left, edge);
959 }
960
961 static inline void
962 screen_area_add_strut_top(const StrutPartial *s, const Rect *monitor_area,
963                           gint edge, Strut *ret)
964 {
965     if (s->top &&
966         ((s->top_end <= s->top_start) ||
967          (RECT_LEFT(*monitor_area) < s->top_end &&
968           RECT_RIGHT(*monitor_area) > s->top_start)))
969         ret->top = MAX(ret->top, edge);
970 }
971
972 static inline void
973 screen_area_add_strut_right(const StrutPartial *s, const Rect *monitor_area,
974                             gint edge, Strut *ret)
975 {
976     if (s->right &&
977         ((s->right_end <= s->right_start) ||
978          (RECT_TOP(*monitor_area) < s->right_end &&
979           RECT_BOTTOM(*monitor_area) > s->right_start)))
980         ret->right = MAX(ret->right, edge);
981 }
982
983 static inline void
984 screen_area_add_strut_bottom(const StrutPartial *s, const Rect *monitor_area,
985                              gint edge, Strut *ret)
986 {
987     if (s->bottom &&
988         ((s->bottom_end <= s->bottom_start) ||
989          (RECT_LEFT(*monitor_area) < s->bottom_end &&
990           RECT_RIGHT(*monitor_area) > s->bottom_start)))
991         ret->bottom = MAX(ret->bottom, edge);
992 }
993
994 void screen_update_areas()
995 {
996     guint i, x;
997     gulong *dims;
998     GList *it;
999     gint o;
1000
1001     g_free(monitor_area);
1002     extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
1003
1004     if (area) {
1005         for (i = 0; area[i]; ++i)
1006             g_free(area[i]);
1007         g_free(area);
1008     }
1009
1010     area = g_new(Rect*, screen_num_desktops + 2);
1011     for (i = 0; i < screen_num_desktops + 1; ++i)
1012         area[i] = g_new0(Rect, screen_num_monitors + 1);
1013     area[i] = NULL;
1014      
1015     dims = g_new(gulong, 4 * screen_num_desktops);
1016
1017     for (i = 0; i < screen_num_desktops + 1; ++i) {
1018         Strut *struts;
1019         gint l, r, t, b;
1020
1021         struts = g_new0(Strut, screen_num_monitors);
1022
1023         /* calc the xinerama areas */
1024         for (x = 0; x < screen_num_monitors; ++x) {
1025             area[i][x] = monitor_area[x];
1026             if (x == 0) {
1027                 l = monitor_area[x].x;
1028                 t = monitor_area[x].y;
1029                 r = monitor_area[x].x + monitor_area[x].width - 1;
1030                 b = monitor_area[x].y + monitor_area[x].height - 1;
1031             } else {
1032                 l = MIN(l, monitor_area[x].x);
1033                 t = MIN(t, monitor_area[x].y);
1034                 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
1035                 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
1036             }
1037         }
1038         RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
1039
1040         /* apply the struts */
1041
1042         /* find the left-most xin heads, i do this in 2 loops :| */
1043         o = area[i][0].x;
1044         for (x = 1; x < screen_num_monitors; ++x)
1045             o = MIN(o, area[i][x].x);
1046
1047         for (x = 0; x < screen_num_monitors; ++x) {
1048             for (it = client_list; it; it = g_list_next(it)) {
1049                 ObClient *c = it->data;
1050                 screen_area_add_strut_left(&c->strut,
1051                                            &monitor_area[x],
1052                                            o + c->strut.left - area[i][x].x,
1053                                            &struts[x]);
1054             }
1055             screen_area_add_strut_left(&dock_strut,
1056                                        &monitor_area[x],
1057                                        o + dock_strut.left - area[i][x].x,
1058                                        &struts[x]);
1059
1060             area[i][x].x += struts[x].left;
1061             area[i][x].width -= struts[x].left;
1062         }
1063
1064         /* find the top-most xin heads, i do this in 2 loops :| */
1065         o = area[i][0].y;
1066         for (x = 1; x < screen_num_monitors; ++x)
1067             o = MIN(o, area[i][x].y);
1068
1069         for (x = 0; x < screen_num_monitors; ++x) {
1070             for (it = client_list; it; it = g_list_next(it)) {
1071                 ObClient *c = it->data;
1072                 screen_area_add_strut_top(&c->strut,
1073                                            &monitor_area[x],
1074                                            o + c->strut.top - area[i][x].y,
1075                                            &struts[x]);
1076             }
1077             screen_area_add_strut_top(&dock_strut,
1078                                       &monitor_area[x],
1079                                       o + dock_strut.top - area[i][x].y,
1080                                       &struts[x]);
1081
1082             area[i][x].y += struts[x].top;
1083             area[i][x].height -= struts[x].top;
1084         }
1085
1086         /* find the right-most xin heads, i do this in 2 loops :| */
1087         o = area[i][0].x + area[i][0].width - 1;
1088         for (x = 1; x < screen_num_monitors; ++x)
1089             o = MAX(o, area[i][x].x + area[i][x].width - 1);
1090
1091         for (x = 0; x < screen_num_monitors; ++x) {
1092             for (it = client_list; it; it = g_list_next(it)) {
1093                 ObClient *c = it->data;
1094                 screen_area_add_strut_right(&c->strut,
1095                                            &monitor_area[x],
1096                                            (area[i][x].x +
1097                                             area[i][x].width - 1) -
1098                                             (o - c->strut.right),
1099                                             &struts[x]);
1100             }
1101             screen_area_add_strut_right(&dock_strut,
1102                                         &monitor_area[x],
1103                                         (area[i][x].x +
1104                                          area[i][x].width - 1) -
1105                                         (o - dock_strut.right),
1106                                         &struts[x]);
1107
1108             area[i][x].width -= struts[x].right;
1109         }
1110
1111         /* find the bottom-most xin heads, i do this in 2 loops :| */
1112         o = area[i][0].y + area[i][0].height - 1;
1113         for (x = 1; x < screen_num_monitors; ++x)
1114             o = MAX(o, area[i][x].y + area[i][x].height - 1);
1115
1116         for (x = 0; x < screen_num_monitors; ++x) {
1117             for (it = client_list; it; it = g_list_next(it)) {
1118                 ObClient *c = it->data;
1119                 screen_area_add_strut_bottom(&c->strut,
1120                                              &monitor_area[x],
1121                                              (area[i][x].y +
1122                                               area[i][x].height - 1) - \
1123                                              (o - c->strut.bottom),
1124                                              &struts[x]);
1125             }
1126             screen_area_add_strut_bottom(&dock_strut,
1127                                          &monitor_area[x],
1128                                          (area[i][x].y +
1129                                           area[i][x].height - 1) - \
1130                                          (o - dock_strut.bottom),
1131                                          &struts[x]);
1132
1133             area[i][x].height -= struts[x].bottom;
1134         }
1135
1136         l = RECT_LEFT(area[i][0]);
1137         t = RECT_TOP(area[i][0]);
1138         r = RECT_RIGHT(area[i][0]);
1139         b = RECT_BOTTOM(area[i][0]);
1140         for (x = 1; x < screen_num_monitors; ++x) {
1141             l = MIN(l, RECT_LEFT(area[i][x]));
1142             t = MIN(l, RECT_TOP(area[i][x]));
1143             r = MAX(r, RECT_RIGHT(area[i][x]));
1144             b = MAX(b, RECT_BOTTOM(area[i][x]));
1145         }
1146         RECT_SET(area[i][screen_num_monitors], l, t,
1147                  r - l + 1, b - t + 1);
1148
1149         /* XXX optimize when this is run? */
1150
1151         /* the area has changed, adjust all the maximized 
1152            windows */
1153         for (it = client_list; it; it = g_list_next(it)) {
1154             ObClient *c = it->data; 
1155             if (i < screen_num_desktops) {
1156                 if (c->desktop == i)
1157                     client_reconfigure(c);
1158             } else if (c->desktop == DESKTOP_ALL)
1159                 client_reconfigure(c);
1160         }
1161         if (i < screen_num_desktops) {
1162             /* don't set these for the 'all desktops' area */
1163             dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
1164             dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
1165             dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
1166             dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
1167         }
1168
1169         g_free(struts);
1170     }
1171
1172     PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
1173                 dims, 4 * screen_num_desktops);
1174
1175     g_free(dims);
1176 }
1177
1178 Rect *screen_area(guint desktop)
1179 {
1180     return screen_area_monitor(desktop, screen_num_monitors);
1181 }
1182
1183 Rect *screen_area_monitor(guint desktop, guint head)
1184 {
1185     if (head > screen_num_monitors)
1186         return NULL;
1187     if (desktop >= screen_num_desktops) {
1188         if (desktop == DESKTOP_ALL)
1189             return &area[screen_num_desktops][head];
1190         return NULL;
1191     }
1192     return &area[desktop][head];
1193 }
1194
1195 guint screen_find_monitor(Rect *search)
1196 {
1197     guint i;
1198     guint most = 0;
1199     guint mostv = 0;
1200
1201     for (i = 0; i < screen_num_monitors; ++i) {
1202         Rect *area = screen_physical_area_monitor(i);
1203         if (RECT_INTERSECTS_RECT(*area, *search)) {
1204             Rect r;
1205             guint v;
1206
1207             RECT_SET_INTERSECTION(r, *area, *search);
1208             v = r.width * r.height;
1209
1210             if (v > mostv) {
1211                 mostv = v;
1212                 most = i;
1213             }
1214         }
1215     }
1216     return most;
1217 }
1218
1219 Rect *screen_physical_area()
1220 {
1221     return screen_physical_area_monitor(screen_num_monitors);
1222 }
1223
1224 Rect *screen_physical_area_monitor(guint head)
1225 {
1226     if (head > screen_num_monitors)
1227         return NULL;
1228     return &monitor_area[head];
1229 }
1230
1231 void screen_set_root_cursor()
1232 {
1233     if (sn_app_starting())
1234         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1235                       ob_cursor(OB_CURSOR_BUSY));
1236     else
1237         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1238                       ob_cursor(OB_CURSOR_POINTER));
1239 }
1240
1241 gboolean screen_pointer_pos(gint *x, gint *y)
1242 {
1243     Window w;
1244     gint i;
1245     guint u;
1246
1247     return !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
1248                            &w, &w, x, y, &i, &i, &u);
1249 }