]> git.lizzy.rs Git - bspwm.git/blobdiff - tree.c
Transfer nodes moved via configure requests
[bspwm.git] / tree.c
diff --git a/tree.c b/tree.c
index c26810697cf1a30ffc77cd113f404fb387d8fda7..10d947125d4817c0c6ede2886f684d605342f98b 100644 (file)
--- a/tree.c
+++ b/tree.c
-/* * Copyright (c) 2012-2013 Bastien Dejean
+/* Copyright (c) 2012, Bastien Dejean
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
  *
- *  * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
  *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
 #include <float.h>
 #include <limits.h>
-#include <math.h>
 #include "bspwm.h"
 #include "desktop.h"
 #include "ewmh.h"
 #include "history.h"
 #include "monitor.h"
 #include "query.h"
+#include "geometry.h"
+#include "subscribe.h"
 #include "settings.h"
 #include "stack.h"
-#include "tag.h"
 #include "window.h"
 #include "tree.h"
 
 void arrange(monitor_t *m, desktop_t *d)
 {
-    if (d->root == NULL)
-        return;
+       if (d->root == NULL) {
+               return;
+       }
 
-    PRINTF("arrange %s %s\n", m->name, d->name);
+       layout_t last_layout = d->layout;
 
-    xcb_rectangle_t rect = m->rectangle;
-    int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
-    rect.x += m->left_padding + wg;
-    rect.y += m->top_padding + wg;
-    rect.width -= m->left_padding + m->right_padding + wg;
-    rect.height -= m->top_padding + m->bottom_padding + wg;
-    apply_layout(m, d, d->root, rect, rect);
-}
+       if (single_monocle && tiled_count(d->root) == 1) {
+               d->layout = LAYOUT_MONOCLE;
+       }
 
-void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
-{
-    if (n == NULL)
-        return;
+       xcb_rectangle_t rect = m->rectangle;
 
-    n->rectangle = rect;
+       if (!paddingless_monocle || d->layout != LAYOUT_MONOCLE) {
+               rect.x += m->padding.left + d->padding.left;
+               rect.y += m->padding.top + d->padding.top;
+               rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
+               rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
+       }
 
-    if (is_leaf(n)) {
+       if (!gapless_monocle || d->layout != LAYOUT_MONOCLE) {
+               rect.x += d->window_gap;
+               rect.y += d->window_gap;
+               rect.width -= d->window_gap;
+               rect.height -= d->window_gap;
+       }
 
-        if (is_floating(n->client) && n->client->border_width != d->border_width) {
-            int ds = 2 * (d->border_width - n->client->border_width);
-            n->client->floating_rectangle.width += ds;
-            n->client->floating_rectangle.height += ds;
-        }
-
-        if ((borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
-                || n->client->fullscreen
-                || n->client->frame)
-            n->client->border_width = 0;
-        else
-            n->client->border_width = d->border_width;
-
-        xcb_rectangle_t r;
-        if (!n->client->fullscreen) {
-            if (!n->client->floating) {
-                /* tiled clients */
-                if (d->layout == LAYOUT_TILED)
-                    r = rect;
-                else if (d->layout == LAYOUT_MONOCLE)
-                    r = root_rect;
-                else
-                    return;
-                int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
-                int bleed = wg + 2 * n->client->border_width;
-                r.width = (bleed < r.width ? r.width - bleed : 1);
-                r.height = (bleed < r.height ? r.height - bleed : 1);
-                n->client->tiled_rectangle = r;
-            } else {
-                /* floating clients */
-                r = n->client->floating_rectangle;
-            }
-        } else {
-            /* fullscreen clients */
-            r = m->rectangle;
-        }
+       apply_layout(m, d, d->root, rect, rect);
 
-        window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
-        window_border_width(n->client->window, n->client->border_width);
-        window_draw_border(n, d->focus == n, m == mon);
-
-    } else {
-        xcb_rectangle_t first_rect;
-        xcb_rectangle_t second_rect;
+       d->layout = last_layout;
+}
 
-        if (n->first_child->vacant || n->second_child->vacant) {
-            first_rect = second_rect = rect;
-        } else {
-            unsigned int fence;
-            if (n->split_type == TYPE_VERTICAL) {
-                fence = rect.width * n->split_ratio;
-                first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
-                second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
-            } else if (n->split_type == TYPE_HORIZONTAL) {
-                fence = rect.height * n->split_ratio;
-                first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
-                second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
-            }
-        }
+void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
+{
+       if (n == NULL) {
+               return;
+       }
+
+       n->rectangle = rect;
+
+       if (pointer_follows_focus && mon->desk->focus == n && frozen_pointer->action == ACTION_NONE) {
+               xcb_rectangle_t r = rect;
+               r.width -= d->window_gap;
+               r.height -= d->window_gap;
+               center_pointer(r);
+       }
+
+       if (n->presel != NULL) {
+               draw_presel_feedback(m, d, n);
+       }
+
+       if (is_leaf(n)) {
+
+               if (n->client == NULL) {
+                       return;
+               }
+
+               unsigned int bw;
+               if ((borderless_monocle && n->client->state == STATE_TILED && d->layout == LAYOUT_MONOCLE)
+                   || n->client->state == STATE_FULLSCREEN) {
+                       bw = 0;
+               } else {
+                       bw = n->client->border_width;
+               }
+
+               xcb_rectangle_t r;
+               xcb_rectangle_t cr = get_rectangle(d, n);
+               client_state_t s = n->client->state;
+               if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
+                       int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
+                       /* tiled clients */
+                       if (s == STATE_TILED) {
+                               r = rect;
+                               int bleed = wg + 2 * bw;
+                               r.width = (bleed < r.width ? r.width - bleed : 1);
+                               r.height = (bleed < r.height ? r.height - bleed : 1);
+                       /* pseudo-tiled clients */
+                       } else {
+                               r = n->client->floating_rectangle;
+                               if (center_pseudo_tiled) {
+                                       r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
+                                       r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
+                               } else {
+                                       r.x = rect.x;
+                                       r.y = rect.y;
+                               }
+                       }
+                       n->client->tiled_rectangle = r;
+               /* floating clients */
+               } else if (s == STATE_FLOATING) {
+                       r = n->client->floating_rectangle;
+               /* fullscreen clients */
+               } else {
+                       r = m->rectangle;
+               }
+
+               if (!rect_eq(r, cr)) {
+                       window_move_resize(n->id, r.x, r.y, r.width, r.height);
+                       if (frozen_pointer->action == ACTION_NONE) {
+                               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);
+                       }
+               }
+
+               window_border_width(n->id, bw);
+
+       } else {
+               xcb_rectangle_t first_rect;
+               xcb_rectangle_t second_rect;
+
+               if (d->layout == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
+                       first_rect = second_rect = rect;
+               } else {
+                       unsigned int fence;
+                       if (n->split_type == TYPE_VERTICAL) {
+                               fence = rect.width * n->split_ratio;
+                               first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
+                               second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
+                       } else {
+                               fence = rect.height * n->split_ratio;
+                               first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
+                               second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
+                       }
+               }
+
+               apply_layout(m, d, n->first_child, first_rect, root_rect);
+               apply_layout(m, d, n->second_child, second_rect, root_rect);
+       }
+}
 
-        apply_layout(m, d, n->first_child, first_rect, root_rect);
-        apply_layout(m, d, n->second_child, second_rect, root_rect);
-    }
+presel_t *make_presel(void)
+{
+       presel_t *p = malloc(sizeof(presel_t));
+       p->split_dir = DIR_EAST;
+       p->split_ratio = split_ratio;
+       p->feedback = XCB_NONE;
+       return p;
 }
 
-void insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
+void set_ratio(node_t *n, double rat)
 {
-    if (d == NULL || n == NULL)
-        return;
+       if (n == NULL) {
+               return;
+       }
 
-    PRINTF("insert node %X\n", n->client->window);
+       n->split_ratio = rat;
+}
 
-    /* n: new leaf node */
-    /* c: new container node */
-    /* f: focus or insertion anchor */
-    /* p: parent of focus */
-    /* g: grand parent of focus */
+void presel_dir(monitor_t *m, desktop_t *d, node_t *n, direction_t dir)
+{
+       if (n->presel == NULL) {
+               n->presel = make_presel();
+       }
 
-    if (f == NULL)
-        f = d->root;
+       n->presel->split_dir = dir;
 
-    if (f == NULL) {
-        d->root = n;
-    } else {
-        node_t *c = make_node();
-        node_t *p = f->parent;
-        if (p != NULL && f->split_mode == MODE_AUTOMATIC
-                && (p->first_child->vacant || p->second_child->vacant)) {
-            f = p;
-            p = f->parent;
-        }
-        n->parent = c;
-        c->birth_rotation = f->birth_rotation;
-        switch (f->split_mode) {
-            case MODE_AUTOMATIC:
-                if (p == NULL) {
-                    c->first_child = n;
-                    c->second_child = f;
-                    if (m->rectangle.width > m->rectangle.height)
-                        c->split_type = TYPE_VERTICAL;
-                    else
-                        c->split_type = TYPE_HORIZONTAL;
-                    f->parent = c;
-                    d->root = c;
-                } else {
-                    node_t *g = p->parent;
-                    c->parent = g;
-                    if (g != NULL) {
-                        if (is_first_child(p))
-                            g->first_child = c;
-                        else
-                            g->second_child = c;
-                    } else {
-                        d->root = c;
-                    }
-                    c->split_type = p->split_type;
-                    c->split_ratio = p->split_ratio;
-                    p->parent = c;
-                    int rot;
-                    if (is_first_child(f)) {
-                        c->first_child = n;
-                        c->second_child = p;
-                        rot = 90;
-                    } else {
-                        c->first_child = p;
-                        c->second_child = n;
-                        rot = 270;
-                    }
-                    if (!is_floating(n->client))
-                        rotate_tree(p, rot);
-                    n->birth_rotation = rot;
-                }
-                break;
-            case MODE_MANUAL:
-                if (p != NULL) {
-                    if (is_first_child(f))
-                        p->first_child = c;
-                    else
-                        p->second_child = c;
-                }
-                c->split_ratio = f->split_ratio;
-                c->parent = p;
-                f->parent = c;
-                f->birth_rotation = 0;
-                switch (f->split_dir) {
-                    case DIR_LEFT:
-                        c->split_type = TYPE_VERTICAL;
-                        c->first_child = n;
-                        c->second_child = f;
-                        break;
-                    case DIR_RIGHT:
-                        c->split_type = TYPE_VERTICAL;
-                        c->first_child = f;
-                        c->second_child = n;
-                        break;
-                    case DIR_UP:
-                        c->split_type = TYPE_HORIZONTAL;
-                        c->first_child = n;
-                        c->second_child = f;
-                        break;
-                    case DIR_DOWN:
-                        c->split_type = TYPE_HORIZONTAL;
-                        c->first_child = f;
-                        c->second_child = n;
-                        break;
-                }
-                if (d->root == f)
-                    d->root = c;
-                f->split_mode = MODE_AUTOMATIC;
-                break;
-        }
-        if (f->vacant)
-            update_vacant_state(p);
-    }
-    if (d->focus == NULL && is_visible(d, n))
-        d->focus = n;
-    if (n->client->sticky)
-        m->num_sticky++;
-    put_status();
+       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));
 }
 
