19 bool cmd_window(char **args, int num)
21 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
22 coordinates_t trg = ref;
24 if (*args[0] != OPT_CHR) {
25 if (node_from_desc(*args, &ref, &trg))
37 if (streq("-f", *args) || streq("--focus", *args)) {
38 coordinates_t dst = trg;
39 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
41 if (!node_from_desc(*args, &trg, &dst))
44 focus_node(dst.monitor, dst.desktop, dst.node);
45 } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
48 if (desktop_from_desc(*args, &trg, &dst)) {
49 transfer_node(trg.monitor, trg.desktop, dst.monitor, dst.desktop, trg.node);
50 trg.monitor = dst.monitor;
51 trg.desktop = dst.desktop;
55 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
60 if (monitor_from_desc(*args, &trg, &dst)) {
61 transfer_node(trg.monitor, trg.desktop, dst.monitor, dst.monitor->desk, trg.node);
62 trg.monitor = dst.monitor;
63 trg.desktop = dst.monitor->desk;
67 } else if (streq("-w", *args) || streq("--to-window", *args)) {
72 if (node_from_desc(*args, &trg, &dst))
73 transplant_node(trg.monitor, trg.desktop, trg.node, dst.node);
77 } else if (streq("-s", *args) || streq("--swap", *args)) {
82 if (node_from_desc(*args, &trg, &dst))
83 swap_nodes(trg.node, dst.node, true);
87 } else if (streq("-t", *args) || streq("--toggle", *args)) {
91 char *key = strtok(*args, EQL_TOK);
92 char *val = strtok(NULL, EQL_TOK);
93 state_alter_t a = ALTER_NONE;
98 if (parse_bool(val, &b))
103 if (streq("fullscreen", key)) {
104 set_fullscreen(trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->fullscreen));
106 } else if (streq("floating", key)) {
107 set_floating(trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->floating));
109 } else if (streq("locked", key)) {
110 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->locked));
112 } else if (streq("-p", *args) || streq("--presel", *args)) {
114 if (num < 1 || !is_tiled(trg.node->client)
115 || trg.desktop->layout != LAYOUT_TILED)
117 if (streq("cancel", *args)) {
121 if (parse_direction(*args, &dir)) {
122 double rat = trg.node->split_ratio;
123 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
125 if (sscanf(*args, "%lf", &rat) != 1)
128 if (auto_cancel && trg.node->split_mode == MODE_MANUAL
129 && dir == trg.node->split_dir
130 && rat == trg.node->split_ratio) {
133 trg.node->split_mode = MODE_MANUAL;
134 trg.node->split_dir = dir;
135 trg.node->split_ratio = rat;
137 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
142 } else if (streq("-e", *args) || streq("--edge", *args)) {
147 if (!parse_direction(*args, &dir))
151 if (parse_fence_move(*args, &fmo)) {
152 move_fence(trg.node, dir, fmo);
154 node_t *n = find_fence(trg.node, dir);
155 if (n == NULL || sscanf(*args, "%lf", &n->split_ratio) != 1)
159 } else if (streq("-r", *args) || streq("--ratio", *args)) {
163 if (sscanf(*args, "%lf", &trg.node->split_ratio) == 1)
164 window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor);
167 } else if (streq("-c", *args) || streq("--close", *args)) {
170 window_close(trg.node);
171 } else if (streq("-k", *args) || streq("--kill", *args)) {
174 window_kill(trg.desktop, trg.node);
183 arrange(trg.monitor, trg.desktop);
188 bool cmd_desktop(char **args, int num)
190 coordinates_t ref = {mon, mon->desk, NULL};
191 coordinates_t trg = ref;
193 if (*args[0] != OPT_CHR) {
194 if (desktop_from_desc(*args, &ref, &trg))
203 if (streq("-f", *args) || streq("--focus", *args)) {
204 coordinates_t dst = trg;
205 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
207 if (!desktop_from_desc(*args, &trg, &dst))
210 if (auto_alternate && dst.desktop == dst.monitor->desk && dst.monitor->last_desk != NULL)
211 dst.desktop = dst.monitor->last_desk;
212 focus_node(dst.monitor, dst.desktop, dst.desktop->focus);
213 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
215 if (num < 1 || trg.monitor->desk_head == trg.monitor->desk_tail)
218 if (monitor_from_desc(*args, &trg, &dst)) {
219 transfer_desktop(trg.monitor, dst.monitor, dst.desktop);
220 trg.monitor = dst.monitor;
225 } else if (streq("-l", *args) || streq("--layout", *args)) {
231 if (parse_cycle_direction(*args, &cyc))
232 change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
233 else if (parse_layout(*args, &lyt))
234 change_layout(trg.monitor, trg.desktop, lyt);
237 } else if (streq("-n", *args) || streq("--rename", *args)) {
241 strncpy(trg.desktop->name, *args, sizeof(trg.desktop->name));
242 ewmh_update_desktop_names();
244 } else if (streq("-r", *args) || streq("--rm", *args)) {
245 if (trg.desktop->root == NULL
246 && trg.monitor->desk_head != trg.monitor->desk_tail) {
247 remove_desktop(trg.monitor, trg.desktop);
248 desktop_show(trg.monitor->desk);
254 } else if (streq("-c", *args) || streq("--cancel-presel", *args)) {
256 } else if (streq("-F", *args) || streq("--flip", *args)) {
261 if (parse_flip(*args, &flp)) {
262 flip_tree(trg.desktop->root, flp);
265 } else if (streq("-R", *args) || streq("--rotate", *args)) {
269 int rot = atoi(*args);
274 if ((rot % 90) != 0) {
277 rotate_tree(trg.desktop->root, rot);
280 } else if (streq("-B", *args) || streq("--balance", *args)) {
281 balance_tree(trg.desktop->root);
283 } else if (streq("-C", *args) || streq("--circulate", *args)) {
288 if (parse_circulate_direction(*args, &cir)) {
289 circulate_leaves(trg.monitor, trg.desktop, cir);
301 arrange(trg.monitor, trg.desktop);
306 bool cmd_monitor(char **args, int num)
308 coordinates_t ref = {mon, NULL, NULL};
309 coordinates_t trg = ref;
311 if (*args[0] != OPT_CHR) {
312 if (monitor_from_desc(*args, &ref, &trg))
319 if (streq("-f", *args) || streq("--focus", *args)) {
320 coordinates_t dst = trg;
321 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
323 if (!desktop_from_desc(*args, &trg, &dst))
326 if (auto_alternate && dst.monitor == mon && last_mon != NULL)
327 dst.monitor = last_mon;
328 focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus);
329 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
334 add_desktop(trg.monitor, make_desktop(*args));
337 } else if (streq("-r", *args) || streq("--remove-desktops", *args)) {
343 if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail) {
344 remove_desktop(dst.monitor, dst.desktop);
345 desktop_show(dst.monitor->desk);
349 } else if (streq("-p", *args) || streq("--pad", *args)) {
354 snprintf(values, sizeof(values), "%s %s %s %s", args[0], args[1], args[2], args[3]);
355 if (sscanf(values, "%i %i %i %i", &trg.monitor->top_padding, &trg.monitor->right_padding, &trg.monitor->bottom_padding, &trg.monitor->left_padding) == 4)
356 for (desktop_t *d = trg.monitor->desk_head; d != NULL; d = d->next)
357 arrange(trg.monitor, d);
360 } else if (streq("-n", *args) || streq("--rename", *args)) {
364 strncpy(trg.monitor->name, *args, sizeof(trg.monitor->name));
375 bool cmd_query(char **args, int num, char *rsp) {
376 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
377 coordinates_t trg = {NULL, NULL, NULL};
378 domain_t dom = DOMAIN_TREE;
382 if (streq("-T", *args) || streq("--tree", *args)) {
383 dom = DOMAIN_TREE, d++;
384 } else if (streq("-M", *args) || streq("--monitors", *args)) {
385 dom = DOMAIN_MONITOR, d++;
386 } else if (streq("-D", *args) || streq("--desktops", *args)) {
387 dom = DOMAIN_DESKTOP, d++;
388 } else if (streq("-W", *args) || streq("--windows", *args)) {
389 dom = DOMAIN_WINDOW, d++;
390 } else if (streq("-H", *args) || streq("--history", *args)) {
391 dom = DOMAIN_HISTORY, d++;
392 } else if (streq("-m", *args) || streq("--monitor", *args)) {
393 trg.monitor = ref.monitor;
394 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
396 if (!monitor_from_desc(*args, &ref, &trg))
400 } else if (streq("-d", *args) || streq("--desktop", *args)) {
401 trg.monitor = ref.monitor;
402 trg.desktop = ref.desktop;
403 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
405 if (!desktop_from_desc(*args, &ref, &trg))
409 } else if (streq("-w", *args) || streq("--window", *args)) {
411 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
413 if (!node_from_desc(*args, &ref, &trg))
426 if (dom == DOMAIN_HISTORY)
427 query_history(trg, rsp);
428 else if (dom == DOMAIN_WINDOW)
429 query_windows(trg, rsp);
431 query_monitors(trg, dom, rsp);
436 bool cmd_rule(char **args, int num, char *rsp) {
438 if (streq("-a", *args) || streq("--add", *args)) {
442 rule_t *rule = make_rule();
443 strncpy(rule->cause.name, *args, sizeof(rule->cause.name));
446 if (streq("--floating", *args)) {
447 rule->effect.floating = true;
448 } else if (streq("--follow", *args)) {
449 rule->effect.follow = true;
450 } else if (streq("-d", *args) || streq("--desktop", *args)) {
456 strncpy(rule->effect.desc, *args, sizeof(rule->effect.desc));
464 } else if (streq("-r", *args) || streq("--rm", *args)) {
470 if (sscanf(*args, "%X", &uid) == 1)
471 remove_rule_by_uid(uid);
476 } else if (streq("-l", *args) || streq("--list", *args)) {
478 list_rules(num > 0 ? *args : NULL, rsp);
488 bool cmd_pointer(char **args, int num) {
490 if (streq("-t", *args) || streq("--track", *args)) {
495 if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1)
499 } else if (streq("-g", *args) || streq("--grab", *args)) {
503 pointer_action_t pac;
504 if (parse_pointer_action(*args, &pac))
517 bool cmd_restore(char **args, int num) {
519 if (streq("-T", *args) || streq("--tree", *args)) {
524 } else if (streq("-H", *args) || streq("--history", *args)) {
528 restore_history(*args);
538 bool cmd_control(char **args, int num) {
540 if (streq("--adopt-orphans", *args)) {
542 } else if (streq("--put-status", *args)) {
544 } else if (streq("--toggle-visibility", *args)) {
555 bool cmd_config(char **args, int num, char *rsp) {
557 return set_setting(*args, *(args + 1));
559 return get_setting(*args, rsp);
564 bool cmd_quit(char **args, int num) {
565 if (num > 0 && sscanf(*args, "%i", &exit_status) != 1)
571 bool handle_message(char *msg, int msg_len, char *rsp)
575 char **args = malloc(cap * sizeof(char *));
579 for (int i = 0, j = 0; i < msg_len; i++) {
581 args[num++] = msg + j;
586 char **new = realloc(args, cap * sizeof(char *));
599 char **args_orig = args;
600 bool ret = process_message(args, num, rsp);
605 bool process_message(char **args, int num, char *rsp)
607 if (streq("window", *args)) {
608 return cmd_window(++args, --num);
609 } else if (streq("desktop", *args)) {
610 return cmd_desktop(++args, --num);
611 } else if (streq("monitor", *args)) {
612 return cmd_monitor(++args, --num);
613 } else if (streq("query", *args)) {
614 return cmd_query(++args, --num, rsp);
615 } else if (streq("restore", *args)) {
616 return cmd_restore(++args, --num);
617 } else if (streq("control", *args)) {
618 return cmd_control(++args, --num);
619 } else if (streq("rule", *args)) {
620 return cmd_rule(++args, --num, rsp);
621 } else if (streq("pointer", *args)) {
622 return cmd_pointer(++args, --num);
623 } else if (streq("config", *args)) {
624 return cmd_config(++args, --num, rsp);
625 } else if (streq("quit", *args)) {
626 return cmd_quit(++args, --num);
632 bool set_setting(char *name, char *value)
634 if (streq("border_width", name)) {
635 if (sscanf(value, "%u", &border_width) != 1)
637 } else if (streq("window_gap", name)) {
638 if (sscanf(value, "%i", &window_gap) != 1)
640 } else if (streq("split_ratio", name)) {
641 if (sscanf(value, "%lf", &split_ratio) != 1)
643 } else if (streq("left_padding", name)) {
644 if (sscanf(value, "%i", &mon->left_padding) != 1)
646 } else if (streq("right_padding", name)) {
647 if (sscanf(value, "%i", &mon->right_padding) != 1)
649 } else if (streq("top_padding", name)) {
650 if (sscanf(value, "%i", &mon->top_padding) != 1)
652 } else if (streq("bottom_padding", name)) {
653 if (sscanf(value, "%i", &mon->bottom_padding) != 1)
655 #define SETCOLOR(s) \
656 } else if (streq(#s, name)) { \
657 if (get_color(value, &s ## _pxl)) \
658 strncpy(s, value, sizeof(s)); \
661 SETCOLOR(focused_border_color)
662 SETCOLOR(active_border_color)
663 SETCOLOR(normal_border_color)
664 SETCOLOR(presel_border_color)
665 SETCOLOR(focused_locked_border_color)
666 SETCOLOR(active_locked_border_color)
667 SETCOLOR(normal_locked_border_color)
668 SETCOLOR(urgent_border_color)
670 } else if (streq("focus_follows_pointer", name)) {
672 if (parse_bool(value, &b) && b != focus_follows_pointer) {
673 uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)};
674 for (monitor_t *m = mon_head; m != NULL; m = m->next)
675 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
676 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root))
677 xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values);
678 if (focus_follows_pointer)
679 disable_motion_recorder();
681 enable_motion_recorder();
682 focus_follows_pointer = b;
688 } else if (streq(#s, name)) { \
689 if (!parse_bool(value, &s)) \
691 SETBOOL(borderless_monocle)
692 SETBOOL(gapless_monocle)
693 SETBOOL(pointer_follows_monitor)
694 SETBOOL(monitor_focus_fallback)
695 SETBOOL(adaptative_raise)
696 SETBOOL(apply_shadow_property)
697 SETBOOL(auto_alternate)
699 SETBOOL(focus_by_distance)
700 SETBOOL(history_aware_focus)
702 } else if (streq("wm_name", name)) {
703 strncpy(wm_name, value, sizeof(wm_name));
704 ewmh_update_wm_name();
710 for (monitor_t *m = mon_head; m != NULL; m = m->next)
711 for (desktop_t *d = m->desk_head; d != NULL; d = d->next)
717 bool get_setting(char *name, char* rsp)
719 if (streq("border_width", name))
720 snprintf(rsp, BUFSIZ, "%u", border_width);
721 else if (streq("split_ratio", name))
722 snprintf(rsp, BUFSIZ, "%lf", split_ratio);
723 else if (streq("window_gap", name))
724 snprintf(rsp, BUFSIZ, "%i", window_gap);
725 else if (streq("left_padding", name))
726 snprintf(rsp, BUFSIZ, "%i", mon->left_padding);
727 else if (streq("right_padding", name))
728 snprintf(rsp, BUFSIZ, "%i", mon->right_padding);
729 else if (streq("top_padding", name))
730 snprintf(rsp, BUFSIZ, "%i", mon->top_padding);
731 else if (streq("bottom_padding", name))
732 snprintf(rsp, BUFSIZ, "%i", mon->bottom_padding);
733 #define GETCOLOR(s) \
734 else if (streq(#s, name)) \
735 snprintf(rsp, BUFSIZ, "%s (%06X)", s, s##_pxl);
736 GETCOLOR(focused_border_color)
737 GETCOLOR(active_border_color)
738 GETCOLOR(normal_border_color)
739 GETCOLOR(presel_border_color)
740 GETCOLOR(focused_locked_border_color)
741 GETCOLOR(active_locked_border_color)
742 GETCOLOR(normal_locked_border_color)
743 GETCOLOR(urgent_border_color)
746 else if (streq(#s, name)) \
747 snprintf(rsp, BUFSIZ, "%s", BOOLSTR(s));
748 GETBOOL(borderless_monocle)
749 GETBOOL(gapless_monocle)
750 GETBOOL(focus_follows_pointer)
751 GETBOOL(pointer_follows_monitor)
752 GETBOOL(monitor_focus_fallback)
753 GETBOOL(adaptative_raise)
754 GETBOOL(apply_shadow_property)
755 GETBOOL(auto_alternate)
757 GETBOOL(focus_by_distance)
758 GETBOOL(history_aware_focus)
760 else if (streq("wm_name", name))
761 snprintf(rsp, BUFSIZ, "%s", wm_name);
767 bool parse_bool(char *value, bool *b)
769 if (streq("true", value) || streq("on", value)) {
772 } else if (streq("false", value) || streq("off", value)) {
779 bool parse_layout(char *s, layout_t *l)
781 if (streq("monocle", s)) {
784 } else if (streq("tiled", s)) {
791 bool parse_direction(char *s, direction_t *d)
793 if (streq("right", s)) {
796 } else if (streq("down", s)) {
799 } else if (streq("left", s)) {
802 } else if (streq("up", s)) {
809 bool parse_cycle_direction(char *s, cycle_dir_t *d)
811 if (streq("next", s)) {
814 } else if (streq("prev", s)) {
821 bool parse_circulate_direction(char *s, circulate_dir_t *d)
823 if (streq("forward", s)) {
824 *d = CIRCULATE_FORWARD;
826 } else if (streq("backward", s)) {
827 *d = CIRCULATE_BACKWARD;
833 bool parse_flip(char *s, flip_t *f)
835 if (streq("horizontal", s)) {
836 *f = FLIP_HORIZONTAL;
838 } else if (streq("vertical", s)) {
845 bool parse_fence_move(char *s, fence_move_t *m)
847 if (streq("push", s)) {
850 } else if (streq("pull", s)) {
857 bool parse_pointer_action(char *s, pointer_action_t *a)
859 if (streq("move", s)) {
862 } else if (streq("resize_corner", s)) {
863 *a = ACTION_RESIZE_CORNER;
865 } else if (streq("resize_side", s)) {
866 *a = ACTION_RESIZE_SIDE;
868 } else if (streq("focus", s)) {
875 bool parse_window_id(char *s, long int *i)
879 long int ret = strtol(s, &end, 0);
880 if (errno != 0 || *end != '\0')