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