]> git.lizzy.rs Git - bspwm.git/blobdiff - tree.c
Cosmetic improvements
[bspwm.git] / tree.c
diff --git a/tree.c b/tree.c
index b616b043789ad1668d7954c01f3a73e5fb97d3db..3edd59b6cc96514f15a23489544e66d1d4a491ab 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -1,10 +1,7 @@
-#include <stdio.h>
-#include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 #include <math.h>
-#include <xcb/xcb.h>
-#include <xcb/xcb_event.h>
+#include <limits.h>
+#include <float.h>
 #include "settings.h"
 #include "helpers.h"
 #include "window.h"
@@ -22,7 +19,7 @@ bool is_tiled(client_t *c)
 {
     if (c == NULL)
         return false;
-    return (!c->floating && !c->transient && !c->fullscreen);
+    return (!c->floating && !c->fullscreen);
 }
 
 bool is_floating(client_t *c)
@@ -42,8 +39,94 @@ 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 ? INC_EXP : DEC_EXP));
+/**
+ * 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 (sel.urgency != CLIENT_URGENCY_ALL &&
+            t->client->urgent
+            ? sel.urgency == CLIENT_URGENCY_OFF
+            : sel.urgency == CLIENT_URGENCY_ON
+       ) return false;
+
+    return true;
+}
+
+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;
+
+    if (sel.urgency != DESKTOP_URGENCY_ALL &&
+            is_urgent(t)
+            ? sel.urgency == DESKTOP_URGENCY_OFF
+            : sel.urgency == DESKTOP_URGENCY_ON
+       ) return false;
+
+    return true;
+}
+
+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;
+}
+
+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 change_layout(monitor_t *m, desktop_t *d, layout_t l)
+{
+    d->layout = l;
+    arrange(m, d);
+    if (d == mon->desk)
+        put_status();
+}
+
+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 *first_extrema(node_t *n)
@@ -66,30 +149,63 @@ node_t *second_extrema(node_t *n)
         return second_extrema(n->second_child);
 }
 
-node_t *next_leaf(node_t *n)
+node_t *next_leaf(node_t *n, node_t *r)
 {
     if (n == NULL)
         return NULL;
     node_t *p = n;
-    while (is_second_child(p))
+    while (is_second_child(p) && p != r)
         p = p->parent;
-    if (p->parent == NULL)
+    if (p == r)
         return NULL;
     return first_extrema(p->parent->second_child);
 }
 
-node_t *prev_leaf(node_t *n)
+node_t *prev_leaf(node_t *n, node_t *r)
 {
     if (n == NULL)
         return NULL;
     node_t *p = n;
-    while (is_first_child(p))
+    while (is_first_child(p) && p != r)
         p = p->parent;
-    if (p->parent == NULL)
+    if (p == r)
         return NULL;
     return second_extrema(p->parent->first_child);
 }
 
+/* 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;
+}
+
 node_t *find_fence(node_t *n, direction_t dir)
 {
     node_t *p;
@@ -111,19 +227,113 @@ node_t *find_fence(node_t *n, direction_t dir)
     return NULL;
 }
 
-node_t *find_neighbor(node_t *n, direction_t dir)
+
+node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
 {
-    node_t *fence = find_fence(n, dir);
+    if (n == NULL || n->client->fullscreen
+            || (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
+        return NULL;
 
-    if (fence == NULL)
+    node_t *nearest = NULL;
+    if (history_aware_focus)
+        nearest = nearest_from_history(d->history, n, dir, sel);
+    if (nearest == NULL)
+        nearest = nearest_from_distance(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)
+{
+    if (n == NULL || !is_tiled(n->client))
         return NULL;
 
+    node_t *target = find_fence(n, dir);
+    if (target == NULL)
+        return NULL;
     if (dir == DIR_UP || dir == DIR_LEFT)
-        return second_extrema(fence->first_child);
+        target = target->first_child;
     else if (dir == DIR_DOWN || dir == DIR_RIGHT)
-        return first_extrema(fence->second_child);
+        target = target->second_child;
 
-    return NULL;
+    node_t *nearest = NULL;
+    int min_rank = INT_MAX;
+
+    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))
+            continue;
+
+        int rank = history_rank(f, 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)
@@ -134,7 +344,7 @@ int tiled_area(node_t *n)
     return rect.width * rect.height;
 }
 
-node_t *find_by_area(desktop_t *d, swap_arg_t a)
+node_t *find_biggest(desktop_t *d, node_t *c, client_select_t sel)
 {
     if (d == NULL)
         return NULL;
@@ -142,13 +352,14 @@ node_t *find_by_area(desktop_t *d, swap_arg_t a)
     node_t *r = NULL;
     int r_area = tiled_area(r);
 
-    for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f)) {
+    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 ((a == SWAP_BIGGEST && f_area > r_area)
-                || (a == SWAP_SMALLEST && f_area < r_area)) {
+        } else if (f_area > r_area) {
             r = f;
             r_area = f_area;
         }
@@ -171,23 +382,23 @@ void move_fence(node_t *n, direction_t dir, fence_move_t mov)
         change_split_ratio(fence, CHANGE_DECREASE);
 }
 
-void rotate_tree(node_t *n, rotate_t rot)
+void rotate_tree(node_t *n, int rot)
 {
-    if (n == NULL || is_leaf(n))
+    if (n == NULL || is_leaf(n) || rot == 0)
         return;
 
     node_t *tmp;
 
-    if ((rot == ROTATE_CLOCKWISE && n->split_type == TYPE_HORIZONTAL)
-            || (rot == ROTATE_COUNTER_CLOCKWISE && n->split_type == TYPE_VERTICAL)
-            || rot == ROTATE_FULL_CYCLE) {
+    if ((rot == 90 && n->split_type == TYPE_HORIZONTAL)
+            || (rot == 270 && n->split_type == TYPE_VERTICAL)
+            || rot == 180) {
         tmp = n->first_child;
         n->first_child = n->second_child;
         n->second_child = tmp;
         n->split_ratio = 1.0 - n->split_ratio;
     }
 
-    if (rot != ROTATE_FULL_CYCLE) {
+    if (rot != 180) {
         if (n->split_type == TYPE_HORIZONTAL)
             n->split_type = TYPE_VERTICAL;
         else if (n->split_type == TYPE_VERTICAL)
@@ -198,6 +409,33 @@ void rotate_tree(node_t *n, rotate_t rot)
     rotate_tree(n->second_child, rot);
 }
 
+void rotate_brother(node_t *n)
+{
+    if (n == NULL || n->parent == NULL)
+        return;
+    if (is_first_child(n))
+        rotate_tree(n->parent->second_child, n->birth_rotation);
+    else
+        rotate_tree(n->parent->first_child, 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)
+{
+    if (n == NULL || n->parent == NULL)
+        return;
+    if (is_first_child(n))
+        unrotate_tree(n->parent->second_child, n->birth_rotation);
+    else
+        unrotate_tree(n->parent->first_child, n->birth_rotation);
+}
+
 void flip_tree(node_t *n, flip_t flp)
 {
     if (n == NULL || is_leaf(n))
@@ -235,6 +473,9 @@ int balance_tree(node_t *n)
 
 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;
@@ -254,8 +495,6 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, x
     n->rectangle = rect;
 
     if (is_leaf(n)) {
-        if (n->client->fullscreen)
-            return;
 
         if (is_floating(n->client) && n->client->border_width != border_width) {
             int ds = 2 * (border_width - n->client->border_width);
@@ -263,24 +502,34 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, x
             n->client->floating_rectangle.height += ds;
         }
 
-        if (borderless_monocle && is_tiled(n->client) && d->layout == LAYOUT_MONOCLE)
+        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 (is_tiled(n->client)) {
-            if (d->layout == LAYOUT_TILED)
-                r = rect;
-            else if (d->layout == LAYOUT_MONOCLE)
-                r = root_rect;
-            int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : 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;
+        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 : 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 {
-            r = n->client->floating_rectangle;
+            /* fullscreen clients */
+            r = m->rectangle;
         }
 
         window_move_resize(n->client->window, r.x, r.y, r.width, r.height);
