fix a seg from ignoreing PROP_GETSS failure
[dana/openbox.git] / openbox / screen.c
1 #include "openbox.h"
2 #include "prop.h"
3 #include "config.h"
4 #include "screen.h"
5 #include "client.h"
6 #include "frame.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 char   **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     guint32 *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, "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(guint32, 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_SETA32(ob_root, net_supported, atom, supported, num_support);
144     g_free(supported);
145
146     return TRUE;
147 }
148
149 void screen_startup()
150 {
151     GSList *it;
152     guint i;
153
154     /* get the initial size */
155     screen_resize();
156
157     /* set the names */
158     screen_desktop_names = g_new0(char*,
159                                   g_slist_length(config_desktops_names) + 1);
160     for (i = 0, it = config_desktops_names; it; ++i, it = it->next)
161         screen_desktop_names[i] = it->data; /* dont strdup */
162     PROP_SETSS(ob_root, net_desktop_names, screen_desktop_names);
163     g_free(screen_desktop_names); /* dont free the individual strings */
164     screen_desktop_names = NULL;
165
166     screen_num_desktops = 0;
167     screen_set_num_desktops(config_desktops_num);
168     screen_desktop = 0;
169     screen_set_desktop(0);
170
171     /* don't start in showing-desktop mode */
172     screen_showing_desktop = FALSE;
173     PROP_SET32(ob_root, net_showing_desktop, cardinal, screen_showing_desktop);
174
175     screen_update_layout();
176 }
177
178 void screen_shutdown()
179 {
180     XSelectInput(ob_display, ob_root, NoEventMask);
181
182     PROP_ERASE(ob_root, openbox_pid); /* we're not running here no more! */
183     PROP_ERASE(ob_root, net_supported); /* not without us */
184     PROP_ERASE(ob_root, net_showing_desktop); /* don't keep this mode */
185
186     XDestroyWindow(ob_display, support_window);
187
188     g_strfreev(screen_desktop_names);
189     g_free(strut);
190     g_free(area);
191 }
192
193 void screen_resize()
194 {
195     /* XXX RandR support here? */
196     guint32 geometry[2];
197
198     /* Set the _NET_DESKTOP_GEOMETRY hint */
199     geometry[0] = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
200     geometry[1] = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
201     PROP_SETA32(ob_root, net_desktop_geometry, cardinal, geometry, 2);
202     screen_physical_size.width = geometry[0];
203     screen_physical_size.height = geometry[1];
204
205     if (ob_state == State_Starting)
206         return;
207
208     screen_update_struts();
209
210     /* XXX adjust more stuff ? */
211 }
212
213 void screen_set_num_desktops(guint num)
214 {
215     guint i, old;
216     guint32 *viewport;
217     GList *it;
218
219     g_assert(num > 0);
220
221     old = screen_num_desktops;
222     screen_num_desktops = num;
223     PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
224
225     /* set the viewport hint */
226     viewport = g_new0(guint32, num * 2);
227     PROP_SETA32(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
228     g_free(viewport);
229
230     /* change our struts/area to match */
231     screen_update_struts();
232
233     /* the number of rows/columns will differ */
234     screen_update_layout();
235
236     /* may be some unnamed desktops that we need to fill in with names */
237     screen_update_desktop_names();
238
239     /* update the focus lists */
240     /* free our lists for the desktops which have disappeared */
241     for (i = num; i < old; ++i)
242         g_list_free(focus_order[i]);
243     /* realloc the array */
244     focus_order = g_renew(GList*, focus_order, num);
245     /* set the new lists to be empty */
246     for (i = old; i < num; ++i)
247         focus_order[i] = NULL;
248
249     /* move windows on desktops that will no longer exist! */
250     for (it = client_list; it != NULL; it = it->next) {
251         Client *c = it->data;
252         if (c->desktop >= num)
253             client_set_desktop(c, num - 1, FALSE);
254     }
255
256     dispatch_ob(Event_Ob_NumDesktops, num, old);
257
258     /* change our desktop if we're on one that no longer exists! */
259     if (screen_desktop >= screen_num_desktops)
260         screen_set_desktop(num - 1);
261 }
262
263 void screen_set_desktop(guint num)
264 {
265     GList *it;
266     guint old;
267     XEvent e;
268      
269     g_assert(num < screen_num_desktops);
270
271     old = screen_desktop;
272     screen_desktop = num;
273     PROP_SET32(ob_root, net_current_desktop, cardinal, num);
274
275     if (old == num) return;
276
277     g_message("Moving to desktop %d", num+1);
278
279     /* show windows before hiding the rest to lessen the enter/leave events */
280
281     /* show windows from top to bottom */
282     for (it = stacking_list; it != NULL; it = it->next) {
283         Client *c = it->data;
284         if (!c->frame->visible && client_should_show(c))
285             frame_show(c->frame);
286     }
287
288     /* hide windows from bottom to top */
289     for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
290         Client *c = it->data;
291         if (c->frame->visible && !client_should_show(c))
292             frame_hide(c->frame);
293     }
294
295     /* focus the last focused window on the desktop, and ignore enter events
296        from the switch so it doesnt mess with the focus */
297     XSync(ob_display, FALSE);
298     while (XCheckTypedEvent(ob_display, EnterNotify, &e));
299     focus_fallback(Fallback_Desktop);
300
301     dispatch_ob(Event_Ob_Desktop, num, old);
302 }
303
304 void screen_update_layout()
305 {
306     guint32 *data = NULL;
307     guint num;
308
309     /* defaults */
310     screen_desktop_layout.orientation = prop_atoms.net_wm_orientation_horz;
311     screen_desktop_layout.start_corner = prop_atoms.net_wm_topleft;
312     screen_desktop_layout.rows = 1;
313     screen_desktop_layout.columns = screen_num_desktops;
314
315     if (PROP_GETA32(ob_root, net_desktop_layout, cardinal, &data, &num)) {
316         if (num == 3 || num == 4) {
317             if (data[0] == prop_atoms.net_wm_orientation_vert)
318                 screen_desktop_layout.orientation = data[0];
319             if (num == 3)
320                 screen_desktop_layout.start_corner =
321                     prop_atoms.net_wm_topright;
322             else {
323                 if (data[3] == prop_atoms.net_wm_topright)
324                     screen_desktop_layout.start_corner = data[3];
325                 else if (data[3] == prop_atoms.net_wm_bottomright)
326                     screen_desktop_layout.start_corner = data[3];
327                 else if (data[3] == prop_atoms.net_wm_bottomleft)
328                     screen_desktop_layout.start_corner = data[3];
329             }
330
331             /* fill in a zero rows/columns */
332             if (!(data[1] == 0 && data[2] == 0)) { /* both 0's is bad data.. */
333                 if (data[1] == 0) {
334                     data[1] = (screen_num_desktops +
335                                screen_num_desktops % data[2]) / data[2];
336                 } else if (data[2] == 0) {
337                     data[2] = (screen_num_desktops +
338                                screen_num_desktops % data[1]) / data[1];
339                 }
340                 screen_desktop_layout.columns = data[1];
341                 screen_desktop_layout.rows = data[2];
342             }
343
344             /* bounds checking */
345             if (screen_desktop_layout.orientation ==
346                 prop_atoms.net_wm_orientation_horz) {
347                 if (screen_desktop_layout.rows > screen_num_desktops)
348                     screen_desktop_layout.rows = screen_num_desktops;
349                 if (screen_desktop_layout.columns >
350                     ((screen_num_desktops + screen_num_desktops %
351                       screen_desktop_layout.rows) /
352                      screen_desktop_layout.rows))
353                     screen_desktop_layout.columns =
354                         (screen_num_desktops + screen_num_desktops %
355                          screen_desktop_layout.rows) /
356                         screen_desktop_layout.rows;
357             } else {
358                 if (screen_desktop_layout.columns > screen_num_desktops)
359                     screen_desktop_layout.columns = screen_num_desktops;
360                 if (screen_desktop_layout.rows >
361                     ((screen_num_desktops + screen_num_desktops %
362                       screen_desktop_layout.columns) /
363                      screen_desktop_layout.columns))
364                     screen_desktop_layout.rows =
365                         (screen_num_desktops + screen_num_desktops %
366                          screen_desktop_layout.columns) /
367                         screen_desktop_layout.columns;
368             }
369         }
370         g_free(data);
371     }
372 }
373
374 void screen_update_desktop_names()
375 {
376     guint i;
377
378     /* empty the array */
379     g_strfreev(screen_desktop_names);
380
381     if (PROP_GETSS(ob_root, net_desktop_names, utf8, &screen_desktop_names))
382         for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
383     else
384         i = 0;
385     if (i <= screen_num_desktops) {
386         screen_desktop_names = g_renew(char*, screen_desktop_names,
387                                        screen_num_desktops + 1);
388         screen_desktop_names[screen_num_desktops] = NULL;
389         for (; i < screen_num_desktops; ++i)
390             screen_desktop_names[i] = g_strdup("Unnamed Desktop");
391     }
392 }
393
394 void screen_show_desktop(gboolean show)
395 {
396     GList *it;
397      
398     if (show == screen_showing_desktop) return; /* no change */
399
400     screen_showing_desktop = show;
401
402     if (show) {
403         /* bottom to top */
404         for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
405             Client *client = it->data;
406             if (client->frame->visible && !client_should_show(client))
407                 frame_hide(client->frame);
408         }
409     } else {
410         /* top to bottom */
411         for (it = stacking_list; it != NULL; it = it->next) {
412             Client *client = it->data;
413             if (!client->frame->visible && client_should_show(client))
414                 frame_show(client->frame);
415         }
416     }
417
418     show = !!show; /* make it boolean */
419     PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
420
421     dispatch_ob(Event_Ob_ShowDesktop, show, 0);
422 }
423
424 void screen_install_colormap(Client *client, gboolean install)
425 {
426     XWindowAttributes wa;
427
428     if (client == NULL) {
429         if (install)
430             XInstallColormap(ob_display, render_colormap);
431         else
432             XUninstallColormap(ob_display, render_colormap);
433     } else {
434         if (XGetWindowAttributes(ob_display, client->window, &wa) &&
435             wa.colormap != None) {
436             if (install)
437                 XInstallColormap(ob_display, wa.colormap);
438             else
439                 XUninstallColormap(ob_display, wa.colormap);
440         }
441     }
442 }
443
444 void screen_update_struts()
445 {
446     GList *it;
447     guint i;
448      
449     g_free(strut);
450     strut = g_new0(Strut, screen_num_desktops + 1);
451
452     for (it = client_list; it != NULL; it = it->next) {
453         Client *c = it->data;
454         if (c->iconic) continue; /* these dont count in the strut */
455     
456         if (c->desktop == 0xffffffff) {
457             for (i = 0; i < screen_num_desktops; ++i)
458                 STRUT_ADD(strut[i], c->strut);
459         } else {
460             g_assert(c->desktop < screen_num_desktops);
461             STRUT_ADD(strut[c->desktop], c->strut);
462         }
463         /* apply to the 'all desktops' strut */
464         STRUT_ADD(strut[screen_num_desktops], c->strut);
465     }
466     screen_update_area();
467 }
468
469 static void screen_update_area()
470 {
471     guint i;
472     guint32 *dims;
473
474     g_free(area);
475     area = g_new0(Rect, screen_num_desktops + 1);
476      
477     dims = g_new(guint32, 4 * screen_num_desktops);
478     for (i = 0; i < screen_num_desktops + 1; ++i) {
479         Rect old_area = area[i];
480 /*
481   #ifdef    XINERAMA
482   // reset to the full areas
483   if (isXineramaActive())
484   xineramaUsableArea = getXineramaAreas();
485   #endif // XINERAMA
486 */
487
488         RECT_SET(area[i], strut[i].left, strut[i].top,
489                  screen_physical_size.width - (strut[i].left +
490                                                strut[i].right),
491                  screen_physical_size.height - (strut[i].top +
492                                                 strut[i].bottom));
493     
494 /*
495   #ifdef    XINERAMA
496   if (isXineramaActive()) {
497   // keep each of the ximerama-defined areas inside the strut
498   RectList::iterator xit, xend = xineramaUsableArea.end();
499   for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
500   if (xit->x() < usableArea.x()) {
501   xit->setX(usableArea.x());
502   xit->setWidth(xit->width() - usableArea.x());
503   }
504   if (xit->y() < usableArea.y()) {
505   xit->setY(usableArea.y());
506   xit->setHeight(xit->height() - usableArea.y());
507   }
508   if (xit->x() + xit->width() > usableArea.width())
509   xit->setWidth(usableArea.width() - xit->x());
510   if (xit->y() + xit->height() > usableArea.height())
511   xit->setHeight(usableArea.height() - xit->y());
512   }
513   }
514   #endif // XINERAMA
515 */
516         if (!RECT_EQUAL(old_area, area[i])) {
517             /* the area has changed, adjust all the maximized windows */
518             GList *it;
519             for (it = client_list; it; it = it->next) {
520                 Client *c = it->data;
521                 if (i < screen_num_desktops) {
522                     if (c->desktop == i)
523                         client_remaximize(c);
524                 } else {
525                     /* the 'all desktops' size */
526                     if (c->desktop == DESKTOP_ALL)
527                         client_remaximize(c);
528                 }
529             }
530         }
531
532         /* don't set these for the 'all desktops' area */
533         if (i < screen_num_desktops) {
534             dims[(i * 4) + 0] = area[i].x;
535             dims[(i * 4) + 1] = area[i].y;
536             dims[(i * 4) + 2] = area[i].width;
537             dims[(i * 4) + 3] = area[i].height;
538         }
539     }
540     PROP_SETA32(ob_root, net_workarea, cardinal,
541                 dims, 4 * screen_num_desktops);
542     g_free(dims);
543 }
544
545 Rect *screen_area(guint desktop)
546 {
547     if (desktop >= screen_num_desktops) {
548         if (desktop == DESKTOP_ALL)
549             return &area[screen_num_desktops];
550         return NULL;
551     }
552     return &area[desktop];
553 }
554
555 Strut *screen_strut(guint desktop)
556 {
557     if (desktop >= screen_num_desktops) {
558         if (desktop == DESKTOP_ALL)
559             return &strut[screen_num_desktops];
560         return NULL;
561     }
562     return &strut[desktop];
563 }