-void pseudo_focus(desktop_t *d, node_t *n)
+void presel_ratio(monitor_t *m, desktop_t *d, node_t *n, double ratio)
 {
-    d->focus = n;
-    if (n != NULL)
-        stack(n);
-}
+       if (n->presel == NULL) {
+               n->presel = make_presel();
+       }
 
-void focus_node(monitor_t *m, desktop_t *d, node_t *n)
-{
-    if (mon->desk != d || n == NULL)
-        clear_input_focus();
+       n->presel->split_ratio = ratio;
 
-    if (m->num_sticky > 0 && d != m->desk) {
-        node_t *a = first_extrema(m->desk->root);
-        sticky_still = false;
-        while (a != NULL) {
-            node_t *b = next_leaf(a, m->desk->root);
-            if (a->client->sticky)
-                transfer_node(m, m->desk, a, m, d, d->focus);
-            a = b;
-        }
-        sticky_still = true;
-        if (n == NULL && d->focus != NULL && is_visible(d, d->focus))
-            n = d->focus;
-    }
-
-    if (n != NULL && d->focus != NULL && n != d->focus && d->focus->client->fullscreen) {
-        set_fullscreen(d->focus, false);
-        arrange(m, d);
-    }
-
-    if (mon != m) {
-        for (desktop_t *cd = mon->desk_head; cd != NULL; cd = cd->next)
-            window_draw_border(cd->focus, true, false);
-        for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next)
-            if (cd != d)
-                window_draw_border(cd->focus, true, true);
-        if (d->focus == n)
-            window_draw_border(n, true, true);
-    }
+       put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X ratio %lf\n", m->id, d->id, n->id, ratio);
+}
 
-    if (d->focus != n) {
-        window_draw_border(d->focus, false, true);
-        window_draw_border(n, true, true);
-    }
+void cancel_presel(monitor_t *m, desktop_t *d, node_t *n)
+{
+       if (n->presel == NULL) {
+               return;
+       }
 
-    focus_desktop(m, d);
-    pseudo_focus(d, n);
+       if (n->presel->feedback != XCB_NONE) {
+               xcb_destroy_window(dpy, n->presel->feedback);
+       }
 
-    if (n == NULL) {
-        history_add(m, d, NULL);
-        ewmh_update_active_window();
-        return;
-    }
+       free(n->presel);
+       n->presel = NULL;
 
-    PRINTF("focus node %X\n", n->client->window);
+       put_status(SBSC_MASK_NODE_PRESEL, "node_presel 0x%08X 0x%08X 0x%08X cancel\n", m->id, d->id, n->id);
+}
 
-    n->client->urgent = false;
+node_t *find_public(desktop_t *d)
+{
+       unsigned int b_manual_area = 0;
+       unsigned int b_automatic_area = 0;
+       node_t *b_manual = NULL;
+       node_t *b_automatic = NULL;
+       for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
+               if (n->vacant) {
+                       continue;
+               }
+               unsigned int n_area = node_area(d, n);
+               if (n_area > b_manual_area && (n->presel != NULL || !n->private)) {
+                       b_manual = n;
+                       b_manual_area = n_area;
+               }
+               if (n_area > b_automatic_area &&
+                   n->presel == NULL && !n->private && private_count(n->parent) == 0) {
+                       b_automatic = n;
+                       b_automatic_area = n_area;
+               }
+       }
+       if (b_automatic != NULL) {
+               return b_automatic;
+       } else {
+               return b_manual;
+       }
+}
 
-    if (!is_visible(d, n))
-        tag_node(m, d, n, d, n->client->tags_field | d->tags_field);
-    history_add(m, d, n);
-    set_input_focus(n);
+node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
+{
+       if (d == NULL || n == NULL) {
+               return NULL;
+       }
+
+       /* n: inserted node */
+       /* c: new internal node */
+       /* f: focus or insertion anchor */
+       /* p: parent of focus */
+       /* g: grand parent of focus */
+
+       if (f == NULL) {
+               f = d->root;
+       }
+
+       if (f == NULL) {
+               d->root = n;
+       } else if (IS_RECEPTACLE(f) && f->presel == NULL) {
+               node_t *p = f->parent;
+               if (p != NULL) {
+                       if (is_first_child(f)) {
+                               p->first_child = n;
+                       } else {
+                               p->second_child = n;
+                       }
+               } else {
+                       d->root = n;
+               }
+               n->parent = p;
+               free(f);
+       } else {
+               node_t *c = make_node(XCB_NONE);
+               node_t *p = f->parent;
+               if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
+                       node_t *k = find_public(d);
+                       if (k != NULL) {
+                               f = k;
+                               p = f->parent;
+                       }
+                       if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
+                               xcb_rectangle_t rect = get_rectangle(d, f);
+                               presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
+                       }
+               }
+               if (f->presel == NULL && p != NULL && p->vacant) {
+                       f = p;
+                       p = f->parent;
+               }
+               n->parent = c;
+               c->birth_rotation = f->birth_rotation;
+               if (f->presel == NULL) {
+                       if (p == NULL) {
+                               if (initial_polarity == FIRST_CHILD) {
+                                       c->first_child = n;
+                                       c->second_child = f;
+                               } else {
+                                       c->first_child = f;
+                                       c->second_child = n;
+                               }
+                               if (m->rectangle.width > m->rectangle.height) {
+                                       c->split_type = TYPE_VERTICAL;
+                               } else {
+                                       c->split_type = TYPE_HORIZONTAL;
+                               }
+                               f->parent = c;
+                               d->root = c;
+                       } else {
+                               node_t *g = p->parent;
+                               c->parent = g;
+                               if (g != NULL) {
+                                       if (is_first_child(p)) {
+                                               g->first_child = c;
+                                       } else {
+                                               g->second_child = c;
+                                       }
+                               } else {
+                                       d->root = c;
+                               }
+                               c->split_type = p->split_type;
+                               c->split_ratio = p->split_ratio;
+                               p->parent = c;
+                               int rot;
+                               if (is_first_child(f)) {
+                                       c->first_child = n;
+                                       c->second_child = p;
+                                       rot = 90;
+                               } else {
+                                       c->first_child = p;
+                                       c->second_child = n;
+                                       rot = 270;
+                               }
+                               n->birth_rotation = rot;
+                               if (!n->vacant) {
+                                       rotate_tree(p, rot);
+                               }
+                       }
+               } else {
+                       if (p != NULL) {
+                               if (is_first_child(f)) {
+                                       p->first_child = c;
+                               } else {
+                                       p->second_child = c;
+                               }
+                       }
+                       c->split_ratio = f->presel->split_ratio;
+                       c->parent = p;
+                       f->parent = c;
+                       f->birth_rotation = 0;
+                       switch (f->presel->split_dir) {
+                               case DIR_WEST:
+                                       c->split_type = TYPE_VERTICAL;
+                                       c->first_child = n;
+                                       c->second_child = f;
+                                       break;
+                               case DIR_EAST:
+                                       c->split_type = TYPE_VERTICAL;
+                                       c->first_child = f;
+                                       c->second_child = n;
+                                       break;
+                               case DIR_NORTH:
+                                       c->split_type = TYPE_HORIZONTAL;
+                                       c->first_child = n;
+                                       c->second_child = f;
+                                       break;
+                               case DIR_SOUTH:
+                                       c->split_type = TYPE_HORIZONTAL;
+                                       c->first_child = f;
+                                       c->second_child = n;
+                                       break;
+                       }
+                       if (d->root == f) {
+                               d->root = c;
+                       }
+                       cancel_presel(m, d, f);
+               }
+               if (f->vacant) {
+                       propagate_vacant_state(m, d, f);
+               }
+       }
+
+       if (d->focus == NULL && !IS_RECEPTACLE(n)) {
+               d->focus = n;
+       }
+
+       return f;
+}
 
-    if (focus_follows_pointer) {
-        xcb_window_t win = XCB_NONE;
-        query_pointer(&win, NULL);
-        if (win != n->client->window)
-            enable_motion_recorder();
-        else
-            disable_motion_recorder();
-    }
+void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
+{
+       node_t *r = make_node(XCB_NONE);
+       insert_node(m, d, r, n);
+}
 
