]> git.lizzy.rs Git - bspwm.git/blob - src/tree.c
bspwm: port rounded corners patch to latest version
[bspwm.git] / src / tree.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 <limits.h>
29 #include "bspwm.h"
30 #include "desktop.h"
31 #include "ewmh.h"
32 #include "history.h"
33 #include "monitor.h"
34 #include "query.h"
35 #include "geometry.h"
36 #include "subscribe.h"
37 #include "settings.h"
38 #include "pointer.h"
39 #include "stack.h"
40 #include "window.h"
41 #include "tree.h"
42
43 void arrange(monitor_t *m, desktop_t *d)
44 {
45         if (d->root == NULL) {
46                 return;
47         }
48
49         xcb_rectangle_t rect = m->rectangle;
50
51         rect.x += m->padding.left + d->padding.left;
52         rect.y += m->padding.top + d->padding.top;
53         rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
54         rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
55
56         if (d->layout == LAYOUT_MONOCLE) {
57                 rect.x += monocle_padding.left;
58                 rect.y += monocle_padding.top;
59                 rect.width -= monocle_padding.left + monocle_padding.right;
60                 rect.height -= monocle_padding.top + monocle_padding.bottom;
61         }
62
63         if (!gapless_monocle || d->layout != LAYOUT_MONOCLE) {
64                 rect.x += d->window_gap;
65                 rect.y += d->window_gap;
66                 rect.width -= d->window_gap;
67                 rect.height -= d->window_gap;
68         }
69
70         apply_layout(m, d, d->root, rect, rect);
71 }
72
73 void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
74 {
75         if (n == NULL) {
76                 return;
77         }
78
79         n->rectangle = rect;
80
81         if (n->presel != NULL) {
82                 draw_presel_feedback(m, d, n);
83         }
84
85         if (is_leaf(n)) {
86         unsigned int br = 0;
87
88                 if (n->client == NULL) {
89                         return;
90                 }
91
92                 unsigned int bw;
93                 bool the_only_window = !m->prev && !m->next && d->root->client;
94                 if ((borderless_monocle && d->layout == LAYOUT_MONOCLE && IS_TILED(n->client))
95                     || (borderless_singleton && the_only_window)
96                     || n->client->state == STATE_FULLSCREEN) {
97                         bw = 0;
98                 } else {
99                         bw = n->client->border_width;
100                 }
101
102                 xcb_rectangle_t r;
103                 xcb_rectangle_t cr = get_window_rectangle(n);
104                 client_state_t s = n->client->state;
105                 /* tiled and pseudo-tiled clients */
106                 if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
107                         int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
108                         r = rect;
109                         int bleed = wg + 2 * bw;
110                         r.width = (bleed < r.width ? r.width - bleed : 1);
111                         r.height = (bleed < r.height ? r.height - bleed : 1);
112                         /* pseudo-tiled clients */
113                         if (s == STATE_PSEUDO_TILED) {
114                                 xcb_rectangle_t f = n->client->floating_rectangle;
115                                 r.width = MIN(r.width, f.width);
116                                 r.height = MIN(r.height, f.height);
117                                 if (center_pseudo_tiled) {
118                                         r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
119                                         r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
120                                 }
121                         }
122                         n->client->tiled_rectangle = r;
123             if (!gapless_monocle || 1 != LAYOUT_MONOCLE)
124                 br = n->client->border_radius;
125                 /* floating clients */
126                 } else if (s == STATE_FLOATING) {
127                         r = n->client->floating_rectangle;
128             br = n->client->border_radius;
129                 /* fullscreen clients */
130                 } else {
131                         r = m->rectangle;
132                         n->client->tiled_rectangle = r;
133                 }
134
135                 apply_size_hints(n->client, &r.width, &r.height);
136
137                 if (!rect_eq(r, cr)) {
138                         window_move_resize(n->id, r.x, r.y, r.width, r.height);
139             window_rounded_border(n);
140                         if (!grabbing) {
141                                 put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", m->id, d->id, n->id, r.width, r.height, r.x, r.y);
142                         }
143                 }
144
145                 window_border_width(n->id, bw);
146         window_border_radius(n->client, br);
147         window_rounded_border(n);
148
149         } else {
150                 xcb_rectangle_t first_rect;
151                 xcb_rectangle_t second_rect;
152
153                 if (d->layout == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
154                         first_rect = second_rect = rect;
155                 } else {
156                         unsigned int fence;
157                         if (n->split_type == TYPE_VERTICAL) {
158                                 fence = rect.width * n->split_ratio;
159                                 if ((n->first_child->constraints.min_width + n->second_child->constraints.min_width) <= rect.width) {
160                                         if (fence < n->first_child->constraints.min_width) {
161                                                 fence = n->first_child->constraints.min_width;
162                                                 n->split_ratio = (double) fence / (double) rect.width;
163                                         } else if (fence > (uint16_t) (rect.width - n->second_child->constraints.min_width)) {
164                                                 fence = (rect.width - n->second_child->constraints.min_width);
165                                                 n->split_ratio = (double) fence / (double) rect.width;
166                                         }
167                                 }
168                                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
169                                 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
170                         } else {
171                                 fence = rect.height * n->split_ratio;
172                                 if ((n->first_child->constraints.min_height + n->second_child->constraints.min_height) <= rect.height) {
173                                         if (fence < n->first_child->constraints.min_height) {
174                                                 fence = n->first_child->constraints.min_height;
175                                                 n->split_ratio = (double) fence / (double) rect.height;
176                                         } else if (fence > (uint16_t) (rect.height - n->second_child->constraints.min_height)) {
177                                                 fence = (rect.height - n->second_child->constraints.min_height);
178                                                 n->split_ratio = (double) fence / (double) rect.height;
179                                         }
180                                 }
181                                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
182                                 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
183                         }
184                 }
185
186                 apply_layout(m, d, n->first_child, first_rect, root_rect);
187                 apply_layout(m, d, n->second_child, second_rect, root_rect);
188         }
189 }
190
191 presel_t *make_presel(void)
192 {
193         presel_t *p = calloc(1, sizeof(presel_t));
194         p->split_dir = DIR_EAST;
195         p->split_ratio = split_ratio;
196         p->feedback = XCB_NONE;
197         return p;
198 }
199
200 void set_type(node_t *n, split_type_t typ)
201 {
202         if (n == NULL) {
203                 return;
204         }
205
206         n->split_type = typ;
207         update_constraints(n);
208         rebuild_constraints_towards_root(n);
209 }
210
211 void set_ratio(node_t *n, double rat)
212 {
213         if (n == NULL) {
214                 return;
215         }
216
217         n->split_ratio = rat;
218 }
219
220 void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
221 {
222         if (n->presel == NULL) {
223                 n->presel = make_presel();
224         }
225
226         n->presel->split_dir = dir;
227
228         put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X dir %s\n", m->id, d->id, n->id, SPLIT_DIR_STR(dir));
229 }
230
231 void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
232 {
233         if (n->presel == NULL) {
234                 n->presel = make_presel();
235         }
236
237         n->presel->split_ratio = ratio;
238
239         put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
240 }
241
242 void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
243 {
244         if (n->presel == NULL) {
245                 return;
246         }
247
248         if (n->presel->feedback != XCB_NONE) {
249                 xcb_destroy_window(dpy, n->presel->feedback);
250         }
251
252         free(n->presel);
253         n->presel = NULL;
254
255         put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
256 }
257
258 void cancel_presel_in(monitor_t *m, desktop_t *d, node_t *n)
259 {
260         if (n == NULL) {
261                 return;
262         }
263         cancel_presel(m, d, n);
264         cancel_presel_in(m, d, n->first_child);
265         cancel_presel_in(m, d, n->second_child);
266 }
267
268 node_t *find_public(desktop_t *d)
269 {
270         unsigned int b_manual_area = 0;
271         unsigned int b_automatic_area = 0;
272         node_t *b_manual = NULL;
273         node_t *b_automatic = NULL;
274         for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
275                 if (n->vacant) {
276                         continue;
277                 }
278                 unsigned int n_area = node_area(d, n);
279                 if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
280                         b_manual = n;
281                         b_manual_area = n_area;
282                 }
283                 if (n_area > b_automatic_area &&
284                     n->presel == NULL && !n->private && private_count(n->parent) == 0) {
285                         b_automatic = n;
286                         b_automatic_area = n_area;
287                 }
288         }
289         if (b_automatic != NULL) {
290                 return b_automatic;
291         } else {
292                 return b_manual;
293         }
294 }
295
296 node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
297 {
298         if (d == NULL || n == NULL) {
299                 return NULL;
300         }
301
302         /* n: inserted node */
303         /* c: new internal node */
304         /* f: focus or insertion anchor */
305         /* p: parent of focus */
306         /* g: grand parent of focus */
307
308         if (f == NULL) {
309                 f = d->root;
310         }
311
312         if (f == NULL) {
313                 d->root = n;
314         } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
315                 node_t *p = f->parent;
316                 if (p != NULL) {
317                         if (is_first_child(f)) {
318                                 p->first_child = n;
319                         } else {
320                                 p->second_child = n;
321                         }
322                 } else {
323                         d->root = n;
324                 }
325                 n->parent = p;
326                 free(f);
327                 f = NULL;
328         } else {
329                 node_t *c = make_node(XCB_NONE);
330                 node_t *p = f->parent;
331                 if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
332                         node_t *k = find_public(d);
333                         if (k != NULL) {
334                                 f = k;
335                                 p = f->parent;
336                         }
337                         if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
338                                 xcb_rectangle_t rect = get_rectangle(m, d, f);
339                                 presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
340                         }
341                 }
342                 n->parent = c;
343                 if (f->presel == NULL) {
344                         bool single_tiled = f->client != NULL && IS_TILED(f->client) && tiled_count(d->root, true) == 1;
345                         if (p == NULL || automatic_scheme != SCHEME_SPIRAL || single_tiled) {
346                                 if (p != NULL) {
347                                         if (is_first_child(f)) {
348                                                 p->first_child = c;
349                                         } else {
350                                                 p->second_child = c;
351                                         }
352                                 } else {
353                                         d->root = c;
354                                 }
355                                 c->parent = p;
356                                 f->parent = c;
357                                 if (initial_polarity == FIRST_CHILD) {
358                                         c->first_child = n;
359                                         c->second_child = f;
360                                 } else {
361                                         c->first_child = f;
362                                         c->second_child = n;
363                                 }
364                                 if (p == NULL || automatic_scheme == SCHEME_LONGEST_SIDE || single_tiled) {
365                                         if (f->rectangle.width > f->rectangle.height) {
366                                                 c->split_type = TYPE_VERTICAL;
367                                         } else {
368                                                 c->split_type = TYPE_HORIZONTAL;
369                                         }
370                                 } else {
371                                         node_t *q = p;
372                                         while (q != NULL && (q->first_child->vacant || q->second_child->vacant)) {
373                                                 q = q->parent;
374                                         }
375                                         if (q == NULL) {
376                                                 q = p;
377                                         }
378                                         if (q->split_type == TYPE_HORIZONTAL) {
379                                                 c->split_type = TYPE_VERTICAL;
380                                         } else {
381                                                 c->split_type = TYPE_HORIZONTAL;
382                                         }
383                                 }
384                         } else {
385                                 node_t *g = p->parent;
386                                 c->parent = g;
387                                 if (g != NULL) {
388                                         if (is_first_child(p)) {
389                                                 g->first_child = c;
390                                         } else {
391                                                 g->second_child = c;
392                                         }
393                                 } else {
394                                         d->root = c;
395                                 }
396                                 c->split_type = p->split_type;
397                                 c->split_ratio = p->split_ratio;
398                                 p->parent = c;
399                                 int rot;
400                                 if (is_first_child(f)) {
401                                         c->first_child = n;
402                                         c->second_child = p;
403                                         rot = 90;
404                                 } else {
405                                         c->first_child = p;
406                                         c->second_child = n;
407                                         rot = 270;
408                                 }
409                                 if (!n->vacant) {
410                                         rotate_tree(p, rot);
411                                 }
412                         }
413                 } else {
414                         if (p != NULL) {
415                                 if (is_first_child(f)) {
416                                         p->first_child = c;
417                                 } else {
418                                         p->second_child = c;
419                                 }
420                         }
421                         c->split_ratio = f->presel->split_ratio;
422                         c->parent = p;
423                         f->parent = c;
424                         switch (f->presel->split_dir) {
425                                 case DIR_WEST:
426                                         c->split_type = TYPE_VERTICAL;
427                                         c->first_child = n;
428                                         c->second_child = f;
429                                         break;
430                                 case DIR_EAST:
431                                         c->split_type = TYPE_VERTICAL;
432                                         c->first_child = f;
433                                         c->second_child = n;
434                                         break;
435                                 case DIR_NORTH:
436                                         c->split_type = TYPE_HORIZONTAL;
437                                         c->first_child = n;
438                                         c->second_child = f;
439                                         break;
440                                 case DIR_SOUTH:
441                                         c->split_type = TYPE_HORIZONTAL;
442                                         c->first_child = f;
443                                         c->second_child = n;
444                                         break;
445                         }
446                         if (d->root == f) {
447                                 d->root = c;
448                         }
449                         cancel_presel(m, d, f);
450                         set_marked(m, d, n, false);
451                 }
452         }
453
454         propagate_flags_upward(m, d, n);
455
456         if (d->focus == NULL && is_focusable(n)) {
457                 d->focus = n;
458         }
459
460         return f;
461 }
462
463 void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
464 {
465         node_t *r = make_node(XCB_NONE);
466         insert_node(m, d, r, n);
467         put_status(SBSC_MASK_NODE_ADD, "node_add 0x%08X 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n != NULL ? n->id : 0, r->id);
468
469         if (single_monocle && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
470                 set_layout(m, d, d->user_layout, false);
471         }
472 }
473
474 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
475 {
476         if (n == NULL && d->root != NULL) {
477                 n = d->focus;
478                 if (n == NULL) {
479                         n = history_last_node(d, NULL);
480                 }
481                 if (n == NULL) {
482                         n = first_focusable_leaf(d->root);
483                 }
484         }
485
486         if (d == mon->desk || (n != NULL && !is_focusable(n))) {
487                 return false;
488         }
489
490         if (n != NULL) {
491                 if (d->focus != NULL && n != d->focus) {
492                         neutralize_occluding_windows(m, d, n);
493                 }
494                 stack(d, n, true);
495                 if (d->focus != n) {
496                         for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
497                                 if (f->client != NULL && !is_descendant(f, n)) {
498                                         window_draw_border(f->id, get_border_color(false, (m == mon)));
499                                 }
500                         }
501                 }
502                 draw_border(n, true, (m == mon));
503         }
504
505         d->focus = n;
506         history_add(m, d, n, false);
507
508         put_status(SBSC_MASK_REPORT);
509
510         if (n == NULL) {
511                 return true;
512         }
513
514         put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
515
516         return true;
517 }
518
519 void transfer_sticky_nodes(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
520 {
521         if (n == NULL) {
522                 return;
523         } else if (n->sticky) {
524                 sticky_still = false;
525                 transfer_node(ms, ds, n, md, dd, dd->focus, false);
526                 sticky_still = true;
527         } else {
528                 /* we need references to the children because n might be freed after
529                  * the first recursive call */
530                 node_t *first_child = n->first_child;
531                 node_t *second_child = n->second_child;
532                 transfer_sticky_nodes(ms, ds, md, dd, first_child);
533                 transfer_sticky_nodes(ms, ds, md, dd, second_child);
534         }
535 }
536
537 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
538 {
539         if (m == NULL) {
540                 m = mon;
541                 if (m == NULL) {
542                         m = history_last_monitor(NULL);
543                 }
544                 if (m == NULL) {
545                         m = mon_head;
546                 }
547         }
548
549         if (m == NULL) {
550                 return false;
551         }
552
553         if (d == NULL) {
554                 d = m->desk;
555                 if (d == NULL) {
556                         d = history_last_desktop(m, NULL);
557                 }
558                 if (d == NULL) {
559                         d = m->desk_head;
560                 }
561         }
562
563         if (d == NULL) {
564                 return false;
565         }
566
567         bool guess = (n == NULL);
568
569         if (n == NULL && d->root != NULL) {
570                 n = d->focus;
571                 if (n == NULL) {
572                         n = history_last_node(d, NULL);
573                 }
574                 if (n == NULL) {
575                         n = first_focusable_leaf(d->root);
576                 }
577         }
578
579         if (n != NULL && !is_focusable(n)) {
580                 return false;
581         }
582
583         if ((mon != NULL && mon->desk != d) || n == NULL || n->client == NULL) {
584                 clear_input_focus();
585         }
586
587         if (m->sticky_count > 0 && m->desk != NULL && d != m->desk) {
588                 if (guess && m->desk->focus != NULL && m->desk->focus->sticky) {
589                         n = m->desk->focus;
590                 }
591
592                 transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
593
594                 if (n == NULL && d->focus != NULL) {
595                         n = d->focus;
596                 }
597         }
598
599         if (d->focus != NULL && n != d->focus) {
600                 neutralize_occluding_windows(m, d, n);
601         }
602
603         if (n != NULL && n->client != NULL && n->client->urgent) {
604                 set_urgent(m, d, n, false);
605         }
606
607         if (mon != m) {
608                 if (mon != NULL) {
609                         for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
610                                 draw_border(e->focus, true, false);
611                         }
612                 }
613                 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
614                         if (e == d) {
615                                 continue;
616                         }
617                         draw_border(e->focus, true, true);
618                 }
619         }
620
621         if (d->focus != n) {
622                 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
623                         if (f->client != NULL && !is_descendant(f, n)) {
624                                 window_draw_border(f->id, get_border_color(false, true));
625                         }
626                 }
627         }
628
629         draw_border(n, true, true);
630
631         bool desk_changed = (m != mon || m->desk != d);
632         bool has_input_focus = false;
633
634         if (mon != m) {
635                 mon = m;
636
637                 if (pointer_follows_monitor) {
638                         center_pointer(m->rectangle);
639                 }
640
641                 put_status(SBSC_MASK_MONITOR_FOCUS, "monitor_focus 0x%08X\n", m->id);
642         }
643
644         if (m->desk != d) {
645                 show_desktop(d);
646                 set_input_focus(n);
647                 has_input_focus = true;
648                 hide_desktop(m->desk);
649                 m->desk = d;
650         }
651
652         if (desk_changed) {
653                 ewmh_update_current_desktop();
654                 put_status(SBSC_MASK_DESKTOP_FOCUS, "desktop_focus 0x%08X 0x%08X\n", m->id, d->id);
655         }
656
657         d->focus = n;
658         if (!has_input_focus) {
659                 set_input_focus(n);
660         }
661         ewmh_update_active_window();
662         history_add(m, d, n, true);
663
664         put_status(SBSC_MASK_REPORT);
665
666         if (n == NULL) {
667                 if (focus_follows_pointer) {
668                         update_motion_recorder();
669                 }
670                 return true;
671         }
672
673         put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
674
675         stack(d, n, true);
676
677         if (pointer_follows_focus) {
678                 center_pointer(get_rectangle(m, d, n));
679         } else if (focus_follows_pointer) {
680                 update_motion_recorder();
681         }
682
683         return true;
684 }
685
686 void hide_node(desktop_t *d, node_t *n)
687 {
688         if (n == NULL || (!hide_sticky && n->sticky)) {
689                 return;
690         } else {
691                 if (!n->hidden) {
692                         if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
693                                 window_hide(n->presel->feedback);
694                         }
695                         if (n->client != NULL) {
696                                 window_hide(n->id);
697                         }
698                 }
699                 if (n->client != NULL) {
700                         n->client->shown = false;
701                 }
702                 hide_node(d, n->first_child);
703                 hide_node(d, n->second_child);
704         }
705 }
706
707 void show_node(desktop_t *d, node_t *n)
708 {
709         if (n == NULL) {
710                 return;
711         } else {
712                 if (!n->hidden) {
713                         if (n->client != NULL) {
714                                 window_show(n->id);
715                         }
716                         if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
717                                 window_show(n->presel->feedback);
718                         }
719                 }
720                 if (n->client != NULL) {
721                         n->client->shown = true;
722                 }
723                 show_node(d, n->first_child);
724                 show_node(d, n->second_child);
725         }
726 }
727
728 node_t *make_node(uint32_t id)
729 {
730         if (id == XCB_NONE) {
731                 id = xcb_generate_id(dpy);
732         }
733         node_t *n = calloc(1, sizeof(node_t));
734         n->id = id;
735         n->parent = n->first_child = n->second_child = NULL;
736         n->vacant = n->hidden = n->sticky = n->private = n->locked = n->marked = false;
737         n->split_ratio = split_ratio;
738         n->split_type = TYPE_VERTICAL;
739         n->constraints = (constraints_t) {MIN_WIDTH, MIN_HEIGHT};
740         n->presel = NULL;
741         n->client = NULL;
742         return n;
743 }
744
745 client_t *make_client(void)
746 {
747         client_t *c = calloc(1, sizeof(client_t));
748         c->state = c->last_state = STATE_TILED;
749         c->layer = c->last_layer = LAYER_NORMAL;
750         snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
751         snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
752         c->border_width = border_width;
753         c->border_radius = border_radius;
754         c->urgent = false;
755         c->shown = false;
756     c->sets_own_shape = false;
757         c->wm_flags = 0;
758         c->icccm_props.input_hint = true;
759         c->icccm_props.take_focus = false;
760         c->icccm_props.delete_window = false;
761         c->size_hints.flags = 0;
762         return c;
763 }
764
765 void initialize_client(node_t *n)
766 {
767         xcb_window_t win = n->id;
768         client_t *c = n->client;
769         xcb_icccm_get_wm_protocols_reply_t protos;
770         if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protos, NULL) == 1) {
771                 for (uint32_t i = 0; i < protos.atoms_len; i++) {
772                         if (protos.atoms[i] == WM_TAKE_FOCUS) {
773                                 c->icccm_props.take_focus = true;
774                         } else if (protos.atoms[i] == WM_DELETE_WINDOW) {
775                                 c->icccm_props.delete_window = true;
776                         }
777                 }
778                 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
779         }
780         xcb_ewmh_get_atoms_reply_t wm_state;
781         if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
782                 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
783 #define HANDLE_WM_STATE(s) \
784                         if (wm_state.atoms[i] == ewmh->_NET_WM_STATE_##s) { \
785                                 c->wm_flags |= WM_FLAG_##s; continue; \
786                         }
787                         HANDLE_WM_STATE(MODAL)
788                         HANDLE_WM_STATE(STICKY)
789                         HANDLE_WM_STATE(MAXIMIZED_VERT)
790                         HANDLE_WM_STATE(MAXIMIZED_HORZ)
791                         HANDLE_WM_STATE(SHADED)
792                         HANDLE_WM_STATE(SKIP_TASKBAR)
793                         HANDLE_WM_STATE(SKIP_PAGER)
794                         HANDLE_WM_STATE(HIDDEN)
795                         HANDLE_WM_STATE(FULLSCREEN)
796                         HANDLE_WM_STATE(ABOVE)
797                         HANDLE_WM_STATE(BELOW)
798                         HANDLE_WM_STATE(DEMANDS_ATTENTION)
799 #undef HANDLE_WM_STATE
800                 }
801                 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
802         }
803         xcb_icccm_wm_hints_t hints;
804         if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
805                 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
806                 c->icccm_props.input_hint = hints.input;
807         }
808         xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &c->size_hints, NULL);
809 }
810
811 bool is_focusable(node_t *n)
812 {
813         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
814                 if (f->client != NULL && !f->hidden) {
815                         return true;
816                 }
817         }
818         return false;
819 }
820
821 bool is_leaf(node_t *n)
822 {
823         return (n != NULL && n->first_child == NULL && n->second_child == NULL);
824 }
825
826 bool is_first_child(node_t *n)
827 {
828         return (n != NULL && n->parent != NULL && n->parent->first_child == n);
829 }
830
831 bool is_second_child(node_t *n)
832 {
833         return (n != NULL && n->parent != NULL && n->parent->second_child == n);
834 }
835
836 unsigned int clients_count_in(node_t *n)
837 {
838         if (n == NULL) {
839                 return 0;
840         } else {
841                 return (n->client != NULL ? 1 : 0) +
842                         clients_count_in(n->first_child) +
843                         clients_count_in(n->second_child);
844         }
845 }
846
847 node_t *brother_tree(node_t *n)
848 {
849         if (n == NULL || n->parent == NULL) {
850                 return NULL;
851         }
852         if (is_first_child(n)) {
853                 return n->parent->second_child;
854         } else {
855                 return n->parent->first_child;
856         }
857 }
858
859 node_t *first_extrema(node_t *n)
860 {
861         if (n == NULL) {
862                 return NULL;
863         } else if (n->first_child == NULL) {
864                 return n;
865         } else {
866                 return first_extrema(n->first_child);
867         }
868 }
869
870 node_t *second_extrema(node_t *n)
871 {
872         if (n == NULL) {
873                 return NULL;
874         } else if (n->second_child == NULL) {
875                 return n;
876         } else {
877                 return second_extrema(n->second_child);
878         }
879 }
880
881 node_t *first_focusable_leaf(node_t *n)
882 {
883         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
884                 if (f->client != NULL && !f->hidden) {
885                         return f;
886                 }
887         }
888         return NULL;
889 }
890
891 node_t *next_node(node_t *n)
892 {
893         if (n == NULL) {
894                 return NULL;
895         }
896
897         if (n->second_child != NULL) {
898                 return first_extrema(n->second_child);
899         } else {
900                 node_t *p = n;
901                 while (is_second_child(p)) {
902                         p = p->parent;
903                 }
904                 if (is_first_child(p)) {
905                         return p->parent;
906                 } else {
907                         return NULL;
908                 }
909         }
910 }
911
912 node_t *prev_node(node_t *n)
913 {
914         if (n == NULL) {
915                 return NULL;
916         }
917
918         if (n->first_child != NULL) {
919                 return second_extrema(n->first_child);
920         } else {
921                 node_t *p = n;
922                 while (is_first_child(p)) {
923                         p = p->parent;
924                 }
925                 if (is_second_child(p)) {
926                         return p->parent;
927                 } else {
928                         return NULL;
929                 }
930         }
931 }
932
933 node_t *next_leaf(node_t *n, node_t *r)
934 {
935         if (n == NULL) {
936                 return NULL;
937         }
938         node_t *p = n;
939         while (is_second_child(p) && p != r) {
940                 p = p->parent;
941         }
942         if (p == r) {
943                 return NULL;
944         }
945         return first_extrema(p->parent->second_child);
946 }
947
948 node_t *prev_leaf(node_t *n, node_t *r)
949 {
950         if (n == NULL) {
951                 return NULL;
952         }
953         node_t *p = n;
954         while (is_first_child(p) && p != r) {
955                 p = p->parent;
956         }
957         if (p == r) {
958                 return NULL;
959         }
960         return second_extrema(p->parent->first_child);
961 }
962
963 node_t *next_tiled_leaf(node_t *n, node_t *r)
964 {
965         node_t *next = next_leaf(n, r);
966         if (next == NULL || (next->client != NULL && !next->vacant)) {
967                 return next;
968         } else {
969                 return next_tiled_leaf(next, r);
970         }
971 }
972
973 node_t *prev_tiled_leaf(node_t *n, node_t *r)
974 {
975         node_t *prev = prev_leaf(n, r);
976         if (prev == NULL || (prev->client != NULL && !prev->vacant)) {
977                 return prev;
978         } else {
979                 return prev_tiled_leaf(prev, r);
980         }
981 }
982
983 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
984 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
985 {
986         switch (dir) {
987                 case DIR_EAST:
988                         return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
989                         break;
990                 case DIR_SOUTH:
991                         return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
992                         break;
993                 case DIR_WEST:
994                         return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
995                         break;
996                 case DIR_NORTH:
997                         return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
998                         break;
999         }
1000         return false;
1001 }
1002
1003 node_t *find_fence(node_t *n, direction_t dir)
1004 {
1005         node_t *p;
1006
1007         if (n == NULL) {
1008                 return NULL;
1009         }
1010
1011         p = n->parent;
1012
1013         while (p != NULL) {
1014                 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
1015                     (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
1016                     (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
1017                     (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
1018                         return p;
1019                 p = p->parent;
1020         }
1021
1022         return NULL;
1023 }
1024
1025 /* returns *true* if *a* is a child of *b* */
1026 bool is_child(node_t *a, node_t *b)
1027 {
1028         if (a == NULL || b == NULL) {
1029                 return false;
1030         }
1031         return (a->parent != NULL && a->parent == b);
1032 }
1033
1034 /* returns *true* if *a* is a descendant of *b* */
1035 bool is_descendant(node_t *a, node_t *b)
1036 {
1037         if (a == NULL || b == NULL) {
1038                 return false;
1039         }
1040         while (a != b && a != NULL) {
1041                 a = a->parent;
1042         }
1043         return a == b;
1044 }
1045
1046 bool find_by_id(uint32_t id, coordinates_t *loc)
1047 {
1048         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1049                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1050                         node_t *n = find_by_id_in(d->root, id);
1051                         if (n != NULL) {
1052                                 loc->monitor = m;
1053                                 loc->desktop = d;
1054                                 loc->node = n;
1055                                 return true;
1056                         }
1057                 }
1058         }
1059         return false;
1060 }
1061
1062 node_t *find_by_id_in(node_t *r, uint32_t id)
1063 {
1064         if (r == NULL) {
1065                 return NULL;
1066         } else if (r->id == id) {
1067                 return r;
1068         } else {
1069                 node_t *f = find_by_id_in(r->first_child, id);
1070                 if (f != NULL) {
1071                         return f;
1072                 } else {
1073                         return find_by_id_in(r->second_child, id);
1074                 }
1075         }
1076 }
1077
1078 void find_any_node(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1079 {
1080         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1081                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1082                         if (find_any_node_in(m, d, d->root, ref, dst, sel)) {
1083                                 return;
1084                         }
1085                 }
1086         }
1087 }
1088
1089 bool find_any_node_in(monitor_t *m, desktop_t *d, node_t *n, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1090 {
1091         if (n == NULL) {
1092                 return false;
1093         } else {
1094                 coordinates_t loc = {m, d, n};
1095                 if (node_matches(&loc, ref, sel)) {
1096                         *dst = loc;
1097                         return true;
1098                 } else {
1099                         if (find_any_node_in(m, d, n->first_child, ref, dst, sel)) {
1100                                 return true;
1101                         } else {
1102                                 return find_any_node_in(m, d, n->second_child, ref, dst, sel);
1103                         }
1104                 }
1105         }
1106 }
1107
1108 void find_first_ancestor(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1109 {
1110         if (ref->node == NULL) {
1111                 return;
1112         }
1113
1114         coordinates_t loc = {ref->monitor, ref->desktop, ref->node};
1115         while ((loc.node = loc.node->parent) != NULL) {
1116                 if (node_matches(&loc, ref, sel)) {
1117                         *dst = loc;
1118                         return;
1119                 }
1120         }
1121 }
1122
1123 /* Based on https://github.com/ntrrgc/right-window */
1124 void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t *sel)
1125 {
1126         xcb_rectangle_t rect = get_rectangle(ref->monitor, ref->desktop, ref->node);
1127         uint32_t md = UINT32_MAX, mr = UINT32_MAX;
1128
1129         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1130                 desktop_t *d = m->desk;
1131                 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1132                         coordinates_t loc = {m, d, f};
1133                         xcb_rectangle_t r = get_rectangle(m, d, f);
1134                         if (f == ref->node ||
1135                             f->client == NULL ||
1136                             f->hidden ||
1137                             is_descendant(f, ref->node) ||
1138                             !node_matches(&loc, ref, sel) ||
1139                             !on_dir_side(rect, r, dir)) {
1140                                 continue;
1141                         }
1142                         uint32_t fd = boundary_distance(rect, r, dir);
1143                         uint32_t fr = history_rank(f);
1144                         if (fd < md || (fd == md && fr < mr)) {
1145                                 md = fd;
1146                                 mr = fr;
1147                                 *dst = loc;
1148                         }
1149                 }
1150         }
1151 }
1152
1153 unsigned int node_area(desktop_t *d, node_t *n)
1154 {
1155         if (n == NULL) {
1156                 return 0;
1157         }
1158         return area(get_rectangle(NULL, d, n));
1159 }
1160
1161 int tiled_count(node_t *n, bool include_receptacles)
1162 {
1163         if (n == NULL) {
1164                 return 0;
1165         }
1166         int cnt = 0;
1167         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1168                 if (!f->hidden && ((include_receptacles && f->client == NULL) ||
1169                                    (f->client != NULL && IS_TILED(f->client)))) {
1170                         cnt++;
1171                 }
1172         }
1173         return cnt;
1174 }
1175
1176 void find_by_area(area_peak_t ap, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
1177 {
1178         unsigned int p_area;
1179         if (ap == AREA_BIGGEST) {
1180                 p_area = 0;
1181         } else {
1182                 p_area = UINT_MAX;
1183         }
1184
1185         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1186                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1187                         for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1188                                 coordinates_t loc = {m, d, f};
1189                                 if (f->vacant || !node_matches(&loc, ref, sel)) {
1190                                         continue;
1191                                 }
1192                                 unsigned int f_area = node_area(d, f);
1193                                 if ((ap == AREA_BIGGEST && f_area > p_area) || (ap == AREA_SMALLEST && f_area < p_area)) {
1194                                         *dst = loc;
1195                                         p_area = f_area;
1196                                 }
1197                         }
1198                 }
1199         }
1200 }
1201
1202 void rotate_tree(node_t *n, int deg)
1203 {
1204         rotate_tree_rec(n, deg);
1205         rebuild_constraints_from_leaves(n);
1206         rebuild_constraints_towards_root(n);
1207 }
1208
1209 void rotate_tree_rec(node_t *n, int deg)
1210 {
1211         if (n == NULL || is_leaf(n) || deg == 0) {
1212                 return;
1213         }
1214
1215         node_t *tmp;
1216
1217         if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1218             (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1219             deg == 180) {
1220                 tmp = n->first_child;
1221                 n->first_child = n->second_child;
1222                 n->second_child = tmp;
1223                 n->split_ratio = 1.0 - n->split_ratio;
1224         }
1225
1226         if (deg != 180) {
1227                 if (n->split_type == TYPE_HORIZONTAL) {
1228                         n->split_type = TYPE_VERTICAL;
1229                 } else if (n->split_type == TYPE_VERTICAL) {
1230                         n->split_type = TYPE_HORIZONTAL;
1231                 }
1232         }
1233
1234         rotate_tree_rec(n->first_child, deg);
1235         rotate_tree_rec(n->second_child, deg);
1236 }
1237
1238 void flip_tree(node_t *n, flip_t flp)
1239 {
1240         if (n == NULL || is_leaf(n)) {
1241                 return;
1242         }
1243
1244         node_t *tmp;
1245
1246         if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1247             (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1248                 tmp = n->first_child;
1249                 n->first_child = n->second_child;
1250                 n->second_child = tmp;
1251                 n->split_ratio = 1.0 - n->split_ratio;
1252         }
1253
1254         flip_tree(n->first_child, flp);
1255         flip_tree(n->second_child, flp);
1256 }
1257
1258 void equalize_tree(node_t *n)
1259 {
1260         if (n == NULL || n->vacant) {
1261                 return;
1262         } else {
1263                 n->split_ratio = split_ratio;
1264                 equalize_tree(n->first_child);
1265                 equalize_tree(n->second_child);
1266         }
1267 }
1268
1269 int balance_tree(node_t *n)
1270 {
1271         if (n == NULL || n->vacant) {
1272                 return 0;
1273         } else if (is_leaf(n)) {
1274                 return 1;
1275         } else {
1276                 int b1 = balance_tree(n->first_child);
1277                 int b2 = balance_tree(n->second_child);
1278                 int b = b1 + b2;
1279                 if (b1 > 0 && b2 > 0) {
1280                         n->split_ratio = (double) b1 / b;
1281                 }
1282                 return b;
1283         }
1284 }
1285
1286 /* Adjust the split ratios so that they keep their position
1287  * despite the potential alteration of their rectangle. */
1288 void adjust_ratios(node_t *n, xcb_rectangle_t rect)
1289 {
1290         if (n == NULL) {
1291                 return;
1292         }
1293
1294         double ratio;
1295
1296         if (n->split_type == TYPE_VERTICAL) {
1297                 double position = (double) n->rectangle.x + n->split_ratio * (double) n->rectangle.width;
1298                 ratio = (position - (double) rect.x) / (double) rect.width;
1299         } else {
1300                 double position = (double) n->rectangle.y + n->split_ratio * (double) n->rectangle.height;
1301                 ratio = (position - (double) rect.y) / (double) rect.height;
1302         }
1303
1304         ratio = MAX(0.0, ratio);
1305         ratio = MIN(1.0, ratio);
1306         n->split_ratio = ratio;
1307
1308         xcb_rectangle_t first_rect;
1309         xcb_rectangle_t second_rect;
1310         unsigned int fence;
1311
1312         if (n->split_type == TYPE_VERTICAL) {
1313                 fence = rect.width * n->split_ratio;
1314                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
1315                 second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
1316         } else {
1317                 fence = rect.height * n->split_ratio;
1318                 first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
1319                 second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
1320         }
1321
1322         adjust_ratios(n->first_child, first_rect);
1323         adjust_ratios(n->second_child, second_rect);
1324 }
1325
1326 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1327 {
1328         if (d == NULL || n == NULL) {
1329                 return;
1330         }
1331
1332         node_t *p = n->parent;
1333
1334         if (p == NULL) {
1335                 d->root = NULL;
1336                 d->focus = NULL;
1337         } else {
1338                 if (d->focus == p || is_descendant(d->focus, n)) {
1339                         d->focus = NULL;
1340                 }
1341
1342                 history_remove(d, p, false);
1343                 cancel_presel(m, d, p);
1344
1345                 if (p->sticky) {
1346                         m->sticky_count--;
1347                 }
1348
1349                 node_t *b = brother_tree(n);
1350                 node_t *g = p->parent;
1351
1352                 b->parent = g;
1353
1354                 if (g != NULL) {
1355                         if (is_first_child(p)) {
1356                                 g->first_child = b;
1357                         } else {
1358                                 g->second_child = b;
1359                         }
1360                 } else {
1361                         d->root = b;
1362                 }
1363
1364                 if (!n->vacant && removal_adjustment) {
1365                         if (automatic_scheme == SCHEME_SPIRAL) {
1366                                 if (is_first_child(n)) {
1367                                         rotate_tree(b, 270);
1368                                 } else {
1369                                         rotate_tree(b, 90);
1370                                 }
1371                         } else if (automatic_scheme == SCHEME_LONGEST_SIDE || g == NULL) {
1372                                 if (p != NULL) {
1373                                         if (p->rectangle.width > p->rectangle.height) {
1374                                                 b->split_type = TYPE_VERTICAL;
1375                                         } else {
1376                                                 b->split_type = TYPE_HORIZONTAL;
1377                                         }
1378                                 }
1379                         } else if (automatic_scheme == SCHEME_ALTERNATE) {
1380                                 if (g->split_type == TYPE_HORIZONTAL) {
1381                                         b->split_type = TYPE_VERTICAL;
1382                                 } else {
1383                                         b->split_type = TYPE_HORIZONTAL;
1384                                 }
1385                         }
1386                 }
1387
1388                 free(p);
1389                 n->parent = NULL;
1390
1391                 propagate_flags_upward(m, d, b);
1392         }
1393 }
1394
1395 void close_node(node_t *n)
1396 {
1397         if (n == NULL) {
1398                 return;
1399         } else if (n->client != NULL) {
1400                 if (n->client->icccm_props.delete_window) {
1401                         send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1402                 } else {
1403                         xcb_kill_client(dpy, n->id);
1404                 }
1405         } else {
1406                 close_node(n->first_child);
1407                 close_node(n->second_child);
1408         }
1409 }
1410
1411 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1412 {
1413         if (n == NULL) {
1414                 return;
1415         }
1416
1417         if (IS_RECEPTACLE(n)) {
1418                 put_status(SBSC_MASK_NODE_REMOVE, "node_remove 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
1419                 remove_node(m, d, n);
1420         } else {
1421                 for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1422                         if (f->client != NULL) {
1423                                 xcb_kill_client(dpy, f->id);
1424                         }
1425                 }
1426         }
1427 }
1428
1429 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1430 {
1431         if (n == NULL) {
1432                 return;
1433         }
1434
1435         unlink_node(m, d, n);
1436         history_remove(d, n, true);
1437         remove_stack_node(n);
1438         cancel_presel_in(m, d, n);
1439         if (m->sticky_count > 0 && d == m->desk) {
1440                 m->sticky_count -= sticky_count(n);
1441         }
1442         clients_count -= clients_count_in(n);
1443         if (is_descendant(grabbed_node, n)) {
1444                 grabbed_node = NULL;
1445         }
1446         free_node(n);
1447
1448         if (single_monocle && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
1449                 set_layout(m, d, LAYOUT_MONOCLE, false);
1450         }
1451
1452         ewmh_update_client_list(false);
1453         ewmh_update_client_list(true);
1454
1455         if (mon != NULL && d->focus == NULL) {
1456                 if (d == mon->desk) {
1457                         focus_node(m, d, NULL);
1458                 } else {
1459                         activate_node(m, d, NULL);
1460                 }
1461         }
1462 }
1463
1464 void free_node(node_t *n)
1465 {
1466         if (n == NULL) {
1467                 return;
1468         }
1469         node_t *first_child = n->first_child;
1470         node_t *second_child = n->second_child;
1471         free(n->client);
1472         free(n);
1473         free_node(first_child);
1474         free_node(second_child);
1475 }
1476
1477 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2, bool follow)
1478 {
1479         if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1480             (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1481                           (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1482                 return false;
1483         }
1484
1485         put_status(SBSC_MASK_NODE_SWAP, "node_swap 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n", m1->id, d1->id, n1->id, m2->id, d2->id, n2->id);
1486
1487         node_t *pn1 = n1->parent;
1488         node_t *pn2 = n2->parent;
1489         bool n1_first_child = is_first_child(n1);
1490         bool n2_first_child = is_first_child(n2);
1491         bool n1_held_focus = is_descendant(d1->focus, n1);
1492         bool n2_held_focus = is_descendant(d2->focus, n2);
1493         node_t *last_d1_focus = d1->focus;
1494         node_t *last_d2_focus = d2->focus;
1495
1496         if (pn1 != NULL) {
1497                 if (n1_first_child) {
1498                         pn1->first_child = n2;
1499                 } else {
1500                         pn1->second_child = n2;
1501                 }
1502         }
1503
1504         if (pn2 != NULL) {
1505                 if (n2_first_child) {
1506                         pn2->first_child = n1;
1507                 } else {
1508                         pn2->second_child = n1;
1509                 }
1510         }
1511
1512         n1->parent = pn2;
1513         n2->parent = pn1;
1514
1515         propagate_flags_upward(m2, d2, n1);
1516         propagate_flags_upward(m1, d1, n2);
1517
1518         if (d1 != d2) {
1519                 if (d1->root == n1) {
1520                         d1->root = n2;
1521                 }
1522
1523                 if (d2->root == n2) {
1524                         d2->root = n1;
1525                 }
1526
1527                 if (n1_held_focus) {
1528                         d1->focus = n2_held_focus ? last_d2_focus : n2;
1529                 }
1530
1531                 if (n2_held_focus) {
1532                         d2->focus = n1_held_focus ? last_d1_focus : n1;
1533                 }
1534
1535                 if (m1 != m2) {
1536                         adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1537                         adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1538                 }
1539
1540                 ewmh_set_wm_desktop(n1, d2);
1541                 ewmh_set_wm_desktop(n2, d1);
1542
1543                 history_remove(d1, n1, true);
1544                 history_remove(d2, n2, true);
1545
1546                 bool d1_was_focused = (d1 == mon->desk);
1547                 bool d2_was_focused = (d2 == mon->desk);
1548
1549                 if (m1->desk != d1 && m2->desk == d2) {
1550                         show_node(d2, n1);
1551                         if (!follow || !d2_was_focused || !n2_held_focus) {
1552                                 hide_node(d2, n2);
1553                         }
1554                 } else if (m1->desk == d1 && m2->desk != d2) {
1555                         if (!follow || !d1_was_focused || !n1_held_focus) {
1556                                 hide_node(d1, n1);
1557                         }
1558                         show_node(d1, n2);
1559                 }
1560
1561                 if (single_monocle) {
1562                         layout_t l1 = tiled_count(d1->root, true) <= 1 ? LAYOUT_MONOCLE : d1->user_layout;
1563                         layout_t l2 = tiled_count(d2->root, true) <= 1 ? LAYOUT_MONOCLE : d2->user_layout;
1564                         set_layout(m1, d1, l1, false);
1565                         set_layout(m2, d2, l2, false);
1566                 }
1567
1568                 if (n1_held_focus) {
1569                         if (d1_was_focused) {
1570                                 if (follow) {
1571                                         focus_node(m2, d2, last_d1_focus);
1572                                 } else {
1573                                         focus_node(m1, d1, d1->focus);
1574                                 }
1575                         } else {
1576                                 activate_node(m1, d1, d1->focus);
1577                         }
1578                 } else {
1579                         draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1580                 }
1581
1582                 if (n2_held_focus) {
1583                         if (d2_was_focused) {
1584                                 if (follow) {
1585                                         focus_node(m1, d1, last_d2_focus);
1586                                 } else {
1587                                         focus_node(m2, d2, d2->focus);
1588                                 }
1589                         } else {
1590                                 activate_node(m2, d2, d2->focus);
1591                         }
1592                 } else {
1593                         draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1594                 }
1595         } else {
1596                 if (!n1_held_focus) {
1597                         draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1598                 }
1599                 if (!n2_held_focus) {
1600                         draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1601                 }
1602         }
1603
1604         arrange(m1, d1);
1605
1606         if (d1 != d2) {
1607                 arrange(m2, d2);
1608         } else {
1609                 if (pointer_follows_focus && (n1_held_focus || n2_held_focus)) {
1610                         center_pointer(get_rectangle(m1, d1, d1->focus));
1611                 }
1612         }
1613
1614         return true;
1615 }
1616
1617 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd, bool follow)
1618 {
1619         if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1620                 return false;
1621         }
1622
1623         unsigned int sc = (ms->sticky_count > 0 && ds == ms->desk) ? sticky_count(ns) : 0;
1624         if (sticky_still && sc > 0 && dd != md->desk) {
1625                 return false;
1626         }
1627
1628         put_status(SBSC_MASK_NODE_TRANSFER, "node_transfer 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X 0x%08X\n", ms->id, ds->id, ns->id, md->id, dd->id, nd!=NULL?nd->id:0);
1629
1630         bool held_focus = is_descendant(ds->focus, ns);
1631         /* avoid ending up with a dangling pointer (because of unlink_node) */
1632         node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1633         bool ds_was_focused = (ds == mon->desk);
1634
1635         if (held_focus && ds_was_focused) {
1636                 clear_input_focus();
1637         }
1638
1639         unlink_node(ms, ds, ns);
1640         insert_node(md, dd, ns, nd);
1641
1642         if (md != ms) {
1643                 if (ns->client == NULL || monitor_from_client(ns->client) != md) {
1644                         adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1645                 }
1646                 ms->sticky_count -= sc;
1647                 md->sticky_count += sc;
1648         }
1649
1650         if (ds != dd) {
1651                 ewmh_set_wm_desktop(ns, dd);
1652                 if (sticky_still) {
1653                         if (ds == ms->desk && dd != md->desk) {
1654                                 hide_node(ds, ns);
1655                         } else if (ds != ms->desk && dd == md->desk) {
1656                                 show_node(dd, ns);
1657                         }
1658                 }
1659         }
1660
1661         history_remove(ds, ns, true);
1662         stack(dd, ns, false);
1663
1664         if (ds == dd) {
1665                 if (held_focus) {
1666                         if (ds_was_focused) {
1667                                 focus_node(ms, ds, last_ds_focus);
1668                         } else {
1669                                 activate_node(ms, ds, last_ds_focus);
1670                         }
1671                 } else {
1672                         draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1673                 }
1674         } else {
1675                 if (single_monocle) {
1676                         if (ds->layout != LAYOUT_MONOCLE && tiled_count(ds->root, true) <= 1) {
1677                                 set_layout(ms, ds, LAYOUT_MONOCLE, false);
1678                         }
1679                         if (dd->layout == LAYOUT_MONOCLE && tiled_count(dd->root, true) > 1) {
1680                                 set_layout(md, dd, dd->user_layout, false);
1681                         }
1682                 }
1683                 if (held_focus) {
1684                         if (follow) {
1685                                 if (ds_was_focused) {
1686                                         focus_node(md, dd, last_ds_focus);
1687                                 }
1688                                 activate_node(ms, ds, ds->focus);
1689                         } else {
1690                                 if (ds_was_focused) {
1691                                         focus_node(ms, ds, ds->focus);
1692                                 } else {
1693                                         activate_node(ms, ds, ds->focus);
1694                                 }
1695                         }
1696                 }
1697                 if (!held_focus || !follow || !ds_was_focused) {
1698                         if (dd->focus == ns) {
1699                                 if (dd == mon->desk) {
1700                                         focus_node(md, dd, held_focus ? last_ds_focus : ns);
1701                                 } else {
1702                                         activate_node(md, dd, held_focus ? last_ds_focus : ns);
1703                                 }
1704                         } else {
1705                                 draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1706                         }
1707                 }
1708         }
1709
1710         arrange(ms, ds);
1711
1712         if (ds != dd) {
1713                 arrange(md, dd);
1714         }
1715
1716         return true;
1717 }
1718
1719 bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t *sel)
1720 {
1721         monitor_t *m = ref->monitor;
1722         desktop_t *d = ref->desktop;
1723         node_t *n = ref->node;
1724         n = (dir == CYCLE_PREV ? prev_node(n) : next_node(n));
1725
1726 #define HANDLE_BOUNDARIES(m, d, n)  \
1727         while (n == NULL) { \
1728                 d = (dir == CYCLE_PREV ? d->prev : d->next); \
1729                 if (d == NULL) { \
1730                         m = (dir == CYCLE_PREV ? m->prev : m->next); \
1731                         if (m == NULL) { \
1732                                 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
1733                         } \
1734                         d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
1735                 } \
1736                 n = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
1737                 if (ref->node == NULL && d == ref->desktop) { \
1738                         break; \
1739                 } \
1740         }
1741         HANDLE_BOUNDARIES(m, d, n);
1742
1743         while (n != ref->node) {
1744                 coordinates_t loc = {m, d, n};
1745                 if (node_matches(&loc, ref, sel)) {
1746                         *dst = loc;
1747                         return true;
1748                 }
1749                 n = (dir == CYCLE_PREV ? prev_node(n) : next_node(n));
1750                 HANDLE_BOUNDARIES(m, d, n);
1751                 if (ref->node == NULL && d == ref->desktop) {
1752                         break;
1753                 }
1754         }
1755 #undef HANDLE_BOUNDARIES
1756         return false;
1757 }
1758
1759 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1760 {
1761         if (tiled_count(n, false) < 2) {
1762                 return;
1763         }
1764         node_t *p = d->focus->parent;
1765         bool focus_first_child = is_first_child(d->focus);
1766         if (dir == CIRCULATE_FORWARD) {
1767                 node_t *e = second_extrema(n);
1768                 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1769                         e = prev_leaf(e, n);
1770                 }
1771                 for (node_t *s = e, *f = prev_tiled_leaf(s, n); f != NULL; s = prev_tiled_leaf(f, n), f = prev_tiled_leaf(s, n)) {
1772                         swap_nodes(m, d, f, m, d, s, false);
1773                 }
1774         } else {
1775                 node_t *e = first_extrema(n);
1776                 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1777                         e = next_leaf(e, n);
1778                 }
1779                 for (node_t *f = e, *s = next_tiled_leaf(f, n); s != NULL; f = next_tiled_leaf(s, n), s = next_tiled_leaf(f, n)) {
1780                         swap_nodes(m, d, f, m, d, s, false);
1781                 }
1782         }
1783         if (p != NULL) {
1784                 node_t *f = focus_first_child ? p->first_child : p->second_child;
1785                 if (is_leaf(f)) {
1786                         if (d == mon->desk) {
1787                                 focus_node(m, d, f);
1788                         } else {
1789                                 activate_node(m, d, f);
1790                         }
1791                 }
1792         }
1793 }
1794
1795 void set_vacant(monitor_t *m, desktop_t *d, node_t *n, bool value)
1796 {
1797         if (n->vacant == value) {
1798                 return;
1799         }
1800
1801         propagate_vacant_downward(m, d, n, value);
1802         propagate_vacant_upward(m, d, n);
1803 }
1804
1805 void set_vacant_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1806 {
1807         if (n->vacant == value) {
1808                 return;
1809         }
1810
1811         n->vacant = value;
1812
1813         if (value) {
1814                 cancel_presel(m, d, n);
1815         }
1816 }
1817
1818 void propagate_vacant_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1819 {
1820         if (n == NULL) {
1821                 return;
1822         }
1823
1824         set_vacant_local(m, d, n, value);
1825
1826         propagate_vacant_downward(m, d, n->first_child, value);
1827         propagate_vacant_downward(m, d, n->second_child, value);
1828 }
1829
1830 void propagate_vacant_upward(monitor_t *m, desktop_t *d, node_t *n)
1831 {
1832         if (n == NULL) {
1833                 return;
1834         }
1835
1836         node_t *p = n->parent;
1837
1838         if (p != NULL) {
1839                 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1840         }
1841
1842         propagate_vacant_upward(m, d, p);
1843 }
1844
1845 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1846 {
1847         if (n == NULL || n->client == NULL || n->client->layer == l) {
1848                 return false;
1849         }
1850
1851         n->client->last_layer = n->client->layer;
1852         n->client->layer = l;
1853
1854         if (l == LAYER_ABOVE) {
1855                 n->client->wm_flags |= WM_FLAG_ABOVE;
1856                 n->client->wm_flags &= ~WM_FLAG_BELOW;
1857         } else if (l == LAYER_BELOW) {
1858                 n->client->wm_flags |= WM_FLAG_BELOW;
1859                 n->client->wm_flags &= ~WM_FLAG_ABOVE;
1860         } else {
1861                 n->client->wm_flags &= ~(WM_FLAG_ABOVE | WM_FLAG_BELOW);
1862         }
1863
1864         ewmh_wm_state_update(n);
1865
1866         put_status(SBSC_MASK_NODE_LAYER, "node_layer 0x%08X 0x%08X 0x%08X %s\n", m->id, d->id, n->id, LAYER_STR(l));
1867
1868         if (d->focus == n) {
1869                 neutralize_occluding_windows(m, d, n);
1870         }
1871
1872         stack(d, n, (d->focus == n));
1873
1874         return true;
1875 }
1876
1877 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1878 {
1879         if (n == NULL || n->client == NULL || n->client->state == s) {
1880                 return false;
1881         }
1882
1883         client_t *c = n->client;
1884
1885         bool was_tiled = IS_TILED(c);
1886
1887         c->last_state = c->state;
1888         c->state = s;
1889
1890         switch (c->last_state) {
1891                 case STATE_TILED:
1892                 case STATE_PSEUDO_TILED:
1893                         break;
1894                 case STATE_FLOATING:
1895                         set_floating(m, d, n, false);
1896                         break;
1897                 case STATE_FULLSCREEN:
1898                         set_fullscreen(m, d, n, false);
1899                         break;
1900         }
1901
1902         put_status(SBSC_MASK_NODE_STATE, "node_state 0x%08X 0x%08X 0x%08X %s off\n", m->id, d->id, n->id, STATE_STR(c->last_state));
1903
1904         switch (c->state) {
1905                 case STATE_TILED:
1906                 case STATE_PSEUDO_TILED:
1907                         break;
1908                 case STATE_FLOATING:
1909                         set_floating(m, d, n, true);
1910                         break;
1911                 case STATE_FULLSCREEN:
1912                         set_fullscreen(m, d, n, true);
1913                         break;
1914         }
1915
1916         put_status(SBSC_MASK_NODE_STATE, "node_state 0x%08X 0x%08X 0x%08X %s on\n", m->id, d->id, n->id, STATE_STR(c->state));
1917
1918         if (n == m->desk->focus) {
1919                 put_status(SBSC_MASK_REPORT);
1920         }
1921
1922         if (single_monocle && was_tiled != IS_TILED(c)) {
1923                 if (was_tiled && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
1924                         set_layout(m, d, LAYOUT_MONOCLE, false);
1925                 } else if (!was_tiled && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
1926                         set_layout(m, d, d->user_layout, false);
1927                 }
1928         }
1929
1930         return true;
1931 }
1932
1933 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1934 {
1935         if (n == NULL) {
1936                 return;
1937         }
1938
1939         cancel_presel(m, d, n);
1940         if (!n->hidden) {
1941                 set_vacant(m, d, n, value);
1942         }
1943
1944         if (!value && d->focus == n) {
1945                 neutralize_occluding_windows(m, d, n);
1946         }
1947
1948         stack(d, n, (d->focus == n));
1949 }
1950
1951 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1952 {
1953         if (n == NULL) {
1954                 return;
1955         }
1956
1957         client_t *c = n->client;
1958
1959         cancel_presel(m, d, n);
1960         if (!n->hidden) {
1961                 set_vacant(m, d, n, value);
1962         }
1963
1964         if (value) {
1965                 c->wm_flags |= WM_FLAG_FULLSCREEN;
1966         } else {
1967                 c->wm_flags &= ~WM_FLAG_FULLSCREEN;
1968                 if (d->focus == n) {
1969                         neutralize_occluding_windows(m, d, n);
1970                 }
1971         }
1972
1973         ewmh_wm_state_update(n);
1974         stack(d, n, (d->focus == n));
1975 }
1976
1977 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1978 {
1979         bool changed = false;
1980         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1981                 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1982                         if (a != f && a->client != NULL && f->client != NULL &&
1983                             IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1984                                 set_state(m, d, a, a->client->last_state);
1985                                 changed = true;
1986                         }
1987                 }
1988         }
1989         if (changed) {
1990                 arrange(m, d);
1991         }
1992 }
1993
1994 void rebuild_constraints_from_leaves(node_t *n)
1995 {
1996         if (n == NULL || is_leaf(n)) {
1997                 return;
1998         } else {
1999                 rebuild_constraints_from_leaves(n->first_child);
2000                 rebuild_constraints_from_leaves(n->second_child);
2001                 update_constraints(n);
2002         }
2003 }
2004
2005 void rebuild_constraints_towards_root(node_t *n)
2006 {
2007         if (n == NULL) {
2008                 return;
2009         }
2010
2011         node_t *p = n->parent;
2012
2013         if (p != NULL) {
2014                 update_constraints(p);
2015         }
2016
2017         rebuild_constraints_towards_root(p);
2018 }
2019
2020 void update_constraints(node_t *n)
2021 {
2022         if (n == NULL || is_leaf(n)) {
2023                 return;
2024         }
2025         if (n->split_type == TYPE_VERTICAL) {
2026                 n->constraints.min_width = n->first_child->constraints.min_width + n->second_child->constraints.min_width;
2027                 n->constraints.min_height = MAX(n->first_child->constraints.min_height, n->second_child->constraints.min_height);
2028         } else {
2029                 n->constraints.min_width = MAX(n->first_child->constraints.min_width, n->second_child->constraints.min_width);
2030                 n->constraints.min_height = n->first_child->constraints.min_height + n->second_child->constraints.min_height;
2031         }
2032 }
2033
2034 void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
2035 {
2036         if (n == NULL) {
2037                 return;
2038         }
2039
2040         node_t *p = n->parent;
2041
2042         if (p != NULL) {
2043                 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
2044                 set_hidden_local(m, d, p, (p->first_child->hidden && p->second_child->hidden));
2045                 update_constraints(p);
2046         }
2047
2048         propagate_flags_upward(m, d, p);
2049 }
2050
2051 void set_hidden(monitor_t *m, desktop_t *d, node_t *n, bool value)
2052 {
2053         if (n == NULL || n->hidden == value) {
2054                 return;
2055         }
2056
2057         bool held_focus = is_descendant(d->focus, n);
2058
2059         propagate_hidden_downward(m, d, n, value);
2060         propagate_hidden_upward(m, d, n);
2061
2062         put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X hidden %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2063
2064         if (held_focus || d->focus == NULL) {
2065                 if (d->focus != NULL) {
2066                         d->focus = NULL;
2067                         draw_border(n, false, (mon == m));
2068                 }
2069                 if (d == mon->desk) {
2070                         focus_node(m, d, d->focus);
2071                 } else {
2072                         activate_node(m, d, d->focus);
2073                 }
2074         }
2075
2076         if (single_monocle) {
2077                 if (value && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
2078                         set_layout(m, d, LAYOUT_MONOCLE, false);
2079                 } else if (!value && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
2080                         set_layout(m, d, d->user_layout, false);
2081                 }
2082         }
2083 }
2084
2085 void set_hidden_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
2086 {
2087         if (n->hidden == value) {
2088                 return;
2089         }
2090
2091         n->hidden = value;
2092
2093         if (n->client != NULL) {
2094                 if (n->client->shown) {
2095                         window_set_visibility(n->id, !value);
2096                 }
2097
2098                 if (IS_TILED(n->client)) {
2099                         set_vacant(m, d, n, value);
2100                 }
2101
2102                 if (value) {
2103                         n->client->wm_flags |= WM_FLAG_HIDDEN;
2104                 } else {
2105                         n->client->wm_flags &= ~WM_FLAG_HIDDEN;
2106                 }
2107
2108                 ewmh_wm_state_update(n);
2109         }
2110 }
2111
2112 void propagate_hidden_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
2113 {
2114         if (n == NULL) {
2115                 return;
2116         }
2117
2118         set_hidden_local(m, d, n, value);
2119
2120         propagate_hidden_downward(m, d, n->first_child, value);
2121         propagate_hidden_downward(m, d, n->second_child, value);
2122 }
2123
2124 void propagate_hidden_upward(monitor_t *m, desktop_t *d, node_t *n)
2125 {
2126         if (n == NULL) {
2127                 return;
2128         }
2129
2130         node_t *p = n->parent;
2131
2132         if (p != NULL) {
2133                 set_hidden_local(m, d, p, p->first_child->hidden && p->second_child->hidden);
2134         }
2135
2136         propagate_hidden_upward(m, d, p);
2137 }
2138
2139 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
2140 {
2141         if (n == NULL || n->sticky == value) {
2142                 return;
2143         }
2144
2145         if (d != m->desk) {
2146                 transfer_node(m, d, n, m, m->desk, m->desk->focus, false);
2147         }
2148
2149         n->sticky = value;
2150
2151         if (value) {
2152                 m->sticky_count++;
2153         } else {
2154                 m->sticky_count--;
2155         }
2156
2157         if (n->client != NULL) {
2158                 if (value) {
2159                         n->client->wm_flags |= WM_FLAG_STICKY;
2160                 } else {
2161                         n->client->wm_flags &= ~WM_FLAG_STICKY;
2162                 }
2163                 ewmh_wm_state_update(n);
2164         }
2165
2166         put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X sticky %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2167
2168         if (n == m->desk->focus) {
2169                 put_status(SBSC_MASK_REPORT);
2170         }
2171 }
2172
2173 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
2174 {
2175         if (n == NULL || n->private == value) {
2176                 return;
2177         }
2178
2179         n->private = value;
2180
2181         put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X private %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2182
2183         if (n == m->desk->focus) {
2184                 put_status(SBSC_MASK_REPORT);
2185         }
2186 }
2187
2188 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
2189 {
2190         if (n == NULL || n->locked == value) {
2191                 return;
2192         }
2193
2194         n->locked = value;
2195
2196         put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X locked %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2197
2198         if (n == m->desk->focus) {
2199                 put_status(SBSC_MASK_REPORT);
2200         }
2201 }
2202
2203 void set_marked(monitor_t *m, desktop_t *d, node_t *n, bool value)
2204 {
2205         if (n == NULL || n->marked == value) {
2206                 return;
2207         }
2208
2209         n->marked = value;
2210
2211         put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X marked %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2212
2213         if (n == m->desk->focus) {
2214                 put_status(SBSC_MASK_REPORT);
2215         }
2216 }
2217
2218 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
2219 {
2220         if (value && mon->desk->focus == n) {
2221                 return;
2222         }
2223
2224         n->client->urgent = value;
2225
2226         if (value) {
2227                 n->client->wm_flags |= WM_FLAG_DEMANDS_ATTENTION;
2228         } else {
2229                 n->client->wm_flags &= ~WM_FLAG_DEMANDS_ATTENTION;
2230         }
2231
2232         ewmh_wm_state_update(n);
2233
2234         put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X urgent %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
2235         put_status(SBSC_MASK_REPORT);
2236 }
2237
2238 xcb_rectangle_t get_rectangle(monitor_t *m, desktop_t *d, node_t *n)
2239 {
2240         if (n == NULL) {
2241                 return m->rectangle;
2242         }
2243         client_t *c = n->client;
2244         if (c != NULL) {
2245                 if (IS_FLOATING(c)) {
2246                         return c->floating_rectangle;
2247                 } else {
2248                         return c->tiled_rectangle;
2249                 }
2250         } else {
2251                 int wg = (d == NULL ? 0 : (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap));
2252                 xcb_rectangle_t rect = n->rectangle;
2253                 rect.width -= wg;
2254                 rect.height -= wg;
2255                 return rect;
2256         }
2257 }
2258
2259 void listen_enter_notify(node_t *n, bool enable)
2260 {
2261         uint32_t mask = CLIENT_EVENT_MASK | (enable ? XCB_EVENT_MASK_ENTER_WINDOW : 0);
2262         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
2263                 if (f->client == NULL) {
2264                         continue;
2265                 }
2266                 xcb_change_window_attributes(dpy, f->id, XCB_CW_EVENT_MASK, &mask);
2267                 if (f->presel != NULL) {
2268                         xcb_change_window_attributes(dpy, f->presel->feedback, XCB_CW_EVENT_MASK, &mask);
2269                 }
2270         }
2271 }
2272
2273 void regenerate_ids_in(node_t *n)
2274 {
2275         if (n == NULL || n->client != NULL) {
2276                 return;
2277         }
2278         n->id = xcb_generate_id(dpy);
2279         regenerate_ids_in(n->first_child);
2280         regenerate_ids_in(n->second_child);
2281 }
2282
2283 #define DEF_FLAG_COUNT(flag) \
2284         unsigned int flag##_count(node_t *n) \
2285         { \
2286                 if (n == NULL) { \
2287                         return 0; \
2288                 } else { \
2289                         return ((n->flag ? 1 : 0) + \
2290                                 flag##_count(n->first_child) + \
2291                                 flag##_count(n->second_child)); \
2292                 } \
2293         }
2294         DEF_FLAG_COUNT(sticky)
2295         DEF_FLAG_COUNT(private)
2296         DEF_FLAG_COUNT(locked)
2297 #undef DEF_FLAG_COUNT