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