]> git.lizzy.rs Git - bspwm.git/blob - src/window.c
cd2340dfd19bf6024f37abd1e1678b8a20cfe1ea
[bspwm.git] / src / 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 <stdio.h>
26 #include <stdlib.h>
27 #include <stdbool.h>
28 #include <string.h>
29 #include <xcb/shape.h>
30 #include "bspwm.h"
31 #include "ewmh.h"
32 #include "monitor.h"
33 #include "desktop.h"
34 #include "query.h"
35 #include "rule.h"
36 #include "settings.h"
37 #include "geometry.h"
38 #include "pointer.h"
39 #include "stack.h"
40 #include "tree.h"
41 #include "parse.h"
42 #include "window.h"
43
44 void schedule_window(xcb_window_t win)
45 {
46         coordinates_t loc;
47         uint8_t override_redirect = 0;
48         xcb_get_window_attributes_reply_t *wa = xcb_get_window_attributes_reply(dpy, xcb_get_window_attributes(dpy, win), NULL);
49
50         if (wa != NULL) {
51                 override_redirect = wa->override_redirect;
52                 free(wa);
53         }
54
55         if (override_redirect || locate_window(win, &loc)) {
56                 return;
57         }
58
59         /* ignore pending windows */
60         for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
61                 if (pr->win == win) {
62                         return;
63                 }
64         }
65
66         rule_consequence_t *csq = make_rule_consequence();
67         apply_rules(win, csq);
68         if (!schedule_rules(win, csq)) {
69                 manage_window(win, csq, -1);
70                 free(csq);
71         }
72 }
73
74 bool manage_window(xcb_window_t win, rule_consequence_t *csq, int fd)
75 {
76         monitor_t *m = mon;
77         desktop_t *d = mon->desk;
78         node_t *f = mon->desk->focus;
79
80         parse_rule_consequence(fd, csq);
81
82         if (!ignore_ewmh_struts && ewmh_handle_struts(win)) {
83                 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
84                         for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
85                                 arrange(m, d);
86                         }
87                 }
88         }
89
90         if (!csq->manage) {
91                 free(csq->layer);
92                 free(csq->state);
93                 window_show(win);
94                 return false;
95         }
96
97         if (csq->node_desc[0] != '\0') {
98                 coordinates_t ref = {m, d, f};
99                 coordinates_t trg = {NULL, NULL, NULL};
100                 if (node_from_desc(csq->node_desc, &ref, &trg) == SELECTOR_OK) {
101                         m = trg.monitor;
102                         d = trg.desktop;
103                         f = trg.node;
104                 }
105         } else if (csq->desktop_desc[0] != '\0') {
106                 coordinates_t ref = {m, d, NULL};
107                 coordinates_t trg = {NULL, NULL, NULL};
108                 if (desktop_from_desc(csq->desktop_desc, &ref, &trg) == SELECTOR_OK) {
109                         m = trg.monitor;
110                         d = trg.desktop;
111                         f = trg.desktop->focus;
112                 }
113         } else if (csq->monitor_desc[0] != '\0') {
114                 coordinates_t ref = {m, NULL, NULL};
115                 coordinates_t trg = {NULL, NULL, NULL};
116                 if (monitor_from_desc(csq->monitor_desc, &ref, &trg) == SELECTOR_OK) {
117                         m = trg.monitor;
118                         d = trg.monitor->desk;
119                         f = trg.monitor->desk->focus;
120                 }
121         }
122
123         if (csq->sticky) {
124                 m = mon;
125                 d = mon->desk;
126                 f = mon->desk->focus;
127         }
128
129         if (csq->split_dir != NULL && f != NULL) {
130                 presel_dir(m, d, f, *csq->split_dir);
131         }
132
133         if (csq->split_ratio != 0 && f != NULL) {
134                 presel_ratio(m, d, f, csq->split_ratio);
135         }
136
137         node_t *n = make_node(win);
138         client_t *c = make_client();
139         c->border_width = csq->border ? d->border_width : 0;
140         n->client = c;
141         initialize_client(n);
142         initialize_floating_rectangle(n);
143
144         if (csq->rect != NULL) {
145                 c->floating_rectangle = *csq->rect;
146                 free(csq->rect);
147         } else if (c->floating_rectangle.x == 0 && c->floating_rectangle.y == 0) {
148                 csq->center = true;
149         }
150
151         monitor_t *mm = monitor_from_client(c);
152         embrace_client(mm, c);
153         adapt_geometry(&mm->rectangle, &m->rectangle, n);
154
155         if (csq->center) {
156                 window_center(m, c);
157         }
158
159         snprintf(c->class_name, sizeof(c->class_name), "%s", csq->class_name);
160         snprintf(c->instance_name, sizeof(c->instance_name), "%s", csq->instance_name);
161
162         if ((csq->state != NULL && (*(csq->state) == STATE_FLOATING || *(csq->state) == STATE_FULLSCREEN)) || csq->hidden) {
163                 n->vacant = true;
164         }
165
166         f = insert_node(m, d, n, f);
167         clients_count++;
168         if (single_monocle && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
169                 set_layout(m, d, d->user_layout, false);
170         }
171
172         n->vacant = false;
173
174         put_status(SBSC_MASK_NODE_ADD, "node_add 0x%08X 0x%08X 0x%08X 0x%08X\n", m->id, d->id, f!=NULL?f->id:0, win);
175
176         if (f != NULL && f->client != NULL && csq->state != NULL && *(csq->state) == STATE_FLOATING) {
177                 c->layer = f->client->layer;
178         }
179
180         if (csq->layer != NULL) {
181                 c->layer = *(csq->layer);
182         }
183
184         if (csq->state != NULL) {
185                 set_state(m, d, n, *(csq->state));
186         }
187
188         set_hidden(m, d, n, csq->hidden);
189         set_sticky(m, d, n, csq->sticky);
190         set_private(m, d, n, csq->private);
191         set_locked(m, d, n, csq->locked);
192         set_marked(m, d, n, csq->marked);
193
194         arrange(m, d);
195
196         uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)};
197         xcb_change_window_attributes(dpy, win, XCB_CW_EVENT_MASK, values);
198         set_window_state(win, XCB_ICCCM_WM_STATE_NORMAL);
199         window_grab_buttons(win);
200
201         if (d == m->desk) {
202                 show_node(d, n);
203         } else {
204                 hide_node(d, n);
205         }
206
207         ewmh_update_client_list(false);
208         ewmh_set_wm_desktop(n, d);
209
210         if (!csq->hidden && csq->focus) {
211                 if (d == mon->desk || csq->follow) {
212                         focus_node(m, d, n);
213                 } else {
214                         activate_node(m, d, n);
215                 }
216         } else {
217                 stack(d, n, false);
218                 draw_border(n, false, (m == mon));
219         }
220
221         free(csq->layer);
222         free(csq->state);
223
224         return true;
225 }
226
227 void set_window_state(xcb_window_t win, xcb_icccm_wm_state_t state)
228 {
229         long data[] = {state, XCB_NONE};
230         xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, WM_STATE, WM_STATE, 32, 2, data);
231 }
232
233 void unmanage_window(xcb_window_t win)
234 {
235         coordinates_t loc;
236         if (locate_window(win, &loc)) {
237                 put_status(SBSC_MASK_NODE_REMOVE, "node_remove 0x%08X 0x%08X 0x%08X\n", loc.monitor->id, loc.desktop->id, win);
238                 remove_node(loc.monitor, loc.desktop, loc.node);
239                 arrange(loc.monitor, loc.desktop);
240         } else {
241                 for (pending_rule_t *pr = pending_rule_head; pr != NULL; pr = pr->next) {
242                         if (pr->win == win) {
243                                 remove_pending_rule(pr);
244                                 return;
245                         }
246                 }
247         }
248 }
249
250 bool is_presel_window(xcb_window_t win)
251 {
252         xcb_icccm_get_wm_class_reply_t reply;
253         bool ret = false;
254         if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
255                 if (streq(BSPWM_CLASS_NAME, reply.class_name) && streq(PRESEL_FEEDBACK_I, reply.instance_name)) {
256                         ret = true;
257                 }
258                 xcb_icccm_get_wm_class_reply_wipe(&reply);
259         }
260         return ret;
261 }
262
263 void initialize_presel_feedback(node_t *n)
264 {
265         if (n == NULL || n->presel == NULL || n->presel->feedback != XCB_NONE) {
266                 return;
267         }
268
269         xcb_window_t win = xcb_generate_id(dpy);
270         uint32_t mask = XCB_CW_BACK_PIXEL | XCB_CW_SAVE_UNDER;
271         uint32_t values[] = {get_color_pixel(presel_feedback_color), 1};
272         xcb_create_window(dpy, XCB_COPY_FROM_PARENT, win, root, 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
273                                   XCB_COPY_FROM_PARENT, mask, values);
274
275         xcb_icccm_set_wm_class(dpy, win, sizeof(PRESEL_FEEDBACK_IC), PRESEL_FEEDBACK_IC);
276         /* Make presel window's input shape NULL to pass any input to window below */
277         xcb_shape_rectangles(dpy, XCB_SHAPE_SO_SET, XCB_SHAPE_SK_INPUT, XCB_CLIP_ORDERING_UNSORTED, win, 0, 0, 0, NULL);
278         stacking_list_t *s = stack_tail;
279         while (s != NULL && !IS_TILED(s->node->client)) {
280                 s = s->prev;
281         }
282         if (s != NULL) {
283                 window_above(win, s->node->id);
284         }
285         n->presel->feedback = win;
286 }
287
288 void draw_presel_feedback(monitor_t *m, desktop_t *d, node_t *n)
289 {
290         if (n == NULL || n->presel == NULL || d->user_layout == LAYOUT_MONOCLE || !presel_feedback) {
291                 return;
292         }
293
294         bool exists = (n->presel->feedback != XCB_NONE);
295         if (!exists) {
296                 initialize_presel_feedback(n);
297         }
298
299         int gap = gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap;
300         presel_t *p = n->presel;
301         xcb_rectangle_t rect = n->rectangle;
302         rect.x = rect.y = 0;
303         rect.width -= gap;
304         rect.height -= gap;
305         xcb_rectangle_t presel_rect = rect;
306
307         switch (p->split_dir) {
308                 case DIR_NORTH:
309                         presel_rect.height = p->split_ratio * rect.height;
310                         break;
311                 case DIR_EAST:
312                         presel_rect.width = (1 - p->split_ratio) * rect.width;
313                         presel_rect.x = rect.width - presel_rect.width;
314                         break;
315                 case DIR_SOUTH:
316                         presel_rect.height = (1 - p->split_ratio) * rect.height;
317                         presel_rect.y = rect.height - presel_rect.height;
318                         break;
319                 case DIR_WEST:
320                         presel_rect.width = p->split_ratio * rect.width;
321                         break;
322         }
323
324         window_move_resize(p->feedback, n->rectangle.x + presel_rect.x, n->rectangle.y + presel_rect.y,
325                            presel_rect.width, presel_rect.height);
326
327         if (!exists && m->desk == d) {
328                 window_show(p->feedback);
329         }
330 }
331
332 void refresh_presel_feedbacks(monitor_t *m, desktop_t *d, node_t *n)
333 {
334         if (n == NULL) {
335                 return;
336         } else {
337                 if (n->presel != NULL) {
338                         draw_presel_feedback(m, d, n);
339                 }
340                 refresh_presel_feedbacks(m, d, n->first_child);
341                 refresh_presel_feedbacks(m, d, n->second_child);
342         }
343 }
344
345 void show_presel_feedbacks(monitor_t *m, desktop_t *d, node_t *n)
346 {
347         if (n == NULL) {
348                 return;
349         } else {
350                 if (n->presel != NULL) {
351                         window_show(n->presel->feedback);
352                 }
353                 show_presel_feedbacks(m, d, n->first_child);
354                 show_presel_feedbacks(m, d, n->second_child);
355         }
356 }
357
358 void hide_presel_feedbacks(monitor_t *m, desktop_t *d, node_t *n)
359 {
360         if (n == NULL) {
361                 return;
362         } else {
363                 if (n->presel != NULL) {
364                         window_hide(n->presel->feedback);
365                 }
366                 hide_presel_feedbacks(m, d, n->first_child);
367                 hide_presel_feedbacks(m, d, n->second_child);
368         }
369 }
370
371 void update_colors(void)
372 {
373         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
374                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
375                         update_colors_in(d->root, d, m);
376                 }
377         }
378 }
379
380 void update_colors_in(node_t *n, desktop_t *d, monitor_t *m)
381 {
382         if (n == NULL) {
383                 return;
384         } else {
385                 if (n->presel != NULL) {
386                         uint32_t pxl = get_color_pixel(presel_feedback_color);
387                         xcb_change_window_attributes(dpy, n->presel->feedback, XCB_CW_BACK_PIXEL, &pxl);
388                         if (d == m->desk) {
389                                 /* hack to induce back pixel refresh */
390                                 window_hide(n->presel->feedback);
391                                 window_show(n->presel->feedback);
392                         }
393                 }
394                 if (n == d->focus) {
395                         draw_border(n, true, (m == mon));
396                 } else if (n->client != NULL) {
397                         draw_border(n, false, (m == mon));
398                 } else {
399                         update_colors_in(n->first_child, d, m);
400                         update_colors_in(n->second_child, d, m);
401                 }
402         }
403 }
404
405 void draw_border(node_t *n, bool focused_node, bool focused_monitor)
406 {
407         if (n == NULL) {
408                 return;
409         }
410
411         uint32_t border_color_pxl = get_border_color(focused_node, focused_monitor);
412         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
413                 if (f->client != NULL) {
414                         window_draw_border(f->id, border_color_pxl);
415                 }
416         }
417 }
418
419 void window_draw_border(xcb_window_t win, uint32_t border_color_pxl)
420 {
421         xcb_change_window_attributes(dpy, win, XCB_CW_BORDER_PIXEL, &border_color_pxl);
422 }
423
424 void adopt_orphans(void)
425 {
426         xcb_query_tree_reply_t *qtr = xcb_query_tree_reply(dpy, xcb_query_tree(dpy, root), NULL);
427         if (qtr == NULL) {
428                 return;
429         }
430
431         int len = xcb_query_tree_children_length(qtr);
432         xcb_window_t *wins = xcb_query_tree_children(qtr);
433
434         for (int i = 0; i < len; i++) {
435                 uint32_t idx;
436                 xcb_window_t win = wins[i];
437                 if (xcb_ewmh_get_wm_desktop_reply(ewmh, xcb_ewmh_get_wm_desktop(ewmh, win), &idx, NULL) == 1) {
438                         schedule_window(win);
439                 }
440         }
441
442         free(qtr);
443 }
444
445 uint32_t get_border_color(bool focused_node, bool focused_monitor)
446 {
447         if (focused_monitor && focused_node) {
448                 return get_color_pixel(focused_border_color);
449         } else if (focused_node) {
450                 return get_color_pixel(active_border_color);
451         } else {
452                 return get_color_pixel(normal_border_color);
453         }
454 }
455
456 void initialize_floating_rectangle(node_t *n)
457 {
458         client_t *c = n->client;
459
460         xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, n->id), NULL);
461
462         if (geo != NULL) {
463                 c->floating_rectangle = (xcb_rectangle_t) {geo->x, geo->y, geo->width, geo->height};
464         }
465
466         free(geo);
467 }
468
469 xcb_rectangle_t get_window_rectangle(node_t *n)
470 {
471         client_t *c = n->client;
472         if (c != NULL) {
473                 xcb_get_geometry_reply_t *g = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, n->id), NULL);
474                 if (g != NULL) {
475                         xcb_rectangle_t rect = (xcb_rectangle_t) {g->x, g->y, g->width, g->height};
476                         free(g);
477                         return rect;
478                 }
479         }
480         return (xcb_rectangle_t) {0, 0, screen_width, screen_height};
481 }
482
483 bool move_client(coordinates_t *loc, int dx, int dy)
484 {
485         node_t *n = loc->node;
486
487         if (n == NULL || n->client == NULL) {
488                 return false;
489         }
490
491         monitor_t *pm = NULL;
492
493         if (IS_TILED(n->client)) {
494                 if (!grabbing) {
495                         return false;
496                 }
497                 xcb_window_t pwin = XCB_NONE;
498                 query_pointer(&pwin, NULL);
499                 if (pwin == n->id) {
500                         return false;
501                 }
502                 coordinates_t dst;
503                 bool is_managed = (pwin != XCB_NONE && locate_window(pwin, &dst));
504                 if (is_managed && dst.monitor == loc->monitor && IS_TILED(dst.node->client)) {
505                         swap_nodes(loc->monitor, loc->desktop, n, loc->monitor, loc->desktop, dst.node, false);
506                         return true;
507                 } else {
508                         if (is_managed && dst.monitor == loc->monitor) {
509                                 return false;
510                         } else {
511                                 xcb_point_t pt = {0, 0};
512                                 query_pointer(NULL, &pt);
513                                 pm = monitor_from_point(pt);
514                         }
515                 }
516         } else {
517                 client_t *c = n->client;
518                 xcb_rectangle_t rect = c->floating_rectangle;
519                 int16_t x = rect.x + dx;
520                 int16_t y = rect.y + dy;
521
522                 window_move(n->id, x, y);
523
524                 c->floating_rectangle.x = x;
525                 c->floating_rectangle.y = y;
526                 if (!grabbing) {
527                         put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", loc->monitor->id, loc->desktop->id, loc->node->id, rect.width, rect.height, x, y);
528                 }
529                 pm = monitor_from_client(c);
530         }
531
532         if (pm == NULL || pm == loc->monitor) {
533                 return true;
534         }
535
536         transfer_node(loc->monitor, loc->desktop, n, pm, pm->desk, pm->desk->focus, true);
537         loc->monitor = pm;
538         loc->desktop = pm->desk;
539
540         return true;
541 }
542
543 bool resize_client(coordinates_t *loc, resize_handle_t rh, int dx, int dy, bool relative)
544 {
545         node_t *n = loc->node;
546         if (n == NULL || n->client == NULL || n->client->state == STATE_FULLSCREEN) {
547                 return false;
548         }
549         node_t *horizontal_fence = NULL, *vertical_fence = NULL;
550         xcb_rectangle_t rect = get_rectangle(NULL, NULL, n);
551         uint16_t width = rect.width, height = rect.height;
552         int16_t x = rect.x, y = rect.y;
553         if (n->client->state == STATE_TILED) {
554                 if (rh & HANDLE_LEFT) {
555                         vertical_fence = find_fence(n, DIR_WEST);
556                 } else if (rh & HANDLE_RIGHT) {
557                         vertical_fence = find_fence(n, DIR_EAST);
558                 }
559                 if (rh & HANDLE_TOP) {
560                         horizontal_fence = find_fence(n, DIR_NORTH);
561                 } else if (rh & HANDLE_BOTTOM) {
562                         horizontal_fence = find_fence(n, DIR_SOUTH);
563                 }
564                 if (vertical_fence == NULL && horizontal_fence == NULL) {
565                         return false;
566                 }
567                 if (vertical_fence != NULL) {
568                         double sr = 0.0;
569                         if (relative) {
570                                 sr = vertical_fence->split_ratio + (double) dx / (double) vertical_fence->rectangle.width;
571                         } else {
572                                 sr = (double) (dx - vertical_fence->rectangle.x) / (double) vertical_fence->rectangle.width;
573                         }
574                         sr = MAX(0, sr);
575                         sr = MIN(1, sr);
576                         vertical_fence->split_ratio = sr;
577                 }
578                 if (horizontal_fence != NULL) {
579                         double sr = 0.0;
580                         if (relative) {
581                                 sr = horizontal_fence->split_ratio + (double) dy / (double) horizontal_fence->rectangle.height;
582                         } else {
583                                 sr = (double) (dy - horizontal_fence->rectangle.y) / (double) horizontal_fence->rectangle.height;
584                         }
585                         sr = MAX(0, sr);
586                         sr = MIN(1, sr);
587                         horizontal_fence->split_ratio = sr;
588                 }
589                 node_t *target_fence = horizontal_fence != NULL ? horizontal_fence : vertical_fence;
590                 adjust_ratios(target_fence, target_fence->rectangle);
591                 arrange(loc->monitor, loc->desktop);
592         } else {
593                 int w = width, h = height;
594                 if (relative) {
595                         w += dx * (rh & HANDLE_LEFT ? -1 : (rh & HANDLE_RIGHT ? 1 : 0));
596                         h += dy * (rh & HANDLE_TOP ? -1 : (rh & HANDLE_BOTTOM ? 1 : 0));
597                 } else {
598                         if (rh & HANDLE_LEFT) {
599                                 w = x + width - dx;
600                         } else if (rh & HANDLE_RIGHT) {
601                                 w = dx - x;
602                         }
603                         if (rh & HANDLE_TOP) {
604                                 h = y + height - dy;
605                         } else if (rh & HANDLE_BOTTOM) {
606                                 h = dy - y;
607                         }
608                 }
609                 width = MAX(1, w);
610                 height = MAX(1, h);
611                 apply_size_hints(n->client, &width, &height);
612                 if (rh & HANDLE_LEFT) {
613                         x += rect.width - width;
614                 }
615                 if (rh & HANDLE_TOP) {
616                         y += rect.height - height;
617                 }
618                 n->client->floating_rectangle = (xcb_rectangle_t) {x, y, width, height};
619                 if (n->client->state == STATE_FLOATING) {
620                         window_move_resize(n->id, x, y, width, height);
621
622                         if (!grabbing) {
623                                 put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", loc->monitor->id, loc->desktop->id, loc->node->id, width, height, x, y);
624                         }
625                 } else {
626                         arrange(loc->monitor, loc->desktop);
627                 }
628         }
629         return true;
630 }
631
632 /* taken from awesomeWM */
633 void apply_size_hints(client_t *c, uint16_t *width, uint16_t *height)
634 {
635         if (!honor_size_hints) {
636                 return;
637         }
638
639         int32_t minw = 0, minh = 0;
640         int32_t basew = 0, baseh = 0, real_basew = 0, real_baseh = 0;
641
642         if (c->state == STATE_FULLSCREEN) {
643                 return;
644         }
645
646         if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
647                 basew = c->size_hints.base_width;
648                 baseh = c->size_hints.base_height;
649                 real_basew = basew;
650                 real_baseh = baseh;
651         } else if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
652                 /* base size is substituted with min size if not specified */
653                 basew = c->size_hints.min_width;
654                 baseh = c->size_hints.min_height;
655         }
656
657         if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MIN_SIZE) {
658                 minw = c->size_hints.min_width;
659                 minh = c->size_hints.min_height;
660         } else if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_BASE_SIZE) {
661                 /* min size is substituted with base size if not specified */
662                 minw = c->size_hints.base_width;
663                 minh = c->size_hints.base_height;
664         }
665
666         /* Handle the size aspect ratio */
667         if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_ASPECT &&
668             c->size_hints.min_aspect_den > 0 &&
669             c->size_hints.max_aspect_den > 0 &&
670             *height > real_baseh &&
671             *width > real_basew) {
672                 /* ICCCM mandates:
673                  * If a base size is provided along with the aspect ratio fields, the base size should be subtracted from the
674                  * window size prior to checking that the aspect ratio falls in range. If a base size is not provided, nothing
675                  * should be subtracted from the window size. (The minimum size is not to be used in place of the base size for
676                  * this purpose.)
677                  */
678                 double dx = *width - real_basew;
679                 double dy = *height - real_baseh;
680                 double ratio = dx / dy;
681                 double min = c->size_hints.min_aspect_num / (double) c->size_hints.min_aspect_den;
682                 double max = c->size_hints.max_aspect_num / (double) c->size_hints.max_aspect_den;
683
684                 if (max > 0 && min > 0 && ratio > 0) {
685                         if (ratio < min) {
686                                 /* dx is lower than allowed, make dy lower to compensate this (+ 0.5 to force proper rounding). */
687                                 dy = dx / min + 0.5;
688                                 *width  = dx + real_basew;
689                                 *height = dy + real_baseh;
690                         } else if (ratio > max) {
691                                 /* dx is too high, lower it (+0.5 for proper rounding) */
692                                 dx = dy * max + 0.5;
693                                 *width  = dx + real_basew;
694                                 *height = dy + real_baseh;
695                         }
696                 }
697         }
698
699         /* Handle the minimum size */
700         *width = MAX(*width, minw);
701         *height = MAX(*height, minh);
702
703         /* Handle the maximum size */
704         if (c->size_hints.flags & XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)
705         {
706                 if (c->size_hints.max_width > 0) {
707                         *width = MIN(*width, c->size_hints.max_width);
708                 }
709                 if (c->size_hints.max_height > 0) {
710                         *height = MIN(*height, c->size_hints.max_height);
711                 }
712         }
713
714         /* Handle the size increment */
715         if (c->size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_RESIZE_INC | XCB_ICCCM_SIZE_HINT_BASE_SIZE) &&
716             c->size_hints.width_inc > 0 && c->size_hints.height_inc > 0) {
717                 uint16_t t1 = *width, t2 = *height;
718                 unsigned_subtract(t1, basew);
719                 unsigned_subtract(t2, baseh);
720                 *width -= t1 % c->size_hints.width_inc;
721                 *height -= t2 % c->size_hints.height_inc;
722         }
723 }
724
725 void query_pointer(xcb_window_t *win, xcb_point_t *pt)
726 {
727         if (motion_recorder.enabled) {
728                 window_hide(motion_recorder.id);
729         }
730
731         xcb_query_pointer_reply_t *qpr = xcb_query_pointer_reply(dpy, xcb_query_pointer(dpy, root), NULL);
732
733         if (qpr != NULL) {
734                 if (win != NULL) {
735                         if (qpr->child == XCB_NONE) {
736                                 xcb_point_t mpt = (xcb_point_t) {qpr->root_x, qpr->root_y};
737                                 monitor_t *m = monitor_from_point(mpt);
738                                 if (m != NULL) {
739                                         desktop_t *d = m->desk;
740                                         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
741                                                 if (n->client == NULL && is_inside(mpt, get_rectangle(m, d, n))) {
742                                                         *win = n->id;
743                                                         break;
744                                                 }
745                                         }
746                                 }
747                         } else {
748                                 *win = qpr->child;
749                                 xcb_point_t pt = {qpr->root_x, qpr->root_y};
750                                 for (stacking_list_t *s = stack_tail; s != NULL; s = s->prev) {
751                                         if (!s->node->client->shown || s->node->hidden) {
752                                                 continue;
753                                         }
754                                         xcb_rectangle_t rect = get_rectangle(NULL, NULL, s->node);
755                                         if (is_inside(pt, rect)) {
756                                                 if (s->node->id == qpr->child || is_presel_window(qpr->child)) {
757                                                         *win = s->node->id;
758                                                 }
759                                                 break;
760                                         }
761                                 }
762                         }
763                 }
764                 if (pt != NULL) {
765                         *pt = (xcb_point_t) {qpr->root_x, qpr->root_y};
766                 }
767         }
768
769         free(qpr);
770
771         if (motion_recorder.enabled) {
772                 window_show(motion_recorder.id);
773         }
774 }
775
776 void update_motion_recorder(void)
777 {
778         xcb_point_t pt;
779         xcb_window_t win = XCB_NONE;
780         query_pointer(&win, &pt);
781         if (win == XCB_NONE) {
782                 return;
783         }
784         monitor_t *m = monitor_from_point(pt);
785         if (m == NULL) {
786                 return;
787         }
788         desktop_t *d = m->desk;
789         node_t *n = NULL;
790         for (n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
791                 if (n->id == win || (n->presel != NULL && n->presel->feedback == win)) {
792                         break;
793                 }
794         }
795         if ((n != NULL && n != mon->desk->focus) || (n == NULL && m != mon)) {
796                 enable_motion_recorder(win);
797         } else {
798                 disable_motion_recorder();
799         }
800 }
801
802 void enable_motion_recorder(xcb_window_t win)
803 {
804         xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, win), NULL);
805         if (geo != NULL) {
806                 uint16_t width = geo->width + 2 * geo->border_width;
807                 uint16_t height = geo->height + 2 * geo->border_width;
808                 window_move_resize(motion_recorder.id, geo->x, geo->y, width, height);
809                 window_above(motion_recorder.id, win);
810                 window_show(motion_recorder.id);
811                 motion_recorder.enabled = true;
812         }
813         free(geo);
814 }
815
816 void disable_motion_recorder(void)
817 {
818         if (!motion_recorder.enabled) {
819                 return;
820         }
821         window_hide(motion_recorder.id);
822         motion_recorder.enabled = false;
823 }
824
825 void window_border_width(xcb_window_t win, uint32_t bw)
826 {
827         uint32_t values[] = {bw};
828         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_BORDER_WIDTH, values);
829 }
830
831 void window_move(xcb_window_t win, int16_t x, int16_t y)
832 {
833         uint32_t values[] = {x, y};
834         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y, values);
835 }
836
837 void window_resize(xcb_window_t win, uint16_t w, uint16_t h)
838 {
839         uint32_t values[] = {w, h};
840         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_WIDTH_HEIGHT, values);
841 }
842
843 void window_move_resize(xcb_window_t win, int16_t x, int16_t y, uint16_t w, uint16_t h)
844 {
845         uint32_t values[] = {x, y, w, h};
846         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_X_Y_WIDTH_HEIGHT, values);
847 }
848
849 void window_center(monitor_t *m, client_t *c)
850 {
851         xcb_rectangle_t *r = &c->floating_rectangle;
852         xcb_rectangle_t a = m->rectangle;
853         if (r->width >= a.width) {
854                 r->x = a.x;
855         } else {
856                 r->x = a.x + (a.width - r->width) / 2;
857         }
858         if (r->height >= a.height) {
859                 r->y = a.y;
860         } else {
861                 r->y = a.y + (a.height - r->height) / 2;
862         }
863         r->x -= c->border_width;
864         r->y -= c->border_width;
865 }
866
867 void window_stack(xcb_window_t w1, xcb_window_t w2, uint32_t mode)
868 {
869         if (w2 == XCB_NONE) {
870                 return;
871         }
872         uint16_t mask = XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE;
873         uint32_t values[] = {w2, mode};
874         xcb_configure_window(dpy, w1, mask, values);
875 }
876
877 /* Stack w1 above w2 */
878 void window_above(xcb_window_t w1, xcb_window_t w2)
879 {
880         window_stack(w1, w2, XCB_STACK_MODE_ABOVE);
881 }
882
883 /* Stack w1 below w2 */
884 void window_below(xcb_window_t w1, xcb_window_t w2)
885 {
886         window_stack(w1, w2, XCB_STACK_MODE_BELOW);
887 }
888
889 void window_lower(xcb_window_t win)
890 {
891         uint32_t values[] = {XCB_STACK_MODE_BELOW};
892         xcb_configure_window(dpy, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
893 }
894
895 void window_set_visibility(xcb_window_t win, bool visible)
896 {
897         uint32_t values_off[] = {ROOT_EVENT_MASK & ~XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY};
898         uint32_t values_on[] = {ROOT_EVENT_MASK};
899         xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_off);
900         if (visible) {
901                 set_window_state(win, XCB_ICCCM_WM_STATE_NORMAL);
902                 xcb_map_window(dpy, win);
903         } else {
904                 xcb_unmap_window(dpy, win);
905                 set_window_state(win, XCB_ICCCM_WM_STATE_ICONIC);
906         }
907         xcb_change_window_attributes(dpy, root, XCB_CW_EVENT_MASK, values_on);
908 }
909
910 void window_hide(xcb_window_t win)
911 {
912         window_set_visibility(win, false);
913 }
914
915 void window_show(xcb_window_t win)
916 {
917         window_set_visibility(win, true);
918 }
919
920 void update_input_focus(void)
921 {
922         set_input_focus(mon->desk->focus);
923 }
924
925 void set_input_focus(node_t *n)
926 {
927         if (n == NULL || n->client == NULL) {
928                 clear_input_focus();
929         } else {
930                 if (n->client->icccm_props.input_hint) {
931                         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_PARENT, n->id, XCB_CURRENT_TIME);
932                 } else if (n->client->icccm_props.take_focus) {
933                         send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_TAKE_FOCUS);
934                 }
935         }
936 }
937
938 void clear_input_focus(void)
939 {
940         xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
941 }
942
943 void center_pointer(xcb_rectangle_t r)
944 {
945         if (grabbing) {
946                 return;
947         }
948         int16_t cx = r.x + r.width / 2;
949         int16_t cy = r.y + r.height / 2;
950         xcb_warp_pointer(dpy, XCB_NONE, root, 0, 0, 0, 0, cx, cy);
951 }
952
953 void get_atom(char *name, xcb_atom_t *atom)
954 {
955         xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(dpy, xcb_intern_atom(dpy, 0, strlen(name), name), NULL);
956         if (reply != NULL) {
957                 *atom = reply->atom;
958         } else {
959                 *atom = XCB_NONE;
960         }
961         free(reply);
962 }
963
964 void set_atom(xcb_window_t win, xcb_atom_t atom, uint32_t value)
965 {
966         xcb_change_property(dpy, XCB_PROP_MODE_REPLACE, win, atom, XCB_ATOM_CARDINAL, 32, 1, &value);
967 }
968
969 void send_client_message(xcb_window_t win, xcb_atom_t property, xcb_atom_t value)
970 {
971         xcb_client_message_event_t *e = calloc(32, 1);
972
973         e->response_type = XCB_CLIENT_MESSAGE;
974         e->window = win;
975         e->type = property;
976         e->format = 32;
977         e->data.data32[0] = value;
978         e->data.data32[1] = XCB_CURRENT_TIME;
979
980         xcb_send_event(dpy, false, win, XCB_EVENT_MASK_NO_EVENT, (char *) e);
981         xcb_flush(dpy);
982         free(e);
983 }
984
985 bool window_exists(xcb_window_t win)
986 {
987         xcb_generic_error_t *err;
988         free(xcb_query_tree_reply(dpy, xcb_query_tree(dpy, win), &err));
989
990         if (err != NULL) {
991                 free(err);
992                 return false;
993         }
994
995         return true;
996 }