@@ -299,7 +548,6 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, x
                 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};
@@ -312,134 +560,157 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, x
     }
 }
 
-void insert_node(monitor_t *m, desktop_t *d, node_t *n)
+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);
 
-    node_t *focus = d->focus;
+    /* n: new leaf node */
+    /* c: new container node */
+    /* f: focus or insertion anchor */
+    /* p: parent of focus */
+    /* g: grand parent of focus */
 
-    if (focus == NULL) {
+    if (f == NULL) {
         d->root = n;
     } else {
-        node_t *dad = make_node();
-        node_t *fopar = focus->parent;
-        n->parent = dad;
-        n->client->born_as = split_mode;
-        switch (split_mode) {
+        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 (fopar == NULL) {
-                    dad->first_child = n;
-                    dad->second_child = focus;
+                if (p == NULL) {
+                    c->first_child = n;
+                    c->second_child = f;
                     if (m->rectangle.width > m->rectangle.height)
-                        dad->split_type = TYPE_VERTICAL;
+                        c->split_type = TYPE_VERTICAL;
                     else
-                        dad->split_type = TYPE_HORIZONTAL;
-                    focus->parent = dad;
-                    d->root = dad;
+                        c->split_type = TYPE_HORIZONTAL;
+                    f->parent = c;
+                    d->root = c;
                 } else {
-                    node_t *grandpa = fopar->parent;
-                    dad->parent = grandpa;
-                    if (grandpa != NULL) {
-                        if (is_first_child(fopar))
-                            grandpa->first_child = dad;
+                    node_t *g = p->parent;
+                    c->parent = g;
+                    if (g != NULL) {
+                        if (is_first_child(p))
+                            g->first_child = c;
                         else
-                            grandpa->second_child = dad;
+                            g->second_child = c;
                     } else {
-                        d->root = dad;
+                        d->root = c;
                     }
-                    dad->split_type = fopar->split_type;
-                    dad->split_ratio = fopar->split_ratio;
-                    fopar->parent = dad;
-                    if (is_first_child(focus)) {
-                        dad->first_child = n;
-                        dad->second_child = fopar;
-                        rotate_tree(fopar, ROTATE_CLOCKWISE);
+                    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 {
-                        dad->first_child = fopar;
-                        dad->second_child = n;
-                        rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
+                        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 (fopar != NULL) {
-                    if (is_first_child(focus))
-                        fopar->first_child = dad;
+                if (p != NULL) {
+                    if (is_first_child(f))
+                        p->first_child = c;
                     else
-                        fopar->second_child = dad;
+                        p->second_child = c;
                 }
-                dad->split_ratio = focus->split_ratio;
-                dad->parent = fopar;
-                focus->parent = dad;
-                switch (split_dir) {
+                c->split_ratio = f->split_ratio;
+                c->parent = p;
+                f->parent = c;
+                f->birth_rotation = 0;
+                switch (f->split_dir) {
                     case DIR_LEFT:
-                        dad->split_type = TYPE_VERTICAL;
-                        dad->first_child = n;
-                        dad->second_child = focus;
+                        c->split_type = TYPE_VERTICAL;
+                        c->first_child = n;
+                        c->second_child = f;
                         break;
                     case DIR_RIGHT:
-                        dad->split_type = TYPE_VERTICAL;
-                        dad->first_child = focus;
-                        dad->second_child = n;
+                        c->split_type = TYPE_VERTICAL;
+                        c->first_child = f;
+                        c->second_child = n;
                         break;
                     case DIR_UP:
-                        dad->split_type = TYPE_HORIZONTAL;
-                        dad->first_child = n;
-                        dad->second_child = focus;
+                        c->split_type = TYPE_HORIZONTAL;
+                        c->first_child = n;
+                        c->second_child = f;
                         break;
                     case DIR_DOWN:
-                        dad->split_type = TYPE_HORIZONTAL;
-                        dad->first_child = focus;
-                        dad->second_child = n;
+                        c->split_type = TYPE_HORIZONTAL;
+                        c->first_child = f;
+                        c->second_child = n;
                         break;
                 }
-                if (d->root == focus)
-                    d->root = dad;
-                split_mode = MODE_AUTOMATIC;
+                if (d->root == f)
+                    d->root = c;
+                f->split_mode = MODE_AUTOMATIC;
                 break;
         }
-        if (focus->vacant)
-            update_vacant_state(fopar);
+        if (f->vacant)
+            update_vacant_state(p);
     }
+    put_status();
 }
 
-void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
+void pseudo_focus(desktop_t *d, node_t *n)
 {
-    if (n == NULL)
+    if (d->focus == n)
         return;
+    d->focus = n;
+    history_add(d->history, n);
+}
 
-    PRINTF("focus node %X\n", n->client->window);
+void focus_node(monitor_t *m, desktop_t *d, node_t *n)
+{
+    if (n == NULL && d->root != NULL)
+        return;
 
-    split_mode = MODE_AUTOMATIC;
-    n->client->urgent = false;
+    if (mon->desk != d)
+        clear_input_focus();
 
-    if (is_mapped) {
-        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);
+    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);
-        }
-        xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
     }
 
-    if (!is_tiled(n->client)) {
-        if (!adaptative_raise || !might_cover(d, n))
-            window_raise(n->client->window);
-    } else {
-        window_pseudo_raise(d, n->client->window);
+    if (d->focus != n) {
+        window_draw_border(d->focus, false, true);
+        window_draw_border(n, true, true);
+    }
+
+    select_desktop(m, d);
+
+    if (n == NULL) {
+        ewmh_update_active_window();
+        return;
     }
 
+    PRINTF("focus node %X\n", n->client->window);
+
+    n->client->urgent = false;
+
+    pseudo_focus(d, n);
+    stack(d, n);
+
+    set_input_focus(n);
+
     if (focus_follows_pointer) {
         xcb_window_t win = XCB_NONE;
         query_pointer(&win, NULL);
@@ -449,22 +720,12 @@ void focus_node(monitor_t *m, desktop_t *d, node_t *n, bool is_mapped)
             disable_motion_recorder();
     }
 
-    if (d->focus != n) {
-        d->last_focus = d->focus;
-        d->focus = n;
-    }
-
     ewmh_update_active_window();
-    put_status();
 }
 
 void update_current(void)
 {
-    if (mon->desk->focus == NULL)
-        ewmh_update_active_window();
-    else
-        focus_node(mon, mon->desk, mon->desk->focus, true);
-    put_status();
+    focus_node(mon, mon->desk, mon->desk->focus);
 }
 
 void unlink_node(desktop_t *d, node_t *n)
@@ -479,19 +740,17 @@ void unlink_node(desktop_t *d, node_t *n)
     if (p == NULL) {
         d->root = NULL;
         d->focus = NULL;
-        d->last_focus = NULL;
     } else {
         node_t *b;
         node_t *g = p->parent;
-        bool n_first_child = is_first_child(n);
-        if (n_first_child) {
+        if (is_first_child(n)) {
             b = p->second_child;
-            if (n->client->born_as == MODE_AUTOMATIC)
-                rotate_tree(b, ROTATE_COUNTER_CLOCKWISE);
+            if (!n->vacant)
+                unrotate_tree(b, n->birth_rotation);
         } else {
             b = p->first_child;
-            if (n->client->born_as == MODE_AUTOMATIC)
-                rotate_tree(b, ROTATE_CLOCKWISE);
+            if (!n->vacant)
+                unrotate_tree(b, n->birth_rotation);
         }
         b->parent = g;
         if (g != NULL) {
@@ -503,31 +762,27 @@ void unlink_node(desktop_t *d, node_t *n)
             d->root = b;
         }
 
+        b->birth_rotation = p->birth_rotation;
         n->parent = NULL;
         free(p);
 
-        if (n == d->last_focus) {
-            d->last_focus = NULL;
-        } else if (n == d->focus) {
-            if (d->last_focus != NULL)
-                d->focus = d->last_focus;
-            else
-                d->focus = (n_first_child ? first_extrema(b) : second_extrema(b));
-            d->last_focus = NULL;
-        }
+        if (n == d->focus)
+            d->focus = history_get(d->history, 1);
 
         update_vacant_state(b->parent);
     }
+    put_status();
 }
 
 void remove_node(desktop_t *d, node_t *n)
 {
-    if (d == NULL || n == NULL)
+    if (n == NULL)
         return;
 
     PRINTF("remove node %X\n", n->client->window);
 
     unlink_node(d, n);
+    history_remove(d->history, n);
     free(n->client);
     free(n);
 
@@ -551,7 +806,7 @@ void destroy_tree(node_t *n)
     destroy_tree(second_tree);
 }
 
-void swap_nodes(desktop_t *d1, node_t *n1, desktop_t *d2, node_t *n2)
+void swap_nodes(node_t *n1, node_t *n2)
 {
     if (n1 == NULL || n2 == NULL || n1 == n2)
         return;
@@ -563,6 +818,8 @@ void swap_nodes(desktop_t *d1, node_t *n1, desktop_t *d2, node_t *n2)
     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;
 
     if (pn1 != NULL) {
         if (n1_first_child)
@@ -580,37 +837,41 @@ void swap_nodes(desktop_t *d1, node_t *n1, desktop_t *d2, node_t *n2)
 
     n1->parent = pn2;
     n2->parent = pn1;
+    n1->birth_rotation = br2;
+    n2->birth_rotation = br1;
 
     if (n1->vacant != n2->vacant) {
         update_vacant_state(n1->parent);
         update_vacant_state(n2->parent);
     }
 
-    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;
-    }
+    /* 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 || ds == NULL || dd == NULL || ms == NULL || md == NULL || (ms == md && dd == ds))
+    if (n == NULL || dd == ds)
         return;
 
     PRINTF("transfer node %X\n", n->client->window);
 
     unlink_node(ds, n);
-    insert_node(md, dd, 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) {
@@ -621,160 +882,161 @@ void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, n
 
     fit_monitor(md, n->client);
 
-    if (n->client->fullscreen)
-        window_move_resize(n->client->window, md->rectangle.x, md->rectangle.y, md->rectangle.width, md->rectangle.height);
-
-    if (ds != ms->desk && dd == md->desk) {
+    if (ds != ms->desk && dd == md->desk)
         window_show(n->client->window);
-        focus_node(md, dd, n, true);
-    } else {
-        focus_node(md, dd, n, false);
-    }
+
+    pseudo_focus(dd, n);
+
+    if (md->desk == dd)
+        stack(dd, n);
+
+    arrange(ms, ds);
+    arrange(md, dd);
 
     if (ds == ms->desk || dd == md->desk)
         update_current();
 }
 
+void transplant_node(monitor_t *m, desktop_t *d, node_t *n1, node_t *n2)
+{
+    bool was_focused = (d->focus == n1);
+    unlink_node(d, n1);
+    insert_node(m, d, n1, n2);
+    if (was_focused)
+        pseudo_focus(d, n1);
+}
+
 void select_monitor(monitor_t *m)
 {
-    if (m == NULL || mon == m)
+    if (mon == m)
         return;
 
     PRINTF("select monitor %s\n", m->name);
 
-    focus_node(m, m->desk, m->desk->focus, true);
-
     last_mon = mon;
     mon = m;
 
+    if (pointer_follows_monitor)
+        center_pointer(m);
+
     ewmh_update_current_desktop();
     put_status();
 }
 
-void select_desktop(desktop_t *d)
+monitor_t *nearest_monitor(monitor_t *m, direction_t dir, desktop_select_t sel)
 {
-    if (d == NULL || d == mon->desk)
-        return;
-
-    PRINTF("select desktop %s\n", d->name);
-
-    clear_input_focus();
+    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;
+            }
+        }
+    }
+    return nearest;
+}
 
-    if (visible) {
-        node_t *n = first_extrema(d->root);
+void select_desktop(monitor_t *m, desktop_t *d)
+{
+    select_monitor(m);
 
-        while (n != NULL) {
-            window_show(n->client->window);
-            n = next_leaf(n);
-        }
+    if (d == mon->desk)
+        return;
 
-        n = first_extrema(mon->desk->root);
+    PRINTF("select desktop %s\n", d->name);
 
-        while (n != NULL) {
-            window_hide(n->client->window);
-            n = next_leaf(n);
-        }
-    }
+    desktop_show(d);
+    desktop_hide(mon->desk);
 
     mon->last_desk = mon->desk;
     mon->desk = d;
 
-    update_current();
     ewmh_update_current_desktop();
     put_status();
 }
 
-void cycle_monitor(cycle_dir_t dir)
+monitor_t *closest_monitor(monitor_t *m, cycle_dir_t dir, desktop_select_t sel)
 {
-    if (dir == CYCLE_NEXT)
-        select_monitor((mon->next == NULL ? mon_head : mon->next));
-    else if (dir == CYCLE_PREV)
-        select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
+    monitor_t *f = (dir == CYCLE_PREV ? m->prev : m->next);
+    if (f == NULL)
+        f = (dir == CYCLE_PREV ? mon_tail : mon_head);
+
+    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);
+    }
+
+    return NULL;
 }
 
-void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
+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 (skip == DESKTOP_SKIP_NONE
-                || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
-                || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
-            select_desktop(f);
-            return;
-        }
+        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);
     }
+
+    return NULL;
 }
 
-void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_client_t skip)
+node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t sel)
 {
     if (n == NULL)
-        return;
-
-    PUTS("cycle leaf");
+        return NULL;
 
-    node_t *f = (dir == CYCLE_PREV ? prev_leaf(n) : next_leaf(n));
+    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));
 
     while (f != n) {
-        bool tiled = is_tiled(f->client);
-        if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !tiled) || (skip == CLIENT_SKIP_FLOATING && tiled)
-                || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
-                || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0)) {
-            focus_node(m, d, f, true);
-            return;
-        }
-        f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(f));
+        if (node_matches(n, f, 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 nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip_client_t skip)
+void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir)
 {
-    if (n == NULL)
-        return;
-
-    PUTS("nearest leaf");
-
-    node_t *x = NULL;
-
-    for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f))
-        if (skip == CLIENT_SKIP_NONE || (skip == CLIENT_SKIP_TILED && !is_tiled(f->client)) || (skip == CLIENT_SKIP_FLOATING && is_tiled(f->client))
-                || (skip == CLIENT_SKIP_CLASS_DIFFER && strcmp(f->client->class_name, n->client->class_name) == 0)
-                || (skip == CLIENT_SKIP_CLASS_EQUAL && strcmp(f->client->class_name, n->client->class_name) != 0))
-            if ((dir == NEAREST_OLDER
-                        && (f->client->uid < n->client->uid)
-                        && (x == NULL || f->client->uid > x->client->uid))
-                    || (dir == NEAREST_NEWER
-                        && (f->client->uid > n->client->uid)
-                        && (x == NULL || f->client->uid < x->client->uid)))
-                x = f;
-
-    focus_node(m, d, x, true);
-}
-
-void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
     if (d == NULL || d->root == NULL || is_leaf(d->root))
         return;
-    node_t *par = d->focus->parent;
+    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); f != NULL; s = prev_leaf(f), f = prev_leaf(s))
-            swap_nodes(d, f, d, s);
+        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);
     else
-        for (node_t *f = first_extrema(d->root), *s = next_leaf(f); s != NULL; f = next_leaf(s), s = next_leaf(f))
-            swap_nodes(d, f, d, s);
+        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);
     if (focus_first_child)
-        focus_node(m, d, par->first_child, true);
+        focus_node(m, d, p->first_child);
     else
-        focus_node(m, d, par->second_child, true);
+        focus_node(m, d, p->second_child);
 }
 
 void update_vacant_state(node_t *n)
@@ -807,225 +1069,3 @@ void fit_monitor(monitor_t *m, client_t *c)
         crect.y -= mrect.height;
     c->floating_rectangle = crect;
 }
-
-void put_status(void)
-{
-    if (status_fifo == NULL)
-        return;
-    if (status_prefix != NULL)
-        fprintf(status_fifo, "%s", status_prefix);
-    bool urgent = false;
-    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-        fprintf(status_fifo, "%c%s:", (mon == m ? 'M' : 'm'), m->name);
-        for (desktop_t *d = m->desk_head; d != NULL; d = d->next, urgent = false) {
-            for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n))
-                urgent |= n->client->urgent;
-            fprintf(status_fifo, "%c%s:", m->desk == d ? (urgent ? 'U' : 'D') : (d->root == NULL ? 'E' : (urgent ? 'u' : 'd')), d->name);
-        }
-    }
-    fprintf(status_fifo, "L%s\n", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
-    fflush(status_fifo);
-}
-
-void list_monitors(list_option_t opt, char *rsp)
-{
-    char line[MAXLEN];
-    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-        snprintf(line, sizeof(line), "%s %ux%u%+i%+i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y);
-        strncat(rsp, line, REMLEN(rsp));
-        if (m == mon)
-            strncat(rsp, " #\n", REMLEN(rsp));
-        else if (m == last_mon)
-            strncat(rsp, " ~\n", REMLEN(rsp));
-        else
-            strncat(rsp, "\n", REMLEN(rsp));
-        if (opt == LIST_OPTION_VERBOSE)
-            list_desktops(m, opt, 1, rsp);
-    }
-}
-
-void list_desktops(monitor_t *m, list_option_t opt, unsigned int depth, char *rsp)
-{
-    char line[MAXLEN];
-    for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
-        for (unsigned int i = 0; i < depth; i++)
-            strncat(rsp, "  ", REMLEN(rsp));
-        snprintf(line, sizeof(line), "%s %c", d->name, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
-        strncat(rsp, line, REMLEN(rsp));
-        if (d == m->desk)
-            strncat(rsp, " @\n", REMLEN(rsp));
-        else if (d == m->last_desk)
-            strncat(rsp, " ~\n", REMLEN(rsp));
-        else
-            strncat(rsp, "\n", REMLEN(rsp));
-        if (opt == LIST_OPTION_VERBOSE)
-            list(d, d->root, rsp, depth + 1);
-    }
-}
-
-void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
-{
-    if (n == NULL)
-        return;
-
-    char line[MAXLEN];
-
-    for (unsigned int i = 0; i < depth; i++)
-        strncat(rsp, "  ", REMLEN(rsp));
-
-    if (is_leaf(n)) {
-        client_t *c = n->client;
-        snprintf(line, sizeof(line), "%c %s %X %u %u %ux%u%+i%+i %c%c%c%c%c", (c->born_as == MODE_AUTOMATIC ? 'a' : 'm'), c->class_name, c->window, c->uid, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'));
-    } else {
-        snprintf(line, sizeof(line), "%c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), n->split_ratio);
-    }
-
-    strncat(rsp, line, REMLEN(rsp));
-
-    if (n == d->focus)
-        strncat(rsp, " *\n", REMLEN(rsp));
-    else if (n == d->last_focus)
-        strncat(rsp, " ~\n", REMLEN(rsp));
-    else
-        strncat(rsp, "\n", REMLEN(rsp));
-
-    list(d, n->first_child, rsp, depth + 1);
-    list(d, n->second_child, rsp, depth + 1);
-}
-
-void restore(char *file_path)
-{
-    if (file_path == NULL)
-        return;
-
-    FILE *snapshot = fopen(file_path, "r");
-    if (snapshot == NULL) {
-        warn("restore: can't open file\n");
-        return;
-    }
-
-    char line[MAXLEN];
-    monitor_t *m = NULL;
-    desktop_t *d = NULL;
-    node_t *n = NULL;
-    num_clients = 0;
-    unsigned int level, last_level = 0, max_uid = 0;
-    bool aborted = false;
-
-    while (!aborted && fgets(line, sizeof(line), snapshot) != NULL) {
-        unsigned int len = strlen(line);
-        level = 0;
-        while (level < strlen(line) && isspace(line[level]))
-            level++;
-        if (level == 0) {
-            if (m == NULL)
-                m = mon_head;
-            else
-                m = m->next;
-            if (len >= 2)
-                switch (line[len - 2]) {
-                    case '#':
-                        mon = m;
-                        break;
-                    case '~':
-                        last_mon = m;
-                        break;
-                }
-        } else if (level == 2) {
-            if (d == NULL)
-                d = m->desk_head;
-            else
-                d = d->next;
-            int i = len - 1;
-            while (i > 0 && !isupper(line[i]))
-                i--;
-            if (line[i] == 'M')
-                d->layout = LAYOUT_MONOCLE;
-            else if (line[i] == 'T')
-                d->layout = LAYOUT_TILED;
-            if (len >= 2)
-                switch (line[len - 2]) {
-                    case '@':
-                        m->desk = d;
-                        break;
-                    case '~':
-                        m->last_desk = d;
-                        break;
-                }
-        } else {
-            node_t *birth = make_node();
-            if (level == 4) {
-                empty_desktop(d);
-                d->root = birth;
-            } else {
-                if (level > last_level) {
-                    n->first_child = birth;
-                } else {
-                    do {
-                        n = n->parent;
-                    } while (n != NULL && n->second_child != NULL);
-                    if (n == NULL) {
-                        warn("restore: file is malformed\n");
-                        aborted = true;
-                    }
-                    n->second_child = birth;
-                }
-                birth->parent = n;
-            }
-            n = birth;
-
-            if (isupper(line[level])) {
-                char st;
-                sscanf(line + level, "%c %lf", &st, &n->split_ratio);
-                if (st == 'H')
-                    n->split_type = TYPE_HORIZONTAL;
-                else if (st == 'V')
-                    n->split_type = TYPE_VERTICAL;
-            } else {
-                client_t *c = make_client(XCB_NONE);
-                num_clients++;
-                char ba, floating, transient, fullscreen, urgent, locked;
-                sscanf(line + level, "%c %s %X %u %u %hux%hu%hi%hi %c%c%c%c%c", &ba, c->class_name, &c->window, &c->uid, &c->border_width, &c->floating_rectangle.width, &c->floating_rectangle.height, &c->floating_rectangle.x, &c->floating_rectangle.y, &floating, &transient, &fullscreen, &urgent, &locked);
-                if (ba == 'a')
-                    c->born_as = MODE_AUTOMATIC;
-                else if (ba == 'm')
-                    c->born_as = MODE_MANUAL;
-                c->floating = (floating == '-' ? false : true);
-                c->transient = (transient == '-' ? false : true);
-                c->fullscreen = (fullscreen == '-' ? false : true);
-                c->urgent = (urgent == '-' ? false : true);
-                c->locked = (locked == '-' ? false : true);
-                if (c->uid > max_uid)
-                    max_uid = c->uid;
-                n->client = c;
-                if (len >= 2)
-                    switch (line[len - 2]) {
-                        case '*':
-                            d->focus = n;
-                            break;
-                        case '~':
-                            d->last_focus = n;
-                            break;
-                    }
-            }
-        }
-        last_level = level;
-    }
-
-    fclose(snapshot);
-
-    if (!aborted) {
-        client_uid = max_uid + 1;
-        for (monitor_t *m = mon_head; m != NULL; m = m->next)
-            for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
-                for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n)) {
-                    uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK_FFP : CLIENT_EVENT_MASK)};
-                    xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
-                    if (n->client->floating) {
-                        n->vacant = true;
-                        update_vacant_state(n->parent);
-                    }
-                }
-        ewmh_update_current_desktop();
-    }
-}