]> git.lizzy.rs Git - bspwm.git/blobdiff - tree.c
Enhance external rules example scripts
[bspwm.git] / tree.c
diff --git a/tree.c b/tree.c
index d57c24f5c972f0db972aa159e2ba4a31e6ee9ce4..f33cfb42fc9fbe2e33ff5f1f4d3f3ca40c43079b 100644 (file)
--- a/tree.c
+++ b/tree.c
-#include <string.h>
-#include <math.h>
-#include <limits.h>
+/* * Copyright (c) 2012-2013 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:
+ *
+ *  * 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.
+ *
+ * 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
+ * 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
+ * (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 <float.h>
-#include "settings.h"
-#include "helpers.h"
-#include "window.h"
-#include "types.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 "settings.h"
+#include "stack.h"
+#include "window.h"
 #include "tree.h"
 
-bool is_leaf(node_t *n)
+void arrange(monitor_t *m, desktop_t *d)
 {
-    return (n != NULL && n->first_child == NULL && n->second_child == NULL);
-}
+    if (d->root == NULL)
+        return;
 
-bool is_tiled(client_t *c)
-{
-    if (c == NULL)
-        return false;
-    return (!c->floating && !c->fullscreen);
-}
+    PRINTF("arrange %s %s\n", m->name, d->name);
 
-bool is_floating(client_t *c)
-{
-    if (c == NULL)
-        return false;
-    return (c->floating && !c->fullscreen);
+    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);
 }
 
-bool is_first_child(node_t *n)
+void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
 {
-    return (n != NULL && n->parent != NULL && n->parent->first_child == n);
-}
+    if (n == NULL)
+        return;
 
-bool is_second_child(node_t *n)
-{
-    return (n != NULL && n->parent != NULL && n->parent->second_child == n);
-}
+    n->rectangle = rect;
 
-/**
- * Check if the specified node matches the selection criteria.
- *
- * Arguments:
- *  node_t *c           - the active node
- *  node_t *t           - the node to test
- *  client_sel_t sel    - the selection criteria
- *
- * Returns true if the node matches.
- **/
-bool node_matches(node_t *c, node_t *t, client_select_t sel)
-{
-    if (sel.type != CLIENT_TYPE_ALL &&
-            is_tiled(t->client)
-            ? sel.type == CLIENT_TYPE_FLOATING
-            : sel.type == CLIENT_TYPE_TILED
-       ) return false;
-
-    if (sel.class != CLIENT_CLASS_ALL &&
-            streq(c->client->class_name, t->client->class_name)
-            ? sel.class == CLIENT_CLASS_DIFFER
-            : sel.class == CLIENT_CLASS_EQUAL
-       ) return false;
-
-    if (sel.mode != CLIENT_MODE_ALL &&
-            t->split_mode == MODE_MANUAL
-            ? sel.mode == CLIENT_MODE_AUTOMATIC
-            : sel.mode == CLIENT_MODE_MANUAL)
-        return false;
+    if (is_leaf(n)) {
 
-    if (sel.urgency != CLIENT_URGENCY_ALL &&
-            t->client->urgent
-            ? sel.urgency == CLIENT_URGENCY_OFF
-            : sel.urgency == CLIENT_URGENCY_ON
-       ) return false;
+        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;
 
-    return true;
-}
+        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;
+        }
 
-bool desktop_matches(desktop_t *t, desktop_select_t sel) {
-    if (sel.status != DESKTOP_STATUS_ALL &&
-            t->root == NULL
-            ? sel.status == DESKTOP_STATUS_OCCUPIED
-            : sel.status == DESKTOP_STATUS_FREE
-       ) return false;
+        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);
 
-    if (sel.urgency != DESKTOP_URGENCY_ALL &&
-            is_urgent(t)
-            ? sel.urgency == DESKTOP_URGENCY_OFF
-            : sel.urgency == DESKTOP_URGENCY_ON
-       ) return false;
+    } else {
+        xcb_rectangle_t first_rect;
+        xcb_rectangle_t second_rect;
 
-    return true;
-}
+        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};
+            }
+        }
 
