]> git.lizzy.rs Git - bspwm.git/blobdiff - src/tree.c
bspwm: port rounded corners patch to latest version
[bspwm.git] / src / tree.c
index d8fd107b3cd00a4e3ec93dda1e8a01e7aa32ca53..c3f84703b0e27db35656d3f1b80123a67d8f77ab 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"
@@ -45,32 +46,31 @@ void arrange(monitor_t *m, desktop_t *d)
                return;
        }
 
-       layout_t l = d->layout;
-
-       if (single_monocle && tiled_count(d->root) <= 1) {
-               l = LAYOUT_MONOCLE;
-       }
-
        xcb_rectangle_t rect = m->rectangle;
 
-       if (!paddingless_monocle || l != LAYOUT_MONOCLE) {
-               rect.x += m->padding.left + d->padding.left;
-               rect.y += m->padding.top + d->padding.top;
-               rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
-               rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
+       rect.x += m->padding.left + d->padding.left;
+       rect.y += m->padding.top + d->padding.top;
+       rect.width -= m->padding.left + d->padding.left + d->padding.right + m->padding.right;
+       rect.height -= m->padding.top + d->padding.top + d->padding.bottom + m->padding.bottom;
+
+       if (d->layout == LAYOUT_MONOCLE) {
+               rect.x += monocle_padding.left;
+               rect.y += monocle_padding.top;
+               rect.width -= monocle_padding.left + monocle_padding.right;
+               rect.height -= monocle_padding.top + monocle_padding.bottom;
        }
 
-       if (!gapless_monocle || l != LAYOUT_MONOCLE) {
+       if (!gapless_monocle || d->layout != LAYOUT_MONOCLE) {
                rect.x += d->window_gap;
                rect.y += d->window_gap;
                rect.width -= d->window_gap;
                rect.height -= d->window_gap;
        }
 
-       apply_layout(m, d, d->root, l, rect, rect);
+       apply_layout(m, d, d->root, rect, rect);
 }
 
-void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
+void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, xcb_rectangle_t root_rect)
 {
        if (n == NULL) {
                return;
@@ -78,25 +78,21 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectang
 
        n->rectangle = rect;
 
-       if (pointer_follows_focus && mon->desk->focus == n) {
-               xcb_rectangle_t r = rect;
-               r.width -= d->window_gap;
-               r.height -= d->window_gap;
-               center_pointer(r);
-       }
-
        if (n->presel != NULL) {
                draw_presel_feedback(m, d, n);
        }
 
        if (is_leaf(n)) {
+        unsigned int br = 0;
 
                if (n->client == NULL) {
                        return;
                }
 
                unsigned int bw;
-               if ((borderless_monocle && n->client->state == STATE_TILED && l == LAYOUT_MONOCLE)
+               bool the_only_window = !m->prev && !m->next && d->root->client;
+               if ((borderless_monocle && d->layout == LAYOUT_MONOCLE && IS_TILED(n->client))
+                   || (borderless_singleton && the_only_window)
                    || n->client->state == STATE_FULLSCREEN) {
                        bw = 0;
                } else {
@@ -106,29 +102,30 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectang
                xcb_rectangle_t r;
                xcb_rectangle_t cr = get_window_rectangle(n);
                client_state_t s = n->client->state;
+               /* tiled and pseudo-tiled clients */
                if (s == STATE_TILED || s == STATE_PSEUDO_TILED) {
-                       int wg = (gapless_monocle && l == LAYOUT_MONOCLE ? 0 : d->window_gap);
-                       /* tiled clients */
-                       if (s == STATE_TILED) {
-                               r = rect;
-                               int bleed = wg + 2 * bw;
-                               r.width = (bleed < r.width ? r.width - bleed : 1);
-                               r.height = (bleed < r.height ? r.height - bleed : 1);
+                       int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap);
+                       r = rect;
+                       int bleed = wg + 2 * bw;
+                       r.width = (bleed < r.width ? r.width - bleed : 1);
+                       r.height = (bleed < r.height ? r.height - bleed : 1);
                        /* pseudo-tiled clients */
-                       } else {
-                               r = n->client->floating_rectangle;
+                       if (s == STATE_PSEUDO_TILED) {
+                               xcb_rectangle_t f = n->client->floating_rectangle;
+                               r.width = MIN(r.width, f.width);
+                               r.height = MIN(r.height, f.height);
                                if (center_pseudo_tiled) {
                                        r.x = rect.x - bw + (rect.width - wg - r.width) / 2;
                                        r.y = rect.y - bw + (rect.height - wg - r.height) / 2;
-                               } else {
-                                       r.x = rect.x;
-                                       r.y = rect.y;
                                }
                        }
                        n->client->tiled_rectangle = r;
+            if (!gapless_monocle || 1 != LAYOUT_MONOCLE)
+                br = n->client->border_radius;
                /* floating clients */
                } else if (s == STATE_FLOATING) {
                        r = n->client->floating_rectangle;
+            br = n->client->border_radius;
                /* fullscreen clients */
                } else {
                        r = m->rectangle;
@@ -139,34 +136,55 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectang
 
                if (!rect_eq(r, cr)) {
                        window_move_resize(n->id, r.x, r.y, r.width, r.height);
+            window_rounded_border(n);
                        if (!grabbing) {
                                put_status(SBSC_MASK_NODE_GEOMETRY, "node_geometry 0x%08X 0x%08X 0x%08X %ux%u+%i+%i\n", m->id, d->id, n->id, r.width, r.height, r.x, r.y);
                        }
                }
 
                window_border_width(n->id, bw);
+        window_border_radius(n->client, br);
+        window_rounded_border(n);
 
        } else {
                xcb_rectangle_t first_rect;
                xcb_rectangle_t second_rect;
 
-               if (l == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
+               if (d->layout == LAYOUT_MONOCLE || n->first_child->vacant || n->second_child->vacant) {
                        first_rect = second_rect = rect;
                } else {
                        unsigned int fence;
                        if (n->split_type == TYPE_VERTICAL) {
                                fence = rect.width * n->split_ratio;
+                               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};
                        }
                }
 
-               apply_layout(m, d, n->first_child, l, first_rect, root_rect);
-               apply_layout(m, d, n->second_child, l, second_rect, root_rect);
+               apply_layout(m, d, n->first_child, first_rect, root_rect);
+               apply_layout(m, d, n->second_child, second_rect, root_rect);
        }
 }
 
@@ -179,6 +197,17 @@ presel_t *make_presel(void)
        return p;
 }
 
+void set_type(node_t *n, split_type_t typ)
+{
+       if (n == NULL) {
+               return;
+       }
+
+       n->split_type = typ;
+       update_constraints(n);
+       rebuild_constraints_towards_root(n);
+}
+
 void set_ratio(node_t *n, double rat)
 {
        if (n == NULL) {
@@ -306,18 +335,25 @@ 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) {
-                       f = p;
-                       p = f->parent;
-               }
                n->parent = c;
-               c->birth_rotation = f->birth_rotation;
                if (f->presel == NULL) {
-                       if (p == NULL) {
+                       bool single_tiled = f->client != NULL && IS_TILED(f->client) && tiled_count(d->root, true) == 1;
+                       if (p == NULL || automatic_scheme != SCHEME_SPIRAL || single_tiled) {
+                               if (p != NULL) {
+                                       if (is_first_child(f)) {
+                                               p->first_child = c;
+                                       } else {
+                                               p->second_child = c;
+                                       }
+                               } else {
+                                       d->root = c;
+                               }
+                               c->parent = p;
+                               f->parent = c;
                                if (initial_polarity == FIRST_CHILD) {
                                        c->first_child = n;
                                        c->second_child = f;
@@ -325,13 +361,26 @@ node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
                                        c->first_child = f;
                                        c->second_child = n;
                                }
-                               if (m->rectangle.width > m->rectangle.height) {
-                                       c->split_type = TYPE_VERTICAL;
+                               if (p == NULL || automatic_scheme == SCHEME_LONGEST_SIDE || single_tiled) {
+                                       if (f->rectangle.width > f->rectangle.height) {
+                                               c->split_type = TYPE_VERTICAL;
+                                       } else {
+                                               c->split_type = TYPE_HORIZONTAL;
+                                       }
                                } else {
-                                       c->split_type = TYPE_HORIZONTAL;
+                                       node_t *q = p;
+                                       while (q != NULL && (q->first_child->vacant || q->second_child->vacant)) {
+                                               q = q->parent;
+                                       }
+                                       if (q == NULL) {
+                                               q = p;
+                                       }
+                                       if (q->split_type == TYPE_HORIZONTAL) {
+                                               c->split_type = TYPE_VERTICAL;
+                                       } else {
+                                               c->split_type = TYPE_HORIZONTAL;
+                                       }
                                }
-                               f->parent = c;
-                               d->root = c;
                        } else {
                                node_t *g = p->parent;
                                c->parent = g;
@@ -357,7 +406,6 @@ 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) {
                                        rotate_tree(p, rot);
                                }
@@ -373,7 +421,6 @@ node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *f)
                        c->split_ratio = f->presel->split_ratio;
                        c->parent = p;
                        f->parent = c;
-                       f->birth_rotation = 0;
                        switch (f->presel->split_dir) {
                                case DIR_WEST:
                                        c->split_type = TYPE_VERTICAL;
@@ -400,6 +447,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);
                }
        }
 
@@ -416,23 +464,20 @@ void insert_receptacle(monitor_t *m, desktop_t *d, node_t *n)
 {
        node_t *r = make_node(XCB_NONE);
        insert_node(m, d, r, n);
+       put_status(SBSC_MASK_NODE_ADD, "node_add 0x%08X 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n != NULL ? n->id : 0, r->id);
+
+       if (single_monocle && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
+               set_layout(m, d, d->user_layout, false);
+       }
 }
 
 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 +502,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);
 
@@ -473,26 +516,31 @@ bool activate_node(monitor_t *m, desktop_t *d, node_t *n)
        return true;
 }
 
-void transfer_sticky_nodes(monitor_t *m, desktop_t *ds, desktop_t *dd, node_t *n)
+void transfer_sticky_nodes(monitor_t *ms, desktop_t *ds, monitor_t *md, 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(ms, ds, n, md, dd, dd->focus, false);
+               sticky_still = true;
        } else {
                /* we need references to the children because n might be freed after
                 * the first recursive call */
                node_t *first_child = n->first_child;
                node_t *second_child = n->second_child;
-               transfer_sticky_nodes(m, ds, dd, first_child);
-               transfer_sticky_nodes(m, ds, dd, second_child);
+               transfer_sticky_nodes(ms, ds, md, dd, first_child);
+               transfer_sticky_nodes(ms, ds, md, dd, second_child);
        }
 }
 
 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 +551,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 +564,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);
                }
