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