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