1 /* Copyright (c) 2012, Bastien Dejean
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 void handle_message(char *msg, int msg_len, FILE *rsp)
49 char **args = calloc(cap, sizeof(char *));
52 perror("Handle message: calloc");
56 for (int i = 0, j = 0; i < msg_len; i++) {
58 args[num++] = msg + j;
63 char **new = realloc(args, cap * sizeof(char *));
66 perror("Handle message: realloc");
76 fail(rsp, "No arguments given.\n");
80 char **args_orig = args;
81 process_message(args, num, rsp);
85 void process_message(char **args, int num, FILE *rsp)
87 if (streq("node", *args)) {
88 cmd_node(++args, --num, rsp);
89 } else if (streq("desktop", *args)) {
90 cmd_desktop(++args, --num, rsp);
91 } else if (streq("monitor", *args)) {
92 cmd_monitor(++args, --num, rsp);
93 } else if (streq("query", *args)) {
94 cmd_query(++args, --num, rsp);
95 } else if (streq("subscribe", *args)) {
96 cmd_subscribe(++args, --num, rsp);
98 } else if (streq("wm", *args)) {
99 cmd_wm(++args, --num, rsp);
100 } else if (streq("rule", *args)) {
101 cmd_rule(++args, --num, rsp);
102 } else if (streq("config", *args)) {
103 cmd_config(++args, --num, rsp);
104 } else if (streq("quit", *args)) {
105 cmd_quit(++args, --num, rsp);
107 fail(rsp, "Unknown domain or command: '%s'.\n", *args);
114 void cmd_node(char **args, int num, FILE *rsp)
117 fail(rsp, "node: Missing arguments.\n");
121 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
122 coordinates_t trg = ref;
124 if ((*args)[0] != OPT_CHR) {
126 if ((ret = node_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
129 handle_failure(ret, "node", *args, rsp);
135 fail(rsp, "node: Missing commands.\n");
139 bool changed = false;
142 if (streq("-f", *args) || streq("--focus", *args)) {
143 coordinates_t dst = trg;
144 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
147 if ((ret = node_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
148 handle_failure(ret, "node -f", *args, rsp);
152 if (dst.node == NULL || !focus_node(dst.monitor, dst.desktop, dst.node)) {
156 } else if (streq("-a", *args) || streq("--activate", *args)) {
157 coordinates_t dst = trg;
158 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
161 if ((ret = node_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
162 handle_failure(ret, "node -a", *args, rsp);
166 if (dst.node == NULL || !activate_node(dst.monitor, dst.desktop, dst.node)) {
170 } else if (streq("-d", *args) || streq("--to-desktop", *args)) {
173 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
178 if ((ret = desktop_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
180 if (num > 1 && streq("--follow", *(args+1))) {
184 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.desktop->focus, follow)) {
185 trg.monitor = dst.monitor;
186 trg.desktop = dst.desktop;
192 handle_failure(ret, "node -d", *args, rsp);
195 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
198 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
203 if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
205 if (num > 1 && streq("--follow", *(args+1))) {
209 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus, follow)) {
210 trg.monitor = dst.monitor;
211 trg.desktop = dst.monitor->desk;
217 handle_failure(ret, "node -m", *args, rsp);
220 } else if (streq("-n", *args) || streq("--to-node", *args)) {
223 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
228 if ((ret = node_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
230 if (num > 1 && streq("--follow", *(args+1))) {
234 if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node, follow)) {
235 trg.monitor = dst.monitor;
236 trg.desktop = dst.desktop;
242 handle_failure(ret, "node -n", *args, rsp);
245 } else if (streq("-s", *args) || streq("--swap", *args)) {
248 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
253 if ((ret = node_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
255 if (num > 1 && streq("--follow", *(args+1))) {
259 if (swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node, follow)) {
260 trg.monitor = dst.monitor;
261 trg.desktop = dst.desktop;
267 handle_failure(ret, "node -s", *args, rsp);
270 } else if (streq("-l", *args) || streq("--layer", *args)) {
273 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
277 if (parse_stack_layer(*args, &lyr)) {
278 if (!set_layer(trg.monitor, trg.desktop, trg.node, lyr)) {
283 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
286 } else if (streq("-t", *args) || streq("--state", *args)) {
289 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
293 bool alternate = false;
294 if ((*args)[0] == '~') {
298 if (parse_client_state(*args, &cst)) {
299 if (alternate && trg.node != NULL && trg.node->client != NULL &&
300 trg.node->client->state == cst) {
301 cst = trg.node->client->last_state;
303 if (!set_state(trg.monitor, trg.desktop, trg.node, cst)) {
309 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
312 } else if (streq("-g", *args) || streq("--flag", *args)) {
315 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
318 if (trg.node == NULL) {
322 char *key = strtok(*args, EQL_TOK);
323 char *val = strtok(NULL, EQL_TOK);
329 if (parse_bool(val, &b)) {
332 fail(rsp, "node %s: Invalid value for %s: '%s'.\n", *(args - 1), key, val);
336 if (streq("hidden", key)) {
337 set_hidden(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->hidden));
339 } else if (streq("sticky", key)) {
340 set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->sticky));
341 } else if (streq("private", key)) {
342 set_private(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->private));
343 } else if (streq("locked", key)) {
344 set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->locked));
345 } else if (streq("marked", key)) {
346 set_marked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->marked));
348 fail(rsp, "node %s: Invalid key: '%s'.\n", *(args - 1), key);
351 } else if (streq("-p", *args) || streq("--presel-dir", *args)) {
354 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
357 if (trg.node == NULL || trg.node->vacant) {
361 if (streq("cancel", *args)) {
362 cancel_presel(trg.monitor, trg.desktop, trg.node);
364 bool alternate = false;
365 if ((*args)[0] == '~') {
370 if (parse_direction(*args, &dir)) {
371 if (alternate && trg.node->presel != NULL && trg.node->presel->split_dir == dir) {
372 cancel_presel(trg.monitor, trg.desktop, trg.node);
374 presel_dir(trg.monitor, trg.desktop, trg.node, dir);
375 if (!IS_RECEPTACLE(trg.node)) {
376 draw_presel_feedback(trg.monitor, trg.desktop, trg.node);
380 fail(rsp, "node %s: Invalid argument: '%s%s'.\n", *(args - 1), alternate?"~":"", *args);
384 } else if (streq("-o", *args) || streq("--presel-ratio", *args)) {
387 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
390 if (trg.node == NULL || trg.node->vacant) {
395 if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1) {
396 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
399 presel_ratio(trg.monitor, trg.desktop, trg.node, rat);
400 draw_presel_feedback(trg.monitor, trg.desktop, trg.node);
402 } else if (streq("-v", *args) || streq("--move", *args)) {
405 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
409 if (sscanf(*args, "%i", &dx) == 1) {
411 if (sscanf(*args, "%i", &dy) == 1) {
412 if (!move_client(&trg, dx, dy)) {
417 fail(rsp, "node %s: Invalid dy argument: '%s'.\n", *(args - 3), *args);
421 fail(rsp, "node %s: Invalid dx argument: '%s'.\n", *(args - 2), *args);
424 } else if (streq("-z", *args) || streq("--resize", *args)) {
427 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
431 if (parse_resize_handle(*args, &rh)) {
434 if (sscanf(*args, "%i", &dx) == 1) {
436 if (sscanf(*args, "%i", &dy) == 1) {
437 if (!resize_client(&trg, rh, dx, dy, true)) {
442 fail(rsp, "node %s: Invalid dy argument: '%s'.\n", *(args - 3), *args);
446 fail(rsp, "node %s: Invalid dx argument: '%s'.\n", *(args - 2), *args);
450 fail(rsp, "node %s: Invalid resize handle argument: '%s'.\n", *(args - 1), *args);
453 } else if (streq("-r", *args) || streq("--ratio", *args)) {
456 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
459 if (trg.node == NULL) {
463 if ((*args)[0] == '+' || (*args)[0] == '-') {
465 if (sscanf(*args, "%f", &delta) == 1) {
466 double rat = trg.node->split_ratio;
467 if (delta > -1 && delta < 1) {
470 int max = (trg.node->split_type == TYPE_HORIZONTAL ? trg.node->rectangle.height : trg.node->rectangle.width);
471 rat = ((max * rat) + delta) / max;
473 if (rat > 0 && rat < 1) {
474 set_ratio(trg.node, rat);
480 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
485 if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) {
486 set_ratio(trg.node, rat);
488 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
493 } else if (streq("-F", *args) || streq("--flip", *args)) {
496 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
499 if (trg.node == NULL) {
504 if (parse_flip(*args, &flp)) {
505 flip_tree(trg.node, flp);
511 } else if (streq("-R", *args) || streq("--rotate", *args)) {
514 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
517 if (trg.node == NULL) {
522 if (parse_degree(*args, °)) {
523 rotate_tree(trg.node, deg);
526 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
529 } else if (streq("-E", *args) || streq("--equalize", *args)) {
530 if (trg.node == NULL) {
534 equalize_tree(trg.node);
536 } else if (streq("-B", *args) || streq("--balance", *args)) {
537 if (trg.node == NULL) {
541 balance_tree(trg.node);
543 } else if (streq("-C", *args) || streq("--circulate", *args)) {
546 fail(rsp, "node %s: Not enough arguments.\n", *(args - 1));
549 if (trg.node == NULL) {
554 if (parse_circulate_direction(*args, &cir)) {
555 circulate_leaves(trg.monitor, trg.desktop, trg.node, cir);
558 fail(rsp, "node %s: Invalid argument: '%s'.\n", *(args - 1), *args);
561 } else if (streq("-i", *args) || streq("--insert-receptacle", *args)) {
562 insert_receptacle(trg.monitor, trg.desktop, trg.node);
564 } else if (streq("-c", *args) || streq("--close", *args)) {
566 fail(rsp, "node %s: Trailing commands.\n", *args);
569 if (trg.node == NULL || locked_count(trg.node) > 0) {
573 close_node(trg.node);
575 } else if (streq("-k", *args) || streq("--kill", *args)) {
577 fail(rsp, "node %s: Trailing commands.\n", *args);
580 if (trg.node == NULL) {
584 kill_node(trg.monitor, trg.desktop, trg.node);
588 fail(rsp, "node: Unknown command: '%s'.\n", *args);
596 arrange(trg.monitor, trg.desktop);
600 void cmd_desktop(char **args, int num, FILE *rsp)
603 fail(rsp, "desktop: Missing arguments.\n");
607 coordinates_t ref = {mon, mon->desk, NULL};
608 coordinates_t trg = ref;
610 if ((*args)[0] != OPT_CHR) {
612 if ((ret = desktop_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
615 handle_failure(ret, "desktop", *args, rsp);
621 fail(rsp, "desktop: Missing commands.\n");
625 bool changed = false;
628 if (streq("-f", *args) || streq("--focus", *args)) {
629 coordinates_t dst = trg;
630 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
633 if ((ret = desktop_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
634 handle_failure(ret, "desktop -f", *args, rsp);
638 focus_node(dst.monitor, dst.desktop, NULL);
639 } else if (streq("-a", *args) || streq("--activate", *args)) {
640 coordinates_t dst = trg;
641 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
644 if ((ret = desktop_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
645 handle_failure(ret, "desktop -a", *args, rsp);
649 if (activate_desktop(dst.monitor, dst.desktop)) {
650 activate_node(dst.monitor, dst.desktop, NULL);
655 } else if (streq("-m", *args) || streq("--to-monitor", *args)) {
658 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
661 if (trg.monitor->desk_head == trg.monitor->desk_tail) {
667 if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
669 if (num > 1 && streq("--follow", *(args+1))) {
673 if (transfer_desktop(trg.monitor, dst.monitor, trg.desktop, follow)) {
674 trg.monitor = dst.monitor;
680 handle_failure(ret, "desktop -m", *args, rsp);
683 } else if (streq("-s", *args) || streq("--swap", *args)) {
686 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
691 if ((ret = desktop_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
693 if (num > 1 && streq("--follow", *(args+1))) {
697 if (swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop, follow)) {
698 trg.monitor = dst.monitor;
704 handle_failure(ret, "desktop -s", *args, rsp);
707 } else if (streq("-b", *args) || streq("--bubble", *args)) {
710 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
714 if (parse_cycle_direction(*args, &cyc)) {
715 desktop_t *d = trg.desktop;
716 if (cyc == CYCLE_PREV) {
717 if (d->prev == NULL) {
718 while (d->next != NULL) {
719 swap_desktops(trg.monitor, d, trg.monitor, d->next, false);
722 swap_desktops(trg.monitor, d, trg.monitor, d->prev, false);
725 if (d->next == NULL) {
726 while (d->prev != NULL) {
727 swap_desktops(trg.monitor, d, trg.monitor, d->prev, false);
730 swap_desktops(trg.monitor, d, trg.monitor, d->next, false);
734 fail(rsp, "desktop %s: Invalid argument: '%s'.\n", *(args - 1), *args);
737 } else if (streq("-l", *args) || streq("--layout", *args)) {
740 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
746 if (parse_cycle_direction(*args, &cyc)) {
747 ret = set_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2);
748 } else if (parse_layout(*args, &lyt)) {
749 ret = set_layout(trg.monitor, trg.desktop, lyt);
751 fail(rsp, "desktop %s: Invalid argument: '%s'.\n", *(args - 1), *args);
758 } else if (streq("-n", *args) || streq("--rename", *args)) {
761 fail(rsp, "desktop %s: Not enough arguments.\n", *(args - 1));
764 rename_desktop(trg.monitor, trg.desktop, *args);
765 } else if (streq("-r", *args) || streq("--remove", *args)) {
767 fail(rsp, "desktop %s: Trailing commands.\n", *args);
770 if (trg.monitor->desk_head != trg.monitor->desk_tail) {
771 desktop_t *fallback = trg.desktop->prev == NULL ?
774 merge_desktops(trg.monitor, trg.desktop, trg.monitor, fallback);
775 remove_desktop(trg.monitor, trg.desktop);
782 fail(rsp, "desktop: Unknown command: '%s'.\n", *args);
789 arrange(trg.monitor, trg.desktop);
793 void cmd_monitor(char **args, int num, FILE *rsp)
796 fail(rsp, "monitor: Missing arguments.\n");
800 coordinates_t ref = {mon, NULL, NULL};
801 coordinates_t trg = ref;
803 if ((*args)[0] != OPT_CHR) {
805 if ((ret = monitor_from_desc(*args, &ref, &trg)) == SELECTOR_OK) {
808 handle_failure(ret, "monitor", *args, rsp);
814 fail(rsp, "monitor: Missing commands.\n");
819 if (streq("-f", *args) || streq("--focus", *args)) {
820 coordinates_t dst = trg;
821 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
824 if ((ret = monitor_from_desc(*args, &ref, &dst)) != SELECTOR_OK) {
825 handle_failure(ret, "monitor -f", *args, rsp);
830 focus_node(dst.monitor, NULL, NULL);
831 } else if (streq("-s", *args) || streq("--swap", *args)) {
834 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
839 if ((ret = monitor_from_desc(*args, &ref, &dst)) == SELECTOR_OK) {
840 if (!swap_monitors(trg.monitor, dst.monitor)) {
845 handle_failure(ret, "monitor -s", *args, rsp);
848 } else if (streq("-d", *args) || streq("--reset-desktops", *args)) {
851 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
854 desktop_t *d = trg.monitor->desk_head;
855 while (num > 0 && d != NULL) {
856 rename_desktop(trg.monitor, d, *args);
860 put_status(SBSC_MASK_REPORT);
862 add_desktop(trg.monitor, make_desktop(*args, XCB_NONE));
866 desktop_t *next = d->next;
867 if (d == mon->desk) {
868 focus_node(trg.monitor, d->prev, d->prev->focus);
870 merge_desktops(trg.monitor, d, mon, mon->desk);
871 remove_desktop(trg.monitor, d);
874 } else if (streq("-a", *args) || streq("--add-desktops", *args)) {
877 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
881 add_desktop(trg.monitor, make_desktop(*args, XCB_NONE));
884 } else if (streq("-r", *args) || streq("--remove", *args)) {
886 fail(rsp, "monitor %s: Trailing commands.\n", *args);
889 if (mon_head == mon_tail) {
893 remove_monitor(trg.monitor);
895 } else if (streq("-o", *args) || streq("--reorder-desktops", *args)) {
898 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
901 desktop_t *d = trg.monitor->desk_head;
902 while (d != NULL && num > 0) {
903 desktop_t *next = d->next;
905 if (locate_desktop(*args, &dst) && dst.monitor == trg.monitor) {
906 swap_desktops(trg.monitor, d, dst.monitor, dst.desktop, false);
907 if (next == dst.desktop) {
914 } else if (streq("-g", *args) || streq("--rectangle", *args)) {
917 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
921 if (parse_rectangle(*args, &r)) {
922 update_root(trg.monitor, &r);
924 fail(rsp, "monitor %s: Invalid argument: '%s'.\n", *(args - 1), *args);
927 } else if (streq("-n", *args) || streq("--rename", *args)) {
930 fail(rsp, "monitor %s: Not enough arguments.\n", *(args - 1));
933 rename_monitor(trg.monitor, *args);
935 fail(rsp, "monitor: Unknown command: '%s'.\n", *args);
942 void cmd_query(char **args, int num, FILE *rsp)
944 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
945 coordinates_t trg = {NULL, NULL, NULL};
946 monitor_select_t *monitor_sel = NULL;
947 desktop_select_t *desktop_sel = NULL;
948 node_select_t *node_sel = NULL;
949 domain_t dom = DOMAIN_TREE;
950 bool print_ids = true;
954 fail(rsp, "query: Missing arguments.\n");
959 if (streq("-T", *args) || streq("--tree", *args)) {
960 dom = DOMAIN_TREE, d++;
961 } else if (streq("-M", *args) || streq("--monitors", *args)) {
962 dom = DOMAIN_MONITOR, d++;
963 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
966 coordinates_t tmp = ref;
967 if ((ret = monitor_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
968 handle_failure(ret, "query -M", *args, rsp);
972 } else if (streq("-D", *args) || streq("--desktops", *args)) {
973 dom = DOMAIN_DESKTOP, d++;
974 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
977 coordinates_t tmp = ref;
978 if ((ret = desktop_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
979 handle_failure(ret, "query -D", *args, rsp);
983 } else if (streq("-N", *args) || streq("--nodes", *args)) {
984 dom = DOMAIN_NODE, d++;
985 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
988 coordinates_t tmp = ref;
989 if ((ret = node_from_desc(*args, &tmp, &ref)) != SELECTOR_OK) {
990 handle_failure(ret, "query -N", *args, rsp);
994 } else if (streq("-m", *args) || streq("--monitor", *args)) {
995 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
998 if ((*args)[0] == '.') {
1000 monitor_sel = malloc(sizeof(monitor_select_t));
1001 *monitor_sel = make_monitor_select();
1002 char *desc = copy_string(*args, strlen(*args));
1003 if (!parse_monitor_modifiers(desc, monitor_sel)) {
1004 handle_failure(SELECTOR_BAD_MODIFIERS, "query -m", *args, rsp);
1009 } else if ((ret = monitor_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1010 handle_failure(ret, "query -m", *args, rsp);
1014 trg.monitor = ref.monitor;
1016 } else if (streq("-d", *args) || streq("--desktop", *args)) {
1017 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
1020 if ((*args)[0] == '.') {
1022 desktop_sel = malloc(sizeof(desktop_select_t));
1023 *desktop_sel = make_desktop_select();
1024 char *desc = copy_string(*args, strlen(*args));
1025 if (!parse_desktop_modifiers(desc, desktop_sel)) {
1026 handle_failure(SELECTOR_BAD_MODIFIERS, "query -d", *args, rsp);
1031 } else if ((ret = desktop_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1032 handle_failure(ret, "query -d", *args, rsp);
1036 trg.monitor = ref.monitor;
1037 trg.desktop = ref.desktop;
1039 } else if (streq("-n", *args) || streq("--node", *args)) {
1040 if (num > 1 && *(args + 1)[0] != OPT_CHR) {
1043 if ((*args)[0] == '.') {
1045 node_sel = malloc(sizeof(node_select_t));
1046 *node_sel = make_node_select();
1047 char *desc = copy_string(*args, strlen(*args));
1048 if (!parse_node_modifiers(desc, node_sel)) {
1049 handle_failure(SELECTOR_BAD_MODIFIERS, "query -n", *args, rsp);
1054 } else if ((ret = node_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1055 handle_failure(ret, "query -n", *args, rsp);
1060 if (ref.node == NULL) {
1065 } else if (streq("--names", *args)) {
1068 fail(rsp, "query: Unknown option: '%s'.\n", *args);
1075 fail(rsp, "query: No commands given.\n");
1080 fail(rsp, "query: Multiple commands given.\n");
1084 if (dom == DOMAIN_TREE && trg.monitor == NULL) {
1085 fail(rsp, "query -T: No options given.\n");
1089 if (!print_ids && (dom == DOMAIN_NODE || dom == DOMAIN_TREE)) {
1090 fail(rsp, "query -%c: --names only applies to -M and -D.\n", dom == DOMAIN_NODE ? 'N' : 'T');
1094 if (dom == DOMAIN_NODE) {
1095 if (query_node_ids(&ref, &trg, node_sel, rsp) < 1) {
1098 } else if (dom == DOMAIN_DESKTOP) {
1099 if (query_desktop_ids(&ref, &trg, desktop_sel, print_ids ? fprint_desktop_id : fprint_desktop_name, rsp) < 1) {
1102 } else if (dom == DOMAIN_MONITOR) {
1103 if (query_monitor_ids(&ref, &trg, monitor_sel, print_ids ? fprint_monitor_id : fprint_monitor_name, rsp) < 1) {
1107 if (trg.node != NULL) {
1108 query_node(trg.node, rsp);
1109 } else if (trg.desktop != NULL) {
1110 query_desktop(trg.desktop, rsp);
1112 query_monitor(trg.monitor, rsp);
1123 void cmd_rule(char **args, int num, FILE *rsp)
1126 fail(rsp, "rule: Missing commands.\n");
1131 if (streq("-a", *args) || streq("--add", *args)) {
1134 fail(rsp, "rule %s: Not enough arguments.\n", *(args - 1));
1137 rule_t *rule = make_rule();
1138 char *class_name = strtok(*args, COL_TOK);
1139 char *instance_name = strtok(NULL, COL_TOK);
1140 snprintf(rule->class_name, sizeof(rule->class_name), "%s", class_name);
1141 snprintf(rule->instance_name, sizeof(rule->instance_name), "%s", instance_name==NULL?MATCH_ANY:instance_name);
1145 if (streq("-o", *args) || streq("--one-shot", *args)) {
1146 rule->one_shot = true;
1148 for (size_t j = 0; i < sizeof(rule->effect) && j < strlen(*args); i++, j++) {
1149 rule->effect[i] = (*args)[j];
1151 if (num > 1 && i < sizeof(rule->effect)) {
1152 rule->effect[i++] = ' ';
1157 rule->effect[MIN(i, sizeof(rule->effect) - 1)] = '\0';
1159 } else if (streq("-r", *args) || streq("--remove", *args)) {
1162 fail(rsp, "rule %s: Not enough arguments.\n", *(args - 1));
1167 if (parse_index(*args, &idx)) {
1168 remove_rule_by_index(idx - 1);
1169 } else if (streq("tail", *args)) {
1170 remove_rule(rule_tail);
1171 } else if (streq("head", *args)) {
1172 remove_rule(rule_head);
1174 remove_rule_by_cause(*args);
1178 } else if (streq("-l", *args) || streq("--list", *args)) {
1181 fail(rsp, "rule: Unknown command: '%s'.\n", *args);
1188 void cmd_wm(char **args, int num, FILE *rsp)
1191 fail(rsp, "wm: Missing commands.\n");
1196 if (streq("-d", *args) || streq("--dump-state", *args)) {
1199 } else if (streq("-l", *args) || streq("--load-state", *args)) {
1202 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1205 if (!restore_tree(*args)) {
1209 } else if (streq("-a", *args) || streq("--add-monitor", *args)) {
1212 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1218 if (parse_rectangle(*args, &r)) {
1219 monitor_t *m = make_monitor(name, &r, XCB_NONE);
1221 add_desktop(m, make_desktop(NULL, XCB_NONE));
1223 fail(rsp, "wm %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1226 } else if (streq("-O", *args) || streq("--reorder-monitors", *args)) {
1229 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1232 monitor_t *m = mon_head;
1233 while (m != NULL && num > 0) {
1234 monitor_t *next = m->next;
1236 if (locate_monitor(*args, &dst)) {
1237 swap_monitors(m, dst.monitor);
1238 if (next == dst.monitor) {
1245 } else if (streq("-o", *args) || streq("--adopt-orphans", *args)) {
1247 } else if (streq("-g", *args) || streq("--get-status", *args)) {
1249 } else if (streq("-h", *args) || streq("--record-history", *args)) {
1252 fail(rsp, "wm %s: Not enough arguments.\n", *(args - 1));
1256 if (parse_bool(*args, &b)) {
1259 fail(rsp, "wm %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1263 fail(rsp, "wm: Unknown command: '%s'.\n", *args);
1270 void cmd_subscribe(char **args, int num, FILE *rsp)
1275 char *fifo_path = NULL;
1276 subscriber_mask_t mask;
1279 if (streq("-c", *args) || streq("--count", *args)) {
1282 fail(rsp, "subscribe %s: Not enough arguments.\n", *(args - 1));
1285 if (sscanf(*args, "%i", &count) != 1 || count < 1) {
1286 fail(rsp, "subscribe %s: Invalid argument: '%s'.\n", *(args - 1), *args);
1289 } else if (streq("-f", *args) || streq("--fifo", *args)) {
1290 fifo_path = mktempfifo(FIFO_TEMPLATE);
1291 if (fifo_path == NULL) {
1292 fail(rsp, "subscribe %s: Can't create FIFO.\n", *(args - 1));
1295 } else if (parse_subscriber_mask(*args, &mask)) {
1298 fail(rsp, "subscribe: Invalid argument: '%s'.\n", *args);
1305 field = SBSC_MASK_REPORT;
1309 fprintf(rsp, "%s\n", fifo_path);
1313 stream = fopen(fifo_path, "w");
1315 if (stream == NULL) {
1316 perror("subscribe: fopen");
1317 goto free_fifo_path;
1321 add_subscriber(stream, fifo_path, field, count);
1335 void cmd_quit(char **args, int num, FILE *rsp)
1337 if (num > 0 && sscanf(*args, "%i", &exit_status) != 1) {
1338 fail(rsp, "%s: Invalid argument: '%s'.\n", *(args - 1), *args);
1344 void cmd_config(char **args, int num, FILE *rsp)
1347 fail(rsp, "config: Missing arguments.\n");
1351 coordinates_t ref = {mon, mon->desk, mon->desk->focus};
1352 coordinates_t trg = {NULL, NULL, NULL};
1354 while (num > 0 && (*args)[0] == OPT_CHR) {
1355 if (streq("-m", *args) || streq("--monitor", *args)) {
1358 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1362 if ((ret = monitor_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1363 handle_failure(ret, "config -m", *args, rsp);
1366 } else if (streq("-d", *args) || streq("--desktop", *args)) {
1369 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1373 if ((ret = desktop_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1374 handle_failure(ret, "config -d", *args, rsp);
1377 } else if (streq("-n", *args) || streq("--node", *args)) {
1380 fail(rsp, "config %s: Not enough arguments.\n", *(args - 1));
1384 if ((ret = node_from_desc(*args, &ref, &trg)) != SELECTOR_OK) {
1385 handle_failure(ret, "config -n", *args, rsp);
1389 fail(rsp, "config: Unknown option: '%s'.\n", *args);
1395 set_setting(trg, *args, *(args + 1), rsp);
1396 } else if (num == 1) {
1397 get_setting(trg, *args, rsp);
1399 fail(rsp, "config: Was expecting 1 or 2 arguments, received %i.\n", num);
1403 void set_setting(coordinates_t loc, char *name, char *value, FILE *rsp)
1405 bool colors_changed = false;
1406 #define SET_DEF_DEFMON_DEFDESK_WIN(k, v) \
1407 if (loc.node != NULL) { \
1408 for (node_t *n = first_extrema(loc.node); n != NULL; n = next_leaf(n, loc.node)) { \
1409 if (n->client != NULL) { \
1413 } else if (loc.desktop != NULL) { \
1414 loc.desktop->k = v; \
1415 for (node_t *n = first_extrema(loc.desktop->root); n != NULL; n = next_leaf(n, loc.desktop->root)) { \
1416 if (n->client != NULL) { \
1420 } else if (loc.monitor != NULL) { \
1421 loc.monitor->k = v; \
1422 for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) { \
1424 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) { \
1425 if (n->client != NULL) { \
1432 for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1434 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) { \
1436 for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) { \
1437 if (n->client != NULL) { \
1444 if (streq("border_width", name)) {
1446 if (sscanf(value, "%u", &bw) != 1) {
1447 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1450 SET_DEF_DEFMON_DEFDESK_WIN(border_width, bw)
1451 #undef SET_DEF_DEFMON_DEFDESK_WIN
1452 #define SET_DEF_DEFMON_DESK(k, v) \
1453 if (loc.desktop != NULL) { \
1454 loc.desktop->k = v; \
1455 } else if (loc.monitor != NULL) { \
1456 loc.monitor->k = v; \
1457 for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) { \
1462 for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1464 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) { \
1469 } else if (streq("window_gap", name)) {
1471 if (sscanf(value, "%i", &wg) != 1) {
1475 SET_DEF_DEFMON_DESK(window_gap, wg)
1476 #undef SET_DEF_DEFMON_DESK
1477 #define SET_DEF_MON_DESK(k, v) \
1478 if (loc.desktop != NULL) { \
1479 loc.desktop->k = v; \
1480 } else if (loc.monitor != NULL) { \
1481 loc.monitor->k = v; \
1484 for (monitor_t *m = mon_head; m != NULL; m = m->next) { \
1488 } else if (streq("top_padding", name)) {
1490 if (sscanf(value, "%i", &tp) != 1) {
1491 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1494 SET_DEF_MON_DESK(padding.top, tp)
1495 } else if (streq("right_padding", name)) {
1497 if (sscanf(value, "%i", &rp) != 1) {
1498 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1501 SET_DEF_MON_DESK(padding.right, rp)
1502 } else if (streq("bottom_padding", name)) {
1504 if (sscanf(value, "%i", &bp) != 1) {
1505 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1508 SET_DEF_MON_DESK(padding.bottom, bp)
1509 } else if (streq("left_padding", name)) {
1511 if (sscanf(value, "%i", &lp) != 1) {
1512 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1515 SET_DEF_MON_DESK(padding.left, lp)
1516 #undef SET_DEF_MON_DESK
1517 #define SET_STR(s) \
1518 } else if (streq(#s, name)) { \
1519 if (snprintf(s, sizeof(s), "%s", value) < 0) { \
1523 SET_STR(external_rules_command)
1524 SET_STR(status_prefix)
1526 } else if (streq("split_ratio", name)) {
1528 if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1) {
1531 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1535 #define SET_COLOR(s) \
1536 } else if (streq(#s, name)) { \
1537 if (!is_hex_color(value)) { \
1538 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1541 snprintf(s, sizeof(s), "%s", value); \
1542 colors_changed = true; \
1544 SET_COLOR(normal_border_color)
1545 SET_COLOR(active_border_color)
1546 SET_COLOR(focused_border_color)
1547 SET_COLOR(presel_feedback_color)
1549 } else if (streq("initial_polarity", name)) {
1551 if (parse_child_polarity(value, &p)) {
1552 initial_polarity = p;
1554 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1557 } else if (streq("directional_focus_tightness", name)) {
1559 if (parse_tightness(value, &p)) {
1560 directional_focus_tightness = p;
1562 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1565 } else if (streq("pointer_modifier", name)) {
1566 if (parse_modifier_mask(value, &pointer_modifier)) {
1570 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1573 } else if (streq("pointer_motion_interval", name)) {
1574 if (sscanf(value, "%u", &pointer_motion_interval) != 1) {
1575 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1578 } else if (streq("pointer_action1", name) ||
1579 streq("pointer_action2", name) ||
1580 streq("pointer_action3", name)) {
1581 int index = name[14] - '1';
1582 if (parse_pointer_action(value, &pointer_actions[index])) {
1586 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1589 } else if (streq("click_to_focus", name)) {
1590 if (parse_button_index(value, &click_to_focus)) {
1594 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1597 } else if (streq("focus_follows_pointer", name)) {
1599 if (parse_bool(value, &b)) {
1600 if (b == focus_follows_pointer) {
1604 focus_follows_pointer = b;
1605 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1606 if (focus_follows_pointer) {
1607 window_show(m->root);
1609 window_hide(m->root);
1611 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1612 listen_enter_notify(d->root, focus_follows_pointer);
1615 if (focus_follows_pointer) {
1616 update_motion_recorder();
1618 disable_motion_recorder();
1622 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value);
1625 #define SET_BOOL(s) \
1626 } else if (streq(#s, name)) { \
1627 if (!parse_bool(value, &s)) { \
1628 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1631 SET_BOOL(borderless_monocle)
1632 SET_BOOL(gapless_monocle)
1633 SET_BOOL(paddingless_monocle)
1634 SET_BOOL(single_monocle)
1635 SET_BOOL(swallow_first_click)
1636 SET_BOOL(pointer_follows_focus)
1637 SET_BOOL(pointer_follows_monitor)
1638 SET_BOOL(ignore_ewmh_focus)
1639 SET_BOOL(center_pseudo_tiled)
1640 SET_BOOL(honor_size_hints)
1642 #define SET_MON_BOOL(s) \
1643 } else if (streq(#s, name)) { \
1644 if (!parse_bool(value, &s)) { \
1645 fail(rsp, "config: %s: Invalid value: '%s'.\n", name, value); \
1649 update_monitors(); \
1651 SET_MON_BOOL(remove_disabled_monitors)
1652 SET_MON_BOOL(remove_unplugged_monitors)
1653 SET_MON_BOOL(merge_overlapping_monitors)
1656 fail(rsp, "config: Unknown setting: '%s'.\n", name);
1660 for (monitor_t *m = mon_head; m != NULL; m = m->next) {
1661 for (desktop_t *d = m->desk_head; d != NULL; d = d->next) {
1663 if (colors_changed) {
1664 update_colors_in(d->root, d, m);
1670 void get_setting(coordinates_t loc, char *name, FILE* rsp)
1672 if (streq("split_ratio", name)) {
1673 fprintf(rsp, "%lf", split_ratio);
1674 } else if (streq("border_width", name)) {
1675 if (loc.node != NULL) {
1676 for (node_t *n = first_extrema(loc.node); n != NULL; n = next_leaf(n, loc.node)) {
1677 if (n->client != NULL) {
1678 fprintf(rsp, "%u", n->client->border_width);
1682 } else if (loc.desktop != NULL) {
1683 fprintf(rsp, "%u", loc.desktop->border_width);
1684 } else if (loc.monitor != NULL) {
1685 fprintf(rsp, "%u", loc.monitor->border_width);
1687 fprintf(rsp, "%u", border_width);
1689 } else if (streq("window_gap", name)) {
1690 if (loc.desktop != NULL) {
1691 fprintf(rsp, "%i", loc.desktop->window_gap);
1692 } else if (loc.monitor != NULL) {
1693 fprintf(rsp, "%i", loc.monitor->window_gap);
1695 fprintf(rsp, "%i", window_gap);
1697 #define GET_DEF_MON_DESK(k) \
1698 if (loc.desktop != NULL) { \
1699 fprintf(rsp, "%i", loc.desktop->k); \
1700 } else if (loc.monitor != NULL) { \
1701 fprintf(rsp, "%i", loc.monitor->k); \
1703 fprintf(rsp, "%i", k); \
1705 } else if (streq("top_padding", name)) {
1706 GET_DEF_MON_DESK(padding.top)
1707 } else if (streq("right_padding", name)) {
1708 GET_DEF_MON_DESK(padding.right)
1709 } else if (streq("bottom_padding", name)) {
1710 GET_DEF_MON_DESK(padding.bottom)
1711 } else if (streq("left_padding", name)) {
1712 GET_DEF_MON_DESK(padding.left)
1713 #undef GET_DEF_MON_DESK
1714 } else if (streq("external_rules_command", name)) {
1715 fprintf(rsp, "%s", external_rules_command);
1716 } else if (streq("status_prefix", name)) {
1717 fprintf(rsp, "%s", status_prefix);
1718 } else if (streq("initial_polarity", name)) {
1719 fprintf(rsp, "%s", CHILD_POL_STR(initial_polarity));
1720 } else if (streq("directional_focus_tightness", name)) {
1721 fprintf(rsp, "%s", TIGHTNESS_STR(directional_focus_tightness));
1722 } else if (streq("pointer_modifier", name)) {
1723 print_modifier_mask(pointer_modifier, rsp);
1724 } else if (streq("click_to_focus", name)) {
1725 print_button_index(click_to_focus, rsp);
1726 } else if (streq("pointer_motion_interval", name)) {
1727 fprintf(rsp, "%u", pointer_motion_interval);
1728 } else if (streq("pointer_action1", name) ||
1729 streq("pointer_action2", name) ||
1730 streq("pointer_action3", name)) {
1731 int index = name[14] - '1';
1732 print_pointer_action(pointer_actions[index], rsp);
1733 #define GET_COLOR(s) \
1734 } else if (streq(#s, name)) { \
1735 fprintf(rsp, "%s", s);
1736 GET_COLOR(normal_border_color)
1737 GET_COLOR(active_border_color)
1738 GET_COLOR(focused_border_color)
1739 GET_COLOR(presel_feedback_color)
1741 #define GET_BOOL(s) \
1742 } else if (streq(#s, name)) { \
1743 fprintf(rsp, "%s", BOOL_STR(s));
1744 GET_BOOL(borderless_monocle)
1745 GET_BOOL(gapless_monocle)
1746 GET_BOOL(paddingless_monocle)
1747 GET_BOOL(single_monocle)
1748 GET_BOOL(swallow_first_click)
1749 GET_BOOL(focus_follows_pointer)
1750 GET_BOOL(pointer_follows_focus)
1751 GET_BOOL(pointer_follows_monitor)
1752 GET_BOOL(ignore_ewmh_focus)
1753 GET_BOOL(center_pseudo_tiled)
1754 GET_BOOL(honor_size_hints)
1755 GET_BOOL(remove_disabled_monitors)
1756 GET_BOOL(remove_unplugged_monitors)
1757 GET_BOOL(merge_overlapping_monitors)
1760 fail(rsp, "config: Unknown setting: '%s'.\n", name);
1766 void handle_failure(int code, char *src, char *val, FILE *rsp)
1769 case SELECTOR_BAD_DESCRIPTOR:
1770 fail(rsp, "%s: Invalid descriptor found in '%s'.\n", src, val);
1772 case SELECTOR_BAD_MODIFIERS:
1773 fail(rsp, "%s: Invalid modifier found in '%s'.\n", src, val);
1775 case SELECTOR_INVALID:
1781 void fail(FILE *rsp, char *fmt, ...)
1783 fprintf(rsp, FAILURE_MESSAGE);
1786 vfprintf(rsp, fmt, ap);