]> git.lizzy.rs Git - bspwm.git/blobdiff - query.c
Add example bindings for history navigation
[bspwm.git] / query.c
diff --git a/query.c b/query.c
index a25e53f7faddad3d773d6c61c49c2950470f483a..c269f6f0636e8320c95f3767b1cf6b4c62041681 100644 (file)
--- a/query.c
+++ b/query.c
@@ -1,10 +1,11 @@
-#include <stdlib.h>
-#include <string.h>
 #include <stdio.h>
+#include <string.h>
 #include "bspwm.h"
-#include "tree.h"
-#include "settings.h"
+#include "desktop.h"
+#include "history.h"
 #include "messages.h"
+#include "monitor.h"
+#include "tree.h"
 #include "query.h"
 
 void query_monitors(coordinates_t loc, domain_t dom, char *rsp)
@@ -22,9 +23,7 @@ void query_monitors(coordinates_t loc, domain_t dom, char *rsp)
                 snprintf(line, sizeof(line), "%s %ux%u%+i%+i %i,%i,%i,%i", m->name, m->rectangle.width, m->rectangle.height, m->rectangle.x, m->rectangle.y, m->top_padding, m->right_padding, m->bottom_padding, m->left_padding);
                 strncat(rsp, line, REMLEN(rsp));
                 if (m == mon)
-                    strncat(rsp, " #", REMLEN(rsp));
-                else if (m == last_mon)
-                    strncat(rsp, " ~", REMLEN(rsp));
+                    strncat(rsp, " *", REMLEN(rsp));
                 strncat(rsp, "\n", REMLEN(rsp));
             }
         }
@@ -45,12 +44,10 @@ void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int
             strncat(rsp, line, REMLEN(rsp));
             continue;
         } else {
-            snprintf(line, sizeof(line), "%s %i %c", d->name, d->window_gap, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
+            snprintf(line, sizeof(line), "%s %u %i %u %c", d->name, d->border_width, d->window_gap, d->tags_field, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
             strncat(rsp, line, REMLEN(rsp));
             if (d == m->desk)
-                strncat(rsp, " @", REMLEN(rsp));
-            else if (d == m->last_desk)
-                strncat(rsp, " ~", REMLEN(rsp));
+                strncat(rsp, " *", REMLEN(rsp));
             strncat(rsp, "\n", REMLEN(rsp));
         }
         query_tree(d, d->root, rsp, depth + 1);
@@ -69,7 +66,7 @@ void query_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
 
     if (is_leaf(n)) {
         client_t *c = n->client;
-        snprintf(line, sizeof(line), "%c %s %X %u %ux%u%+i%+i %c %c%c%c%c%c%c", (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), c->class_name, c->window, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))), (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (n->split_mode ? 'p' : '-'));
+        snprintf(line, sizeof(line), "%c %s 0x%X %u %u %ux%u%+i%+i %c %c%c%c%c%c%c%c", (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), c->class_name, c->window, c->tags_field, c->border_width, c->floating_rectangle.width, c->floating_rectangle.height, c->floating_rectangle.x, c->floating_rectangle.y, (n->split_dir == DIR_UP ? 'U' : (n->split_dir == DIR_RIGHT ? 'R' : (n->split_dir == DIR_DOWN ? 'D' : 'L'))), (c->floating ? 'f' : '-'), (c->transient ? 't' : '-'), (c->fullscreen ? 'F' : '-'), (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'), (n->split_mode ? 'p' : '-'));
     } else {
         snprintf(line, sizeof(line), "%c %c %.2f", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'), (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')), n->split_ratio);
     }
@@ -87,19 +84,26 @@ void query_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
 void query_history(coordinates_t loc, char *rsp)
 {
     char line[MAXLEN];
-    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-        if (loc.monitor != NULL && m != loc.monitor)
+    for (history_t *h = history_head; h != NULL; h = h->next) {
+        if ((loc.monitor != NULL && h->loc.monitor != loc.monitor)
+                || (loc.desktop != NULL && h->loc.desktop != loc.desktop))
             continue;
-        for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
-            if (loc.desktop != NULL && d != loc.desktop)
-                continue;
-            snprintf(line, sizeof(line), "%s\n", d->name);
-            strncat(rsp, line, REMLEN(rsp));
-            for (node_list_t *a = d->history->tail; a != NULL; a = a->prev) {
-                snprintf(line, sizeof(line), "  %X\n", a->node->client->window);
-                strncat(rsp, line, REMLEN(rsp));
-            }
-        }
+        xcb_window_t win = XCB_NONE;
+        if (h->loc.node != NULL)
+            win = h->loc.node->client->window;
+        snprintf(line, sizeof(line), "%s %s 0x%X", h->loc.monitor->name, h->loc.desktop->name, win);
+        strncat(rsp, line, REMLEN(rsp));
+        strncat(rsp, "\n", REMLEN(rsp));
+    }
+}
+
+void query_stack(char *rsp)
+{
+    char line[MAXLEN];
+    for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
+        snprintf(line, sizeof(line), "0x%X", s->node->client->window);
+        strncat(rsp, line, REMLEN(rsp));
+        strncat(rsp, "\n", REMLEN(rsp));
     }
 }
 