-bool is_urgent(desktop_t *d)
-{
-    for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
-        if (n->client->urgent)
-            return true;
-    return false;
+        apply_layout(m, d, n->first_child, first_rect, root_rect);
+        apply_layout(m, d, n->second_child, second_rect, root_rect);
+    }
 }
 
-void change_split_ratio(node_t *n, value_change_t chg)
+void insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
 {
-    n->split_ratio = pow(n->split_ratio,
-            (chg == CHANGE_INCREASE ? (1 / GROWTH_FACTOR) : GROWTH_FACTOR));
-}
+    if (d == NULL || n == NULL)
+        return;
 
-void change_layout(monitor_t *m, desktop_t *d, layout_t l)
-{
-    d->layout = l;
-    arrange(m, d);
-    if (d == mon->desk)
-        put_status();
-}
+    PRINTF("insert node %X\n", n->client->window);
 
-void reset_mode(coordinates_t *loc)
-{
-    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);
+    /* n: new leaf node */
+    /* c: new container 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 {
+        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;
+        }
+        if (((f->client != NULL && f->client->private) || (p != NULL && p->privacy_level > 0))
+                    && f->split_mode == MODE_AUTOMATIC) {
+            node_t *closest = NULL;
+            node_t *public = NULL;
+            closest_public(d, f, &closest, &public);
+            if (public != NULL) {
+                f = public;
+                p = f->parent;
+            } else {
+                if (closest != NULL) {
+                    f = closest;
+                    p = f->parent;
+                }
+                f->split_mode = MODE_MANUAL;
+                xcb_rectangle_t rect = f->client->tiled_rectangle;
+                f->split_dir = (rect.width >= rect.height ? DIR_LEFT : DIR_UP);
+                if (f->client->private) {
+                    get_opposite(f->split_dir, &f->split_dir);
+                    update_privacy_level(f, false);
+                }
+            }
+        }
+        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 (f->client != NULL && f->client->private)
+            update_privacy_level(f, true);
     }
+    if (n->client->private)
+        update_privacy_level(n, true);
+    if (d->focus == NULL)
+        d->focus = n;
+    if (n->client->sticky)
+        m->num_sticky++;
+    put_status();
 }
 
-node_t *brother_tree(node_t *n)
+void pseudo_focus(desktop_t *d, 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;
+    d->focus = n;
+    if (n != NULL)
+        stack(n, STACK_ABOVE);
 }
 
-node_t *first_extrema(node_t *n)
+void focus_node(monitor_t *m, desktop_t *d, node_t *n)
 {
-    if (n == NULL)
-        return NULL;
-    else if (n->first_child == NULL)
-        return n;
-    else
-        return first_extrema(n->first_child);
-}
+    if (mon->desk != d || n == NULL)
+        clear_input_focus();
 
-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 (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)
+            n = d->focus;
+    }
 
-node_t *next_leaf(node_t *n, node_t *r)
-{
+    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);
+    }
+
+    if (d->focus != n) {
+        window_draw_border(d->focus, false, true);
+        window_draw_border(n, true, true);
+    }
+
+    focus_desktop(m, d);
+    pseudo_focus(d, n);
+
+    if (n == NULL) {
+        history_add(m, d, NULL);
+        ewmh_update_active_window();
+        return;
+    }
+
+    PRINTF("focus node %X\n", n->client->window);
+
+    n->client->urgent = false;
+
+    history_add(m, d, n);
+    set_input_focus(n);
+
+    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();
+    }
+
+    ewmh_update_active_window();
+}
+
+void update_current(void)
+{
+    focus_node(mon, mon->desk, mon->desk->focus);
+}
+
+node_t *make_node(void)
+{
+    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->privacy_level = 0;
+    n->client = NULL;
+    n->vacant = false;
+    return n;
+}
+
+client_t *make_client(xcb_window_t win)
+{
+    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->private = 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;
+}
+
+bool is_leaf(node_t *n)
+{
+    return (n != NULL && n->first_child == NULL && n->second_child == NULL);
+}
+
+bool is_tiled(client_t *c)
+{
+    if (c == NULL)
+        return false;
+    return (!c->floating && !c->fullscreen);
+}
+
+bool is_floating(client_t *c)
+{
+    if (c == NULL)
+        return false;
+    return (c->floating && !c->fullscreen);
+}
+
+bool is_first_child(node_t *n)
+{
+    return (n != NULL && n->parent != NULL && n->parent->first_child == n);
+}
+
+bool is_second_child(node_t *n)
+{
+    return (n != NULL && n->parent != NULL && n->parent->second_child == n);
+}
+
+void change_split_ratio(node_t *n, value_change_t chg)
+{
+    n->split_ratio = pow(n->split_ratio,
+            (chg == CHANGE_INCREASE ? (1 / growth_factor) : growth_factor));
+}
+
+void reset_mode(coordinates_t *loc)
+{
+    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);
+        }
+    }
+}
+
+node_t *brother_tree(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;
+}
+
+void closest_public(desktop_t *d, node_t *n, node_t **closest, node_t **public)
+{
+    if (n == NULL)
+        return;
+    node_t *prev = prev_leaf(n, d->root);
+    node_t *next = next_leaf(n, d->root);
+    while (prev != NULL || next != NULL) {
+#define TESTLOOP(n) \
+        if (n != NULL) { \
+            if (is_tiled(n->client)) { \
+                if (n->privacy_level == 0) { \
+                    if (n->parent == NULL || n->parent->privacy_level == 0) { \
+                        *public = n; \
+                        return; \
+                    } else if (*closest == NULL) { \
+                        *closest = n; \
+                    } \
+                } \
+            } \
+            n = n##_leaf(n, d->root); \
+        }
+        TESTLOOP(prev)
+        TESTLOOP(next)
+#undef TESTLOOP
+    }
+}
+
+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);
+}
+
+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);
+}
+
+node_t *next_leaf(node_t *n, node_t *r)
+{
     if (n == NULL)
         return NULL;
     node_t *p = n;
@@ -183,6 +508,24 @@ node_t *prev_leaf(node_t *n, node_t *r)
     return second_extrema(p->parent->first_child);
 }
 
+node_t *next_tiled_leaf(desktop_t *d, node_t *n, node_t *r)
+{
+    node_t *next = next_leaf(n, r);
+    if (next == NULL || is_tiled(next->client))
+        return next;
+    else
+        return next_tiled_leaf(d, next, r);
+}
+
+node_t *prev_tiled_leaf(desktop_t *d, node_t *n, node_t *r)
+{
+    node_t *prev = prev_leaf(n, r);
+    if (prev == NULL || is_tiled(prev->client))
+        return prev;
+    else
+        return prev_tiled_leaf(d, prev, r);
+}
+
 /* bool is_adjacent(node_t *a, node_t *r) */
 /* { */
 /*     node_t *f = r->parent; */