-    ewmh_update_active_window();
+bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
+{
+       if (IS_RECEPTACLE(n)) {
+               return false;
+       }
+
+       if (n == NULL && d->root != NULL) {
+               n = history_last_node(d, NULL);
+               if (n == NULL) {
+                       n = first_focusable_leaf(d->root);
+               }
+       }
+
+       if (n != NULL) {
+               if (d->focus != NULL && n != d->focus) {
+                       neutralize_occluding_windows(m, d, n);
+               }
+               stack(d, n, true);
+               if (d->focus != n) {
+                       for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
+                               if (f->client != NULL && !is_descendant(f, n)) {
+                                       window_draw_border(f->id, get_border_color(false, (m == mon)));
+                               }
+                       }
+               }
+               draw_border(n, true, (m == mon));
+       }
+
+       d->focus = n;
+       history_add(m, d, n);
+
+       put_status(SBSC_MASK_REPORT);
+
+       if (n == NULL) {
+               return true;
+       }
+
+       put_status(SBSC_MASK_NODE_ACTIVATE, "node_activate 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
+
+       return true;
 }
 
-void update_current(void)
+void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n)
 {
-    focus_node(mon, mon->desk, mon->desk->focus);
+       if (n == NULL) {
+               return;
+       } else if (n->sticky) {
+               transfer_node(m, ds, n, m, dd, dd->focus);
+       } else {
+               /* we need references to the children because n might be freed after
+                * the first recursive call */
+               node_t *first_child = n->first_child;
+               node_t *second_child = n->second_child;
+               transfer_sticky_nodes(m, ds, dd, first_child);
+               transfer_sticky_nodes(m, ds, dd, second_child);
+       }
 }
 
-node_t *make_node(void)
+bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
 {
-    node_t *n = malloc(sizeof(node_t));
-    n->parent = n->first_child = n->second_child = NULL;
-    n->split_ratio = split_ratio;
-    n->split_mode = MODE_AUTOMATIC;
-    n->split_type = TYPE_VERTICAL;
-    n->birth_rotation = 0;
-    n->client = NULL;
-    n->vacant = false;
-    return n;
+       if (IS_RECEPTACLE(n)) {
+               return false;
+       }
+
+       if (n == NULL && d->root != NULL) {
+               n = history_last_node(d, NULL);
+               if (n == NULL) {
+                       n = first_focusable_leaf(d->root);
+               }
+       }
+
+       if (mon->desk != d || n == NULL || n->client == NULL) {
+               clear_input_focus();
+       }
+
+       if (m->sticky_count > 0 && d != m->desk) {
+               sticky_still = false;
+               transfer_sticky_nodes(m, m->desk, d, m->desk->root);
+               sticky_still = true;
+               if (n == NULL && d->focus != NULL) {
+                       n = d->focus;
+               }
+       }
+
+       if (d->focus != NULL && n != d->focus) {
+               neutralize_occluding_windows(m, d, n);
+       }
+
+       if (n != NULL && n->client != NULL && n->client->urgent) {
+               set_urgent(m, d, n, false);
+       }
+
+       if (mon != m) {
+               for (desktop_t *e = mon->desk_head; e != NULL; e = e->next) {
+                       draw_border(e->focus, true, false);
+               }
+               for (desktop_t *e = m->desk_head; e != NULL; e = e->next) {
+                       if (e == d) {
+                               continue;
+                       }
+                       draw_border(e->focus, true, true);
+               }
+       }
+
+       if (d->focus != n) {
+               for (node_t *f = first_extrema(d->focus); f != NULL; f = next_leaf(f, d->focus)) {
+                       if (f->client != NULL && !is_descendant(f, n)) {
+                               window_draw_border(f->id, get_border_color(false, true));
+                       }
+               }
+       }
+
+       draw_border(n, true, true);
+
+       focus_desktop(m, d);
+
+       d->focus = n;
+       ewmh_update_active_window();
+       history_add(m, d, n);
+
+       put_status(SBSC_MASK_REPORT);
+
+       if (n == NULL) {
+               return true;
+       }
+
+       put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
+
+       stack(d, n, true);
+       set_input_focus(n);
+
+       if (focus_follows_pointer) {
+               xcb_window_t win = XCB_NONE;
+               query_pointer(&win, NULL);
+               if (win != n->id) {
+                       enable_motion_recorder();
+               } else {
+                       disable_motion_recorder();
+               }
+       }
+
+       if (pointer_follows_focus) {
+               center_pointer(get_rectangle(d, n));
+       }
+
+       return true;
 }
 
-client_t *make_client(xcb_window_t win)
+void update_focused(void)
 {
-    client_t *c = malloc(sizeof(client_t));
-    snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
-    c->border_width = BORDER_WIDTH;
-    c->window = win;
-    c->floating = c->transient = c->fullscreen = c->locked = c->sticky = c->urgent = false;
-    c->frame = c->icccm_focus = false;
-    xcb_icccm_get_wm_protocols_reply_t protocols;
-    if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protocols, NULL) == 1) {
-        if (has_proto(WM_TAKE_FOCUS, &protocols))
-            c->icccm_focus = true;
-        xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
-    }
-    c->num_states = 0;
-    xcb_ewmh_get_atoms_reply_t wm_state;
-    if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
-        for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_STATE; i++)
-            ewmh_wm_state_add(c, wm_state.atoms[i]);
-        xcb_ewmh_get_atoms_reply_wipe(&wm_state);
-    }
-    return c;
+       focus_node(mon, mon->desk, mon->desk->focus);
 }
 
-bool is_visible(desktop_t *d, node_t *n)
+void hide_node(node_t *n)
 {
-    return (d->tags_field & n->client->tags_field) != 0;
+       if (n == NULL) {
+               return;
+       } else {
+               if (n->presel != NULL) {
+                       window_hide(n->presel->feedback);
+               }
+               if (n->client != NULL) {
+                       window_hide(n->id);
+                       n->client->visible = false;
+               }
+               hide_node(n->first_child);
+               hide_node(n->second_child);
+       }
 }
 
-bool is_leaf(node_t *n)
+void show_node(node_t *n)
 {
-    return (n != NULL && n->first_child == NULL && n->second_child == NULL);
+       if (n == NULL) {
+               return;
+       } else {
+               if (n->client != NULL) {
+                       window_show(n->id);
+                       n->client->visible = true;
+               }
+               if (n->presel != NULL) {
+                       window_show(n->presel->feedback);
+               }
+               show_node(n->first_child);
+               show_node(n->second_child);
+       }
 }
 
-bool is_tiled(client_t *c)
+node_t *make_node(uint32_t id)
 {
-    if (c == NULL)
-        return false;
-    return (!c->floating && !c->fullscreen);
+       if (id == XCB_NONE) {
+               id = xcb_generate_id(dpy);
+       }
+       node_t *n = malloc(sizeof(node_t));
+       n->id = id;
+       n->parent = n->first_child = n->second_child = NULL;
+       n->vacant = n->sticky = n->private = n->locked = false;
+       n->split_ratio = split_ratio;
+       n->split_type = TYPE_VERTICAL;
+       n->birth_rotation = 0;
+       n->presel = NULL;
+       n->client = NULL;
+       return n;
 }
 
-bool is_floating(client_t *c)
+client_t *make_client(void)
 {
-    if (c == NULL)
-        return false;
-    return (c->floating && !c->fullscreen);
+       client_t *c = malloc(sizeof(client_t));
+       c->state = c->last_state = STATE_TILED;
+       c->layer = c->last_layer = LAYER_NORMAL;
+       snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
+       snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
+       c->border_width = border_width;
+       c->urgent = c->visible = c->icccm_focus = false;
+       c->icccm_input = true;
+       c->wm_states_count = 0;
+       return c;
 }
 
-bool is_first_child(node_t *n)
+void initialize_client(node_t *n)
 {
-    return (n != NULL && n->parent != NULL && n->parent->first_child == n);
+       xcb_window_t win = n->id;
+       if (win != XCB_NONE) {
+               client_t *c = n->client;
+               xcb_icccm_get_wm_protocols_reply_t protocols;
+               if (xcb_icccm_get_wm_protocols_reply(dpy, xcb_icccm_get_wm_protocols(dpy, win, ewmh->WM_PROTOCOLS), &protocols, NULL) == 1) {
+                       if (has_proto(WM_TAKE_FOCUS, &protocols)) {
+                               c->icccm_focus = true;
+                       }
+                       xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
+               }
+               xcb_ewmh_get_atoms_reply_t wm_state;
+               if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &wm_state, NULL) == 1) {
+                       for (unsigned int i = 0; i < wm_state.atoms_len && i < MAX_WM_STATES; i++) {
+                               ewmh_wm_state_add(n, wm_state.atoms[i]);
+                       }
+                       xcb_ewmh_get_atoms_reply_wipe(&wm_state);
+               }
+               xcb_icccm_wm_hints_t hints;
+               if (xcb_icccm_get_wm_hints_reply(dpy, xcb_icccm_get_wm_hints(dpy, win), &hints, NULL) == 1
+                   && (hints.flags & XCB_ICCCM_WM_HINT_INPUT)) {
+                       c->icccm_input = hints.input;
+               }
+       }
 }
 
-bool is_second_child(node_t *n)
+bool is_leaf(node_t *n)
 {
-    return (n != NULL && n->parent != NULL && n->parent->second_child == n);
+       return (n != NULL && n->first_child == NULL && n->second_child == NULL);
 }
 
-void change_split_ratio(node_t *n, value_change_t chg)
+bool is_first_child(node_t *n)
 {
-    n->split_ratio = pow(n->split_ratio,
-            (chg == CHANGE_INCREASE ? (1 / growth_factor) : growth_factor));
+       return (n != NULL && n->parent != NULL && n->parent->first_child == n);
 }
 
