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