]> git.lizzy.rs Git - bspwm.git/blobdiff - src/query.c
bspwm: port rounded corners patch to latest version
[bspwm.git] / src / query.c
index 7dcdf38112502c976a1c48b1243246719f6f4249..a3f906ebaef8e9731f379f52a49de0382dd5035d 100644 (file)
@@ -35,7 +35,7 @@
 #include "query.h"
 #include "geometry.h"
 
-void query_tree(FILE *rsp)
+void query_state(FILE *rsp)
 {
        fprintf(rsp, "{");
        fprintf(rsp, "\"focusedMonitorId\":%u,", mon->id);
@@ -58,8 +58,12 @@ void query_tree(FILE *rsp)
        fprintf(rsp,",");
        fprintf(rsp, "\"stackingList\":");
        query_stack(rsp);
+       if (restart) {
+               fprintf(rsp,",");
+               fprintf(rsp, "\"eventSubscribers\":");
+               query_subscribers(rsp);
+       }
        fprintf(rsp, "}");
-
 }
 
 void query_monitor(monitor_t *m, FILE *rsp)
@@ -72,6 +76,7 @@ void query_monitor(monitor_t *m, FILE *rsp)
        fprintf(rsp, "\"stickyCount\":%i,", m->sticky_count);
        fprintf(rsp, "\"windowGap\":%i,", m->window_gap);
        fprintf(rsp, "\"borderWidth\":%u,", m->border_width);
+       fprintf(rsp, "\"borderRadius\":%u,", m->border_radius);
        fprintf(rsp, "\"focusedDesktopId\":%u,", m->desk->id);
        fprintf(rsp, "\"padding\":");
        query_padding(m->padding, rsp);
@@ -97,8 +102,10 @@ void query_desktop(desktop_t *d, FILE *rsp)
        fprintf(rsp, "\"name\":\"%s\",", d->name);
        fprintf(rsp, "\"id\":%u,", d->id);
        fprintf(rsp, "\"layout\":\"%s\",", LAYOUT_STR(d->layout));
+       fprintf(rsp, "\"userLayout\":\"%s\",", LAYOUT_STR(d->user_layout));
        fprintf(rsp, "\"windowGap\":%i,", d->window_gap);
        fprintf(rsp, "\"borderWidth\":%u,", d->border_width);
+       fprintf(rsp, "\"borderRadius\":%u,", d->border_radius);
        fprintf(rsp, "\"focusedNodeId\":%u,", d->focus != NULL ? d->focus->id : 0);
        fprintf(rsp, "\"padding\":");
        query_padding(d->padding, rsp);
@@ -117,7 +124,6 @@ void query_node(node_t *n, FILE *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, "\"hidden\":%s,", BOOL_STR(n->hidden));
                fprintf(rsp, "\"sticky\":%s,", BOOL_STR(n->sticky));
@@ -163,6 +169,7 @@ void query_client(client_t *c, FILE *rsp)
                fprintf(rsp, "\"className\":\"%s\",", c->class_name);
                fprintf(rsp, "\"instanceName\":\"%s\",", c->instance_name);
                fprintf(rsp, "\"borderWidth\":%u,", c->border_width);
+               fprintf(rsp, "\"borderRadius\":%u,", c->border_radius);
                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));
@@ -222,15 +229,35 @@ void query_stack(FILE *rsp)
        fprintf(rsp, "]");
 }
 
