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