@@ -237,8 +580,7 @@ node_t *find_fence(node_t *n, direction_t dir)
     return NULL;
 }
 
-
-node_t *nearest_neighbor(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, client_select_t sel)
 {
     if (n == NULL || n->client->fullscreen
             || (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
@@ -246,13 +588,13 @@ node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir, client_select
 
     node_t *nearest = NULL;
     if (history_aware_focus)
-        nearest = nearest_from_history(d->history, n, dir, sel);
+        nearest = nearest_from_history(m, d, n, dir, sel);
     if (nearest == NULL)
-        nearest = nearest_from_distance(d, n, dir, sel);
+        nearest = nearest_from_distance(m, d, n, dir, sel);
     return nearest;
 }
 
-node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir, client_select_t sel)
+node_t *nearest_from_history(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
 {
     if (n == NULL || !is_tiled(n->client))
         return NULL;
@@ -267,465 +609,216 @@ node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir, cli
 
     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;
-        if (!node_matches(n, a, sel))
+        coordinates_t loc = {m, d, a};
+        if (!node_matches(&loc, &ref, sel))
             continue;
 
-        int rank = history_rank(f, a);
+        int rank = history_rank(d, a);
         if (rank >= 0 && rank < min_rank) {
             nearest = a;
             min_rank = rank;
         }
     }
-
-    return nearest;
-}
-
-node_t *nearest_from_distance(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
-{
-    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 *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;
-
-    for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
-        if (a == n) continue;
-        if (!node_matches(n, a, sel)) continue;
-        if (is_tiled(a->client) != is_tiled(n->client)) continue;
-        if (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;
-        }
-    }
-
-    return nearest;
-}
-
-void get_opposite(direction_t src, direction_t *dst)
-{
-    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;
-    }
-}
-
-int tiled_area(node_t *n)
-{
-    if (n == NULL)
-        return -1;
-    xcb_rectangle_t rect = n->client->tiled_rectangle;
-    return rect.width * rect.height;
-}
-
-node_t *find_biggest(desktop_t *d, node_t *c, client_select_t sel)
-{
-    if (d == NULL)
-        return NULL;
-
-    node_t *r = NULL;
-    int r_area = tiled_area(r);
-
-    for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
-        if (!is_tiled(f->client) || !node_matches(c, f, 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;
-        }
-    }
-
-    return r;
-}
-
-void move_fence(node_t *n, direction_t dir, fence_move_t mov)
-{
-    if (n == NULL)
-        return;
-
-    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);
-}
-
-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);
-}
-
-void rotate_brother(node_t *n)
-{
-    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);
-}
-
-void unrotate_brother(node_t *n)
-{
-    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;
-    }
-
-    flip_tree(n->first_child, flp);
-    flip_tree(n->second_child, flp);
-}
-
-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;
-    }
-}
-
-void arrange(monitor_t *m, desktop_t *d)
-{
-    if (d->root == NULL)
-        return;
-
-    PRINTF("arrange %s%s%s\n", (num_monitors > 1 ? m->name : ""), (num_monitors > 1 ? " " : ""), d->name);
-
-    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);
-}
-
-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 (is_leaf(n)) {
-
-        if (is_floating(n->client) && n->client->border_width != border_width) {
-            int ds = 2 * (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->border_width = 0;
-        else
-            n->client->border_width = 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;
-        }
-
-        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, n == d->focus, m == mon);
-
-    } else {
-        xcb_rectangle_t first_rect;
-        xcb_rectangle_t second_rect;
-
-        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};
-            }
-        }
-
-        apply_layout(m, d, n->first_child, first_rect, root_rect);
-        apply_layout(m, d, n->second_child, second_rect, root_rect);
-    }
-}
-
-void insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
-{
-    if (d == NULL || n == NULL)
-        return;
-
-    PRINTF("insert node %X\n", n->client->window);
-
-    /* n: new leaf node */
-    /* c: new container node */
-    /* f: focus or insertion anchor */
-    /* p: parent of focus */
-    /* g: grand parent of focus */
-
-    if (f == NULL) {
-        d->root = n;
-    } else {
-        node_t *c = make_node();
-        node_t *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);
-    }
-    put_status();
+
+    return nearest;
 }
 
