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