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