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