-void pseudo_focus(desktop_t *d, node_t *n)
+node_t *nearest_from_distance(monitor_t *m, desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
+{
+    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 *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 ||
+                !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;
+        }
+    }
+
+    return nearest;
+}
+
+void get_opposite(direction_t src, direction_t *dst)
+{
+    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;
+    }
+}
+
+int tiled_area(node_t *n)
+{
+    if (n == NULL)
+        return -1;
+    xcb_rectangle_t rect = n->client->tiled_rectangle;
+    return rect.width * rect.height;
+}
+
+node_t *find_biggest(monitor_t *m, desktop_t *d, node_t *n, client_select_t sel)
+{
+    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_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;
+        }
+    }
+
+    return r;
+}
+
+void move_fence(node_t *n, direction_t dir, fence_move_t mov)
 {
-    if (n == NULL || d->focus == n)
+    if (n == NULL)
         return;
-    d->focus = n;
-    history_add(d->history, n);
-    stack(d, n);
+
+    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);
 }
 
-void focus_node(monitor_t *m, desktop_t *d, node_t *n)
+void rotate_tree(node_t *n, int deg)
 {
-    if (n == NULL && d->root != NULL)
+    if (n == NULL || is_leaf(n) || deg == 0)
         return;
 
-    if (mon->desk != d)
-        clear_input_focus();
+    node_t *tmp;
 
-    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);
+    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 (d->focus != n) {
-        window_draw_border(d->focus, false, true);
-        window_draw_border(n, true, true);
+    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;
     }
 
-    select_desktop(m, d);
+    rotate_tree(n->first_child, deg);
+    rotate_tree(n->second_child, deg);
+}
 
