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