-void reset_mode(coordinates_t *loc)
+bool is_second_child(node_t *n)
 {
-    if (loc->node != NULL) {
-        loc->node->split_mode = MODE_AUTOMATIC;
-        window_draw_border(loc->node, loc->desktop->focus == loc->node, mon == loc->monitor);
-    } else if (loc->desktop != NULL) {
-        for (node_t *a = first_extrema(loc->desktop->root); a != NULL; a = next_leaf(a, loc->desktop->root)) {
-            a->split_mode = MODE_AUTOMATIC;
-            window_draw_border(a, loc->desktop->focus == a, mon == loc->monitor);
-        }
-    }
+       return (n != NULL && n->parent != NULL && n->parent->second_child == n);
 }
 
-node_t *brother_tree(node_t *n)
+unsigned int clients_count_in(node_t *n)
 {
-    if (n == NULL || n->parent == NULL)
-        return NULL;
-    if (is_first_child(n))
-        return n->parent->second_child;
-    else
-        return n->parent->first_child;
+       if (n == NULL) {
+               return 0;
+       } else {
+               return (n->client != NULL ? 1 : 0) +
+                       clients_count_in(n->first_child) +
+                       clients_count_in(n->second_child);
+       }
 }
 
-node_t *closest_visible(desktop_t *d, node_t *n)
+node_t *brother_tree(node_t *n)
 {
-    if (n == NULL)
-        return NULL;
-    node_t *prev = prev_leaf(n, d->root);
-    node_t *next = next_leaf(n, d->root);
-    while (prev != NULL || next != NULL) {
-        if (prev != NULL) {
-            if (is_visible(d, prev))
-                return prev;
-            else
-                prev = prev_leaf(prev, d->root);
-        }
-        if (next != NULL) {
-            if (is_visible(d, next))
-                return next;
-            else
-                next = next_leaf(next, d->root);
-        }
-    }
-    return NULL;
+       if (n == NULL || n->parent == NULL) {
+               return NULL;
+       }
+       if (is_first_child(n)) {
+               return n->parent->second_child;
+       } else {
+               return n->parent->first_child;
+       }
 }
 
 node_t *first_extrema(node_t *n)
 {
-    if (n == NULL)
-        return NULL;
-    else if (n->first_child == NULL)
-        return n;
-    else
-        return first_extrema(n->first_child);
+       if (n == NULL) {
+               return NULL;
+       } else if (n->first_child == NULL) {
+               return n;
+       } else {
+               return first_extrema(n->first_child);
+       }
 }
 
 node_t *second_extrema(node_t *n)
 {
-    if (n == NULL)
-        return NULL;
-    else if (n->second_child == NULL)
-        return n;
-    else
-        return second_extrema(n->second_child);
+       if (n == NULL) {
+               return NULL;
+       } else if (n->second_child == NULL) {
+               return n;
+       } else {
+               return second_extrema(n->second_child);
+       }
+}
+
+node_t *first_focusable_leaf(node_t *n)
+{
+       for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
+               if (f->client != NULL) {
+                       return f;
+               }
+       }
+       return NULL;
 }
 
 node_t *next_leaf(node_t *n, node_t *r)
 {
-    if (n == NULL)
-        return NULL;
-    node_t *p = n;
-    while (is_second_child(p) && p != r)
-        p = p->parent;
-    if (p == r)
-        return NULL;
-    return first_extrema(p->parent->second_child);
+       if (n == NULL) {
+               return NULL;
+       }
+       node_t *p = n;
+       while (is_second_child(p) && p != r) {
+               p = p->parent;
+       }
+       if (p == r) {
+               return NULL;
+       }
+       return first_extrema(p->parent->second_child);
 }
 
 node_t *prev_leaf(node_t *n, node_t *r)
 {
-    if (n == NULL)
-        return NULL;
-    node_t *p = n;
-    while (is_first_child(p) && p != r)
-        p = p->parent;
-    if (p == r)
-        return NULL;
-    return second_extrema(p->parent->first_child);
+       if (n == NULL) {
+               return NULL;
+       }
+       node_t *p = n;
+       while (is_first_child(p) && p != r) {
+               p = p->parent;
+       }
+       if (p == r) {
+               return NULL;
+       }
+       return second_extrema(p->parent->first_child);
 }
 
-node_t *next_visible_leaf(desktop_t *d, node_t *n, node_t *r)
+node_t *next_tiled_leaf(node_t *n, node_t *r)
 {
-    node_t *next = next_leaf(n, r);
-    if (next == NULL || is_visible(d, next))
-        return next;
-    else
-        return next_visible_leaf(d, next, r);
+       node_t *next = next_leaf(n, r);
+       if (next == NULL || (next->client != NULL && IS_TILED(next->client))) {
+               return next;
+       } else {
+               return next_tiled_leaf(next, r);
+       }
 }
 
-node_t *prev_visible_leaf(desktop_t *d, node_t *n, node_t *r)
+node_t *prev_tiled_leaf(node_t *n, node_t *r)
 {
-    node_t *prev = prev_leaf(n, r);
-    if (prev == NULL || is_visible(d, prev))
-        return prev;
-    else
-        return prev_visible_leaf(d, prev, r);
+       node_t *prev = prev_leaf(n, r);
+       if (prev == NULL || (prev->client != NULL && IS_TILED(prev->client))) {
+               return prev;
+       } else {
+               return prev_tiled_leaf(prev, r);
+       }
 }
 