-    if (n == NULL) {
-        ewmh_update_active_window();
+void rotate_brother(node_t *n)
+{
+    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);
+}
 
-    PRINTF("focus node %X\n", n->client->window);
+void unrotate_brother(node_t *n)
+{
+    unrotate_tree(brother_tree(n), n->birth_rotation);
+}
 
-    n->client->urgent = false;
+void flip_tree(node_t *n, flip_t flp)
+{
+    if (n == NULL || is_leaf(n))
+        return;
 
-    pseudo_focus(d, n);
-    set_input_focus(n);
+    node_t *tmp;
 
-    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();
+    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;
     }
 
-    ewmh_update_active_window();
+    flip_tree(n->first_child, flp);
+    flip_tree(n->second_child, flp);
 }
 
-void update_current(void)
+int balance_tree(node_t *n)
 {
-    focus_node(mon, mon->desk, mon->desk->focus);
+    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(desktop_t *d, node_t *n)
+void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
 {
     if (d == NULL || n == NULL)
         return;
@@ -738,8 +831,18 @@ void unlink_node(desktop_t *d, node_t *n)
         d->root = NULL;
         d->focus = NULL;
     } else {
+        if (n == d->focus) {
+            d->focus = history_get_node(d, n);
+            if (d->focus == NULL)
+                d->focus = first_extrema(d->root);
+        }
+
+        if (n->client->private)
+            update_privacy_level(n, false);
+
         node_t *b;
         node_t *g = p->parent;
+
         if (is_first_child(n)) {
             b = p->second_child;
             if (!n->vacant)
@@ -749,7 +852,9 @@ void unlink_node(desktop_t *d, node_t *n)
             if (!n->vacant)
                 unrotate_tree(b, n->birth_rotation);
         }
+
         b->parent = g;
+
         if (g != NULL) {
             if (is_first_child(p))
                 g->first_child = b;
@@ -762,31 +867,31 @@ void unlink_node(desktop_t *d, node_t *n)
         b->birth_rotation = p->birth_rotation;
         n->parent = NULL;
         free(p);
-
-        if (n == d->focus)
-            d->focus = history_get(d->history, 1);
-
         update_vacant_state(b->parent);
     }
+    if (n->client->sticky)
+        m->num_sticky--;
     put_status();
 }
 
-void remove_node(desktop_t *d, node_t *n)
+void remove_node(monitor_t *m, desktop_t *d, node_t *n)
 {
     if (n == NULL)
         return;
 
     PRINTF("remove node %X\n", n->client->window);
 
-    unlink_node(d, n);
-    history_remove(d->history, n);
+    bool focused = (n == mon->desk->focus);
+    unlink_node(m, d, n);
+    history_remove(d, n);
+    remove_stack_node(n);
     free(n->client);
     free(n);
 
     num_clients--;
     ewmh_update_client_list();
 
-    if (mon->desk == d)
+    if (focused)
         update_current();
 }
 
@@ -803,20 +908,21 @@ void destroy_tree(node_t *n)
     destroy_tree(second_tree);
 }
 
-void swap_nodes(node_t *n1, node_t *n2)
+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)
-        return;
+    if (n1 == NULL || n2 == NULL || n1 == n2 || (d1 != d2 && (n1->client->sticky || n2->client->sticky)))
+        return false;
 
-    PUTS("swap nodes");
+    PRINTF("swap nodes %X %X", n1->client->window, n2->client->window);
 
-    /* (n1 and n2 are leaves) */
     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;