-int query_node_ids(coordinates_t *ref, coordinates_t *trg, node_select_t *sel, FILE *rsp)
+void query_subscribers(FILE *rsp)
+{
+       fprintf(rsp, "[");
+       for (subscriber_list_t *s = subscribe_head; s != NULL; s = s->next) {
+               fprintf(rsp, "{\"fileDescriptor\": %i", fileno(s->stream));
+               if (s->fifo_path != NULL) {
+                       fprintf(rsp, ",\"fifoPath\":\"%s\"", s->fifo_path);
+               }
+               fprintf(rsp, ",\"field\":%i,\"count\":%i}", s->field, s->count);
+               if (s->next != NULL) {
+                       fprintf(rsp, ",");
+               }
+       }
+       fprintf(rsp, "]");
+}
+
+int query_node_ids(coordinates_t *mon_ref, coordinates_t *desk_ref, coordinates_t* ref, coordinates_t *trg, monitor_select_t *mon_sel, desktop_select_t *desk_sel, node_select_t *sel, FILE *rsp)
 {
        int count = 0;
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-               if (trg->monitor != NULL && m != trg->monitor) {
+               coordinates_t loc = {m, NULL, NULL};
+               if ((trg->monitor != NULL && m != trg->monitor) ||
+                   (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
                        continue;
                }
                for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
-                       if (trg->desktop != NULL && d != trg->desktop) {
+                       coordinates_t loc = {m, d, NULL};
+                       if ((trg->desktop != NULL && d != trg->desktop) ||
+                           (desk_sel != NULL && !desktop_matches(&loc, desk_ref, desk_sel))) {
                                continue;
                        }
                        count += query_node_ids_in(d->root, d, m, ref, trg, sel, rsp);
@@ -247,7 +274,7 @@ int query_node_ids_in(node_t *n, desktop_t *d, monitor_t *m, coordinates_t *ref,
        } else {
                coordinates_t loc = {m, d, n};
                if ((trg->node == NULL || n == trg->node) &&
-                   (sel == NULL || node_matches(&loc, ref, *sel))) {
+                   (sel == NULL || node_matches(&loc, ref, sel))) {
                        fprintf(rsp, "0x%08X\n", n->id);
                        count++;
                }
@@ -257,17 +284,19 @@ int query_node_ids_in(node_t *n, desktop_t *d, monitor_t *m, coordinates_t *ref,
        return count;
 }
 
-int query_desktop_ids(coordinates_t *ref, coordinates_t *trg, desktop_select_t *sel, desktop_printer_t printer, FILE *rsp)
+int query_desktop_ids(coordinates_t* mon_ref, coordinates_t *ref, coordinates_t *trg, monitor_select_t *mon_sel, desktop_select_t *sel, desktop_printer_t printer, FILE *rsp)
 {
        int count = 0;
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
-               if (trg->monitor != NULL && m != trg->monitor) {
+               coordinates_t loc = {m, NULL, NULL};
+               if ((trg->monitor != NULL && m != trg->monitor) ||
+                   (mon_sel != NULL && !monitor_matches(&loc, mon_ref, mon_sel))) {
                        continue;
                }
                for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
                        coordinates_t loc = {m, d, NULL};
                        if ((trg->desktop != NULL && d != trg->desktop) ||
-                           (sel != NULL && !desktop_matches(&loc, ref, *sel))) {
+                           (sel != NULL && !desktop_matches(&loc, ref, sel))) {
                                continue;
                        }
                        printer(d, rsp);
@@ -283,7 +312,7 @@ int query_monitor_ids(coordinates_t *ref, coordinates_t *trg, monitor_select_t *
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
                coordinates_t loc = {m, NULL, NULL};
                if ((trg->monitor != NULL && m != trg->monitor) ||
-                       (sel != NULL && !monitor_matches(&loc, ref, *sel))) {
+                   (sel != NULL && !monitor_matches(&loc, ref, sel))) {
                        continue;
                }
                printer(m, rsp);
@@ -312,6 +341,22 @@ void fprint_desktop_name(desktop_t *d, FILE *rsp)
        fprintf(rsp, "%s\n", d->name);
 }
 
+void print_ignore_request(state_transition_t st, FILE *rsp)
+{
+       if (st == 0) {
+               fprintf(rsp, "none");
+       } else {
+               unsigned int cnt = 0;
+               if (st & STATE_TRANSITION_ENTER) {
+                       fprintf(rsp, "enter");
+                       cnt++;
+               }
+               if (st & STATE_TRANSITION_EXIT) {
+                       fprintf(rsp, "%sexit", cnt > 0 ? "," : "");
+               }
+       }
+}
+
 void print_modifier_mask(uint16_t m, FILE *rsp)
 {
        switch (m) {
@@ -384,6 +429,26 @@ void print_pointer_action(pointer_action_t a, FILE *rsp)
        }
 }
 
+void resolve_rule_consequence(rule_consequence_t *csq)
+{
+       coordinates_t ref = {mon, mon->desk, mon->desk->focus};
+       coordinates_t dst = {NULL, NULL, NULL};
+       monitor_t *monitor = monitor_from_desc(csq->monitor_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.monitor;
+       desktop_t *desktop = desktop_from_desc(csq->desktop_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.desktop;
+       node_t *node = node_from_desc(csq->node_desc, &ref, &dst) != SELECTOR_OK ? NULL : dst.node;
+
+#define PRINT_OBJECT_ID(name) \
+       if (name == NULL) { \
+               csq->name##_desc[0] = '\0'; \
+       } else { \
+               snprintf(csq->name##_desc, 11, "0x%08X", name->id); \
+       }
+       PRINT_OBJECT_ID(monitor)
+       PRINT_OBJECT_ID(desktop)
+       PRINT_OBJECT_ID(node)
+#undef PRINT_OBJECT_ID
+}
+
 void print_rule_consequence(char **buf, rule_consequence_t *csq)
 {
        char *rect_buf = NULL;
@@ -392,11 +457,12 @@ void print_rule_consequence(char **buf, rule_consequence_t *csq)
                rect_buf = malloc(1);
                *rect_buf = '\0';
        }
+
        asprintf(buf, "monitor=%s desktop=%s node=%s state=%s layer=%s split_dir=%s split_ratio=%lf hidden=%s sticky=%s private=%s locked=%s marked=%s center=%s follow=%s manage=%s focus=%s border=%s rectangle=%s",
                csq->monitor_desc, csq->desktop_desc, csq->node_desc,
                csq->state == NULL ? "" : STATE_STR(*csq->state),
                csq->layer == NULL ? "" : LAYER_STR(*csq->layer),
-               csq->split_dir, csq->split_ratio,
+               csq->split_dir == NULL ? "" : SPLIT_DIR_STR(*csq->split_dir), csq->split_ratio,
                ON_OFF_STR(csq->hidden), ON_OFF_STR(csq->sticky), ON_OFF_STR(csq->private),
                ON_OFF_STR(csq->locked), ON_OFF_STR(csq->marked), ON_OFF_STR(csq->center), ON_OFF_STR(csq->follow),
                ON_OFF_STR(csq->manage), ON_OFF_STR(csq->focus), ON_OFF_STR(csq->border), rect_buf);
@@ -415,8 +481,8 @@ node_select_t make_node_select(void)
        node_select_t sel = {
                .automatic = OPTION_NONE,
                .focused = OPTION_NONE,
-               .local = OPTION_NONE,
                .active = OPTION_NONE,
+               .local = OPTION_NONE,
                .leaf = OPTION_NONE,
                .window = OPTION_NONE,
                .tiled = OPTION_NONE,
@@ -434,7 +500,9 @@ node_select_t make_node_select(void)
                .ancestor_of = OPTION_NONE,
                .below = OPTION_NONE,
                .normal = OPTION_NONE,
-               .above = OPTION_NONE
+               .above = OPTION_NONE,
+               .horizontal = OPTION_NONE,
+               .vertical = OPTION_NONE
        };
        return sel;
 }
@@ -444,8 +512,13 @@ desktop_select_t make_desktop_select(void)
        desktop_select_t sel = {
                .occupied = OPTION_NONE,
                .focused = OPTION_NONE,
+               .active = OPTION_NONE,
                .urgent = OPTION_NONE,
-               .local = OPTION_NONE
+               .local = OPTION_NONE,
+               .tiled = OPTION_NONE,
+               .monocle = OPTION_NONE,
+               .user_tiled = OPTION_NONE,
+               .user_monocle = OPTION_NONE
        };
        return sel;
 }
@@ -461,18 +534,24 @@ monitor_select_t make_monitor_select(void)
 
 int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
+       dst->node = NULL;
+
        coordinates_t ref_copy = *ref;
        ref = &ref_copy;
        char *desc_copy = copy_string(desc, strlen(desc));
        desc = desc_copy;
 
-       char *hash = NULL;
+       char *hash = strrchr(desc, '#');
        char *path = strrchr(desc, '@');
-       if (path == NULL) {
-               hash = strrchr(desc, '#');
-       } else {
-               if (path != desc && *(path - 1) == '#') {
+       char *colon = strrchr(desc, ':');
+
+       /* Adjust or discard hashes inside a DESKTOP_SEL, e.g. `newest#@prev#older:/1/2` */
+       if (hash != NULL && colon != NULL && path != NULL &&
+           path < hash && hash < colon) {
+               if (path > desc && *(path - 1) == '#') {
                        hash = path - 1;
+               } else {
+                       hash = NULL;
                }
        }
 
@@ -488,42 +567,50 @@ int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                }
        }
 
+       /* Discard colons within references, e.g. `@next.occupied:/#any.descendant_of.window` */
+       if (colon != NULL && hash != NULL && colon < hash) {
+               colon = NULL;
+       }
+
        node_select_t sel = make_node_select();
-       char *colon = strrchr(desc, ':');
 
        if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
                free(desc_copy);
                return SELECTOR_BAD_MODIFIERS;
        }
 
-       dst->node = NULL;
-
        direction_t dir;
        cycle_dir_t cyc;
        history_dir_t hdi;
        if (parse_direction(desc, &dir)) {
-               find_nearest_neighbor(ref, dst, dir, sel);
+               find_nearest_neighbor(ref, dst, dir, &sel);
        } else if (parse_cycle_direction(desc, &cyc)) {
-               find_closest_node(ref, dst, cyc, sel);
+               find_closest_node(ref, dst, cyc, &sel);
        } else if (parse_history_direction(desc, &hdi)) {
-               history_find_node(hdi, ref, dst, sel);
+               history_find_node(hdi, ref, dst, &sel);
+       } else if (streq("any", desc)) {
+               find_any_node(ref, dst, &sel);
+       } else if (streq("first_ancestor", desc)) {
+               find_first_ancestor(ref, dst, &sel);
        } else if (streq("last", desc)) {
-               history_find_node(HISTORY_OLDER, ref, dst, sel);
+               history_find_node(HISTORY_OLDER, ref, dst, &sel);
        } else if (streq("newest", desc)) {
-               history_find_newest_node(ref, dst, sel);
+               history_find_newest_node(ref, dst, &sel);
        } else if (streq("biggest", desc)) {
-               find_biggest(ref, dst, sel);
+               find_by_area(AREA_BIGGEST, ref, dst, &sel);
+       } else if (streq("smallest", desc)) {
+               find_by_area(AREA_SMALLEST, ref, dst, &sel);
        } else if (streq("pointed", desc)) {
                xcb_window_t win = XCB_NONE;
                query_pointer(&win, NULL);
-               if (locate_window(win, dst) && node_matches(dst, ref, sel)) {
+               if (locate_leaf(win, dst) && node_matches(dst, ref, &sel)) {
                        return SELECTOR_OK;
                } else {
                        return SELECTOR_INVALID;
                }
        } else if (streq("focused", desc)) {
                coordinates_t loc = {mon, mon->desk, mon->desk->focus};
-               if (node_matches(&loc, ref, sel)) {
+               if (node_matches(&loc, ref, &sel)) {
                        *dst = loc;
                }
        } else if (*desc == '@') {
@@ -566,7 +653,7 @@ int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                }
                free(desc_copy);
                if (dst->node != NULL) {
-                       if (node_matches(dst, ref, sel)) {
+                       if (node_matches(dst, ref, &sel)) {
                                return SELECTOR_OK;
                        } else {
                                return SELECTOR_INVALID;
@@ -579,7 +666,7 @@ int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                uint32_t id;
                if (parse_id(desc, &id)) {
                        free(desc_copy);
-                       if (find_by_id(id, dst) && node_matches(dst, ref, sel)) {
+                       if (find_by_id(id, dst) && node_matches(dst, ref, &sel)) {
                                return SELECTOR_OK;
                        } else {
                                return SELECTOR_INVALID;
@@ -601,6 +688,8 @@ int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 
 int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
+       dst->desktop = NULL;
+
        if (*desc == '%') {
                locate_desktop(desc + 1, dst);
                goto end;
@@ -612,6 +701,12 @@ int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
        desc = desc_copy;
 
        char *hash = strrchr(desc, '#');
+       char *colon = strrchr(desc, ':');
+
+       /* Discard hashes inside a MONITOR_SEL, e.g. `primary#next:focused` */
+       if (hash != NULL && colon != NULL && hash < colon) {
+               hash = NULL;
+       }
 
        if (hash != NULL) {
                *hash = '\0';
@@ -625,31 +720,35 @@ int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                }
        }
 
+       /* Discard colons within references, e.g. `DisplayPort-1:focused#next.local` */
+       if (colon != NULL && hash != NULL && colon < hash) {
+               colon = NULL;
+       }
+
        desktop_select_t sel = make_desktop_select();
-       char *colon = strrchr(desc, ':');
 
        if (!parse_desktop_modifiers(colon != NULL ? colon : desc, &sel)) {
                free(desc_copy);
                return SELECTOR_BAD_MODIFIERS;
        }
 
-       dst->desktop = NULL;
-
        cycle_dir_t cyc;
        history_dir_t hdi;
        uint16_t idx;
        uint32_t id;
        if (parse_cycle_direction(desc, &cyc)) {
-               find_closest_desktop(ref, dst, cyc, sel);
+               find_closest_desktop(ref, dst, cyc, &sel);
        } else if (parse_history_direction(desc, &hdi)) {
-               history_find_desktop(hdi, ref, dst, sel);
+               history_find_desktop(hdi, ref, dst, &sel);
+       } else if (streq("any", desc)) {
+               find_any_desktop(ref, dst, &sel);
        } else if (streq("last", desc)) {
-               history_find_desktop(HISTORY_OLDER, ref, dst, sel);
+               history_find_desktop(HISTORY_OLDER, ref, dst, &sel);
        } else if (streq("newest", desc)) {
-               history_find_newest_desktop(ref, dst, sel);
+               history_find_newest_desktop(ref, dst, &sel);
        } else if (streq("focused", desc)) {
                coordinates_t loc = {mon, mon->desk, NULL};
-               if (desktop_matches(&loc, ref, sel)) {
+               if (desktop_matches(&loc, ref, &sel)) {
                        *dst = loc;
                }
        } else if (colon != NULL) {
@@ -658,12 +757,12 @@ int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                if ((ret = monitor_from_desc(desc, ref, dst)) == SELECTOR_OK) {
                        if (streq("focused", colon + 1)) {
                                coordinates_t loc = {dst->monitor, dst->monitor->desk, NULL};
-                               if (desktop_matches(&loc, ref, sel)) {
+                               if (desktop_matches(&loc, ref, &sel)) {
                                        *dst = loc;
                                }
                        } else if (parse_index(colon + 1, &idx)) {
                                free(desc_copy);
-                               if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, sel)) {
+                               if (desktop_from_index(idx, dst, dst->monitor) && desktop_matches(dst, ref, &sel)) {
                                        return SELECTOR_OK;
                                } else {
                                        return SELECTOR_INVALID;
@@ -678,21 +777,21 @@ int desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                }
        } else if (parse_index(desc, &idx) && desktop_from_index(idx, dst, NULL)) {
                free(desc_copy);
-               if (desktop_matches(dst, ref, sel)) {
+               if (desktop_matches(dst, ref, &sel)) {
                        return SELECTOR_OK;
                } else {
                        return SELECTOR_INVALID;
                }
        } else if (parse_id(desc, &id) && desktop_from_id(id, dst, NULL)) {
                free(desc_copy);
-               if (desktop_matches(dst, ref, sel)) {
+               if (desktop_matches(dst, ref, &sel)) {
                        return SELECTOR_OK;
                } else {
                        return SELECTOR_INVALID;
                }
        } else {
                int hits = 0;
-               if (desktop_from_name(desc, ref, dst, sel, &hits)) {
+               if (desktop_from_name(desc, ref, dst, &sel, &hits)) {
                        free(desc_copy);
                        return SELECTOR_OK;
                } else {
@@ -717,6 +816,8 @@ end:
 
 int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
+       dst->monitor = NULL;
+
        if (*desc == '%') {
                locate_monitor(desc + 1, dst);
                goto end;
@@ -748,33 +849,33 @@ int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                return SELECTOR_BAD_MODIFIERS;
        }
 
-       dst->monitor = NULL;
-
        direction_t dir;
        cycle_dir_t cyc;
        history_dir_t hdi;
        uint16_t idx;
        uint32_t id;
        if (parse_direction(desc, &dir)) {
-               dst->monitor = nearest_monitor(ref->monitor, dir, sel);
+               dst->monitor = nearest_monitor(ref->monitor, dir, &sel);
        } else if (parse_cycle_direction(desc, &cyc)) {
-               dst->monitor = closest_monitor(ref->monitor, cyc, sel);
+               dst->monitor = closest_monitor(ref->monitor, cyc, &sel);
        } else if (parse_history_direction(desc, &hdi)) {
-               history_find_monitor(hdi, ref, dst, sel);
+               history_find_monitor(hdi, ref, dst, &sel);
+       } else if (streq("any", desc)) {
+               find_any_monitor(ref, dst, &sel);
        } else if (streq("last", desc)) {
-               history_find_monitor(HISTORY_OLDER, ref, dst, sel);
+               history_find_monitor(HISTORY_OLDER, ref, dst, &sel);
        } else if (streq("newest", desc)) {
-               history_find_newest_monitor(ref, dst, sel);
+               history_find_newest_monitor(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)) {
+                       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)) {
+               if (monitor_matches(&loc, ref, &sel)) {
                        dst->monitor = mon;
                }
        } else if (streq("pointed", desc)) {
@@ -788,14 +889,14 @@ int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                }
        } else if (parse_index(desc, &idx) && monitor_from_index(idx, dst)) {
                free(desc_copy);
-               if (monitor_matches(dst, ref, sel)) {
+               if (monitor_matches(dst, ref, &sel)) {
                        return SELECTOR_OK;
                } else {
                        return SELECTOR_INVALID;
                }
        } else if (parse_id(desc, &id) && monitor_from_id(id, dst)) {
                free(desc_copy);
-               if (monitor_matches(dst, ref, sel)) {
+               if (monitor_matches(dst, ref, &sel)) {
                        return SELECTOR_OK;
                } else {
                        return SELECTOR_INVALID;
@@ -803,7 +904,7 @@ int monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
        } else {
                if (locate_monitor(desc, dst)) {
                        free(desc_copy);
-                       if (monitor_matches(dst, ref, sel)) {
+                       if (monitor_matches(dst, ref, &sel)) {
                                return SELECTOR_OK;
                        } else {
                                return SELECTOR_INVALID;
@@ -824,6 +925,23 @@ end:
        return SELECTOR_OK;
 }
 
+bool locate_leaf(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->id == win) {
+                                       loc->monitor = m;
+                                       loc->desktop = d;
+                                       loc->node = n;
+                                       return true;
+                               }
+                       }
+               }
+       }
+       return false;
+}
+
 bool locate_window(xcb_window_t win, coordinates_t *loc)
 {
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
@@ -887,7 +1005,7 @@ bool desktop_from_id(uint32_t id, coordinates_t *loc, monitor_t *mm)
        return false;
 }
 
-bool desktop_from_name(char *name, coordinates_t *ref, coordinates_t *dst, desktop_select_t sel, int *hits)
+bool desktop_from_name(char *name, coordinates_t *ref, coordinates_t *dst, desktop_select_t *sel, int *hits)
 {
        for (monitor_t *m = mon_head; m != NULL; m = m->next) {
                for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
@@ -951,59 +1069,66 @@ bool monitor_from_index(int idx, coordinates_t *loc)
        return false;
 }
 
-bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t sel)
+bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
 {
        if (loc->node == NULL) {
                return false;
        }
 
-       if (sel.focused != OPTION_NONE &&
+       if (sel->focused != OPTION_NONE &&
+           loc->node != mon->desk->focus
+           ? sel->focused == OPTION_TRUE
+           : sel->focused == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel->active != OPTION_NONE &&
            loc->node != loc->desktop->focus
-           ? sel.focused == OPTION_TRUE
-           : sel.focused == OPTION_FALSE) {
+           ? sel->active == OPTION_TRUE
+           : sel->active == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.automatic != OPTION_NONE &&
+       if (sel->automatic != OPTION_NONE &&
            loc->node->presel != NULL
-           ? sel.automatic == OPTION_TRUE
-           : sel.automatic == OPTION_FALSE) {
+           ? sel->automatic == OPTION_TRUE
+           : sel->automatic == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.local != OPTION_NONE &&
+       if (sel->local != OPTION_NONE &&
            loc->desktop != ref->desktop
-           ? sel.local == OPTION_TRUE
-           : sel.local == OPTION_FALSE) {
+           ? sel->local == OPTION_TRUE
+           : sel->local == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.active != OPTION_NONE &&
+       if (sel->active != OPTION_NONE &&
            loc->desktop != loc->monitor->desk
-           ? sel.active == OPTION_TRUE
-           : sel.active == OPTION_FALSE) {
+           ? sel->active == OPTION_TRUE
+           : sel->active == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.leaf != OPTION_NONE &&
+       if (sel->leaf != OPTION_NONE &&
            !is_leaf(loc->node)
-           ? sel.leaf == OPTION_TRUE
-           : sel.leaf == OPTION_FALSE) {
+           ? sel->leaf == OPTION_TRUE
+           : sel->leaf == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.window != OPTION_NONE &&
+       if (sel->window != OPTION_NONE &&
            loc->node->client == NULL
-           ? sel.window == OPTION_TRUE
-           : sel.window == OPTION_FALSE) {
+           ? sel->window == OPTION_TRUE
+           : sel->window == OPTION_FALSE) {
                return false;
        }
 
 #define NFLAG(p) \
-       if (sel.p != OPTION_NONE && \
+       if (sel->p != OPTION_NONE && \
            !loc->node->p \
-           ? sel.p == OPTION_TRUE \
-           : sel.p == OPTION_FALSE) { \
+           ? sel->p == OPTION_TRUE \
+           : sel->p == OPTION_FALSE) { \
                return false; \
        }
        NFLAG(hidden)
@@ -1013,46 +1138,59 @@ bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t sel)
        NFLAG(marked)
 #undef NFLAG
 
-       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;
+#define NSPLIT(p, e) \
+       if (sel->p != OPTION_NONE && \
+           loc->node->split_type != e \
+           ? sel->p == OPTION_TRUE \
+           : sel->p == OPTION_FALSE) { \
+               return false; \
        }
+       NSPLIT(horizontal, TYPE_HORIZONTAL)
+       NSPLIT(vertical, TYPE_VERTICAL)
+#undef NSPLIT
 
-       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) {
+       if (sel->descendant_of != OPTION_NONE &&
+           !is_descendant(loc->node, ref->node)
+           ? sel->descendant_of == OPTION_TRUE
+           : sel->descendant_of == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.descendant_of != OPTION_NONE &&
-           !is_descendant(loc->node, ref->node)
-           ? sel.descendant_of == OPTION_TRUE
-           : sel.descendant_of == OPTION_FALSE) {
+       if (sel->ancestor_of != OPTION_NONE &&
+           !is_descendant(ref->node, loc->node)
+           ? sel->ancestor_of == OPTION_TRUE
+           : sel->ancestor_of == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.ancestor_of != OPTION_NONE &&
-           !is_descendant(ref->node, loc->node)
-           ? sel.ancestor_of == OPTION_TRUE
-           : sel.ancestor_of == OPTION_FALSE) {
+       if (loc->node->client == NULL) {
+               if (sel->same_class == OPTION_TRUE ||
+                   sel->tiled == OPTION_TRUE ||
+                   sel->pseudo_tiled == OPTION_TRUE ||
+                   sel->floating == OPTION_TRUE ||
+                   sel->fullscreen == OPTION_TRUE ||
+                   sel->below == OPTION_TRUE ||
+                   sel->normal == OPTION_TRUE ||
+                   sel->above == OPTION_TRUE ||
+                   sel->urgent == OPTION_TRUE) {
+                       return false;
+               }
+               return true;
+       }
+
+       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 && \
+       if (sel->p != OPTION_NONE && \
            loc->node->client->state != e \
-           ? sel.p == OPTION_TRUE \
-           : sel.p == OPTION_FALSE) { \
+           ? sel->p == OPTION_TRUE \
+           : sel->p == OPTION_FALSE) { \
                return false; \
        }
        WSTATE(tiled, STATE_TILED)
@@ -1062,10 +1200,10 @@ bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t sel)
 #undef WSTATE
 
 #define WLAYER(p, e) \
-       if (sel.p != OPTION_NONE && \
+       if (sel->p != OPTION_NONE && \
            loc->node->client->layer != e \
-           ? sel.p == OPTION_TRUE \
-           : sel.p == OPTION_FALSE) { \
+           ? sel->p == OPTION_TRUE \
+           : sel->p == OPTION_FALSE) { \
                return false; \
        }
        WLAYER(below, LAYER_BELOW)
@@ -1074,10 +1212,10 @@ bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t sel)
 #undef WLAYER
 
 #define WFLAG(p) \
-       if (sel.p != OPTION_NONE && \
+       if (sel->p != OPTION_NONE && \
            !loc->node->client->p \
-           ? sel.p == OPTION_TRUE \
-           : sel.p == OPTION_FALSE) { \
+           ? sel->p == OPTION_TRUE \
+           : sel->p == OPTION_FALSE) { \
                return false; \
        }
        WFLAG(urgent)
@@ -1086,52 +1224,81 @@ bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t sel)
        return true;
 }
 
-bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
+bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t *sel)
 {
-       if (sel.occupied != OPTION_NONE &&
+       if (sel->occupied != OPTION_NONE &&
            loc->desktop->root == NULL
-           ? sel.occupied == OPTION_TRUE
-           : sel.occupied == OPTION_FALSE) {
+           ? sel->occupied == OPTION_TRUE
+           : sel->occupied == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel->focused != OPTION_NONE &&
+           loc->desktop != mon->desk
+           ? sel->focused == OPTION_TRUE
+           : sel->focused == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.focused != OPTION_NONE &&
+       if (sel->active != OPTION_NONE &&
            loc->desktop != loc->monitor->desk
-           ? sel.focused == OPTION_TRUE
-           : sel.focused == OPTION_FALSE) {
+           ? sel->active == OPTION_TRUE
+           : sel->active == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.urgent != OPTION_NONE &&
+       if (sel->urgent != OPTION_NONE &&
            !is_urgent(loc->desktop)
-           ? sel.urgent == OPTION_TRUE
-           : sel.urgent == OPTION_FALSE) {
+           ? sel->urgent == OPTION_TRUE
+           : sel->urgent == OPTION_FALSE) {
                return false;
        }
 
-       if (sel.local != OPTION_NONE &&
+       if (sel->local != OPTION_NONE &&
            ref->monitor != loc->monitor
-           ? sel.local == OPTION_TRUE
-           : sel.local == OPTION_FALSE) {
+           ? sel->local == OPTION_TRUE
+           : sel->local == OPTION_FALSE) {
                return false;
        }
 
+#define DLAYOUT(p, e) \
+       if (sel->p != OPTION_NONE && \
+           loc->desktop->layout != e \
+           ? sel->p == OPTION_TRUE \
+           : sel->p == OPTION_FALSE) { \
+               return false; \
+       }
+       DLAYOUT(tiled, LAYOUT_TILED)
+       DLAYOUT(monocle, LAYOUT_MONOCLE)
+#undef DLAYOUT
+
+#define DUSERLAYOUT(p, e) \
+       if (sel->p != OPTION_NONE && \
+           loc->desktop->user_layout != e \
+           ? sel->p == OPTION_TRUE \
+           : sel->p == OPTION_FALSE) { \
+               return false; \
+       }
+       DUSERLAYOUT(user_tiled, LAYOUT_TILED)
+       DUSERLAYOUT(user_monocle, LAYOUT_MONOCLE)
+#undef DUSERLAYOUT
+
        return true;
 }
 
-bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t sel)
+bool monitor_matches(coordinates_t *loc, __attribute__((unused)) coordinates_t *ref, monitor_select_t *sel)
 {
-       if (sel.occupied != OPTION_NONE &&
+       if (sel->occupied != OPTION_NONE &&
            loc->monitor->desk->root == NULL
-           ? sel.occupied == OPTION_TRUE
-           : sel.occupied == OPTION_FALSE) {
+           ? 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) {
+       if (sel->focused != OPTION_NONE &&
+           loc->monitor != mon
+           ? sel->focused == OPTION_TRUE
+           : sel->focused == OPTION_FALSE) {
                return false;
        }