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