]> git.lizzy.rs Git - bspwm.git/blobdiff - tree.c
New setting: `history_aware_focus`
[bspwm.git] / tree.c
diff --git a/tree.c b/tree.c
index 203da15aa172e8fa4d9a0cdf6e5f8fc59077403b..a6041a92546b11bad8603093d7ad3c33353c64e2 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -2,7 +2,9 @@
 #include <stdlib.h>
 #include <string.h>
 #include <ctype.h>
+#include <limits.h>
 #include <math.h>
+#include <float.h>
 #include <xcb/xcb.h>
 #include <xcb/xcb_event.h>
 #include "settings.h"
@@ -22,7 +24,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,10 +44,19 @@ 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) {
+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));
 }
 
+void change_layout(monitor_t *m, desktop_t *d, layout_t l)
+{
+    d->layout = l;
+    arrange(m, d);
+    if (d == mon->desk)
+        put_status();
+}
+
 node_t *first_extrema(node_t *n)
 {
     if (n == NULL)
@@ -66,30 +77,43 @@ 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;
+}
+
 node_t *find_fence(node_t *n, direction_t dir)
 {
     node_t *p;
@@ -113,17 +137,142 @@ node_t *find_fence(node_t *n, direction_t dir)
 
 node_t *find_neighbor(node_t *n, direction_t dir)
 {
+    if (n == NULL)
+        return NULL;
+
     node_t *fence = find_fence(n, dir);
 
     if (fence == NULL)
         return NULL;
 
+    node_t *nearest = NULL;
+
     if (dir == DIR_UP || dir == DIR_LEFT)
-        return second_extrema(fence->first_child);
+        nearest = second_extrema(fence->first_child);
     else if (dir == DIR_DOWN || dir == DIR_RIGHT)
-        return first_extrema(fence->second_child);
+        nearest = first_extrema(fence->second_child);
 
-    return NULL;
+    return nearest;
+}
+
+node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir)
+{
+    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)
+        target = target->first_child;
+    else if (dir == DIR_DOWN || dir == DIR_RIGHT)
+        target = target->second_child;
+
+    node_t *nearest = NULL;
+    int min_rank = INT_MAX;
+
+    for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
+        if (!is_tiled(a->client) || !is_adjacent(a, target) || a == n)
+            continue;
+        int rank = history_rank(f, a);
+        if (rank >= 0 && rank < min_rank) {
+            nearest = a;
+            min_rank = rank;
+        }
+    }
+
+    return nearest;
+}
+
+node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir)
+{
+    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 (is_tiled(a->client) != is_tiled(n->client)
+                || (is_tiled(a->client) && !is_adjacent(a, target))
+                || a == n)
+            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)
+{
+    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)) {
+        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)
@@ -142,7 +291,7 @@ void move_fence(node_t *n, direction_t dir, fence_move_t mov)
 
 void rotate_tree(node_t *n, rotate_t rot)
 {
-    if (n == NULL || is_leaf(n))
+    if (n == NULL || is_leaf(n) || rot == ROTATE_IDENTITY)
         return;
 
     node_t *tmp;
@@ -167,9 +316,95 @@ 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, rotate_t rot)
+{
+    switch(rot) {
+        case ROTATE_CLOCKWISE:
+            rotate_tree(n, ROTATE_COUNTER_CLOCKWISE);
+            break;
+        case ROTATE_COUNTER_CLOCKWISE:
+            rotate_tree(n, ROTATE_CLOCKWISE);
+            break;
+        case ROTATE_IDENTITY:
+        case ROTATE_FULL_CYCLE:
+            break;
+    }
+}
+
+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))
+        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);
+}
+
+void list_history(char *rsp)
+{
+    char line[MAXLEN];
+    for (monitor_t *m = mon_head; m != NULL; m = m->next)
+        for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+            snprintf(line, sizeof(line), "%s\n", d->name);
+            strncat(rsp, line, REMLEN(rsp));
+            for (node_list_t *a = d->history->tail; a != NULL; a = a->prev) {
+                snprintf(line, sizeof(line), "  %X\n", a->node->client->window);
+                strncat(rsp, line, REMLEN(rsp));
+            }
+        }
+}
+
+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;
@@ -178,8 +413,6 @@ void arrange(monitor_t *m, desktop_t *d)
     rect.y += m->top_padding + wg;
     rect.width -= m->left_padding + m->right_padding + wg;
     rect.height -= m->top_padding + m->bottom_padding + wg;
-    if (focus_follows_mouse)
-        get_pointer_position(&pointer_position);
     apply_layout(m, d, d->root, rect, rect);
 }
 
@@ -191,8 +424,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);
@@ -200,24 +431,32 @@ 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;
+                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);
@@ -264,7 +503,7 @@ void insert_node(monitor_t *m, desktop_t *d, node_t *n)
         node_t *dad = make_node();
         node_t *fopar = focus->parent;
         n->parent = dad;