@@ -528,10 +584,13 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
                clear_input_focus();
        }
 
-       if (m->sticky_count > 0 && d != m->desk) {
-               sticky_still = false;
-               transfer_sticky_nodes(m, m->desk, d, m->desk->root);
-               sticky_still = true;
+       if (m->sticky_count > 0 && m->desk != NULL && d != m->desk) {
+               if (guess && m->desk->focus != NULL && m->desk->focus->sticky) {
+                       n = m->desk->focus;
+               }
+
+               transfer_sticky_nodes(m, m->desk, m, d, m->desk->root);
+
                if (n == NULL && d->focus != NULL) {
                        n = d->focus;
                }
@@ -569,11 +628,38 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
 
        draw_border(n, true, true);
 
-       focus_desktop(m, d);
+       bool desk_changed = (m != mon || m->desk != d);
+       bool has_input_focus = false;
+
+       if (mon != m) {
+               mon = m;
+
+               if (pointer_follows_monitor) {
+                       center_pointer(m->rectangle);
+               }
+
+               put_status(SBSC_MASK_MONITOR_FOCUS, "monitor_focus 0x%08X\n", m->id);
+       }
+
+       if (m->desk != d) {
+               show_desktop(d);
+               set_input_focus(n);
+               has_input_focus = true;
+               hide_desktop(m->desk);
+               m->desk = d;
+       }
+
+       if (desk_changed) {
+               ewmh_update_current_desktop();
+               put_status(SBSC_MASK_DESKTOP_FOCUS, "desktop_focus 0x%08X 0x%08X\n", m->id, d->id);
+       }
 
        d->focus = n;
+       if (!has_input_focus) {
+               set_input_focus(n);
+       }
        ewmh_update_active_window();
-       history_add(m, d, n);
+       history_add(m, d, n, true);
 
        put_status(SBSC_MASK_REPORT);
 
@@ -587,10 +673,9 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
        put_status(SBSC_MASK_NODE_FOCUS, "node_focus 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
 
        stack(d, n, true);
-       set_input_focus(n);
 
        if (pointer_follows_focus) {
-               center_pointer(get_rectangle(d, n));
+               center_pointer(get_rectangle(m, d, n));
        } else if (focus_follows_pointer) {
                update_motion_recorder();
        }
@@ -600,7 +685,7 @@ bool focus_node(monitor_t *m, desktop_t *d, node_t *n)
 
 void hide_node(desktop_t *d, node_t *n)
 {
-       if (n == NULL) {
+       if (n == NULL || (!hide_sticky && n->sticky)) {
                return;
        } else {
                if (!n->hidden) {
@@ -648,10 +733,10 @@ 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;
@@ -665,8 +750,10 @@ client_t *make_client(void)
        snprintf(c->class_name, sizeof(c->class_name), "%s", MISSING_VALUE);
        snprintf(c->instance_name, sizeof(c->instance_name), "%s", MISSING_VALUE);
        c->border_width = border_width;
+       c->border_radius = border_radius;
        c->urgent = false;
        c->shown = false;
+    c->sets_own_shape = false;
        c->wm_flags = 0;
        c->icccm_props.input_hint = true;
        c->icccm_props.take_focus = false;
@@ -801,6 +888,48 @@ node_t *first_focusable_leaf(node_t *n)
        return NULL;
 }
 
+node_t *next_node(node_t *n)
+{
+       if (n == NULL) {
+               return NULL;
+       }
+
+       if (n->second_child != NULL) {
+               return first_extrema(n->second_child);
+       } else {
+               node_t *p = n;
+               while (is_second_child(p)) {
+                       p = p->parent;
+               }
+               if (is_first_child(p)) {
+                       return p->parent;
+               } else {
+                       return NULL;
+               }
+       }
+}
+
+node_t *prev_node(node_t *n)
+{
+       if (n == NULL) {
+               return NULL;
+       }
+
+       if (n->first_child != NULL) {
+               return second_extrema(n->first_child);
+       } else {
+               node_t *p = n;
+               while (is_first_child(p)) {
+                       p = p->parent;
+               }
+               if (is_second_child(p)) {
+                       return p->parent;
+               } else {
+                       return NULL;
+               }
+       }
+}
+
 node_t *next_leaf(node_t *n, node_t *r)
 {
        if (n == NULL) {
@@ -946,20 +1075,62 @@ 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)
+{
+       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);
+                       }
+               }
+       }
+}
+
+void find_first_ancestor(coordinates_t *ref, coordinates_t *dst, node_select_t *sel)
 {
        if (ref->node == NULL) {
                return;
        }
 
-       xcb_rectangle_t rect = get_rectangle(ref->desktop, ref->node);
-       double md = DBL_MAX, mr = UINT32_MAX;
+       coordinates_t loc = {ref->monitor, ref->desktop, ref->node};
+       while ((loc.node = loc.node->parent) != NULL) {
+               if (node_matches(&loc, ref, sel)) {
+                       *dst = loc;
+                       return;
+               }
+       }
+}
+
+/* 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 +1139,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,38 +1155,44 @@ 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) {
                        for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
                                coordinates_t loc = {m, d, f};
-                               if (f->client == NULL || f->vacant || !node_matches(&loc, ref, sel)) {
+                               if (f->vacant || !node_matches(&loc, ref, 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 +1200,13 @@ 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_from_leaves(n);
+       rebuild_constraints_towards_root(n);
+}
+
+void rotate_tree_rec(node_t *n, int deg)
 {
        if (n == NULL || is_leaf(n) || deg == 0) {
                return;
@@ -1047,26 +1231,8 @@ void rotate_tree(node_t *n, int deg)
                }
        }
 
-       rotate_tree(n->first_child, deg);
-       rotate_tree(n->second_child, deg);
-}
-
-void rotate_brother(node_t *n)
-{
-       rotate_tree(brother_tree(n), n->birth_rotation);
-}
-
-void unrotate_tree(node_t *n, int rot)
-{
-       if (rot == 0) {
-               return;
-       }
-       rotate_tree(n, 360 - rot);
-}
-
-void unrotate_brother(node_t *n)
-{
-       unrotate_tree(brother_tree(n), n->birth_rotation);
+       rotate_tree_rec(n->first_child, deg);
+       rotate_tree_rec(n->second_child, deg);
 }
 
 void flip_tree(node_t *n, flip_t flp)
@@ -1117,6 +1283,46 @@ int balance_tree(node_t *n)
        }
 }
 
+/* Adjust the split ratios so that they keep their position
+ * despite the potential alteration of their rectangle. */
+void adjust_ratios(node_t *n, xcb_rectangle_t rect)
+{
+       if (n == NULL) {
+               return;
+       }
+
+       double ratio;
+
+       if (n->split_type == TYPE_VERTICAL) {
+               double position = (double) n->rectangle.x + n->split_ratio * (double) n->rectangle.width;
+               ratio = (position - (double) rect.x) / (double) rect.width;
+       } else {
+               double position = (double) n->rectangle.y + n->split_ratio * (double) n->rectangle.height;
+               ratio = (position - (double) rect.y) / (double) rect.height;
+       }
+
+       ratio = MAX(0.0, ratio);
+       ratio = MIN(1.0, ratio);
+       n->split_ratio = ratio;
+
+       xcb_rectangle_t first_rect;
+       xcb_rectangle_t second_rect;
+       unsigned int fence;
+
+       if (n->split_type == TYPE_VERTICAL) {
+               fence = rect.width * n->split_ratio;
+               first_rect = (xcb_rectangle_t) {rect.x, rect.y, fence, rect.height};
+               second_rect = (xcb_rectangle_t) {rect.x + fence, rect.y, rect.width - fence, rect.height};
+       } else {
+               fence = rect.height * n->split_ratio;
+               first_rect = (xcb_rectangle_t) {rect.x, rect.y, rect.width, fence};
+               second_rect = (xcb_rectangle_t) {rect.x, rect.y + fence, rect.width, rect.height - fence};
+       }
+
+       adjust_ratios(n->first_child, first_rect);
+       adjust_ratios(n->second_child, second_rect);
+}
+
 void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
 {
        if (d == NULL || n == NULL) {
@@ -1135,6 +1341,7 @@ void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
 
                history_remove(d, p, false);
                cancel_presel(m, d, p);
+
                if (p->sticky) {
                        m->sticky_count--;
                }
@@ -1142,10 +1349,6 @@ 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) {
-                       unrotate_tree(b, n->birth_rotation);
-               }
-
                b->parent = g;
 
                if (g != NULL) {
@@ -1158,7 +1361,29 @@ void unlink_node(monitor_t *m, desktop_t *d, node_t *n)
                        d->root = b;
                }
 
-               b->birth_rotation = p->birth_rotation;
+               if (!n->vacant && removal_adjustment) {
+                       if (automatic_scheme == SCHEME_SPIRAL) {
+                               if (is_first_child(n)) {
+                                       rotate_tree(b, 270);
+                               } else {
+                                       rotate_tree(b, 90);
+                               }
+                       } else if (automatic_scheme == SCHEME_LONGEST_SIDE || g == NULL) {
+                               if (p != NULL) {
+                                       if (p->rectangle.width > p->rectangle.height) {
+                                               b->split_type = TYPE_VERTICAL;
+                                       } else {
+                                               b->split_type = TYPE_HORIZONTAL;
+                                       }
+                               }
+                       } else if (automatic_scheme == SCHEME_ALTERNATE) {
+                               if (g->split_type == TYPE_HORIZONTAL) {
+                                       b->split_type = TYPE_VERTICAL;
+                               } else {
+                                       b->split_type = TYPE_HORIZONTAL;
+                               }
+                       }
+               }
 
                free(p);
                n->parent = NULL;
@@ -1189,13 +1414,16 @@ void kill_node(monitor_t *m, desktop_t *d, node_t *n)
                return;
        }
 
-       for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
-               if (f->client != NULL) {
-                       xcb_kill_client(dpy, f->id);
+       if (IS_RECEPTACLE(n)) {
+               put_status(SBSC_MASK_NODE_REMOVE, "node_remove 0x%08X 0x%08X 0x%08X\n", m->id, d->id, n->id);
+               remove_node(m, d, n);
+       } else {
+               for (node_t *f = first_extrema(n); f != NULL; f = next_leaf(f, n)) {
+                       if (f->client != NULL) {
+                               xcb_kill_client(dpy, f->id);
+                       }
                }
        }
-
-       remove_node(m, d, n);
 }
 
 void remove_node(monitor_t *m, desktop_t *d, node_t *n)
@@ -1208,7 +1436,7 @@ void remove_node(monitor_t *m, desktop_t *d, node_t *n)
        history_remove(d, n, true);
        remove_stack_node(n);
        cancel_presel_in(m, d, n);
-       if (m->sticky_count > 0) {
+       if (m->sticky_count > 0 && d == m->desk) {
                m->sticky_count -= sticky_count(n);
        }
        clients_count -= clients_count_in(n);
@@ -1217,6 +1445,10 @@ void remove_node(monitor_t *m, desktop_t *d, node_t *n)
        }
        free_node(n);
 
+       if (single_monocle && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
+               set_layout(m, d, LAYOUT_MONOCLE, false);
+       }
+
        ewmh_update_client_list(false);
        ewmh_update_client_list(true);
 
@@ -1242,7 +1474,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) ||
@@ -1256,8 +1488,6 @@ bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop
        node_t *pn2 = n2->parent;
        bool n1_first_child = is_first_child(n1);
        bool n2_first_child = is_first_child(n2);
-       int br1 = n1->birth_rotation;
-       int br2 = n2->birth_rotation;
        bool n1_held_focus = is_descendant(d1->focus, n1);
        bool n2_held_focus = is_descendant(d2->focus, n2);
        node_t *last_d1_focus = d1->focus;
@@ -1281,8 +1511,6 @@ bool swap_nodes(monitor_t *m1, desktop_t *d1, node_t *n1, monitor_t *m2, desktop
 
        n1->parent = pn2;
        n2->parent = pn1;
-       n1->birth_rotation = br2;
-       n2->birth_rotation = br1;
 
        propagate_flags_upward(m2, d2, n1);
        propagate_flags_upward(m1, d1, n2);
@@ -1312,19 +1540,38 @@ 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 (single_monocle) {
+                       layout_t l1 = tiled_count(d1->root, true) <= 1 ? LAYOUT_MONOCLE : d1->user_layout;
+                       layout_t l2 = tiled_count(d2->root, true) <= 1 ? LAYOUT_MONOCLE : d2->user_layout;
+                       set_layout(m1, d1, l1, false);
+                       set_layout(m2, d2, l2, false);
+               }
+
                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,32 +1580,48 @@ 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 {
+               if (!n1_held_focus) {
+                       draw_border(n1, is_descendant(n1, d2->focus), (m2 == mon));
+               }
+               if (!n2_held_focus) {
+                       draw_border(n2, is_descendant(n2, d1->focus), (m1 == mon));
+               }
        }
 
        arrange(m1, d1);
 
        if (d1 != d2) {
                arrange(m2, d2);
+       } else {
+               if (pointer_follows_focus && (n1_held_focus || n2_held_focus)) {
+                       center_pointer(get_rectangle(m1, d1, d1->focus));
+               }
        }
 
        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) {
+       unsigned int sc = (ms->sticky_count > 0 && ds == ms->desk) ? sticky_count(ns) : 0;
+       if (sticky_still && sc > 0 && dd != md->desk) {
                return false;
        }
 
@@ -1367,16 +1630,21 @@ 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);
+               }
+               ms->sticky_count -= sc;
+               md->sticky_count += sc;
        }
 
        if (ds != dd) {
@@ -1390,12 +1658,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);
@@ -1404,21 +1672,38 @@ bool transfer_node(monitor_t *ms, desktop_t *ds, node_t *ns, monitor_t *md, desk
                        draw_border(ns, is_descendant(ns, ds->focus), (ms == mon));
                }
        } else {
+               if (single_monocle) {
+                       if (ds->layout != LAYOUT_MONOCLE && tiled_count(ds->root, true) <= 1) {
+                               set_layout(ms, ds, LAYOUT_MONOCLE, false);
+                       }
+                       if (dd->layout == LAYOUT_MONOCLE && tiled_count(dd->root, true) > 1) {
+                               set_layout(md, dd, dd->user_layout, false);
+                       }
+               }
                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 +1716,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_node(n) : next_node(n));
 
