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