+    int pl1 = n1->privacy_level;
+    int pl2 = n2->privacy_level;
 
     if (pn1 != NULL) {
         if (n1_first_child)
@@ -836,169 +942,106 @@ void swap_nodes(node_t *n1, node_t *n2)
     n2->parent = pn1;
     n1->birth_rotation = br2;
     n2->birth_rotation = br1;
+    n1->privacy_level = pl2;
+    n2->privacy_level = pl1;
 
     if (n1->vacant != n2->vacant) {
         update_vacant_state(n1->parent);
         update_vacant_state(n2->parent);
     }
 
-    /* If we ever need to generalize: */
-    /* if (d1 != d2) { */
-    /*     if (d1->root == n1) */
-    /*         d1->root = n2; */
-    /*     if (d1->focus == n1) */
-    /*         d1->focus = n2; */
-    /*     if (d1->last_focus == n1) */
-    /*         d1->last_focus = n2; */
-    /*     if (d2->root == n2) */
-    /*         d2->root = n1; */
-    /*     if (d2->focus == n2) */
-    /*         d2->focus = n1; */
-    /*     if (d2->last_focus == n2) */
-    /*         d2->last_focus = n1; */
-    /* } */
-}
-
-void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, node_t *n)
-{
-    if (n == NULL || dd == ds)
-        return;
-
-    PRINTF("transfer node %X\n", n->client->window);
-
-    unlink_node(ds, n);
-    history_remove(ds->history, n);
-    insert_node(md, dd, n, dd->focus);
-    ewmh_set_wm_desktop(n, dd);
-
-    if (ds == ms->desk && dd != md->desk) {
-        if (n == ds->focus)
-            clear_input_focus();
-        window_hide(n->client->window);
+    if (n1->client->private != n2->client->private) {
+        n1->client->private = !n1->client->private;
+        n2->client->private = !n2->client->private;
     }
 
-    fit_monitor(md, n->client);
+    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) {
+            translate_client(m2, m1, n2->client);
+            translate_client(m1, m2, n1->client);
+        }
 
-    if (ds != ms->desk && dd == md->desk)
-        window_show(n->client->window);
+        ewmh_set_wm_desktop(n1, d2);
+        ewmh_set_wm_desktop(n2, d1);
+        history_swap_nodes(m1, d1, n1, m2, d2, n2);
 
-    pseudo_focus(dd, n);
+        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);
+        }
 
-    arrange(ms, ds);
-    arrange(md, dd);
+        update_input_focus();
+    }
 
-    if (ds == ms->desk || dd == md->desk)
-        update_current();
+    return true;
 }
 
-void transplant_node(monitor_t *m, desktop_t *d, node_t *n1, node_t *n2)
+bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
 {
-    if (n1 == n2)
-        return;
-    bool was_focused = (d->focus == n1);
-    unlink_node(d, n1);
-    insert_node(m, d, n1, n2);
-    if (was_focused)
-        pseudo_focus(d, n1);
-}
+    if (ns == NULL || ns == nd || (sticky_still && ns->client->sticky))
+        return false;
 
-void select_monitor(monitor_t *m)
-{
-    if (mon == m)
-        return;
+    PRINTF("transfer node %X\n", ns->client->window);
 
-    PRINTF("select monitor %s\n", m->name);
+    bool focused = (ns == mon->desk->focus);
+    bool active = (ns == ds->focus);
 
-    last_mon = mon;
-    mon = m;
+    if (focused)
+        clear_input_focus();
 
-    if (pointer_follows_monitor)
-        center_pointer(m);
+    unlink_node(ms, ds, ns);
+    insert_node(md, dd, ns, nd);
 
-    ewmh_update_current_desktop();
-    put_status();
-}
+    if (md != ms)
+        translate_client(ms, md, ns->client);
 
-monitor_t *nearest_monitor(monitor_t *m, direction_t dir, desktop_select_t sel)
-{
-    int dmin = INT_MAX;
-    monitor_t *nearest = NULL;
-    xcb_rectangle_t rect = m->rectangle;
-    for (monitor_t *f = mon_head; f != NULL; f = f->next) {
-        if (f == m)
-            continue;
-        if (!desktop_matches(f->desk, sel))
-            continue;
-        xcb_rectangle_t r = f->rectangle;
-        if ((dir == DIR_LEFT && r.x < rect.x) ||
-                (dir == DIR_RIGHT && r.x >= (rect.x + rect.width)) ||
-                (dir == DIR_UP && r.y < rect.y) ||
-                (dir == DIR_DOWN && r.y >= (rect.y + rect.height))) {
-            int d = abs((r.x + r.width / 2) - (rect.x + rect.width / 2)) +
-                abs((r.y + r.height / 2) - (rect.y + rect.height / 2));
-            if (d < dmin) {
-                dmin = d;
-                nearest = f;
-            }
+    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);
         }
+        if (ns->client->fullscreen && dd->focus != ns)
+            set_fullscreen(ns, false);
     }
