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