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