-        n->client->born_as = split_mode;
+        dad->birth_rotation = focus->birth_rotation;
         switch (split_mode) {
             case MODE_AUTOMATIC:
                 if (fopar == NULL) {
@@ -290,15 +529,19 @@ void insert_node(monitor_t *m, desktop_t *d, node_t *n)
                     dad->split_type = fopar->split_type;
                     dad->split_ratio = fopar->split_ratio;
                     fopar->parent = dad;
+                    rotate_t rot;
                     if (is_first_child(focus)) {
                         dad->first_child = n;
                         dad->second_child = fopar;
-                        rotate_tree(fopar, ROTATE_CLOCKWISE);
+                        rot = ROTATE_CLOCKWISE;
                     } else {
                         dad->first_child = fopar;
                         dad->second_child = n;
-                        rotate_tree(fopar, ROTATE_COUNTER_CLOCKWISE);
+                        rot = ROTATE_COUNTER_CLOCKWISE;
                     }
+                    if (!is_floating(n->client))
+                        rotate_tree(fopar, rot);
+                    n->birth_rotation = rot;
                 }
                 break;
             case MODE_MANUAL:
@@ -311,6 +554,7 @@ void insert_node(monitor_t *m, desktop_t *d, node_t *n)
                 dad->split_ratio = focus->split_ratio;
                 dad->parent = fopar;
                 focus->parent = dad;
+                focus->birth_rotation = ROTATE_IDENTITY;
                 switch (split_dir) {
                     case DIR_LEFT:
                         dad->split_type = TYPE_VERTICAL;
@@ -341,60 +585,73 @@ void insert_node(monitor_t *m, desktop_t *d, node_t *n)
         if (focus->vacant)
             update_vacant_state(fopar);
     }
+    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 (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->desk != d)
+        clear_input_focus();
+
+    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 (focus_follows_mouse)
-            get_pointer_position(&pointer_position);
-        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);
     }
 
-    if (d->focus != n) {
-        d->last_focus = d->focus;
-        d->focus = n;
+    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);
+
+    xcb_set_input_focus(dpy, XCB_INPUT_FOCUS_POINTER_ROOT, n->client->window, XCB_CURRENT_TIME);
+
+    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();
-    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)
@@ -409,19 +666,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) {
@@ -433,26 +688,22 @@ 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);
     }
+    history_remove(d->history, n);
+    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);
@@ -493,6 +744,8 @@ void swap_nodes(node_t *n1, node_t *n2)
     node_t *pn2 = n2->parent;
     bool n1_first_child = is_first_child(n1);
     bool n2_first_child = is_first_child(n2);
+    rotate_t br1 = n1->birth_rotation;
+    rotate_t br2 = n2->birth_rotation;
 
     if (pn1 != NULL) {
         if (n1_first_child)
@@ -510,16 +763,34 @@ void swap_nodes(node_t *n1, 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 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);
@@ -529,20 +800,23 @@ void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, n
     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);
     }
 
     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();
@@ -550,57 +824,48 @@ void transfer_node(monitor_t *ms, desktop_t *ds, monitor_t *md, desktop_t *dd, n
 
 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)
+void select_desktop(monitor_t *m, desktop_t *d)
 {
-    if (d == NULL || d == mon->desk)
+    select_monitor(m);
+
+    if (d == mon->desk)
         return;
 
     PRINTF("select desktop %s\n", d->name);
 
-    if (visible) {
-        node_t *n = first_extrema(d->root);
-
-        while (n != NULL) {
-            window_show(n->client->window);
-            n = next_leaf(n);
-        }
-
-        n = first_extrema(mon->desk->root);
-
-        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 *m = NULL;
     if (dir == CYCLE_NEXT)
-        select_monitor((mon->next == NULL ? mon_head : mon->next));
+        m = (mon->next == NULL ? mon_head : mon->next);
     else if (dir == CYCLE_PREV)
-        select_monitor((mon->prev == NULL ? mon_tail : mon->prev));
+        m = (mon->prev == NULL ? mon_tail : mon->prev);
+    focus_node(m, m->desk, m->desk->focus);
 }
 
 void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t skip)
@@ -610,10 +875,10 @@ void cycle_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, skip_desktop_t s
         f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
 
     while (f != d) {
-        if (skip == DESKTOP_SKIP_NONE 
+        if (skip == DESKTOP_SKIP_NONE
                 || (skip == DESKTOP_SKIP_FREE && f->root != NULL)
                 || (skip == DESKTOP_SKIP_OCCUPIED && f->root == NULL)) {
-            select_desktop(f);
+            focus_node(m, f, f->focus);
             return;
         }
         f = (dir == CYCLE_PREV ? f->prev : f->next);
@@ -629,7 +894,7 @@ void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_cli
 
     PUTS("cycle leaf");
 
-    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));
 
@@ -638,10 +903,10 @@ void cycle_leaf(monitor_t *m, desktop_t *d, node_t *n, cycle_dir_t dir, skip_cli
         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);
+            focus_node(m, d, f);
             return;
         }
-        f = (dir == CYCLE_PREV ? prev_leaf(f) : next_leaf(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));
     }
@@ -656,7 +921,7 @@ void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip
 
     node_t *x = NULL;
 
