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