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