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 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;
121 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
126 if (monitor_from_desc(*args, &trg, &dst)) {
127 transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
128 trg.monitor = dst.monitor;
129 trg.desktop = dst.monitor->desk;
133 } else if (streq("-w", *args) || streq("--to-window", *args)) {
138 if (node_from_desc(*args, &trg, &dst))
139 transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node);
143 } else if (streq("-s", *args) || streq("--swap", *args)) {
148 if (node_from_desc(*args, &trg, &dst))
149 swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node);
152 if (trg.desktop != dst.desktop)
153 arrange(dst.monitor, dst.desktop);
155 } else if (streq("-t", *args) || streq("--toggle", *args)) {
159 char *key = strtok(*args, EQL_TOK);
160 char *val = strtok(NULL, EQL_TOK);
161 state_alter_t a = ALTER_NONE;
166 if (parse_bool(val, &b))
171 if (streq("fullscreen", key)) {
172 set_fullscreen(trg.node, (a == ALTER_SET ? b : !trg.node->client->fullscreen));
174 } else if (streq("floating", key)) {
175 set_floating(trg.node, (a == ALTER_SET ? b : !trg.node->client->floating));
177 } else if (streq("locked", key)) {
178 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->locked));
179 } else if (streq("sticky", key)) {
180 set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->sticky));
182 } else if (streq("-p", *args) || streq("--presel", *args)) {
184 if (num < 1 || !is_tiled(trg.node->client)
185 || trg.desktop->layout != LAYOUT_TILED)
187 if (streq("cancel", *args)) {
191 if (parse_direction(*args, &dir)) {
192 double rat = trg.node->split_ratio;
193 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
195 if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1)
198 if (auto_cancel && trg.node->split_mode == MODE_MANUAL
199 && dir == trg.node->split_dir
200 && rat == trg.node->split_ratio) {
203 trg.node->split_mode = MODE_MANUAL;
204 trg.node->split_dir = dir;
205 trg.node->split_ratio = rat;
207 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
212 } else if (streq("-e", *args) || streq("--edge", *args)) {
217 if (!parse_direction(*args, &dir))
219 node_t *n = find_fence(trg.node, dir);
224 if (parse_fence_move(*args, &fmo)) {
225 move_fence(n, dir, fmo);
228 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1)
229 n->split_ratio = rat;
234 } else if (streq("-r", *args) || streq("--ratio", *args)) {
239 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
240 trg.node->split_ratio = rat;
241 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
245 } else if (streq("-R", *args) || streq("--rotate", *args)) {
250 if (!parse_direction(*args, &dir))
252 node_t *n = find_fence(trg.node, dir);
257 if (parse_degree(*args, °)) {
263 } else if (streq("-c", *args) || streq("--close", *args)) {
266 window_close(trg.node);
267 } else if (streq("-k", *args) || streq("--kill", *args)) {
270 window_kill(trg.desktop, trg.node);
279 arrange(trg.monitor, trg.desktop);
284 bool cmd_desktop(char **args, int num)
289 coordinates_t ref = {mon, mon->desk, NULL};
290 coordinates_t trg = ref;
292 if (*args[0] != OPT_CHR) {
293 if (desktop_from_desc(*args, &ref, &trg))
302 if (streq("-f", *args) || streq("--focus", *args)) {
303 coordinates_t dst = trg;
304 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
306 if (!desktop_from_desc(*args, &trg, &dst))
309 if (auto_alternate && dst.desktop == dst.monitor->desk) {
310 desktop_select_t sel;
311 sel.status = DESKTOP_STATUS_ALL;
312 sel.urgency = DESKTOP_URGENCY_ALL;
313 history_last_desktop(dst.desktop, sel, &dst);
315 focus_node(dst.monitor, dst.desktop, dst.desktop->focus);
316 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
318 if (num < 1 || trg.monitor->desk_head == trg.monitor->desk_tail)
321 if (monitor_from_desc(*args, &trg, &dst)) {
322 transfer_desktop(trg.monitor, dst.monitor, trg.desktop);
323 trg.monitor = dst.monitor;
328 } else if (streq("-s", *args) || streq("--swap", *args)) {
333 if (desktop_from_desc(*args, &trg, &dst) && trg.monitor == dst.monitor)
334 swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop);
337 } else if (streq("-l", *args) || streq("--layout", *args)) {
343 if (parse_cycle_direction(*args, &cyc))
344 change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
345 else if (parse_layout(*args, &lyt))
346 change_layout(trg.monitor, trg.desktop, lyt);
349 } else if (streq("-n", *args) || streq("--rename", *args)) {
353 snprintf(trg.desktop->name, sizeof(trg.desktop->name), "%s", *args);
354 ewmh_update_desktop_names();
356 } else if (streq("-r", *args) || streq("--remove", *args)) {
357 if (trg.desktop->root == NULL
358 && trg.monitor->desk_head != trg.monitor->desk_tail) {
359 remove_desktop(trg.monitor, trg.desktop);
360 show_desktop(trg.monitor->desk);
366 } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
368 } else if (streq("-F", *args) || streq("--flip", *args)) {
373 if (parse_flip(*args, &flp)) {
374 flip_tree(trg.desktop->root, flp);
379 } else if (streq("-R", *args) || streq("--rotate", *args)) {
384 if (parse_degree(*args, °)) {
385 rotate_tree(trg.desktop->root, deg);
390 } else if (streq("-B", *args) || streq("--balance", *args)) {
391 balance_tree(trg.desktop->root);
393 } else if (streq("-C", *args) || streq("--circulate", *args)) {
398 if (parse_circulate_direction(*args, &cir)) {
399 circulate_leaves(trg.monitor, trg.desktop, cir);
411 arrange(trg.monitor, trg.desktop);
416 bool cmd_monitor(char **args, int num)
421 coordinates_t ref = {mon, NULL, NULL};
422 coordinates_t trg = ref;
424 if (*args[0] != OPT_CHR) {
425 if (monitor_from_desc(*args, &ref, &trg))
432 if (streq("-f", *args) || streq("--focus", *args)) {
433 coordinates_t dst = trg;
434 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
436 if (!monitor_from_desc(*args, &trg, &dst))
439 if (auto_alternate && dst.monitor == mon) {
440 desktop_select_t sel;
441 sel.status = DESKTOP_STATUS_ALL;
442 sel.urgency = DESKTOP_URGENCY_ALL;
443 history_last_monitor(dst.monitor, sel, &dst);
445 focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
446 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
451 add_desktop(trg.monitor, make_desktop(*args));
454 } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
460 if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail && dst.desktop->root == NULL) {
461 remove_desktop(dst.monitor, dst.desktop);
462 show_desktop(dst.monitor->desk);
466 } else if (streq("-n", *args) || streq("--rename", *args)) {
470 snprintf(trg.monitor->name, sizeof(trg.monitor->name), "%s", *args);
472 } else if (streq("-s", *args) || streq("--swap", *args)) {
477 if (monitor_from_desc(*args, &trg, &dst))
478 swap_monitors(trg.monitor, dst.monitor);
490 bool cmd_query(char **args, int num, char *rsp) {
491 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
492 coordinates_t trg = {NULL, NULL, NULL};
493 domain_t dom = DOMAIN_TREE;
497 if (streq("-T", *args) || streq("--tree", *args)) {
498 dom = DOMAIN_TREE, d++;
499 } else if (streq("-M", *args) || streq("--monitors", *args)) {
500 dom = DOMAIN_MONITOR, d++;
501 } else if (streq("-D", *args) || streq("--desktops", *args)) {
502 dom = DOMAIN_DESKTOP, d++;
503 } else if (streq("-W", *args) || streq("--windows", *args)) {
504 dom = DOMAIN_WINDOW, d++;
505 } else if (streq("-H", *args) || streq("--history", *args)) {
506 dom = DOMAIN_HISTORY, d++;
507 } else if (streq("-S", *args) || streq("--stack", *args)) {
508 dom = DOMAIN_STACK, d++;
509 } else if (streq("-m", *args) || streq("--monitor", *args)) {
510 trg.monitor = ref.monitor;
511 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
513 if (!monitor_from_desc(*args, &ref, &trg))
517 } else if (streq("-d", *args) || streq("--desktop", *args)) {
518 trg.monitor = ref.monitor;
519 trg.desktop = ref.desktop;
520 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
522 if (!desktop_from_desc(*args, &ref, &trg))
526 } else if (streq("-w", *args) || streq("--window", *args)) {
528 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
530 if (!node_from_desc(*args, &ref, &trg))
543 if (dom == DOMAIN_HISTORY)
544 query_history(trg, rsp);
545 else if (dom == DOMAIN_STACK)
547 else if (dom == DOMAIN_WINDOW)
548 query_windows(trg, rsp);
550 query_monitors(trg, dom, rsp);
555 bool cmd_rule(char **args, int num, char *rsp) {
559 if (streq("-a", *args) || streq("--add", *args)) {
563 rule_t *rule = make_rule();
564 snprintf(rule->cause.name, sizeof(rule->cause.name), "%s", *args);
567 if (streq("--floating", *args)) {
568 rule->effect.floating = true;
569 } else if (streq("--fullscreen", *args)) {
570 rule->effect.fullscreen = true;
571 } else if (streq("--locked", *args)) {
572 rule->effect.locked = true;
573 } else if (streq("--sticky", *args)) {
574 rule->effect.sticky = true;
575 } else if (streq("--follow", *args)) {
576 rule->effect.follow = true;
577 } else if (streq("--focus", *args)) {
578 rule->effect.focus = true;
579 } else if (streq("--unmanage", *args)) {
580 rule->effect.unmanage = true;
581 } else if (streq("--one-shot", *args)) {
582 rule->one_shot = true;
583 } else if (streq("-d", *args) || streq("--desktop", *args)) {
590 snprintf(rule->effect.desc, sizeof(rule->effect.desc), "%s", *args);
599 } else if (streq("-r", *args) || streq("--remove", *args)) {
605 if (sscanf(*args, "%X", &uid) == 1)
606 remove_rule_by_uid(uid);
607 else if (streq("tail", *args))
608 remove_rule(rule_tail);
609 else if (streq("head", *args))
610 remove_rule(rule_head);
615 } else if (streq("-l", *args) || streq("--list", *args)) {
617 list_rules(num > 0 ? *args : NULL, rsp);
627 bool cmd_pointer(char **args, int num) {
631 if (streq("-t", *args) || streq("--track", *args)) {
636 if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
640 } else if (streq("-g", *args) || streq("--grab", *args)) {
644 pointer_action_t pac;
645 if (parse_pointer_action(*args, &pac))
658 bool cmd_restore(char **args, int num) {
662 if (streq("-T", *args) || streq("--tree", *args)) {
667 } else if (streq("-H", *args) || streq("--history", *args)) {
671 restore_history(*args);
672 } else if (streq("-S", *args) || streq("--stack", *args)) {
676 restore_stack(*args);
686 bool cmd_control(char **args, int num) {
690 if (streq("--adopt-orphans", *args)) {
692 } else if (streq("--put-status", *args)) {
694 } else if (streq("--toggle-visibility", *args)) {
705 bool cmd_config(char **args, int num, char *rsp) {
708 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
709 coordinates_t trg = {NULL, NULL, NULL};
710 if (*args[0] == OPT_CHR) {
711 if (streq("-d", *args) || streq("--desktop", *args)) {
715 if (!desktop_from_desc(*args, &ref, &trg))
717 } else if (streq("-m", *args) || streq("--monitor", *args)) {
721 if (!monitor_from_desc(*args, &ref, &trg))
729 return set_setting(trg, *args, *(args + 1));
731 return get_setting(trg, *args, rsp);
736 bool cmd_quit(char **args, int num) {
737 if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
743 bool set_setting(coordinates_t loc, char *name, char *value)
745 #define DESKSET(k, v) \
746 if (loc.desktop != NULL) \
747 loc.desktop->k = v; \
748 else if (loc.monitor != NULL) \
749 for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \
752 for (monitor_t *m = mon_head; m != NULL; m = m->next) \
753 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) \
755 if (streq("border_width", name)) {
757 if (sscanf(value, "%u", &bw) != 1)
759 DESKSET(border_width, bw)
760 } else if (streq("window_gap", name)) {
762 if (sscanf(value, "%i", &wg) != 1)
764 DESKSET(window_gap, wg)
767 } else if (streq(#k, name)) { \
769 if (sscanf(value, "%i", &v) != 1) \
771 if (loc.monitor != NULL) \
772 loc.monitor->k = v; \
774 for (monitor_t *m = mon_head; m!= NULL; m = m->next) \
777 MONSET(right_padding)
778 MONSET(bottom_padding)
781 } else if (streq("split_ratio", name)) {
783 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1)
788 } else if (streq("growth_factor", name)) {
790 if (sscanf(value, "%lf", &g) == 1 && g > 1)
795 #define SETCOLOR(s) \
796 } else if (streq(#s, name)) { \
797 snprintf(s, sizeof(s), "%s", value);
798 SETCOLOR(focused_border_color)
799 SETCOLOR(active_border_color)
800 SETCOLOR(normal_border_color)
801 SETCOLOR(presel_border_color)
802 SETCOLOR(focused_locked_border_color)
803 SETCOLOR(active_locked_border_color)
804 SETCOLOR(normal_locked_border_color)
805 SETCOLOR(focused_sticky_border_color)
806 SETCOLOR(normal_sticky_border_color)
807 SETCOLOR(urgent_border_color)
809 } else if (streq("focus_follows_pointer", name)) {
811 if (parse_bool(value, &b) && b != focus_follows_pointer) {
812 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)};
813 for (monitor_t *m = mon_head; m != NULL; m = m->next)
814 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
815 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
816 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
817 if (focus_follows_pointer) {
818 for (monitor_t *m = mon_head; m != NULL; m = m->next)
819 window_hide(m->root);
820 disable_motion_recorder();
822 for (monitor_t *m = mon_head; m != NULL; m = m->next)
823 window_show(m->root);
824 enable_motion_recorder();
826 focus_follows_pointer = b;
832 } else if (streq(#s, name)) { \
833 if (!parse_bool(value, &s)) \
835 SETBOOL(borderless_monocle)
836 SETBOOL(gapless_monocle)
837 SETBOOL(pointer_follows_monitor)
838 SETBOOL(apply_floating_atom)
839 SETBOOL(auto_alternate)
841 SETBOOL(history_aware_focus)
842 SETBOOL(honor_ewmh_focus)
848 for (monitor_t *m = mon_head; m != NULL; m = m->next)
849 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
855 bool get_setting(coordinates_t loc, char *name, char* rsp)
857 if (streq("split_ratio", name))
858 snprintf(rsp, BUFSIZ, "%lf", split_ratio);
859 else if (streq("growth_factor", name))
860 snprintf(rsp, BUFSIZ, "%lf", growth_factor);
861 else if (streq("window_gap", name))
862 if (loc.desktop == NULL)
865 snprintf(rsp, BUFSIZ, "%i", loc.desktop->window_gap);
866 else if (streq("border_width", name))
867 if (loc.desktop == NULL)
870 snprintf(rsp, BUFSIZ, "%u", loc.desktop->border_width);
872 else if (streq(#k, name)) \
873 if (loc.monitor == NULL) \
876 snprintf(rsp, BUFSIZ, "%i", loc.monitor->k);
878 MONGET(right_padding)
879 MONGET(bottom_padding)
882 #define GETCOLOR(s) \
883 else if (streq(#s, name)) \
884 snprintf(rsp, BUFSIZ, "%s", s);
885 GETCOLOR(focused_border_color)
886 GETCOLOR(active_border_color)
887 GETCOLOR(normal_border_color)
888 GETCOLOR(presel_border_color)
889 GETCOLOR(focused_locked_border_color)
890 GETCOLOR(active_locked_border_color)
891 GETCOLOR(normal_locked_border_color)
892 GETCOLOR(focused_sticky_border_color)
893 GETCOLOR(normal_sticky_border_color)
894 GETCOLOR(urgent_border_color)
897 else if (streq(#s, name)) \
898 snprintf(rsp, BUFSIZ, "%s", BOOLSTR(s));
899 GETBOOL(borderless_monocle)
900 GETBOOL(gapless_monocle)
901 GETBOOL(focus_follows_pointer)
902 GETBOOL(pointer_follows_monitor)
903 GETBOOL(apply_floating_atom)
904 GETBOOL(auto_alternate)
906 GETBOOL(history_aware_focus)
907 GETBOOL(honor_ewmh_focus)
914 bool parse_bool(char *value, bool *b)
916 if (streq("true", value) || streq("on", value)) {
919 } else if (streq("false", value) || streq("off", value)) {
926 bool parse_layout(char *s, layout_t *l)
928 if (streq("monocle", s)) {
931 } else if (streq("tiled", s)) {
938 bool parse_direction(char *s, direction_t *d)
940 if (streq("right", s)) {
943 } else if (streq("down", s)) {
946 } else if (streq("left", s)) {
949 } else if (streq("up", s)) {
956 bool parse_cycle_direction(char *s, cycle_dir_t *d)
958 if (streq("next", s)) {
961 } else if (streq("prev", s)) {
968 bool parse_circulate_direction(char *s, circulate_dir_t *d)
970 if (streq("forward", s)) {
971 *d = CIRCULATE_FORWARD;
973 } else if (streq("backward", s)) {
974 *d = CIRCULATE_BACKWARD;
980 bool parse_flip(char *s, flip_t *f)
982 if (streq("horizontal", s)) {
983 *f = FLIP_HORIZONTAL;
985 } else if (streq("vertical", s)) {
992 bool parse_fence_move(char *s, fence_move_t *m)
994 if (streq("push", s)) {
997 } else if (streq("pull", s)) {
1004 bool parse_pointer_action(char *s, pointer_action_t *a)
1006 if (streq("move", s)) {
1009 } else if (streq("resize_corner", s)) {
1010 *a = ACTION_RESIZE_CORNER;
1012 } else if (streq("resize_side", s)) {
1013 *a = ACTION_RESIZE_SIDE;
1015 } else if (streq("focus", s)) {
1022 bool parse_degree(char *s, int *d)
1029 if ((i % 90) != 0) {
1037 bool parse_window_id(char *s, long int *i)
1041 long int ret = strtol(s, &end, 0);
1042 if (errno != 0 || *end != '\0')
1049 bool parse_index(char *s, int *i)
1051 return sscanf(s, "^%i", i) == 1;