+ 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;
+ }
+ 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;
+ }
+ 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;
+ }
+ 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;
+ }
+ 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;
+ }
+ if (num >= cap) {
+ cap *= 2;
+ char **new = realloc(args, cap * sizeof(char *));
+ if (new == NULL) {
+ free(args);
+ return false;
+ } else {
+ args = new;
+ }
+ }
+ }
+
+ if (num < 1)
+ return false;
+
+ char **args_orig = args;
+ bool ret = process_message(args, num, rsp);
+ free(args_orig);
+ return ret;
+}
+
+bool process_message(char **args, int num, char *rsp)
+{
+ 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;
+}
+
+bool set_setting(char *name, char *value)
+{
+ 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) && 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;
+ }
+
+ 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 (streq("true", value) || streq("on", value)) {
+ *b = true;
+ return true;
+ } else if (streq("false", value) || streq("off", value)) {
+ *b = false;
+ return true;
+ }
+ return false;
+}
+
+bool parse_layout(char *s, layout_t *l)
+{
+ if (streq("monocle", s)) {
+ *l = LAYOUT_MONOCLE;
+ return true;
+ } else if (streq("tiled", s)) {
+ *l = LAYOUT_TILED;
+ return true;
+ }
+ return false;
+}
+
+bool parse_direction(char *s, direction_t *d)
+{
+ if (streq("right", s)) {
+ *d = DIR_RIGHT;
+ return true;
+ } else if (streq("down", s)) {
+ *d = DIR_DOWN;
+ return true;
+ } else if (streq("left", s)) {
+ *d = DIR_LEFT;
+ return true;
+ } else if (streq("up", s)) {
+ *d = DIR_UP;
+ return true;
+ }
+ return false;
+}
+
+bool parse_cycle_direction(char *s, cycle_dir_t *d)
+{
+ if (streq("next", s)) {
+ *d = CYCLE_NEXT;
+ return true;
+ } else if (streq("prev", s)) {
+ *d = CYCLE_PREV;
+ return true;
+ }
+ return false;
+}
+
+bool parse_circulate_direction(char *s, circulate_dir_t *d)
+{
+ if (streq("forward", s)) {
+ *d = CIRCULATE_FORWARD;
+ return true;
+ } else if (streq("backward", s)) {
+ *d = CIRCULATE_BACKWARD;
+ return true;
+ }
+ return false;
+}
+
+bool parse_flip(char *s, flip_t *f)
+{
+ if (streq("horizontal", s)) {
+ *f = FLIP_HORIZONTAL;
+ return true;
+ } else if (streq("vertical", s)) {
+ *f = FLIP_VERTICAL;
+ return true;
+ }
+ return false;
+}
+
+bool parse_fence_move(char *s, fence_move_t *m)
+{
+ if (streq("push", s)) {
+ *m = MOVE_PUSH;
+ return true;
+ } else if (streq("pull", s)) {
+ *m = MOVE_PULL;
+ return true;
+ }
+ return false;
+}
+
+bool parse_pointer_action(char *s, pointer_action_t *a)
+{
+ 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 false;
+}
+
+bool parse_window_id(char *s, long int *i)
+{
+ char *end;
+ errno = 0;
+ long int ret = strtol(s, &end, 0);
+ if (errno != 0 || *end != '\0')
+ return false;
+ else
+ *i = ret;
+ return true;