-/* bool is_adjacent(node_t *a, node_t *r) */
-/* { */
-/*     node_t *f = r->parent; */
-/*     node_t *p = a; */
-/*     bool first_child = is_first_child(r); */
-/*     while (p != r) { */
-/*         if (p->parent->split_type == f->split_type && is_first_child(p) == first_child) */
-/*             return false; */
-/*         p = p->parent; */
-/*     } */
-/*     return true; */
-/* } */
-
 /* Returns true if *b* is adjacent to *a* in the direction *dir* */
 bool is_adjacent(node_t *a, node_t *b, direction_t dir)
 {
-    switch (dir) {
-        case DIR_RIGHT:
-            return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
-            break;
-        case DIR_DOWN:
-            return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
-            break;
-        case DIR_LEFT:
-            return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
-            break;
-        case DIR_UP:
-            return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
-            break;
-    }
-    return false;
+       switch (dir) {
+               case DIR_EAST:
+                       return (a->rectangle.x + a->rectangle.width) == b->rectangle.x;
+                       break;
+               case DIR_SOUTH:
+                       return (a->rectangle.y + a->rectangle.height) == b->rectangle.y;
+                       break;
+               case DIR_WEST:
+                       return (b->rectangle.x + b->rectangle.width) == a->rectangle.x;
+                       break;
+               case DIR_NORTH:
+                       return (b->rectangle.y + b->rectangle.height) == a->rectangle.y;
+                       break;
+       }
+       return false;
 }
 
 node_t *find_fence(node_t *n, direction_t dir)
 {
-    node_t *p;
+       node_t *p;
 
-    if (n == NULL)
-        return NULL;
+       if (n == NULL) {
+               return NULL;
+       }
 
-    p = n->parent;
+       p = n->parent;
 
-    while (p != NULL) {
-        if ((dir == DIR_UP && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y)
-                || (dir == DIR_LEFT && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x)
-                || (dir == DIR_DOWN && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height))
-                || (dir == DIR_RIGHT && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
-            return p;
-        p = p->parent;
-    }
+       while (p != NULL) {
+               if ((dir == DIR_NORTH && p->split_type == TYPE_HORIZONTAL && p->rectangle.y < n->rectangle.y) ||
+                   (dir == DIR_WEST && p->split_type == TYPE_VERTICAL && p->rectangle.x < n->rectangle.x) ||
+                   (dir == DIR_SOUTH && p->split_type == TYPE_HORIZONTAL && (p->rectangle.y + p->rectangle.height) > (n->rectangle.y + n->rectangle.height)) ||
+                   (dir == DIR_EAST && p->split_type == TYPE_VERTICAL && (p->rectangle.x + p->rectangle.width) > (n->rectangle.x + n->rectangle.width)))
+                       return p;
+               p = p->parent;
+       }
 
-    return NULL;
+       return NULL;
 }
 
-
-node_t *nearest_neighbor(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
+node_t *nearest_neighbor(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
 {
-    if (n == NULL || n->client->fullscreen
-            || (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
-        return NULL;
-
-    node_t *nearest = NULL;
-    if (history_aware_focus)
-        nearest = nearest_from_history(m, d, n, dir, sel);
-    if (nearest == NULL)
-        nearest = nearest_from_distance(m, d, n, dir, sel);
+       if (n == NULL || (n->client != NULL && IS_FULLSCREEN(n->client)) ||
+           (d->layout == LAYOUT_MONOCLE && (n->client != NULL && IS_TILED(n->client)))) {
+               return NULL;
+       }
+
+       node_t *nearest = NULL;
+       if (history_aware_focus) {
+               nearest = nearest_from_history(m, d, n, dir, sel);
+       }
+    if (nearest == NULL) {
+        if (focus_by_distance) {
+            nearest = nearest_from_distance(m, d, n, dir, sel);
+        } else {
+            nearest = nearest_from_tree(m, d, n, dir, sel);
+        }
+    }
     return nearest;
 }
 
-node_t *nearest_from_history(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
+/* returns *true* if *a* is a child of *b* */
+bool is_child(node_t *a, node_t *b)
 {
-    if (n == NULL || !is_tiled(n->client))
-        return NULL;
+       if (a == NULL || b == NULL) {
+               return false;
+       }
+       return (a->parent != NULL && a->parent == b);
+}
 
-    node_t *target = find_fence(n, dir);
-    if (target == NULL)
-        return NULL;
-    if (dir == DIR_UP || dir == DIR_LEFT)
-        target = target->first_child;
-    else if (dir == DIR_DOWN || dir == DIR_RIGHT)
-        target = target->second_child;
+/* returns *true* if *a* is a descendant of *b* */
+bool is_descendant(node_t *a, node_t *b)
+{
+       if (a == NULL || b == NULL) {
+               return false;
+       }
+       while (a != b && a != NULL) {
+               a = a->parent;
+       }
+       return a == b;
+}
 
-    node_t *nearest = NULL;
-    int min_rank = INT_MAX;
-    coordinates_t ref = {m, d, n};
-
-    for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
-        if (a->vacant || !is_adjacent(n, a, dir) || a == n)
-            continue;
-        coordinates_t loc = {m, d, a};
-        if (!node_matches(&loc, &ref, sel))
-            continue;
-
-        int rank = history_rank(d, a);
-        if (rank >= 0 && rank < min_rank) {
-            nearest = a;
-            min_rank = rank;
-        }
-    }
+bool find_by_id(uint32_t id, coordinates_t *loc)
+{
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+                       node_t *n = find_by_id_in(d->root, id);
+                       if (n != NULL) {
+                               loc->monitor = m;
+                               loc->desktop = d;
+                               loc->node = n;
+                               return true;
+                       }
+               }
+       }
+       return false;
+}
 
-    return nearest;
+node_t *find_by_id_in(node_t *r, uint32_t id)
+{
+       if (r == NULL) {
+               return NULL;
+       } else if (r->id == id) {
+               return r;
+       } else {
+               node_t *f = find_by_id_in(r->first_child, id);
+               if (f != NULL) {
+                       return f;
+               } else {
+                       return find_by_id_in(r->second_child, id);
+               }
+       }
 }
 
-node_t *nearest_from_distance(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
+node_t *nearest_from_tree(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
 {
-    if (n == NULL)
+    if (n == NULL) {
         return NULL;
+    }
 
-    node_t *target = NULL;
-
-    if (is_tiled(n->client)) {
-        target = find_fence(n, dir);
-        if (target == NULL)
-            return NULL;
-        if (dir == DIR_UP || dir == DIR_LEFT)
-            target = target->first_child;
-        else if (dir == DIR_DOWN || dir == DIR_RIGHT)
-            target = target->second_child;
-    } else {
-        target = d->root;
+    node_t *fence = find_fence(n, dir);
+
+    if (fence == NULL) {
+        return NULL;
     }
 
     node_t *nearest = NULL;
-    direction_t dir2;
-    xcb_point_t pt;
-    xcb_point_t pt2;
-    get_side_handle(n->client, dir, &pt);
-    get_opposite(dir, &dir2);
-    double ds = DBL_MAX;
-    coordinates_t ref = {m, d, n};
-
-    for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
-        coordinates_t loc = {m, d, a};
-        if (a == n ||
-                !is_visible(d, a) ||
-                !node_matches(&loc, &ref, sel) ||
-                is_tiled(a->client) != is_tiled(n->client) ||
-                (is_tiled(a->client) && !is_adjacent(n, a, dir)))
-            continue;
-
-        get_side_handle(a->client, dir2, &pt2);
-        double ds2 = distance(pt, pt2);
-        if (ds2 < ds) {
-            ds = ds2;
-            nearest = a;
-        }
+
+    if (dir == DIR_NORTH || dir == DIR_WEST) {
+        nearest = second_extrema(fence->first_child);
+    } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
+        nearest = first_extrema(fence->second_child);
     }
 
-    return nearest;
+       coordinates_t ref = {m, d, n};
+       coordinates_t loc = {m, d, nearest};
+
+       if (nearest != NULL && (nearest->vacant ||
+                               nearest->client == NULL || !node_matches(&loc, &ref, sel))) {
+               return NULL;
+       }
+
+       return nearest;
 }
 
-void get_opposite(direction_t src, direction_t *dst)
+node_t *nearest_from_history(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
 {
-    switch (src) {
-        case DIR_RIGHT:
-            *dst = DIR_LEFT;
-            break;
-        case DIR_DOWN:
-            *dst = DIR_UP;
-            break;
-        case DIR_LEFT:
-            *dst = DIR_RIGHT;
-            break;
-        case DIR_UP:
-            *dst = DIR_DOWN;
-            break;
-    }
+       if (n == NULL || (n->client != NULL && !IS_TILED(n->client))) {
+               return NULL;
+       }
+
+       node_t *target = find_fence(n, dir);
+       if (target == NULL) {
+               return NULL;
+       }
+       if (dir == DIR_NORTH || dir == DIR_WEST) {
+               target = target->first_child;
+       } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
+               target = target->second_child;
+       }
+
+       node_t *nearest = NULL;
+       int min_rank = INT_MAX;
+       coordinates_t ref = {m, d, n};
+
+       for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
+               if (a->vacant || a->client == NULL || !is_adjacent(n, a, dir) || a == n) {
+                       continue;
+               }
+               coordinates_t loc = {m, d, a};
+               if (!node_matches(&loc, &ref, sel)) {
+                       continue;
+               }
+               int rank = history_rank(d, a);
+               if (rank >= 0 && rank < min_rank) {
+                       nearest = a;
+                       min_rank = rank;
+               }
+       }
+
+       return nearest;
 }
 
-int tiled_area(node_t *n)
+node_t *nearest_from_distance(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, node_select_t sel)
 {
-    if (n == NULL)
-        return -1;
-    xcb_rectangle_t rect = n->client->tiled_rectangle;
-    return rect.width * rect.height;
+       if (n == NULL) {
+               return NULL;
+       }
+
+       node_t *target = NULL;
+
+       if (n->client == NULL || IS_TILED(n->client)) {
+               target = find_fence(n, dir);
+               if (target == NULL) {
+                       return NULL;
+               }
+               if (dir == DIR_NORTH || dir == DIR_WEST) {
+                       target = target->first_child;
+               } else if (dir == DIR_SOUTH || dir == DIR_EAST) {
+                       target = target->second_child;
+               }
+       } else {
+               target = d->root;
+       }
+
+       node_t *nearest = NULL;
+       direction_t dir2 = DIR_NORTH;
+       xcb_point_t pt;
+       xcb_point_t pt2;
+       get_side_handle(d, n, dir, &pt);
+       get_opposite(dir, &dir2);
+       double ds = DBL_MAX;
+       coordinates_t ref = {m, d, n};
+
+       for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
+               coordinates_t loc = {m, d, a};
+               if (a == n ||
+                   a->client == NULL ||
+                   !node_matches(&loc, &ref, sel) ||
+                   (n->client != NULL && (IS_TILED(a->client) != IS_TILED(n->client))) ||
+                   (IS_TILED(a->client) && !is_adjacent(n, a, dir))) {
+                       continue;
+               }
+               get_side_handle(d, a, dir2, &pt2);
+               double ds2 = distance(pt, pt2);
+               if (ds2 < ds) {
+                       ds = ds2;
+                       nearest = a;
+               }
+       }
+
+       return nearest;
 }
 
-node_t *find_biggest(monitor_t *m, desktop_t *d, node_t *n, client_select_t sel)
+void get_opposite(direction_t src, direction_t *dst)
 {
-    if (d == NULL)
-        return NULL;
-
-    node_t *r = NULL;
-    int r_area = tiled_area(r);
-    coordinates_t ref = {m, d, n};
-
-    for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
-        coordinates_t loc = {m, d, f};
-        if (!is_visible(d, f) || !is_tiled(f->client) || !node_matches(&loc, &ref, sel))
-            continue;
-        int f_area = tiled_area(f);
-        if (r == NULL) {
-            r = f;
-            r_area = f_area;
-        } else if (f_area > r_area) {
-            r = f;
-            r_area = f_area;
-        }
-    }
+       switch (src) {
+               case DIR_EAST:
+                       *dst = DIR_WEST;
+                       break;
+               case DIR_SOUTH:
+                       *dst = DIR_NORTH;
+                       break;
+               case DIR_WEST:
+                       *dst = DIR_EAST;
+                       break;
+               case DIR_NORTH:
+                       *dst = DIR_SOUTH;
+                       break;
+       }
+}
 
-    return r;
+unsigned int node_area(desktop_t *d, node_t *n)
+{
+       if (n == NULL) {
+               return 0;
+       }
+       return area(get_rectangle(d, n));
 }
 
-void move_fence(node_t *n, direction_t dir, fence_move_t mov)
+int tiled_count(node_t *n)
 {
-    if (n == NULL)
-        return;
+       if (n == NULL) {
+               return 0;
+       }
+       int cnt = 0;
+       for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
+               if (f->client != NULL && IS_TILED(f->client)) {
+                       cnt++;
+               }
+       }
+       return cnt;
+}
 
-    if ((mov == MOVE_PUSH && (dir == DIR_RIGHT || dir == DIR_DOWN))
-            || (mov == MOVE_PULL && (dir == DIR_LEFT || dir == DIR_UP)))
-        change_split_ratio(n, CHANGE_INCREASE);
-    else
-        change_split_ratio(n, CHANGE_DECREASE);
+node_t *find_biggest(monitor_t *m, desktop_t *d, node_t *n, node_select_t sel)
+{
+       if (d == NULL) {
+               return NULL;
+       }
+
+       node_t *b = NULL;
+       unsigned int b_area = 0;
+       coordinates_t ref = {m, d, n};
+
+       for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
+               coordinates_t loc = {m, d, f};
+               if (f->client == NULL || f->vacant || !node_matches(&loc, &ref, sel)) {
+                       continue;
+               }
+               unsigned int f_area = node_area(d, f);
+               if (f_area > b_area) {
+                       b = f;
+                       b_area = f_area;
+               }
+       }
+
+       return b;
 }
 
 void rotate_tree(node_t *n, int deg)
 {
-    if (n == NULL || is_leaf(n) || deg == 0)
-        return;
-
-    node_t *tmp;
-
-    if ((deg == 90 && n->split_type == TYPE_HORIZONTAL)
-            || (deg == 270 && n->split_type == TYPE_VERTICAL)
-            || deg == 180) {
-        tmp = n->first_child;
-        n->first_child = n->second_child;
-        n->second_child = tmp;
-        n->split_ratio = 1.0 - n->split_ratio;
-    }
-
-    if (deg != 180) {
-        if (n->split_type == TYPE_HORIZONTAL)
-            n->split_type = TYPE_VERTICAL;
-        else if (n->split_type == TYPE_VERTICAL)
-            n->split_type = TYPE_HORIZONTAL;
-    }
-
-    rotate_tree(n->first_child, deg);
-    rotate_tree(n->second_child, deg);
+       if (n == NULL || is_leaf(n) || deg == 0) {
+               return;
+       }
+
+       node_t *tmp;
+
+       if ((deg == 90 && n->split_type == TYPE_HORIZONTAL) ||
+           (deg == 270 && n->split_type == TYPE_VERTICAL) ||
+           deg == 180) {
+               tmp = n->first_child;
+               n->first_child = n->second_child;
+               n->second_child = tmp;
+               n->split_ratio = 1.0 - n->split_ratio;
+       }
+
+       if (deg != 180) {
+               if (n->split_type == TYPE_HORIZONTAL) {
+                       n->split_type = TYPE_VERTICAL;
+               } else if (n->split_type == TYPE_VERTICAL) {
+                       n->split_type = TYPE_HORIZONTAL;
+               }
+       }
+
+       rotate_tree(n->first_child, deg);
+       rotate_tree(n->second_child, deg);
 }
 
 void rotate_brother(node_t *n)
 {
-    rotate_tree(brother_tree(n), n->birth_rotation);
+       rotate_tree(brother_tree(n), n->birth_rotation);
 }
 
 void unrotate_tree(node_t *n, int rot)
 {
-    if (rot == 0)
-        return;
-    rotate_tree(n, 360 - rot);
+       if (rot == 0) {
+               return;
+       }
+       rotate_tree(n, 360 - rot);
 }
 
 void unrotate_brother(node_t *n)
 {
-    unrotate_tree(brother_tree(n), n->birth_rotation);
+       unrotate_tree(brother_tree(n), n->birth_rotation);
 }
 
 void flip_tree(node_t *n, flip_t flp)
 {
-    if (n == NULL || is_leaf(n))
-        return;
-
-    node_t *tmp;
-
-    if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL)
-            || (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
-        tmp = n->first_child;
-        n->first_child = n->second_child;
-        n->second_child = tmp;
-        n->split_ratio = 1.0 - n->split_ratio;
-    }
+       if (n == NULL || is_leaf(n)) {
+               return;
+       }
+
+       node_t *tmp;
+
+       if ((flp == FLIP_HORIZONTAL && n->split_type == TYPE_HORIZONTAL) ||
+           (flp == FLIP_VERTICAL && n->split_type == TYPE_VERTICAL)) {
+               tmp = n->first_child;
+               n->first_child = n->second_child;
+               n->second_child = tmp;
+               n->split_ratio = 1.0 - n->split_ratio;
+       }
+
+       flip_tree(n->first_child, flp);
+       flip_tree(n->second_child, flp);
+}
 
