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