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