-    flip_tree(n->first_child, flp);
-    flip_tree(n->second_child, flp);
+void equalize_tree(node_t *n)
+{
+       if (n == NULL || n->vacant) {
+               return;
+       } else {
+               n->split_ratio = split_ratio;
+               equalize_tree(n->first_child);
+               equalize_tree(n->second_child);
+       }
 }
 
 int balance_tree(node_t *n)
 {
-    if (n == NULL || n->vacant) {
-        return 0;
-    } else if (is_leaf(n)) {
-        return 1;
-    } else {
-        int b1 = balance_tree(n->first_child);
-        int b2 = balance_tree(n->second_child);
-        int b = b1 + b2;
-        if (b1 > 0 && b2 > 0)
-            n->split_ratio = (double) b1 / b;
-        return b;
-    }
+       if (n == NULL || n->vacant) {
+               return 0;
+       } else if (is_leaf(n)) {
+               return 1;
+       } else {
+               int b1 = balance_tree(n->first_child);
+               int b2 = balance_tree(n->second_child);
+               int b = b1 + b2;
+               if (b1 > 0 && b2 > 0) {
+                       n->split_ratio = (double) b1 / b;
+               }
+               return b;
+       }
 }
 
 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
 {
-    if (d == NULL || n == NULL)
-        return;
+       if (d == NULL || n == NULL) {
+               return;
+       }
+
+       node_t *p = n->parent;
+
+       if (p == NULL) {
+               d->root = NULL;
+               d->focus = NULL;
+       } else {
+               if (d->focus == p || is_descendant(d->focus, n)) {
+                       d->focus = NULL;
+               }
+
+               node_t *b = brother_tree(n);
+               node_t *g = p->parent;
+
+               if (!n->vacant) {
+                       unrotate_tree(b, n->birth_rotation);
+               }
+
+               b->parent = g;
+
+               if (g != NULL) {
+                       if (is_first_child(p)) {
+                               g->first_child = b;
+                       } else {
+                               g->second_child = b;
+                       }
+               } else {
+                       d->root = b;
+               }
+
+               b->birth_rotation = p->birth_rotation;
+               n->parent = NULL;
+
+               history_remove(d, p, false);
+               cancel_presel(m, d, p);
+               if (p->sticky) {
+                       m->sticky_count--;
+               }
+               free(p);
+
+               propagate_vacant_state(m, d, b);
+       }
+}
 
-    PRINTF("unlink node %X\n", n->client->window);
+void close_node(node_t *n)
+{
+       if (n == NULL) {
+               return;
+       } else if (n->client != NULL) {
+               send_client_message(n->id, ewmh->WM_PROTOCOLS, WM_DELETE_WINDOW);
+       } else {
+               close_node(n->first_child);
+               close_node(n->second_child);
+       }
+}
 
-    node_t *p = n->parent;
+void kill_node(monitor_t *m, desktop_t *d, node_t *n)
+{
+       if (n == NULL) {
+               return;
+       }
 
-    if (p == NULL) {
-        d->root = NULL;
-        d->focus = NULL;
-    } else {
-        if (n == d->focus) {
-            d->focus = history_get_node(d, n);
-            if (d->focus == NULL)
-                d->focus = closest_visible(d, n);
-        }
+       for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
+               if (f->client != NULL) {
+                       xcb_kill_client(dpy, f->id);
+               }
+       }
 
-        node_t *b;
-        node_t *g = p->parent;
+       remove_node(m, d, n);
+}
 
-        if (is_first_child(n)) {
-            b = p->second_child;
-            if (!n->vacant)
-                unrotate_tree(b, n->birth_rotation);
-        } else {
-            b = p->first_child;
-            if (!n->vacant)
-                unrotate_tree(b, n->birth_rotation);
-        }
+void remove_node(monitor_t *m, desktop_t *d, node_t *n)
+{
+       if (n == NULL) {
+               return;
+       }
+
+       node_t *last_focus = d->focus;
+
+       unlink_node(m, d, n);
+       history_remove(d, n, true);
+       remove_stack_node(n);
+       cancel_presel(m, d, n);
+       if (m->sticky_count > 0) {
+               m->sticky_count -= sticky_count(n);
+       }
+       clients_count -= clients_count_in(n);
+       free(n->client);
+       free(n);
+
+       ewmh_update_client_list(false);
+       ewmh_update_client_list(true);
+
+       if (d->focus != last_focus) {
+               if (d == mon->desk) {
+                       focus_node(m, d, d->focus);
+               } else {
+                       activate_node(m, d, d->focus);
+               }
+       }
+}
 
-        b->parent = g;
+void destroy_tree(monitor_t *m, desktop_t *d, node_t *n)
+{
+       if (n == NULL) {
+               return;
+       }
+       node_t *first_child = n->first_child;
+       node_t *second_child = n->second_child;
+       if (n->client != NULL) {
+               remove_stack_node(n);
+               clients_count--;
+       }
+       if (n->sticky) {
+               m->sticky_count--;
+       }
+       cancel_presel(m, d, n);
+       free(n->client);
+       free(n);
+       if (first_child != NULL && second_child != NULL) {
+               first_child->parent = second_child->parent = NULL;
+               destroy_tree(m, d, first_child);
+               destroy_tree(m, d, second_child);
+       }
+}
 
