]> git.lizzy.rs Git - bspwm.git/blobdiff - src/query.c
bspwm: port rounded corners patch to latest version
[bspwm.git] / src / query.c
index f1be4b2569377381bb13022326c98085d7a06ca0..a3f906ebaef8e9731f379f52a49de0382dd5035d 100644 (file)
@@ -76,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);
@@ -104,6 +105,7 @@ void query_desktop(desktop_t *d, FILE *rsp)
        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);
@@ -167,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));
@@ -242,15 +245,19 @@ void query_subscribers(FILE *rsp)
        fprintf(rsp, "]");
 }
 
-int query_node_ids(coordinates_t *ref, coordinates_t *trg, node_select_t *sel, FILE *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);
@@ -277,11 +284,13 @@ 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) {
@@ -303,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);
@@ -420,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;
@@ -428,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);
@@ -484,7 +514,11 @@ desktop_select_t make_desktop_select(void)
                .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;
 }
@@ -500,6 +534,8 @@ 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));
@@ -509,7 +545,7 @@ int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
        char *path = strrchr(desc, '@');
        char *colon = strrchr(desc, ':');
 
-       /* Discard hashes inside a DESKTOP_SEL */
+       /* 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) == '#') {
@@ -531,6 +567,11 @@ 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();
 
        if (!parse_node_modifiers(colon != NULL ? colon : desc, &sel)) {
@@ -538,8 +579,6 @@ int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                return SELECTOR_BAD_MODIFIERS;
        }
 
-       dst->node = NULL;
-
        direction_t dir;
        cycle_dir_t cyc;
        history_dir_t hdi;
@@ -564,7 +603,7 @@ int node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
        } 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;
@@ -649,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;
@@ -660,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';
@@ -673,16 +720,18 @@ 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;
@@ -767,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;
@@ -798,8 +849,6 @@ 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;
@@ -876,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) {
@@ -1072,26 +1138,16 @@ 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;
-       }
-
-       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 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 (sel->descendant_of != OPTION_NONE &&
            !is_descendant(loc->node, ref->node)
@@ -1107,6 +1163,29 @@ bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
                return 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 && \
            loc->node->client->state != e \
@@ -1142,20 +1221,6 @@ bool node_matches(coordinates_t *loc, coordinates_t *ref, node_select_t *sel)
        WFLAG(urgent)
 #undef WFLAG
 
-       if (sel->horizontal != OPTION_NONE &&
-           loc->node->split_type != TYPE_HORIZONTAL
-           ? sel->horizontal == OPTION_TRUE
-           : sel->horizontal == OPTION_FALSE) {
-               return false;
-       }
-
-       if (sel->vertical != OPTION_NONE &&
-           loc->node->split_type != TYPE_VERTICAL
-           ? sel->vertical == OPTION_TRUE
-           : sel->vertical == OPTION_FALSE) {
-               return false;
-       }
-
        return true;
 }
 
@@ -1196,6 +1261,28 @@ bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t *s
                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;
 }