]> git.lizzy.rs Git - bspwm.git/blobdiff - rule.c
Mention the default reference
[bspwm.git] / rule.c
diff --git a/rule.c b/rule.c
index 862445e12261167e481f37607090c6118660a212..c6853d4c90030f5fed22eb36365e1e07d358964a 100644 (file)
--- a/rule.c
+++ b/rule.c
  */
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <sys/types.h>
 #include <string.h>
 #include <unistd.h>
 #include "bspwm.h"
 #include "ewmh.h"
 #include "window.h"
-#include "messages.h"
+#include "parse.h"
 #include "settings.h"
 #include "rule.h"
 
 rule_t *make_rule(void)
 {
        rule_t *r = malloc(sizeof(rule_t));
-       r->cause[0] = r->effect[0] = '\0';
+       r->class_name[0] = r->instance_name[0] = r->effect[0] = '\0';
        r->next = r->prev = NULL;
        r->one_shot = false;
        return r;
@@ -72,21 +75,26 @@ void remove_rule(rule_t *r)
 void remove_rule_by_cause(char *cause)
 {
        rule_t *r = rule_head;
+       char *class_name = strtok(cause, COL_TOK);
+       char *instance_name = strtok(NULL, COL_TOK);
        while (r != NULL) {
                rule_t *next = r->next;
-               if (streq(r->cause, cause))
+               if ((streq(class_name, MATCH_ANY) || streq(r->class_name, class_name)) &&
+                   (instance_name == NULL || streq(instance_name, MATCH_ANY) || streq(r->instance_name, instance_name))) {
                        remove_rule(r);
+               }
                r = next;
        }
 }
 
 bool remove_rule_by_index(int idx)
 {
-       for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
+       for (rule_t *r = rule_head; r != NULL; r = r->next, idx--) {
                if (idx == 0) {
                        remove_rule(r);
                        return true;
                }
+       }
        return false;
 }
 
@@ -94,6 +102,8 @@ rule_consequence_t *make_rule_conquence(void)
 {
        rule_consequence_t *rc = calloc(1, sizeof(rule_consequence_t));
        rc->manage = rc->focus = rc->border = true;
+       rc->layer = NULL;
+       rc->state = NULL;
        return rc;
 }
 
@@ -109,9 +119,9 @@ pending_rule_t *make_pending_rule(int fd, xcb_window_t win, rule_consequence_t *
 
 void add_pending_rule(pending_rule_t *pr)
 {
-       if (pr == NULL)
+       if (pr == NULL) {
                return;
-       PRINTF("add pending rule %i\n", pr->fd);
+       }
        if (pending_rule_head == NULL) {
                pending_rule_head = pending_rule_tail = pr;
        } else {
@@ -123,19 +133,23 @@ void add_pending_rule(pending_rule_t *pr)
 
 void remove_pending_rule(pending_rule_t *pr)
 {
-       if (pr == NULL)
+       if (pr == NULL) {
                return;
-       PRINTF("remove pending rule %i\n", pr->fd);
+       }
        pending_rule_t *a = pr->prev;
        pending_rule_t *b = pr->next;
-       if (a != NULL)
+       if (a != NULL) {
                a->next = b;
-       if (b != NULL)
+       }
+       if (b != NULL) {
                b->prev = a;
-       if (pr == pending_rule_head)
+       }
+       if (pr == pending_rule_head) {
                pending_rule_head = b;
-       if (pr == pending_rule_tail)
+       }
+       if (pr == pending_rule_tail) {
                pending_rule_tail = a;
+       }
        close(pr->fd);
        free(pr->csq);
        free(pr);
@@ -152,14 +166,18 @@ void apply_rules(xcb_window_t win, rule_consequence_t *csq)
                            a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
                                csq->focus = false;
                        } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
-                               csq->floating = true;
+                               if (csq->state == NULL) {
+                                       csq->state = malloc(sizeof(client_state_t));
+                               }
+                               *(csq->state) = STATE_FLOATING;
                                csq->center = true;
                        } else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK ||
                                   a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP ||
                                   a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
                                csq->manage = false;
-                               if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
+                               if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP) {
                                        window_lower(win);
+                               }
                        }
                }
                xcb_ewmh_get_atoms_reply_wipe(&win_type);
