]> git.lizzy.rs Git - bspwm.git/blobdiff - tree.c
Cosmetic improvements
[bspwm.git] / tree.c
diff --git a/tree.c b/tree.c
index 16760b4e94ac0d01dab3701f7b66c6e366222f18..3edd59b6cc96514f15a23489544e66d1d4a491ab 100644 (file)
--- a/tree.c
+++ b/tree.c
@@ -39,6 +39,69 @@ bool is_second_child(node_t *n)
     return (n != NULL && n->parent != NULL && n->parent->second_child == n);
 }
 
+/**
+ * Check if the specified node matches the selection criteria.
+ *
+ * Arguments:
+ *  node_t *c           - the active node
+ *  node_t *t           - the node to test
+ *  client_sel_t sel    - the selection criteria
+ *
+ * Returns true if the node matches.
+ **/
+bool node_matches(node_t *c, node_t *t, client_select_t sel)
+{
+    if (sel.type != CLIENT_TYPE_ALL &&
+            is_tiled(t->client)
+            ? sel.type == CLIENT_TYPE_FLOATING
+            : sel.type == CLIENT_TYPE_TILED
+       ) return false;
+
+    if (sel.class != CLIENT_CLASS_ALL &&
+            streq(c->client->class_name, t->client->class_name)
+            ? sel.class == CLIENT_CLASS_DIFFER
+            : sel.class == CLIENT_CLASS_EQUAL
+       ) return false;
+
+    if (sel.mode != CLIENT_MODE_ALL &&
+            t->split_mode == MODE_MANUAL
+            ? sel.mode == CLIENT_MODE_AUTOMATIC
+            : sel.mode == CLIENT_MODE_MANUAL)
+        return false;
+
+    if (sel.urgency != CLIENT_URGENCY_ALL &&
+            t->client->urgent
+            ? sel.urgency == CLIENT_URGENCY_OFF
+            : sel.urgency == CLIENT_URGENCY_ON
+       ) return false;
+
+    return true;
+}
+
+bool desktop_matches(desktop_t *t, desktop_select_t sel) {
+    if (sel.status != DESKTOP_STATUS_ALL &&
+            t->root == NULL
+            ? sel.status == DESKTOP_STATUS_OCCUPIED
+            : sel.status == DESKTOP_STATUS_FREE
+       ) return false;
+
+    if (sel.urgency != DESKTOP_URGENCY_ALL &&
+            is_urgent(t)
+            ? sel.urgency == DESKTOP_URGENCY_OFF
+            : sel.urgency == DESKTOP_URGENCY_ON
+       ) return false;
+
+    return true;
+}
+
+bool is_urgent(desktop_t *d)
+{
+    for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
+        if (n->client->urgent)
+            return true;
+    return false;
+}
+
 void change_split_ratio(node_t *n, value_change_t chg)
 {
     n->split_ratio = pow(n->split_ratio,
@@ -165,7 +228,7 @@ node_t *find_fence(node_t *n, direction_t dir)
 }
 
 
-node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir)
+node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
 {
     if (n == NULL || n->client->fullscreen
             || (d->layout == LAYOUT_MONOCLE && is_tiled(n->client)))
@@ -173,38 +236,13 @@ node_t *nearest_neighbor(desktop_t *d, node_t *n, direction_t dir)
 
     node_t *nearest = NULL;
     if (history_aware_focus)
-        nearest = nearest_from_history(d->history, n, dir);
-    if (nearest == NULL) {
-        if (focus_by_distance) {
-            nearest = nearest_from_distance(d, n, dir);
-        } else {
-            nearest = nearest_from_tree(n, dir);
-        }
-    }
-    return nearest;
-}
-
-node_t *nearest_from_tree(node_t *n, direction_t dir)
-{
-    if (n == NULL)
-        return NULL;
-
-    node_t *fence = find_fence(n, dir);
-
-    if (fence == NULL)
-        return NULL;
-
-    node_t *nearest = NULL;
-
-    if (dir == DIR_UP || dir == DIR_LEFT)
-        nearest = second_extrema(fence->first_child);
-    else if (dir == DIR_DOWN || dir == DIR_RIGHT)
-        nearest = first_extrema(fence->second_child);
-
+        nearest = nearest_from_history(d->history, n, dir, sel);
+    if (nearest == NULL)
+        nearest = nearest_from_distance(d, n, dir, sel);
     return nearest;
 }
 
-node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir)
+node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir, client_select_t sel)
 {
     if (n == NULL || !is_tiled(n->client))
         return NULL;
@@ -223,6 +261,9 @@ node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir)
     for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
         if (a->vacant || !is_adjacent(n, a, dir) || a == n)
             continue;
+        if (!node_matches(n, a, sel))
+            continue;
+
         int rank = history_rank(f, a);
         if (rank >= 0 && rank < min_rank) {
             nearest = a;
@@ -233,7 +274,7 @@ node_t *nearest_from_history(focus_history_t *f, node_t *n, direction_t dir)
     return nearest;
 }
 
