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