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