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