X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=messages.c;h=334fc3ad89a1f5ebc2a371f3dbd9bfd602d00da3;hb=d2abc2d20eb620f91342d0b64e57b45398f46948;hp=a40daba75323d13813704479b8697f54e7da43ed;hpb=16e73b6342d98bd40f0a679074931005d5de8ebc;p=bspwm.git diff --git a/messages.c b/messages.c index a40daba..c689b8e 100644 --- a/messages.c +++ b/messages.c @@ -1,910 +1,1245 @@ +/* Copyright (c) 2012-2014, Bastien Dejean + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those + * of the authors and should not be interpreted as representing official policies, + * either expressed or implied, of the FreeBSD Project. + */ + +#include #include -#include #include -#include "settings.h" -#include "messages.h" -#include "common.h" -#include "types.h" +#include #include "bspwm.h" +#include "desktop.h" #include "ewmh.h" -#include "helpers.h" -#include "window.h" -#include "events.h" +#include "history.h" +#include "monitor.h" +#include "pointer.h" +#include "query.h" +#include "rule.h" +#include "restore.h" +#include "settings.h" #include "tree.h" -#include "rules.h" +#include "window.h" +#include "common.h" +#include "subscribe.h" +#include "messages.h" -void process_message(char *msg, char *rsp) +int handle_message(char *msg, int msg_len, FILE *rsp) { - char *cmd = strtok(msg, TOK_SEP); - - if (cmd == NULL) - return; - - if (strcmp(cmd, "get") == 0) { - char *name = strtok(NULL, TOK_SEP); - get_setting(name, rsp); - } else if (strcmp(cmd, "set") == 0) { - char *name = strtok(NULL, TOK_SEP); - char *value = strtok(NULL, TOK_SEP); - set_setting(name, value, rsp); - } else if (strcmp(cmd, "list") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - desktop_location_t loc; - if (locate_desktop(name, &loc)) - list(loc.desktop, loc.desktop->root, rsp, 0); - } else { - list(mon->desk, mon->desk->root, rsp, 0); - } - } else if (strcmp(cmd, "list_monitors") == 0) { - char *arg = strtok(NULL, TOK_SEP); - list_option_t opt; - if (parse_list_option(arg, &opt)) - list_monitors(opt, rsp); - } else if (strcmp(cmd, "list_desktops") == 0) { - char *arg = strtok(NULL, TOK_SEP); - list_option_t opt; - if (parse_list_option(arg, &opt)) - list_desktops(mon, opt, 0, rsp); - } else if (strcmp(cmd, "list_windows") == 0) { - list_windows(rsp); - } else if (strcmp(cmd, "list_history") == 0) { - list_history(rsp); - } else if (strcmp(cmd, "list_rules") == 0) { - list_rules(rsp); - } else if (strcmp(cmd, "close") == 0) { - window_close(mon->desk->focus); - } else if (strcmp(cmd, "kill") == 0) { - window_kill(mon, mon->desk, mon->desk->focus); - } else if (strcmp(cmd, "rotate") == 0) { - char *deg = strtok(NULL, TOK_SEP); - if (deg != NULL) { - rotate_t r; - if (parse_rotate(deg, &r)) - rotate_tree(mon->desk->root, r); - } - arrange(mon, mon->desk); - } else if (strcmp(cmd, "flip") == 0) { - char *flp = strtok(NULL, TOK_SEP); - if (flp != NULL) { - flip_t f; - if (parse_flip(flp, &f)) - flip_tree(mon->desk->root, f); - } - arrange(mon, mon->desk); - } else if (strcmp(cmd, "balance") == 0) { - balance_tree(mon->desk->root); - arrange(mon, mon->desk); - } else if (strcmp(cmd, "grab_pointer") == 0) { - char *pac = strtok(NULL, TOK_SEP); - if (pac != NULL) { - pointer_action_t a; - if (parse_pointer_action(pac, &a)) - grab_pointer(a); - } - } else if (strcmp(cmd, "track_pointer") == 0) { - char *arg1 = strtok(NULL, TOK_SEP); - char *arg2 = strtok(NULL, TOK_SEP); - if (arg1 == NULL || arg2 == NULL) - return; - int root_x, root_y; - if (sscanf(arg1, "%i", &root_x) == 1 && sscanf(arg2, "%i", &root_y) == 1) - track_pointer(root_x, root_y); - } else if (strcmp(cmd, "layout") == 0) { - char *lyt = strtok(NULL, TOK_SEP); - if (lyt != NULL) { - layout_t l; - if (parse_layout(lyt, &l)) { - char *name = strtok(NULL, TOK_SEP); - if (name == NULL) { - change_layout(mon, mon->desk, l); - } else { - desktop_location_t loc; - do { - if (locate_desktop(name, &loc)) - change_layout(loc.monitor, loc.desktop, l); - } while ((name = strtok(NULL, TOK_SEP)) != NULL); - } - } - } - } else if (strcmp(cmd, "cycle_layout") == 0) { - if (mon->desk->layout == LAYOUT_MONOCLE) - change_layout(mon, mon->desk, LAYOUT_TILED); - else - change_layout(mon, mon->desk, LAYOUT_MONOCLE); - } else if (strcmp(cmd, "shift") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - direction_t d; - if (parse_direction(dir, &d)) { - node_t *n = nearest_neighbor(mon->desk, mon->desk->focus, d); - if (n != NULL) { - swap_nodes(mon->desk->focus, n, true); - arrange(mon, mon->desk); - } else if (monitor_focus_fallback) { - monitor_t *m = nearest_monitor(d); - if (m != NULL) - transfer_node(mon, mon->desk, m, m->desk, mon->desk->focus); - } - } - } - } else if (strcmp(cmd, "toggle_fullscreen") == 0) { - toggle_fullscreen(mon->desk, mon->desk->focus); - arrange(mon, mon->desk); - } else if (strcmp(cmd, "toggle_floating") == 0) { - toggle_floating(mon->desk, mon->desk->focus); - arrange(mon, mon->desk); - } else if (strcmp(cmd, "toggle_locked") == 0) { - toggle_locked(mon, mon->desk, mon->desk->focus); - } else if (strcmp(cmd, "toggle_visibility") == 0) { - toggle_visibility(); - } else if (strcmp(cmd, "pad") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - monitor_t *m = find_monitor(name); - if (m != NULL) { - char args[BUFSIZ] = {0}, *s; - while ((s = strtok(NULL, TOK_SEP)) != NULL) { - strncat(args, s, REMLEN(args)); - strncat(args, TOK_SEP, REMLEN(args)); - } - if (strlen(args) > 0) { - sscanf(args, "%i %i %i %i", &m->top_padding, &m->right_padding, &m->bottom_padding, &m->left_padding); - arrange(m, m->desk); - } else { - snprintf(rsp, BUFSIZ, "%i %i %i %i\n", m->top_padding, m->right_padding, m->bottom_padding, m->left_padding); - } - } - } - } else if (strcmp(cmd, "ratio") == 0) { - char *value; - if (mon->desk->focus != NULL && (value = strtok(NULL, TOK_SEP)) != NULL && - sscanf(value, "%lf", &mon->desk->focus->split_ratio) == 1) - window_draw_border(mon->desk->focus, true, true); - } else if (strcmp(cmd, "cancel") == 0) { - if (mon->desk->focus == NULL) - return; - char *opt = strtok(NULL, TOK_SEP); - cancel_option_t o; - if (parse_cancel_option(opt, &o)) - reset_mode(mon->desk, mon->desk->focus, o); - } else if (strcmp(cmd, "presel") == 0) { - if (mon->desk->focus == NULL || !is_tiled(mon->desk->focus->client) || mon->desk->layout != LAYOUT_TILED) - return; - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - direction_t d; - if (parse_direction(dir, &d)) { - char *rat = strtok(NULL, TOK_SEP); - double r = mon->desk->focus->split_ratio; - if (rat != NULL) - sscanf(rat, "%lf", &r); - if (auto_cancel && mon->desk->focus->split_mode == MODE_MANUAL - && d == mon->desk->focus->split_dir - && r == mon->desk->focus->split_ratio) { - reset_mode(mon->desk, mon->desk->focus, CANCEL_OPTION_FOCUSED); - } else { - mon->desk->focus->split_mode = MODE_MANUAL; - mon->desk->focus->split_dir = d; - mon->desk->focus->split_ratio = r; - } - window_draw_border(mon->desk->focus, true, true); - } - } - } else if (strcmp(cmd, "push") == 0 || strcmp(cmd, "pull") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - fence_move_t m; - direction_t d; - if (parse_fence_move(cmd, &m) && parse_direction(dir, &d)) { - move_fence(mon->desk->focus, d, m); - arrange(mon, mon->desk); - } - } - } else if (strcmp(cmd, "fence_ratio") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - direction_t d; - node_t *n; - if (parse_direction(dir, &d) && (n = find_fence(mon->desk->focus, d)) != NULL) { - char *value = strtok(NULL, TOK_SEP); - if (value != NULL && sscanf(value, "%lf", &n->split_ratio) == 1) - arrange(mon, mon->desk); - } - } - } else if (strcmp(cmd, "drop_to_monitor") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - cycle_dir_t d; - if (parse_cycle_direction(dir, &d)) { - monitor_t *m; - if (d == CYCLE_NEXT) - m = ((mon->next == NULL ? mon_head : mon->next)); - else - m = ((mon->prev == NULL ? mon_tail : mon->prev)); - transfer_node(mon, mon->desk, m, m->desk, mon->desk->focus); - char *opt = strtok(NULL, TOK_SEP); - send_option_t o; - if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW) - focus_node(m, m->desk, m->desk->focus); - } - } - } else if (strcmp(cmd, "send_to_monitor") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - monitor_t *m = find_monitor(name); - if (m != NULL && m != mon) { - transfer_node(mon, mon->desk, m, m->desk, mon->desk->focus); - char *opt = strtok(NULL, TOK_SEP); - send_option_t o; - if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW) - focus_node(m, m->desk, m->desk->focus); - } - } - } else if (strcmp(cmd, "drop_to") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - cycle_dir_t c; - if (parse_cycle_direction(dir, &c)) { - desktop_t *d; - if (c == CYCLE_NEXT) - d = ((mon->desk->next == NULL ? mon->desk_head : mon->desk->next)); - else - d = ((mon->desk->prev == NULL ? mon->desk_tail : mon->desk->prev)); - transfer_node(mon, mon->desk, mon, d, mon->desk->focus); - char *opt = strtok(NULL, TOK_SEP); - send_option_t o; - if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW) - focus_node(mon, d, d->focus); - } - } - } else if (strcmp(cmd, "send_to") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - desktop_location_t loc; - if (locate_desktop(name, &loc)) { - transfer_node(mon, mon->desk, loc.monitor, loc.desktop, mon->desk->focus); - char *opt = strtok(NULL, TOK_SEP); - send_option_t o; - if (parse_send_option(opt, &o) && o == SEND_OPTION_FOLLOW) - focus_node(loc.monitor, loc.desktop, loc.desktop->focus); - } - } - } else if (strcmp(cmd, "rename_monitor") == 0) { - char *cur_name = strtok(NULL, TOK_SEP); - if (cur_name != NULL) { - monitor_t *m = find_monitor(cur_name); - if (m != NULL) { - char *new_name = strtok(NULL, TOK_SEP); - if (new_name != NULL) { - strncpy(m->name, new_name, sizeof(m->name)); - put_status(); - } - } - } - } else if (strcmp(cmd, "rename") == 0) { - char *cur_name = strtok(NULL, TOK_SEP); - if (cur_name != NULL) { - desktop_location_t loc; - if (locate_desktop(cur_name, &loc)) { - char *new_name = strtok(NULL, TOK_SEP); - if (new_name != NULL) { - strncpy(loc.desktop->name, new_name, sizeof(loc.desktop->name)); - ewmh_update_desktop_names(); - put_status(); - } - } - } - } else if (strcmp(cmd, "use_monitor") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - monitor_t *m = find_monitor(name); - if (m != NULL) { - if (auto_alternate && m == mon && last_mon != NULL) - m = last_mon; - focus_node(m, m->desk, m->desk->focus); - } - } - } else if (strcmp(cmd, "focus_monitor") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - direction_t d; - if (parse_direction(dir, &d)) { - monitor_t *m = nearest_monitor(d); - if (m != NULL) - focus_node(m, m->desk, m->desk->focus); - } - } - } else if (strcmp(cmd, "use") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - desktop_location_t loc; - if (locate_desktop(name, &loc)) { - if (auto_alternate && loc.desktop == mon->desk && mon->last_desk != NULL) - focus_node(mon, mon->last_desk, mon->last_desk->focus); - else - focus_node(loc.monitor, loc.desktop, loc.desktop->focus); - } - } - } else if (strcmp(cmd, "cycle_monitor") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - cycle_dir_t d; - if (parse_cycle_direction(dir, &d)) - cycle_monitor(d); - } - } else if (strcmp(cmd, "cycle_desktop") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - cycle_dir_t d; - if (parse_cycle_direction(dir, &d)) { - skip_desktop_t k; - char *skip = strtok(NULL, TOK_SEP); - if (parse_skip_desktop(skip, &k)) - cycle_desktop(mon, mon->desk, d, k); - } - } - } else if (strcmp(cmd, "cycle") == 0) { - if (mon->desk->focus != NULL && mon->desk->focus->client->fullscreen) - return; - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - cycle_dir_t d; - if (parse_cycle_direction(dir, &d)) { - skip_client_t k; - char *skip = strtok(NULL, TOK_SEP); - if (parse_skip_client(skip, &k)) - cycle_leaf(mon, mon->desk, mon->desk->focus, d, k); - } - } - } else if (strcmp(cmd, "nearest") == 0) { - if (mon->desk->focus != NULL && mon->desk->focus->client->fullscreen) - return; - char *arg = strtok(NULL, TOK_SEP); - if (arg != NULL) { - nearest_arg_t a; - if (parse_nearest_argument(arg, &a)) { - skip_client_t k; - char *skip = strtok(NULL, TOK_SEP); - if (parse_skip_client(skip, &k)) - nearest_leaf(mon, mon->desk, mon->desk->focus, a, k); - } - } - } else if (strcmp(cmd, "biggest") == 0) { - node_t *n = find_biggest(mon->desk); - if (n != NULL) - snprintf(rsp, BUFSIZ, "0x%X", n->client->window); - } else if (strcmp(cmd, "circulate") == 0) { - if (mon->desk->layout == LAYOUT_MONOCLE - || (mon->desk->focus != NULL && !is_tiled(mon->desk->focus->client))) - return; - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - circulate_dir_t d; - if (parse_circulate_direction(dir, &d)) - circulate_leaves(mon, mon->desk, d); - } - arrange(mon, mon->desk); - } else if (strcmp(cmd, "rule") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - rule_t *rule = make_rule(); - strncpy(rule->cause.name, name, sizeof(rule->cause.name)); - char *arg = strtok(NULL, TOK_SEP); - while (arg != NULL) { - if (strcmp(arg, "floating") == 0) { - rule->effect.floating = true; - } else if (strcmp(arg, "follow") == 0) { - rule->effect.follow = true; - } else { - desktop_location_t loc; - if (locate_desktop(arg, &loc)) { - rule->effect.monitor = loc.monitor; - rule->effect.desktop = loc.desktop; - } - } - arg = strtok(NULL, TOK_SEP); - } - add_rule(rule); - } - } else if (strcmp(cmd, "remove_rule") == 0) { - char *arg; - unsigned int uid; - while ((arg = strtok(NULL, TOK_SEP)) != NULL) - if (sscanf(arg, "%X", &uid) > 0) - remove_rule_by_uid(uid); - } else if (strcmp(cmd, "swap") == 0) { - char *opt = strtok(NULL, TOK_SEP); - swap_option_t o; - if (!parse_swap_option(opt, &o)) - return; - node_t *last_focus = history_get(mon->desk->history, 1); - swap_nodes(mon->desk->focus, last_focus, true); - arrange(mon, mon->desk); - if (o == SWAP_OPTION_SWAP_FOCUS) - focus_node(mon, mon->desk, last_focus); - } else if (strcmp(cmd, "alternate") == 0) { - focus_node(mon, mon->desk, history_get(mon->desk->history, 1)); - } else if (strcmp(cmd, "alternate_desktop") == 0) { - if (mon->last_desk != NULL) - focus_node(mon, mon->last_desk, mon->last_desk->focus); - } else if (strcmp(cmd, "alternate_monitor") == 0) { - if (last_mon != NULL) - focus_node(last_mon, last_mon->desk, last_mon->desk->focus); - } else if (strcmp(cmd, "add_in") == 0) { - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - monitor_t *m = find_monitor(name); - if (m != NULL) - for (name = strtok(NULL, TOK_SEP); name != NULL; name = strtok(NULL, TOK_SEP)) - add_desktop(m, make_desktop(name)); - } - } else if (strcmp(cmd, "add") == 0) { - for (char *name = strtok(NULL, TOK_SEP); name != NULL; name = strtok(NULL, TOK_SEP)) - add_desktop(mon, make_desktop(name)); - } else if (strcmp(cmd, "remove_desktop") == 0) { - for (char *name = strtok(NULL, TOK_SEP); name != NULL; name = strtok(NULL, TOK_SEP)) { - desktop_location_t loc; - if (locate_desktop(name, &loc)) { - if (loc.desktop->root == NULL && loc.monitor->desk_head != loc.monitor->desk_tail) { - remove_desktop(loc.monitor, loc.desktop); - desktop_show(loc.monitor->desk); - } - } - } - update_current(); - } else if (strcmp(cmd, "send_desktop_to") == 0) { - if (mon->desk_head == mon->desk_tail) - return; - char *name = strtok(NULL, TOK_SEP); - if (name != NULL) { - monitor_t *m = find_monitor(name); - if (m != NULL && m != mon) { - char *opt = strtok(NULL, TOK_SEP); - send_option_t o; - if (!parse_send_option(opt, &o)) - return; - desktop_t *d = mon->desk; - transfer_desktop(mon, m, d); - if (o == SEND_OPTION_FOLLOW) - focus_node(m, d, d->focus); - else if (o == SEND_OPTION_DONT_FOLLOW) - update_current(); - } - } - } else if (strcmp(cmd, "focus") == 0) { - char *dir = strtok(NULL, TOK_SEP); - if (dir != NULL) { - direction_t d; - if (parse_direction(dir, &d)) { - node_t *n = nearest_neighbor(mon->desk, mon->desk->focus, d); - if (n != NULL) { - focus_node(mon, mon->desk, n); - } else if (monitor_focus_fallback) { - monitor_t *m = nearest_monitor(d); - if (m != NULL) - focus_node(m, m->desk, m->desk->focus); - } - } - } - } else if (strcmp(cmd, "put_status") == 0) { - put_status(); - } else if (strcmp(cmd, "adopt_orphans") == 0) { - adopt_orphans(); - } else if (strcmp(cmd, "restore_layout") == 0) { - char *arg = strtok(NULL, TOK_SEP); - restore_layout(arg); - } else if (strcmp(cmd, "restore_history") == 0) { - char *arg = strtok(NULL, TOK_SEP); - restore_history(arg); - } else if (strcmp(cmd, "quit") == 0) { - char *arg = strtok(NULL, TOK_SEP); - if (arg != NULL) - sscanf(arg, "%i", &exit_status); - quit(); - } else { - snprintf(rsp, BUFSIZ, "unknown command: %s", cmd); - } + int cap = INIT_CAP; + int num = 0; + char **args = malloc(cap * sizeof(char *)); + if (args == NULL) + return MSG_FAILURE; + + for (int i = 0, j = 0; i < msg_len; i++) { + if (msg[i] == 0) { + args[num++] = msg + j; + j = i + 1; + } + if (num >= cap) { + cap *= 2; + char **new = realloc(args, cap * sizeof(char *)); + if (new == NULL) { + free(args); + return MSG_FAILURE; + } else { + args = new; + } + } + } + + if (num < 1) { + free(args); + return MSG_SYNTAX; + } + + char **args_orig = args; + int ret = process_message(args, num, rsp); + free(args_orig); + return ret; } -void set_setting(char *name, char *value, char *rsp) +int process_message(char **args, int num, FILE *rsp) { - if (name == NULL || value == NULL) - return; - - if (strcmp(name, "border_width") == 0) { - sscanf(value, "%u", &border_width); - } else if (strcmp(name, "window_gap") == 0) { - sscanf(value, "%i", &window_gap); - } else if (strcmp(name, "split_ratio") == 0) { - sscanf(value, "%lf", &split_ratio); - } else if (strcmp(name, "left_padding") == 0) { - sscanf(value, "%i", &mon->left_padding); - } else if (strcmp(name, "right_padding") == 0) { - sscanf(value, "%i", &mon->right_padding); - } else if (strcmp(name, "top_padding") == 0) { - sscanf(value, "%i", &mon->top_padding); - } else if (strcmp(name, "bottom_padding") == 0) { - sscanf(value, "%i", &mon->bottom_padding); - } else if (strcmp(name, "focused_border_color") == 0) { - strncpy(focused_border_color, value, sizeof(focused_border_color)); - focused_border_color_pxl = get_color(focused_border_color); - } else if (strcmp(name, "active_border_color") == 0) { - strncpy(active_border_color, value, sizeof(active_border_color)); - active_border_color_pxl = get_color(active_border_color); - } else if (strcmp(name, "normal_border_color") == 0) { - strncpy(normal_border_color, value, sizeof(normal_border_color)); - normal_border_color_pxl = get_color(normal_border_color); - } else if (strcmp(name, "presel_border_color") == 0) { - strncpy(presel_border_color, value, sizeof(presel_border_color)); - presel_border_color_pxl = get_color(presel_border_color); - } else if (strcmp(name, "focused_locked_border_color") == 0) { - strncpy(focused_locked_border_color, value, sizeof(focused_locked_border_color)); - focused_locked_border_color_pxl = get_color(focused_locked_border_color); - } else if (strcmp(name, "active_locked_border_color") == 0) { - strncpy(active_locked_border_color, value, sizeof(active_locked_border_color)); - active_locked_border_color_pxl = get_color(active_locked_border_color); - } else if (strcmp(name, "normal_locked_border_color") == 0) { - strncpy(normal_locked_border_color, value, sizeof(normal_locked_border_color)); - normal_locked_border_color_pxl = get_color(normal_locked_border_color); - } else if (strcmp(name, "urgent_border_color") == 0) { - strncpy(urgent_border_color, value, sizeof(urgent_border_color)); - urgent_border_color_pxl = get_color(urgent_border_color); - } else if (strcmp(name, "borderless_monocle") == 0) { - bool b; - if (parse_bool(value, &b)) - borderless_monocle = b; - } else if (strcmp(name, "gapless_monocle") == 0) { - bool b; - if (parse_bool(value, &b)) - gapless_monocle = b; - } else if (strcmp(name, "focus_follows_pointer") == 0) { - bool b; - if (parse_bool(value, &b) && b != focus_follows_pointer) { - uint32_t values[] = {(focus_follows_pointer ? CLIENT_EVENT_MASK : CLIENT_EVENT_MASK_FFP)}; - for (monitor_t *m = mon_head; m != NULL; m = m->next) - for (desktop_t *d = m->desk_head; d != NULL; d = d->next) - for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) - xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values); - if (focus_follows_pointer) - disable_motion_recorder(); - else - enable_motion_recorder(); - focus_follows_pointer = b; - } - return; - } else if (strcmp(name, "pointer_follows_monitor") == 0) { - bool b; - if (parse_bool(value, &b)) - pointer_follows_monitor = b; - return; - } else if (strcmp(name, "monitor_focus_fallback") == 0) { - bool b; - if (parse_bool(value, &b)) - monitor_focus_fallback = b; - return; - } else if (strcmp(name, "adaptative_raise") == 0) { - bool b; - if (parse_bool(value, &b)) - adaptative_raise = b; - return; - } else if (strcmp(name, "apply_shadow_property") == 0) { - bool b; - if (parse_bool(value, &b)) - apply_shadow_property = b; - return; - } else if (strcmp(name, "auto_alternate") == 0) { - bool b; - if (parse_bool(value, &b)) - auto_alternate = b; - return; - } else if (strcmp(name, "auto_cancel") == 0) { - bool b; - if (parse_bool(value, &b)) - auto_cancel = b; - return; - } else if (strcmp(name, "focus_by_distance") == 0) { - bool b; - if (parse_bool(value, &b)) - focus_by_distance = b; - return; - } else if (strcmp(name, "history_aware_focus") == 0) { - bool b; - if (parse_bool(value, &b)) - history_aware_focus = b; - return; - } else if (strcmp(name, "wm_name") == 0) { - strncpy(wm_name, value, sizeof(wm_name)); - ewmh_update_wm_name(); - return; - } else { - snprintf(rsp, BUFSIZ, "unknown setting: %s", name); - return; - } - - for (monitor_t *m = mon_head; m != NULL; m = m->next) - for (desktop_t *d = m->desk_head; d != NULL; d = d->next) - arrange(m, d); + if (streq("window", *args)) { + return cmd_window(++args, --num); + } else if (streq("desktop", *args)) { + return cmd_desktop(++args, --num); + } else if (streq("monitor", *args)) { + return cmd_monitor(++args, --num); + } else if (streq("query", *args)) { + return cmd_query(++args, --num, rsp); + } else if (streq("restore", *args)) { + return cmd_restore(++args, --num); + } else if (streq("control", *args)) { + return cmd_control(++args, --num, rsp); + } else if (streq("rule", *args)) { + return cmd_rule(++args, --num, rsp); + } else if (streq("pointer", *args)) { + return cmd_pointer(++args, --num); + } else if (streq("config", *args)) { + return cmd_config(++args, --num, rsp); + } else if (streq("quit", *args)) { + return cmd_quit(++args, --num); + } + + return MSG_UNKNOWN; } -void get_setting(char *name, char* rsp) +int cmd_window(char **args, int num) { - if (name == NULL) - return; - - if (strcmp(name, "border_width") == 0) - snprintf(rsp, BUFSIZ, "%u", border_width); - else if (strcmp(name, "window_gap") == 0) - snprintf(rsp, BUFSIZ, "%i", window_gap); - else if (strcmp(name, "split_ratio") == 0) - snprintf(rsp, BUFSIZ, "%lf", split_ratio); - else if (strcmp(name, "left_padding") == 0) - snprintf(rsp, BUFSIZ, "%i", mon->left_padding); - else if (strcmp(name, "right_padding") == 0) - snprintf(rsp, BUFSIZ, "%i", mon->right_padding); - else if (strcmp(name, "top_padding") == 0) - snprintf(rsp, BUFSIZ, "%i", mon->top_padding); - else if (strcmp(name, "bottom_padding") == 0) - snprintf(rsp, BUFSIZ, "%i", mon->bottom_padding); - else if (strcmp(name, "focused_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", focused_border_color, focused_border_color_pxl); - else if (strcmp(name, "active_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", active_border_color, active_border_color_pxl); - else if (strcmp(name, "normal_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", normal_border_color, normal_border_color_pxl); - else if (strcmp(name, "presel_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", presel_border_color, presel_border_color_pxl); - else if (strcmp(name, "focused_locked_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", focused_locked_border_color, focused_locked_border_color_pxl); - else if (strcmp(name, "active_locked_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", active_locked_border_color, active_locked_border_color_pxl); - else if (strcmp(name, "normal_locked_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", normal_locked_border_color, normal_locked_border_color_pxl); - else if (strcmp(name, "urgent_border_color") == 0) - snprintf(rsp, BUFSIZ, "%s (%06X)", urgent_border_color, urgent_border_color_pxl); - else if (strcmp(name, "borderless_monocle") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(borderless_monocle)); - else if (strcmp(name, "gapless_monocle") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(gapless_monocle)); - else if (strcmp(name, "focus_follows_pointer") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(focus_follows_pointer)); - else if (strcmp(name, "pointer_follows_monitor") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(pointer_follows_monitor)); - else if (strcmp(name, "monitor_focus_fallback") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(monitor_focus_fallback)); - else if (strcmp(name, "adaptative_raise") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(adaptative_raise)); - else if (strcmp(name, "apply_shadow_property") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(apply_shadow_property)); - else if (strcmp(name, "auto_alternate") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(auto_alternate)); - else if (strcmp(name, "auto_cancel") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(auto_cancel)); - else if (strcmp(name, "focus_by_distance") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(focus_by_distance)); - else if (strcmp(name, "history_aware_focus") == 0) - snprintf(rsp, BUFSIZ, "%s", BOOLSTR(history_aware_focus)); - else if (strcmp(name, "wm_name") == 0) - snprintf(rsp, BUFSIZ, "%s", wm_name); - else - snprintf(rsp, BUFSIZ, "unknown setting: %s", name); + if (num < 1) + return MSG_SYNTAX; + + coordinates_t ref = {mon, mon->desk, mon->desk->focus}; + coordinates_t trg = ref; + + if ((*args)[0] != OPT_CHR) { + if (node_from_desc(*args, &ref, &trg)) + num--, args++; + else + return MSG_FAILURE; + } + + if (trg.node == NULL) + return MSG_FAILURE; + + bool dirty = false; + + while (num > 0) { + if (streq("-f", *args) || streq("--focus", *args)) { + coordinates_t dst = trg; + if (num > 1 && *(args + 1)[0] != OPT_CHR) { + num--, args++; + if (!node_from_desc(*args, &trg, &dst)) + return MSG_FAILURE; + } + focus_node(dst.monitor, dst.desktop, dst.node); + } else if (streq("-d", *args) || streq("--to-desktop", *args)) { + num--, args++; + coordinates_t dst; + if (desktop_from_desc(*args, &trg, &dst)) { + if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.desktop->focus)) { + trg.monitor = dst.monitor; + trg.desktop = dst.desktop; + } + } else { + return MSG_FAILURE; + } + } else if (streq("-m", *args) || streq("--to-monitor", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + coordinates_t dst; + if (monitor_from_desc(*args, &trg, &dst)) { + if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.monitor->desk, dst.monitor->desk->focus)) { + trg.monitor = dst.monitor; + trg.desktop = dst.monitor->desk; + } + } else { + return MSG_FAILURE; + } + } else if (streq("-w", *args) || streq("--to-window", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + coordinates_t dst; + if (node_from_desc(*args, &trg, &dst)) { + if (transfer_node(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) { + trg.monitor = dst.monitor; + trg.desktop = dst.desktop; + } + } else { + return MSG_FAILURE; + } + } else if (streq("-s", *args) || streq("--swap", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + coordinates_t dst; + if (node_from_desc(*args, &trg, &dst)) { + if (swap_nodes(trg.monitor, trg.desktop, trg.node, dst.monitor, dst.desktop, dst.node)) { + if (trg.desktop != dst.desktop) + arrange(trg.monitor, trg.desktop); + trg.monitor = dst.monitor; + trg.desktop = dst.desktop; + dirty = true; + } + } else { + return MSG_FAILURE; + } + } else if (streq("-t", *args) || streq("--toggle", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + char *key = strtok(*args, EQL_TOK); + char *val = strtok(NULL, EQL_TOK); + alter_state_t a; + bool b; + if (val == NULL) { + a = ALTER_TOGGLE; + } else { + if (parse_bool(val, &b)) + a = ALTER_SET; + else + return MSG_FAILURE; + } + if (streq("fullscreen", key)) { + set_fullscreen(trg.node, (a == ALTER_SET ? b : !trg.node->client->fullscreen)); + dirty = true; + } else if (streq("pseudo_tiled", key)) { + set_pseudo_tiled(trg.node, (a == ALTER_SET ? b : !trg.node->client->pseudo_tiled)); + dirty = true; + } else if (streq("floating", key)) { + set_floating(trg.node, (a == ALTER_SET ? b : !trg.node->client->floating)); + dirty = true; + } else if (streq("locked", key)) { + set_locked(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->locked)); + } else if (streq("sticky", key)) { + set_sticky(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->sticky)); + } else if (streq("private", key)) { + set_private(trg.monitor, trg.desktop, trg.node, (a == ALTER_SET ? b : !trg.node->client->private)); + } else { + return MSG_FAILURE; + } + } else if (streq("-p", *args) || streq("--presel", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + if (!is_tiled(trg.node->client) || + trg.desktop->layout != LAYOUT_TILED) + return MSG_FAILURE; + if (streq("cancel", *args)) { + reset_mode(&trg); + } else { + direction_t dir; + if (parse_direction(*args, &dir)) { + double rat = trg.node->split_ratio; + if (num > 1 && *(args + 1)[0] != OPT_CHR) { + num--, args++; + if (sscanf(*args, "%lf", &rat) != 1 || rat <= 0 || rat >= 1) + return MSG_FAILURE; + } + if (auto_cancel && trg.node->split_mode == MODE_MANUAL && + dir == trg.node->split_dir && + rat == trg.node->split_ratio) { + reset_mode(&trg); + } else { + trg.node->split_mode = MODE_MANUAL; + trg.node->split_dir = dir; + trg.node->split_ratio = rat; + } + window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor); + } else { + return MSG_FAILURE; + } + } + } else if (streq("-e", *args) || streq("--edge", *args)) { + num--, args++; + if (num < 2) + return MSG_SYNTAX; + direction_t dir; + if (!parse_direction(*args, &dir)) + return MSG_FAILURE; + node_t *n = find_fence(trg.node, dir); + if (n == NULL) + return MSG_FAILURE; + num--, args++; + if ((*args)[0] == '+' || (*args)[0] == '-') { + int pix; + if (sscanf(*args, "%i", &pix) == 1) { + int max = (n->split_type == TYPE_HORIZONTAL ? n->rectangle.height : n->rectangle.width); + double rat = ((max * n->split_ratio) + pix) / max; + if (rat > 0 && rat < 1) + n->split_ratio = rat; + else + return MSG_FAILURE; + } else { + return MSG_FAILURE; + } + } else { + double rat; + if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) + n->split_ratio = rat; + else + return MSG_FAILURE; + } + dirty = true; + } else if (streq("-r", *args) || streq("--ratio", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + double rat; + if (sscanf(*args, "%lf", &rat) == 1 && rat > 0 && rat < 1) { + trg.node->split_ratio = rat; + window_draw_border(trg.node, trg.desktop->focus == trg.node, mon == trg.monitor); + } else { + return MSG_FAILURE; + } + } else if (streq("-R", *args) || streq("--rotate", *args)) { + num--, args++; + if (num < 2) + return MSG_SYNTAX; + direction_t dir; + if (!parse_direction(*args, &dir)) + return MSG_FAILURE; + node_t *n = find_fence(trg.node, dir); + if (n == NULL) + return MSG_FAILURE; + num--, args++; + int deg; + if (parse_degree(*args, °)) { + rotate_tree(n, deg); + dirty = true; + } else { + return MSG_FAILURE; + } + } else if (streq("-c", *args) || streq("--close", *args)) { + if (num > 1) + return MSG_SYNTAX; + window_close(trg.node); + } else if (streq("-k", *args) || streq("--kill", *args)) { + if (num > 1) + return MSG_SYNTAX; + window_kill(trg.monitor, trg.desktop, trg.node); + dirty = true; + } else { + return MSG_SYNTAX; + } + + num--, args++; + } + + if (dirty) + arrange(trg.monitor, trg.desktop); + + return MSG_SUCCESS; } -bool parse_bool(char *value, bool *b) +int cmd_desktop(char **args, int num) { - if (strcmp(value, "true") == 0) { - *b = true; - return true; - } else if (strcmp(value, "false") == 0) { - *b = false; - return true; - } - return false; + if (num < 1) + return MSG_SYNTAX; + + coordinates_t ref = {mon, mon->desk, NULL}; + coordinates_t trg = ref; + + if ((*args)[0] != OPT_CHR) { + if (desktop_from_desc(*args, &ref, &trg)) + num--, args++; + else + return MSG_FAILURE; + } + + bool dirty = false; + + while (num > 0) { + if (streq("-f", *args) || streq("--focus", *args)) { + coordinates_t dst = trg; + if (num > 1 && *(args + 1)[0] != OPT_CHR) { + num--, args++; + if (!desktop_from_desc(*args, &trg, &dst)) + return MSG_FAILURE; + } + if (auto_alternate && dst.desktop == mon->desk) { + desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false}; + history_find_desktop(HISTORY_OLDER, &trg, &dst, sel); + } + focus_node(dst.monitor, dst.desktop, dst.desktop->focus); + } else if (streq("-m", *args) || streq("--to-monitor", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + if (trg.monitor->desk_head == trg.monitor->desk_tail) + return MSG_FAILURE; + coordinates_t dst; + if (monitor_from_desc(*args, &trg, &dst)) { + transfer_desktop(trg.monitor, dst.monitor, trg.desktop); + trg.monitor = dst.monitor; + update_current(); + } else { + return MSG_FAILURE; + } + } else if (streq("-s", *args) || streq("--swap", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + coordinates_t dst; + if (desktop_from_desc(*args, &trg, &dst)) + swap_desktops(trg.monitor, trg.desktop, dst.monitor, dst.desktop); + else + return MSG_FAILURE; + } else if (streq("-l", *args) || streq("--layout", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + layout_t lyt; + cycle_dir_t cyc; + if (parse_cycle_direction(*args, &cyc)) + change_layout(trg.monitor, trg.desktop, (trg.desktop->layout + 1) % 2); + else if (parse_layout(*args, &lyt)) + change_layout(trg.monitor, trg.desktop, lyt); + else + return MSG_FAILURE; + } else if (streq("-n", *args) || streq("--rename", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + snprintf(trg.desktop->name, sizeof(trg.desktop->name), "%s", *args); + ewmh_update_desktop_names(); + put_status(); + } else if (streq("-r", *args) || streq("--remove", *args)) { + if (trg.desktop->root == NULL && + trg.monitor->desk_head != trg.monitor->desk_tail) { + remove_desktop(trg.monitor, trg.desktop); + show_desktop(trg.monitor->desk); + update_current(); + return MSG_SUCCESS; + } else { + return MSG_FAILURE; + } + } else if (streq("-c", *args) || streq("--cancel-presel", *args)) { + reset_mode(&trg); + } else if (streq("-F", *args) || streq("--flip", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + flip_t flp; + if (parse_flip(*args, &flp)) { + flip_tree(trg.desktop->root, flp); + dirty = true; + } else { + return MSG_FAILURE; + } + } else if (streq("-R", *args) || streq("--rotate", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + int deg; + if (parse_degree(*args, °)) { + rotate_tree(trg.desktop->root, deg); + dirty = true; + } else { + return MSG_FAILURE; + } + } else if (streq("-E", *args) || streq("--equalize", *args)) { + equalize_tree(trg.desktop->root); + dirty = true; + } else if (streq("-B", *args) || streq("--balance", *args)) { + balance_tree(trg.desktop->root); + dirty = true; + } else if (streq("-C", *args) || streq("--circulate", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + circulate_dir_t cir; + if (parse_circulate_direction(*args, &cir)) { + circulate_leaves(trg.monitor, trg.desktop, cir); + dirty = true; + } else { + return MSG_FAILURE; + } + } else if (streq("-t", *args) || streq("--toggle", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + char *key = strtok(*args, EQL_TOK); + char *val = strtok(NULL, EQL_TOK); + alter_state_t a; + bool b; + if (val == NULL) { + a = ALTER_TOGGLE; + } else { + if (parse_bool(val, &b)) + a = ALTER_SET; + else + return MSG_FAILURE; + } + if (streq("floating", key)) + trg.desktop->floating = (a == ALTER_SET ? b : !trg.desktop->floating); + else + return MSG_FAILURE; + } else { + return MSG_SYNTAX; + } + num--, args++; + } + + if (dirty) + arrange(trg.monitor, trg.desktop); + + return MSG_SUCCESS; } -bool parse_layout(char *s, layout_t *l) +int cmd_monitor(char **args, int num) { - if (strcmp(s, "monocle") == 0) { - *l = LAYOUT_MONOCLE; - return true; - } else if (strcmp(s, "tiled") == 0) { - *l = LAYOUT_TILED; - return true; - } - return false; + if (num < 1) + return MSG_SYNTAX; + + coordinates_t ref = {mon, NULL, NULL}; + coordinates_t trg = ref; + + if ((*args)[0] != OPT_CHR) { + if (monitor_from_desc(*args, &ref, &trg)) + num--, args++; + else + return MSG_FAILURE; + } + + while (num > 0) { + if (streq("-f", *args) || streq("--focus", *args)) { + coordinates_t dst = trg; + if (num > 1 && *(args + 1)[0] != OPT_CHR) { + num--, args++; + if (!monitor_from_desc(*args, &trg, &dst)) + return MSG_FAILURE; + } + if (auto_alternate && dst.monitor == mon) { + desktop_select_t sel = {DESKTOP_STATUS_ALL, false, false}; + history_find_monitor(HISTORY_OLDER, &trg, &dst, sel); + } + focus_node(dst.monitor, dst.monitor->desk, dst.monitor->desk->focus); + } else if (streq("-d", *args) || streq("--reset-desktops", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + desktop_t *d = trg.monitor->desk_head; + while (num > 0 && d != NULL) { + snprintf(d->name, sizeof(d->name), "%s", *args); + d = d->next; + num--, args++; + } + put_status(); + while (num > 0) { + add_desktop(trg.monitor, make_desktop(*args)); + num--, args++; + } + while (d != NULL) { + desktop_t *next = d->next; + if (d == mon->desk) + focus_node(trg.monitor, d->prev, d->prev->focus); + merge_desktops(trg.monitor, d, mon, mon->desk); + remove_desktop(trg.monitor, d); + d = next; + } + } else if (streq("-a", *args) || streq("--add-desktops", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + while (num > 0) { + add_desktop(trg.monitor, make_desktop(*args)); + num--, args++; + } + } else if (streq("-r", *args) || streq("--remove-desktops", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + while (num > 0) { + coordinates_t dst; + if (locate_desktop(*args, &dst) && dst.monitor->desk_head != dst.monitor->desk_tail && dst.desktop->root == NULL) { + remove_desktop(dst.monitor, dst.desktop); + show_desktop(dst.monitor->desk); + } + num--, args++; + } + } else if (streq("-o", *args) || streq("--order-desktops", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + desktop_t *d = trg.monitor->desk_head; + while (d != NULL && num > 0) { + desktop_t *next = d->next; + coordinates_t dst; + if (locate_desktop(*args, &dst) && dst.monitor == trg.monitor) { + swap_desktops(trg.monitor, d, dst.monitor, dst.desktop); + if (next == dst.desktop) + next = d; + } + d = next; + num--, args++; + } + } else if (streq("-n", *args) || streq("--rename", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + snprintf(trg.monitor->name, sizeof(trg.monitor->name), "%s", *args); + put_status(); + } else if (streq("-s", *args) || streq("--swap", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + coordinates_t dst; + if (monitor_from_desc(*args, &trg, &dst)) + swap_monitors(trg.monitor, dst.monitor); + else + return MSG_FAILURE; + } else { + return MSG_SYNTAX; + } + num--, args++; + } + + return MSG_SUCCESS; } -bool parse_direction(char *s, direction_t *d) +int cmd_query(char **args, int num, FILE *rsp) { - if (strcmp(s, "up") == 0) { - *d = DIR_UP; - return true; - } else if (strcmp(s, "down") == 0) { - *d = DIR_DOWN; - return true; - } else if (strcmp(s, "left") == 0) { - *d = DIR_LEFT; - return true; - } else if (strcmp(s, "right") == 0) { - *d = DIR_RIGHT; - return true; - } - return false; + coordinates_t ref = {mon, mon->desk, mon->desk->focus}; + coordinates_t trg = {NULL, NULL, NULL}; + domain_t dom = DOMAIN_TREE; + int d = 0, t = 0; + + while (num > 0) { + if (streq("-T", *args) || streq("--tree", *args)) { + dom = DOMAIN_TREE, d++; + } else if (streq("-M", *args) || streq("--monitors", *args)) { + dom = DOMAIN_MONITOR, d++; + } else if (streq("-D", *args) || streq("--desktops", *args)) { + dom = DOMAIN_DESKTOP, d++; + } else if (streq("-W", *args) || streq("--windows", *args)) { + dom = DOMAIN_WINDOW, d++; + } else if (streq("-H", *args) || streq("--history", *args)) { + dom = DOMAIN_HISTORY, d++; + } else if (streq("-S", *args) || streq("--stack", *args)) { + dom = DOMAIN_STACK, d++; + } else if (streq("-m", *args) || streq("--monitor", *args)) { + trg.monitor = ref.monitor; + if (num > 1 && *(args + 1)[0] != OPT_CHR) { + num--, args++; + if (!monitor_from_desc(*args, &ref, &trg)) + return MSG_FAILURE; + } + t++; + } else if (streq("-d", *args) || streq("--desktop", *args)) { + trg.monitor = ref.monitor; + trg.desktop = ref.desktop; + if (num > 1 && *(args + 1)[0] != OPT_CHR) { + num--, args++; + if (!desktop_from_desc(*args, &ref, &trg)) + return MSG_FAILURE; + } + t++; + } else if (streq("-w", *args) || streq("--window", *args)) { + trg = ref; + if (num > 1 && *(args + 1)[0] != OPT_CHR) { + num--, args++; + if (!node_from_desc(*args, &ref, &trg)) + return MSG_FAILURE; + } + t++; + } else { + return MSG_SYNTAX; + } + num--, args++; + } + + if (d != 1 || t > 1) + return MSG_SYNTAX; + + if (dom == DOMAIN_HISTORY) + query_history(trg, rsp); + else if (dom == DOMAIN_STACK) + query_stack(rsp); + else if (dom == DOMAIN_WINDOW) + query_windows(trg, rsp); + else + query_monitors(trg, dom, rsp); + + return MSG_SUCCESS; } -bool parse_nearest_argument(char *s, nearest_arg_t *a) +int cmd_rule(char **args, int num, FILE *rsp) { - if (strcmp(s, "older") == 0) { - *a = NEAREST_OLDER; - return true; - } else if (strcmp(s, "newer") == 0) { - *a = NEAREST_NEWER; - return true; - } - return false; + if (num < 1) + return MSG_SYNTAX; + while (num > 0) { + if (streq("-a", *args) || streq("--add", *args)) { + num--, args++; + if (num < 2) + return MSG_SYNTAX; + rule_t *rule = make_rule(); + snprintf(rule->cause, sizeof(rule->cause), "%s", *args); + num--, args++; + size_t i = 0; + while (num > 0) { + if (streq("-o", *args) || streq("--one-shot", *args)) { + rule->one_shot = true; + } else { + for (size_t j = 0; i < sizeof(rule->effect) && j < strlen(*args); i++, j++) + rule->effect[i] = (*args)[j]; + if (num > 1 && i < sizeof(rule->effect)) + rule->effect[i++] = ' '; + } + num--, args++; + } + rule->effect[MIN(i, sizeof(rule->effect) - 1)] = '\0'; + add_rule(rule); + } else if (streq("-r", *args) || streq("--remove", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + int idx; + while (num > 0) { + if (parse_index(*args, &idx)) + remove_rule_by_index(idx - 1); + else if (streq("tail", *args)) + remove_rule(rule_tail); + else if (streq("head", *args)) + remove_rule(rule_head); + else + remove_rule_by_cause(*args); + num--, args++; + } + } else if (streq("-l", *args) || streq("--list", *args)) { + num--, args++; + list_rules(num > 0 ? *args : NULL, rsp); + } else { + return MSG_SYNTAX; + } + num--, args++; + } + + return MSG_SUCCESS; } -bool parse_cycle_direction(char *s, cycle_dir_t *d) +int cmd_pointer(char **args, int num) { - if (strcmp(s, "prev") == 0) { - *d = CYCLE_PREV; - return true; - } else if (strcmp(s, "next") == 0) { - *d = CYCLE_NEXT; - return true; - } - return false; + if (num < 1) + return MSG_SYNTAX; + while (num > 0) { + if (streq("-t", *args) || streq("--track", *args)) { + num--, args++; + if (num < 2) + return MSG_SYNTAX; + int x, y; + if (sscanf(*args, "%i", &x) == 1 && sscanf(*(args + 1), "%i", &y) == 1) + track_pointer(x, y); + else + return MSG_FAILURE; + num--, args++; + } else if (streq("-g", *args) || streq("--grab", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + pointer_action_t pac; + if (parse_pointer_action(*args, &pac)) + grab_pointer(pac); + else + return MSG_FAILURE; + } else if (streq("-u", *args) || streq("--ungrab", *args)) { + ungrab_pointer(); + } else { + return MSG_SYNTAX; + } + num--, args++; + } + + return MSG_SUCCESS; } -bool parse_circulate_direction(char *s, circulate_dir_t *d) +int cmd_restore(char **args, int num) { - if (strcmp(s, "forward") == 0) { - *d = CIRCULATE_FORWARD; - return true; - } else if (strcmp(s, "backward") == 0) { - *d = CIRCULATE_BACKWARD; - return true; - } - return false; + if (num < 1) + return MSG_SYNTAX; + while (num > 0) { + if (streq("-T", *args) || streq("--tree", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + restore_tree(*args); + } else if (streq("-H", *args) || streq("--history", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + restore_history(*args); + } else if (streq("-S", *args) || streq("--stack", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + restore_stack(*args); + } else { + return MSG_SYNTAX; + } + num--, args++; + } + + return MSG_SUCCESS; } -bool parse_skip_client(char *s, skip_client_t *k) +int cmd_control(char **args, int num, FILE *rsp) { - if (s == NULL) { - *k = CLIENT_SKIP_NONE; - return true; - } else if (strcmp(s, "--skip-floating") == 0) { - *k = CLIENT_SKIP_FLOATING; - return true; - } else if (strcmp(s, "--skip-tiled") == 0) { - *k = CLIENT_SKIP_TILED; - return true; - } else if (strcmp(s, "--skip-class-equal") == 0) { - *k = CLIENT_SKIP_CLASS_EQUAL; - return true; - } else if (strcmp(s, "--skip-class-differ") == 0) { - *k = CLIENT_SKIP_CLASS_DIFFER; - return true; - } - return false; + if (num < 1) + return MSG_SYNTAX; + while (num > 0) { + if (streq("--adopt-orphans", *args)) { + adopt_orphans(); + } else if (streq("--toggle-visibility", *args)) { + toggle_visibility(); + } else if (streq("--subscribe", *args)) { + return MSG_SUBSCRIBE; + } else if (streq("--get-status", *args)) { + print_status(rsp); + } else if (streq("--record-history", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + bool b; + if (parse_bool(*args, &b)) + record_history = b; + else + return MSG_SYNTAX; + } else { + return MSG_SYNTAX; + } + num--, args++; + } + + return MSG_SUCCESS; } -bool parse_skip_desktop(char *s, skip_desktop_t *k) +int cmd_config(char **args, int num, FILE *rsp) { - if (s == NULL) { - *k = DESKTOP_SKIP_NONE; - return true; - } else if (strcmp(s, "--skip-free") == 0) { - *k = DESKTOP_SKIP_FREE; - return true; - } else if (strcmp(s, "--skip-occupied") == 0) { - *k = DESKTOP_SKIP_OCCUPIED; - return true; - } - return false; + if (num < 1) + return MSG_SYNTAX; + coordinates_t ref = {mon, mon->desk, mon->desk->focus}; + coordinates_t trg = {NULL, NULL, NULL}; + if ((*args)[0] == OPT_CHR) { + if (streq("-m", *args) || streq("--monitor", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + if (!monitor_from_desc(*args, &ref, &trg)) + return MSG_FAILURE; + } else if (streq("-d", *args) || streq("--desktop", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + if (!desktop_from_desc(*args, &ref, &trg)) + return MSG_FAILURE; + } else if (streq("-w", *args) || streq("--window", *args)) { + num--, args++; + if (num < 1) + return MSG_SYNTAX; + if (!node_from_desc(*args, &ref, &trg)) + return MSG_FAILURE; + } else { + return MSG_SYNTAX; + } + num--, args++; + } + if (num == 2) + return set_setting(trg, *args, *(args + 1)); + else if (num == 1) + return get_setting(trg, *args, rsp); + else + return MSG_SYNTAX; } -bool parse_list_option(char *s, list_option_t *o) +int cmd_quit(char **args, int num) { - if (s == NULL) { - *o = LIST_OPTION_VERBOSE; - return true; - } else if (strcmp(s, "--quiet") == 0) { - *o = LIST_OPTION_QUIET; - return true; - } - return false; + if (num > 0 && sscanf(*args, "%i", &exit_status) != 1) + return MSG_FAILURE; + running = false; + return MSG_SUCCESS; } -bool parse_send_option(char *s, send_option_t *o) +int set_setting(coordinates_t loc, char *name, char *value) { - if (s == NULL) { - *o = SEND_OPTION_DONT_FOLLOW; - return true; - } else if (strcmp(s, "--follow") == 0) { - *o = SEND_OPTION_FOLLOW; - return true; - } - return false; +#define DESKWINSET(k, v) \ + if (loc.node != NULL) \ + loc.node->client->k = v; \ + else if (loc.desktop != NULL) \ + loc.desktop->k = v; \ + else if (loc.monitor != NULL) \ + for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \ + d->k = v; \ + else \ + for (monitor_t *m = mon_head; m != NULL; m = m->next) \ + for (desktop_t *d = m->desk_head; d != NULL; d = d->next) \ + d->k = v; + if (streq("border_width", name)) { + unsigned int bw; + if (sscanf(value, "%u", &bw) != 1) + return MSG_FAILURE; + DESKWINSET(border_width, bw) +#undef DESKWINSET +#define DESKSET(k, v) \ + if (loc.desktop != NULL) \ + loc.desktop->k = v; \ + else if (loc.monitor != NULL) \ + for (desktop_t *d = loc.monitor->desk_head; d != NULL; d = d->next) \ + d->k = v; \ + else \ + for (monitor_t *m = mon_head; m != NULL; m = m->next) \ + for (desktop_t *d = m->desk_head; d != NULL; d = d->next) \ + d->k = v; + } else if (streq("window_gap", name)) { + int wg; + if (sscanf(value, "%i", &wg) != 1) + return MSG_FAILURE; + DESKSET(window_gap, wg) +#undef DESKSET +#define MONDESKSET(k, v) \ + if (loc.desktop != NULL) \ + loc.desktop->k = v; \ + else if (loc.monitor != NULL) \ + loc.monitor->k = v; \ + else \ + for (monitor_t *m = mon_head; m != NULL; m = m->next) \ + m->k = v; + } else if (streq("top_padding", name)) { + int tp; + if (sscanf(value, "%i", &tp) != 1) + return MSG_FAILURE; + MONDESKSET(top_padding, tp) + } else if (streq("right_padding", name)) { + int rp; + if (sscanf(value, "%i", &rp) != 1) + return MSG_FAILURE; + MONDESKSET(right_padding, rp) + } else if (streq("bottom_padding", name)) { + int bp; + if (sscanf(value, "%i", &bp) != 1) + return MSG_FAILURE; + MONDESKSET(bottom_padding, bp) + } else if (streq("left_padding", name)) { + int lp; + if (sscanf(value, "%i", &lp) != 1) + return MSG_FAILURE; + MONDESKSET(left_padding, lp) +#undef MONDESKSET +#define SETSTR(s) \ + } else if (streq(#s, name)) { \ + return snprintf(s, sizeof(s), "%s", value) >= 0; + SETSTR(external_rules_command) + SETSTR(status_prefix) +#undef SETSTR + } else if (streq("split_ratio", name)) { + double r; + if (sscanf(value, "%lf", &r) == 1 && r > 0 && r < 1) + split_ratio = r; + else + return MSG_FAILURE; + return MSG_SUCCESS; +#define SETCOLOR(s) \ + } else if (streq(#s, name)) { \ + snprintf(s, sizeof(s), "%s", value); + SETCOLOR(focused_border_color) + SETCOLOR(active_border_color) + SETCOLOR(normal_border_color) + SETCOLOR(presel_border_color) + SETCOLOR(focused_locked_border_color) + SETCOLOR(active_locked_border_color) + SETCOLOR(normal_locked_border_color) + SETCOLOR(focused_sticky_border_color) + SETCOLOR(active_sticky_border_color) + SETCOLOR(normal_sticky_border_color) + SETCOLOR(focused_private_border_color) + SETCOLOR(active_private_border_color) + SETCOLOR(normal_private_border_color) + SETCOLOR(urgent_border_color) +#undef SETCOLOR + } else if (streq("focus_follows_pointer", name)) { + bool b; + if (parse_bool(value, &b) && b != focus_follows_pointer) { + focus_follows_pointer = b; + for (monitor_t *m = mon_head; m != NULL; m = m->next) + for (desktop_t *d = m->desk_head; d != NULL; d = d->next) + for (node_t *n = first_extrema(d->root); n != NULL; n = next_leaf(n, d->root)) { + uint32_t values[] = {CLIENT_EVENT_MASK | (focus_follows_pointer ? XCB_EVENT_MASK_ENTER_WINDOW : 0)}; + xcb_change_window_attributes(dpy, n->client->window, XCB_CW_EVENT_MASK, values); + } + if (focus_follows_pointer) { + for (monitor_t *m = mon_head; m != NULL; m = m->next) + window_show(m->root); + enable_motion_recorder(); + } else { + for (monitor_t *m = mon_head; m != NULL; m = m->next) + window_hide(m->root); + disable_motion_recorder(); + } + return MSG_SUCCESS; + } else { + return MSG_FAILURE; + } +#define SETBOOL(s) \ + } else if (streq(#s, name)) { \ + if (!parse_bool(value, &s)) \ + return MSG_FAILURE; + SETBOOL(borderless_monocle) + SETBOOL(gapless_monocle) + SETBOOL(pointer_follows_monitor) + SETBOOL(apply_floating_atom) + SETBOOL(auto_alternate) + SETBOOL(auto_cancel) + SETBOOL(history_aware_focus) + SETBOOL(ignore_ewmh_focus) + SETBOOL(remove_disabled_monitor) + SETBOOL(remove_unplugged_monitors) + SETBOOL(merge_overlapping_monitors) +#undef SETBOOL + } else { + return MSG_FAILURE; + } + + for (monitor_t *m = mon_head; m != NULL; m = m->next) + for (desktop_t *d = m->desk_head; d != NULL; d = d->next) + arrange(m, d); + + return MSG_SUCCESS; } -bool parse_swap_option(char *s, swap_option_t *o) +int get_setting(coordinates_t loc, char *name, FILE* rsp) { - if (s == NULL) { - *o = SWAP_OPTION_SWAP_FOCUS; - return true; - } else if (strcmp(s, "--keep-focus") == 0) { - *o = SWAP_OPTION_KEEP_FOCUS; - return true; - } - return false; + if (streq("split_ratio", name)) + fprintf(rsp, "%lf", split_ratio); + else if (streq("window_gap", name)) + if (loc.desktop == NULL) + return MSG_FAILURE; + else + fprintf(rsp, "%i", loc.desktop->window_gap); + else if (streq("border_width", name)) + if (loc.node != NULL) + fprintf(rsp, "%u", loc.node->client->border_width); + else if (loc.desktop != NULL) + fprintf(rsp, "%u", loc.desktop->border_width); + else + return MSG_FAILURE; + else if (streq("external_rules_command", name)) + fprintf(rsp, "%s", external_rules_command); + else if (streq("status_prefix", name)) + fprintf(rsp, "%s", status_prefix); +#define MONDESKGET(k) \ + else if (streq(#k, name)) \ + if (loc.desktop != NULL) \ + fprintf(rsp, "%i", loc.desktop->k); \ + else if (loc.monitor != NULL) \ + fprintf(rsp, "%i", loc.monitor->k); \ + else \ + return MSG_FAILURE; + MONDESKGET(top_padding) + MONDESKGET(right_padding) + MONDESKGET(bottom_padding) + MONDESKGET(left_padding) +#undef DESKGET +#define GETCOLOR(s) \ + else if (streq(#s, name)) \ + fprintf(rsp, "%s", s); + GETCOLOR(focused_border_color) + GETCOLOR(active_border_color) + GETCOLOR(normal_border_color) + GETCOLOR(presel_border_color) + GETCOLOR(focused_locked_border_color) + GETCOLOR(active_locked_border_color) + GETCOLOR(normal_locked_border_color) + GETCOLOR(focused_sticky_border_color) + GETCOLOR(active_sticky_border_color) + GETCOLOR(normal_sticky_border_color) + GETCOLOR(urgent_border_color) +#undef GETCOLOR +#define GETBOOL(s) \ + else if (streq(#s, name)) \ + fprintf(rsp, "%s", BOOLSTR(s)); + GETBOOL(borderless_monocle) + GETBOOL(gapless_monocle) + GETBOOL(focus_follows_pointer) + GETBOOL(pointer_follows_monitor) + GETBOOL(apply_floating_atom) + GETBOOL(auto_alternate) + GETBOOL(auto_cancel) + GETBOOL(history_aware_focus) + GETBOOL(ignore_ewmh_focus) + GETBOOL(remove_disabled_monitor) + GETBOOL(remove_unplugged_monitors) + GETBOOL(merge_overlapping_monitors) +#undef GETBOOL + else + return MSG_FAILURE; + return MSG_SUCCESS; } -bool parse_cancel_option(char *s, cancel_option_t *o) +bool parse_bool(char *value, bool *b) { - if (s == NULL) { - *o = CANCEL_OPTION_FOCUSED; - return true; - } else if (strcmp(s, "--all") == 0) { - *o = CANCEL_OPTION_ALL; - return true; - } - return false; + if (streq("true", value) || streq("on", value)) { + *b = true; + return true; + } else if (streq("false", value) || streq("off", value)) { + *b = false; + return true; + } + return false; } -bool parse_rotate(char *s, rotate_t *r) +bool parse_layout(char *s, layout_t *l) { - if (strcmp(s, "clockwise") == 0) { - *r = ROTATE_CLOCKWISE; - return true; - } else if (strcmp(s, "counter_clockwise") == 0) { - *r = ROTATE_COUNTER_CLOCKWISE; - return true; - } else if (strcmp(s, "full_cycle") == 0) { - *r = ROTATE_FULL_CYCLE; - return true; - } - return false; + if (streq("monocle", s)) { + *l = LAYOUT_MONOCLE; + return true; + } else if (streq("tiled", s)) { + *l = LAYOUT_TILED; + return true; + } + return false; } -bool parse_flip(char *s, flip_t *f) +bool parse_direction(char *s, direction_t *d) +{ + if (streq("right", s)) { + *d = DIR_RIGHT; + return true; + } else if (streq("down", s)) { + *d = DIR_DOWN; + return true; + } else if (streq("left", s)) { + *d = DIR_LEFT; + return true; + } else if (streq("up", s)) { + *d = DIR_UP; + return true; + } + return false; +} + +bool parse_cycle_direction(char *s, cycle_dir_t *d) +{ + if (streq("next", s)) { + *d = CYCLE_NEXT; + return true; + } else if (streq("prev", s)) { + *d = CYCLE_PREV; + return true; + } + return false; +} + +bool parse_circulate_direction(char *s, circulate_dir_t *d) +{ + if (streq("forward", s)) { + *d = CIRCULATE_FORWARD; + return true; + } else if (streq("backward", s)) { + *d = CIRCULATE_BACKWARD; + return true; + } + return false; +} + +bool parse_history_direction(char *s, history_dir_t *d) { - if (strcmp(s, "horizontal") == 0) { - *f = FLIP_HORIZONTAL; - return true; - } else if (strcmp(s, "vertical") == 0) { - *f = FLIP_VERTICAL; - return true; - } - return false; + if (streq("older", s)) { + *d = HISTORY_OLDER; + return true; + } else if (streq("newer", s)) { + *d = HISTORY_NEWER; + return true; + } + return false; } -bool parse_fence_move(char *s, fence_move_t *m) + +bool parse_flip(char *s, flip_t *f) { - if (strcmp(s, "push") == 0) { - *m = MOVE_PUSH; - return true; - } else if (strcmp(s, "pull") == 0) { - *m = MOVE_PULL; - return true; - } - return false; + if (streq("horizontal", s)) { + *f = FLIP_HORIZONTAL; + return true; + } else if (streq("vertical", s)) { + *f = FLIP_VERTICAL; + return true; + } + return false; } bool parse_pointer_action(char *s, pointer_action_t *a) { - if (strcmp(s, "move") == 0) { - *a = ACTION_MOVE; - return true; - } else if (strcmp(s, "resize_corner") == 0) { - *a = ACTION_RESIZE_CORNER; - return true; - } else if (strcmp(s, "resize_side") == 0) { - *a = ACTION_RESIZE_SIDE; - return true; - } else if (strcmp(s, "focus") == 0) { - *a = ACTION_FOCUS; - return true; - } - return false; + if (streq("move", s)) { + *a = ACTION_MOVE; + return true; + } else if (streq("resize_corner", s)) { + *a = ACTION_RESIZE_CORNER; + return true; + } else if (streq("resize_side", s)) { + *a = ACTION_RESIZE_SIDE; + return true; + } else if (streq("focus", s)) { + *a = ACTION_FOCUS; + return true; + } + return false; +} + +bool parse_degree(char *s, int *d) +{ + int i = atoi(s); + while (i < 0) + i += 360; + while (i > 359) + i -= 360; + if ((i % 90) != 0) { + return false; + } else { + *d = i; + return true; + } +} + +bool parse_window_id(char *s, long int *i) +{ + char *end; + errno = 0; + long int ret = strtol(s, &end, 0); + if (errno != 0 || *end != '\0') + return false; + else + *i = ret; + return true; +} + +bool parse_bool_declaration(char *s, char **key, bool *value, alter_state_t *state) +{ + *key = strtok(s, EQL_TOK); + char *v = strtok(NULL, EQL_TOK); + if (v == NULL) { + *state = ALTER_TOGGLE; + return true; + } else { + if (parse_bool(v, value)) { + *state = ALTER_SET; + return true; + } else { + return false; + } + } + return false; +} + +bool parse_index(char *s, int *i) +{ + int idx; + if (sscanf(s, "^%i", &idx) != 1 || idx < 1) + return false; + *i = idx; + return true; }