]> git.lizzy.rs Git - bspwm.git/blobdiff - src/tree.c
Add new setting: cancel_birth_rotation
[bspwm.git] / src / tree.c
index 08e6c9520ba51eb85693ed3b588a33724fba545c..0d645a162c6ea97e2aaaf0db056847c94eebe8d2 100644 (file)
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
+#include <limits.h>
 #include "bspwm.h"
 #include "desktop.h"
 #include "ewmh.h"
@@ -47,7 +48,7 @@ void arrange(monitor_t *m, desktop_t *d)
 
        layout_t l = d->layout;
 
-       if (single_monocle && tiled_count(d->root) <= 1) {
+       if (single_monocle && tiled_count(d->root, true) <= 1) {
                l = LAYOUT_MONOCLE;
        }
 
@@ -96,7 +97,7 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectang
                }
 
                unsigned int bw;
-               if ((borderless_monocle && n->client->state == STATE_TILED && l == LAYOUT_MONOCLE)
+               if ((borderless_monocle && l == LAYOUT_MONOCLE && IS_TILED(n->client))
                    || n->client->state == STATE_FULLSCREEN) {
                        bw = 0;
                } else {
@@ -156,10 +157,28 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectang
                        unsigned int fence;
                        if (n->split_type == TYPE_VERTICAL) {
                                fence = rect.width * n->split_ratio;
+                               if ((n->first_child->constraints.min_width + n->second_child->constraints.min_width) <= rect.width) {
+                                       if (fence < n->first_child->constraints.min_width) {
+                                               fence = n->first_child->constraints.min_width;
+                                               n->split_ratio = (double) fence / (double) rect.width;
+                                       } else if (fence > (uint16_t) (rect.width - n->second_child->constraints.min_width)) {
+                                               fence = (rect.width - n->second_child->constraints.min_width);
+                                               n->split_ratio = (double) fence / (double) rect.width;
+                                       }
+                               }
                                first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
                                second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
                        } else {
                                fence = rect.height * n->split_ratio;
+                               if ((n->first_child->constraints.min_height + n->second_child->constraints.min_height) <= rect.height) {
+                                       if (fence < n->first_child->constraints.min_height) {
+                                               fence = n->first_child->constraints.min_height;
+                                               n->split_ratio = (double) fence / (double) rect.height;
+                                       } else if (fence > (uint16_t) (rect.height - n->second_child->constraints.min_height)) {
+                                               fence = (rect.height - n->second_child->constraints.min_height);
+                                               n->split_ratio = (double) fence / (double) rect.height;
+                                       }
+                               }
                                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};
                        }
@@ -306,11 +325,11 @@ node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
                                p = f->parent;
                        }
                        if (f->presel == NULL && (f->private || private_count(f->parent) > 0)) {
-                               xcb_rectangle_t rect = get_rectangle(d, f);
+                               xcb_rectangle_t rect = get_rectangle(m, d, f);
                                presel_dir(m, d, f, (rect.width >= rect.height ? DIR_EAST : DIR_SOUTH));
                        }
                }
-               if (f->presel == NULL && p != NULL && p->vacant) {
+               while (f->presel == NULL && p != NULL && (f->vacant || brother_tree(f)->vacant)) {
                        f = p;
                        p = f->parent;
                }
@@ -357,8 +376,8 @@ node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
                                        c->second_child = n;
                                        rot = 270;
                                }
-                               n->birth_rotation = rot;
                                if (!n->vacant) {
+                                       n->birth_rotation = rot;
                                        rotate_tree(p, rot);
                                }
                        }
@@ -400,6 +419,7 @@ node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
                                d->root = c;
                        }
                        cancel_presel(m, d, f);
+                       set_marked(m, d, n, false);
                }
        }
 
