add more options for focus fallback, use an enum for all the types of fallbacks.
[dana/openbox.git] / openbox / screen.c
1 #include "openbox.h"
2 #include "prop.h"
3 #include "screen.h"
4 #include "client.h"
5 #include "frame.h"
6 #include "engine.h"
7 #include "focus.h"
8 #include "dispatch.h"
9 #include "../render/render.h"
10
11 #include <X11/Xlib.h>
12 #ifdef HAVE_UNISTD_H
13 #  include <sys/types.h>
14 #  include <unistd.h>
15 #endif
16
17 /*! The event mask to grab on the root window */
18 #define ROOT_EVENTMASK (/*ColormapChangeMask |*/ PropertyChangeMask | \
19                         EnterWindowMask | LeaveWindowMask | \
20                         SubstructureNotifyMask | SubstructureRedirectMask | \
21                         ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
22
23 guint    screen_num_desktops    = 0;
24 guint    screen_desktop         = 0;
25 Size     screen_physical_size;
26 gboolean screen_showing_desktop;
27 DesktopLayout screen_desktop_layout;
28 GPtrArray *screen_desktop_names;
29
30 static Rect  *area = NULL;
31 static Strut *strut = NULL;
32 static Window support_window = None;
33
34 static void screen_update_area();
35
36 static gboolean running;
37 static int another_running(Display *d, XErrorEvent *e)
38 {
39     (void)d;(void)e;
40     g_message("A window manager is already running on screen %d",
41               ob_screen);
42     running = TRUE;
43     return -1;
44 }
45
46 gboolean screen_annex()
47 {
48     XErrorHandler old;
49     pid_t pid;
50     int i, num_support;
51     Atom *supported;
52
53     running = FALSE;
54     old = XSetErrorHandler(another_running);
55     XSelectInput(ob_display, ob_root, ROOT_EVENTMASK);
56     XSync(ob_display, FALSE);
57     XSetErrorHandler(old);
58     if (running)
59         return FALSE;
60
61     g_message("Managing screen %d", ob_screen);
62
63     /* set the mouse cursor for the root window (the default cursor) */
64     XDefineCursor(ob_display, ob_root, ob_cursors.left_ptr);
65
66     /* set the OPENBOX_PID hint */
67     pid = getpid();
68     PROP_SET32(ob_root, openbox_pid, cardinal, pid);
69
70     /* create the netwm support window */
71     support_window = XCreateSimpleWindow(ob_display, ob_root, 0,0,1,1,0,0,0);
72
73     /* set supporting window */
74     PROP_SET32(ob_root, net_supporting_wm_check, window, support_window);
75
76     /* set properties on the supporting window */
77     PROP_SETS(support_window, net_wm_name, utf8, "Openbox");
78     PROP_SET32(support_window, net_supporting_wm_check, window,support_window);
79
80     /* set the _NET_SUPPORTED_ATOMS hint */
81     num_support = 48;
82     i = 0;
83     supported = g_new(Atom, num_support);
84     supported[i++] = prop_atoms.net_current_desktop;
85     supported[i++] = prop_atoms.net_number_of_desktops;
86     supported[i++] = prop_atoms.net_desktop_geometry;
87     supported[i++] = prop_atoms.net_desktop_viewport;
88     supported[i++] = prop_atoms.net_active_window;
89     supported[i++] = prop_atoms.net_workarea;
90     supported[i++] = prop_atoms.net_client_list;
91     supported[i++] = prop_atoms.net_client_list_stacking;
92     supported[i++] = prop_atoms.net_desktop_names;
93     supported[i++] = prop_atoms.net_close_window;
94     supported[i++] = prop_atoms.net_desktop_layout;
95     supported[i++] = prop_atoms.net_showing_desktop;
96     supported[i++] = prop_atoms.net_wm_name;
97     supported[i++] = prop_atoms.net_wm_visible_name;
98     supported[i++] = prop_atoms.net_wm_icon_name;
99     supported[i++] = prop_atoms.net_wm_visible_icon_name;
100     supported[i++] = prop_atoms.net_wm_desktop;
101     supported[i++] = prop_atoms.net_wm_strut;
102     supported[i++] = prop_atoms.net_wm_window_type;
103     supported[i++] = prop_atoms.net_wm_window_type_desktop;
104     supported[i++] = prop_atoms.net_wm_window_type_dock;
105     supported[i++] = prop_atoms.net_wm_window_type_toolbar;
106     supported[i++] = prop_atoms.net_wm_window_type_menu;
107     supported[i++] = prop_atoms.net_wm_window_type_utility;
108     supported[i++] = prop_atoms.net_wm_window_type_splash;
109     supported[i++] = prop_atoms.net_wm_window_type_dialog;
110     supported[i++] = prop_atoms.net_wm_window_type_normal;
111     supported[i++] = prop_atoms.net_wm_allowed_actions;
112     supported[i++] = prop_atoms.net_wm_action_move;
113     supported[i++] = prop_atoms.net_wm_action_resize;
114     supported[i++] = prop_atoms.net_wm_action_minimize;
115     supported[i++] = prop_atoms.net_wm_action_shade;
116     supported[i++] = prop_atoms.net_wm_action_maximize_horz;
117     supported[i++] = prop_atoms.net_wm_action_maximize_vert;
118     supported[i++] = prop_atoms.net_wm_action_fullscreen;
119     supported[i++] = prop_atoms.net_wm_action_change_desktop;
120     supported[i++] = prop_atoms.net_wm_action_close;
121     supported[i++] = prop_atoms.net_wm_state;
122     supported[i++] = prop_atoms.net_wm_state_modal;
123     supported[i++] = prop_atoms.net_wm_state_maximized_vert;
124     supported[i++] = prop_atoms.net_wm_state_maximized_horz;
125     supported[i++] = prop_atoms.net_wm_state_shaded;
126     supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
127     supported[i++] = prop_atoms.net_wm_state_skip_pager;
128     supported[i++] = prop_atoms.net_wm_state_hidden;
129     supported[i++] = prop_atoms.net_wm_state_fullscreen;
130     supported[i++] = prop_atoms.net_wm_state_above;
131     supported[i++] = prop_atoms.net_wm_state_below;
132     g_assert(i == num_support);
133 /*
134   supported[] = prop_atoms.net_wm_moveresize;
135   supported[] = prop_atoms.net_wm_moveresize_size_topleft;
136   supported[] = prop_atoms.net_wm_moveresize_size_topright;
137   supported[] = prop_atoms.net_wm_moveresize_size_bottomleft;
138   supported[] = prop_atoms.net_wm_moveresize_size_bottomright;
139   supported[] = prop_atoms.net_wm_moveresize_move;
140   supported[] = prop_atoms.net_wm_action_stick;
141 */
142
143     PROP_SET32A(ob_root, net_supported, atom, supported, num_support);
144     g_free(supported);
145
146     return TRUE;
147 }
148
149 void screen_startup()
150 {
151     screen_desktop_names = g_ptr_array_new();
152      
153     /* get the initial size */
154     screen_resize();
155
156     screen_num_desktops = 0;
157     screen_set_num_desktops(4);
158     screen_desktop = 0;
159     screen_set_desktop(0);
160
161     /* don't start in showing-desktop mode */
162     screen_showing_desktop = FALSE;
163     PROP_SET32(ob_root, net_showing_desktop, cardinal, screen_showing_desktop);
164
165     screen_update_layout();
166 }
167
168 void screen_shutdown()
169 {
170     guint i;
171
172     XSelectInput(ob_display, ob_root, NoEventMask);
173
174     PROP_ERASE(ob_root, openbox_pid); /* we're not running here no more! */
175     PROP_ERASE(ob_root, net_supported); /* not without us */
176     PROP_ERASE(ob_root, net_showing_desktop); /* don't keep this mode */
177
178     XDestroyWindow(ob_display, support_window);
179
180     for (i = 0; i < screen_desktop_names->len; ++i)
181         g_free(g_ptr_array_index(screen_desktop_names, i));
182     g_ptr_array_free(screen_desktop_names, TRUE);
183     g_free(strut);
184     g_free(area);
185 }
186
187 void screen_resize()
188 {
189     /* XXX RandR support here? */
190     int geometry[2];
191
192     /* Set the _NET_DESKTOP_GEOMETRY hint */
193     geometry[0] = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
194     geometry[1] = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
195     PROP_SET32A(ob_root, net_desktop_geometry, cardinal, geometry, 2);
196     screen_physical_size.width = geometry[0];
197     screen_physical_size.height = geometry[1];
198
199     if (ob_state == State_Starting)
200         return;
201
202     screen_update_struts();
203
204     /* XXX adjust more stuff ? */
205 }
206
207 void screen_set_num_desktops(guint num)
208 {
209     guint i, old;
210     gulong *viewport;
211     GList *it;
212
213     g_assert(num > 0);
214
215     old = screen_num_desktops;
216     screen_num_desktops = num;
217     PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
218
219     /* set the viewport hint */
220     viewport = g_new0(gulong, num * 2);
221     PROP_SET32A(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
222     g_free(viewport);
223
224     /* change our struts/area to match */
225     screen_update_struts();
226
227     /* the number of rows/columns will differ */
228     screen_update_layout();
229
230     /* may be some unnamed desktops that we need to fill in with names */
231     screen_update_desktop_names();
232
233     /* update the focus lists */
234     /* free our lists for the desktops which have disappeared */
235     for (i = num; i < old; ++i)
236         g_list_free(focus_order[i]);
237     /* realloc the array */
238     focus_order = g_renew(GList*, focus_order, num);
239     /* set the new lists to be empty */
240     for (i = old; i < num; ++i)
241         focus_order[i] = NULL;
242
243     /* move windows on desktops that will no longer exist! */
244     for (it = client_list; it != NULL; it = it->next) {
245         Client *c = it->data;
246         if (c->desktop >= num)
247             client_set_desktop(c, num - 1, FALSE);
248     }
249
250     dispatch_ob(Event_Ob_NumDesktops, num, old);
251
252     /* change our desktop if we're on one that no longer exists! */
253     if (screen_desktop >= screen_num_desktops)
254         screen_set_desktop(num - 1);
255 }
256
257 void screen_set_desktop(guint num)
258 {
259     GList *it;
260     guint old;
261     XEvent e;
262      
263     g_assert(num < screen_num_desktops);
264
265     old = screen_desktop;
266     screen_desktop = num;
267     PROP_SET32(ob_root, net_current_desktop, cardinal, num);
268
269     if (old == num) return;
270
271     g_message("Moving to desktop %d", num+1);
272
273     /* show windows before hiding the rest to lessen the enter/leave events */
274
275     /* show windows from top to bottom */
276     for (it = stacking_list; it != NULL; it = it->next) {
277         Client *c = it->data;
278         if (!c->frame->visible && client_should_show(c))
279             engine_frame_show(c->frame);
280     }
281
282     /* hide windows from bottom to top */
283     for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
284         Client *c = it->data;
285         if (c->frame->visible && !client_should_show(c))
286             engine_frame_hide(c->frame);
287     }
288
289     /* focus the last focused window on the desktop, and ignore enter events
290        from the switch so it doesnt mess with the focus */
291     XSync(ob_display, FALSE);
292     while (XCheckTypedEvent(ob_display, EnterNotify, &e));
293     focus_fallback(Fallback_Desktop);
294
295     dispatch_ob(Event_Ob_Desktop, num, old);
296 }
297
298 void screen_update_layout()
299 {
300     unsigned long *data = NULL;
301
302     /* defaults */
303     screen_desktop_layout.orientation = prop_atoms.net_wm_orientation_horz;
304     screen_desktop_layout.start_corner = prop_atoms.net_wm_topleft;
305     screen_desktop_layout.rows = 1;
306     screen_desktop_layout.columns = screen_num_desktops;
307
308     if (PROP_GET32A(ob_root, net_desktop_layout, cardinal, data, 4)) {
309         if (data[0] == prop_atoms.net_wm_orientation_vert)
310             screen_desktop_layout.orientation = data[0];
311         if (data[3] == prop_atoms.net_wm_topright)
312             screen_desktop_layout.start_corner = data[3];
313         else if (data[3] == prop_atoms.net_wm_bottomright)
314             screen_desktop_layout.start_corner = data[3];
315         else if (data[3] == prop_atoms.net_wm_bottomleft)
316             screen_desktop_layout.start_corner = data[3];
317
318         /* fill in a zero rows/columns */
319         if (!(data[1] == 0 && data[2] == 0)) { /* both 0's is bad data.. */
320             if (data[1] == 0) {
321                 data[1] = (screen_num_desktops +
322                            screen_num_desktops % data[2]) / data[2];
323             } else if (data[2] == 0) {
324                 data[2] = (screen_num_desktops +
325                            screen_num_desktops % data[1]) / data[1];
326             }
327             screen_desktop_layout.columns = data[1];
328             screen_desktop_layout.rows = data[2];
329         }
330
331         /* bounds checking */
332         if (screen_desktop_layout.orientation ==
333             prop_atoms.net_wm_orientation_horz) {
334             if (screen_desktop_layout.rows > screen_num_desktops)
335                 screen_desktop_layout.rows = screen_num_desktops;
336             if (screen_desktop_layout.columns > ((screen_num_desktops +
337                                                   screen_num_desktops %
338                                                   screen_desktop_layout.rows) /
339                                                  screen_desktop_layout.rows))
340                 screen_desktop_layout.columns =
341                     (screen_num_desktops + screen_num_desktops %
342                      screen_desktop_layout.rows) /
343                     screen_desktop_layout.rows;
344         } else {
345             if (screen_desktop_layout.columns > screen_num_desktops)
346                 screen_desktop_layout.columns = screen_num_desktops;
347             if (screen_desktop_layout.rows > ((screen_num_desktops +
348                                                screen_num_desktops %
349                                                screen_desktop_layout.columns) /
350                                               screen_desktop_layout.columns))
351                 screen_desktop_layout.rows =
352                     (screen_num_desktops + screen_num_desktops %
353                      screen_desktop_layout.columns) /
354                     screen_desktop_layout.columns;
355         }
356         g_free(data);
357     }
358 }
359
360 void screen_update_desktop_names()
361 {
362     guint i;
363
364     /* empty the array */
365     for (i = 0; i < screen_desktop_names->len; ++i)
366         g_free(g_ptr_array_index(screen_desktop_names, i));
367     g_ptr_array_set_size(screen_desktop_names, 0);
368
369     PROP_GETSA(ob_root, net_desktop_names, utf8, screen_desktop_names);
370
371     while (screen_desktop_names->len < screen_num_desktops)
372         g_ptr_array_add(screen_desktop_names, g_strdup("Unnamed Desktop"));
373 }
374
375 void screen_show_desktop(gboolean show)
376 {
377     GList *it;
378      
379     if (show == screen_showing_desktop) return; /* no change */
380
381     screen_showing_desktop = show;
382
383     if (show) {
384         /* bottom to top */
385         for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
386             Client *client = it->data;
387             if (client->frame->visible && !client_should_show(client))
388                 engine_frame_hide(client->frame);
389         }
390     } else {
391         /* top to bottom */
392         for (it = stacking_list; it != NULL; it = it->next) {
393             Client *client = it->data;
394             if (!client->frame->visible && client_should_show(client))
395                 engine_frame_show(client->frame);
396         }
397     }
398
399     show = !!show; /* make it boolean */
400     PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
401
402     dispatch_ob(Event_Ob_ShowDesktop, show, 0);
403 }
404
405 void screen_install_colormap(Client *client, gboolean install)
406 {
407     XWindowAttributes wa;
408
409     if (client == NULL) {
410         if (install)
411             XInstallColormap(ob_display, render_colormap);
412         else
413             XUninstallColormap(ob_display, render_colormap);
414     } else {
415         if (XGetWindowAttributes(ob_display, client->window, &wa) &&
416             wa.colormap != None) {
417             if (install)
418                 XInstallColormap(ob_display, wa.colormap);
419             else
420                 XUninstallColormap(ob_display, wa.colormap);
421         }
422     }
423 }
424
425 void screen_update_struts()
426 {
427     GList *it;
428     guint i;
429      
430     g_free(strut);
431     strut = g_new0(Strut, screen_num_desktops + 1);
432
433     for (it = client_list; it != NULL; it = it->next) {
434         Client *c = it->data;
435         if (c->iconic) continue; /* these dont count in the strut */
436     
437         if (c->desktop == 0xffffffff) {
438             for (i = 0; i < screen_num_desktops; ++i)
439                 STRUT_ADD(strut[i], c->strut);
440         } else {
441             g_assert(c->desktop < screen_num_desktops);
442             STRUT_ADD(strut[c->desktop], c->strut);
443         }
444         /* apply to the 'all desktops' strut */
445         STRUT_ADD(strut[screen_num_desktops], c->strut);
446     }
447     screen_update_area();
448 }
449
450 static void screen_update_area()
451 {
452     guint i;
453     gulong *dims;
454
455     g_free(area);
456     area = g_new0(Rect, screen_num_desktops + 1);
457      
458     dims = g_new(unsigned long, 4 * screen_num_desktops);
459     for (i = 0; i < screen_num_desktops + 1; ++i) {
460         Rect old_area = area[i];
461 /*
462   #ifdef    XINERAMA
463   // reset to the full areas
464   if (isXineramaActive())
465   xineramaUsableArea = getXineramaAreas();
466   #endif // XINERAMA
467 */
468
469         RECT_SET(area[i], strut[i].left, strut[i].top,
470                  screen_physical_size.width - (strut[i].left +
471                                                strut[i].right),
472                  screen_physical_size.height - (strut[i].top +
473                                                 strut[i].bottom));
474     
475 /*
476   #ifdef    XINERAMA
477   if (isXineramaActive()) {
478   // keep each of the ximerama-defined areas inside the strut
479   RectList::iterator xit, xend = xineramaUsableArea.end();
480   for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
481   if (xit->x() < usableArea.x()) {
482   xit->setX(usableArea.x());
483   xit->setWidth(xit->width() - usableArea.x());
484   }
485   if (xit->y() < usableArea.y()) {
486   xit->setY(usableArea.y());
487   xit->setHeight(xit->height() - usableArea.y());
488   }
489   if (xit->x() + xit->width() > usableArea.width())
490   xit->setWidth(usableArea.width() - xit->x());
491   if (xit->y() + xit->height() > usableArea.height())
492   xit->setHeight(usableArea.height() - xit->y());
493   }
494   }
495   #endif // XINERAMA
496 */
497         if (!RECT_EQUAL(old_area, area[i])) {
498             /* the area has changed, adjust all the maximized windows */
499             GList *it;
500             for (it = client_list; it; it = it->next) {
501                 Client *c = it->data;
502                 if (i < screen_num_desktops) {
503                     if (c->desktop == i)
504                         client_remaximize(c);
505                 } else {
506                     /* the 'all desktops' size */
507                     if (c->desktop == DESKTOP_ALL)
508                         client_remaximize(c);
509                 }
510             }
511         }
512
513         /* don't set these for the 'all desktops' area */
514         if (i < screen_num_desktops) {
515             dims[(i * 4) + 0] = area[i].x;
516             dims[(i * 4) + 1] = area[i].y;
517             dims[(i * 4) + 2] = area[i].width;
518             dims[(i * 4) + 3] = area[i].height;
519         }
520     }
521     PROP_SET32A(ob_root, net_workarea, cardinal,
522                 dims, 4 * screen_num_desktops);
523     g_free(dims);
524 }
525
526 Rect *screen_area(guint desktop)
527 {
528     if (desktop >= screen_num_desktops) {
529         if (desktop == DESKTOP_ALL)
530             return &area[screen_num_desktops];
531         return NULL;
532     }
533     return &area[desktop];
534 }
535
536 Strut *screen_strut(guint desktop)
537 {
538     if (desktop >= screen_num_desktops) {
539         if (desktop == DESKTOP_ALL)
540             return &strut[screen_num_desktops];
541         return NULL;
542     }
543     return &strut[desktop];
544 }