@@ -125,11 +129,7 @@ void query_windows(coordinates_t loc, char *rsp)
 
 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
-    client_select_t sel;
-    sel.type = CLIENT_TYPE_ALL;
-    sel.class = CLIENT_CLASS_ALL;
-    sel.mode = CLIENT_MODE_ALL;
-    sel.urgency = CLIENT_URGENCY_ALL;
+    client_select_t sel = {CLIENT_TYPE_ALL, CLIENT_CLASS_ALL, false, false, false};
     char *tok;
     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
         tok[0] = '\0';
@@ -142,14 +142,12 @@ bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
             sel.class = CLIENT_CLASS_EQUAL;
         } else if (streq("unlike", tok)) {
             sel.class = CLIENT_CLASS_DIFFER;
-        } else if (streq("automatic", tok)) {
-            sel.mode = CLIENT_MODE_AUTOMATIC;
-        } else if (streq("manual", tok)) {
-            sel.mode = CLIENT_MODE_MANUAL;
         } else if (streq("urgent", tok)) {
-            sel.urgency = CLIENT_URGENCY_ON;
-        } else if (streq("nonurgent", tok)) {
-            sel.urgency = CLIENT_URGENCY_OFF;
+            sel.urgent = true;
+        } else if (streq("manual", tok)) {
+            sel.manual = true;
+        } else if (streq("local", tok)) {
+            sel.local = true;
         }
     }
 
@@ -159,16 +157,20 @@ bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 
     direction_t dir;
     cycle_dir_t cyc;
