start with 0 desktops, so the focus_order stuff all gets initialized properly
[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
33 static void screen_update_area();
34
35 static gboolean running;
36 static int another_running(Display *d, XErrorEvent *e)
37 {
38     (void)d;(void)e;
39     g_message("A window manager is already running on screen %d",
40               ob_screen);
41     running = TRUE;
42     return -1;
43 }
44
45 gboolean screen_annex()
46 {
47     XErrorHandler old;
48     Window support;
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 = 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);
75
76     /* set properties on the supporting window */
77     PROP_SETS(support, net_wm_name, utf8, "Openbox");
78     PROP_SET32(support, net_supporting_wm_check, window, support);
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_set_num_desktops(4);
157     screen_set_desktop(0);
158
159     /* don't start in showing-desktop mode */
160     screen_showing_desktop = FALSE;
161     PROP_SET32(ob_root, net_showing_desktop, cardinal, screen_showing_desktop);
162
163     screen_update_layout();
164 }
165
166 void screen_shutdown()
167 {
168     guint i;
169     for (i = 0; i < screen_desktop_names->len; ++i)
170         g_free(g_ptr_array_index(screen_desktop_names, i));
171     g_ptr_array_free(screen_desktop_names, TRUE);
172     g_free(strut);
173     g_free(area);
174 }
175
176 void screen_resize()
177 {
178     /* Set the _NET_DESKTOP_GEOMETRY hint */
179     /* XXX RandR support here? */
180     int geometry[2];
181
182     geometry[0] = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
183     geometry[1] = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
184     PROP_SET32A(ob_root, net_desktop_geometry, cardinal, geometry, 2);
185     screen_physical_size.width = geometry[0];
186     screen_physical_size.height = geometry[1];
187
188     if (ob_state == State_Starting)
189         return;
190
191     screen_update_struts();
192
193     /* XXX adjust more stuff ? */
194 }
195
196 void screen_set_num_desktops(guint num)
197 {
198     guint i, old;
199     gulong *viewport;
200      
201     g_assert(num > 0);
202   
203     /* move windows on desktops that will no longer exist! */
204     /* XXX
205        std::list<Client*>::iterator it, end = clients.end();
206        for (it = clients.begin(); it != end; ++it) {
207        unsigned int d = (*it)->desktop();
208        if (d >= num && d != 0xffffffff) {
209        XEvent ce;
210        ce.xclient.type = ClientMessage;
211        ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
212        ce.xclient.display = **otk::display;
213        ce.xclient.window = (*it)->window();
214        ce.xclient.format = 32;
215        ce.xclient.data.l[0] = num - 1;
216        XSendEvent(**otk::display, _info->rootWindow(), false,
217        SubstructureNotifyMask | SubstructureRedirectMask, &ce);
218        }
219        }
220     */
221
222     old = screen_num_desktops;
223     screen_num_desktops = num;
224     PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
225
226     /* set the viewport hint */
227     viewport = g_new0(gulong, num * 2);
228     PROP_SET32A(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
229     g_free(viewport);
230
231     /* change our struts/area to match */
232     screen_update_struts();
233
234     /* the number of rows/columns will differ */
235     screen_update_layout();
236
237     /* may be some unnamed desktops that we need to fill in with names */
238     screen_update_desktop_names();
239
240     /* update the focus lists */
241     /* free our lists for the desktops which have disappeared */
242     for (i = num; i < old; ++i)
243         g_list_free(focus_order[i]);
244     /* realloc the array */
245     focus_order = g_renew(GList*, focus_order, num);
246     /* set the new lists to be empty */
247     for (i = old; i < num; ++i)
248         focus_order[i] = NULL;
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      
262     g_assert(num < screen_num_desktops);
263
264     g_message("Moving to desktop %u", num);
265   
266     old = screen_desktop;
267     screen_desktop = num;
268     PROP_SET32(ob_root, net_current_desktop, cardinal, num);
269
270     if (old == num) return;
271
272     /* hide windows from bottom to top */
273     for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
274         Client *c = it->data;
275         if (c->frame->visible && !client_should_show(c))
276             engine_frame_hide(c->frame);
277     }
278
279     /* show windows from top to bottom */
280     for (it = stacking_list; it != NULL; it = it->next) {
281         Client *c = it->data;
282         if (!c->frame->visible && client_should_show(c))
283             engine_frame_show(c->frame);
284     }
285
286     dispatch_ob(Event_Ob_Desktop, num, old);
287 }
288
289 void screen_update_layout()
290 {
291     unsigned long *data = NULL;
292
293     /* defaults */
294     screen_desktop_layout.orientation = prop_atoms.net_wm_orientation_horz;
295     screen_desktop_layout.start_corner = prop_atoms.net_wm_topleft;
296     screen_desktop_layout.rows = 1;
297     screen_desktop_layout.columns = screen_num_desktops;
298
299     if (PROP_GET32A(ob_root, net_desktop_layout, cardinal, data, 4)) {
300         if (data[0] == prop_atoms.net_wm_orientation_vert)
301             screen_desktop_layout.orientation = data[0];
302         if (data[3] == prop_atoms.net_wm_topright)
303             screen_desktop_layout.start_corner = data[3];
304         else if (data[3] == prop_atoms.net_wm_bottomright)
305             screen_desktop_layout.start_corner = data[3];
306         else if (data[3] == prop_atoms.net_wm_bottomleft)
307             screen_desktop_layout.start_corner = data[3];
308
309         /* fill in a zero rows/columns */
310         if (!(data[1] == 0 && data[2] == 0)) { /* both 0's is bad data.. */
311             if (data[1] == 0) {
312                 data[1] = (screen_num_desktops +
313                            screen_num_desktops % data[2]) / data[2];
314             } else if (data[2] == 0) {
315                 data[2] = (screen_num_desktops +
316                            screen_num_desktops % data[1]) / data[1];
317             }
318             screen_desktop_layout.columns = data[1];
319             screen_desktop_layout.rows = data[2];
320         }
321
322         /* bounds checking */
323         if (screen_desktop_layout.orientation ==
324             prop_atoms.net_wm_orientation_horz) {
325             if (screen_desktop_layout.rows > screen_num_desktops)
326                 screen_desktop_layout.rows = screen_num_desktops;
327             if (screen_desktop_layout.columns > ((screen_num_desktops +
328                                                   screen_num_desktops %
329                                                   screen_desktop_layout.rows) /
330                                                  screen_desktop_layout.rows))
331                 screen_desktop_layout.columns =
332                     (screen_num_desktops + screen_num_desktops %
333                      screen_desktop_layout.rows) /
334                     screen_desktop_layout.rows;
335         } else {
336             if (screen_desktop_layout.columns > screen_num_desktops)
337                 screen_desktop_layout.columns = screen_num_desktops;
338             if (screen_desktop_layout.rows > ((screen_num_desktops +
339                                                screen_num_desktops %
340                                                screen_desktop_layout.columns) /
341                                               screen_desktop_layout.columns))
342                 screen_desktop_layout.rows =
343                     (screen_num_desktops + screen_num_desktops %
344                      screen_desktop_layout.columns) /
345                     screen_desktop_layout.columns;
346         }
347         g_free(data);
348     }
349 }
350
351 void screen_update_desktop_names()
352 {
353     guint i;
354
355     /* empty the array */
356     for (i = 0; i < screen_desktop_names->len; ++i)
357         g_free(g_ptr_array_index(screen_desktop_names, i));
358     g_ptr_array_set_size(screen_desktop_names, 0);
359
360     PROP_GETSA(ob_root, net_desktop_names, utf8, screen_desktop_names);
361
362     while (screen_desktop_names->len < screen_num_desktops)
363         g_ptr_array_add(screen_desktop_names, g_strdup("Unnamed Desktop"));
364 }
365
366 void screen_show_desktop(gboolean show)
367 {
368     GList *it;
369     static Window saved_focus = 0;
370      
371     if (show == screen_showing_desktop) return; /* no change */
372
373     /* save the window focus, and restore it when leaving the show-desktop
374        mode */
375     if (show && focus_client)
376         saved_focus = focus_client->window;
377   
378     screen_showing_desktop = show;
379
380     if (show) {
381         /* bottom to top */
382         for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
383             Client *client = it->data;
384             if (client->type == Type_Desktop)
385                 client_focus(client);
386             else if (client->frame->visible && !client_should_show(client))
387                 engine_frame_hide(client->frame);
388         }
389     } else {
390         /* top to bottom */
391         for (it = stacking_list; it != NULL; it = it->next) {
392             Client *client = it->data;
393             if (!client->frame->visible && client_should_show(client))
394                 engine_frame_show(client->frame);
395         }
396     }
397
398     if (!show) {
399         Client *f = focus_client;
400         if (!f || f->type == Type_Desktop) {
401             Client *c = g_hash_table_lookup(client_map,
402                                             (gpointer)saved_focus);
403             if (c) client_focus(c);
404         }
405     }
406
407     show = !!show; /* make it boolean */
408     PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
409
410     dispatch_ob(Event_Ob_ShowDesktop, show, 0);
411 }
412
413 void screen_install_colormap(Client *client, gboolean install)
414 {
415     if (client == NULL) {
416         if (install)
417             XInstallColormap(ob_display, render_colormap);
418         else
419             XUninstallColormap(ob_display, render_colormap);
420     } else {
421         XWindowAttributes wa;
422         if (XGetWindowAttributes(ob_display, client->window, &wa)) {
423             if (install)
424                 XInstallColormap(ob_display, wa.colormap);
425             else
426                 XUninstallColormap(ob_display, wa.colormap);
427         }
428     }
429 }
430
431 void screen_update_struts()
432 {
433     GSList *it;
434     guint i;
435      
436     if (strut != NULL)
437         g_free(strut);
438     strut = g_new0(Strut, screen_num_desktops + 1);
439
440     for (it = client_list; it; it = it->next) {
441         Client *c = it->data;
442         if (c->iconic) continue; /* these dont count in the strut */
443     
444         if (c->desktop == 0xffffffff) {
445             for (i = 0; i < screen_num_desktops; ++i)
446                 STRUT_ADD(strut[i], c->strut);
447         } else {
448             g_assert(c->desktop < screen_num_desktops);
449             STRUT_ADD(strut[c->desktop], c->strut);
450         }
451         /* apply to the 'all desktops' strut */
452         STRUT_ADD(strut[screen_num_desktops], c->strut);
453     }
454     screen_update_area();
455 }
456
457 static void screen_update_area()
458 {
459     guint i;
460     gulong *dims;
461      
462     if (area != NULL)
463         g_free(area);
464     area = g_new0(Rect, screen_num_desktops + 1);
465      
466     dims = g_new(unsigned long, 4 * screen_num_desktops);
467     for (i = 0; i < screen_num_desktops + 1; ++i) {
468         Rect old_area = area[i];
469 /*
470   #ifdef    XINERAMA
471   // reset to the full areas
472   if (isXineramaActive())
473   xineramaUsableArea = getXineramaAreas();
474   #endif // XINERAMA
475 */
476
477         RECT_SET(area[i], strut[i].left, strut[i].top,
478                  screen_physical_size.width - (strut[i].left +
479                                                strut[i].right),
480                  screen_physical_size.height - (strut[i].top +
481                                                 strut[i].bottom));
482     
483 /*
484   #ifdef    XINERAMA
485   if (isXineramaActive()) {
486   // keep each of the ximerama-defined areas inside the strut
487   RectList::iterator xit, xend = xineramaUsableArea.end();
488   for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
489   if (xit->x() < usableArea.x()) {
490   xit->setX(usableArea.x());
491   xit->setWidth(xit->width() - usableArea.x());
492   }
493   if (xit->y() < usableArea.y()) {
494   xit->setY(usableArea.y());
495   xit->setHeight(xit->height() - usableArea.y());
496   }
497   if (xit->x() + xit->width() > usableArea.width())
498   xit->setWidth(usableArea.width() - xit->x());
499   if (xit->y() + xit->height() > usableArea.height())
500   xit->setHeight(usableArea.height() - xit->y());
501   }
502   }
503   #endif // XINERAMA
504 */
505         if (!RECT_EQUAL(old_area, area[i])) {
506             /* the area has changed, adjust all the maximized windows */
507             GSList *it;
508             for (it = client_list; it; it = it->next) {
509                 Client *c = it->data;
510                 if (i < screen_num_desktops) {
511                     if (c->desktop == i)
512                         client_remaximize(c);
513                 } else {
514                     /* the 'all desktops' size */
515                     if (c->desktop == DESKTOP_ALL)
516                         client_remaximize(c);
517                 }
518             }
519         }
520
521         /* don't set these for the 'all desktops' area */
522         if (i < screen_num_desktops) {
523             dims[(i * 4) + 0] = area[i].x;
524             dims[(i * 4) + 1] = area[i].y;
525             dims[(i * 4) + 2] = area[i].width;
526             dims[(i * 4) + 3] = area[i].height;
527         }
528     }
529     PROP_SET32A(ob_root, net_workarea, cardinal,
530                 dims, 4 * screen_num_desktops);
531     g_free(dims);
532 }
533
534 Rect *screen_area(guint desktop)
535 {
536     if (desktop >= screen_num_desktops) {
537         if (desktop == DESKTOP_ALL)
538             return &area[screen_num_desktops];
539         return NULL;
540     }
541     return &area[desktop];
542 }
543
544 Strut *screen_strut(guint desktop)
545 {
546     if (desktop >= screen_num_desktops) {
547         if (desktop == DESKTOP_ALL)
548             return &strut[screen_num_desktops];
549         return NULL;
550     }
551     return &strut[desktop];
552 }