-    return nearest;
-}
-
-void select_desktop(monitor_t *m, desktop_t *d)
-{
-    select_monitor(m);
-
-    if (d == mon->desk)
-        return;
-
-    PRINTF("select desktop %s\n", d->name);
-
-    desktop_show(d);
-    desktop_hide(mon->desk);
-
-    mon->last_desk = mon->desk;
-    mon->desk = d;
 
-    ewmh_update_current_desktop();
-    put_status();
-}
-
-monitor_t *closest_monitor(monitor_t *m, cycle_dir_t dir, desktop_select_t sel)
-{
-    monitor_t *f = (dir == CYCLE_PREV ? m->prev : m->next);
-    if (f == NULL)
-        f = (dir == CYCLE_PREV ? mon_tail : mon_head);
+    history_transfer_node(md, dd, ns);
+    stack(ns, STACK_BELOW);
 
-    while (f != m) {
-        if (desktop_matches(f->desk, sel))
-            return f;
-        f = (dir == CYCLE_PREV ? m->prev : m->next);
-        if (f == NULL)
-            f = (dir == CYCLE_PREV ? mon_tail : mon_head);
+    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();
     }
 
-    return NULL;
-}
-
-desktop_t *closest_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, desktop_select_t sel)
-{
-    desktop_t *f = (dir == CYCLE_PREV ? d->prev : d->next);
-    if (f == NULL)
-        f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
-
-    while (f != d) {
-        if (desktop_matches(f, sel))
-            return f;
-        f = (dir == CYCLE_PREV ? f->prev : f->next);
-        if (f == NULL)
-            f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
-    }
+    arrange(ms, ds);
+    if (ds != dd)
+        arrange(md, dd);
 
-    return NULL;
+    return true;
 }
 
-node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
+node_t *closest_node(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
 {
     if (n == NULL)
         return NULL;
@@ -1007,8 +1050,10 @@ node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t s
     if (f == NULL)
         f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
 
+    coordinates_t ref = {m, d, n};
     while (f != n) {
-        if (node_matches(n, f, sel))
+        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)
@@ -1019,16 +1064,16 @@ node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t s
 
 void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
 {
-    if (d == NULL || d->root == NULL || is_leaf(d->root))
+    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);
     if (dir == CIRCULATE_FORWARD)
-        for (node_t *s = second_extrema(d->root), *f = prev_leaf(s, d->root); f != NULL; s = prev_leaf(f, d->root), f = prev_leaf(s, d->root))
-            swap_nodes(f, s);
+        for (node_t *s = second_extrema(d->root), *f = prev_tiled_leaf(d, s, d->root); f != NULL; s = prev_tiled_leaf(d, f, d->root), f = prev_tiled_leaf(d, s, d->root))
+            swap_nodes(m, d, f, m, d, s);
     else
-        for (node_t *f = first_extrema(d->root), *s = next_leaf(f, d->root); s != NULL; f = next_leaf(s, d->root), s = next_leaf(f, d->root))
-            swap_nodes(f, s);
+        for (node_t *f = first_extrema(d->root), *s = next_tiled_leaf(d, f, d->root); s != NULL; f = next_tiled_leaf(d, s, d->root), s = next_tiled_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
@@ -1051,17 +1096,9 @@ void update_vacant_state(node_t *n)
     }
 }
 
-void fit_monitor(monitor_t *m, client_t *c)
+void update_privacy_level(node_t *n, bool value)
 {
-    xcb_rectangle_t crect = c->floating_rectangle;
-    xcb_rectangle_t mrect = m->rectangle;
-    while (crect.x < mrect.x)
-        crect.x += mrect.width;
-    while (crect.x > (mrect.x + mrect.width - 1))
-        crect.x -= mrect.width;
-    while (crect.y < mrect.y)
-        crect.y += mrect.height;
-    while (crect.y > (mrect.y + mrect.height - 1))
-        crect.y -= mrect.height;
-    c->floating_rectangle = crect;
+    int v = (value ? 1 : -1);
+    for (node_t *p = n; p != NULL; p = p->parent)
+        p->privacy_level += v;
 }