]> git.lizzy.rs Git - bspwm.git/blobdiff - query.c
Prevent potential memory leak
[bspwm.git] / query.c
diff --git a/query.c b/query.c
index 061b93f4835c9d460f7d1f5e99c710a50983f17d..18cfa74d2cfbaa95b85ed68f2e06226aa9a64f63 100644 (file)
--- a/query.c
+++ b/query.c
@@ -23,6 +23,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <strings.h>
 #include <string.h>
 #include "bspwm.h"
@@ -64,10 +65,10 @@ void query_desktops(monitor_t *m, domain_t dom, coordinates_t loc, unsigned int
                        fprintf(rsp, "%s\n", d->name);
                        continue;
                } else {
-                       fprintf(rsp, "%s %u %i %i,%i,%i,%i %c %c%s\n", d->name, d->border_width,
+                       fprintf(rsp, "%s %u %i %i,%i,%i,%i %c%s\n", d->name, d->border_width,
                                d->window_gap,
                                d->top_padding, d->right_padding, d->bottom_padding, d->left_padding,
-                               (d->layout == LAYOUT_TILED ? 'T' : 'M'), (d->floating ? 'f' : '-'),
+                               (d->layout == LAYOUT_TILED ? 'T' : 'M'),
                                (d == m->desk ? " *" : ""));
                }
                query_tree(d, d->root, rsp, depth + 1);
@@ -84,15 +85,16 @@ void query_tree(desktop_t *d, node_t *n, FILE *rsp, unsigned int depth)
 
        if (is_leaf(n)) {
                client_t *c = n->client;
-               fprintf(rsp, "%c %s %s 0x%X %u %ux%u%+i%+i %c %c%c%c%c%c%c%c%c%s\n",
+               fprintf(rsp, "%c %s %s 0x%X %u %ux%u%+i%+i %c%c %c%c %c%c%c%c%s\n",
                         (n->birth_rotation == 90 ? 'a' : (n->birth_rotation == 270 ? 'c' : 'm')),
                         c->class_name, c->instance_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->pseudo_tiled ? 'd' : '-'), (c->fullscreen ? 'F' : '-'),
-                        (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'),
-                        (c->private ? 'i' : '-'), (n->split_mode ? 'p' : '-'),
+                        (n->split_mode == MODE_AUTOMATIC ? '-' : 'p'),
+                        (c->state == STATE_TILED ? '-' : (c->state == STATE_FLOATING ? 'f' : (c->state == STATE_FULLSCREEN ? 'F' : 'p'))),
+                        (c->layer == LAYER_NORMAL ? '-' : (c->layer == LAYER_ABOVE ? 'a' : 'b')),
+                        (c->urgent ? 'u' : '-'), (c->locked ? 'l' : '-'), (c->sticky ? 's' : '-'), (c->private ? 'i' : '-'),
                         (n == d->focus ? " *" : ""));
        } else {
                fprintf(rsp, "%c %c %lf\n", (n->split_type == TYPE_HORIZONTAL ? 'H' : 'V'),
@@ -139,31 +141,111 @@ void query_windows(coordinates_t loc, FILE *rsp)
        }
 }
 
+client_select_t make_client_select(void)
+{
+       client_select_t sel = {
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE,
+               NULL
+       };
+       return sel;
+}
+
+desktop_select_t make_desktop_select(void)
+{
+       desktop_select_t sel = {
+               OPTION_NONE,
+               OPTION_NONE,
+               OPTION_NONE
+       };
+       return sel;
+}
+
+void cleanup_client_select(client_select_t *sel)
+{
+       free(sel->layer);
+}
+
 bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
-       client_select_t sel = {CLIENT_TYPE_ALL, CLIENT_CLASS_ALL, CLIENT_MODE_ALL, false, false, false};
+       client_select_t sel = make_client_select();
        char *tok;
        while ((tok = strrchr(desc, CAT_CHR)) != NULL) {
                tok[0] = '\0';
                tok++;
                if (streq("tiled", tok)) {
-                       sel.type = CLIENT_TYPE_TILED;
+                       sel.tiled = OPTION_TRUE;
+               } else if (streq("!tiled", tok)) {
+                       sel.tiled = OPTION_FALSE;
+               } else if (streq("pseudo_tiled", tok)) {
+                       sel.pseudo_tiled = OPTION_TRUE;
+               } else if (streq("!pseudo_tiled", tok)) {
+                       sel.pseudo_tiled = OPTION_FALSE;
                } 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("manual", tok)) {
-                       sel.mode = CLIENT_MODE_MANUAL;
+                       sel.floating = OPTION_TRUE;
+               } else if (streq("!floating", tok)) {
+                       sel.floating = OPTION_FALSE;
+               } else if (streq("same_class", tok)) {
+                       sel.same_class = OPTION_TRUE;
+               } else if (streq("!same_class", tok)) {
+                       sel.same_class = OPTION_FALSE;
                } else if (streq("automatic", tok)) {
-                       sel.mode = CLIENT_MODE_AUTOMATIC;
+                       sel.automatic = OPTION_TRUE;
+               } else if (streq("!automatic", tok)) {
+                       sel.automatic = OPTION_FALSE;
+               } else if (streq("fullscreen", tok)) {
+                       sel.fullscreen = OPTION_TRUE;
+               } else if (streq("!fullscreen", tok)) {
+                       sel.fullscreen = OPTION_FALSE;
                } else if (streq("urgent", tok)) {
-                       sel.urgent = true;
+                       sel.urgent = OPTION_TRUE;
+               } else if (streq("!urgent", tok)) {
+                       sel.urgent = OPTION_FALSE;
                } else if (streq("local", tok)) {
-                       sel.local = true;
-               } else if (streq("unfocused", tok)) {
-                       sel.unfocused = true;
+                       sel.local = OPTION_TRUE;
+               } else if (streq("!local", tok)) {
+                       sel.local = OPTION_FALSE;
+               } else if (streq("private", tok)) {
+                       sel.private = OPTION_TRUE;
+               } else if (streq("!private", tok)) {
+                       sel.private = OPTION_FALSE;
+               } else if (streq("sticky", tok)) {
+                       sel.sticky = OPTION_TRUE;
+               } else if (streq("!sticky", tok)) {
+                       sel.sticky = OPTION_FALSE;
+               } else if (streq("locked", tok)) {
+                       sel.locked = OPTION_TRUE;
+               } else if (streq("!locked", tok)) {
+                       sel.locked = OPTION_FALSE;
+               } else if (streq("focused", tok)) {
+                       sel.focused = OPTION_TRUE;
+               } else if (streq("!focused", tok)) {
+                       sel.focused = OPTION_FALSE;
+               } else if (streq("below", tok)) {
+                       if (sel.layer == NULL) {
+                               sel.layer = malloc(sizeof(stack_layer_t));
+                       }
+                       *(sel.layer) = LAYER_BELOW;
+               } else if (streq("normal", tok)) {
+                       if (sel.layer == NULL) {
+                               sel.layer = malloc(sizeof(stack_layer_t));
+                       }
+                       *(sel.layer) = LAYER_NORMAL;
+               } else if (streq("above", tok)) {
+                       if (sel.layer == NULL) {
+                               sel.layer = malloc(sizeof(stack_layer_t));
+                       }
+                       *(sel.layer) = LAYER_ABOVE;
                }
        }
 
@@ -177,7 +259,7 @@ bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
        if (parse_direction(desc, &dir)) {
                dst->node = nearest_neighbor(ref->monitor, ref->desktop, ref->node, dir, sel);
                if (dst->node == NULL && num_monitors > 1) {
-                       monitor_t *m = nearest_monitor(ref->monitor, dir, (desktop_select_t) {DESKTOP_STATUS_ALL, false, false});
+                       monitor_t *m = nearest_monitor(ref->monitor, dir, make_desktop_select());
                        if (m != NULL) {
                                coordinates_t loc = {m, m->desk, m->desk->focus};
                                if (node_matches(&loc, ref, sel)) {
@@ -208,24 +290,30 @@ bool node_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
                        locate_window(wid, dst);
        }
 
+       cleanup_client_select(&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};
+       desktop_select_t sel = make_desktop_select();
        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;
+               if (streq("occupied", tok)) {
+                       sel.occupied = OPTION_FALSE;
+               } else if (streq("!occupied", tok)) {
+                       sel.occupied = OPTION_TRUE;
                } else if (streq("urgent", tok)) {
-                       sel.urgent = true;
+                       sel.urgent = OPTION_TRUE;
+               } else if (streq("!urgent", tok)) {
+                       sel.urgent = OPTION_FALSE;
                } else if (streq("local", tok)) {
-                       sel.local = true;
+                       sel.local = OPTION_TRUE;
+               } else if (streq("!local", tok)) {
+                       sel.local = OPTION_FALSE;
                }
        }
 
@@ -268,15 +356,15 @@ bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 
 bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst)
 {
-       desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false};
+       desktop_select_t sel = make_desktop_select();
        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;
+               if (streq("occupied", tok)) {
+                       sel.occupied = OPTION_FALSE;
+               } else if (streq("!occupied", tok)) {
+                       sel.occupied = OPTION_TRUE;
                }
        }
 
@@ -382,49 +470,104 @@ bool node_matches(coordinates_t *loc, coordinates_t *ref, client_select_t sel)
        if (loc->node == NULL)
                return false;
 
-       if (sel.type != CLIENT_TYPE_ALL &&
-           is_tiled(loc->node->client)
-           ? sel.type == CLIENT_TYPE_FLOATING
-           : sel.type == CLIENT_TYPE_TILED)
+#define WSTATE(prop) \
+       if (sel.prop != OPTION_NONE && \
+           !loc->node->client->prop \
+           ? sel.prop == OPTION_TRUE \
+           : sel.prop == OPTION_FALSE) { \
+               return false; \
+       }
+       WSTATE(locked)
+       WSTATE(sticky)
+       WSTATE(private)
+       WSTATE(urgent)
+#undef MATCHSTATE
+
+       if (sel.tiled != OPTION_NONE &&
+           loc->node->client->state != STATE_TILED
+           ? sel.tiled == OPTION_TRUE
+           : sel.tiled == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.pseudo_tiled != OPTION_NONE &&
+           loc->node->client->state != STATE_PSEUDO_TILED
+           ? sel.pseudo_tiled == OPTION_TRUE
+           : sel.pseudo_tiled == OPTION_FALSE) {
+               return false;
+       }
+
+       if (sel.floating != OPTION_NONE &&
+           loc->node->client->state != STATE_FLOATING
+           ? sel.floating == OPTION_TRUE
+           : sel.floating == OPTION_FALSE) {
                return false;
+       }
+
+       if (sel.fullscreen != OPTION_NONE &&
+           loc->node->client->state != STATE_FULLSCREEN
+           ? sel.fullscreen == OPTION_TRUE
+           : sel.fullscreen == OPTION_FALSE) {
+               return false;
+       }
 
-       if (sel.class != CLIENT_CLASS_ALL && ref->node != NULL &&
+       if (sel.same_class != OPTION_NONE && ref->node != NULL &&
            streq(loc->node->client->class_name, ref->node->client->class_name)
-           ? sel.class == CLIENT_CLASS_DIFFER
-           : sel.class == CLIENT_CLASS_EQUAL)
+           ? sel.same_class == OPTION_FALSE
+           : sel.same_class == OPTION_TRUE) {
                return false;
+       }
 
-       if (sel.mode != CLIENT_MODE_ALL &&
+       if (sel.automatic != OPTION_NONE &&
            loc->node->split_mode == MODE_MANUAL
-           ? sel.mode == CLIENT_MODE_AUTOMATIC
-           : sel.mode == CLIENT_MODE_MANUAL)
+           ? sel.automatic == OPTION_TRUE
+           : sel.automatic == OPTION_FALSE) {
                return false;
+       }
 
-       if (sel.local && loc->desktop != ref->desktop)
+       if (sel.local != OPTION_NONE &&
+           loc->desktop != ref->desktop
+           ? sel.local == OPTION_TRUE
+           : sel.local == OPTION_FALSE) {
                return false;
+       }
 
-       if (sel.urgent && !loc->node->client->urgent)
+       if (sel.layer != NULL && loc->node->client->layer != *(sel.layer)) {
                return false;
+       }
 
-       if (sel.unfocused && loc->node == mon->desk->focus)
+       if (sel.focused != OPTION_NONE &&
+           loc->node != loc->desktop->focus
+           ? sel.focused == OPTION_TRUE
+           : sel.focused == OPTION_FALSE) {
                return false;
+       }
 
        return true;
 }
 
 bool desktop_matches(coordinates_t *loc, coordinates_t *ref, desktop_select_t sel)
 {
-       if (sel.status != DESKTOP_STATUS_ALL &&
+       if (sel.occupied != OPTION_NONE &&
            loc->desktop->root == NULL
-           ? sel.status == DESKTOP_STATUS_OCCUPIED
-           : sel.status == DESKTOP_STATUS_FREE)
+           ? sel.occupied == OPTION_TRUE
+           : sel.occupied == OPTION_FALSE) {
                return false;
+       }
 
-       if (sel.urgent && !is_urgent(loc->desktop))
+       if (sel.urgent != OPTION_NONE &&
+           !is_urgent(loc->desktop)
+           ? sel.urgent == OPTION_TRUE
+           : sel.urgent == OPTION_FALSE) {
                return false;
+       }
 
-       if (sel.local && ref->monitor != loc->monitor)
+       if (sel.local != OPTION_NONE &&
+           ref->monitor != loc->monitor
+           ? sel.local == OPTION_TRUE
+           : sel.local == OPTION_FALSE) {
                return false;
+       }
 
        return true;
 }