@@ -170,31 +188,48 @@ void apply_rules(xcb_window_t win, rule_consequence_t *csq)
        if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
                for (unsigned int i = 0; i < win_state.atoms_len; i++) {
                        xcb_atom_t a = win_state.atoms[i];
-                       if (a == ewmh->_NET_WM_STATE_FULLSCREEN)
-                               csq->fullscreen = true;
-                       else if (a == ewmh->_NET_WM_STATE_STICKY)
+                       if (a == ewmh->_NET_WM_STATE_FULLSCREEN) {
+                               if (csq->state == NULL) {
+                                       csq->state = malloc(sizeof(client_state_t));
+                               }
+                               *(csq->state) = STATE_FULLSCREEN;
+                       } else if (a == ewmh->_NET_WM_STATE_BELOW) {
+                               if (csq->layer == NULL) {
+                                       csq->layer = malloc(sizeof(stack_layer_t));
+                               }
+                               *(csq->layer) = LAYER_BELOW;
+                       } else if (a == ewmh->_NET_WM_STATE_ABOVE) {
+                               if (csq->layer == NULL) {
+                                       csq->layer = malloc(sizeof(stack_layer_t));
+                               }
+                               *(csq->layer) = LAYER_ABOVE;
+                       } else if (a == ewmh->_NET_WM_STATE_STICKY) {
                                csq->sticky = true;
+                       }
                }
                xcb_ewmh_get_atoms_reply_wipe(&win_state);
        }
 
+       xcb_window_t transient_for = XCB_NONE;
+       xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
+       if (transient_for != XCB_NONE) {
+               if (csq->state == NULL) {
+                       csq->state = malloc(sizeof(client_state_t));
+               }
+               *(csq->state) = STATE_FLOATING;
+       }
+
        xcb_size_hints_t size_hints;
        if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
-               if (size_hints.min_width > 0 && size_hints.min_height > 0 &&
-                   size_hints.min_width == size_hints.max_width &&
-                   size_hints.min_height == size_hints.max_height)
-                       csq->floating = true;
-               csq->min_width = size_hints.min_width;
-               csq->max_width = size_hints.max_width;
-               csq->min_height = size_hints.min_height;
-               csq->max_height = size_hints.max_height;
+               if ((size_hints.flags & (XCB_ICCCM_SIZE_HINT_P_MIN_SIZE|XCB_ICCCM_SIZE_HINT_P_MAX_SIZE)) &&
+                   size_hints.min_width == size_hints.max_width && size_hints.min_height == size_hints.max_height) {
+                       if (csq->state == NULL) {
+                               csq->state = malloc(sizeof(client_state_t));
+                       }
+                       *(csq->state) = STATE_FLOATING;
+               }
        }
 