-    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 (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))
@@ -668,24 +933,25 @@ void nearest_leaf(monitor_t *m, desktop_t *d, node_t *n, nearest_arg_t dir, skip
                         && (x == NULL || f->client->uid < x->client->uid)))
                 x = f;
 
-    focus_node(m, d, x, true);
+    focus_node(m, d, x);
 }
 
-void circulate_leaves(monitor_t *m, desktop_t *d, circulate_dir_t dir) {
+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;
     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))
+        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))
+        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, par->first_child);
     else
-        focus_node(m, d, par->second_child, true);
+        focus_node(m, d, par->second_child);
 }
 
 void update_vacant_state(node_t *n)
@@ -723,16 +989,20 @@ 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))
+            for (node_t *n = first_extrema(d->root); n != NULL && !urgent; n = next_leaf(n, d->root))
                 urgent |= n->client->urgent;
-            fprintf(status_fifo, "%c%c%s:", (m->desk == d ? 'D' : (d->root != NULL ? 'd' : '_')), (urgent ? '!' : '_'), d->name);
+            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:W%X\n", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"), (mon->desk->focus == NULL ? 0 : mon->desk->focus->client->window));
+    if (mon != NULL && mon->desk != NULL)
+        fprintf(status_fifo, "L%s", (mon->desk->layout == LAYOUT_TILED ? "tiled" : "monocle"));
+    fprintf(status_fifo, "\n");
     fflush(status_fifo);
 }
 
@@ -784,17 +1054,15 @@ void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
 
     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' : '-'));
+        snprintf(line, sizeof(line), "%c %s %X %u %u %ux%u%+i%+i %c%c%c%c%c", (n->birth_rotation == ROTATE_CLOCKWISE ? 'a' : (n->birth_rotation == ROTATE_COUNTER_CLOCKWISE ? 'c' : '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);
+        snprintf(line, sizeof(line), "%c %c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), (n->birth_rotation == ROTATE_CLOCKWISE ? 'a' : (n->birth_rotation == ROTATE_COUNTER_CLOCKWISE ? 'c' : 'm')), 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));
 
@@ -802,7 +1070,7 @@ void list(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
     list(d, n->second_child, rsp, depth + 1);
 }
 
-void restore(char *file_path)
+void restore_layout(char *file_path)
 {
     if (file_path == NULL)
         return;
@@ -813,6 +1081,8 @@ void restore(char *file_path)
         return;
     }
 
+    PUTS("restore layout");
+
     char line[MAXLEN];
     monitor_t *m = NULL;
     desktop_t *d = NULL;
@@ -883,9 +1153,10 @@ void restore(char *file_path)
             }
             n = birth;
 
+            char br;
             if (isupper(line[level])) {
                 char st;
-                sscanf(line + level, "%c %lf", &st, &n->split_ratio);
+                sscanf(line + level, "%c %c %lf", &st, &br, &n->split_ratio);
                 if (st == 'H')
                     n->split_type = TYPE_HORIZONTAL;
                 else if (st == 'V')
@@ -893,12 +1164,8 @@ void restore(char *file_path)
             } 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;
+                char floating, transient, fullscreen, urgent, locked;
+                sscanf(line + level, "%c %s %X %u %u %hux%hu%hi%hi %c%c%c%c%c", &br, 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);
                 c->floating = (floating == '-' ? false : true);
                 c->transient = (transient == '-' ? false : true);
                 c->fullscreen = (fullscreen == '-' ? false : true);
@@ -907,32 +1174,73 @@ void restore(char *file_path)
                 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;
-                    }
+                if (len >= 2 && line[len - 2] == '*')
+                    d->focus = n;
             }
+            if (br == 'a')
+                n->birth_rotation = ROTATE_CLOCKWISE;
+            else if (br == 'c')
+                n->birth_rotation = ROTATE_COUNTER_CLOCKWISE;
+            else if (br == 'm')
+                n->birth_rotation = ROTATE_IDENTITY;
         }
         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[] = {CLIENT_EVENT_MASK};
+                for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
+                    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();
+    }
+}
+
+void restore_history(char *file_path)
+{
+    if (file_path == NULL)
+        return;
+
+    FILE *snapshot = fopen(file_path, "r");
+    if (snapshot == NULL) {
+        warn("restore history: can't open file\n");
+        return;
+    }
+
+    PUTS("restore history");
+
+    char line[MAXLEN];
+    desktop_t *d = NULL;
+    unsigned int level;
+
+    while (fgets(line, sizeof(line), snapshot) != NULL) {
+        unsigned int i = strlen(line) - 1;
+        while (i > 0 && isspace(line[i]))
+            line[i--] = '\0';
+        level = 0;
+        while (level < strlen(line) && isspace(line[level]))
+            level++;
+        if (level == 0) {
+            desktop_location_t loc;
+            if (locate_desktop(line + level, &loc))
+                d = loc.desktop;
+        } else if (d != NULL) {
+            xcb_window_t win;
+            if (sscanf(line + level, "%X", &win) == 1) {
+                window_location_t loc;
+                if (locate_window(win, &loc))
+                    history_add(d->history, loc.node);
+            }
+        }
     }
 
     fclose(snapshot);