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