-       xcb_window_t transient_for = XCB_NONE;
-       xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
-       if (transient_for != XCB_NONE)
-               csq->floating = true;
-
        xcb_icccm_get_wm_class_reply_t reply;
        if (xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL) == 1) {
                snprintf(csq->class_name, sizeof(csq->class_name), "%s", reply.class_name);
@@ -205,9 +240,8 @@ void apply_rules(xcb_window_t win, rule_consequence_t *csq)
        rule_t *rule = rule_head;
        while (rule != NULL) {
                rule_t *next = rule->next;
-               if (streq(rule->cause, MATCH_ANY) ||
-                   streq(rule->cause, csq->class_name) ||
-                   streq(rule->cause, csq->instance_name)) {
+               if ((streq(rule->class_name, MATCH_ANY) || streq(rule->class_name, csq->class_name)) &&
+                   (streq(rule->instance_name, MATCH_ANY) || streq(rule->instance_name, csq->instance_name))) {
                        char effect[MAXLEN];
                        snprintf(effect, sizeof(effect), "%s", rule->effect);
                        char *key = strtok(effect, CSQ_BLK);
@@ -217,8 +251,10 @@ void apply_rules(xcb_window_t win, rule_consequence_t *csq)
                                key = strtok(NULL, CSQ_BLK);
                                value = strtok(NULL, CSQ_BLK);
                        }
-                       if (rule->one_shot)
+                       if (rule->one_shot) {
                                remove_rule(rule);
+                               break;
+                       }
                }
                rule = next;
        }
@@ -226,15 +262,18 @@ void apply_rules(xcb_window_t win, rule_consequence_t *csq)
 
 bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
 {
-       if (external_rules_command[0] == '\0')
+       if (external_rules_command[0] == '\0') {
                return false;
+       }
        int fds[2];
-       if (pipe(fds) == -1)
+       if (pipe(fds) == -1) {
                return false;
+       }
        pid_t pid = fork();
        if (pid == 0) {
-               if (dpy != NULL)
+               if (dpy != NULL) {
                        close(xcb_get_file_descriptor(dpy));
+               }
                dup2(fds[1], 1);
                close(fds[0]);
                char wid[SMALEN];
@@ -252,8 +291,9 @@ bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
 
 void parse_rule_consequence(int fd, rule_consequence_t *csq)
 {
-       if (fd == -1)
+       if (fd == -1) {
                return;
+       }
        char data[BUFSIZ];
        int nb;
        while ((nb = read(fd, data, sizeof(data))) > 0) {
@@ -276,26 +316,40 @@ void parse_key_value(char *key, char *value, rule_consequence_t *csq)
                snprintf(csq->monitor_desc, sizeof(csq->monitor_desc), "%s", value);
        } else if (streq("desktop", key)) {
                snprintf(csq->desktop_desc, sizeof(csq->desktop_desc), "%s", value);
-       } else if (streq("window", key)) {
+       } else if (streq("node", key)) {
                snprintf(csq->node_desc, sizeof(csq->node_desc), "%s", value);
        } else if (streq("split_dir", key)) {
                snprintf(csq->split_dir, sizeof(csq->split_dir), "%s", value);
+       } else if (streq("state", key)) {
+               client_state_t cst;
+               if (parse_client_state(value, &cst)) {
+                       if (csq->state == NULL) {
+                               csq->state = malloc(sizeof(client_state_t));
+                       }
+                       *(csq->state) = cst;
+               }
+       } else if (streq("layer", key)) {
+               stack_layer_t lyr;
+               if (parse_stack_layer(value, &lyr)) {
+                       if (csq->layer == NULL) {
+                               csq->layer = malloc(sizeof(stack_layer_t));
+                       }
+                       *(csq->layer) = lyr;
+               }
        } else if (streq("split_ratio", key)) {
                double rat;
                if (sscanf(value, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
                        csq->split_ratio = rat;
                }
        } else if (parse_bool(value, &v)) {
-               if (streq("floating", key))
-                       csq->floating = v;
+               if (streq("hidden", key))
+                       csq->hidden = true;
 #define SETCSQ(name) \
                else if (streq(#name, key)) \
                        csq->name = v;
-               SETCSQ(pseudo_tiled)
-               SETCSQ(fullscreen)
-               SETCSQ(locked)
                SETCSQ(sticky)
                SETCSQ(private)
+               SETCSQ(locked)
                SETCSQ(center)
                SETCSQ(follow)
                SETCSQ(manage)
@@ -305,11 +359,9 @@ void parse_key_value(char *key, char *value, rule_consequence_t *csq)
        }
 }
 
-void list_rules(char *pattern, FILE *rsp)
+void list_rules(FILE *rsp)
 {
        for (rule_t *r = rule_head; r != NULL; r = r->next) {
-               if (pattern != NULL && !streq(pattern, r->cause))
-                       continue;
-               fprintf(rsp, "%s => %s\n", r->cause, r->effect);
+               fprintf(rsp, "%s:%s %c> %s\n", r->class_name, r->instance_name, r->one_shot?'-':'=', r->effect);
        }
 }