-node_t *nearest_from_distance(desktop_t *d, node_t *n, direction_t dir)
+node_t *nearest_from_distance(desktop_t *d, node_t *n, direction_t dir, client_select_t sel)
 {
     if (n == NULL)
         return NULL;
@@ -261,10 +302,11 @@ node_t *nearest_from_distance(desktop_t *d, node_t *n, direction_t dir)
     double ds = DBL_MAX;
 
     for (node_t *a = first_extrema(target); a != NULL; a = next_leaf(a, target)) {
-        if (is_tiled(a->client) != is_tiled(n->client)
-                || (is_tiled(a->client) && !is_adjacent(n, a, dir))
-                || a == n)
-            continue;
+        if (a == n) continue;
+        if (!node_matches(n, a, sel)) continue;
+        if (is_tiled(a->client) != is_tiled(n->client)) continue;
+        if (is_tiled(a->client) && !is_adjacent(n, a, dir)) continue;
+
         get_side_handle(a->client, dir2, &pt2);
         double ds2 = distance(pt, pt2);
         if (ds2 < ds) {
@@ -302,7 +344,7 @@ int tiled_area(node_t *n)
     return rect.width * rect.height;
 }
 
-node_t *find_biggest(desktop_t *d)
+node_t *find_biggest(desktop_t *d, node_t *c, client_select_t sel)
 {
     if (d == NULL)
         return NULL;
@@ -311,7 +353,7 @@ node_t *find_biggest(desktop_t *d)
     int r_area = tiled_area(r);
 
     for (node_t *f = first_extrema(d->root); f != NULL; f = next_leaf(f, d->root)) {
-        if (!is_tiled(f->client))
+        if (!is_tiled(f->client) || !node_matches(c, f, sel))
             continue;
         int f_area = tiled_area(f);
         if (r == NULL) {
@@ -474,6 +516,8 @@ void apply_layout(monitor_t *m, desktop_t *d, node_t *n, xcb_rectangle_t rect, x
                     r = rect;
                 else if (d->layout == LAYOUT_MONOCLE)
                     r = root_rect;
+                else
+                    return;
                 int wg = (gapless_monocle && d->layout == LAYOUT_MONOCLE ? 0 : window_gap);
                 int bleed = wg + 2 * n->client->border_width;
                 r.width = (bleed < r.width ? r.width - bleed : 1);
@@ -879,7 +923,7 @@ void select_monitor(monitor_t *m)
     put_status();
 }
 
-monitor_t *nearest_monitor(monitor_t *m, direction_t dir)
+monitor_t *nearest_monitor(monitor_t *m, direction_t dir, desktop_select_t sel)
 {
     int dmin = INT_MAX;
     monitor_t *nearest = NULL;
@@ -887,6 +931,8 @@ monitor_t *nearest_monitor(monitor_t *m, direction_t dir)
     for (monitor_t *f = mon_head; f != NULL; f = f->next) {
         if (f == m)
             continue;
+        if (!desktop_matches(f->desk, sel))
+            continue;
         xcb_rectangle_t r = f->rectangle;
         if ((dir == DIR_LEFT && r.x < rect.x) ||
                 (dir == DIR_RIGHT && r.x >= (rect.x + rect.width)) ||
@@ -929,11 +975,8 @@ monitor_t *closest_monitor(monitor_t *m, cycle_dir_t dir, desktop_select_t sel)
         f = (dir == CYCLE_PREV ? mon_tail : mon_head);
 
     while (f != m) {
-        if (sel == DESKTOP_ALL
-                || (sel == DESKTOP_FREE && f->desk->root == NULL)
-                || (sel == DESKTOP_OCCUPIED && f->desk->root != NULL)) {
+        if (desktop_matches(f->desk, sel))
             return f;
-        }
         f = (dir == CYCLE_PREV ? m->prev : m->next);
         if (f == NULL)
             f = (dir == CYCLE_PREV ? mon_tail : mon_head);
@@ -949,11 +992,8 @@ desktop_t *closest_desktop(monitor_t *m, desktop_t *d, cycle_dir_t dir, desktop_
         f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
 
     while (f != d) {
-        if (sel == DESKTOP_ALL
-                || (sel == DESKTOP_FREE && f->root == NULL)
-                || (sel == DESKTOP_OCCUPIED && f->root != NULL)) {
+        if (desktop_matches(f, sel))
             return f;
-        }
         f = (dir == CYCLE_PREV ? f->prev : f->next);
         if (f == NULL)
             f = (dir == CYCLE_PREV ? m->desk_tail : m->desk_head);
@@ -972,17 +1012,8 @@ node_t *closest_node(desktop_t *d, node_t *n, cycle_dir_t dir, client_select_t s
         f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));
 
     while (f != n) {
-        bool tiled = is_tiled(f->client);
-        if ((sel.type == CLIENT_TYPE_ALL
-                    || (tiled && sel.type == CLIENT_TYPE_TILED)
-                    || (!tiled && sel.type == CLIENT_TYPE_FLOATING)) &&
-                (sel.class == CLIENT_CLASS_ALL
-                 || (sel.class == CLIENT_CLASS_EQUAL
-                     && streq(f->client->class_name, n->client->class_name))
-                 || (sel.class == CLIENT_CLASS_DIFFER
-                     && !streq(f->client->class_name, n->client->class_name)))) {
+        if (node_matches(n, f, sel))
             return f;
-        }
         f = (dir == CYCLE_PREV ? prev_leaf(f, d->root) : next_leaf(f, d->root));
         if (f == NULL)
             f = (dir == CYCLE_PREV ? second_extrema(d->root) : first_extrema(d->root));