19 bool handle_message(char *msg, int msg_len, char *rsp)
23 char **args = malloc(cap * sizeof(char *));
27 for (int i = 0, j = 0; i < msg_len; i++) {
29 args[num++] = msg + j;
34 char **new = realloc(args, cap * sizeof(char *));
49 char **args_orig = args;
50 bool ret = process_message(args, num, rsp);
55 bool process_message(char **args, int num, char *rsp)
57 if (streq("window", *args)) {
58 return cmd_window(++args, --num);
59 } else if (streq("desktop", *args)) {
60 return cmd_desktop(++args, --num);
61 } else if (streq("monitor", *args)) {
62 return cmd_monitor(++args, --num);
63 } else if (streq("query", *args)) {
64 return cmd_query(++args, --num, rsp);
65 } else if (streq("restore", *args)) {
66 return cmd_restore(++args, --num);
67 } else if (streq("control", *args)) {
68 return cmd_control(++args, --num);
69 } else if (streq("rule", *args)) {
70 return cmd_rule(++args, --num, rsp);
71 } else if (streq("pointer", *args)) {
72 return cmd_pointer(++args, --num);
73 } else if (streq("config", *args)) {
74 return cmd_config(++args, --num, rsp);
75 } else if (streq("quit", *args)) {
76 return cmd_quit(++args, --num);
82 bool cmd_window(char **args, int num)
87 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
88 coordinates_t trg = ref;
90 if (*args[0] != OPT_CHR) {
91 if (node_from_desc(*args, &ref, &trg))
103 if (streq("-f", *args) || streq("--focus", *args)) {
104 coordinates_t dst = trg;
105 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
107 if (!node_from_desc(*args, &trg, &dst))
110 focus_node(dst.monitor, dst.desktop, dst.node);
111 } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
114 if (desktop_from_desc(*args, &trg, &dst)) {
115 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.desktop->focus)) {
116 trg.monitor = dst.monitor;
117 trg.desktop = dst.desktop;
122 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
127 if (monitor_from_desc(*args, &trg, &dst)) {
128 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus)) {
129 trg.monitor = dst.monitor;
130 trg.desktop = dst.monitor->desk;
135 } else if (streq("-w", *args) || streq("--to-window", *args)) {
140 if (node_from_desc(*args, &trg, &dst)) {
141 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) {
142 trg.monitor = dst.monitor;
143 trg.desktop = dst.desktop;
148 } else if (streq("-s", *args) || streq("--swap", *args)) {
153 if (node_from_desc(*args, &trg, &dst)) {
154 if (swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) {
155 if (trg.desktop != dst.desktop)
156 arrange(trg.monitor, trg.desktop);
157 trg.monitor = dst.monitor;
158 trg.desktop = dst.desktop;
164 } else if (streq("-t", *args) || streq("--toggle", *args)) {
168 char *key = strtok(*args, EQL_TOK);
169 char *val = strtok(NULL, EQL_TOK);
170 state_alter_t a = ALTER_NONE;
175 if (parse_bool(val, &b))
180 if (streq("fullscreen", key)) {
181 set_fullscreen(trg.node, (a == ALTER_SET ? b : !trg.node->client->fullscreen));
183 } else if (streq("floating", key)) {
184 set_floating(trg.node, (a == ALTER_SET ? b : !trg.node->client->floating));
186 } else if (streq("locked", key)) {
187 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->locked));
188 } else if (streq("sticky", key)) {
189 set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->sticky));
191 } else if (streq("-p", *args) || streq("--presel", *args)) {
193 if (num < 1 || !is_tiled(trg.node->client)
194 || trg.desktop->layout != LAYOUT_TILED)
196 if (streq("cancel", *args)) {
200 if (parse_direction(*args, &dir)) {
201 double rat = trg.node->split_ratio;
202 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
204 if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1)
207 if (auto_cancel && trg.node->split_mode == MODE_MANUAL
208 && dir == trg.node->split_dir
209 && rat == trg.node->split_ratio) {
212 trg.node->split_mode = MODE_MANUAL;
213 trg.node->split_dir = dir;
214 trg.node->split_ratio = rat;
216 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
221 } else if (streq("-e", *args) || streq("--edge", *args)) {
226 if (!parse_direction(*args, &dir))
228 node_t *n = find_fence(trg.node, dir);
233 if (parse_fence_move(*args, &fmo)) {
234 move_fence(n, dir, fmo);
237 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1)
238 n->split_ratio = rat;
243 } else if (streq("-r", *args) || streq("--ratio", *args)) {
248 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
249 trg.node->split_ratio = rat;
250 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
254 } else if (streq("-R", *args) || streq("--rotate", *args)) {
259 if (!parse_direction(*args, &dir))
261 node_t *n = find_fence(trg.node, dir);
266 if (parse_degree(*args, °)) {
272 } else if (streq("-c", *args) || streq("--close", *args)) {
275 window_close(trg.node);
276 } else if (streq("-k", *args) || streq("--kill", *args)) {
279 window_kill(trg.desktop, trg.node);
288 arrange(trg.monitor, trg.desktop);
293 bool cmd_desktop(char **args, int num)
298 coordinates_t ref = {mon, mon->desk, NULL};
299 coordinates_t trg = ref;
301 if (*args[0] != OPT_CHR) {
302 if (desktop_from_desc(*args, &ref, &trg))
311 if (streq("-f", *args) || streq("--focus", *args)) {
312 coordinates_t dst = trg;
313 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
315 if (!desktop_from_desc(*args, &trg, &dst))
318 if (auto_alternate && dst.desktop == dst.monitor->desk) {
319 desktop_select_t sel;
320 sel.status = DESKTOP_STATUS_ALL;
321 sel.urgency = DESKTOP_URGENCY_ALL;
322 history_last_desktop(dst.desktop, sel, &dst);
324 focus_node(dst.monitor, dst.desktop, dst.desktop->focus);
325 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
327 if (num < 1 || trg.monitor->desk_head == trg.monitor->desk_tail)
330 if (monitor_from_desc(*args, &trg, &dst)) {
331 transfer_desktop(trg.monitor, dst.monitor, trg.desktop);
332 trg.monitor = dst.monitor;
337 } else if (streq("-s", *args) || streq("--swap", *args)) {
342 if (desktop_from_desc(*args, &trg, &dst) && trg.monitor == dst.monitor)
343 swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop);
346 } else if (streq("-l", *args) || streq("--layout", *args)) {
352 if (parse_cycle_direction(*args, &cyc))
353 change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
354 else if (parse_layout(*args, &lyt))
355 change_layout(trg.monitor, trg.desktop, lyt);
358 } else if (streq("-n", *args) || streq("--rename", *args)) {
362 snprintf(trg.desktop->name, sizeof(trg.desktop->name), "%s", *args);
363 ewmh_update_desktop_names();
365 } else if (streq("-r", *args) || streq("--remove", *args)) {
366 if (trg.desktop->root == NULL
367 && trg.monitor->desk_head != trg.monitor->desk_tail) {
368 remove_desktop(trg.monitor, trg.desktop);
369 show_desktop(trg.monitor->desk);
375 } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
377 } else if (streq("-F", *args) || streq("--flip", *args)) {
382 if (parse_flip(*args, &flp)) {
383 flip_tree(trg.desktop->root, flp);
388 } else if (streq("-R", *args) || streq("--rotate", *args)) {
393 if (parse_degree(*args, °)) {
394 rotate_tree(trg.desktop->root, deg);
399 } else if (streq("-B", *args) || streq("--balance", *args)) {
400 balance_tree(trg.desktop->root);
402 } else if (streq("-C", *args) || streq("--circulate", *args)) {
407 if (parse_circulate_direction(*args, &cir)) {
408 circulate_leaves(trg.monitor, trg.desktop, cir);
420 arrange(trg.monitor, trg.desktop);
425 bool cmd_monitor(char **args, int num)
430 coordinates_t ref = {mon, NULL, NULL};
431 coordinates_t trg = ref;
433 if (*args[0] != OPT_CHR) {
434 if (monitor_from_desc(*args, &ref, &trg))
441 if (streq("-f", *args) || streq("--focus", *args)) {
442 coordinates_t dst = trg;
443 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
445 if (!monitor_from_desc(*args, &trg, &dst))
448 if (auto_alternate && dst.monitor == mon) {
449 desktop_select_t sel;
450 sel.status = DESKTOP_STATUS_ALL;
451 sel.urgency = DESKTOP_URGENCY_ALL;
452 history_last_monitor(dst.monitor, sel, &dst);
454 focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
455 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
460 add_desktop(trg.monitor, make_desktop(*args));
463 } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
469 if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail && dst.desktop->root == NULL) {
470 remove_desktop(dst.monitor, dst.desktop);
471 show_desktop(dst.monitor->desk);
475 } else if (streq("-n", *args) || streq("--rename", *args)) {
479 snprintf(trg.monitor->name, sizeof(trg.monitor->name), "%s", *args);
481 } else if (streq("-s", *args) || streq("--swap", *args)) {
486 if (monitor_from_desc(*args, &trg, &dst))
487 swap_monitors(trg.monitor, dst.monitor);
499 bool cmd_query(char **args, int num, char *rsp) {
500 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
501 coordinates_t trg = {NULL, NULL, NULL};
502 domain_t dom = DOMAIN_TREE;
506 if (streq("-T", *args) || streq("--tree", *args)) {
507 dom = DOMAIN_TREE, d++;
508 } else if (streq("-M", *args) || streq("--monitors", *args)) {
509 dom = DOMAIN_MONITOR, d++;
510 } else if (streq("-D", *args) || streq("--desktops", *args)) {
511 dom = DOMAIN_DESKTOP, d++;
512 } else if (streq("-W", *args) || streq("--windows", *args)) {
513 dom = DOMAIN_WINDOW, d++;
514 } else if (streq("-H", *args) || streq("--history", *args)) {
515 dom = DOMAIN_HISTORY, d++;
516 } else if (streq("-S", *args) || streq("--stack", *args)) {
517 dom = DOMAIN_STACK, d++;
518 } else if (streq("-m", *args) || streq("--monitor", *args)) {
519 trg.monitor = ref.monitor;
520 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
522 if (!monitor_from_desc(*args, &ref, &trg))
526 } else if (streq("-d", *args) || streq("--desktop", *args)) {
527 trg.monitor = ref.monitor;
528 trg.desktop = ref.desktop;
529 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
531 if (!desktop_from_desc(*args, &ref, &trg))
535 } else if (streq("-w", *args) || streq("--window", *args)) {
537 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
539 if (!node_from_desc(*args, &ref, &trg))
552 if (dom == DOMAIN_HISTORY)
553 query_history(trg, rsp);
554 else if (dom == DOMAIN_STACK)
556 else if (dom == DOMAIN_WINDOW)
557 query_windows(trg, rsp);
559 query_monitors(trg, dom, rsp);
564 bool cmd_rule(char **args, int num, char *rsp) {
568 if (streq("-a", *args) || streq("--add", *args)) {
572 rule_t *rule = make_rule();
573 snprintf(rule->cause.name, sizeof(rule->cause.name), "%s", *args);
576 if (streq("--floating", *args)) {
577 rule->effect.floating = true;
578 } else if (streq("--fullscreen", *args)) {
579 rule->effect.fullscreen = true;
580 } else if (streq("--locked", *args)) {
581 rule->effect.locked = true;
582 } else if (streq("--sticky", *args)) {
583 rule->effect.sticky = true;
584 } else if (streq("--follow", *args)) {
585 rule->effect.follow = true;
586 } else if (streq("--focus", *args)) {
587 rule->effect.focus = true;
588 } else if (streq("--unmanage", *args)) {
589 rule->effect.unmanage = true;
590 } else if (streq("--one-shot", *args)) {
591 rule->one_shot = true;
592 } else if (streq("-d", *args) || streq("--desktop", *args)) {
599 snprintf(rule->effect.desc, sizeof(rule->effect.desc), "%s", *args);
608 } else if (streq("-r", *args) || streq("--remove", *args)) {
614 if (sscanf(*args, "%X", &uid) == 1)
615 remove_rule_by_uid(uid);
616 else if (streq("tail", *args))
617 remove_rule(rule_tail);
618 else if (streq("head", *args))
619 remove_rule(rule_head);
624 } else if (streq("-l", *args) || streq("--list", *args)) {
626 list_rules(num > 0 ? *args : NULL, rsp);
636 bool cmd_pointer(char **args, int num) {
640 if (streq("-t", *args) || streq("--track", *args)) {
645 if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
649 } else if (streq("-g", *args) || streq("--grab", *args)) {
653 pointer_action_t pac;
654 if (parse_pointer_action(*args, &pac))
667 bool cmd_restore(char **args, int num) {
671 if (streq("-T", *args) || streq("--tree", *args)) {
676 } else if (streq("-H", *args) || streq("--history", *args)) {
680 restore_history(*args);
681 } else if (streq("-S", *args) || streq("--stack", *args)) {
685 restore_stack(*args);
695 bool cmd_control(char **args, int num) {
699 if (streq("--adopt-orphans", *args)) {
701 } else if (streq("--put-status", *args)) {
703 } else if (streq("--toggle-visibility", *args)) {
714 bool cmd_config(char **args, int num, char *rsp) {
717 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
718 coordinates_t trg = {NULL, NULL, NULL};
719 if (*args[0] == OPT_CHR) {
720 if (streq("-d", *args) || streq("--desktop", *args)) {
724 if (!desktop_from_desc(*args, &ref, &trg))
726 } else if (streq("-m", *args) || streq("--monitor", *args)) {
730 if (!monitor_from_desc(*args, &ref, &trg))
738 return set_setting(trg, *args, *(args + 1));
740 return get_setting(trg, *args, rsp);
745 bool cmd_quit(char **args, int num) {
746 if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
752 bool set_setting(coordinates_t loc, char *name, char *value)
754 #define DESKSET(k, v) \
755 if (loc.desktop != NULL) \
756 loc.desktop->k = v; \
757 else if (loc.monitor != NULL) \
758 for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
761 for (monitor_t *m = mon_head; m != NULL; m = m->next) \
762 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) \
764 if (streq("border_width", name)) {
766 if (sscanf(value, "%u", &bw) != 1)
768 DESKSET(border_width, bw)
769 } else if (streq("window_gap", name)) {
771 if (sscanf(value, "%i", &wg) != 1)
773 DESKSET(window_gap, wg)
776 } else if (streq(#k, name)) { \
778 if (sscanf(value, "%i", &v) != 1) \
780 if (loc.monitor != NULL) \
781 loc.monitor->k = v; \
783 for (monitor_t *m = mon_head; m!= NULL; m = m->next) \
786 MONSET(right_padding)
787 MONSET(bottom_padding)
790 } else if (streq("split_ratio", name)) {
792 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1)
797 } else if (streq("growth_factor", name)) {
799 if (sscanf(value, "%lf", &g) == 1 && g > 1)
804 #define SETCOLOR(s) \
805 } else if (streq(#s, name)) { \
806 snprintf(s, sizeof(s), "%s", value);
807 SETCOLOR(focused_border_color)
808 SETCOLOR(active_border_color)
809 SETCOLOR(normal_border_color)
810 SETCOLOR(presel_border_color)
811 SETCOLOR(focused_locked_border_color)
812 SETCOLOR(active_locked_border_color)
813 SETCOLOR(normal_locked_border_color)
814 SETCOLOR(focused_sticky_border_color)
815 SETCOLOR(normal_sticky_border_color)
816 SETCOLOR(urgent_border_color)
818 } else if (streq("focus_follows_pointer", name)) {
820 if (parse_bool(value, &b) && b != focus_follows_pointer) {
821 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)};
822 for (monitor_t *m = mon_head; m != NULL; m = m->next)
823 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
824 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
825 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
826 if (focus_follows_pointer) {
827 for (monitor_t *m = mon_head; m != NULL; m = m->next)
828 window_hide(m->root);
829 disable_motion_recorder();
831 for (monitor_t *m = mon_head; m != NULL; m = m->next)
832 window_show(m->root);
833 enable_motion_recorder();
835 focus_follows_pointer = b;
841 } else if (streq(#s, name)) { \
842 if (!parse_bool(value, &s)) \
844 SETBOOL(borderless_monocle)
845 SETBOOL(gapless_monocle)
846 SETBOOL(pointer_follows_monitor)
847 SETBOOL(apply_floating_atom)
848 SETBOOL(auto_alternate)
850 SETBOOL(history_aware_focus)
851 SETBOOL(honor_ewmh_focus)
857 for (monitor_t *m = mon_head; m != NULL; m = m->next)
858 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
864 bool get_setting(coordinates_t loc, char *name, char* rsp)
866 if (streq("split_ratio", name))
867 snprintf(rsp, BUFSIZ, "%lf", split_ratio);
868 else if (streq("growth_factor", name))
869 snprintf(rsp, BUFSIZ, "%lf", growth_factor);
870 else if (streq("window_gap", name))
871 if (loc.desktop == NULL)
874 snprintf(rsp, BUFSIZ, "%i", loc.desktop->window_gap);
875 else if (streq("border_width", name))
876 if (loc.desktop == NULL)
879 snprintf(rsp, BUFSIZ, "%u", loc.desktop->border_width);
881 else if (streq(#k, name)) \
882 if (loc.monitor == NULL) \
885 snprintf(rsp, BUFSIZ, "%i", loc.monitor->k);
887 MONGET(right_padding)
888 MONGET(bottom_padding)
891 #define GETCOLOR(s) \
892 else if (streq(#s, name)) \
893 snprintf(rsp, BUFSIZ, "%s", s);
894 GETCOLOR(focused_border_color)
895 GETCOLOR(active_border_color)
896 GETCOLOR(normal_border_color)
897 GETCOLOR(presel_border_color)
898 GETCOLOR(focused_locked_border_color)
899 GETCOLOR(active_locked_border_color)
900 GETCOLOR(normal_locked_border_color)
901 GETCOLOR(focused_sticky_border_color)
902 GETCOLOR(normal_sticky_border_color)
903 GETCOLOR(urgent_border_color)
906 else if (streq(#s, name)) \
907 snprintf(rsp, BUFSIZ, "%s", BOOLSTR(s));
908 GETBOOL(borderless_monocle)
909 GETBOOL(gapless_monocle)
910 GETBOOL(focus_follows_pointer)
911 GETBOOL(pointer_follows_monitor)
912 GETBOOL(apply_floating_atom)
913 GETBOOL(auto_alternate)
915 GETBOOL(history_aware_focus)
916 GETBOOL(honor_ewmh_focus)
923 bool parse_bool(char *value, bool *b)
925 if (streq("true", value) || streq("on", value)) {
928 } else if (streq("false", value) || streq("off", value)) {
935 bool parse_layout(char *s, layout_t *l)
937 if (streq("monocle", s)) {
940 } else if (streq("tiled", s)) {
947 bool parse_direction(char *s, direction_t *d)
949 if (streq("right", s)) {
952 } else if (streq("down", s)) {
955 } else if (streq("left", s)) {
958 } else if (streq("up", s)) {
965 bool parse_cycle_direction(char *s, cycle_dir_t *d)
967 if (streq("next", s)) {
970 } else if (streq("prev", s)) {
977 bool parse_circulate_direction(char *s, circulate_dir_t *d)
979 if (streq("forward", s)) {
980 *d = CIRCULATE_FORWARD;
982 } else if (streq("backward", s)) {
983 *d = CIRCULATE_BACKWARD;
989 bool parse_flip(char *s, flip_t *f)
991 if (streq("horizontal", s)) {
992 *f = FLIP_HORIZONTAL;
994 } else if (streq("vertical", s)) {
1001 bool parse_fence_move(char *s, fence_move_t *m)
1003 if (streq("push", s)) {
1006 } else if (streq("pull", s)) {
1013 bool parse_pointer_action(char *s, pointer_action_t *a)
1015 if (streq("move", s)) {
1018 } else if (streq("resize_corner", s)) {
1019 *a = ACTION_RESIZE_CORNER;
1021 } else if (streq("resize_side", s)) {
1022 *a = ACTION_RESIZE_SIDE;
1024 } else if (streq("focus", s)) {
1031 bool parse_degree(char *s, int *d)
1038 if ((i % 90) != 0) {
1046 bool parse_window_id(char *s, long int *i)
1050 long int ret = strtol(s, &end, 0);
1051 if (errno != 0 || *end != '\0')
1058 bool parse_index(char *s, int *i)
1060 return sscanf(s, "^%i", i) == 1;