@@ -420,19 +440,11 @@ void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
 
 bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
 {
-       if (d == NULL) {
-               d = history_last_desktop(m, NULL);
-               if (d == NULL) {
-                       d = m->desk_head;
-               }
-       }
-
-       if (d == NULL) {
-               return false;
-       }
-
        if (n == NULL && d->root != NULL) {
-               n = history_last_node(d, NULL);
+               n = d->focus;
+               if (n == NULL) {
+                       n = history_last_node(d, NULL);
+               }
                if (n == NULL) {
                        n = first_focusable_leaf(d->root);
                }
@@ -457,10 +469,8 @@ bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
                draw_border(n, true, (m == mon));
        }
 
-       activate_desktop(m, d);
-
        d->focus = n;
-       history_add(m, d, n);
+       history_add(m, d, n, false);
 
        put_status(SBSC_MASK_REPORT);
 
@@ -478,7 +488,9 @@ void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n
        if (n == NULL) {
                return;
        } else if (n->sticky) {
-               transfer_node(m, ds, n, m, dd, dd->focus);
+               sticky_still = false;
+               transfer_node(m, ds, n, m, dd, dd->focus, false);
+               sticky_still = true;
        } else {
                /* we need references to the children because n might be freed after
                 * the first recursive call */
@@ -492,7 +504,10 @@ void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n
 bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
 {
        if (m == NULL) {
-               m = history_last_monitor(NULL);
+               m = mon;
+               if (m == NULL) {
+                       m = history_last_monitor(NULL);
+               }
                if (m == NULL) {
                        m = mon_head;
                }
@@ -503,7 +518,10 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
        }
 
        if (d == NULL) {
-               d = history_last_desktop(m, NULL);
+               d = m->desk;
+               if (d == NULL) {
+                       d = history_last_desktop(m, NULL);
+               }
                if (d == NULL) {
                        d = m->desk_head;
                }
@@ -513,8 +531,13 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
                return false;
        }
 
+       bool guess = (n == NULL);
+
        if (n == NULL && d->root != NULL) {
-               n = history_last_node(d, NULL);
+               n = d->focus;
+               if (n == NULL) {
+                       n = history_last_node(d, NULL);
+               }
                if (n == NULL) {
                        n = first_focusable_leaf(d->root);
                }
@@ -529,9 +552,12 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
        }
 
        if (m->sticky_count > 0 && d != m->desk) {
-               sticky_still = false;
+               if (guess && m->desk->focus != NULL && m->desk->focus->sticky) {
+                       n = m->desk->focus;
+               }
+
                transfer_sticky_nodes(m, m->desk, d, m->desk->root);
-               sticky_still = true;
+
                if (n == NULL && d->focus != NULL) {
                        n = d->focus;
                }
@@ -573,7 +599,7 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
 
        d->focus = n;
        ewmh_update_active_window();
-       history_add(m, d, n);
+       history_add(m, d, n, true);
 
        put_status(SBSC_MASK_REPORT);
 
@@ -590,7 +616,7 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
        set_input_focus(n);
 
        if (pointer_follows_focus) {
-               center_pointer(get_rectangle(d, n));
+               center_pointer(get_rectangle(m, d, n));
        } else if (focus_follows_pointer) {
                update_motion_recorder();
        }
@@ -648,10 +674,11 @@ node_t *make_node(uint32_t id)
        node_t *n = calloc(1, sizeof(node_t));
        n->id = id;
        n->parent = n->first_child = n->second_child = NULL;
-       n->vacant = n->hidden = n->sticky = n->private = n->locked = false;
+       n->vacant = n->hidden = n->sticky = n->private = n->locked = n->marked = false;
        n->split_ratio = split_ratio;
        n->split_type = TYPE_VERTICAL;
        n->birth_rotation = 0;
+       n->constraints = (constraints_t) {MIN_WIDTH, MIN_HEIGHT};
        n->presel = NULL;
        n->client = NULL;
        return n;
@@ -946,20 +973,47 @@ node_t *find_by_id_in(node_t *r, uint32_t id)
        }
 }
 
-void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t sel)
+void find_any_node(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
 {
-       if (ref->node == NULL) {
-               return;
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+                       if (find_any_node_in(m, d, d->root, ref, dst, sel)) {
+                               return;
+                       }
+               }
+       }
+}
+
+bool find_any_node_in(monitor_t *m, desktop_t *d, node_t *n, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
+{
+       if (n == NULL) {
+               return false;
+       } else {
+               coordinates_t loc = {m, d, n};
+               if (node_matches(&loc, ref, sel)) {
+                       *dst = loc;
+                       return true;
+               } else {
+                       if (find_any_node_in(m, d, n->first_child, ref, dst, sel)) {
+                               return true;
+                       } else {
+                               return find_any_node_in(m, d, n->second_child, ref, dst, sel);
+                       }
+               }
        }
+}
 
-       xcb_rectangle_t rect = get_rectangle(ref->desktop, ref->node);
-       double md = DBL_MAX, mr = UINT32_MAX;
+/* Based on https://github.com/ntrrgc/right-window */
+void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t dir, node_select_t *sel)
+{
+       xcb_rectangle_t rect = get_rectangle(ref->monitor, ref->desktop, ref->node);
+       uint32_t md = UINT32_MAX, mr = UINT32_MAX;
 
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
                desktop_t *d = m->desk;
                for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
                        coordinates_t loc = {m, d, f};
-                       xcb_rectangle_t r = get_rectangle(d, f);
+                       xcb_rectangle_t r = get_rectangle(m, d, f);
                        if (f == ref->node ||
                            f->client == NULL ||
                            f->hidden ||
@@ -968,7 +1022,7 @@ void find_nearest_neighbor(coordinates_t *ref, coordinates_t *dst, direction_t d
                            !on_dir_side(rect, r, dir)) {
                                continue;
                        }
-                       double fd = distance_center(rect, r);
+                       uint32_t fd = boundary_distance(rect, r, dir);
                        uint32_t fr = history_rank(f);
                        if (fd < md || (fd == md && fr < mr)) {
                                md = fd;
@@ -984,26 +1038,32 @@ unsigned int node_area(desktop_t *d, node_t *n)
        if (n == NULL) {
                return 0;
        }
-       return area(get_rectangle(d, n));
+       return area(get_rectangle(NULL, d, n));
 }
 
-int tiled_count(node_t *n)
+int tiled_count(node_t *n, bool include_receptacles)
 {
        if (n == NULL) {
                return 0;
        }
        int cnt = 0;
        for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
-               if (!f->hidden && f->client != NULL && IS_TILED(f->client)) {
+               if (!f->hidden && ((include_receptacles && f->client == NULL) ||
+                                  (f->client != NULL && IS_TILED(f->client)))) {
                        cnt++;
                }
        }
        return cnt;
 }
 
-void find_biggest(coordinates_t *ref, coordinates_t *dst, node_select_t sel)
+void find_by_area(area_peak_t ap, coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
 {
-       unsigned int b_area = 0;
+       unsigned int p_area;
+       if (ap == AREA_BIGGEST) {
+               p_area = 0;
+       } else {
+               p_area = UINT_MAX;
+       }
 
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
                for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
@@ -1013,9 +1073,9 @@ void find_biggest(coordinates_t *ref, coordinates_t *dst, node_select_t sel)
                                        continue;
                                }
                                unsigned int f_area = node_area(d, f);
-                               if (f_area > b_area) {
+                               if ((ap == AREA_BIGGEST && f_area > p_area) || (ap == AREA_SMALLEST && f_area < p_area)) {
                                        *dst = loc;
-                                       b_area = f_area;
+                                       p_area = f_area;
                                }
                        }
                }
@@ -1023,6 +1083,12 @@ void find_biggest(coordinates_t *ref, coordinates_t *dst, node_select_t sel)
 }
 
 void rotate_tree(node_t *n, int deg)
+{
+       rotate_tree_rec(n, deg);
+       rebuild_constraints(n);
+}
+
+void rotate_tree_rec(node_t *n, int deg)
 {
        if (n == NULL || is_leaf(n) || deg == 0) {
                return;
@@ -1047,8 +1113,8 @@ void rotate_tree(node_t *n, int deg)
                }
        }
 
-       rotate_tree(n->first_child, deg);
-       rotate_tree(n->second_child, deg);
+       rotate_tree_rec(n->first_child, deg);
+       rotate_tree_rec(n->second_child, deg);
 }
 
 void rotate_brother(node_t *n)
@@ -1142,7 +1208,7 @@ void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
                node_t *b = brother_tree(n);
                node_t *g = p->parent;
 
-               if (!n->vacant) {
+               if (!n->vacant && cancel_birth_rotation) {
                        unrotate_tree(b, n->birth_rotation);
                }
 
@@ -1242,7 +1308,7 @@ void free_node(node_t *n)
        free_node(second_child);
 }
 
-bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2)
+bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop_t *d2, node_t *n2, bool follow)
 {
        if (n1 == NULL || n2 == NULL || n1 == n2 || is_descendant(n1, n2) || is_descendant(n2, n1) ||
            (d1 != d2 && ((m1->sticky_count > 0 && sticky_count(n1) > 0) ||
@@ -1312,19 +1378,31 @@ bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop
                ewmh_set_wm_desktop(n1, d2);
                ewmh_set_wm_desktop(n2, d1);
 
-               history_swap_nodes(m1, d1, n1, m2, d2, n2);
+               history_remove(d1, n1, true);
+               history_remove(d2, n2, true);
+
+               bool d1_was_focused = (d1 == mon->desk);
+               bool d2_was_focused = (d2 == mon->desk);
 
                if (m1->desk != d1 && m2->desk == d2) {
                        show_node(d2, n1);
-                       hide_node(d2, n2);
+                       if (!follow || !d2_was_focused || !n2_held_focus) {
+                               hide_node(d2, n2);
+                       }
                } else if (m1->desk == d1 && m2->desk != d2) {
-                       hide_node(d1, n1);
+                       if (!follow || !d1_was_focused || !n1_held_focus) {
+                               hide_node(d1, n1);
+                       }
                        show_node(d1, n2);
                }
 
                if (n1_held_focus) {
-                       if (d1 == mon->desk) {
-                               focus_node(m1, d1, d1->focus);
+                       if (d1_was_focused) {
+                               if (follow) {
+                                       focus_node(m2, d2, last_d1_focus);
+                               } else {
+                                       focus_node(m1, d1, d1->focus);
+                               }
                        } else {
                                activate_node(m1, d1, d1->focus);
                        }
@@ -1333,14 +1411,21 @@ bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop
                }
 
                if (n2_held_focus) {
-                       if (d2 == mon->desk) {
-                               focus_node(m2, d2, d2->focus);
+                       if (d2_was_focused) {
+                               if (follow) {
+                                       focus_node(m1, d1, last_d2_focus);
+                               } else {
+                                       focus_node(m2, d2, d2->focus);
+                               }
                        } else {
                                activate_node(m2, d2, d2->focus);
                        }
                } else {
                        draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
                }
+       } else {
+               draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
+               draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
        }
 
        arrange(m1, d1);
@@ -1352,13 +1437,13 @@ bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop
        return true;
 }
 
-bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd)
+bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desktop_t *dd, node_t *nd, bool follow)
 {
        if (ns == NULL || ns == nd || is_child(ns, nd) || is_descendant(nd, ns)) {
                return false;
        }
 
-       if (sticky_still && ds != dd && ms->sticky_count > 0 && sticky_count(ns) > 0) {
+       if (sticky_still && ms->sticky_count > 0 && sticky_count(ns) > 0 && dd != md->desk) {
                return false;
        }
 
@@ -1367,16 +1452,24 @@ bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desk
        bool held_focus = is_descendant(ds->focus, ns);
        /* avoid ending up with a dangling pointer (because of unlink_node) */
        node_t *last_ds_focus = is_child(ns, ds->focus) ? NULL : ds->focus;
+       bool ds_was_focused = (ds == mon->desk);
 
-       if (held_focus && ds == mon->desk) {
+       if (held_focus && ds_was_focused) {
                clear_input_focus();
        }
 
        unlink_node(ms, ds, ns);
        insert_node(md, dd, ns, nd);
 
-       if (md != ms && (ns->client == NULL || monitor_from_client(ns->client) != md)) {
-               adapt_geometry(&ms->rectangle, &md->rectangle, ns);
+       if (md != ms) {
+               if (ns->client == NULL || monitor_from_client(ns->client) != md) {
+                       adapt_geometry(&ms->rectangle, &md->rectangle, ns);
+               }
+
+               if (ms->sticky_count > 0 && sticky_count(ns) > 0) {
+                       ms->sticky_count -= sticky_count(ns);
+                       md->sticky_count += sticky_count(ns);
+               }
        }
 
        if (ds != dd) {
@@ -1390,12 +1483,12 @@ bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desk
                }
        }
 
-       history_transfer_node(md, dd, ns);
+       history_remove(ds, ns, true);
        stack(dd, ns, false);
 
        if (ds == dd) {
                if (held_focus) {
-                       if (ds == mon->desk) {
+                       if (ds_was_focused) {
                                focus_node(ms, ds, last_ds_focus);
                        } else {
                                activate_node(ms, ds, last_ds_focus);
@@ -1405,20 +1498,29 @@ bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desk
                }
        } else {
                if (held_focus) {
-                       if (ds == mon->desk) {
-                               focus_node(ms, ds, ds->focus);
-                       } else {
+                       if (follow) {
+                               if (ds_was_focused) {
+                                       focus_node(md, dd, last_ds_focus);
+                               }
                                activate_node(ms, ds, ds->focus);
+                       } else {
+                               if (ds_was_focused) {
+                                       focus_node(ms, ds, ds->focus);
+                               } else {
+                                       activate_node(ms, ds, ds->focus);
+                               }
                        }
                }
-               if (dd->focus == ns) {
-                       if (dd == mon->desk) {
-                               focus_node(md, dd, held_focus ? last_ds_focus : dd->focus);
+               if (!held_focus || !follow || !ds_was_focused) {
+                       if (dd->focus == ns) {
+                               if (dd == mon->desk) {
+                                       focus_node(md, dd, held_focus ? last_ds_focus : ns);
+                               } else {
+                                       activate_node(md, dd, held_focus ? last_ds_focus : ns);
+                               }
                        } else {
-                               activate_node(md, dd, held_focus ? last_ds_focus : dd->focus);
+                               draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
                        }
-               } else {
-                       draw_border(ns, is_descendant(ns, dd->focus), (md == mon));
                }
        }
 
@@ -1431,20 +1533,15 @@ bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desk
        return true;
 }
 
-bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t sel)
+bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir, node_select_t *sel)
 {
-       if (ref->node == NULL) {
-               return false;
-       }
-
        monitor_t *m = ref->monitor;
        desktop_t *d = ref->desktop;
        node_t *n = ref->node;
+       n = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
 
-       node_t *f = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
-
-#define HANDLE_BOUNDARIES(f)  \
-       while (f == NULL) { \
+#define HANDLE_BOUNDARIES(m, d, n)  \
+       while (n == NULL) { \
                d = (dir == CYCLE_PREV ? d->prev : d->next); \
                if (d == NULL) { \
                        m = (dir == CYCLE_PREV ? m->prev : m->next); \
@@ -1453,18 +1550,21 @@ bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir,
                        } \
                        d = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head); \
                } \
-               f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
+               n = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root)); \
+               if (ref->node == NULL && d == ref->desktop) { \
+                       break; \
+               } \
        }
-       HANDLE_BOUNDARIES(f);
+       HANDLE_BOUNDARIES(m, d, n);
 
-       while (f != n) {
-               coordinates_t loc = {m, d, f};
-               if (f->client != NULL && !f->hidden && node_matches(&loc, ref, sel)) {
+       while (n != ref->node) {
+               coordinates_t loc = {m, d, n};
+               if (n->client != NULL && !n->hidden && node_matches(&loc, ref, sel)) {
                        *dst = loc;
                        return true;
                }
-               f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
-               HANDLE_BOUNDARIES(f);
+               n = (dir == CYCLE_PREV ? prev_leaf(n, d->root) : next_leaf(n, d->root));
+               HANDLE_BOUNDARIES(m, d, n);
        }
 #undef HANDLE_BOUNDARIES
        return false;
@@ -1472,7 +1572,7 @@ bool find_closest_node(coordinates_t *ref, coordinates_t *dst, cycle_dir_t dir,
 
 void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir)
 {
-       if (n == NULL || d->focus == NULL || is_leaf(n)) {
+       if (tiled_count(n, false) < 2) {
                return;
        }
        node_t *p = d->focus->parent;
@@ -1483,7 +1583,7 @@ void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir
                        e = prev_leaf(e, n);
                }
                for (node_t *s = e, *f = prev_tiled_leaf(s, n); f != NULL; s = prev_tiled_leaf(f, n), f = prev_tiled_leaf(s, n)) {
-                       swap_nodes(m, d, f, m, d, s);
+                       swap_nodes(m, d, f, m, d, s, false);
                }
        } else {
                node_t *e = first_extrema(n);
@@ -1491,13 +1591,18 @@ void circulate_leaves(monitor_t *m, desktop_t *d, node_t *n, circulate_dir_t dir
                        e = next_leaf(e, n);
                }
                for (node_t *f = e, *s = next_tiled_leaf(f, n); s != NULL; f = next_tiled_leaf(s, n), s = next_tiled_leaf(f, n)) {
-                       swap_nodes(m, d, f, m, d, s);
+                       swap_nodes(m, d, f, m, d, s, false);
                }
        }
-       if (focus_first_child) {
-               focus_node(m, d, p->first_child);
-       } else {
-               focus_node(m, d, p->second_child);
+       if (p != NULL) {
+               node_t *f = focus_first_child ? p->first_child : p->second_child;
+               if (is_leaf(f)) {
+                       if (d == mon->desk) {
+                               focus_node(m, d, f);
+                       } else {
+                               activate_node(m, d, f);
+                       }
+               }
        }
 }
 
@@ -1520,10 +1625,7 @@ void set_vacant_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
        n->vacant = value;
 
        if (value) {
-               unrotate_brother(n);
                cancel_presel(m, d, n);
-       } else {
-               rotate_brother(n);
        }
 }
 
@@ -1556,7 +1658,7 @@ void propagate_vacant_upward(monitor_t *m, desktop_t *d, node_t *n)
 
 bool set_layer(monitor_t *m, desktop_t *d, node_t *n, stack_layer_t l)
 {
-       if (n == NULL || n->client->layer == l) {
+       if (n == NULL || n->client == NULL || n->client->layer == l) {
                return false;
        }
 
@@ -1692,6 +1794,31 @@ void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
        }
 }
 
+void rebuild_constraints(node_t *n)
+{
+       if (n == NULL || is_leaf(n)) {
+               return;
+       } else {
+               rebuild_constraints(n->first_child);
+               rebuild_constraints(n->second_child);
+               update_constraints(n);
+       }
+}
+
+void update_constraints(node_t *n)
+{
+       if (n == NULL || is_leaf(n)) {
+               return;
+       }
+       if (n->split_type == TYPE_VERTICAL) {
+               n->constraints.min_width = n->first_child->constraints.min_width + n->second_child->constraints.min_width;
+               n->constraints.min_height = MAX(n->first_child->constraints.min_height, n->second_child->constraints.min_height);
+       } else {
+               n->constraints.min_width = MAX(n->first_child->constraints.min_width, n->second_child->constraints.min_width);
+               n->constraints.min_height = n->first_child->constraints.min_height + n->second_child->constraints.min_height;
+       }
+}
+
 void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
 {
        if (n == NULL) {
@@ -1703,6 +1830,7 @@ void propagate_flags_upward(monitor_t *m, desktop_t *d, node_t *n)
        if (p != NULL) {
                set_vacant_local(m, d, p, (p->first_child->vacant && p->second_child->vacant));
                set_hidden_local(m, d, p, (p->first_child->hidden && p->second_child->hidden));
+               update_constraints(p);
        }
 
        propagate_flags_upward(m, d, p);
@@ -1795,7 +1923,7 @@ void set_sticky(monitor_t *m, desktop_t *d, node_t *n, bool value)
        }
 
        if (d != m->desk) {
-               transfer_node(m, d, n, m, m->desk, m->desk->focus);
+               transfer_node(m, d, n, m, m->desk, m->desk->focus, false);
        }
 
        n->sticky = value;
@@ -1852,6 +1980,21 @@ void set_locked(monitor_t *m, desktop_t *d, node_t *n, bool value)
        }
 }
 
+void set_marked(monitor_t *m, desktop_t *d, node_t *n, bool value)
+{
+       if (n == NULL || n->marked == value) {
+               return;
+       }
+
+       n->marked = value;
+
+       put_status(SBSC_MASK_NODE_FLAG, "node_flag 0x%08X 0x%08X 0x%08X marked %s\n", m->id, d->id, n->id, ON_OFF_STR(value));
+
+       if (n == m->desk->focus) {
+               put_status(SBSC_MASK_REPORT);
+       }
+}
+
 void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
 {
        if (value && mon->desk->focus == n) {
@@ -1879,8 +2022,11 @@ bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
                a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
 }
 
-xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
+xcb_rectangle_t get_rectangle(monitor_t *m, desktop_t *d, node_t *n)
 {
+       if (n == NULL) {
+               return m->rectangle;
+       }
        client_t *c = n->client;
        if (c != NULL) {
                if (IS_FLOATING(c)) {
@@ -1911,6 +2057,16 @@ void listen_enter_notify(node_t *n, bool enable)
        }
 }
 
+void regenerate_ids_in(node_t *n)
+{
+       if (n == NULL || n->client != NULL) {
+               return;
+       }
+       n->id = xcb_generate_id(dpy);
+       regenerate_ids_in(n->first_child);
+       regenerate_ids_in(n->second_child);
+}
+
 #define DEF_FLAG_COUNT(flag) \
        unsigned int flag##_count(node_t *n) \
        { \