]> git.lizzy.rs Git - bspwm.git/blobdiff - src/tree.c
Add new setting: cancel_birth_rotation
[bspwm.git] / src / tree.c
index 3d1e541fa58872b6820cded73d9a61d1ab7eec05..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 {
@@ -159,10 +160,11 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectang
                                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;
                                        }
-                                       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};
@@ -171,10 +173,11 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, layout_t l, xcb_rectang
                                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;
                                        }
-                                       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};
@@ -326,7 +329,7 @@ node_t *insert_node(monitor_t *m, desktop_t *d, node_t *n, node_t *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;
                }
@@ -373,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);
                                }
                        }
@@ -416,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);
                }
        }
 
@@ -436,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);
                }
@@ -473,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);
 
@@ -494,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 */
@@ -508,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;
                }
@@ -519,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;
                }
@@ -529,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);
                }
@@ -545,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;
                }
@@ -589,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);
 
@@ -664,7 +674,7 @@ 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;
@@ -963,11 +973,41 @@ node_t *find_by_id_in(node_t *r, uint32_t id)
        }
 }
 
+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);
+                       }
+               }
+       }
+}
+
 /* 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)
+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);
-       double md = UINT32_MAX, mr = UINT32_MAX;
+       uint32_t md = UINT32_MAX, mr = UINT32_MAX;
 
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
                desktop_t *d = m->desk;
@@ -1001,23 +1041,29 @@ unsigned int node_area(desktop_t *d, node_t *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) {
@@ -1027,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;
                                }
                        }
                }
@@ -1162,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);
                }
 
@@ -1262,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) ||
@@ -1332,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);
                        }
@@ -1353,8 +1411,12 @@ 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);
                        }
@@ -1375,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;
        }
 
@@ -1390,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) {
@@ -1413,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);
@@ -1428,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));
                }
        }
 
@@ -1454,7 +1533,7 @@ 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)
 {
        monitor_t *m = ref->monitor;
        desktop_t *d = ref->desktop;
@@ -1493,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 (tiled_count(n) < 2) {
+       if (tiled_count(n, false) < 2) {
                return;
        }
        node_t *p = d->focus->parent;
@@ -1504,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);
@@ -1512,7 +1591,7 @@ 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 (p != NULL) {
@@ -1546,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);
        }
 }
 
@@ -1582,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;
        }
 
@@ -1847,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;
@@ -1904,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) {
@@ -1966,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) \
        { \