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