-        if (g != NULL) {
-            if (is_first_child(p))
-                g->first_child = b;
-            else
-                g->second_child = b;
-        } else {
-            d->root = b;
-        }
+bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
+{
+       if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
+           (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
+                         (m2->sticky_count > 0 && sticky_count(n2) > 0)))) {
+               return false;
+       }
+
+       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);
+
+       node_t *pn1 = n1->parent;
+       node_t *pn2 = n2->parent;
+       bool n1_first_child = is_first_child(n1);
+       bool n2_first_child = is_first_child(n2);
+       int br1 = n1->birth_rotation;
+       int br2 = n2->birth_rotation;
+       bool n1_held_focus = is_descendant(d1->focus, n1);
+       bool n2_held_focus = is_descendant(d2->focus, n2);
+       node_t *last_d1_focus = d1->focus;
+       node_t *last_d2_focus = d2->focus;
+
+       if (pn1 != NULL) {
+               if (n1_first_child) {
+                       pn1->first_child = n2;
+               } else {
+                       pn1->second_child = n2;
+               }
+       }
+
+       if (pn2 != NULL) {
+               if (n2_first_child) {
+                       pn2->first_child = n1;
+               } else {
+                       pn2->second_child = n1;
+               }
+       }
+
+       n1->parent = pn2;
+       n2->parent = pn1;
+       n1->birth_rotation = br2;
+       n2->birth_rotation = br1;
+
+       if (n1->vacant != n2->vacant) {
+               propagate_vacant_state(m2, d2, n1);
+               propagate_vacant_state(m1, d1, n2);
+       }
+
+       if (d1 != d2) {
+               if (d1->root == n1) {
+                       d1->root = n2;
+               }
+
+               if (d2->root == n2) {
+                       d2->root = n1;
+               }
+
+               if (n1_held_focus) {
+                       d1->focus = n2_held_focus ? last_d2_focus : n2;
+               }
+
+               if (n2_held_focus) {
+                       d2->focus = n1_held_focus ? last_d1_focus : n1;
+               }
+
+               if (m1 != m2) {
+                       adapt_geometry(&m2->rectangle, &m1->rectangle, n2);
+                       adapt_geometry(&m1->rectangle, &m2->rectangle, n1);
+               }
+
+               ewmh_set_wm_desktop(n1, d2);
+               ewmh_set_wm_desktop(n2, d1);
+
+               history_swap_nodes(m1, d1, n1, m2, d2, n2);
+
+               if (m1->desk != d1 && m2->desk == d2) {
+                       show_node(n1);
+                       hide_node(n2);
+               } else if (m1->desk == d1 && m2->desk != d2) {
+                       hide_node(n1);
+                       show_node(n2);
+               }
+
+               if (n1_held_focus) {
+                       if (d1 == mon->desk) {
+                               focus_node(m1, d1, d1->focus);
+                       } else {
+                               activate_node(m1, d1, d1->focus);
+                       }
+               } else {
+                       draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
+               }
+
+               if (n2_held_focus) {
+                       if (d2 == mon->desk) {
+                               focus_node(m2, d2, d2->focus);
+                       } else {
+                               activate_node(m2, d2, d2->focus);
+                       }
+               } else {
+                       draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
+               }
+       }
+
+       arrange(m1, d1);
+
+       if (d1 != d2) {
+               arrange(m2, d2);
+       }
+
+       return true;
+}
 
-        b->birth_rotation = p->birth_rotation;
-        n->parent = NULL;
-        free(p);
-        update_vacant_state(b->parent);
-    }
-    if (n->client->sticky)
-        m->num_sticky--;
-    put_status();
+bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
+{
+       if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
+               return false;
+       }
+
+       unsigned int sc = 0;
+
+       if (sticky_still && ms->sticky_count > 0 && ds != dd && (sc = sticky_count(ns)) > 0) {
+               return false;
+       }
+
+       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);
+
+       bool held_focus = is_descendant(ds->focus, ns);
+       /* avoid ending up with a dangling pointer (because of unlink_node) */
+       node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
+
+       if (held_focus && ds == mon->desk) {
+               clear_input_focus();
+       }
+
+       unlink_node(ms, ds, ns);
+       insert_node(md, dd, ns, nd);
+
+       if (md != ms && (ns->client == NULL || monitor_from_client(ns->client) != md)) {
+               adapt_geometry(&ms->rectangle, &md->rectangle, ns);
+       }
+
+       if (ds != dd) {
+               ewmh_set_wm_desktop(ns, dd);
+               if (sc == 0) {
+                       if (ds == ms->desk && dd != md->desk) {
+                               hide_node(ns);
+                       } else if (ds != ms->desk && dd == md->desk) {
+                               show_node(ns);
+                       }
+               }
+       }
+
+       history_transfer_node(md, dd, ns);
+       stack(dd, ns, false);
+
+       if (ds == dd) {
+               if (held_focus) {
+                       if (ds == mon->desk) {
+                               focus_node(ms, ds, last_ds_focus);
+                       } else {
+                               activate_node(ms, ds, last_ds_focus);
+                       }
+               } else {
+                       draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
+               }
+       } else {
+               if (held_focus) {
+                       if (ds == mon->desk) {
+                               focus_node(ms, ds, ds->focus);
+                       } else {
+                               activate_node(ms, ds, ds->focus);
+                       }
+               }
+               if (dd->focus == ns) {
+                       if (dd == mon->desk) {
+                               focus_node(md, dd, held_focus ? last_ds_focus : dd->focus);
+                       } else {
+                               activate_node(md, dd, held_focus ? last_ds_focus : dd->focus);
+                       }
+               } else {
+                       draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
+               }
+       }
+
+       arrange(ms, ds);
+
+       if (ds != dd) {
+               arrange(md, dd);
+       }
+
+       return true;
 }
 
-void remove_node(monitor_t *m, desktop_t *d, node_t *n)
+node_t *closest_node(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, node_select_t sel)
+{
+       if (n == NULL) {
+               return NULL;
+       }
+
+       node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
+       if (f == NULL) {
+               f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
+       }
+
+       coordinates_t ref = {m, d, n};
+       while (f != n) {
+               coordinates_t loc = {m, d, f};
+               if (f->client != NULL && node_matches(&loc, &ref, sel)) {
+                       return f;
+               }
+               f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
+               if (f == NULL) {
+                       f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
+               }
+       }
+       return NULL;
+}
+
+void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
 {
-    if (n == NULL)
-        return;
+       if (n == NULL || d->focus == NULL || is_leaf(n)) {
+               return;
+       }
+       node_t *p = d->focus->parent;
+       bool focus_first_child = is_first_child(d->focus);
+       if (dir == CIRCULATE_FORWARD) {
+               node_t *e = second_extrema(n);
+               while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
+                       e = prev_leaf(e, n);
+               }
+               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)) {
+                       swap_nodes(m, d, f, m, d, s);
+               }
+       } else {
+               node_t *e = first_extrema(n);
+               while (e != NULL && (e->client == NULL || !IS_TILED(e->client))) {
+                       e = next_leaf(e, n);
+               }
+               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)) {
+                       swap_nodes(m, d, f, m, d, s);
+               }
+       }
+       if (focus_first_child) {
+               focus_node(m, d, p->first_child);
+       } else {
+               focus_node(m, d, p->second_child);
+       }
+}
 
-    PRINTF("remove node %X\n", n->client->window);
+void set_vacant_state(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+       n->vacant = value;
 
-    bool focused = (n == mon->desk->focus);
-    unlink_node(m, d, n);
-    history_remove(d, n);
-    remove_stack_node(n);
-    free(n->client);
-    free(n);
+       if (value) {
+               unrotate_brother(n);
+       } else {
+               rotate_brother(n);
+       }
 
-    num_clients--;
-    ewmh_update_client_list();
+       if (value) {
+               cancel_presel(m, d, n);
+       }
 
-    if (focused)
-        update_current();
+       propagate_vacant_state(m, d, n);
 }
 
-void destroy_tree(node_t *n)
+void propagate_vacant_state(monitor_t *m, desktop_t *d, node_t *n)
 {
-    if (n == NULL)
-        return;
-    node_t *first_tree = n->first_child;
-    node_t *second_tree = n->second_child;
-    if (n->client != NULL)
-        free(n->client);
-    free(n);
-    destroy_tree(first_tree);
-    destroy_tree(second_tree);
+       if (n == NULL) {
+               return;
+       }
+
+       node_t *p = n->parent;
+
+       while (p != NULL) {
+               p->vacant = (p->first_child->vacant && p->second_child->vacant);
+               if (p->vacant) {
+                       cancel_presel(m, d, p);
+               }
+               p = p->parent;
+       }
 }
 
-bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
+bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
 {
-    if (n1 == NULL || n2 == NULL || n1 == n2 || (d1 != d2 && (n1->client->sticky || n2->client->sticky)))
-        return false;
+       if (n == NULL || n->client->layer == l) {
+               return false;
+       }
 
-    PRINTF("swap nodes %X %X", n1->client->window, n2->client->window);
+       n->client->last_layer = n->client->layer;
+       n->client->layer = l;
 
-    node_t *pn1 = n1->parent;
-    node_t *pn2 = n2->parent;
-    bool n1_first_child = is_first_child(n1);
-    bool n2_first_child = is_first_child(n2);
-    int br1 = n1->birth_rotation;
-    int br2 = n2->birth_rotation;
+       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));
 
-    if (pn1 != NULL) {
-        if (n1_first_child)
-            pn1->first_child = n2;
-        else
-            pn1->second_child = n2;
-    }
+       if (d->focus == n) {
+               neutralize_occluding_windows(m, d, n);
+       }
 
-    if (pn2 != NULL) {
-        if (n2_first_child)
-            pn2->first_child = n1;
-        else
-            pn2->second_child = n1;
-    }
+       stack(d, n, (d->focus == n));
 
-    n1->parent = pn2;
-    n2->parent = pn1;
-    n1->birth_rotation = br2;
-    n2->birth_rotation = br1;
+       return true;
+}
 
-    if (n1->vacant != n2->vacant) {
-        update_vacant_state(n1->parent);
-        update_vacant_state(n2->parent);
-    }
+bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
+{
+       if (n == NULL || n->client->state == s) {
+               return false;
+       }
+
+       client_t *c = n->client;
+
+       c->last_state = c->state;
+       c->state = s;
+
+       switch (c->last_state) {
+               case STATE_TILED:
+               case STATE_PSEUDO_TILED:
+                       break;
+               case STATE_FLOATING:
+                       set_floating(m, d, n, false);
+                       break;
+               case STATE_FULLSCREEN:
+                       set_fullscreen(m, d, n, false);
+                       break;
+       }
+
+       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));
+
+       switch (c->state) {
+               case STATE_TILED:
+               case STATE_PSEUDO_TILED:
+                       break;
+               case STATE_FLOATING:
+                       set_floating(m, d, n, true);
+                       break;
+               case STATE_FULLSCREEN:
+                       set_fullscreen(m, d, n, true);
+                       break;
+       }
+
+       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));
+
+       if (n == m->desk->focus) {
+               put_status(SBSC_MASK_REPORT);
+       }
+
+       return true;
+}
 
