]> git.lizzy.rs Git - bspwm.git/blob - tree.c
Call activate_desktop in activate_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 = calloc(1, 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         activate_desktop(m, d);
477
478         d->focus = n;
479         history_add(m, d, n);
480
481         put_status(SBSC_MASK_REPORT);
482
483         if (n == NULL) {
484                 return true;
485         }
486
487         put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
488
489         return true;
490 }
491
492 void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n)
493 {
494         if (n == NULL) {
495                 return;
496         } else if (n->sticky) {
497                 transfer_node(m, ds, n, m, dd, dd->focus);
498         } else {
499                 /* we need references to the children because n might be freed after
500                  * the first recursive call */
501                 node_t *first_child = n->first_child;
502                 node_t *second_child = n->second_child;
503                 transfer_sticky_nodes(m, ds, dd, first_child);
504                 transfer_sticky_nodes(m, ds, dd, second_child);
505         }
506 }
507
508 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
509 {
510         if (m == NULL) {
511                 m = history_last_monitor(NULL);
512                 if (m == NULL) {
513                         m = mon_head;
514                 }
515         }
516
517         if (m == NULL) {
518                 return false;
519         }
520
521         if (d == NULL) {
522                 d = history_last_desktop(m, NULL);
523                 if (d == NULL) {
524                         d = m->desk_head;
525                 }
526         }
527
528         if (d == NULL) {
529                 return false;
530         }
531
532         if (n == NULL && d->root != NULL) {
533                 n = history_last_node(d, NULL);
534                 if (n == NULL) {
535                         n = first_focusable_leaf(d->root);
536                 }
537         }
538
539         if (n != NULL && !is_focusable(n)) {
540                 return false;
541         }
542
543         if ((mon != NULL && mon->desk != d) || n == NULL || n->client == NULL) {
544                 clear_input_focus();
545         }
546
547         if (m->sticky_count > 0 && d != m->desk) {
548                 sticky_still = false;
549                 transfer_sticky_nodes(m, m->desk, d, m->desk->root);
550                 sticky_still = true;
551                 if (n == NULL && d->focus != NULL) {
552                         n = d->focus;
553                 }
554         }
555
556         if (d->focus != NULL && n != d->focus) {
557                 neutralize_occluding_windows(m, d, n);
558         }
559
560         if (n != NULL && n->client != NULL && n->client->urgent) {
561                 set_urgent(m, d, n, false);
562         }
563
564         if (mon != m) {
565                 if (mon != NULL) {
566                         for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
567                                 draw_border(e->focus, true, false);
568                         }
569                 }
570                 for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
571                         if (e == d) {
572                                 continue;
573                         }
574                         draw_border(e->focus, true, true);
575                 }
576         }
577
578         if (d->focus != n) {
579                 for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
580                         if (f->client != NULL && !is_descendant(f, n)) {
581                                 window_draw_border(f->id, get_border_color(false, true));
582                         }
583                 }
584         }
585
586         draw_border(n, true, true);
587
588         focus_desktop(m, d);
589
590         d->focus = n;
591         ewmh_update_active_window();
592         history_add(m, d, n);
593
594         put_status(SBSC_MASK_REPORT);
595
596         if (n == NULL) {
597                 return true;
598         }
599
600         put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
601
602         stack(d, n, true);
603         set_input_focus(n);
604
605         if (pointer_follows_focus) {
606                 center_pointer(get_rectangle(d, n));
607         }
608
609         return true;
610 }
611
612 void hide_node(desktop_t *d, node_t *n)
613 {
614         if (n == NULL) {
615                 return;
616         } else {
617                 if (!n->hidden) {
618                         if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
619                                 window_hide(n->presel->feedback);
620                         }
621                         if (n->client != NULL) {
622                                 window_hide(n->id);
623                         }
624                 }
625                 if (n->client != NULL) {
626                         n->client->shown = false;
627                 }
628                 hide_node(d, n->first_child);
629                 hide_node(d, n->second_child);
630         }
631 }
632
633 void show_node(desktop_t *d, node_t *n)
634 {
635         if (n == NULL) {
636                 return;
637         } else {
638                 if (!n->hidden) {
639                         if (n->client != NULL) {
640                                 window_show(n->id);
641                         }
642                         if (n->presel != NULL && d->layout != LAYOUT_MONOCLE) {
643                                 window_show(n->presel->feedback);
644                         }
645                 }
646                 if (n->client != NULL) {
647                         n->client->shown = true;
648                 }
649                 show_node(d, n->first_child);
650                 show_node(d, n->second_child);
651         }
652 }
653
654 node_t *make_node(uint32_t id)
655 {
656         if (id == XCB_NONE) {
657                 id = xcb_generate_id(dpy);
658         }
659         node_t *n = calloc(1, sizeof(node_t));
660         n->id = id;
661         n->parent = n->first_child = n->second_child = NULL;
662         n->vacant = n->hidden = n->sticky = n->private = n->locked = false;
663         n->split_ratio = split_ratio;
664         n->split_type = TYPE_VERTICAL;
665         n->birth_rotation = 0;
666         n->presel = NULL;
667         n->client = NULL;
668         return n;
669 }
670
671 client_t *make_client(void)
672 {
673         client_t *c = calloc(1, sizeof(client_t));
674         c->state = c->last_state = STATE_TILED;
675         c->layer = c->last_layer = LAYER_NORMAL;
676         snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
677         snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
678         c->border_width = border_width;
679         c->urgent = false;
680         c->shown = false;
681         c->wm_flags = 0;
682         c->icccm_props.input_hint = true;
683         c->icccm_props.take_focus = false;
684         c->icccm_props.delete_window = false;
685         c->size_hints.flags = 0;
686         return c;
687 }
688
689 void initialize_client(node_t *n)
690 {
691         xcb_window_t win = n->id;
692         client_t *c = n->client;
693         xcb_icccm_get_wm_protocols_reply_t protos;
694         if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protos, NULL) == 1) {
695                 for (uint32_t i = 0; i < protos.atoms_len; i++) {
696                         if (protos.atoms[i] == WM_TAKE_FOCUS) {
697                                 c->icccm_props.take_focus = true;
698                         } else if (protos.atoms[i] == WM_DELETE_WINDOW) {
699                                 c->icccm_props.delete_window = true;
700                         }
701                 }
702                 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
703         }
704         xcb_ewmh_get_atoms_reply_t wm_state;
705         if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
706                 for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
707 #define HANDLE_WM_STATE(s) \
708                         if (wm_state.atoms[i] == ewmh->_NET_WM_STATE_##s) { \
709                                 c->wm_flags |= WM_FLAG_##s; continue; \
710                         }
711                         HANDLE_WM_STATE(MODAL)
712                         HANDLE_WM_STATE(STICKY)
713                         HANDLE_WM_STATE(MAXIMIZED_VERT)
714                         HANDLE_WM_STATE(MAXIMIZED_HORZ)
715                         HANDLE_WM_STATE(SHADED)
716                         HANDLE_WM_STATE(SKIP_TASKBAR)
717                         HANDLE_WM_STATE(SKIP_PAGER)
718                         HANDLE_WM_STATE(HIDDEN)
719                         HANDLE_WM_STATE(FULLSCREEN)
720                         HANDLE_WM_STATE(ABOVE)
721                         HANDLE_WM_STATE(BELOW)
722                         HANDLE_WM_STATE(DEMANDS_ATTENTION)
723 #undef HANDLE_WM_STATE
724                 }
725                 xcb_ewmh_get_atoms_reply_wipe(&wm_state);
726         }
727         xcb_icccm_wm_hints_t hints;
728         if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
729                 && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
730                 c->icccm_props.input_hint = hints.input;
731         }
732         xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &c->size_hints, NULL);
733 }
734
735 bool is_focusable(node_t *n)
736 {
737         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
738                 if (f->client != NULL && !f->hidden) {
739                         return true;
740                 }
741         }
742         return false;
743 }
744
745 bool is_leaf(node_t *n)
746 {
747         return (n != NULL && n->first_child == NULL && n->second_child == NULL);
748 }
749
750 bool is_first_child(node_t *n)
751 {
752         return (n != NULL && n->parent != NULL && n->parent->first_child == n);
753 }
754
755 bool is_second_child(node_t *n)
756 {
757         return (n != NULL && n->parent != NULL && n->parent->second_child == n);
758 }
759
760 unsigned int clients_count_in(node_t *n)
761 {
762         if (n == NULL) {
763                 return 0;
764         } else {
765                 return (n->client != NULL ? 1 : 0) +
766                         clients_count_in(n->first_child) +
767                         clients_count_in(n->second_child);
768         }
769 }
770
771 node_t *brother_tree(node_t *n)
772 {
773         if (n == NULL || n->parent == NULL) {
774                 return NULL;
775         }
776         if (is_first_child(n)) {
777                 return n->parent->second_child;
778         } else {
779                 return n->parent->first_child;
780         }
781 }
782
783 node_t *first_extrema(node_t *n)
784 {
785         if (n == NULL) {
786                 return NULL;
787         } else if (n->first_child == NULL) {
788                 return n;
789         } else {
790                 return first_extrema(n->first_child);
791         }
792 }
793
794 node_t *second_extrema(node_t *n)
795 {
796         if (n == NULL) {
797                 return NULL;
798         } else if (n->second_child == NULL) {
799                 return n;
800         } else {
801                 return second_extrema(n->second_child);
802         }
803 }
804
805 node_t *first_focusable_leaf(node_t *n)
806 {
807         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
808                 if (f->client != NULL && !f->hidden) {
809                         return f;
810                 }
811         }
812         return NULL;
813 }
814
815 node_t *next_leaf(node_t *n, node_t *r)
816 {
817         if (n == NULL) {
818                 return NULL;
819         }
820         node_t *p = n;
821         while (is_second_child(p) && p != r) {
822                 p = p->parent;
823         }
824         if (p == r) {
825                 return NULL;
826         }
827         return first_extrema(p->parent->second_child);
828 }
829
830 node_t *prev_leaf(node_t *n, node_t *r)
831 {
832         if (n == NULL) {
833                 return NULL;
834         }
835         node_t *p = n;
836         while (is_first_child(p) && p != r) {
837                 p = p->parent;
838         }
839         if (p == r) {
840                 return NULL;
841         }
842         return second_extrema(p->parent->first_child);
843 }
844
845 node_t *next_tiled_leaf(node_t *n, node_t *r)
846 {
847         node_t *next = next_leaf(n, r);
848         if (next == NULL || (next->client != NULL && !next->vacant)) {
849                 return next;
850         } else {
851                 return next_tiled_leaf(next, r);
852         }
853 }
854
855 node_t *prev_tiled_leaf(node_t *n, node_t *r)
856 {
857         node_t *prev = prev_leaf(n, r);
858         if (prev == NULL || (prev->client != NULL && !prev->vacant)) {
859                 return prev;
860         } else {
861                 return prev_tiled_leaf(prev, r);
862         }
863 }
864
865 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
866 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
867 {
868         switch (dir) {
869                 case DIR_EAST:
870                         return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
871                         break;
872                 case DIR_SOUTH:
873                         return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
874                         break;
875                 case DIR_WEST:
876                         return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
877                         break;
878                 case DIR_NORTH:
879                         return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
880                         break;
881         }
882         return false;
883 }
884
885 node_t *find_fence(node_t *n, direction_t dir)
886 {
887         node_t *p;
888
889         if (n == NULL) {
890                 return NULL;
891         }
892
893         p = n->parent;
894
895         while (p != NULL) {
896                 if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
897                     (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
898                     (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
899                     (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
900                         return p;
901                 p = p->parent;
902         }
903
904         return NULL;
905 }
906
907 /* returns *true* if *a* is a child of *b* */
908 bool is_child(node_t *a, node_t *b)
909 {
910         if (a == NULL || b == NULL) {
911                 return false;
912         }
913         return (a->parent != NULL && a->parent == b);
914 }
915
916 /* returns *true* if *a* is a descendant of *b* */
917 bool is_descendant(node_t *a, node_t *b)
918 {
919         if (a == NULL || b == NULL) {
920                 return false;
921         }
922         while (a != b && a != NULL) {
923                 a = a->parent;
924         }
925         return a == b;
926 }
927
928 bool find_by_id(uint32_t id, coordinates_t *loc)
929 {
930         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
931                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
932                         node_t *n = find_by_id_in(d->root, id);
933                         if (n != NULL) {
934                                 loc->monitor = m;
935                                 loc->desktop = d;
936                                 loc->node = n;
937                                 return true;
938                         }
939                 }
940         }
941         return false;
942 }
943
944 node_t *find_by_id_in(node_t *r, uint32_t id)
945 {
946         if (r == NULL) {
947                 return NULL;
948         } else if (r->id == id) {
949                 return r;
950         } else {
951                 node_t *f = find_by_id_in(r->first_child, id);
952                 if (f != NULL) {
953                         return f;
954                 } else {
955                         return find_by_id_in(r->second_child, id);
956                 }
957         }
958 }
959
960 void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t sel)
961 {
962         if (ref->node == NULL) {
963                 return;
964         }
965
966         xcb_rectangle_t rect = get_rectangle(ref->desktop, ref->node);
967         double md = DBL_MAX, mr = UINT32_MAX;
968
969         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
970                 desktop_t *d = m->desk;
971                 for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
972                         coordinates_t loc = {m, d, f};
973                         xcb_rectangle_t r = get_rectangle(d, f);
974                         if (f == ref->node ||
975                             f->client == NULL ||
976                             f->hidden ||
977                             !node_matches(&loc, ref, sel) ||
978                             !on_dir_side(rect, r, dir)) {
979                                 continue;
980                         }
981                         double fd = distance_center(rect, r);
982                         uint32_t fr = history_rank(f);
983                         if (fd < md || (fd == md && fr < mr)) {
984                                 md = fd;
985                                 mr = fr;
986                                 *dst = loc;
987                         }
988                 }
989         }
990 }
991
992 unsigned int node_area(desktop_t *d, node_t *n)
993 {
994         if (n == NULL) {
995                 return 0;
996         }
997         return area(get_rectangle(d, n));
998 }
999
1000 int tiled_count(node_t *n)
1001 {
1002         if (n == NULL) {
1003                 return 0;
1004         }
1005         int cnt = 0;
1006         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1007                 if (!f->hidden && f->client != NULL && IS_TILED(f->client)) {
1008                         cnt++;
1009                 }
1010         }
1011         return cnt;
1012 }
1013
1014 void find_biggest(coordinates_t *ref, coordinates_t *dst, node_select_t sel)
1015 {
1016         unsigned int b_area = 0;
1017
1018         for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1019                 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1020                         for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
1021                                 coordinates_t loc = {m, d, f};
1022                                 if (f->client == NULL || f->vacant || !node_matches(&loc, ref, sel)) {
1023                                         continue;
1024                                 }
1025                                 unsigned int f_area = node_area(d, f);
1026                                 if (f_area > b_area) {
1027                                         *dst = loc;
1028                                         b_area = f_area;
1029                                 }
1030                         }
1031                 }
1032         }
1033 }
1034
1035 void rotate_tree(node_t *n, int deg)
1036 {
1037         if (n == NULL || is_leaf(n) || deg == 0) {
1038                 return;
1039         }
1040
1041         node_t *tmp;
1042
1043         if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
1044             (deg == 270 && n->split_type == TYPE_VERTICAL) ||
1045             deg == 180) {
1046                 tmp = n->first_child;
1047                 n->first_child = n->second_child;
1048                 n->second_child = tmp;
1049                 n->split_ratio = 1.0 - n->split_ratio;
1050         }
1051
1052         if (deg != 180) {
1053                 if (n->split_type == TYPE_HORIZONTAL) {
1054                         n->split_type = TYPE_VERTICAL;
1055                 } else if (n->split_type == TYPE_VERTICAL) {
1056                         n->split_type = TYPE_HORIZONTAL;
1057                 }
1058         }
1059
1060         rotate_tree(n->first_child, deg);
1061         rotate_tree(n->second_child, deg);
1062 }
1063
1064 void rotate_brother(node_t *n)
1065 {
1066         rotate_tree(brother_tree(n), n->birth_rotation);
1067 }
1068
1069 void unrotate_tree(node_t *n, int rot)
1070 {
1071         if (rot == 0) {
1072                 return;
1073         }
1074         rotate_tree(n, 360 - rot);
1075 }
1076
1077 void unrotate_brother(node_t *n)
1078 {
1079         unrotate_tree(brother_tree(n), n->birth_rotation);
1080 }
1081
1082 void flip_tree(node_t *n, flip_t flp)
1083 {
1084         if (n == NULL || is_leaf(n)) {
1085                 return;
1086         }
1087
1088         node_t *tmp;
1089
1090         if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
1091             (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
1092                 tmp = n->first_child;
1093                 n->first_child = n->second_child;
1094                 n->second_child = tmp;
1095                 n->split_ratio = 1.0 - n->split_ratio;
1096         }
1097
1098         flip_tree(n->first_child, flp);
1099         flip_tree(n->second_child, flp);
1100 }
1101
1102 void equalize_tree(node_t *n)
1103 {
1104         if (n == NULL || n->vacant) {
1105                 return;
1106         } else {
1107                 n->split_ratio = split_ratio;
1108                 equalize_tree(n->first_child);
1109                 equalize_tree(n->second_child);
1110         }
1111 }
1112
1113 int balance_tree(node_t *n)
1114 {
1115         if (n == NULL || n->vacant) {
1116                 return 0;
1117         } else if (is_leaf(n)) {
1118                 return 1;
1119         } else {
1120                 int b1 = balance_tree(n->first_child);
1121                 int b2 = balance_tree(n->second_child);
1122                 int b = b1 + b2;
1123                 if (b1 > 0 && b2 > 0) {
1124                         n->split_ratio = (double) b1 / b;
1125                 }
1126                 return b;
1127         }
1128 }
1129
1130 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
1131 {
1132         if (d == NULL || n == NULL) {
1133                 return;
1134         }
1135
1136         node_t *p = n->parent;
1137
1138         if (p == NULL) {
1139                 d->root = NULL;
1140                 d->focus = NULL;
1141         } else {
1142                 if (d->focus == p || is_descendant(d->focus, n)) {
1143                         d->focus = NULL;
1144                 }
1145
1146                 history_remove(d, p, false);
1147                 cancel_presel(m, d, p);
1148                 if (p->sticky) {
1149                         m->sticky_count--;
1150                 }
1151
1152                 node_t *b = brother_tree(n);
1153                 node_t *g = p->parent;
1154
1155                 if (!n->vacant) {
1156                         unrotate_tree(b, n->birth_rotation);
1157                 }
1158
1159                 b->parent = g;
1160
1161                 if (g != NULL) {
1162                         if (is_first_child(p)) {
1163                                 g->first_child = b;
1164                         } else {
1165                                 g->second_child = b;
1166                         }
1167                 } else {
1168                         d->root = b;
1169                 }
1170
1171                 b->birth_rotation = p->birth_rotation;
1172
1173                 free(p);
1174                 n->parent = NULL;
1175
1176                 propagate_flags_upward(m, d, b);
1177         }
1178 }
1179
1180 void close_node(node_t *n)
1181 {
1182         if (n == NULL) {
1183                 return;
1184         } else if (n->client != NULL) {
1185                 if (n->client->icccm_props.delete_window) {
1186                         send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
1187                 } else {
1188                         xcb_kill_client(dpy, n->id);
1189                 }
1190         } else {
1191                 close_node(n->first_child);
1192                 close_node(n->second_child);
1193         }
1194 }
1195
1196 void kill_node(monitor_t *m, desktop_t *d, node_t *n)
1197 {
1198         if (n == NULL) {
1199                 return;
1200         }
1201
1202         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1203                 if (f->client != NULL) {
1204                         xcb_kill_client(dpy, f->id);
1205                 }
1206         }
1207
1208         remove_node(m, d, n);
1209 }
1210
1211 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
1212 {
1213         if (n == NULL) {
1214                 return;
1215         }
1216
1217         unlink_node(m, d, n);
1218         history_remove(d, n, true);
1219         remove_stack_node(n);
1220         cancel_presel_in(m, d, n);
1221         if (m->sticky_count > 0) {
1222                 m->sticky_count -= sticky_count(n);
1223         }
1224         clients_count -= clients_count_in(n);
1225         if (is_descendant(grabbed_node, n)) {
1226                 grabbed_node = NULL;
1227         }
1228         free_node(n);
1229
1230         ewmh_update_client_list(false);
1231         ewmh_update_client_list(true);
1232
1233         if (mon != NULL && d->focus == NULL) {
1234                 if (d == mon->desk) {
1235                         focus_node(m, d, NULL);
1236                 } else {
1237                         activate_node(m, d, NULL);
1238                 }
1239         }
1240 }
1241
1242 void free_node(node_t *n)
1243 {
1244         if (n == NULL) {
1245                 return;
1246         }
1247         node_t *first_child = n->first_child;
1248         node_t *second_child = n->second_child;
1249         free(n->client);
1250         free(n);
1251         free_node(first_child);
1252         free_node(second_child);
1253 }
1254
1255 bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
1256 {
1257         if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
1258             (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
1259                           (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
1260                 return false;
1261         }
1262
1263         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);
1264
1265         node_t *pn1 = n1->parent;
1266         node_t *pn2 = n2->parent;
1267         bool n1_first_child = is_first_child(n1);
1268         bool n2_first_child = is_first_child(n2);
1269         int br1 = n1->birth_rotation;
1270         int br2 = n2->birth_rotation;
1271         bool n1_held_focus = is_descendant(d1->focus, n1);
1272         bool n2_held_focus = is_descendant(d2->focus, n2);
1273         node_t *last_d1_focus = d1->focus;
1274         node_t *last_d2_focus = d2->focus;
1275
1276         if (pn1 != NULL) {
1277                 if (n1_first_child) {
1278                         pn1->first_child = n2;
1279                 } else {
1280                         pn1->second_child = n2;
1281                 }
1282         }
1283
1284         if (pn2 != NULL) {
1285                 if (n2_first_child) {
1286                         pn2->first_child = n1;
1287                 } else {
1288                         pn2->second_child = n1;
1289                 }
1290         }
1291
1292         n1->parent = pn2;
1293         n2->parent = pn1;
1294         n1->birth_rotation = br2;
1295         n2->birth_rotation = br1;
1296
1297         propagate_flags_upward(m2, d2, n1);
1298         propagate_flags_upward(m1, d1, n2);
1299
1300         if (d1 != d2) {
1301                 if (d1->root == n1) {
1302                         d1->root = n2;
1303                 }
1304
1305                 if (d2->root == n2) {
1306                         d2->root = n1;
1307                 }
1308
1309                 if (n1_held_focus) {
1310                         d1->focus = n2_held_focus ? last_d2_focus : n2;
1311                 }
1312
1313                 if (n2_held_focus) {
1314                         d2->focus = n1_held_focus ? last_d1_focus : n1;
1315                 }
1316
1317                 if (m1 != m2) {
1318                         adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
1319                         adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
1320                 }
1321
1322                 ewmh_set_wm_desktop(n1, d2);
1323                 ewmh_set_wm_desktop(n2, d1);
1324
1325                 history_swap_nodes(m1, d1, n1, m2, d2, n2);
1326
1327                 if (m1->desk != d1 && m2->desk == d2) {
1328                         show_node(d2, n1);
1329                         hide_node(d2, n2);
1330                 } else if (m1->desk == d1 && m2->desk != d2) {
1331                         hide_node(d1, n1);
1332                         show_node(d1, n2);
1333                 }
1334
1335                 if (n1_held_focus) {
1336                         if (d1 == mon->desk) {
1337                                 focus_node(m1, d1, d1->focus);
1338                         } else {
1339                                 activate_node(m1, d1, d1->focus);
1340                         }
1341                 } else {
1342                         draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
1343                 }
1344
1345                 if (n2_held_focus) {
1346                         if (d2 == mon->desk) {
1347                                 focus_node(m2, d2, d2->focus);
1348                         } else {
1349                                 activate_node(m2, d2, d2->focus);
1350                         }
1351                 } else {
1352                         draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
1353                 }
1354         }
1355
1356         arrange(m1, d1);
1357
1358         if (d1 != d2) {
1359                 arrange(m2, d2);
1360         }
1361
1362         return true;
1363 }
1364
1365 bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
1366 {
1367         if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
1368                 return false;
1369         }
1370
1371         if (sticky_still && ds != dd && ms->sticky_count > 0 && sticky_count(ns) > 0) {
1372                 return false;
1373         }
1374
1375         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);
1376
1377         bool held_focus = is_descendant(ds->focus, ns);
1378         /* avoid ending up with a dangling pointer (because of unlink_node) */
1379         node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
1380
1381         if (held_focus && ds == mon->desk) {
1382                 clear_input_focus();
1383         }
1384
1385         unlink_node(ms, ds, ns);
1386         insert_node(md, dd, ns, nd);
1387
1388         if (md != ms && (ns->client == NULL || monitor_from_client(ns->client) != md)) {
1389                 adapt_geometry(&ms->rectangle, &md->rectangle, ns);
1390         }
1391
1392         if (ds != dd) {
1393                 ewmh_set_wm_desktop(ns, dd);
1394                 if (sticky_still) {
1395                         if (ds == ms->desk && dd != md->desk) {
1396                                 hide_node(ds, ns);
1397                         } else if (ds != ms->desk && dd == md->desk) {
1398                                 show_node(dd, ns);
1399                         }
1400                 }
1401         }
1402
1403         history_transfer_node(md, dd, ns);
1404         stack(dd, ns, false);
1405
1406         if (ds == dd) {
1407                 if (held_focus) {
1408                         if (ds == mon->desk) {
1409                                 focus_node(ms, ds, last_ds_focus);
1410                         } else {
1411                                 activate_node(ms, ds, last_ds_focus);
1412                         }
1413                 } else {
1414                         draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
1415                 }
1416         } else {
1417                 if (held_focus) {
1418                         if (ds == mon->desk) {
1419                                 focus_node(ms, ds, ds->focus);
1420                         } else {
1421                                 activate_node(ms, ds, ds->focus);
1422                         }
1423                 }
1424                 if (dd->focus == ns) {
1425                         if (dd == mon->desk) {
1426                                 focus_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1427                         } else {
1428                                 activate_node(md, dd, held_focus ? last_ds_focus : dd->focus);
1429                         }
1430                 } else {
1431                         draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
1432                 }
1433         }
1434
1435         arrange(ms, ds);
1436
1437         if (ds != dd) {
1438                 arrange(md, dd);
1439         }
1440
1441         return true;
1442 }
1443
1444 bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t sel)
1445 {
1446         if (ref->node == NULL) {
1447                 return false;
1448         }
1449
1450         monitor_t *m = ref->monitor;
1451         desktop_t *d = ref->desktop;
1452         node_t *n = ref->node;
1453
1454         node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
1455
1456 #define HANDLE_BOUNDARIES(f)  \
1457         while (f == NULL) { \
1458                 d = (dir == CYCLE_PREV ? d->prev : d->next); \
1459                 if (d == NULL) { \
1460                         m = (dir == CYCLE_PREV ? m->prev : m->next); \
1461                         if (m == NULL) { \
1462                                 m = (dir == CYCLE_PREV ? mon_tail : mon_head); \
1463                         } \
1464                         d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
1465                 } \
1466                 f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
1467         }
1468         HANDLE_BOUNDARIES(f);
1469
1470         while (f != n) {
1471                 coordinates_t loc = {m, d, f};
1472                 if (f->client != NULL && !f->hidden && node_matches(&loc, ref, sel)) {
1473                         *dst = loc;
1474                         return true;
1475                 }
1476                 f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
1477                 HANDLE_BOUNDARIES(f);
1478         }
1479 #undef HANDLE_EXTREMUM
1480         return false;
1481 }
1482
1483 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
1484 {
1485         if (n == NULL || d->focus == NULL || is_leaf(n)) {
1486                 return;
1487         }
1488         node_t *p = d->focus->parent;
1489         bool focus_first_child = is_first_child(d->focus);
1490         if (dir == CIRCULATE_FORWARD) {
1491                 node_t *e = second_extrema(n);
1492                 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1493                         e = prev_leaf(e, n);
1494                 }
1495                 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)) {
1496                         swap_nodes(m, d, f, m, d, s);
1497                 }
1498         } else {
1499                 node_t *e = first_extrema(n);
1500                 while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
1501                         e = next_leaf(e, n);
1502                 }
1503                 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)) {
1504                         swap_nodes(m, d, f, m, d, s);
1505                 }
1506         }
1507         if (focus_first_child) {
1508                 focus_node(m, d, p->first_child);
1509         } else {
1510                 focus_node(m, d, p->second_child);
1511         }
1512 }
1513
1514 void set_vacant(monitor_t *m, desktop_t *d, node_t *n, bool value)
1515 {
1516         if (n->vacant == value) {
1517                 return;
1518         }
1519
1520         propagate_vacant_downward(m, d, n, value);
1521         propagate_vacant_upward(m, d, n);
1522 }
1523
1524 void set_vacant_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1525 {
1526         if (n->vacant == value) {
1527                 return;
1528         }
1529
1530         n->vacant = value;
1531
1532         if (value) {
1533                 unrotate_brother(n);
1534                 cancel_presel(m, d, n);
1535         } else {
1536                 rotate_brother(n);
1537         }
1538 }
1539
1540 void propagate_vacant_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1541 {
1542         if (n == NULL) {
1543                 return;
1544         }
1545
1546         set_vacant_local(m, d, n, value);
1547
1548         propagate_vacant_downward(m, d, n->first_child, value);
1549         propagate_vacant_downward(m, d, n->second_child, value);
1550 }
1551
1552 void propagate_vacant_upward(monitor_t *m, desktop_t *d, node_t *n)
1553 {
1554         if (n == NULL) {
1555                 return;
1556         }
1557
1558         node_t *p = n->parent;
1559
1560         if (p != NULL) {
1561                 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1562         }
1563
1564         propagate_vacant_upward(m, d, p);
1565 }
1566
1567 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
1568 {
1569         if (n == NULL || n->client->layer == l) {
1570                 return false;
1571         }
1572
1573         n->client->last_layer = n->client->layer;
1574         n->client->layer = l;
1575
1576         if (l == LAYER_ABOVE) {
1577                 n->client->wm_flags |= WM_FLAG_ABOVE;
1578                 n->client->wm_flags &= ~WM_FLAG_BELOW;
1579         } else if (l == LAYER_BELOW) {
1580                 n->client->wm_flags |= WM_FLAG_BELOW;
1581                 n->client->wm_flags &= ~WM_FLAG_ABOVE;
1582         } else {
1583                 n->client->wm_flags &= ~(WM_FLAG_ABOVE | WM_FLAG_BELOW);
1584         }
1585
1586         ewmh_wm_state_update(n);
1587
1588         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));
1589
1590         if (d->focus == n) {
1591                 neutralize_occluding_windows(m, d, n);
1592         }
1593
1594         stack(d, n, (d->focus == n));
1595
1596         return true;
1597 }
1598
1599 bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
1600 {
1601         if (n == NULL || n->client == NULL || n->client->state == s) {
1602                 return false;
1603         }
1604
1605         client_t *c = n->client;
1606
1607         c->last_state = c->state;
1608         c->state = s;
1609
1610         switch (c->last_state) {
1611                 case STATE_TILED:
1612                 case STATE_PSEUDO_TILED:
1613                         break;
1614                 case STATE_FLOATING:
1615                         set_floating(m, d, n, false);
1616                         break;
1617                 case STATE_FULLSCREEN:
1618                         set_fullscreen(m, d, n, false);
1619                         break;
1620         }
1621
1622         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));
1623
1624         switch (c->state) {
1625                 case STATE_TILED:
1626                 case STATE_PSEUDO_TILED:
1627                         break;
1628                 case STATE_FLOATING:
1629                         set_floating(m, d, n, true);
1630                         break;
1631                 case STATE_FULLSCREEN:
1632                         set_fullscreen(m, d, n, true);
1633                         break;
1634         }
1635
1636         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));
1637
1638         if (n == m->desk->focus) {
1639                 put_status(SBSC_MASK_REPORT);
1640         }
1641
1642         return true;
1643 }
1644
1645 void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
1646 {
1647         if (n == NULL) {
1648                 return;
1649         }
1650
1651         cancel_presel(m, d, n);
1652         set_vacant(m, d, n, value);
1653
1654         if (!value && d->focus == n) {
1655                 neutralize_occluding_windows(m, d, n);
1656         }
1657
1658         stack(d, n, (d->focus == n));
1659 }
1660
1661 void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
1662 {
1663         if (n == NULL) {
1664                 return;
1665         }
1666
1667         client_t *c = n->client;
1668
1669         cancel_presel(m, d, n);
1670         set_vacant(m, d, n, value);
1671
1672         if (value) {
1673                 c->wm_flags |= WM_FLAG_FULLSCREEN;
1674                 c->last_layer = c->layer;
1675                 c->layer = LAYER_ABOVE;
1676         } else {
1677                 c->wm_flags &= ~WM_FLAG_FULLSCREEN;
1678                 c->layer = c->last_layer;
1679                 if (d->focus == n) {
1680                         neutralize_occluding_windows(m, d, n);
1681                 }
1682         }
1683
1684         ewmh_wm_state_update(n);
1685         stack(d, n, (d->focus == n));
1686 }
1687
1688 void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
1689 {
1690         bool changed = false;
1691         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1692                 for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
1693                         if (a != f && a->client != NULL && f->client != NULL &&
1694                             IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
1695                                 set_state(m, d, a, a->client->last_state);
1696                                 changed = true;
1697                         }
1698                 }
1699         }
1700         if (changed) {
1701                 arrange(m, d);
1702         }
1703 }
1704
1705 void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
1706 {
1707         if (n == NULL) {
1708                 return;
1709         }
1710
1711         node_t *p = n->parent;
1712
1713         if (p != NULL) {
1714                 set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
1715                 set_hidden_local(m, d, p, (p->first_child->hidden && p->second_child->hidden));
1716         }
1717
1718         propagate_flags_upward(m, d, p);
1719 }
1720
1721 void set_hidden(monitor_t *m, desktop_t *d, node_t *n, bool value)
1722 {
1723         if (n == NULL || n->hidden == value) {
1724                 return;
1725         }
1726
1727
1728         if (focus_follows_pointer) {
1729                 listen_enter_notify(d->root, false);
1730         }
1731
1732         bool held_focus = is_descendant(d->focus, n);
1733
1734         propagate_hidden_downward(m, d, n, value);
1735         propagate_hidden_upward(m, d, n);
1736
1737         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));
1738
1739         if (held_focus || d->focus == NULL) {
1740                 if (d->focus != NULL) {
1741                         d->focus = NULL;
1742                         draw_border(n, false, (mon == m));
1743                 }
1744                 if (d == mon->desk) {
1745                         focus_node(m, d, d->focus);
1746                 } else {
1747                         activate_node(m, d, d->focus);
1748                 }
1749         }
1750
1751         if (focus_follows_pointer) {
1752                 listen_enter_notify(d->root, true);
1753         }
1754 }
1755
1756 void set_hidden_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
1757 {
1758         if (n->hidden == value) {
1759                 return;
1760         }
1761
1762         n->hidden = value;
1763
1764         if (n->client != NULL) {
1765                 if (n->client->shown) {
1766                         window_set_visibility(n->id, !value);
1767                 }
1768
1769                 if (IS_TILED(n->client)) {
1770                         set_vacant(m, d, n, value);
1771                 }
1772
1773                 if (value) {
1774                         n->client->wm_flags |= WM_FLAG_HIDDEN;
1775                 } else {
1776                         n->client->wm_flags &= ~WM_FLAG_HIDDEN;
1777                 }
1778
1779                 ewmh_wm_state_update(n);
1780         }
1781 }
1782
1783 void propagate_hidden_downward(monitor_t *m, desktop_t *d, node_t *n, bool value)
1784 {
1785         if (n == NULL) {
1786                 return;
1787         }
1788
1789         set_hidden_local(m, d, n, value);
1790
1791         propagate_hidden_downward(m, d, n->first_child, value);
1792         propagate_hidden_downward(m, d, n->second_child, value);
1793 }
1794
1795 void propagate_hidden_upward(monitor_t *m, desktop_t *d, node_t *n)
1796 {
1797         if (n == NULL) {
1798                 return;
1799         }
1800
1801         node_t *p = n->parent;
1802
1803         if (p != NULL) {
1804                 set_hidden_local(m, d, p, p->first_child->hidden && p->second_child->hidden);
1805         }
1806
1807         propagate_hidden_upward(m, d, p);
1808 }
1809
1810 void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
1811 {
1812         if (n == NULL || n->sticky == value) {
1813                 return;
1814         }
1815
1816         if (d != m->desk) {
1817                 transfer_node(m, d, n, m, m->desk, m->desk->focus);
1818         }
1819
1820         n->sticky = value;
1821
1822         if (value) {
1823                 m->sticky_count++;
1824         } else {
1825                 m->sticky_count--;
1826         }
1827
1828         if (n->client != NULL) {
1829                 if (value) {
1830                         n->client->wm_flags |= WM_FLAG_STICKY;
1831                 } else {
1832                         n->client->wm_flags &= ~WM_FLAG_STICKY;
1833                 }
1834                 ewmh_wm_state_update(n);
1835         }
1836
1837         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));
1838
1839         if (n == m->desk->focus) {
1840                 put_status(SBSC_MASK_REPORT);
1841         }
1842 }
1843
1844 void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
1845 {
1846         if (n == NULL || n->private == value) {
1847                 return;
1848         }
1849
1850         n->private = value;
1851
1852         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));
1853
1854         if (n == m->desk->focus) {
1855                 put_status(SBSC_MASK_REPORT);
1856         }
1857 }
1858
1859 void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
1860 {
1861         if (n == NULL || n->locked == value) {
1862                 return;
1863         }
1864
1865         n->locked = value;
1866
1867         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));
1868
1869         if (n == m->desk->focus) {
1870                 put_status(SBSC_MASK_REPORT);
1871         }
1872 }
1873
1874 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
1875 {
1876         if (value && mon->desk->focus == n) {
1877                 return;
1878         }
1879
1880         n->client->urgent = value;
1881
1882         if (value) {
1883                 n->client->wm_flags |= WM_FLAG_DEMANDS_ATTENTION;
1884         } else {
1885                 n->client->wm_flags &= ~WM_FLAG_DEMANDS_ATTENTION;
1886         }
1887
1888         ewmh_wm_state_update(n);
1889
1890         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));
1891         put_status(SBSC_MASK_REPORT);
1892 }
1893
1894 /* Returns true if a contains b */
1895 bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
1896 {
1897         return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width) &&
1898                 a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
1899 }
1900
1901 xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
1902 {
1903         client_t *c = n->client;
1904         if (c != NULL) {
1905                 if (IS_FLOATING(c)) {
1906                         return c->floating_rectangle;
1907                 } else {
1908                         return c->tiled_rectangle;
1909                 }
1910         } else {
1911                 int wg = (d == NULL ? 0 : (gapless_monocle && IS_MONOCLE(d) ? 0 : d->window_gap));
1912                 xcb_rectangle_t rect = n->rectangle;
1913                 rect.width -= wg;
1914                 rect.height -= wg;
1915                 return rect;
1916         }
1917 }
1918
1919 void listen_enter_notify(node_t *n, bool enable)
1920 {
1921         uint32_t mask = CLIENT_EVENT_MASK | (enable ? XCB_EVENT_MASK_ENTER_WINDOW : 0);
1922         for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
1923                 if (f->client == NULL) {
1924                         continue;
1925                 }
1926                 xcb_change_window_attributes(dpy, f->id, XCB_CW_EVENT_MASK, &mask);
1927                 if (f->presel != NULL) {
1928                         xcb_change_window_attributes(dpy, f->presel->feedback, XCB_CW_EVENT_MASK, &mask);
1929                 }
1930         }
1931 }
1932
1933 #define DEF_FLAG_COUNT(flag) \
1934         unsigned int flag##_count(node_t *n) \
1935         { \
1936                 if (n == NULL) { \
1937                         return 0; \
1938                 } else { \
1939                         return ((n->flag ? 1 : 0) + \
1940                                 flag##_count(n->first_child) + \
1941                                 flag##_count(n->second_child)); \
1942                 } \
1943         }
1944         DEF_FLAG_COUNT(sticky)
1945         DEF_FLAG_COUNT(private)
1946         DEF_FLAG_COUNT(locked)
1947 #undef DEF_FLAG_COUNT