]> git.lizzy.rs Git - bspwm.git/blobdiff - messages.c
Rewrite message handling
[bspwm.git] / messages.c
index 9bfcff2d05cc32eb37485eff49fe0867210c0f18..8a34f1829b126246f06b52fb62c1ab6d6202283b 100644 (file)
+#include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include "helpers.h"
-#include "luautils.h"
+#include <errno.h>
 #include "settings.h"
 #include "messages.h"
+#include "query.h"
+#include "restore.h"
 #include "common.h"
 #include "types.h"
 #include "bspwm.h"
-#include "utils.h"
+#include "ewmh.h"
+#include "helpers.h"
+#include "window.h"
+#include "events.h"
 #include "tree.h"
+#include "rules.h"
 
-void process_message(char *msg, char *rsp)
+bool cmd_window(char **args, int num)
 {
-    char *cmd = strtok(msg, TOKEN_SEP);
-
-    if (cmd == NULL)
-        return;
-
-    if (strcmp(cmd, "get") == 0) {
-        char *name = strtok(NULL, TOKEN_SEP);
-        get_setting(name, rsp);
-    } else if (strcmp(cmd, "set") == 0) {
-        char *name = strtok(NULL, TOKEN_SEP);
-        char *value = strtok(NULL, TOKEN_SEP);
-        set_setting(name, value);
-    } else if (strcmp(cmd, "dump") == 0) {
-        dump_tree(desk->root, rsp, 0);
-    } else if (strcmp(cmd, "list") == 0) {
-        list_desktops(rsp);
-    } else if (strcmp(cmd, "rotate") == 0) {
-        char *deg = strtok(NULL, TOKEN_SEP);
-        if (deg != NULL) {
-            rotate_t r;
-            if (parse_rotate(deg, &r)) {
-                rotate_tree(desk->root, r);
-                apply_layout(desk, desk->root, root_rect);
+    coordinates_t ref = {mon, mon->desk, mon->desk->focus};
+    coordinates_t trg = ref;
+
+    if (*args[0] != OPT_CHR) {
+        if (node_from_desc(*args, &ref, &trg))
+            num--, args++;
+        else
+            return false;
+    }
+
+    if (trg.node == NULL)
+        return false;
+
+    bool dirty = false;
+
+    while (num > 0) {
+        if (streq("-f", *args) || streq("--focus", *args)) {
+            coordinates_t dst = trg;
+            if (num > 1 && *(args + 1)[0] != OPT_CHR) {
+                num--, args++;
+                if (!node_from_desc(*args, &trg, &dst))
+                    return false;
             }
+            focus_node(dst.monitor, dst.desktop, dst.node);
+        } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
+            num--, args++;
+            coordinates_t dst;
+            if (desktop_from_desc(*args, &trg, &dst)) {
+                transfer_node(trg.monitor, trg.desktop, dst.monitor, dst.desktop, trg.node);
+                trg.monitor = dst.monitor;
+                trg.desktop = dst.desktop;
+            } else {
+                return false;
+            }
+        } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            coordinates_t dst;
+            if (monitor_from_desc(*args, &trg, &dst)) {
+                transfer_node(trg.monitor, trg.desktop, dst.monitor, dst.monitor->desk, trg.node);
+                trg.monitor = dst.monitor;
+                trg.desktop = dst.monitor->desk;
+            } else {
+                return false;
+            }
+        } else if (streq("-w", *args) || streq("--to-window", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            coordinates_t dst;
+            if (node_from_desc(*args, &trg, &dst))
+                transplant_node(trg.monitor, trg.desktop, trg.node, dst.node);
+            else
+                return false;
+            dirty = true;
+        } else if (streq("-s", *args) || streq("--swap", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            coordinates_t dst;
+            if (node_from_desc(*args, &trg, &dst))
+                swap_nodes(trg.node, dst.node, true);
+            else
+                return false;
+            dirty = true;
+        } else if (streq("-t", *args) || streq("--toggle", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            char *key = strtok(*args, EQL_TOK);
+            char *val = strtok(NULL, EQL_TOK);
+            state_alter_t a = ALTER_NONE;
+            bool b;
+            if (val == NULL) {
+                a = ALTER_TOGGLE;
+            } else {
+                if (parse_bool(val, &b))
+                    a = ALTER_SET;
+                else
+                    return false;
+            }
+            if (streq("fullscreen", key)) {
+                set_fullscreen(trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->fullscreen));
+                dirty = true;
+            } else if (streq("floating", key)) {
+                set_floating(trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->floating));
+                dirty = true;
+            } else if (streq("locked", key)) {
+                set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->locked));
+            }
+        } else if (streq("-p", *args) || streq("--presel", *args)) {
+            num--, args++;
+            if (num < 1 || !is_tiled(trg.node->client)
+                    || trg.desktop->layout != LAYOUT_TILED)
+                return false;
+            if (streq("cancel", *args)) {
+                reset_mode(&trg);
+            } else {
+                direction_t dir;
+                if (parse_direction(*args, &dir)) {
+                    double rat = trg.node->split_ratio;
+                    if (num > 1 && *(args + 1)[0] != OPT_CHR) {
+                        num--, args++;
+                        if (sscanf(*args, "%lf", &rat) != 1)
+                            return false;
+                    }
+                    if (auto_cancel && trg.node->split_mode == MODE_MANUAL
+                            && dir == trg.node->split_dir
+                            && rat == trg.node->split_ratio) {
+                        reset_mode(&trg);
+                    } else {
+                        trg.node->split_mode = MODE_MANUAL;
+                        trg.node->split_dir = dir;
+                        trg.node->split_ratio = rat;
+                    }
+                    window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
+                } else {
+                    return false;
+                }
+            }
+        } else if (streq("-e", *args) || streq("--edge", *args)) {
+            num--, args++;
+            if (num < 2)
+                return false;
+            direction_t dir;
+            if (!parse_direction(*args, &dir))
+                return false;
+            num--, args++;
+            fence_move_t fmo;
+            if (parse_fence_move(*args, &fmo)) {
+                move_fence(trg.node, dir, fmo);
+            } else {
+                node_t *n = find_fence(trg.node, dir);
+                if (n == NULL || sscanf(*args, "%lf", &n->split_ratio) != 1)
+                    return false;
+            }
+            dirty = true;
+        } else if (streq("-r", *args) || streq("--ratio", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            if (sscanf(*args, "%lf", &trg.node->split_ratio) == 1)
+                window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
+            else
+                return false;
+        } else if (streq("-c", *args) || streq("--close", *args)) {
+            if (num > 1)
+                return false;
+            window_close(trg.node);
+        } else if (streq("-k", *args) || streq("--kill", *args)) {
+            if (num > 1)
+                return false;
+            window_kill(trg.desktop, trg.node);
+            dirty = true;
+        } else {
+            return false;
         }
-    } else if (strcmp(cmd, "layout") == 0) {
-        char *lyt = strtok(NULL, TOKEN_SEP);
-        if (lyt != NULL) {
-            layout_t l;
-            if (parse_layout(lyt, &l)) {
-                desk->layout = l;
-                apply_layout(desk, desk->root, root_rect);
+        num--, args++;
+    }
+
+    if (dirty)
+        arrange(trg.monitor, trg.desktop);
+
+    return true;
+}
+
+bool cmd_desktop(char **args, int num)
+{
+    coordinates_t ref = {mon, mon->desk, NULL};
+    coordinates_t trg = ref;
+
+    if (*args[0] != OPT_CHR) {
+        if (desktop_from_desc(*args, &ref, &trg))
+            num--, args++;
+        else
+            return false;
+    }
+
+    bool dirty = false;
+
+    while (num > 0) {
+        if (streq("-f", *args) || streq("--focus", *args)) {
+            coordinates_t dst = trg;
+            if (num > 1 && *(args + 1)[0] != OPT_CHR) {
+                num--, args++;
+                if (!desktop_from_desc(*args, &trg, &dst))
+                    return false;
+            }
+            if (auto_alternate && dst.desktop == dst.monitor->desk && dst.monitor->last_desk != NULL)
+                dst.desktop = dst.monitor->last_desk;
+            focus_node(dst.monitor, dst.desktop, dst.desktop->focus);
+        } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
+            num--, args++;
+            if (num < 1 || trg.monitor->desk_head == trg.monitor->desk_tail)
+                return false;
+            coordinates_t dst;
+            if (monitor_from_desc(*args, &trg, &dst)) {
+                transfer_desktop(trg.monitor, dst.monitor, dst.desktop);
+                trg.monitor = dst.monitor;
+                update_current();
+            } else {
+                return false;
+            }
+        } else if (streq("-l", *args) || streq("--layout", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            layout_t lyt;
+            cycle_dir_t cyc;
+            if (parse_cycle_direction(*args, &cyc))
+                change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
+            else if (parse_layout(*args, &lyt))
+                change_layout(trg.monitor, trg.desktop, lyt);
+            else
+                return false;
+        } else if (streq("-n", *args) || streq("--rename", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            strncpy(trg.desktop->name, *args, sizeof(trg.desktop->name));
+            ewmh_update_desktop_names();
+            put_status();
+        } else if (streq("-r", *args) || streq("--rm", *args)) {
+            if (trg.desktop->root == NULL
+                    && trg.monitor->desk_head != trg.monitor->desk_tail) {
+                remove_desktop(trg.monitor, trg.desktop);
+                desktop_show(trg.monitor->desk);
+                update_current();
+                return true;
+            } else {
+                return false;
+            }
+        } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
+            reset_mode(&trg);
+        } else if (streq("-F", *args) || streq("--flip", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            flip_t flp;
+            if (parse_flip(*args, &flp)) {
+                flip_tree(trg.desktop->root, flp);
+                dirty = true;
+            }
+        } else if (streq("-R", *args) || streq("--rotate", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            int rot = atoi(*args);
+            while (rot < 0)
+                rot += 360;
+            while (rot > 359)
+                rot -= 360;
+            if ((rot % 90) != 0) {
+                return false;
+            } else {
+                rotate_tree(trg.desktop->root, rot);
+                dirty = true;
             }
+        } else if (streq("-B", *args) || streq("--balance", *args)) {
+            balance_tree(trg.desktop->root);
+            dirty = true;
+        } else if (streq("-C", *args) || streq("--circulate", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            circulate_dir_t cir;
+            if (parse_circulate_direction(*args, &cir)) {
+                circulate_leaves(trg.monitor, trg.desktop, cir);
+                dirty = true;
+            } else {
+                return false;
+            }
+        } else {
+            return false;
+        }
+        num--, args++;
+    }
+
+    if (dirty)
+        arrange(trg.monitor, trg.desktop);
+
+    return true;
+}
+
+bool cmd_monitor(char **args, int num)
+{
+    coordinates_t ref = {mon, NULL, NULL};
+    coordinates_t trg = ref;
+
+    if (*args[0] != OPT_CHR) {
+        if (monitor_from_desc(*args, &ref, &trg))
+            num--, args++;
+        else
+            return false;
+    }
+
+    while (num > 0) {
+        if (streq("-f", *args) || streq("--focus", *args)) {
+            coordinates_t dst = trg;
+            if (num > 1 && *(args + 1)[0] != OPT_CHR) {
+                num--, args++;
+                if (!desktop_from_desc(*args, &trg, &dst))
+                    return false;
+            }
+            if (auto_alternate && dst.monitor == mon && last_mon != NULL)
+                dst.monitor = last_mon;
+            focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
+        } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            while (num > 0) {
+                add_desktop(trg.monitor, make_desktop(*args));
+                num--, args++;
+            }
+        } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            while (num > 0) {
+                coordinates_t dst;
+                if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail) {
+                    remove_desktop(dst.monitor, dst.desktop);
+                    desktop_show(dst.monitor->desk);
+                }
+                num--, args++;
+            }
+        } else if (streq("-p", *args) || streq("--pad", *args)) {
+            num--, args++;
+            if (num < 4)
+                return false;
+            char values[MAXLEN];
+            snprintf(values, sizeof(values), "%s %s %s %s", args[0], args[1], args[2], args[3]);
+            if (sscanf(values, "%i %i %i %i", &trg.monitor->top_padding, &trg.monitor->right_padding, &trg.monitor->bottom_padding, &trg.monitor->left_padding) == 4)
+                for (desktop_t *d = trg.monitor->desk_head; d != NULL; d = d->next)
+                    arrange(trg.monitor, d);
+            else
+                return false;
+        } else if (streq("-n", *args) || streq("--rename", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            strncpy(trg.monitor->name, *args, sizeof(trg.monitor->name));
+            put_status();
+        } else {
+            return false;
         }
-    } else if (strcmp(cmd, "insert") == 0) {
-        static unsigned int fake_id = 0;
-        client_t *c = make_client((xcb_window_t) fake_id++);
-        node_t *n = make_node();
-        n->client = c;
-        insert_node(desk, n);
-        apply_layout(desk, desk->root, root_rect);
-    } else if (strcmp(cmd, "ratio") == 0) {
-        char *value = strtok(NULL, TOKEN_SEP);
-        if (value != NULL && desk->focus != NULL)
-            sscanf(value, "%lf", &desk->focus->split_ratio);
-    } else if (strcmp(cmd, "presel") == 0) {
-        char *dir = strtok(NULL, TOKEN_SEP);
-        if (dir != NULL) {
-            direction_t d;
-            if (parse_direction(dir, &d)) {
-                split_mode = MODE_MANUAL;
-                split_dir = d;
-                /* draw_triple_border(desk->focus, active_border_color_pxl); */
+        num--, args++;
+    }
+
+    return true;
+}
+
+bool cmd_query(char **args, int num, char *rsp) {
+    coordinates_t ref = {mon, mon->desk, mon->desk->focus};
+    coordinates_t trg = {NULL, NULL, NULL};
+    domain_t dom = DOMAIN_TREE;
+    int d = 0, t = 0;
+
+    while (num > 0) {
+        if (streq("-T", *args) || streq("--tree", *args)) {
+            dom = DOMAIN_TREE, d++;
+        } else if (streq("-M", *args) || streq("--monitors", *args)) {
+            dom = DOMAIN_MONITOR, d++;
+        } else if (streq("-D", *args) || streq("--desktops", *args)) {
+            dom = DOMAIN_DESKTOP, d++;
+        } else if (streq("-W", *args) || streq("--windows", *args)) {
+            dom = DOMAIN_WINDOW, d++;
+        } else if (streq("-H", *args) || streq("--history", *args)) {
+            dom = DOMAIN_HISTORY, d++;
+        } else if (streq("-m", *args) || streq("--monitor", *args)) {
+            trg.monitor = ref.monitor;
+            if (num > 1 && *(args + 1)[0] != OPT_CHR) {
+                num--, args++;
+                if (!monitor_from_desc(*args, &ref, &trg))
+                    return false;
+            }
+            t++;
+        } else if (streq("-d", *args) || streq("--desktop", *args)) {
+            trg.monitor = ref.monitor;
+            trg.desktop = ref.desktop;
+            if (num > 1 && *(args + 1)[0] != OPT_CHR) {
+                num--, args++;
+                if (!desktop_from_desc(*args, &ref, &trg))
+                    return false;
             }
+            t++;
+        } else if (streq("-w", *args) || streq("--window", *args)) {
+            trg = ref;
+            if (num > 1 && *(args + 1)[0] != OPT_CHR) {
+                num--, args++;
+                if (!node_from_desc(*args, &ref, &trg))
+                    return false;
+            }
+            t++;
+        } else {
+            return false;
         }
-    } else if (strcmp(cmd, "push") == 0 || strcmp(cmd, "pull") == 0) {
-        char *dir = strtok(NULL, TOKEN_SEP);
-        if (dir != NULL) {
-            fence_move_t m;
-            direction_t d;
-            if (parse_fence_move(cmd, &m) && parse_direction(dir, &d)) {
-                move_fence(desk->focus, d, m);
-                apply_layout(desk, desk->focus, root_rect);
+        num--, args++;
+    }
+
+    if (d != 1 || t > 1)
+        return false;
+
+    if (dom == DOMAIN_HISTORY)
+        query_history(trg, rsp);
+    else if (dom == DOMAIN_WINDOW)
+        query_windows(trg, rsp);
+    else
+        query_monitors(trg, dom, rsp);
+
+    return true;
+}
+
+bool cmd_rule(char **args, int num, char *rsp) {
+    while (num > 0) {
+        if (streq("-a", *args) || streq("--add", *args)) {
+            num--, args++;
+            if (num < 2)
+                return false;
+            rule_t *rule = make_rule();
+            strncpy(rule->cause.name, *args, sizeof(rule->cause.name));
+            num--, args++;
+            while (num > 0) {
+                if (streq("--floating", *args)) {
+                    rule->effect.floating = true;
+                } else if (streq("--follow", *args)) {
+                    rule->effect.follow = true;
+                } else if (streq("-d", *args) || streq("--desktop", *args)) {
+                    num--, args++;
+                    if (num < 1) {
+                        free(rule);
+                        return false;
+                    }
+                    strncpy(rule->effect.desc, *args, sizeof(rule->effect.desc));
+                } else {
+                    free(rule);
+                    return false;
+                }
+                num--, args++;
+            }
+            add_rule(rule);
+        } else if (streq("-r", *args) || streq("--rm", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            unsigned int uid;
+            while (num > 0) {
+                if (sscanf(*args, "%X", &uid) == 1)
+                    remove_rule_by_uid(uid);
+                else
+                    return false;
+                num--, args++;
             }
+        } else if (streq("-l", *args) || streq("--list", *args)) {
+            num--, args++;
+            list_rules(num > 0 ? *args : NULL, rsp);
+        } else {
+            return false;
         }
-    } else if (strcmp(cmd, "send_to") == 0) {
-        char *name = strtok(NULL, TOKEN_SEP);
-        if (name != NULL) {
-            desktop_t *d = find_desktop(name);
-            transfer_node(desk, d, desk->focus);
+        num--, args++;
+    }
+
+    return true;
+}
+
+bool cmd_pointer(char **args, int num) {
+    while (num > 0) {
+        if (streq("-t", *args) || streq("--track", *args)) {
+            num--, args++;
+            if (num < 2)
+                return false;
+            int x, y;
+            if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
+                track_pointer(x, y);
+            else
+                return false;
+        } else if (streq("-g", *args) || streq("--grab", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            pointer_action_t pac;
+            if (parse_pointer_action(*args, &pac))
+                grab_pointer(pac);
+            else
+                return false;
+        } else {
+            return false;
         }
-    } else if (strcmp(cmd, "use") == 0) {
-        char *name = strtok(NULL, TOKEN_SEP);
-        if (name != NULL) {
-            desktop_t *d = find_desktop(name);
-            select_desktop(d);
+        num--, args++;
+    }
+
+    return true;
+}
+
+bool cmd_restore(char **args, int num) {
+    while (num > 0) {
+        if (streq("-T", *args) || streq("--tree", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            restore_tree(*args);
+        } else if (streq("-H", *args) || streq("--history", *args)) {
+            num--, args++;
+            if (num < 1)
+                return false;
+            restore_history(*args);
+        } else {
+            return false;
         }
-    } else if (strcmp(cmd, "cycle") == 0) {
-        char *dir = strtok(NULL, TOKEN_SEP);
-        if (dir != NULL) {
-            cycle_dir_t d;
-            if (parse_cycle_direction(dir, &d)) {
-                skip_client_t k;
-                char *skip = strtok(NULL, TOKEN_SEP);
-                if (parse_skip_client(skip, &k))
-                    cycle_leaf(desk, desk->focus, d, k);
-            }
+        num--, args++;
+    }
+
+    return true;
+}
+
+bool cmd_control(char **args, int num) {
+    while (num > 0) {
+        if (streq("--adopt-orphans", *args)) {
+            adopt_orphans();
+        } else if (streq("--put-status", *args)) {
+            put_status();
+        } else if (streq("--toggle-visibility", *args)) {
+            toggle_visibility();
+        } else {
+            return false;
         }
-    } else if (strcmp(cmd, "alternate") == 0) {
-        alternate_desktop();
-    } else if (strcmp(cmd, "add") == 0) {
-        char *name = strtok(NULL, TOKEN_SEP);
-        if (name != NULL) {
-            add_desktop(name);
+        num--, args++;
+    }
+
+    return true;
+}
+
+bool cmd_config(char **args, int num, char *rsp) {
+    if (num == 2)
+        return set_setting(*args, *(args + 1));
+    else if (num == 1)
+        return get_setting(*args, rsp);
+    else
+        return false;
+}
+
+bool cmd_quit(char **args, int num) {
+    if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
+        return false;
+    quit();
+    return true;
+}
+
+bool handle_message(char *msg, int msg_len, char *rsp)
+{
+    int cap = INIT_CAP;
+    int num = 0;
+    char **args = malloc(cap * sizeof(char *));
+    if (args == NULL)
+        return false;
+
+    for (int i = 0, j = 0; i < msg_len; i++) {
+        if (msg[i] == 0) {
+            args[num++] = msg + j;
+            j = i + 1;
         }
-    } else if (strcmp(cmd, "focus") == 0) {
-        char *dir = strtok(NULL, TOKEN_SEP);
-        if (dir != NULL) {
-            direction_t d;
-            if (parse_direction(dir, &d)) {
-                node_t *n = find_neighbor(desk->focus, d);
-                if (n != NULL)
-                    desk->focus = n;
-                /* focus_node(desk, n); */
+        if (num >= cap) {
+            cap *= 2;
+            char **new = realloc(args, cap * sizeof(char *));
+            if (new == NULL) {
+                free(args);
+                return false;
+            } else {
+                args = new;
             }
         }
-    } else if (strcmp(cmd, "quit") == 0) {
-        quit();
-    } else {
-        sprintf(rsp, "unknown command: %s\n", cmd);
     }
+
+    if (num < 1)
+        return false;
+
+    char **args_orig = args;
+    bool ret = process_message(args, num, rsp);
+    free(args_orig);
+    return ret;
 }
 
-void get_setting(char *name, char* rsp)
+bool process_message(char **args, int num, char *rsp)
 {
-    if (name == NULL)
-        return;
-
-    if (strcmp(name, "inner_border_width") == 0)
-        sprintf(rsp, "%u\n", inner_border_width);
-    else if (strcmp(name, "main_border_width") == 0)
-        sprintf(rsp, "%u\n", main_border_width);
-    else if (strcmp(name, "outer_border_width") == 0)
-        sprintf(rsp, "%u\n", outer_border_width);
-    else if (strcmp(name, "border_width") == 0)
-        sprintf(rsp, "%u\n", border_width);
-    else if (strcmp(name, "window_gap") == 0)
-        sprintf(rsp, "%i\n", window_gap);
-    else if (strcmp(name, "left_padding") == 0)
-        sprintf(rsp, "%i\n", left_padding);
-    else if (strcmp(name, "right_padding") == 0)
-        sprintf(rsp, "%i\n", right_padding);
-    else if (strcmp(name, "top_padding") == 0)
-        sprintf(rsp, "%i\n", top_padding);
-    else if (strcmp(name, "bottom_padding") == 0)
-        sprintf(rsp, "%i\n", bottom_padding);
-    else if (strcmp(name, "normal_border_color") == 0)
-        sprintf(rsp, "%s (0x%06X)\n", normal_border_color, normal_border_color_pxl);
-    else if (strcmp(name, "active_border_color") == 0)
-        sprintf(rsp, "%s (0x%06X)\n", active_border_color, active_border_color_pxl);
-    else if (strcmp(name, "inner_border_color") == 0)
-        sprintf(rsp, "%s (0x%06X)\n", inner_border_color, inner_border_color_pxl);
-    else if (strcmp(name, "outer_border_color") == 0)
-        sprintf(rsp, "%s (0x%06X)\n", outer_border_color, outer_border_color_pxl);
-    else if (strcmp(name, "presel_border_color") == 0)
-        sprintf(rsp, "%s (0x%06X)\n", presel_border_color, presel_border_color_pxl);
-    else if (strcmp(name, "locked_border_color") == 0)
-        sprintf(rsp, "%s (0x%06X)\n", locked_border_color, locked_border_color_pxl);
-    else if (strcmp(name, "wm_name") == 0)
-        sprintf(rsp, "%s\n", wm_name);
-    else if (strcmp(name, "adaptive_window_border") == 0)
-        sprintf(rsp, "%s\n", BOOLSTR(adaptive_window_border));
-    else if (strcmp(name, "adaptive_window_gap") == 0)
-        sprintf(rsp, "%s\n", BOOLSTR(adaptive_window_gap));
+    if (streq("window", *args)) {
+        return cmd_window(++args, --num);
+    } else if (streq("desktop", *args)) {
+        return cmd_desktop(++args, --num);
+    } else if (streq("monitor", *args)) {
+        return cmd_monitor(++args, --num);
+    } else if (streq("query", *args)) {
+        return cmd_query(++args, --num, rsp);
+    } else if (streq("restore", *args)) {
+        return cmd_restore(++args, --num);
+    } else if (streq("control", *args)) {
+        return cmd_control(++args, --num);
+    } else if (streq("rule", *args)) {
+        return cmd_rule(++args, --num, rsp);
+    } else if (streq("pointer", *args)) {
+        return cmd_pointer(++args, --num);
+    } else if (streq("config", *args)) {
+        return cmd_config(++args, --num, rsp);
+    } else if (streq("quit", *args)) {
+        return cmd_quit(++args, --num);
+    }
+
+    return false;
 }
 
-void set_setting(char *name, char *value)
+bool set_setting(char *name, char *value)
 {
-    if (name == NULL || value == NULL)
-        return;
-
-    if (strcmp(name, "inner_border_width") == 0) {
-        sscanf(value, "%u", &inner_border_width);
-        border_width = inner_border_width + main_border_width + outer_border_width;
-    } else if (strcmp(name, "main_border_width") == 0) {
-        sscanf(value, "%u", &main_border_width);
-        border_width = inner_border_width + main_border_width + outer_border_width;
-    } else if (strcmp(name, "outer_border_width") == 0) {
-        sscanf(value, "%u", &outer_border_width);
-        border_width = inner_border_width + main_border_width + outer_border_width;
-    } else if (strcmp(name, "window_gap") == 0) {
-        sscanf(value, "%i", &window_gap);
-    } else if (strcmp(name, "left_padding") == 0) {
-        sscanf(value, "%i", &left_padding);
-        update_root_dimensions();
-    } else if (strcmp(name, "right_padding") == 0) {
-        sscanf(value, "%i", &right_padding);
-        update_root_dimensions();
-    } else if (strcmp(name, "top_padding") == 0) {
-        sscanf(value, "%i", &top_padding);
-        update_root_dimensions();
-    } else if (strcmp(name, "bottom_padding") == 0) {
-        sscanf(value, "%i", &bottom_padding);
-        update_root_dimensions();
-    } else if (strcmp(name, "normal_border_color") == 0) {
-        strcpy(normal_border_color, value);
-        normal_border_color_pxl = get_color(normal_border_color);
-    } else if (strcmp(name, "active_border_color") == 0) {
-        strcpy(active_border_color, value);
-        active_border_color_pxl = get_color(active_border_color);
-    } else if (strcmp(name, "inner_border_color") == 0) {
-        strcpy(inner_border_color, value);
-        inner_border_color_pxl = get_color(inner_border_color);
-    } else if (strcmp(name, "outer_border_color") == 0) {
-        strcpy(outer_border_color, value);
-        outer_border_color_pxl = get_color(outer_border_color);
-    } else if (strcmp(name, "presel_border_color") == 0) {
-        strcpy(presel_border_color, value);
-        presel_border_color_pxl = get_color(presel_border_color);
-    } else if (strcmp(name, "locked_border_color") == 0) {
-        strcpy(locked_border_color, value);
-        locked_border_color_pxl = get_color(locked_border_color);
-    } else if (strcmp(name, "adaptive_window_border") == 0) {
+    if (streq("border_width", name)) {
+        if (sscanf(value, "%u", &border_width) != 1)
+            return false;
+    } else if (streq("window_gap", name)) {
+        if (sscanf(value, "%i", &window_gap) != 1)
+            return false;
+    } else if (streq("split_ratio", name)) {
+        if (sscanf(value, "%lf", &split_ratio) != 1)
+            return false;
+    } else if (streq("left_padding", name)) {
+        if (sscanf(value, "%i", &mon->left_padding) != 1)
+            return false;
+    } else if (streq("right_padding", name)) {
+        if (sscanf(value, "%i", &mon->right_padding) != 1)
+            return false;
+    } else if (streq("top_padding", name)) {
+        if (sscanf(value, "%i", &mon->top_padding) != 1)
+            return false;
+    } else if (streq("bottom_padding", name)) {
+        if (sscanf(value, "%i", &mon->bottom_padding) != 1)
+            return false;
+#define SETCOLOR(s) \
+    } else if (streq(#s, name)) { \
+        if (get_color(value, &s ## _pxl)) \
+            strncpy(s, value, sizeof(s)); \
+        else \
+            return false;
+    SETCOLOR(focused_border_color)
+    SETCOLOR(active_border_color)
+    SETCOLOR(normal_border_color)
+    SETCOLOR(presel_border_color)
+    SETCOLOR(focused_locked_border_color)
+    SETCOLOR(active_locked_border_color)
+    SETCOLOR(normal_locked_border_color)
+    SETCOLOR(urgent_border_color)
+#undef SETCOLOR
+    } else if (streq("focus_follows_pointer", name)) {
         bool b;
-        if (parse_bool(value, &b))
-            adaptive_window_border = b;
-    } else if (strcmp(name, "adaptive_window_gap") == 0) {
-        bool b;
-        if (parse_bool(value, &b))
-            adaptive_window_gap = b;
-    } else if (strcmp(name, "wm_name") == 0) {
-        strcpy(wm_name, value);
-        return;
+        if (parse_bool(value, &b) && b != focus_follows_pointer) {
+            uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)};
+            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))
+                        xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
+            if (focus_follows_pointer)
+                disable_motion_recorder();
+            else
+                enable_motion_recorder();
+            focus_follows_pointer = b;
+            return true;
+        } else {
+            return false;
+        }
+#define SETBOOL(s) \
+    } else if (streq(#s, name)) { \
+        if (!parse_bool(value, &s)) \
+            return false;
+        SETBOOL(borderless_monocle)
+        SETBOOL(gapless_monocle)
+        SETBOOL(pointer_follows_monitor)
+        SETBOOL(monitor_focus_fallback)
+        SETBOOL(adaptative_raise)
+        SETBOOL(apply_shadow_property)
+        SETBOOL(auto_alternate)
+        SETBOOL(auto_cancel)
+        SETBOOL(focus_by_distance)
+        SETBOOL(history_aware_focus)
+#undef SETBOOL
+    } else if (streq("wm_name", name)) {
+        strncpy(wm_name, value, sizeof(wm_name));
+        ewmh_update_wm_name();
+        return true;
+    } else {
+        return false;
     }
 
-    apply_layout(desk, desk->root, root_rect);
+    for (monitor_t *m = mon_head; m != NULL; m = m->next)
+        for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
+            arrange(m, d);
+
+    return true;
+}
+
+bool get_setting(char *name, char* rsp)
+{
+    if (streq("border_width", name))
+        snprintf(rsp, BUFSIZ, "%u", border_width);
+    else if (streq("split_ratio", name))
+        snprintf(rsp, BUFSIZ, "%lf", split_ratio);
+    else if (streq("window_gap", name))
+        snprintf(rsp, BUFSIZ, "%i", window_gap);
+    else if (streq("left_padding", name))
+        snprintf(rsp, BUFSIZ, "%i", mon->left_padding);
+    else if (streq("right_padding", name))
+        snprintf(rsp, BUFSIZ, "%i", mon->right_padding);
+    else if (streq("top_padding", name))
+        snprintf(rsp, BUFSIZ, "%i", mon->top_padding);
+    else if (streq("bottom_padding", name))
+        snprintf(rsp, BUFSIZ, "%i", mon->bottom_padding);
+#define GETCOLOR(s) \
+    else if (streq(#s, name)) \
+        snprintf(rsp, BUFSIZ, "%s (%06X)", s, s##_pxl);
+    GETCOLOR(focused_border_color)
+    GETCOLOR(active_border_color)
+    GETCOLOR(normal_border_color)
+    GETCOLOR(presel_border_color)
+    GETCOLOR(focused_locked_border_color)
+    GETCOLOR(active_locked_border_color)
+    GETCOLOR(normal_locked_border_color)
+    GETCOLOR(urgent_border_color)
+#undef GETCOLOR
+#define GETBOOL(s) \
+    else if (streq(#s, name)) \
+        snprintf(rsp, BUFSIZ, "%s", BOOLSTR(s));
+    GETBOOL(borderless_monocle)
+    GETBOOL(gapless_monocle)
+    GETBOOL(focus_follows_pointer)
+    GETBOOL(pointer_follows_monitor)
+    GETBOOL(monitor_focus_fallback)
+    GETBOOL(adaptative_raise)
+    GETBOOL(apply_shadow_property)
+    GETBOOL(auto_alternate)
+    GETBOOL(auto_cancel)
+    GETBOOL(focus_by_distance)
+    GETBOOL(history_aware_focus)
+#undef GETBOOL
+    else if (streq("wm_name", name))
+        snprintf(rsp, BUFSIZ, "%s", wm_name);
+    else
+        return false;
+    return true;
 }
 
 bool parse_bool(char *value, bool *b)
 {
-    if (strcmp(value, "true") == 0) {
+    if (streq("true", value) || streq("on", value)) {
         *b = true;
         return true;
-    } else if (strcmp(value, "false") == 0) {
+    } else if (streq("false", value) || streq("off", value)) {
         *b = false;
         return true;
     }
@@ -244,10 +778,10 @@ bool parse_bool(char *value, bool *b)
 
 bool parse_layout(char *s, layout_t *l)
 {
-    if (strcmp(s, "monocle") == 0) {
+    if (streq("monocle", s)) {
         *l = LAYOUT_MONOCLE;
         return true;
-    } else if (strcmp(s, "tiled") == 0) {
+    } else if (streq("tiled", s)) {
         *l = LAYOUT_TILED;
         return true;
     }
@@ -256,17 +790,17 @@ bool parse_layout(char *s, layout_t *l)
 
 bool parse_direction(char *s, direction_t *d)
 {
-    if (strcmp(s, "up") == 0) {
-        *d = DIR_UP;
+    if (streq("right", s)) {
+        *d = DIR_RIGHT;
         return true;
-    } else if (strcmp(s, "down") == 0) {
+    } else if (streq("down", s)) {
         *d = DIR_DOWN;
         return true;
-    } else if (strcmp(s, "left") == 0) {
+    } else if (streq("left", s)) {
         *d = DIR_LEFT;
         return true;
-    } else if (strcmp(s, "right") == 0) {
-        *d = DIR_RIGHT;
+    } else if (streq("up", s)) {
+        *d = DIR_UP;
         return true;
     }
     return false;
@@ -274,41 +808,35 @@ bool parse_direction(char *s, direction_t *d)
 
 bool parse_cycle_direction(char *s, cycle_dir_t *d)
 {
-    if (strcmp(s, "prev") == 0) {
-        *d = DIR_PREV;
+    if (streq("next", s)) {
+        *d = CYCLE_NEXT;
         return true;
-    } else if (strcmp(s, "next") == 0) {
-        *d = DIR_NEXT;
+    } else if (streq("prev", s)) {
+        *d = CYCLE_PREV;
         return true;
     }
     return false;
 }
 
-bool parse_skip_client(char *s, skip_client_t *k)
+bool parse_circulate_direction(char *s, circulate_dir_t *d)
 {
-    if (s == NULL || strcmp(s, "--skip-none") == 0) {
-        *k = SKIP_NONE;
-        return true;
-    } else if (strcmp(s, "--skip-floating") == 0) {
-        *k = SKIP_FLOATING;
+    if (streq("forward", s)) {
+        *d = CIRCULATE_FORWARD;
         return true;
-    } else if (strcmp(s, "--skip-tiled") == 0) {
-        *k = SKIP_TILED;
+    } else if (streq("backward", s)) {
+        *d = CIRCULATE_BACKWARD;
         return true;
     }
     return false;
 }
 
-bool parse_rotate(char *s, rotate_t *r)
+bool parse_flip(char *s, flip_t *f)
 {
-    if (strcmp(s, "clockwise") == 0) {
-        *r = ROTATE_CLOCKWISE;
+    if (streq("horizontal", s)) {
+        *f = FLIP_HORIZONTAL;
         return true;
-    } else if (strcmp(s, "counter_cw") == 0) {
-        *r = ROTATE_COUNTER_CLOCKWISE;
-        return true;
-    } else if (strcmp(s, "full_cycle") == 0) {
-        *r = ROTATE_FULL_CYCLE;
+    } else if (streq("vertical", s)) {
+        *f = FLIP_VERTICAL;
         return true;
     }
     return false;
@@ -316,41 +844,42 @@ bool parse_rotate(char *s, rotate_t *r)
 
 bool parse_fence_move(char *s, fence_move_t *m)
 {
-    if (strcmp(s, "push") == 0) {
+    if (streq("push", s)) {
         *m = MOVE_PUSH;
         return true;
-    } else if (strcmp(s, "pull") == 0) {
+    } else if (streq("pull", s)) {
         *m = MOVE_PULL;
         return true;
     }
     return false;
 }
 
-desktop_t *find_desktop(char *name)
+bool parse_pointer_action(char *s, pointer_action_t *a)
 {
-    desktop_t *d = desk_head;
-    while (d != NULL) {
-        if (strcmp(d->name, name) == 0)
-            return d;
-        d = d->next;
+    if (streq("move", s)) {
+        *a = ACTION_MOVE;
+        return true;
+    } else if (streq("resize_corner", s)) {
+        *a = ACTION_RESIZE_CORNER;
+        return true;
+    } else if (streq("resize_side", s)) {
+        *a = ACTION_RESIZE_SIDE;
+        return true;
+    } else if (streq("focus", s)) {
+        *a = ACTION_FOCUS;
+        return true;
     }
-    return NULL;
-}
-
-void add_desktop(char *name)
-{
-    desktop_t *d = make_desktop(name);
-    desk_tail->next = d;
-    d->prev = desk_tail;
-    desk_tail = d;
+    return false;
 }
 
-void alternate_desktop(void)
+bool parse_window_id(char *s, long int *i)
 {
-    if (last_desk == NULL)
-        return;
-    desktop_t *tmp = desk;
-    desk = last_desk;
-    last_desk = tmp;
-    select_desktop(desk);
+    char *end;
+    errno = 0;
+    long int ret = strtol(s, &end, 0);
+    if (errno != 0 || *end != '\0')
+        return false;
+    else
+        *i = ret;
+    return true;
 }