From 4cd18449d2927304e02607a7130572d5329c274e Mon Sep 17 00:00:00 2001 From: taiyu Date: Fri, 4 Sep 2015 16:57:03 -0700 Subject: [PATCH 1/4] better handling of commands during config --- include/commands.h | 5 ++++ sway/commands.c | 58 ++++++++++++++++++++++++---------------------- sway/config.c | 28 ++++++++++++---------- 3 files changed, 51 insertions(+), 40 deletions(-) diff --git a/include/commands.h b/include/commands.h index 714d2db0..31bc0b0b 100644 --- a/include/commands.h +++ b/include/commands.h @@ -6,8 +6,13 @@ struct cmd_handler { char *command; bool (*handle)(struct sway_config *config, int argc, char **argv); + // if <0 command is deffered until compositor is ready. + // if =0 command can be called anytime. + // if >0 command can only be called via keybind, ignored in config + int config_type; }; +struct cmd_handler *find_handler(char *line); bool handle_command(struct sway_config *config, char *command); void remove_view_from_scratchpad(); diff --git a/sway/commands.c b/sway/commands.c index cf3d5b3f..fe480f0c 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -860,31 +860,31 @@ static bool cmd_ws_auto_back_and_forth(struct sway_config *config, int argc, cha /* Keep alphabetized */ static struct cmd_handler handlers[] = { - { "bindsym", cmd_bindsym }, - { "default_orientation", cmd_orientation }, - { "exec", cmd_exec }, - { "exec_always", cmd_exec_always }, - { "exit", cmd_exit }, - { "floating", cmd_floating }, - { "floating_modifier", cmd_floating_mod }, - { "focus", cmd_focus }, - { "focus_follows_mouse", cmd_focus_follows_mouse }, - { "fullscreen", cmd_fullscreen }, - { "gaps", cmd_gaps }, - { "kill", cmd_kill }, - { "layout", cmd_layout }, - { "log_colors", cmd_log_colors }, - { "move", cmd_move}, - { "output", cmd_output}, - { "reload", cmd_reload }, - { "resize", cmd_resize }, - { "scratchpad", cmd_scratchpad }, - { "set", cmd_set }, - { "split", cmd_split }, - { "splith", cmd_splith }, - { "splitv", cmd_splitv }, - { "workspace", cmd_workspace }, - { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth } + { "bindsym", cmd_bindsym, 0 }, + { "default_orientation", cmd_orientation, 0}, + { "exec", cmd_exec, -1 }, + { "exec_always", cmd_exec_always, -1 }, + { "exit", cmd_exit, 1 }, + { "floating", cmd_floating, 1 }, + { "floating_modifier", cmd_floating_mod, 0 }, + { "focus", cmd_focus, 1 }, + { "focus_follows_mouse", cmd_focus_follows_mouse, 0 }, + { "fullscreen", cmd_fullscreen, 1 }, + { "gaps", cmd_gaps, 0 }, + { "kill", cmd_kill, 1 }, + { "layout", cmd_layout, 1 }, + { "log_colors", cmd_log_colors, 0 }, + { "move", cmd_move, 1 }, + { "output", cmd_output, 0 }, + { "reload", cmd_reload, 1 }, + { "resize", cmd_resize, 1 }, + { "scratchpad", cmd_scratchpad, 1 }, + { "set", cmd_set, 0 }, + { "split", cmd_split, 1 }, + { "splith", cmd_splith, 1 }, + { "splitv", cmd_splitv, 1 }, + { "workspace", cmd_workspace, -1 }, + { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth, 0 } }; static char **split_directive(char *line, int *argc) { @@ -945,9 +945,11 @@ static int handler_compare(const void *_a, const void *_b) { return strcasecmp(a->command, b->command); } -static struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) { +struct cmd_handler *find_handler(char *line) { struct cmd_handler d = { .command=line }; - struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare); + struct cmd_handler *res = bsearch(&d, handlers, + sizeof(handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); return res; } @@ -964,7 +966,7 @@ bool handle_command(struct sway_config *config, char *exec) { strncpy(cmd, exec, index); cmd[index] = '\0'; } - struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd); + struct cmd_handler *handler = find_handler(cmd); if (handler == NULL) { sway_log(L_ERROR, "Unknown command '%s'", cmd); exec_success = false; // TODO: return error, probably diff --git a/sway/config.c b/sway/config.c index 2d7e241d..9cc5addd 100644 --- a/sway/config.c +++ b/sway/config.c @@ -218,18 +218,22 @@ bool read_config(FILE *file, bool is_active) { // Any command which would require wlc to be initialized // should be queued for later execution list_t *args = split_string(line, " "); - if (!is_active && ( - strcmp("exec", args->items[0]) == 0 || - strcmp("exec_always", args->items[0]) == 0 )) { - sway_log(L_DEBUG, "Deferring command %s", line); - - char *cmd = malloc(strlen(line) + 1); - strcpy(cmd, line); - list_add(temp_config->cmd_queue, cmd); - } else if (!temp_depth && !handle_command(temp_config, line)) { - sway_log(L_DEBUG, "Config load failed for line %s", line); - success = false; - temp_config->failed = true; + struct cmd_handler *handler; + if ((handler = find_handler(args->items[0]))) { + if (handler->config_type > 0) { + sway_log(L_ERROR, "Invalid command during config ``%s''", line); + } else if (handler->config_type < 0 && !is_active) { + sway_log(L_DEBUG, "Deferring command ``%s''", line); + char *cmd = malloc(strlen(line) + 1); + strcpy(cmd, line); + list_add(temp_config->cmd_queue, cmd); + } else if (!temp_depth && !handle_command(temp_config, line)) { + sway_log(L_DEBUG, "Config load failed for line ``%s''", line); + success = false; + temp_config->failed = true; + } + } else { + sway_log(L_ERROR, "Invalid command %s",args->items[0]); } free_flat_list(args); From c5d0b5d4372a9443e07386b9dc47bd1681bbf699 Mon Sep 17 00:00:00 2001 From: taiyu Date: Fri, 4 Sep 2015 17:02:02 -0700 Subject: [PATCH 2/4] style --- sway/config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway/config.c b/sway/config.c index 9cc5addd..cbaab11e 100644 --- a/sway/config.c +++ b/sway/config.c @@ -233,7 +233,7 @@ bool read_config(FILE *file, bool is_active) { temp_config->failed = true; } } else { - sway_log(L_ERROR, "Invalid command %s",args->items[0]); + sway_log(L_ERROR, "Invalid command ``%s''", line); } free_flat_list(args); From afa6747145fe0ddc284ae115df937cd18ad4f3ff Mon Sep 17 00:00:00 2001 From: taiyu Date: Fri, 4 Sep 2015 17:09:07 -0700 Subject: [PATCH 3/4] enum for command type --- include/commands.h | 6 +++++- sway/commands.c | 50 +++++++++++++++++++++++----------------------- sway/config.c | 4 ++-- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/include/commands.h b/include/commands.h index 31bc0b0b..62f8166e 100644 --- a/include/commands.h +++ b/include/commands.h @@ -9,7 +9,11 @@ struct cmd_handler { // if <0 command is deffered until compositor is ready. // if =0 command can be called anytime. // if >0 command can only be called via keybind, ignored in config - int config_type; + enum { + CMD_COMPOSITOR_READY, + CMD_KEYBIND, + CMD_ANYTIME + } config_type; }; struct cmd_handler *find_handler(char *line); diff --git a/sway/commands.c b/sway/commands.c index fe480f0c..0fc98538 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -860,31 +860,31 @@ static bool cmd_ws_auto_back_and_forth(struct sway_config *config, int argc, cha /* Keep alphabetized */ static struct cmd_handler handlers[] = { - { "bindsym", cmd_bindsym, 0 }, - { "default_orientation", cmd_orientation, 0}, - { "exec", cmd_exec, -1 }, - { "exec_always", cmd_exec_always, -1 }, - { "exit", cmd_exit, 1 }, - { "floating", cmd_floating, 1 }, - { "floating_modifier", cmd_floating_mod, 0 }, - { "focus", cmd_focus, 1 }, - { "focus_follows_mouse", cmd_focus_follows_mouse, 0 }, - { "fullscreen", cmd_fullscreen, 1 }, - { "gaps", cmd_gaps, 0 }, - { "kill", cmd_kill, 1 }, - { "layout", cmd_layout, 1 }, - { "log_colors", cmd_log_colors, 0 }, - { "move", cmd_move, 1 }, - { "output", cmd_output, 0 }, - { "reload", cmd_reload, 1 }, - { "resize", cmd_resize, 1 }, - { "scratchpad", cmd_scratchpad, 1 }, - { "set", cmd_set, 0 }, - { "split", cmd_split, 1 }, - { "splith", cmd_splith, 1 }, - { "splitv", cmd_splitv, 1 }, - { "workspace", cmd_workspace, -1 }, - { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth, 0 } + { "bindsym", cmd_bindsym, CMD_ANYTIME }, + { "default_orientation", cmd_orientation, CMD_ANYTIME}, + { "exec", cmd_exec, CMD_COMPOSITOR_READY }, + { "exec_always", cmd_exec_always, CMD_COMPOSITOR_READY }, + { "exit", cmd_exit, CMD_KEYBIND }, + { "floating", cmd_floating, CMD_KEYBIND }, + { "floating_modifier", cmd_floating_mod, CMD_ANYTIME }, + { "focus", cmd_focus, CMD_KEYBIND }, + { "focus_follows_mouse", cmd_focus_follows_mouse, CMD_ANYTIME }, + { "fullscreen", cmd_fullscreen, CMD_KEYBIND }, + { "gaps", cmd_gaps, CMD_ANYTIME }, + { "kill", cmd_kill, CMD_KEYBIND }, + { "layout", cmd_layout, CMD_KEYBIND }, + { "log_colors", cmd_log_colors, CMD_ANYTIME }, + { "move", cmd_move, CMD_KEYBIND }, + { "output", cmd_output, CMD_ANYTIME }, + { "reload", cmd_reload, CMD_KEYBIND }, + { "resize", cmd_resize, CMD_KEYBIND }, + { "scratchpad", cmd_scratchpad, CMD_KEYBIND }, + { "set", cmd_set, CMD_ANYTIME }, + { "split", cmd_split, CMD_KEYBIND }, + { "splith", cmd_splith, CMD_KEYBIND }, + { "splitv", cmd_splitv, CMD_KEYBIND }, + { "workspace", cmd_workspace, CMD_COMPOSITOR_READY }, + { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth, CMD_ANYTIME }, }; static char **split_directive(char *line, int *argc) { diff --git a/sway/config.c b/sway/config.c index cbaab11e..90f6529a 100644 --- a/sway/config.c +++ b/sway/config.c @@ -220,9 +220,9 @@ bool read_config(FILE *file, bool is_active) { list_t *args = split_string(line, " "); struct cmd_handler *handler; if ((handler = find_handler(args->items[0]))) { - if (handler->config_type > 0) { + if (handler->config_type == CMD_KEYBIND) { sway_log(L_ERROR, "Invalid command during config ``%s''", line); - } else if (handler->config_type < 0 && !is_active) { + } else if (handler->config_type == CMD_COMPOSITOR_READY && !is_active) { sway_log(L_DEBUG, "Deferring command ``%s''", line); char *cmd = malloc(strlen(line) + 1); strcpy(cmd, line); From 2ef83664f528b45f8e9b3b0dbd8b8e2e0ec938bf Mon Sep 17 00:00:00 2001 From: taiyu Date: Fri, 4 Sep 2015 17:10:10 -0700 Subject: [PATCH 4/4] remove outdated comment --- include/commands.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/commands.h b/include/commands.h index 62f8166e..808e64eb 100644 --- a/include/commands.h +++ b/include/commands.h @@ -6,9 +6,6 @@ struct cmd_handler { char *command; bool (*handle)(struct sway_config *config, int argc, char **argv); - // if <0 command is deffered until compositor is ready. - // if =0 command can be called anytime. - // if >0 command can only be called via keybind, ignored in config enum { CMD_COMPOSITOR_READY, CMD_KEYBIND,