-       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,26 +1733,32 @@ 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 (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_node(n) : next_node(n));
+               HANDLE_BOUNDARIES(m, d, n);
+               if (ref->node == NULL && d == ref->desktop) {
+                       break;
+               }
        }
-#undef HANDLE_EXTREMUM
+#undef HANDLE_BOUNDARIES
        return false;
 }
 
 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 +1769,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 +1777,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 +1811,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 +1844,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;
        }
 
@@ -1594,6 +1882,8 @@ bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
 
        client_t *c = n->client;
 
+       bool was_tiled = IS_TILED(c);
+
        c->last_state = c->state;
        c->state = s;
 
@@ -1629,6 +1919,14 @@ bool set_state(monitor_t *m, desktop_t *d, node_t *n, client_state_t s)
                put_status(SBSC_MASK_REPORT);
        }
 
+       if (single_monocle && was_tiled != IS_TILED(c)) {
+               if (was_tiled && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
+                       set_layout(m, d, LAYOUT_MONOCLE, false);
+               } else if (!was_tiled && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
+                       set_layout(m, d, d->user_layout, false);
+               }
+       }
+
        return true;
 }
 
@@ -1639,7 +1937,9 @@ void set_floating(monitor_t *m, desktop_t *d, node_t *n, bool value)
        }
 
        cancel_presel(m, d, n);
