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