]> Dogcows Code - chaz/openbox/blob - c/screen.c
dont raise the window on a titlebar drag either
[chaz/openbox] / c / 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 guint old = screen_desktop;
242
243 g_assert(num < screen_num_desktops);
244
245 g_message("Moving to desktop %u", num);
246
247 screen_desktop = num;
248 PROP_SET32(ob_root, net_current_desktop, cardinal, num);
249
250 if (old == num) return;
251
252 /* XXX show/hide all the clients
253 std::list<Client*>::iterator it, end = clients.end();
254 for (it = clients.begin(); it != end; ++it)
255 (*it)->showhide(); */
256
257 /* XXX force the callbacks to fire
258 if (!openbox->focusedClient())
259 openbox->setFocusedClient(0); */
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 GSList *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 for (it = client_list; it; it = g_slist_next(it)) {
354 Client *client = it->data;
355 if (client->type == Type_Desktop) {
356 if (show) client_focus(client);
357 } else
358 client_showhide(client);
359 }
360
361 if (!show) {
362 Client *f = focus_client;
363 if (!f || f->type == Type_Desktop) {
364 Client *c = g_hash_table_lookup(client_map,
365 (gpointer)saved_focus);
366 if (c) client_focus(c);
367 }
368 }
369
370 show = show ? 1 : 0; /* make it boolean */
371 PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
372 }
373
374 void screen_install_colormap(Client *client, gboolean install)
375 {
376 if (client == NULL) {
377 /* XXX DONT USE THE DEFAULT SHIT HERE */
378 if (install)
379 XInstallColormap(ob_display,
380 DefaultColormap(ob_display, ob_screen));
381 else
382 XUninstallColormap(ob_display,
383 DefaultColormap(ob_display, ob_screen));
384 } else {
385 XWindowAttributes wa;
386 if (XGetWindowAttributes(ob_display, client->window, &wa)) {
387 if (install)
388 XInstallColormap(ob_display, wa.colormap);
389 else
390 XUninstallColormap(ob_display, wa.colormap);
391 }
392 }
393 }
394
395 void screen_update_struts()
396 {
397 GSList *it;
398 guint i;
399
400 if (strut != NULL)
401 g_free(strut);
402 strut = g_new0(Strut, screen_num_desktops + 1);
403
404 for (it = client_list; it; it = it->next) {
405 Client *c = it->data;
406 if (c->iconic) continue; /* these dont count in the strut */
407
408 if (c->desktop == 0xffffffff) {
409 for (i = 0; i < screen_num_desktops; ++i)
410 STRUT_ADD(strut[i], c->strut);
411 } else {
412 g_assert(c->desktop < screen_num_desktops);
413 STRUT_ADD(strut[c->desktop], c->strut);
414 }
415 /* apply to the 'all desktops' strut */
416 STRUT_ADD(strut[screen_num_desktops], c->strut);
417 }
418 screen_update_area();
419 }
420
421 static void screen_update_area()
422 {
423 guint i;
424 gulong *dims;
425
426 if (area != NULL)
427 g_free(area);
428 area = g_new0(Rect, screen_num_desktops + 1);
429
430 dims = g_new(unsigned long, 4 * screen_num_desktops);
431 for (i = 0; i < screen_num_desktops + 1; ++i) {
432 Rect old_area = area[i];
433 /*
434 #ifdef XINERAMA
435 // reset to the full areas
436 if (isXineramaActive())
437 xineramaUsableArea = getXineramaAreas();
438 #endif // XINERAMA
439 */
440
441 RECT_SET(area[i], strut[i].left, strut[i].top,
442 screen_physical_size.width - (strut[i].left +
443 strut[i].right),
444 screen_physical_size.height - (strut[i].top +
445 strut[i].bottom));
446
447 /*
448 #ifdef XINERAMA
449 if (isXineramaActive()) {
450 // keep each of the ximerama-defined areas inside the strut
451 RectList::iterator xit, xend = xineramaUsableArea.end();
452 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
453 if (xit->x() < usableArea.x()) {
454 xit->setX(usableArea.x());
455 xit->setWidth(xit->width() - usableArea.x());
456 }
457 if (xit->y() < usableArea.y()) {
458 xit->setY(usableArea.y());
459 xit->setHeight(xit->height() - usableArea.y());
460 }
461 if (xit->x() + xit->width() > usableArea.width())
462 xit->setWidth(usableArea.width() - xit->x());
463 if (xit->y() + xit->height() > usableArea.height())
464 xit->setHeight(usableArea.height() - xit->y());
465 }
466 }
467 #endif // XINERAMA
468 */
469 if (!RECT_EQUAL(old_area, area[i])) {
470 /* the area has changed, adjust all the maximized windows */
471 GSList *it;
472 for (it = client_list; it; it = it->next) {
473 Client *c = it->data;
474 if (i < screen_num_desktops) {
475 if (c->desktop == i)
476 client_remaximize(c);
477 } else {
478 /* the 'all desktops' size */
479 if (c->desktop == DESKTOP_ALL)
480 client_remaximize(c);
481 }
482 }
483 }
484
485 /* don't set these for the 'all desktops' area */
486 if (i < screen_num_desktops) {
487 dims[(i * 4) + 0] = area[i].x;
488 dims[(i * 4) + 1] = area[i].y;
489 dims[(i * 4) + 2] = area[i].width;
490 dims[(i * 4) + 3] = area[i].height;
491 }
492 }
493 PROP_SET32A(ob_root, net_workarea, cardinal,
494 dims, 4 * screen_num_desktops);
495 g_free(dims);
496 }
497
498 Rect *screen_area(guint desktop)
499 {
500 if (desktop >= screen_num_desktops) {
501 if (desktop == DESKTOP_ALL)
502 return &area[screen_num_desktops];
503 return NULL;
504 }
505 return &area[desktop];
506 }
507
508 Strut *screen_strut(guint desktop)
509 {
510 if (desktop >= screen_num_desktops) {
511 if (desktop == DESKTOP_ALL)
512 return &strut[screen_num_desktops];
513 return NULL;
514 }
515 return &strut[desktop];
516 }
This page took 0.05616 seconds and 4 git commands to generate.