-       set_vacant(m, d, n, value);
+       if (!n->hidden) {
+               set_vacant(m, d, n, value);
+       }
 
        if (!value && d->focus == n) {
                neutralize_occluding_windows(m, d, n);
@@ -1657,15 +1957,14 @@ void set_fullscreen(monitor_t *m, desktop_t *d, node_t *n, bool value)
        client_t *c = n->client;
 
        cancel_presel(m, d, n);
-       set_vacant(m, d, n, value);
+       if (!n->hidden) {
+               set_vacant(m, d, n, value);
+       }
 
        if (value) {
                c->wm_flags |= WM_FLAG_FULLSCREEN;
-               c->last_layer = c->layer;
-               c->layer = LAYER_ABOVE;
        } else {
                c->wm_flags &= ~WM_FLAG_FULLSCREEN;
-               c->layer = c->last_layer;
                if (d->focus == n) {
                        neutralize_occluding_windows(m, d, n);
                }
@@ -1692,6 +1991,46 @@ void neutralize_occluding_windows(monitor_t *m, desktop_t *d, node_t *n)
        }
 }
 
+void rebuild_constraints_from_leaves(node_t *n)
+{
+       if (n == NULL || is_leaf(n)) {
+               return;
+       } else {
+               rebuild_constraints_from_leaves(n->first_child);
+               rebuild_constraints_from_leaves(n->second_child);
+               update_constraints(n);
+       }
+}
+
+void rebuild_constraints_towards_root(node_t *n)
+{
+       if (n == NULL) {
+               return;
+       }
+
+       node_t *p = n->parent;
+
+       if (p != NULL) {
+               update_constraints(p);
+       }
+
+       rebuild_constraints_towards_root(p);
+}
+
+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 +2042,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);
@@ -1732,6 +2072,14 @@ void set_hidden(monitor_t *m, desktop_t *d, node_t *n, bool value)
                        activate_node(m, d, d->focus);
                }
        }