+    history_dir_t hdi;
     if (parse_direction(desc, &dir)) {
-        dst->node = nearest_neighbor(dst->desktop, ref->node, dir, sel);
+        dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
     } else if (parse_cycle_direction(desc, &cyc)) {
-        dst->node = closest_node(ref->desktop, ref->node, cyc, sel);
+        dst->node = closest_node(ref->monitor, ref->desktop, ref->node, cyc, sel);
+    } else if (parse_history_direction(desc, &hdi)) {
+        history_find_node(hdi, ref, dst, sel);
     } else if (streq("last", desc)) {
-        dst->node = history_last(ref->desktop->history, ref->node, sel);
+        history_find_node(HISTORY_OLDER, ref, dst, sel);
     } else if (streq("biggest", desc)) {
-        dst->node = find_biggest(ref->desktop, ref->node, sel);
+        dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
     } else if (streq("focused", desc)) {
-        if (node_matches(ref->node, mon->desk->focus, sel)) {
+        coordinates_t loc = {mon, mon->desk, mon->desk->focus};
+        if (node_matches(&loc, ref, sel)) {
             dst->monitor = mon;
             dst->desktop = mon->desk;
             dst->node = mon->desk->focus;
@@ -184,9 +186,7 @@ bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 
 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
-    desktop_select_t sel;
-    sel.status = DESKTOP_STATUS_ALL;
-    sel.urgency = DESKTOP_URGENCY_ALL;
+    desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
     char *tok;
     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
         tok[0] = '\0';
@@ -196,31 +196,32 @@ bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
         } else if (streq("occupied", tok)) {
             sel.status = DESKTOP_STATUS_OCCUPIED;
         } else if (streq("urgent", tok)) {
-            sel.urgency = DESKTOP_URGENCY_ON;
-        } else if (streq("nonurgent", tok)) {
-            sel.urgency = DESKTOP_URGENCY_OFF;
+            sel.urgent = true;
+        } else if (streq("local", tok)) {
+            sel.local = true;
         }
     }
 
     dst->desktop = NULL;
 
     cycle_dir_t cyc;
+    history_dir_t hdi;
     int idx;
     if (parse_cycle_direction(desc, &cyc)) {
         dst->monitor = ref->monitor;
         dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel);
-    } else if (parse_index(desc, &idx)) {
-        desktop_from_index(idx, dst);
+    } else if (parse_history_direction(desc, &hdi)) {
+        history_find_desktop(hdi, ref, dst, sel);
     } else if (streq("last", desc)) {
-        if (mon->last_desk != NULL && desktop_matches(mon->last_desk, sel)) {
-            dst->monitor = mon;
-            dst->desktop = mon->last_desk;
-        }
+        history_find_desktop(HISTORY_OLDER, ref, dst, sel);
     } else if (streq("focused", desc)) {
-        if (desktop_matches(mon->desk, sel)) {
+        coordinates_t loc = {mon, mon->desk, NULL};
+        if (desktop_matches(&loc, ref, sel)) {
             dst->monitor = mon;
             dst->desktop = mon->desk;
         }
+    } else if (parse_index(desc, &idx)) {
+        desktop_from_index(idx, dst);
     } else {
         locate_desktop(desc, dst);
     }
@@ -232,7 +233,7 @@ bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
     desktop_select_t sel;
     sel.status = DESKTOP_STATUS_ALL;
-    sel.urgency = DESKTOP_URGENCY_ALL;
+    sel.urgent = false;
     char *tok;
     while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
         tok[0] = '\0';
@@ -248,22 +249,26 @@ bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 
     direction_t dir;
     cycle_dir_t cyc;
+    history_dir_t hdi;
     int idx;
     if (parse_direction(desc, &dir)) {
         dst->monitor = nearest_monitor(ref->monitor, dir, sel);
     } else if (parse_cycle_direction(desc, &cyc)) {
         dst->monitor = closest_monitor(ref->monitor, cyc, sel);
-    } else if (parse_index(desc, &idx)) {
-        monitor_from_index(idx, dst);
+    } else if (parse_history_direction(desc, &hdi)) {
+        history_find_monitor(hdi, ref, dst, sel);
     } else if (streq("last", desc)) {
-        if (last_mon != NULL && desktop_matches(last_mon->desk, sel))
-            dst->monitor = last_mon;
+        history_find_monitor(HISTORY_OLDER, ref, dst, sel);
     } else if (streq("primary", desc)) {
-        if (pri_mon != NULL && desktop_matches(pri_mon->desk, sel))
+        coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
+        if (pri_mon != NULL && desktop_matches(&loc, ref, sel))
             dst->monitor = pri_mon;
     } else if (streq("focused", desc)) {
-        if (desktop_matches(mon->desk, sel))
+        coordinates_t loc = {mon, mon->desk, NULL};
+        if (desktop_matches(&loc, ref, sel))
             dst->monitor = mon;
+    } else if (parse_index(desc, &idx)) {
+        monitor_from_index(idx, dst);
     } else {
         locate_monitor(desc, dst);
     }
@@ -331,3 +336,46 @@ bool monitor_from_index(int i, coordinates_t *loc)
         }
     return false;
 }
+
+bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
+{
+    if (sel.type != CLIENT_TYPE_ALL &&
+            is_tiled(loc->node->client)
+            ? sel.type == CLIENT_TYPE_FLOATING
+            : sel.type == CLIENT_TYPE_TILED)
+        return false;
+
+    if (sel.class != CLIENT_CLASS_ALL &&
+            streq(loc->node->client->class_name, ref->node->client->class_name)
+            ? sel.class == CLIENT_CLASS_DIFFER
+            : sel.class == CLIENT_CLASS_EQUAL)
+        return false;
+
+    if (sel.manual && loc->node->split_mode != MODE_MANUAL)
+        return false;
+
+    if (sel.local && loc->desktop != ref->desktop)
+        return false;
+
+    if (sel.urgent && !loc->node->client->urgent)
+        return false;
+
+    return true;
+}
+
+bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
+{
+    if (sel.status != DESKTOP_STATUS_ALL &&
+            loc->desktop->root == NULL
+            ? sel.status == DESKTOP_STATUS_OCCUPIED
+            : sel.status == DESKTOP_STATUS_FREE)
+        return false;
+
+    if (sel.urgent && !is_urgent(loc->desktop))
+        return false;
+
+    if (sel.local && ref->monitor != loc->monitor)
+        return false;
+
+    return true;
+}