]> git.lizzy.rs Git - bspwm.git/blob - window.c
Merge translate_position and translate_client
[bspwm.git] / window.c
1 /* * Copyright (c) 2012-2013 Bastien Dejean
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  *  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *  * Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation and/or
11  * other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include "bspwm.h"
28 #include "ewmh.h"
29 #include "monitor.h"
30 #include "query.h"
31 #include "rule.h"
32 #include "settings.h"
33 #include "stack.h"
34 #include "tree.h"
35 #include "window.h"
36
37 void manage_window(monitor_t *m, desktop_t *d, xcb_window_t win)
38 {
39     coordinates_t loc;
40     xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
41     uint8_t override_redirect = 0;
42
43     if (wa != NULL) {
44         override_redirect = wa->override_redirect;
45         free(wa);
46     }
47
48     if (override_redirect || locate_window(win, &loc))
49         return;
50
51     bool floating = false, fullscreen = false, locked = false, sticky = false, follow = false, transient = false, takes_focus = true, frame = false, private = false, center = false, manage = true;
52     handle_rules(win, &m, &d, &floating, &fullscreen, &locked, &sticky, &follow, &transient, &takes_focus, &frame, &private, &center, &manage);
53
54     if (!manage) {
55         disable_floating_atom(win);
56         window_show(win);
57         return;
58     }
59
60     PRINTF("manage %X\n", win);
61
62     client_t *c = make_client(win);
63     update_floating_rectangle(c);
64     translate_client(monitor_from_client(c), m, c);
65     if (center)
66         window_center(m, c);
67     c->frame = frame;
68
69     xcb_icccm_get_wm_class_reply_t reply;
70     if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
71         snprintf(c->class_name, sizeof(c->class_name), "%s", reply.class_name);
72         xcb_icccm_get_wm_class_reply_wipe(&reply);
73     }
74
75     if (c->transient)
76         floating = true;
77
78     node_t *n = make_node();
79     n->client = c;
80
81     insert_node(m, d, n, d->focus);
82
83     disable_floating_atom(c->window);
84     set_floating(n, floating);
85     set_locked(m, d, n, locked);
86     set_sticky(m, d, n, sticky);
87     set_private(m, d, n, private);
88
89     if (d->focus != NULL && d->focus->client->fullscreen)
90         set_fullscreen(d->focus, false);
91
92     set_fullscreen(n, fullscreen);
93     c->transient = transient;
94
95     arrange(m, d);
96
97     bool give_focus = (takes_focus && (d == mon->desk || follow));
98
99     if (give_focus)
100         focus_node(m, d, n);
101     else if (takes_focus)
102         pseudo_focus(d, n);
103     else
104         stack(n, STACK_ABOVE);
105
106     uint32_t values[] = {get_event_mask(n->client)};
107     xcb_change_window_attributes(dpy, c->window, XCB_CW_EVENT_MASK, values);
108
109     if (visible) {
110         if (d == m->desk)
111             window_show(n->client->window);
112         else
113             window_hide(n->client->window);
114     }
115
116     /* the same function is already called in `focus_node` but has no effects on unmapped windows */
117     if (give_focus)
118         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, win, XCB_CURRENT_TIME);
119
120     num_clients++;
121     ewmh_set_wm_desktop(n, d);
122     ewmh_update_client_list();
123 }
124
125 void window_draw_border(node_t *n, bool focused_window, bool focused_monitor)
126 {
127     if (n == NULL || (n->client->border_width < 1 && !n->client->frame)) {
128         return;
129     }
130
131     if (n->client->frame) {
132         draw_frame_background(n, focused_window, focused_monitor);
133         return;
134     }
135
136     xcb_window_t win = n->client->window;
137     uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
138
139     if (n->split_mode == MODE_AUTOMATIC) {
140         xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
141     } else {
142         unsigned int border_width = n->client->border_width;
143         uint32_t presel_border_color_pxl;
144         get_color(presel_border_color, win, &presel_border_color_pxl);
145
146         xcb_rectangle_t actual_rectangle = get_rectangle(n->client);
147
148         uint16_t width = actual_rectangle.width;
149         uint16_t height = actual_rectangle.height;
150
151         uint16_t full_width = width + 2 * border_width;
152         uint16_t full_height = height + 2 * border_width;
153
154         xcb_rectangle_t border_rectangles[] =
155         {
156             { width, 0, 2 * border_width, height + 2 * border_width },
157             { 0, height, width + 2 * border_width, 2 * border_width }
158         };
159
160         xcb_rectangle_t *presel_rectangles;
161
162         uint8_t win_depth = root_depth;
163         xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
164         if (geo != NULL)
165             win_depth = geo->depth;
166         free(geo);
167
168         xcb_pixmap_t pixmap = xcb_generate_id(dpy);
169         xcb_create_pixmap(dpy, win_depth, pixmap, win, full_width, full_height);
170
171         xcb_gcontext_t gc = xcb_generate_id(dpy);
172         xcb_create_gc(dpy, gc, pixmap, 0, NULL);
173
174         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
175         xcb_poly_fill_rectangle(dpy, pixmap, gc, LENGTH(border_rectangles), border_rectangles);
176
177         uint16_t fence = (int16_t) (n->split_ratio * ((n->split_dir == DIR_UP || n->split_dir == DIR_DOWN) ? height : width));
178         presel_rectangles = malloc(2 * sizeof(xcb_rectangle_t));
179         switch (n->split_dir) {
180             case DIR_UP:
181                 presel_rectangles[0] = (xcb_rectangle_t) {width, 0, 2 * border_width, fence};
182                 presel_rectangles[1] = (xcb_rectangle_t) {0, height + border_width, full_width, border_width};
183                 break;
184             case DIR_DOWN:
185                 presel_rectangles[0] = (xcb_rectangle_t) {width, fence + 1, 2 * border_width, height + border_width - (fence + 1)};
186                 presel_rectangles[1] = (xcb_rectangle_t) {0, height, full_width, border_width};
187                 break;
188             case DIR_LEFT:
189                 presel_rectangles[0] = (xcb_rectangle_t) {0, height, fence, 2 * border_width};
190                 presel_rectangles[1] = (xcb_rectangle_t) {width + border_width, 0, border_width, full_height};
191                 break;
192             case DIR_RIGHT:
193                 presel_rectangles[0] = (xcb_rectangle_t) {fence + 1, height, width + border_width - (fence + 1), 2 * border_width};
194                 presel_rectangles[1] = (xcb_rectangle_t) {width, 0, border_width, full_height};
195                 break;
196         }
197         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
198         xcb_poly_fill_rectangle(dpy, pixmap, gc, 2, presel_rectangles);
199         xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXMAP, &pixmap);
200         free(presel_rectangles);
201         xcb_free_gc(dpy, gc);
202         xcb_free_pixmap(dpy, pixmap);
203     }
204 }
205
206 void draw_frame_background(node_t *n, bool focused_window, bool focused_monitor)
207 {
208     if (n == NULL)
209         return;
210
211     xcb_window_t win = n->client->window;
212     uint32_t border_color_pxl = get_border_color(n->client, focused_window, focused_monitor);
213     uint32_t opacity = (focused_window ? (focused_monitor ? focused_frame_opacity : active_frame_opacity) : normal_frame_opacity) * 0xffffffff;
214     xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, _NET_WM_WINDOW_OPACITY, XCB_ATOM_CARDINAL, 32, 1, &opacity);
215     uint8_t win_depth = root_depth;
216     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
217     if (geo != NULL)
218         win_depth = geo->depth;
219     free(geo);
220     xcb_rectangle_t rectangle = get_rectangle(n->client);
221     rectangle.x = rectangle.y = 0;
222     uint16_t width = rectangle.width;
223     uint16_t height = rectangle.height;
224     xcb_pixmap_t pixmap = xcb_generate_id(dpy);
225     xcb_create_pixmap(dpy, win_depth, pixmap, win, width, height);
226     xcb_gcontext_t gc = xcb_generate_id(dpy);
227     xcb_create_gc(dpy, gc, pixmap, 0, NULL);
228     xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &border_color_pxl);
229     xcb_poly_fill_rectangle(dpy, pixmap, gc, 1, &rectangle);
230     if (n->split_mode == MODE_MANUAL) {
231         xcb_rectangle_t presel_rectangle = rectangle;
232         uint32_t presel_border_color_pxl;
233         get_color(presel_border_color, win, &presel_border_color_pxl);
234         xcb_change_gc(dpy, gc, XCB_GC_FOREGROUND, &presel_border_color_pxl);
235         switch (n->split_dir) {
236             case DIR_UP:
237                 presel_rectangle.height = n->split_ratio * rectangle.height;
238                 break;
239             case DIR_RIGHT:
240                 presel_rectangle.width = (1 - n->split_ratio) * rectangle.width;
241                 presel_rectangle.x = rectangle.width - presel_rectangle.width;
242                 break;
243             case DIR_DOWN:
244                 presel_rectangle.height = (1 - n->split_ratio) * rectangle.height;
245                 presel_rectangle.y = rectangle.height - presel_rectangle.height;
246                 break;
247             case DIR_LEFT:
248                 presel_rectangle.width = n->split_ratio * rectangle.width;
249                 break;
250         }
251         xcb_poly_fill_rectangle(dpy, pixmap, gc, 1, &presel_rectangle);
252     }
253     xcb_copy_area(dpy, pixmap, win, gc, 0, 0, 0, 0, width, height);
254     xcb_free_gc(dpy, gc);
255     xcb_free_pixmap(dpy, pixmap);
256 }
257
258 pointer_state_t *make_pointer_state(void)
259 {
260     pointer_state_t *p = malloc(sizeof(pointer_state_t));
261     p->monitor = NULL;
262     p->desktop = NULL;
263     p->node = p->vertical_fence = p->horizontal_fence = NULL;
264     p->client = NULL;
265     p->window = XCB_NONE;
266     p->action = ACTION_NONE;
267     return p;
268 }
269
270 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
271 {
272     return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width)
273             && a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
274 }
275
276 xcb_rectangle_t get_rectangle(client_t *c)
277 {
278     if (is_tiled(c))
279         return c->tiled_rectangle;
280     else
281         return c->floating_rectangle;
282 }
283
284 void get_side_handle(client_t *c, direction_t dir, xcb_point_t *pt)
285 {
286     xcb_rectangle_t rect = get_rectangle(c);
287     switch (dir) {
288         case DIR_RIGHT:
289             pt->x = rect.x + rect.width;
290             pt->y = rect.y + (rect.height / 2);
291             break;
292         case DIR_DOWN:
293             pt->x = rect.x + (rect.width / 2);
294             pt->y = rect.y + rect.height;
295             break;
296         case DIR_LEFT:
297             pt->x = rect.x;
298             pt->y = rect.y + (rect.height / 2);
299             break;
300         case DIR_UP:
301             pt->x = rect.x + (rect.width / 2);
302             pt->y = rect.y;
303             break;
304     }
305 }
306
307 void adopt_orphans(void)
308 {
309     xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
310     if (qtr == NULL)
311         return;
312
313     PUTS("adopt orphans");
314
315     int len = xcb_query_tree_children_length(qtr);
316     xcb_window_t *wins = xcb_query_tree_children(qtr);
317     for (int i = 0; i < len; i++) {
318         uint32_t idx;
319         xcb_window_t win = wins[i];
320         if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
321             coordinates_t loc;
322             if (ewmh_locate_desktop(idx, &loc))
323                 manage_window(loc.monitor, loc.desktop, win);
324             else
325                 manage_window(mon, mon->desk, win);
326         }
327     }
328     free(qtr);
329 }
330
331 void window_close(node_t *n)
332 {
333     if (n == NULL || n->client->locked)
334         return;
335
336     PRINTF("close window %X\n", n->client->window);
337
338     send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
339 }
340
341 void window_kill(monitor_t *m, desktop_t *d, node_t *n)
342 {
343     if (n == NULL)
344         return;
345
346     xcb_window_t win = n->client->window;
347     PRINTF("kill window %X\n", win);
348
349     xcb_kill_client(dpy, win);
350     remove_node(m, d, n);
351 }
352
353 void set_fullscreen(node_t *n, bool value)
354 {
355     if (n == NULL || n->client->frame || n->client->fullscreen == value)
356         return;
357
358     client_t *c = n->client;
359
360     PRINTF("fullscreen %X: %s\n", c->window, BOOLSTR(value));
361
362     c->fullscreen = value;
363     if (value)
364         ewmh_wm_state_add(c, ewmh->_NET_WM_STATE_FULLSCREEN);
365     else
366         ewmh_wm_state_remove(c, ewmh->_NET_WM_STATE_FULLSCREEN);
367     stack(n, STACK_ABOVE);
368 }
369
370 void set_floating(node_t *n, bool value)
371 {
372     if (n == NULL || n->client->transient || n->client->fullscreen || n->client->frame || n->client->floating == value)
373         return;
374
375     PRINTF("floating %X: %s\n", n->client->window, BOOLSTR(value));
376
377     n->split_mode = MODE_AUTOMATIC;
378     client_t *c = n->client;
379     c->floating = n->vacant = value;
380     update_vacant_state(n->parent);
381
382     if (value) {
383         enable_floating_atom(c->window);
384         unrotate_brother(n);
385     } else {
386         disable_floating_atom(c->window);
387         rotate_brother(n);
388     }
389
390     stack(n, STACK_ABOVE);
391 }
392
393 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
394 {
395     if (n == NULL || n->client->locked == value)
396         return;
397
398     client_t *c = n->client;
399
400     PRINTF("set locked %X: %s\n", c->window, BOOLSTR(value));
401
402     c->locked = value;
403     window_draw_border(n, d->focus == n, m == mon);
404 }
405
406 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
407 {
408     if (n == NULL || n->client->sticky == value)
409         return;
410
411     client_t *c = n->client;
412
413     PRINTF("set sticky %X: %s\n", c->window, BOOLSTR(value));
414
415     if (d != m->desk)
416         transfer_node(m, d, n, m, m->desk, m->desk->focus);
417
418     c->sticky = value;
419     if (value) {
420         ewmh_wm_state_add(c, ewmh->_NET_WM_STATE_STICKY);
421         m->num_sticky++;
422     } else {
423         ewmh_wm_state_remove(c, ewmh->_NET_WM_STATE_STICKY);
424         m->num_sticky--;
425     }
426
427     window_draw_border(n, d->focus == n, m == mon);
428 }
429
430 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
431 {
432     if (n == NULL || n->client->private == value)
433         return;
434
435     client_t *c = n->client;
436
437     PRINTF("set private %X: %s\n", c->window, BOOLSTR(value));
438
439     c->private = value;
440     update_privacy_level(n, value);
441     window_draw_border(n, d->focus == n, m == mon);
442 }
443
444 void set_urgency(monitor_t *m, desktop_t *d, node_t *n, bool value)
445 {
446     if (value && mon->desk->focus == n)
447         return;
448     n->client->urgent = value;
449     window_draw_border(n, d->focus == n, m == mon);
450     put_status();
451 }
452
453 void set_floating_atom(xcb_window_t win, uint32_t value)
454 {
455     if (!apply_floating_atom)
456         return;
457     set_atom(win, _BSPWM_FLOATING_WINDOW, value);
458 }
459
460 void enable_floating_atom(xcb_window_t win)
461 {
462     set_floating_atom(win, 1);
463 }
464
465 void disable_floating_atom(xcb_window_t win)
466 {
467     set_floating_atom(win, 0);
468 }
469
470 uint32_t get_border_color(client_t *c, bool focused_window, bool focused_monitor)
471 {
472     if (c == NULL)
473         return 0;
474
475     uint32_t pxl = 0;
476
477     if (focused_monitor && focused_window) {
478         if (c->locked)
479             get_color(focused_locked_border_color, c->window, &pxl);
480         else if (c->sticky)
481             get_color(focused_sticky_border_color, c->window, &pxl);
482         else if (c->private)
483             get_color(focused_private_border_color, c->window, &pxl);
484         else
485             get_color(focused_border_color, c->window, &pxl);
486     } else if (focused_window) {
487         if (c->urgent)
488             get_color(urgent_border_color, c->window, &pxl);
489         else if (c->locked)
490             get_color(active_locked_border_color, c->window, &pxl);
491         else if (c->sticky)
492             get_color(active_sticky_border_color, c->window, &pxl);
493         else if (c->private)
494             get_color(active_private_border_color, c->window, &pxl);
495         else
496             get_color(active_border_color, c->window, &pxl);
497     } else {
498         if (c->urgent)
499             get_color(urgent_border_color, c->window, &pxl);
500         else if (c->locked)
501             get_color(normal_locked_border_color, c->window, &pxl);
502         else if (c->sticky)
503             get_color(normal_sticky_border_color, c->window, &pxl);
504         else if (c->private)
505             get_color(normal_private_border_color, c->window, &pxl);
506         else
507             get_color(normal_border_color, c->window, &pxl);
508     }
509
510     return pxl;
511 }
512
513 void update_floating_rectangle(client_t *c)
514 {
515     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, c->window), NULL);
516
517     if (geo != NULL)
518         c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
519     else
520         c->floating_rectangle = (xcb_rectangle_t) {0, 0, 32, 24};
521
522     free(geo);
523 }
524
525
526 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
527 {
528     window_lower(motion_recorder);
529
530     xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
531
532     if (qpr != NULL) {
533         if (win != NULL)
534             *win = qpr->child;
535         if (pt != NULL)
536             *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
537         free(qpr);
538     }
539
540     window_raise(motion_recorder);
541 }
542
543 bool window_focus(xcb_window_t win)
544 {
545     coordinates_t loc;
546     if (locate_window(win, &loc)) {
547         if (loc.node != mon->desk->focus)
548             focus_node(loc.monitor, loc.desktop, loc.node);
549         return true;
550     }
551     return false;
552 }
553
554 void window_border_width(xcb_window_t win, uint32_t bw)
555 {
556     uint32_t values[] = {bw};
557     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
558 }
559
560 void window_move(xcb_window_t win, int16_t x, int16_t y)
561 {
562     uint32_t values[] = {x, y};
563     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
564 }
565
566 void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
567 {
568     uint32_t values[] = {w, h};
569     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
570 }
571
572 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
573 {
574     uint32_t values[] = {x, y, w, h};
575     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
576 }
577
578 void window_raise(xcb_window_t win)
579 {
580     uint32_t values[] = {XCB_STACK_MODE_ABOVE};
581     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
582 }
583
584 void window_center(monitor_t *m, client_t *c)
585 {
586     xcb_rectangle_t *r = &c->floating_rectangle;
587     xcb_rectangle_t a = m->rectangle;
588     if (r->width >= a.width)
589         r->x = a.x;
590     else
591         r->x = a.x + (a.width - r->width) / 2;
592     if (r->height >= a.height)
593         r->y = a.y;
594     else
595         r->y = a.y + (a.height - r->height) / 2;
596 }
597
598 void window_stack(xcb_window_t w1, xcb_window_t w2, uint32_t mode)
599 {
600     if (w2 == XCB_NONE)
601         return;
602     uint16_t mask = XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE;
603     uint32_t values[] = {w2, mode};
604     xcb_configure_window(dpy, w1, mask, values);
605 }
606
607 void window_above(xcb_window_t w1, xcb_window_t w2)
608 {
609     window_stack(w1, w2, XCB_STACK_MODE_ABOVE);
610 }
611
612 void window_below(xcb_window_t w1, xcb_window_t w2)
613 {
614     window_stack(w1, w2, XCB_STACK_MODE_BELOW);
615 }
616
617 void window_lower(xcb_window_t win)
618 {
619     uint32_t values[] = {XCB_STACK_MODE_BELOW};
620     xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
621 }
622
623 void window_set_visibility(xcb_window_t win, bool visible)
624 {
625     uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
626     uint32_t values_on[] = {ROOT_EVENT_MASK};
627     xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
628     if (visible)
629         xcb_map_window(dpy, win);
630     else
631         xcb_unmap_window(dpy, win);
632     xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
633 }
634
635 void window_hide(xcb_window_t win)
636 {
637     PRINTF("window hide %X\n", win);
638     window_set_visibility(win, false);
639 }
640
641 void window_show(xcb_window_t win)
642 {
643     PRINTF("window show %X\n", win);
644     window_set_visibility(win, true);
645 }
646
647 void toggle_visibility(void)
648 {
649     visible = !visible;
650     if (!visible)
651         clear_input_focus();
652     for (monitor_t *m = mon_head; m != NULL; m = m->next)
653         for (node_t *n = first_extrema(m->desk->root); n != NULL; n = next_leaf(n, m->desk->root))
654             window_set_visibility(n->client->window, visible);
655     if (visible)
656         update_input_focus();
657 }
658
659 void enable_motion_recorder(void)
660 {
661     PUTS("enable motion recorder");
662     window_raise(motion_recorder);
663     window_show(motion_recorder);
664 }
665
666 void disable_motion_recorder(void)
667 {
668     PUTS("disable motion recorder");
669     window_hide(motion_recorder);
670 }
671
672 void update_motion_recorder(void)
673 {
674     xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, root), NULL);
675
676     if (geo != NULL) {
677         window_resize(motion_recorder, geo->width, geo->height);
678         PRINTF("update motion recorder size: %ux%u\n", geo->width, geo->height);
679     }
680
681     free(geo);
682 }
683
684 void update_input_focus(void)
685 {
686     set_input_focus(mon->desk->focus);
687 }
688
689 void set_input_focus(node_t *n)
690 {
691     if (n == NULL) {
692         clear_input_focus();
693     } else {
694         if (n->client->icccm_focus)
695             send_client_message(n->client->window, ewmh->WM_PROTOCOLS, WM_TAKE_FOCUS);
696         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
697     }
698 }
699
700 void clear_input_focus(void)
701 {
702     xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
703 }
704
705 void center_pointer(monitor_t *m)
706 {
707     int16_t cx = m->rectangle.x + m->rectangle.width / 2;
708     int16_t cy = m->rectangle.y + m->rectangle.height / 2;
709     window_lower(motion_recorder);
710     xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
711     window_raise(motion_recorder);
712 }
713
714 void get_atom(char *name, xcb_atom_t *atom)
715 {
716     xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen(name), name), NULL);
717     if (reply != NULL)
718         *atom = reply->atom;
719     else
720         *atom = XCB_NONE;
721     free(reply);
722 }
723
724 void set_atom(xcb_window_t win, xcb_atom_t atom, uint32_t value)
725 {
726     xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, atom, XCB_ATOM_CARDINAL, 32, 1, &value);
727 }
728
729 bool has_proto(xcb_atom_t atom, xcb_icccm_get_wm_protocols_reply_t *protocols)
730 {
731     for (uint32_t i = 0; i < protocols->atoms_len; i++)
732         if (protocols->atoms[i] == atom)
733             return true;
734     return false;
735 }
736
737 void send_client_message(xcb_window_t win, xcb_atom_t property, xcb_atom_t value)
738 {
739     xcb_client_message_event_t e;
740
741     e.response_type = XCB_CLIENT_MESSAGE;
742     e.window = win;
743     e.format = 32;
744     e.sequence = 0;
745     e.type = property;
746     e.data.data32[0] = value;
747     e.data.data32[1] = XCB_CURRENT_TIME;
748
749     xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) &e);
750 }
751
752 uint32_t get_event_mask(client_t *c)
753 {
754     return CLIENT_EVENT_MASK | (c->frame ? XCB_EVENT_MASK_EXPOSURE : 0) | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0);
755 }