-    if (d1 != d2) {
-        if (d1->root == n1)
-            d1->root = n2;
-        if (d1->focus == n1)
-            d1->focus = n2;
-        if (d2->root == n2)
-            d2->root = n1;
-        if (d2->focus == n2)
-            d2->focus = n1;
-
-        if (m1 != m2) {
-            fit_monitor(m1, n2->client);
-            fit_monitor(m2, n1->client);
-        }
+void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+       if (n == NULL) {
+               return;
+       }
 
-        ewmh_set_wm_desktop(n1, d2);
-        ewmh_set_wm_desktop(n2, d1);
-        history_swap_nodes(m1, d1, n1, m2, d2, n2);
+       cancel_presel(m, d, n);
+       set_vacant_state(m, d, n, value);
 
-        if (m1->desk != d1 && m2->desk == d2) {
-            window_show(n1->client->window);
-            window_hide(n2->client->window);
-        } else if (m1->desk == d1 && m2->desk != d2) {
-            window_hide(n1->client->window);
-            window_show(n2->client->window);
-        }
+       if (!value && d->focus == n) {
+               neutralize_occluding_windows(m, d, n);
+       }
 
-        tag_node(m1, d1, n2, d2, n2->client->tags_field);
-        tag_node(m2, d2, n1, d1, n1->client->tags_field);
+       stack(d, n, (d->focus == n));
+}
 
-        update_input_focus();
-    }
+void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+       if (n == NULL) {
+               return;
+       }
+
+       client_t *c = n->client;
+
+       cancel_presel(m, d, n);
+       set_vacant_state(m, d, n, value);
+
+       if (value) {
+               ewmh_wm_state_add(n, ewmh->_NET_WM_STATE_FULLSCREEN);
+               c->last_layer = c->layer;
+               c->layer = LAYER_ABOVE;
+       } else {
+               ewmh_wm_state_remove(n, ewmh->_NET_WM_STATE_FULLSCREEN);
+               c->layer = c->last_layer;
+               if (d->focus == n) {
+                       neutralize_occluding_windows(m, d, n);
+               }
+       }
+
+       stack(d, n, (d->focus == n));
+}
 
-    return true;
+void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
+{
+       bool changed = false;
+       for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
+               for (node_t *a = first_extrema(d->root); a != NULL; a = next_leaf(a, d->root)) {
+                       if (a != f && a->client != NULL && f->client != NULL &&
+                           IS_FULLSCREEN(a->client) && stack_cmp(f->client, a->client) < 0) {
+                               set_state(m, d, a, a->client->last_state);
+                               changed = true;
+                       }
+               }
+       }
+       if (changed) {
+               arrange(m, d);
+       }
 }
 
-bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
+void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+       if (n == NULL || n->locked == value) {
+               return;
+       }
+
+       n->locked = value;
+
+       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));
+
+       if (n == m->desk->focus) {
+               put_status(SBSC_MASK_REPORT);
+       }
+}
+
+void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
 {
-    if (ns == NULL || ns == nd || (sticky_still && ns->client->sticky))
-        return false;
+       if (n == NULL || n->sticky == value) {
+               return;
+       }
 
-    PRINTF("transfer node %X\n", ns->client->window);
+       if (d != m->desk) {
+               transfer_node(m, d, n, m, m->desk, m->desk->focus);
+       }
 
-    bool focused = (ns == mon->desk->focus);
-    bool active = (ns == ds->focus);
+       n->sticky = value;
 
-    if (focused)
-        clear_input_focus();
+       if (value) {
+               ewmh_wm_state_add(n, ewmh->_NET_WM_STATE_STICKY);
+               m->sticky_count++;
+       } else {
+               ewmh_wm_state_remove(n, ewmh->_NET_WM_STATE_STICKY);
+               m->sticky_count--;
+       }
 
-    unlink_node(ms, ds, ns);
-    insert_node(md, dd, ns, nd);
+       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));
 
-    if (md != ms)
-        fit_monitor(md, ns->client);
+       if (n == m->desk->focus) {
+               put_status(SBSC_MASK_REPORT);
+       }
 
-    if (ds != dd) {
-        ewmh_set_wm_desktop(ns, dd);
-        if (!ns->client->sticky) {
-            if (ds == ms->desk && dd != md->desk)
-                window_hide(ns->client->window);
-            else if (ds != ms->desk && dd == md->desk)
-                window_show(ns->client->window);
-        }
-    }
+}
 
-    history_transfer_node(md, dd, ns);
-    stack_under(ns);
-
-    if (ds == dd) {
-        if (focused)
-            focus_node(md, dd, ns);
-        else if (active)
-            pseudo_focus(dd, ns);
-    } else {
-        if (focused)
-            update_current();
-        else if (ns == mon->desk->focus)
-            update_input_focus();
-    }
+void set_private(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+       if (n == NULL || n->private == value) {
+               return;
+       }
 
-    tag_node(md, dd, ns, ds, ns->client->tags_field);
-    arrange(ms, ds);
-    if (ds != dd)
-        arrange(md, dd);
+       n->private = value;
 
-    return true;
+       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));
+
+       if (n == m->desk->focus) {
+               put_status(SBSC_MASK_REPORT);
+       }
 }
 
-node_t *closest_node(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
+void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
 {
-    if (n == NULL)
-        return NULL;
+       if (value && mon->desk->focus == n) {
+               return;
+       }
 
-    node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
-    if (f == NULL)
-        f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
-
-    coordinates_t ref = {m, d, n};
-    while (f != n) {
-        coordinates_t loc = {m, d, f};
-        if (node_matches(&loc, &ref, sel))
-            return f;
-        f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
-        if (f == NULL)
-            f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
-    }
-    return NULL;
-}
-
-void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
-{
-    if (d == NULL || d->root == NULL || d->focus == NULL || is_leaf(d->root))
-        return;
-    node_t *p = d->focus->parent;
-    bool focus_first_child = is_first_child(d->focus);
-    node_t *head, *tail;
-    for (head = first_extrema(d->root); head != NULL && !is_visible(d, head); head = next_leaf(head, d->root))
-        ;
-    for (tail = second_extrema(d->root); tail != NULL && !is_visible(d, tail); tail = prev_leaf(tail, d->root))
-        ;
-    if (head == tail)
-        return;
-    if (dir == CIRCULATE_FORWARD)
-        for (node_t *s = tail, *f = prev_visible_leaf(d, s, d->root); f != NULL; s = prev_visible_leaf(d, f, d->root), f = prev_visible_leaf(d, s, d->root))
-            swap_nodes(m, d, f, m, d, s);
-    else
-        for (node_t *f = head, *s = next_visible_leaf(d, f, d->root); s != NULL; f = next_visible_leaf(d, s, d->root), s = next_visible_leaf(d, f, d->root))
-            swap_nodes(m, d, f, m, d, s);
-    if (focus_first_child)
-        focus_node(m, d, p->first_child);
-    else
-        focus_node(m, d, p->second_child);
-}
-
-void update_vacant_state(node_t *n)
-{
-    if (n == NULL)
-        return;
-
-    PUTS("update vacant state");
-
-    /* n is not a leaf */
-    node_t *p = n;
-
-    while (p != NULL) {
-        p->vacant = (p->first_child->vacant && p->second_child->vacant);
-        p = p->parent;
-    }
+       n->client->urgent = value;
+
+       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));
+       put_status(SBSC_MASK_REPORT);
+}
+
+/* Returns true if a contains b */
+bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
+{
+       return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width) &&
+               a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
+}
+
+xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
+{
+       client_t *c = n->client;
+       if (c != NULL) {
+               xcb_get_geometry_reply_t *g = xcb_get_geometry_reply(dpy, xcb_get_geometry(dpy, n->id), NULL);
+               if (g != NULL) {
+                       xcb_rectangle_t rect = (xcb_rectangle_t) {g->x, g->y, g->width, g->height};
+                       free(g);
+                       return rect;
+               } else {
+                       return (xcb_rectangle_t) {0, 0, screen_width, screen_height};
+               }
+       } else {
+               int wg = (d == NULL ? 0 : (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap));
+               xcb_rectangle_t rect = n->rectangle;
+               rect.width -= wg;
+               rect.height -= wg;
+               return rect;
+       }
+}
+
+void get_side_handle(desktop_t *d, node_t *n, direction_t dir, xcb_point_t *pt)
+{
+       xcb_rectangle_t rect = get_rectangle(d, n);
+
+       switch (dir) {
+               case DIR_EAST:
+                       pt->x = rect.x + rect.width;
+                       pt->y = rect.y + (rect.height / 2);
+                       break;
+               case DIR_SOUTH:
+                       pt->x = rect.x + (rect.width / 2);
+                       pt->y = rect.y + rect.height;
+                       break;
+               case DIR_WEST:
+                       pt->x = rect.x;
+                       pt->y = rect.y + (rect.height / 2);
+                       break;
+               case DIR_NORTH:
+                       pt->x = rect.x + (rect.width / 2);
+                       pt->y = rect.y;
+                       break;
+       }
 }
+
+#define DEF_FLAG_COUNT(flag) \
+       unsigned int flag##_count(node_t *n) \
+       { \
+               if (n == NULL) { \
+                       return 0; \
+               } else { \
+                       return ((n->flag ? 1 : 0) + \
+                               flag##_count(n->first_child) + \
+                               flag##_count(n->second_child)); \
+               } \
+       }
+       DEF_FLAG_COUNT(sticky)
+       DEF_FLAG_COUNT(private)
+       DEF_FLAG_COUNT(locked)
+#undef DEF_FLAG_COUNT