+
+       if (single_monocle) {
+               if (value && d->layout != LAYOUT_MONOCLE && tiled_count(d->root, true) <= 1) {
+                       set_layout(m, d, LAYOUT_MONOCLE, false);
+               } else if (!value && d->layout == LAYOUT_MONOCLE && tiled_count(d->root, true) > 1) {
+                       set_layout(m, d, d->user_layout, false);
+               }
+       }
 }
 
 void set_hidden_local(monitor_t *m, desktop_t *d, node_t *n, bool value)
@@ -1795,7 +2143,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 +2200,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) {
@@ -1872,15 +2235,11 @@ void set_urgent(monitor_t *m, desktop_t *d, node_t *n, bool value)
        put_status(SBSC_MASK_REPORT);
 }
 
-/* Returns true if a contains b */
-bool contains(xcb_rectangle_t a, xcb_rectangle_t b)
-{
-       return (a.x <= b.x && (a.x + a.width) >= (b.x + b.width) &&
-               a.y <= b.y && (a.y + a.height) >= (b.y + b.height));
-}
-
-xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
+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)) {
@@ -1889,7 +2248,7 @@ xcb_rectangle_t get_rectangle(desktop_t *d, node_t *n)
                        return c->tiled_rectangle;
                }
        } else {
-               int wg = (d == NULL ? 0 : (gapless_monocle && IS_MONOCLE(d) ? 0 : d->window_gap));
+               int wg = (d == NULL ? 0 : (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : d->window_gap));
                xcb_rectangle_t rect = n->rectangle;
                rect.width -= wg;
                rect.height -= wg;
@@ -1911,6 +2270,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) \
        { \