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