]> git.lizzy.rs Git - bspwm.git/blobdiff - query.c
Start parsing modifiers after the last colon
[bspwm.git] / query.c
diff --git a/query.c b/query.c
index 682774370dd9158c4a7a242e1d21548926d93655..800b3ce6609bba07a51b2126602a61478b5acf5a 100644 (file)
--- a/query.c
+++ b/query.c
@@ -1,23 +1,23 @@
-/* * Copyright (c) 2012-2013 Bastien Dejean
+/* Copyright (c) 2012, Bastien Dejean
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
  *
- *  * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *  * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
  *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 #include "bspwm.h"
 #include "desktop.h"
 #include "history.h"
-#include "messages.h"
+#include "parse.h"
 #include "monitor.h"
 #include "tree.h"
 #include "query.h"
 
-void query_monitors(coordinates_t loc, domain_t dom, char *rsp)
+void query_tree(FILE *rsp)
 {
-    char line[MAXLEN];
-    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-        if (loc.monitor != NULL && m != loc.monitor)
-            continue;
-        if (dom != DOMAIN_DESKTOP) {
-            if (dom == DOMAIN_MONITOR) {
-                snprintf(line, sizeof(line), "%s\n", m->name);
-                strncat(rsp, line, REMLEN(rsp));
-                continue;
-            } else {
-                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));
-                strncat(rsp, "\n", REMLEN(rsp));
-            }
-        }
-        query_desktops(m, dom, loc, (dom == DOMAIN_DESKTOP ? 0 : 1), rsp);
-    }
+       fprintf(rsp, "{");
+       fprintf(rsp, "\"focusedMonitorName\":\"%s\",", mon->name);
+       fprintf(rsp, "\"clientsCount\":%i,", clients_count);
+       fprintf(rsp, "\"monitors\":");
+       fprintf(rsp, "[");
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               query_monitor(m, rsp);
+               if (m->next != NULL) {
+                       fprintf(rsp, ",");
+               }
+       }
+       fprintf(rsp, "]");
+       fprintf(rsp,",");
+       fprintf(rsp, "\"focusHistory\":");
+       query_history(rsp);
+       fprintf(rsp,",");
+       fprintf(rsp, "\"stackingList\":");
+       query_stack(rsp);
+       fprintf(rsp, "}");
+
+}
+
+void query_monitor(monitor_t *m, FILE *rsp)
+{
+       fprintf(rsp, "{");
+       fprintf(rsp, "\"name\":\"%s\",", m->name);
+       fprintf(rsp, "\"id\":%u,", m->id);
+       fprintf(rsp, "\"wired\":%s,", BOOL_STR(m->wired));
+       fprintf(rsp, "\"topPadding\":%i,", m->top_padding);
+       fprintf(rsp, "\"rightPadding\":%i,", m->right_padding);
+       fprintf(rsp, "\"bottomPadding\":%i,", m->bottom_padding);
+       fprintf(rsp, "\"leftPadding\":%i,", m->left_padding);
+       fprintf(rsp, "\"stickyCount\":%i,", m->sticky_count);
+       fprintf(rsp, "\"rectangle\":");
+       query_rectangle(m->rectangle, rsp);
+       fprintf(rsp,",");
+       fprintf(rsp, "\"focusedDesktopName\":\"%s\",", m->desk->name);
+       fprintf(rsp, "\"desktops\":");
+       fprintf(rsp, "[");
+       for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+               query_desktop(d, rsp);
+               if (d->next != NULL) {
+                       fprintf(rsp,",");
+               }
+       }
+       fprintf(rsp, "]");
+       fprintf(rsp, "}");
+}
+
+void query_desktop(desktop_t *d, FILE *rsp)
+{
+       fprintf(rsp, "{");
+       fprintf(rsp, "\"name\":\"%s\",", d->name);
+       fprintf(rsp, "\"layout\":\"%s\",", LAYOUT_STR(d->layout));
+       fprintf(rsp, "\"topPadding\":%i,", d->top_padding);
+       fprintf(rsp, "\"rightPadding\":%i,", d->right_padding);
+       fprintf(rsp, "\"bottomPadding\":%i,", d->bottom_padding);
+       fprintf(rsp, "\"leftPadding\":%i,", d->left_padding);
+       fprintf(rsp, "\"windowGap\":%i,", d->window_gap);
+       fprintf(rsp, "\"borderWidth\":%u,", d->border_width);
+       fprintf(rsp, "\"focusedNodeId\":%u,", d->focus != NULL ? d->focus->id : 0);
+       fprintf(rsp, "\"root\":");
+       query_node(d->root, rsp);
+       fprintf(rsp, "}");
+}
+
+void query_node(node_t *n, FILE *rsp)
+{
+       if (n == NULL) {
+               fprintf(rsp, "null");
+       } else {
+               fprintf(rsp, "{");
+               fprintf(rsp, "\"id\":%u,", n->id);
+               fprintf(rsp, "\"splitType\":\"%s\",", SPLIT_TYPE_STR(n->split_type));
+               fprintf(rsp, "\"splitRatio\":%lf,", n->split_ratio);
+               fprintf(rsp, "\"birthRotation\":%i,", n->birth_rotation);
+               fprintf(rsp, "\"vacant\":%s,", BOOL_STR(n->vacant));
+               fprintf(rsp, "\"sticky\":%s,", BOOL_STR(n->sticky));
+               fprintf(rsp, "\"private\":%s,", BOOL_STR(n->private));
+               fprintf(rsp, "\"locked\":%s,", BOOL_STR(n->locked));
+               fprintf(rsp, "\"presel\":");
+               query_presel(n->presel, rsp);
+               fprintf(rsp,",");
+               fprintf(rsp, "\"rectangle\":");
+               query_rectangle(n->rectangle, rsp);
+               fprintf(rsp,",");
+               fprintf(rsp, "\"firstChild\":");
+               query_node(n->first_child, rsp);
+               fprintf(rsp,",");
+               fprintf(rsp, "\"secondChild\":");
+               query_node(n->second_child, rsp);
+               fprintf(rsp,",");
+               fprintf(rsp, "\"client\":");
+               query_client(n->client, rsp);
+               fprintf(rsp, "}");
+       }
 }
 
-void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int depth, char *rsp)
+void query_presel(presel_t *p, FILE *rsp)
 {
-    char line[MAXLEN];
-    for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
-        if (loc.desktop != NULL && d != loc.desktop)
-            continue;
-        for (unsigned int i = 0; i < depth; i++)
-            strncat(rsp, "  ", REMLEN(rsp));
-        if (dom == DOMAIN_DESKTOP) {
-            snprintf(line, sizeof(line), "%s\n", d->name);
-            strncat(rsp, line, REMLEN(rsp));
-            continue;
-        } else {
-            snprintf(line, sizeof(line), "%s %u %i %c", d->name, d->border_width, d->window_gap, (d->layout == LAYOUT_TILED ? 'T' : 'M'));
-            strncat(rsp, line, REMLEN(rsp));
-            if (d == m->desk)
-                strncat(rsp, " *", REMLEN(rsp));
-            strncat(rsp, "\n", REMLEN(rsp));
-        }
-        query_tree(d, d->root, rsp, depth + 1);
-    }
+       if (p == NULL) {
+               fprintf(rsp, "null");
+       } else {
+               fprintf(rsp, "{\"splitDir\":\"%s\",\"splitRatio\":%lf}", SPLIT_DIR_STR(p->split_dir), p->split_ratio);
+       }
 }
 
-void query_tree(desktop_t *d, node_t *n, char *rsp, unsigned int depth)
+void query_client(client_t *c, FILE *rsp)
 {
-    if (n == NULL)
-        return;
+       if (c == NULL) {
+               fprintf(rsp, "null");
+       } else {
+               fprintf(rsp, "{");
+               fprintf(rsp, "\"className\":\"%s\",", c->class_name);
+               fprintf(rsp, "\"instanceName\":\"%s\",", c->instance_name);
+               fprintf(rsp, "\"borderWidth\":%u,", c->border_width);
+               fprintf(rsp, "\"state\":\"%s\",", STATE_STR(c->state));
+               fprintf(rsp, "\"lastState\":\"%s\",", STATE_STR(c->last_state));
+               fprintf(rsp, "\"layer\":\"%s\",", LAYER_STR(c->layer));
+               fprintf(rsp, "\"lastLayer\":\"%s\",", LAYER_STR(c->last_layer));
+               fprintf(rsp, "\"urgent\":%s,", BOOL_STR(c->urgent));
+               fprintf(rsp, "\"icccmFocus\":%s,", BOOL_STR(c->icccm_focus));
+               fprintf(rsp, "\"icccmInput\":%s,", BOOL_STR(c->icccm_input));
+               fprintf(rsp, "\"minWidth\":%u,", c->min_width);
+               fprintf(rsp, "\"maxWidth\":%u,", c->max_width);
+               fprintf(rsp, "\"minHeight\":%u,", c->min_height);
+               fprintf(rsp, "\"maxHeight\":%u,", c->max_height);
+               fprintf(rsp, "\"wmStatesCount\":%i,", c->wm_states_count);
+               fprintf(rsp, "\"wmState\":");
+               query_wm_state(c->wm_state, c->wm_states_count, rsp);
+               fprintf(rsp,",");
+               fprintf(rsp, "\"tiledRectangle\":");
+               query_rectangle(c->tiled_rectangle, rsp);
+               fprintf(rsp,",");
+               fprintf(rsp, "\"floatingRectangle\":");
+               query_rectangle(c->floating_rectangle, rsp);
+               fprintf(rsp, "}");
+       }
+}
+
+void query_rectangle(xcb_rectangle_t r, FILE *rsp)
+{
+               fprintf(rsp, "{\"x\":%i,\"y\":%i,\"width\":%u,\"height\":%u}", r.x, r.y, r.width, r.height);
+}
+
+void query_wm_state(xcb_atom_t *wm_state, int wm_states_count, FILE *rsp)
+{
+       fprintf(rsp, "[");
+       for (int i = 0; i < wm_states_count; i++) {
+               fprintf(rsp, "%u", wm_state[i]);
+               if (i < wm_states_count - 1) {
+                       fprintf(rsp, ",");
+               }
+       }
+       fprintf(rsp, "]");
+}
+
+void query_history(FILE *rsp)
+{
+       fprintf(rsp, "[");
+       for (history_t *h = history_head; h != NULL; h = h->next) {
+               query_coordinates(&h->loc, rsp);
+               if (h->next != NULL) {
+                       fprintf(rsp, ",");
+               }
+       }
+       fprintf(rsp, "]");
+}
 
-    char line[MAXLEN];
+void query_coordinates(coordinates_t *loc, FILE *rsp)
+{
+       fprintf(rsp, "{\"monitorName\":\"%s\",\"desktopName\":\"%s\",\"nodeId\":%u}", loc->monitor->name, loc->desktop->name, loc->node!=NULL?loc->node->id:0);
+}
 
-    for (unsigned int i = 0; i < depth; i++)
-        strncat(rsp, "  ", REMLEN(rsp));
+void query_stack(FILE *rsp)
+{
+       fprintf(rsp, "[");
+       for (stacking_list_t *s = stack_head; s != NULL; s = s->next) {
+               fprintf(rsp, "%u", s->node->id);
+               if (s->next != NULL) {
+                       fprintf(rsp, ",");
+               }
+       }
+       fprintf(rsp, "]");
+}
 
-    if (is_leaf(n)) {
-        client_t *c = n->client;
-        snprintf(line, sizeof(line), "%c %s 0x%X %u %ux%u%+i%+i %c %c%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->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' : '-'), (c->frame ? 'e' : '-'), (c->private ? 'i' : '-'), (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);
-    }
+void query_node_ids(coordinates_t loc, node_select_t *sel, FILE *rsp)
+{
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               if (loc.monitor != NULL && m != loc.monitor) {
+                       continue;
+               }
+               for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+                       if (loc.desktop != NULL && d != loc.desktop) {
+                               continue;
+                       }
+                       query_node_ids_in(d->root, d, m, loc, sel, rsp);
+               }
+       }
+}
 
-    strncat(rsp, line, REMLEN(rsp));
+void query_node_ids_in(node_t *n, desktop_t *d, monitor_t *m, coordinates_t loc, node_select_t *sel, FILE *rsp)
+{
+       if (n == NULL) {
+               return;
+       } else {
+               coordinates_t ref = {mon, mon->desk, mon->desk->focus};
+               coordinates_t trg = {m, d, n};
+               if ((loc.node == NULL || n == loc.node) &&
+                   (sel == NULL || node_matches(&trg, &ref, *sel))) {
+                       fprintf(rsp, "0x%07X\n", n->id);
+               }
+               query_node_ids_in(n->first_child, d, m, loc, sel, rsp);
+               query_node_ids_in(n->second_child, d, m, loc, sel, rsp);
+       }
+}
 
-    if (n == d->focus)
-        strncat(rsp, " *", REMLEN(rsp));
-    strncat(rsp, "\n", REMLEN(rsp));
+void query_desktop_names(coordinates_t loc, desktop_select_t *sel, FILE *rsp)
+{
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               if (loc.monitor != NULL && m != loc.monitor) {
+                       continue;
+               }
+               for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+                       coordinates_t ref = {mon, mon->desk, NULL};
+                       coordinates_t trg = {m, d, NULL};
+                       if ((loc.desktop != NULL && d != loc.desktop) ||
+                           (sel != NULL && !desktop_matches(&trg, &ref, *sel))) {
+                               continue;
+                       }
+                       fprintf(rsp, "%s\n", d->name);
+               }
+       }
+}
 
-    query_tree(d, n->first_child, rsp, depth + 1);
-    query_tree(d, n->second_child, rsp, depth + 1);
+void query_monitor_names(coordinates_t loc, monitor_select_t *sel, FILE *rsp)
+{
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               coordinates_t ref = {mon, NULL, NULL};
+               coordinates_t trg = {m, NULL, NULL};
+               if ((loc.monitor != NULL && m != loc.monitor) ||
+                       (sel != NULL && !monitor_matches(&trg, &ref, *sel))) {
+                       continue;
+               }
+               fprintf(rsp, "%s\n", m->name);
+       }
 }
 
-void query_history(coordinates_t loc, char *rsp)
+node_select_t make_node_select(void)
 {
-    char line[MAXLEN];
-    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;
-        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));
-    }
+       node_select_t sel = {
+               .automatic = OPTION_NONE,
+               .focused = OPTION_NONE,
+               .local = OPTION_NONE,
+               .leaf = OPTION_NONE,
+               .tiled = OPTION_NONE,
+               .pseudo_tiled = OPTION_NONE,
+               .floating = OPTION_NONE,
+               .fullscreen = OPTION_NONE,
+               .locked = OPTION_NONE,
+               .sticky = OPTION_NONE,
+               .private = OPTION_NONE,
+               .urgent = OPTION_NONE,
+               .same_class = OPTION_NONE,
+               .below = OPTION_NONE,
+               .normal = OPTION_NONE,
+               .above = OPTION_NONE
+       };
+       return sel;
 }
 
-void query_stack(char *rsp)
+desktop_select_t make_desktop_select(void)
 {
-    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));
-    }
+       desktop_select_t sel = {
+               .occupied = OPTION_NONE,
+               .focused = OPTION_NONE,
+               .urgent = OPTION_NONE,
+               .local = OPTION_NONE
+       };
+       return sel;
 }
 
-void query_windows(coordinates_t loc, char *rsp)
+monitor_select_t make_monitor_select(void)
 {
-    char line[MAXLEN];
-
-    for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-        if (loc.monitor != NULL && m != loc.monitor)
-            continue;
-        for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
-            if (loc.desktop != NULL && d != loc.desktop)
-                continue;
-            for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
-                if (loc.node != NULL && n != loc.node)
-                    continue;
-                snprintf(line, sizeof(line), "0x%X\n", n->client->window);
-                strncat(rsp, line, REMLEN(rsp));
-            }
-        }
-    }
+       monitor_select_t sel = {
+               .occupied = OPTION_NONE,
+               .focused = OPTION_NONE
+       };
+       return sel;
 }
 
 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
-    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';
-        tok++;
-        if (streq("tiled", tok)) {
-            sel.type = CLIENT_TYPE_TILED;
-        } else if (streq("floating", tok)) {
-            sel.type = CLIENT_TYPE_FLOATING;
-        } else if (streq("like", tok)) {
-            sel.class = CLIENT_CLASS_EQUAL;
-        } else if (streq("unlike", tok)) {
-            sel.class = CLIENT_CLASS_DIFFER;
-        } else if (streq("urgent", tok)) {
-            sel.urgent = true;
-        } else if (streq("manual", tok)) {
-            sel.manual = true;
-        } else if (streq("local", tok)) {
-            sel.local = true;
-        }
-    }
-
-    dst->monitor = ref->monitor;
-    dst->desktop = ref->desktop;
-    dst->node = NULL;
-
-    direction_t dir;
-    cycle_dir_t cyc;
-    history_dir_t hdi;
-    if (parse_direction(desc, &dir)) {
-        dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
-    } else if (parse_cycle_direction(desc, &cyc)) {
-        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)) {
-        history_find_node(HISTORY_OLDER, ref, dst, sel);
-    } else if (streq("biggest", desc)) {
-        dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
-    } else if (streq("focused", desc)) {
-        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;
-        }
-    } else {
-        long int wid;
-        if (parse_window_id(desc, &wid))
-            locate_window(wid, dst);
-    }
-
-    return (dst->node != NULL);
+       node_select_t sel = make_node_select();
+
+       char *colon = strrchr(desc, ':');
+
+       if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
+               return false;
+       }
+
+       dst->monitor = ref->monitor;
+       dst->desktop = ref->desktop;
+       dst->node = NULL;
+
+       direction_t dir;
+       cycle_dir_t cyc;
+       history_dir_t hdi;
+       if (parse_direction(desc, &dir)) {
+               dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
+               if (dst->node == NULL && mon_head != mon_tail) {
+                       monitor_t *m = nearest_monitor(ref->monitor, dir, make_monitor_select());
+                       if (m != NULL) {
+                               coordinates_t loc = {m, m->desk, m->desk->focus};
+                               if (node_matches(&loc, ref, sel)) {
+                                       dst->monitor = m;
+                                       dst->desktop = m->desk;
+                                       dst->node = m->desk->focus;
+                               }
+                       }
+               }
+       } else if (parse_cycle_direction(desc, &cyc)) {
+               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)) {
+               history_find_node(HISTORY_OLDER, ref, dst, sel);
+       } else if (streq("biggest", desc)) {
+               dst->node = find_biggest(ref->monitor, ref->desktop, ref->node, sel);
+       } else if (streq("focused", desc)) {
+               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;
+               }
+       } else if (*desc == '@') {
+               desc++;
+               if (colon != NULL) {
+                       *colon = '\0';
+                       if (desktop_from_desc(desc, ref, dst)) {
+                               desc = colon + 1;
+                       } else {
+                               return false;
+                       }
+               }
+               dst->node = (*desc == '/' ? dst->desktop->root : dst->desktop->focus);
+               char *move = strtok(desc, PTH_TOK);
+               while (move != NULL && dst->node != NULL) {
+                       if (streq("first", move) || streq("1", move)) {
+                               dst->node = dst->node->first_child;
+                       } else if (streq("second", move) || streq("2", move)) {
+                               dst->node = dst->node->second_child;
+                       } else if (streq("parent", move)) {
+                               dst->node = dst->node->parent;
+                       } else if (streq("brother", move)) {
+                               dst->node = brother_tree(dst->node);
+                       } else {
+                               direction_t dir;
+                               if (parse_direction(move, &dir)) {
+                                       dst->node = find_fence(dst->node, dir);
+                               } else {
+                                       return false;
+                               }
+                       }
+                       move = strtok(NULL, PTH_TOK);
+               }
+               if (dst->node != NULL) {
+                       return node_matches(dst, ref, sel);
+               }
+       } else {
+               uint32_t id;
+               if (parse_id(desc, &id) && find_by_id(id, dst)) {
+                       return node_matches(dst, ref, sel);
+               }
+       }
+
+       return (dst->node != NULL);
 }
 
 bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
-    desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
-    char *tok;
-    while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
-        tok[0] = '\0';
-        tok++;
-        if (streq("free", tok)) {
-            sel.status = DESKTOP_STATUS_FREE;
-        } else if (streq("occupied", tok)) {
-            sel.status = DESKTOP_STATUS_OCCUPIED;
-        } else if (streq("urgent", tok)) {
-            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_history_direction(desc, &hdi)) {
-        history_find_desktop(hdi, ref, dst, sel);
-    } else if (streq("last", desc)) {
-        history_find_desktop(HISTORY_OLDER, ref, dst, sel);
-    } else if (streq("focused", desc)) {
-        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);
-    }
-
-    return (dst->desktop != NULL);
+       desktop_select_t sel = make_desktop_select();
+
+       char *colon = strrchr(desc, ':');
+
+       if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
+               return false;
+       }
+
+       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_history_direction(desc, &hdi)) {
+               history_find_desktop(hdi, ref, dst, sel);
+       } else if (streq("last", desc)) {
+               history_find_desktop(HISTORY_OLDER, ref, dst, sel);
+       } else if (streq("focused", desc)) {
+               coordinates_t loc = {mon, mon->desk, NULL};
+               if (desktop_matches(&loc, ref, sel)) {
+                       dst->monitor = mon;
+                       dst->desktop = mon->desk;
+               }
+       } else if (colon != NULL) {
+               *colon = '\0';
+               if (monitor_from_desc(desc, ref, dst)) {
+                       if (streq("focused", colon + 1)) {
+                               coordinates_t loc = {dst->monitor, dst->monitor->desk, NULL};
+                               if (desktop_matches(&loc, ref, sel)) {
+                                       dst->desktop = dst->monitor->desk;
+                               }
+                       } else if (parse_index(colon + 1, &idx)) {
+                               if (desktop_from_index(idx, dst, dst->monitor)) {
+                                       return desktop_matches(dst, ref, sel);
+                               }
+                       }
+               }
+       } else if (parse_index(desc, &idx)) {
+               if (desktop_from_index(idx, dst, NULL)) {
+                       return desktop_matches(dst, ref, sel);
+               }
+       } else {
+               if (locate_desktop(desc, dst)) {
+                       return desktop_matches(dst, ref, sel);
+               }
+       }
+
+       return (dst->desktop != NULL);
 }
 
 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
-    desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
-    char *tok;
-    while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
-        tok[0] = '\0';
-        tok++;
-        if (streq("free", tok)) {
-            sel.status = DESKTOP_STATUS_FREE;
-        } else if (streq("occupied", tok)) {
-            sel.status = DESKTOP_STATUS_OCCUPIED;
-        }
-    }
-
-    dst->monitor = NULL;
-
-    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_history_direction(desc, &hdi)) {
-        history_find_monitor(hdi, ref, dst, sel);
-    } else if (streq("last", desc)) {
-        history_find_monitor(HISTORY_OLDER, ref, dst, sel);
-    } else if (streq("primary", desc)) {
-        if (pri_mon != NULL) {
-            coordinates_t loc = {pri_mon, pri_mon->desk, NULL};
-            if (desktop_matches(&loc, ref, sel))
-                dst->monitor = pri_mon;
-        }
-    } else if (streq("focused", desc)) {
-        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);
-    }
-
-    return (dst->monitor != NULL);
+       monitor_select_t sel = make_monitor_select();
+
+       if (!parse_monitor_modifiers(desc, &sel)) {
+               return false;
+       }
+
+       dst->monitor = NULL;
+
+       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_history_direction(desc, &hdi)) {
+               history_find_monitor(hdi, ref, dst, sel);
+       } else if (streq("last", desc)) {
+               history_find_monitor(HISTORY_OLDER, ref, dst, sel);
+       } else if (streq("primary", desc)) {
+               if (pri_mon != NULL) {
+                       coordinates_t loc = {pri_mon, NULL, NULL};
+                       if (monitor_matches(&loc, ref, sel)) {
+                               dst->monitor = pri_mon;
+                       }
+               }
+       } else if (streq("focused", desc)) {
+               coordinates_t loc = {mon, NULL, NULL};
+               if (monitor_matches(&loc, ref, sel)) {
+                       dst->monitor = mon;
+               }
+       } else if (parse_index(desc, &idx)) {
+               if (monitor_from_index(idx, dst)) {
+                       monitor_matches(dst, ref, sel);
+               }
+       } else {
+               if (locate_monitor(desc, dst)) {
+                       return monitor_matches(dst, ref, sel);
+               }
+       }
+
+       return (dst->monitor != NULL);
 }
 
 bool locate_window(xcb_window_t win, coordinates_t *loc)
 {
-    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 *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
-                if (n->client->window == win) {
-                    loc->monitor = m;
-                    loc->desktop = d;
-                    loc->node = n;
-                    return true;
-                }
-    return false;
+       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 *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) {
+                               if (n->id == win) {
+                                       loc->monitor = m;
+                                       loc->desktop = d;
+                                       loc->node = n;
+                                       return true;
+                               }
+                       }
+               }
+       }
+       return false;
 }
 
 bool locate_desktop(char *name, coordinates_t *loc)
 {
-    for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
-            if (streq(d->name, name)) {
-                loc->monitor = m;
-                loc->desktop = d;
-                return true;
-            }
-    return false;
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
+                       if (streq(d->name, name)) {
+                               loc->monitor = m;
+                               loc->desktop = d;
+                               return true;
+                       }
+               }
+       }
+       return false;
 }
 
 bool locate_monitor(char *name, coordinates_t *loc)
 {
-    for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        if (streq(m->name, name)) {
-            loc->monitor = m;
-            return true;
-        }
-    return false;
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               if (streq(m->name, name)) {
+                       loc->monitor = m;
+                       return true;
+               }
+       }
+       return false;
 }
 
-bool desktop_from_index(int i, coordinates_t *loc)
+bool desktop_from_index(int i, coordinates_t *loc, monitor_t *mm)
 {
-    for (monitor_t *m = mon_head; m != NULL; m = m->next)
-        for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--)
-            if (i == 1) {
-                loc->monitor = m;
-                loc->desktop = d;
-                loc->node = NULL;
-                return true;
-            }
-    return false;
+       for (monitor_t *m = mon_head; m != NULL; m = m->next) {
+               if (mm != NULL && m != mm) {
+                       continue;
+               }
+               for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--) {
+                       if (i == 1) {
+                               loc->monitor = m;
+                               loc->desktop = d;
+                               loc->node = NULL;
+                               return true;
+                       }
+               }
+       }
+       return false;
 }
 
 bool monitor_from_index(int i, coordinates_t *loc)
 {
-    for (monitor_t *m = mon_head; m != NULL; m = m->next, i--)
-        if (i == 1) {
-            loc->monitor = m;
-            loc->desktop = NULL;
-            loc->node = NULL;
-            return true;
-        }
-    return false;
+       for (monitor_t *m = mon_head; m != NULL; m = m->next, i--) {
+               if (i == 1) {
+                       loc->monitor = m;
+                       loc->desktop = NULL;
+                       loc->node = NULL;
+                       return true;
+               }
+       }
+       return false;
 }
 
-bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
+bool node_matches(coordinates_t *loc, coordinates_t *ref, node_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;
+       if (loc->node == NULL) {
+               return false;
+       }
+
+       if (sel.focused != OPTION_NONE &&
+           loc->node != mon->desk->focus
+           ? sel.focused == OPTION_TRUE
+           : sel.focused == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.automatic != OPTION_NONE &&
+           loc->node->presel != NULL
+           ? sel.automatic == OPTION_TRUE
+           : sel.automatic == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.local != OPTION_NONE &&
+           loc->desktop != ref->desktop
+           ? sel.local == OPTION_TRUE
+           : sel.local == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.leaf != OPTION_NONE &&
+           loc->node->client == NULL
+           ? sel.leaf == OPTION_TRUE
+           : sel.leaf == OPTION_FALSE) {
+               return false;
+       }
+
+#define NFLAG(p) \
+       if (sel.p != OPTION_NONE && \
+           !loc->node->p \
+           ? sel.p == OPTION_TRUE \
+           : sel.p == OPTION_FALSE) { \
+               return false; \
+       }
+       NFLAG(sticky)
+       NFLAG(private)
+       NFLAG(locked)
+#undef WFLAG
+
+       if (loc->node->client == NULL &&
+               (sel.same_class != OPTION_NONE ||
+                sel.tiled != OPTION_NONE ||
+                sel.pseudo_tiled != OPTION_NONE ||
+                sel.floating != OPTION_NONE ||
+                sel.fullscreen != OPTION_NONE ||
+                sel.below != OPTION_NONE ||
+                sel.normal != OPTION_NONE ||
+                sel.above != OPTION_NONE ||
+                sel.urgent != OPTION_NONE)) {
+               return false;
+       }
+
+       if (ref->node != NULL && ref->node->client != NULL &&
+           sel.same_class != OPTION_NONE &&
+           streq(loc->node->client->class_name, ref->node->client->class_name)
+           ? sel.same_class == OPTION_FALSE
+           : sel.same_class == OPTION_TRUE) {
+               return false;
+       }
+
+#define WSTATE(p, e) \
+       if (sel.p != OPTION_NONE && \
+           loc->node->client->state != e \
+           ? sel.p == OPTION_TRUE \
+           : sel.p == OPTION_FALSE) { \
+               return false; \
+       }
+       WSTATE(tiled, STATE_TILED)
+       WSTATE(pseudo_tiled, STATE_PSEUDO_TILED)
+       WSTATE(floating, STATE_FLOATING)
+       WSTATE(fullscreen, STATE_FULLSCREEN)
+#undef WSTATE
+
+#define WLAYER(p, e) \
+       if (sel.p != OPTION_NONE && \
+           loc->node->client->layer != e \
+           ? sel.p == OPTION_TRUE \
+           : sel.p == OPTION_FALSE) { \
+               return false; \
+       }
+       WLAYER(below, LAYER_BELOW)
+       WLAYER(normal, LAYER_NORMAL)
+       WLAYER(above, LAYER_ABOVE)
+#undef WLAYER
+
+#define WFLAG(p) \
+       if (sel.p != OPTION_NONE && \
+           !loc->node->client->p \
+           ? sel.p == OPTION_TRUE \
+           : sel.p == OPTION_FALSE) { \
+               return false; \
+       }
+       WFLAG(urgent)
+#undef WFLAG
+
+       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;
+       if (sel.occupied != OPTION_NONE &&
+           loc->desktop->root == NULL
+           ? sel.occupied == OPTION_TRUE
+           : sel.occupied == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.focused != OPTION_NONE &&
+           mon->desk != loc->desktop
+           ? sel.focused == OPTION_TRUE
+           : sel.focused == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.urgent != OPTION_NONE &&
+           !is_urgent(loc->desktop)
+           ? sel.urgent == OPTION_TRUE
+           : sel.urgent == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.local != OPTION_NONE &&
+           ref->monitor != loc->monitor
+           ? sel.local == OPTION_TRUE
+           : sel.local == OPTION_FALSE) {
+               return false;
+       }
+
+       return true;
+}
 
-    return true;
+bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t sel)
+{
+       if (sel.occupied != OPTION_NONE &&
+           loc->monitor->desk->root == NULL
+           ? sel.occupied == OPTION_TRUE
+           : sel.occupied == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.focused != OPTION_NONE &&
+           mon != loc->monitor
+           ? sel.focused == OPTION_TRUE
+           : sel.focused == OPTION_FALSE) {
+               return false;
+       }
+
+       return true;
 }