we werent dropping it in screen, we were dropping it in prop
[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     guint i, numnames;
303
304     if (!reconfig)
305         /* get the initial size */
306         screen_resize();
307
308     desktop_cycle_popup = pager_popup_new(FALSE);
309     pager_popup_height(desktop_cycle_popup, POPUP_HEIGHT);
310
311 #if 0
312     /* get the names */
313     if (PROP_GETSS(RootWindow(ob_display, ob_screen),
314                    net_desktop_names, utf8, &screen_desktop_names))
315         for (i = 0; screen_desktop_names[i]; ++i);
316     else
317 #endif
318         i = 0;
319     numnames = g_slist_length(config_desktops_names);
320     if (numnames > i) {
321         GSList *it;
322
323         screen_desktop_names = g_renew(gchar*,screen_desktop_names,numnames+1);
324         screen_desktop_names[numnames] = NULL;
325
326         for (it = g_slist_nth(config_desktops_names, i); it;
327              it = g_slist_next(it), ++i)
328         {
329             screen_desktop_names[i] = g_strdup(it->data);
330         }
331     }
332     /* then set the names */
333     PROP_SETSS(RootWindow(ob_display, ob_screen),
334                net_desktop_names, screen_desktop_names);
335     g_strfreev(screen_desktop_names);
336     screen_desktop_names = NULL;
337
338     if (!reconfig)
339         screen_num_desktops = 0;
340     screen_set_num_desktops(config_desktops_num);
341     if (!reconfig) {
342         guint32 d;
343         /* start on the current desktop when a wm was already running */
344         if (PROP_GET32(RootWindow(ob_display, ob_screen),
345                        net_current_desktop, cardinal, &d) &&
346             d < screen_num_desktops)
347         {
348             screen_set_desktop(d, FALSE);
349         } else if (session_desktop >= 0)
350             screen_set_desktop(MIN((guint)session_desktop,
351                                    screen_num_desktops), FALSE);
352         else
353             screen_set_desktop(MIN(config_screen_firstdesk,
354                                    screen_num_desktops) - 1, FALSE);
355
356         /* don't start in showing-desktop mode */
357         screen_showing_desktop = FALSE;
358         PROP_SET32(RootWindow(ob_display, ob_screen),
359                    net_showing_desktop, cardinal, screen_showing_desktop);
360
361         screen_update_layout();
362     }
363 }
364
365 void screen_shutdown(gboolean reconfig)
366 {
367     Rect **r;
368
369     pager_popup_free(desktop_cycle_popup);
370
371     if (!reconfig) {
372         XSelectInput(ob_display, RootWindow(ob_display, ob_screen),
373                      NoEventMask);
374
375         /* we're not running here no more! */
376         PROP_ERASE(RootWindow(ob_display, ob_screen), openbox_pid);
377         /* not without us */
378         PROP_ERASE(RootWindow(ob_display, ob_screen), net_supported);
379         /* don't keep this mode */
380         PROP_ERASE(RootWindow(ob_display, ob_screen), net_showing_desktop);
381
382         XDestroyWindow(ob_display, screen_support_win);
383     }
384
385     g_strfreev(screen_desktop_names);
386     screen_desktop_names = NULL;
387     for (r = area; *r; ++r)
388         g_free(*r);
389     g_free(area);
390     area = NULL;
391 }
392
393 void screen_resize()
394 {
395     static gint oldw = 0, oldh = 0;
396     gint w, h;
397     GList *it;
398     gulong geometry[2];
399
400     w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
401     h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
402
403     if (w == oldw && h == oldh) return;
404
405     oldw = w; oldh = h;
406
407     /* Set the _NET_DESKTOP_GEOMETRY hint */
408     screen_physical_size.width = geometry[0] = w;
409     screen_physical_size.height = geometry[1] = h;
410     PROP_SETA32(RootWindow(ob_display, ob_screen),
411                 net_desktop_geometry, cardinal, geometry, 2);
412
413     if (ob_state() == OB_STATE_STARTING)
414         return;
415
416     screen_update_areas();
417     dock_configure();
418
419     for (it = client_list; it; it = g_list_next(it))
420         client_move_onscreen(it->data, FALSE);
421 }
422
423 void screen_set_num_desktops(guint num)
424 {
425     guint old;
426     gulong *viewport;
427     GList *it;
428
429     g_assert(num > 0);
430
431     if (screen_num_desktops == num) return;
432
433     old = screen_num_desktops;
434     screen_num_desktops = num;
435     PROP_SET32(RootWindow(ob_display, ob_screen),
436                net_number_of_desktops, cardinal, num);
437
438     /* set the viewport hint */
439     viewport = g_new0(gulong, num * 2);
440     PROP_SETA32(RootWindow(ob_display, ob_screen),
441                 net_desktop_viewport, cardinal, viewport, num * 2);
442     g_free(viewport);
443
444     /* the number of rows/columns will differ */
445     screen_update_layout();
446
447     /* move windows on desktops that will no longer exist! */
448     for (it = client_list; it; it = g_list_next(it)) {
449         ObClient *c = it->data;
450         if (c->desktop >= num && c->desktop != DESKTOP_ALL)
451             client_set_desktop(c, num - 1, FALSE);
452     }
453  
454     /* change our struts/area to match (after moving windows) */
455     screen_update_areas();
456
457     /* may be some unnamed desktops that we need to fill in with names
458      (after updating the areas so the popup can resize) */
459     screen_update_desktop_names();
460
461     /* change our desktop if we're on one that no longer exists! */
462     if (screen_desktop >= screen_num_desktops)
463         screen_set_desktop(num - 1, TRUE);
464 }
465
466 void screen_set_desktop(guint num, gboolean dofocus)
467 {
468     ObClient *c;
469     GList *it;
470     guint old;
471      
472     g_assert(num < screen_num_desktops);
473
474     old = screen_desktop;
475     screen_desktop = num;
476     PROP_SET32(RootWindow(ob_display, ob_screen),
477                net_current_desktop, cardinal, num);
478
479     if (old == num) return;
480
481     screen_last_desktop = old;
482
483     ob_debug("Moving to desktop %d\n", num+1);
484
485     if (moveresize_client)
486         client_set_desktop(moveresize_client, num, TRUE);
487
488     /* show windows before hiding the rest to lessen the enter/leave events */
489
490     /* show windows from top to bottom */
491     for (it = stacking_list; it; it = g_list_next(it)) {
492         if (WINDOW_IS_CLIENT(it->data)) {
493             ObClient *c = it->data;
494             client_show(c);
495         }
496     }
497
498     /* hide windows from bottom to top */
499     for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
500         if (WINDOW_IS_CLIENT(it->data)) {
501             ObClient *c = it->data;
502             client_hide(c);
503         }
504     }
505
506     /* have to try focus here because when you leave an empty desktop
507        there is no focus out to watch for */
508     if (dofocus && (c = focus_fallback_target(TRUE, focus_client))) {
509         /* reduce flicker by hiliting now rather than waiting for the server
510            FocusIn event */
511         frame_adjust_focus(c->frame, TRUE);
512         client_focus(c);
513     }
514
515     event_ignore_queued_enters();
516
517     if (event_curtime != CurrentTime)
518         screen_desktop_user_time = event_curtime;
519 }
520
521 static void get_row_col(guint d, guint *r, guint *c)
522 {
523     switch (screen_desktop_layout.orientation) {
524     case OB_ORIENTATION_HORZ:
525         switch (screen_desktop_layout.start_corner) {
526         case OB_CORNER_TOPLEFT:
527             *r = d / screen_desktop_layout.columns;
528             *c = d % screen_desktop_layout.columns;
529             break;
530         case OB_CORNER_BOTTOMLEFT:
531             *r = screen_desktop_layout.rows - 1 -
532                 d / screen_desktop_layout.columns;
533             *c = d % screen_desktop_layout.columns;
534             break;
535         case OB_CORNER_TOPRIGHT:
536             *r = d / screen_desktop_layout.columns;
537             *c = screen_desktop_layout.columns - 1 -
538                 d % screen_desktop_layout.columns;
539             break;
540         case OB_CORNER_BOTTOMRIGHT:
541             *r = screen_desktop_layout.rows - 1 -
542                 d / screen_desktop_layout.columns;
543             *c = screen_desktop_layout.columns - 1 -
544                 d % screen_desktop_layout.columns;
545             break;
546         }
547         break;
548     case OB_ORIENTATION_VERT:
549         switch (screen_desktop_layout.start_corner) {
550         case OB_CORNER_TOPLEFT:
551             *r = d % screen_desktop_layout.rows;
552             *c = d / screen_desktop_layout.rows;
553             break;
554         case OB_CORNER_BOTTOMLEFT:
555             *r = screen_desktop_layout.rows - 1 -
556                 d % screen_desktop_layout.rows;
557             *c = d / screen_desktop_layout.rows;
558             break;
559         case OB_CORNER_TOPRIGHT:
560             *r = d % screen_desktop_layout.rows;
561             *c = screen_desktop_layout.columns - 1 -
562                 d / screen_desktop_layout.rows;
563             break;
564         case OB_CORNER_BOTTOMRIGHT:
565             *r = screen_desktop_layout.rows - 1 -
566                 d % screen_desktop_layout.rows;
567             *c = screen_desktop_layout.columns - 1 -
568                 d / screen_desktop_layout.rows;
569             break;
570         }
571         break;
572     }
573 }
574
575 static guint translate_row_col(guint r, guint c)
576 {
577     switch (screen_desktop_layout.orientation) {
578     case OB_ORIENTATION_HORZ:
579         switch (screen_desktop_layout.start_corner) {
580         case OB_CORNER_TOPLEFT:
581             return r % screen_desktop_layout.rows *
582                 screen_desktop_layout.columns +
583                 c % screen_desktop_layout.columns;
584         case OB_CORNER_BOTTOMLEFT:
585             return (screen_desktop_layout.rows - 1 -
586                     r % screen_desktop_layout.rows) *
587                 screen_desktop_layout.columns +
588                 c % screen_desktop_layout.columns;
589         case OB_CORNER_TOPRIGHT:
590             return r % screen_desktop_layout.rows *
591                 screen_desktop_layout.columns +
592                 (screen_desktop_layout.columns - 1 -
593                  c % screen_desktop_layout.columns);
594         case OB_CORNER_BOTTOMRIGHT:
595             return (screen_desktop_layout.rows - 1 -
596                     r % screen_desktop_layout.rows) *
597                 screen_desktop_layout.columns +
598                 (screen_desktop_layout.columns - 1 -
599                  c % screen_desktop_layout.columns);
600         }
601     case OB_ORIENTATION_VERT:
602         switch (screen_desktop_layout.start_corner) {
603         case OB_CORNER_TOPLEFT:
604             return c % screen_desktop_layout.columns *
605                 screen_desktop_layout.rows +
606                 r % screen_desktop_layout.rows;
607         case OB_CORNER_BOTTOMLEFT:
608             return c % screen_desktop_layout.columns *
609                 screen_desktop_layout.rows +
610                 (screen_desktop_layout.rows - 1 -
611                  r % screen_desktop_layout.rows);
612         case OB_CORNER_TOPRIGHT:
613             return (screen_desktop_layout.columns - 1 -
614                     c % screen_desktop_layout.columns) *
615                 screen_desktop_layout.rows +
616                 r % screen_desktop_layout.rows;
617         case OB_CORNER_BOTTOMRIGHT:
618             return (screen_desktop_layout.columns - 1 -
619                     c % screen_desktop_layout.columns) *
620                 screen_desktop_layout.rows +
621                 (screen_desktop_layout.rows - 1 -
622                  r % screen_desktop_layout.rows);
623         }
624     }
625     g_assert_not_reached();
626     return 0;
627 }
628
629 void screen_desktop_popup(guint d, gboolean show)
630 {
631     Rect *a;
632
633     if (!show) {
634         pager_popup_hide(desktop_cycle_popup);
635     } else {
636         a = screen_physical_area_monitor(0);
637         pager_popup_position(desktop_cycle_popup, CenterGravity,
638                              a->x + a->width / 2, a->y + a->height / 2);
639         pager_popup_max_width(desktop_cycle_popup,
640                               MAX(a->width/3, POPUP_WIDTH));
641         pager_popup_show(desktop_cycle_popup, screen_desktop_names[d], d);
642     }
643 }
644
645 guint screen_cycle_desktop(ObDirection dir, gboolean wrap, gboolean linear,
646                            gboolean dialog, gboolean done, gboolean cancel)
647 {
648     static gboolean first = TRUE;
649     static guint origd, d;
650     guint r, c;
651
652     if (cancel) {
653         d = origd;
654         goto done_cycle;
655     } else if (done && dialog) {
656         goto done_cycle;
657     }
658     if (first) {
659         first = FALSE;
660         d = origd = screen_desktop;
661     }
662
663     get_row_col(d, &r, &c);
664
665     if (linear) {
666         switch (dir) {
667         case OB_DIRECTION_EAST:
668             if (d < screen_num_desktops - 1)
669                 ++d;
670             else if (wrap)
671                 d = 0;
672             break;
673         case OB_DIRECTION_WEST:
674             if (d > 0)
675                 --d;
676             else if (wrap)
677                 d = screen_num_desktops - 1;
678             break;
679         default:
680             assert(0);
681             return screen_desktop;
682         }
683     } else {
684         switch (dir) {
685         case OB_DIRECTION_EAST:
686             ++c;
687             if (c >= screen_desktop_layout.columns) {
688                 if (wrap) {
689                     c = 0;
690                 } else {
691                     d = screen_desktop;
692                     goto show_cycle_dialog;
693                 }
694             }
695             d = translate_row_col(r, c);
696             if (d >= screen_num_desktops) {
697                 if (wrap) {
698                     ++c;
699                 } else {
700                     d = screen_desktop;
701                     goto show_cycle_dialog;
702                 }
703             }
704             break;
705         case OB_DIRECTION_WEST:
706             --c;
707             if (c >= screen_desktop_layout.columns) {
708                 if (wrap) {
709                     c = screen_desktop_layout.columns - 1;
710                 } else {
711                     d = screen_desktop;
712                     goto show_cycle_dialog;
713                 }
714             }
715             d = translate_row_col(r, c);
716             if (d >= screen_num_desktops) {
717                 if (wrap) {
718                     --c;
719                 } else {
720                     d = screen_desktop;
721                     goto show_cycle_dialog;
722                 }
723             }
724             break;
725         case OB_DIRECTION_SOUTH:
726             ++r;
727             if (r >= screen_desktop_layout.rows) {
728                 if (wrap) {
729                     r = 0;
730                 } else {
731                     d = screen_desktop;
732                     goto show_cycle_dialog;
733                 }
734             }
735             d = translate_row_col(r, c);
736             if (d >= screen_num_desktops) {
737                 if (wrap) {
738                     ++r;
739                 } else {
740                     d = screen_desktop;
741                     goto show_cycle_dialog;
742                 }
743             }
744             break;
745         case OB_DIRECTION_NORTH:
746             --r;
747             if (r >= screen_desktop_layout.rows) {
748                 if (wrap) {
749                     r = screen_desktop_layout.rows - 1;
750                 } else {
751                     d = screen_desktop;
752                     goto show_cycle_dialog;
753                 }
754             }
755             d = translate_row_col(r, c);
756             if (d >= screen_num_desktops) {
757                 if (wrap) {
758                     --r;
759                 } else {
760                     d = screen_desktop;
761                     goto show_cycle_dialog;
762                 }
763             }
764             break;
765         default:
766             assert(0);
767             return d = screen_desktop;
768         }
769
770         d = translate_row_col(r, c);
771     }
772
773 show_cycle_dialog:
774     if (dialog) {
775         screen_desktop_popup(d, TRUE);
776         return d;
777     }
778
779 done_cycle:
780     first = TRUE;
781
782     screen_desktop_popup(0, FALSE);
783
784     return d;
785 }
786
787 void screen_update_layout()
788 {
789     ObOrientation orient;
790     ObCorner corner;
791     guint rows;
792     guint cols;
793     guint32 *data;
794     guint num;
795     gboolean valid = FALSE;
796
797     if (PROP_GETA32(RootWindow(ob_display, ob_screen),
798                     net_desktop_layout, cardinal, &data, &num)) {
799         if (num == 3 || num == 4) {
800
801             if (data[0] == prop_atoms.net_wm_orientation_vert)
802                 orient = OB_ORIENTATION_VERT;
803             else if (data[0] == prop_atoms.net_wm_orientation_horz)
804                 orient = OB_ORIENTATION_HORZ;
805             else
806                 goto screen_update_layout_bail;
807
808             if (num < 4)
809                 corner = OB_CORNER_TOPLEFT;
810             else {
811                 if (data[3] == prop_atoms.net_wm_topleft)
812                     corner = OB_CORNER_TOPLEFT;
813                 else if (data[3] == prop_atoms.net_wm_topright)
814                     corner = OB_CORNER_TOPRIGHT;
815                 else if (data[3] == prop_atoms.net_wm_bottomright)
816                     corner = OB_CORNER_BOTTOMRIGHT;
817                 else if (data[3] == prop_atoms.net_wm_bottomleft)
818                     corner = OB_CORNER_BOTTOMLEFT;
819                 else
820                     goto screen_update_layout_bail;
821             }
822
823             cols = data[1];
824             rows = data[2];
825
826             /* fill in a zero rows/columns */
827             if ((cols == 0 && rows == 0)) { /* both 0's is bad data.. */
828                 goto screen_update_layout_bail;
829             } else {
830                 if (cols == 0) {
831                     cols = screen_num_desktops / rows;
832                     if (rows * cols < screen_num_desktops)
833                         cols++;
834                     if (rows * cols >= screen_num_desktops + cols)
835                         rows--;
836                 } else if (rows == 0) {
837                     rows = screen_num_desktops / cols;
838                     if (cols * rows < screen_num_desktops)
839                         rows++;
840                     if (cols * rows >= screen_num_desktops + rows)
841                         cols--;
842                 }
843             }
844
845             /* bounds checking */
846             if (orient == OB_ORIENTATION_HORZ) {
847                 cols = MIN(screen_num_desktops, cols);
848                 rows = MIN(rows, (screen_num_desktops + cols - 1) / cols);
849                 cols = screen_num_desktops / rows +
850                     !!(screen_num_desktops % rows);
851             } else {
852                 rows = MIN(screen_num_desktops, rows);
853                 cols = MIN(cols, (screen_num_desktops + rows - 1) / rows);
854                 rows = screen_num_desktops / cols +
855                     !!(screen_num_desktops % cols);
856             }
857
858             valid = TRUE;
859         }
860     screen_update_layout_bail:
861         g_free(data);
862     }
863
864     if (!valid) {
865         /* defaults */
866         orient = OB_ORIENTATION_HORZ;
867         corner = OB_CORNER_TOPLEFT;
868         rows = 1;
869         cols = screen_num_desktops;
870     }
871
872     screen_desktop_layout.orientation = orient;
873     screen_desktop_layout.start_corner = corner;
874     screen_desktop_layout.rows = rows;
875     screen_desktop_layout.columns = cols;
876 }
877
878 void screen_update_desktop_names()
879 {
880     guint i;
881
882     /* empty the array */
883     g_strfreev(screen_desktop_names);
884     screen_desktop_names = NULL;
885
886     if (PROP_GETSS(RootWindow(ob_display, ob_screen),
887                    net_desktop_names, utf8, &screen_desktop_names))
888         for (i = 0; screen_desktop_names[i] && i < screen_num_desktops; ++i);
889     else
890         i = 0;
891     if (i < screen_num_desktops) {
892         screen_desktop_names = g_renew(gchar*, screen_desktop_names,
893                                        screen_num_desktops + 1);
894         screen_desktop_names[screen_num_desktops] = NULL;
895         for (; i < screen_num_desktops; ++i)
896             screen_desktop_names[i] = g_strdup_printf("desktop %i", i + 1);
897     }
898
899     /* resize the pager for these names */
900     pager_popup_text_width_to_strings(desktop_cycle_popup,
901                                       screen_desktop_names,
902                                       screen_num_desktops);
903 }
904
905 void screen_show_desktop(gboolean show, gboolean restore_focus)
906 {
907     GList *it;
908      
909     if (show == screen_showing_desktop) return; /* no change */
910
911     screen_showing_desktop = show;
912
913     if (show) {
914         /* bottom to top */
915         for (it = g_list_last(stacking_list); it; it = g_list_previous(it)) {
916             if (WINDOW_IS_CLIENT(it->data)) {
917                 ObClient *client = it->data;
918                 client_showhide(client);
919             }
920         }
921     } else {
922         /* top to bottom */
923         for (it = stacking_list; it; it = g_list_next(it)) {
924             if (WINDOW_IS_CLIENT(it->data)) {
925                 ObClient *client = it->data;
926                 client_showhide(client);
927             }
928         }
929     }
930
931     if (show) {
932         /* focus desktop */
933         for (it = focus_order; it; it = g_list_next(it)) {
934             ObClient *c = it->data;
935             if (c->type == OB_CLIENT_TYPE_DESKTOP &&
936                 (c->desktop == screen_desktop || c->desktop == DESKTOP_ALL) &&
937                 client_focus(it->data))
938                 break;
939         }
940     } else if (restore_focus) {
941         ObClient *c;
942
943         /* use NULL for the "old" argument because the desktop was focused
944            and we don't want to fallback to the desktop by default */
945         if ((c = focus_fallback_target(TRUE, NULL)))
946             client_focus(c);
947     }
948
949     show = !!show; /* make it boolean */
950     PROP_SET32(RootWindow(ob_display, ob_screen),
951                net_showing_desktop, cardinal, show);
952 }
953
954 void screen_install_colormap(ObClient *client, gboolean install)
955 {
956     if (client == NULL) {
957         if (install)
958             XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
959         else
960             XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
961     } else {
962         xerror_set_ignore(TRUE);
963         if (install) {
964             if (client->colormap != None)
965                 XInstallColormap(RrDisplay(ob_rr_inst), client->colormap);
966         } else
967             XUninstallColormap(RrDisplay(ob_rr_inst), client->colormap);
968         xerror_set_ignore(FALSE);
969     }
970 }
971
972 static inline void
973 screen_area_add_strut_left(const StrutPartial *s, const Rect *monitor_area,
974                            gint edge, Strut *ret)
975 {
976     if (s->left &&
977         ((s->left_end <= s->left_start) ||
978          (RECT_TOP(*monitor_area) < s->left_end &&
979           RECT_BOTTOM(*monitor_area) > s->left_start)))
980         ret->left = MAX(ret->left, edge);
981 }
982
983 static inline void
984 screen_area_add_strut_top(const StrutPartial *s, const Rect *monitor_area,
985                           gint edge, Strut *ret)
986 {
987     if (s->top &&
988         ((s->top_end <= s->top_start) ||
989          (RECT_LEFT(*monitor_area) < s->top_end &&
990           RECT_RIGHT(*monitor_area) > s->top_start)))
991         ret->top = MAX(ret->top, edge);
992 }
993
994 static inline void
995 screen_area_add_strut_right(const StrutPartial *s, const Rect *monitor_area,
996                             gint edge, Strut *ret)
997 {
998     if (s->right &&
999         ((s->right_end <= s->right_start) ||
1000          (RECT_TOP(*monitor_area) < s->right_end &&
1001           RECT_BOTTOM(*monitor_area) > s->right_start)))
1002         ret->right = MAX(ret->right, edge);
1003 }
1004
1005 static inline void
1006 screen_area_add_strut_bottom(const StrutPartial *s, const Rect *monitor_area,
1007                              gint edge, Strut *ret)
1008 {
1009     if (s->bottom &&
1010         ((s->bottom_end <= s->bottom_start) ||
1011          (RECT_LEFT(*monitor_area) < s->bottom_end &&
1012           RECT_RIGHT(*monitor_area) > s->bottom_start)))
1013         ret->bottom = MAX(ret->bottom, edge);
1014 }
1015
1016 void screen_update_areas()
1017 {
1018     guint i, x;
1019     gulong *dims;
1020     GList *it;
1021     gint o;
1022
1023     g_free(monitor_area);
1024     extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
1025
1026     if (area) {
1027         for (i = 0; area[i]; ++i)
1028             g_free(area[i]);
1029         g_free(area);
1030     }
1031
1032     area = g_new(Rect*, screen_num_desktops + 2);
1033     for (i = 0; i < screen_num_desktops + 1; ++i)
1034         area[i] = g_new0(Rect, screen_num_monitors + 1);
1035     area[i] = NULL;
1036      
1037     dims = g_new(gulong, 4 * screen_num_desktops);
1038
1039     for (i = 0; i < screen_num_desktops + 1; ++i) {
1040         Strut *struts;
1041         gint l, r, t, b;
1042
1043         struts = g_new0(Strut, screen_num_monitors);
1044
1045         /* calc the xinerama areas */
1046         for (x = 0; x < screen_num_monitors; ++x) {
1047             area[i][x] = monitor_area[x];
1048             if (x == 0) {
1049                 l = monitor_area[x].x;
1050                 t = monitor_area[x].y;
1051                 r = monitor_area[x].x + monitor_area[x].width - 1;
1052                 b = monitor_area[x].y + monitor_area[x].height - 1;
1053             } else {
1054                 l = MIN(l, monitor_area[x].x);
1055                 t = MIN(t, monitor_area[x].y);
1056                 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
1057                 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
1058             }
1059         }
1060         RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
1061
1062         /* apply the struts */
1063
1064         /* find the left-most xin heads, i do this in 2 loops :| */
1065         o = area[i][0].x;
1066         for (x = 1; x < screen_num_monitors; ++x)
1067             o = MIN(o, area[i][x].x);
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_left(&c->strut,
1073                                            &monitor_area[x],
1074                                            o + c->strut.left - area[i][x].x,
1075                                            &struts[x]);
1076             }
1077             screen_area_add_strut_left(&dock_strut,
1078                                        &monitor_area[x],
1079                                        o + dock_strut.left - area[i][x].x,
1080                                        &struts[x]);
1081
1082             area[i][x].x += struts[x].left;
1083             area[i][x].width -= struts[x].left;
1084         }
1085
1086         /* find the top-most xin heads, i do this in 2 loops :| */
1087         o = area[i][0].y;
1088         for (x = 1; x < screen_num_monitors; ++x)
1089             o = MIN(o, area[i][x].y);
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_top(&c->strut,
1095                                            &monitor_area[x],
1096                                            o + c->strut.top - area[i][x].y,
1097                                            &struts[x]);
1098             }
1099             screen_area_add_strut_top(&dock_strut,
1100                                       &monitor_area[x],
1101                                       o + dock_strut.top - area[i][x].y,
1102                                       &struts[x]);
1103
1104             area[i][x].y += struts[x].top;
1105             area[i][x].height -= struts[x].top;
1106         }
1107
1108         /* find the right-most xin heads, i do this in 2 loops :| */
1109         o = area[i][0].x + area[i][0].width - 1;
1110         for (x = 1; x < screen_num_monitors; ++x)
1111             o = MAX(o, area[i][x].x + area[i][x].width - 1);
1112
1113         for (x = 0; x < screen_num_monitors; ++x) {
1114             for (it = client_list; it; it = g_list_next(it)) {
1115                 ObClient *c = it->data;
1116                 screen_area_add_strut_right(&c->strut,
1117                                            &monitor_area[x],
1118                                            (area[i][x].x +
1119                                             area[i][x].width - 1) -
1120                                             (o - c->strut.right),
1121                                             &struts[x]);
1122             }
1123             screen_area_add_strut_right(&dock_strut,
1124                                         &monitor_area[x],
1125                                         (area[i][x].x +
1126                                          area[i][x].width - 1) -
1127                                         (o - dock_strut.right),
1128                                         &struts[x]);
1129
1130             area[i][x].width -= struts[x].right;
1131         }
1132
1133         /* find the bottom-most xin heads, i do this in 2 loops :| */
1134         o = area[i][0].y + area[i][0].height - 1;
1135         for (x = 1; x < screen_num_monitors; ++x)
1136             o = MAX(o, area[i][x].y + area[i][x].height - 1);
1137
1138         for (x = 0; x < screen_num_monitors; ++x) {
1139             for (it = client_list; it; it = g_list_next(it)) {
1140                 ObClient *c = it->data;
1141                 screen_area_add_strut_bottom(&c->strut,
1142                                              &monitor_area[x],
1143                                              (area[i][x].y +
1144                                               area[i][x].height - 1) - \
1145                                              (o - c->strut.bottom),
1146                                              &struts[x]);
1147             }
1148             screen_area_add_strut_bottom(&dock_strut,
1149                                          &monitor_area[x],
1150                                          (area[i][x].y +
1151                                           area[i][x].height - 1) - \
1152                                          (o - dock_strut.bottom),
1153                                          &struts[x]);
1154
1155             area[i][x].height -= struts[x].bottom;
1156         }
1157
1158         l = RECT_LEFT(area[i][0]);
1159         t = RECT_TOP(area[i][0]);
1160         r = RECT_RIGHT(area[i][0]);
1161         b = RECT_BOTTOM(area[i][0]);
1162         for (x = 1; x < screen_num_monitors; ++x) {
1163             l = MIN(l, RECT_LEFT(area[i][x]));
1164             t = MIN(l, RECT_TOP(area[i][x]));
1165             r = MAX(r, RECT_RIGHT(area[i][x]));
1166             b = MAX(b, RECT_BOTTOM(area[i][x]));
1167         }
1168         RECT_SET(area[i][screen_num_monitors], l, t,
1169                  r - l + 1, b - t + 1);
1170
1171         /* XXX optimize when this is run? */
1172
1173         /* the area has changed, adjust all the maximized 
1174            windows */
1175         for (it = client_list; it; it = g_list_next(it)) {
1176             ObClient *c = it->data; 
1177             if (i < screen_num_desktops) {
1178                 if (c->desktop == i)
1179                     client_reconfigure(c);
1180             } else if (c->desktop == DESKTOP_ALL)
1181                 client_reconfigure(c);
1182         }
1183         if (i < screen_num_desktops) {
1184             /* don't set these for the 'all desktops' area */
1185             dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
1186             dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
1187             dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
1188             dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
1189         }
1190
1191         g_free(struts);
1192     }
1193
1194     PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
1195                 dims, 4 * screen_num_desktops);
1196
1197     g_free(dims);
1198 }
1199
1200 Rect *screen_area(guint desktop)
1201 {
1202     return screen_area_monitor(desktop, screen_num_monitors);
1203 }
1204
1205 Rect *screen_area_monitor(guint desktop, guint head)
1206 {
1207     if (head > screen_num_monitors)
1208         return NULL;
1209     if (desktop >= screen_num_desktops) {
1210         if (desktop == DESKTOP_ALL)
1211             return &area[screen_num_desktops][head];
1212         return NULL;
1213     }
1214     return &area[desktop][head];
1215 }
1216
1217 guint screen_find_monitor(Rect *search)
1218 {
1219     guint i;
1220     guint most = 0;
1221     guint mostv = 0;
1222
1223     for (i = 0; i < screen_num_monitors; ++i) {
1224         Rect *area = screen_physical_area_monitor(i);
1225         if (RECT_INTERSECTS_RECT(*area, *search)) {
1226             Rect r;
1227             guint v;
1228
1229             RECT_SET_INTERSECTION(r, *area, *search);
1230             v = r.width * r.height;
1231
1232             if (v > mostv) {
1233                 mostv = v;
1234                 most = i;
1235             }
1236         }
1237     }
1238     return most;
1239 }
1240
1241 Rect *screen_physical_area()
1242 {
1243     return screen_physical_area_monitor(screen_num_monitors);
1244 }
1245
1246 Rect *screen_physical_area_monitor(guint head)
1247 {
1248     if (head > screen_num_monitors)
1249         return NULL;
1250     return &monitor_area[head];
1251 }
1252
1253 void screen_set_root_cursor()
1254 {
1255     if (sn_app_starting())
1256         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1257                       ob_cursor(OB_CURSOR_BUSY));
1258     else
1259         XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1260                       ob_cursor(OB_CURSOR_POINTER));
1261 }
1262
1263 gboolean screen_pointer_pos(gint *x, gint *y)
1264 {
1265     Window w;
1266     gint i;
1267     guint u;
1268
1269     return !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
1270                            &w, &w, x, y, &i, &i, &u);
1271 }