diff options
author | taiyu <taiyu.len@gmail.com> | 2015-09-04 16:57:03 -0700 |
---|---|---|
committer | taiyu <taiyu.len@gmail.com> | 2015-09-04 16:57:03 -0700 |
commit | 4cd18449d2927304e02607a7130572d5329c274e (patch) | |
tree | 6c4b5ea735136cbf50d1f7363bcecee6658df32c | |
parent | default gap value (diff) | |
download | sway-4cd18449d2927304e02607a7130572d5329c274e.tar.gz sway-4cd18449d2927304e02607a7130572d5329c274e.tar.zst sway-4cd18449d2927304e02607a7130572d5329c274e.zip |
better handling of commands during config
-rw-r--r-- | include/commands.h | 5 | ||||
-rw-r--r-- | sway/commands.c | 58 | ||||
-rw-r--r-- | 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 @@ | |||
6 | struct cmd_handler { | 6 | struct cmd_handler { |
7 | char *command; | 7 | char *command; |
8 | bool (*handle)(struct sway_config *config, int argc, char **argv); | 8 | bool (*handle)(struct sway_config *config, int argc, char **argv); |
9 | // if <0 command is deffered until compositor is ready. | ||
10 | // if =0 command can be called anytime. | ||
11 | // if >0 command can only be called via keybind, ignored in config | ||
12 | int config_type; | ||
9 | }; | 13 | }; |
10 | 14 | ||
15 | struct cmd_handler *find_handler(char *line); | ||
11 | bool handle_command(struct sway_config *config, char *command); | 16 | bool handle_command(struct sway_config *config, char *command); |
12 | 17 | ||
13 | void remove_view_from_scratchpad(); | 18 | 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 | |||
860 | 860 | ||
861 | /* Keep alphabetized */ | 861 | /* Keep alphabetized */ |
862 | static struct cmd_handler handlers[] = { | 862 | static struct cmd_handler handlers[] = { |
863 | { "bindsym", cmd_bindsym }, | 863 | { "bindsym", cmd_bindsym, 0 }, |
864 | { "default_orientation", cmd_orientation }, | 864 | { "default_orientation", cmd_orientation, 0}, |
865 | { "exec", cmd_exec }, | 865 | { "exec", cmd_exec, -1 }, |
866 | { "exec_always", cmd_exec_always }, | 866 | { "exec_always", cmd_exec_always, -1 }, |
867 | { "exit", cmd_exit }, | 867 | { "exit", cmd_exit, 1 }, |
868 | { "floating", cmd_floating }, | 868 | { "floating", cmd_floating, 1 }, |
869 | { "floating_modifier", cmd_floating_mod }, | 869 | { "floating_modifier", cmd_floating_mod, 0 }, |
870 | { "focus", cmd_focus }, | 870 | { "focus", cmd_focus, 1 }, |
871 | { "focus_follows_mouse", cmd_focus_follows_mouse }, | 871 | { "focus_follows_mouse", cmd_focus_follows_mouse, 0 }, |
872 | { "fullscreen", cmd_fullscreen }, | 872 | { "fullscreen", cmd_fullscreen, 1 }, |
873 | { "gaps", cmd_gaps }, | 873 | { "gaps", cmd_gaps, 0 }, |
874 | { "kill", cmd_kill }, | 874 | { "kill", cmd_kill, 1 }, |
875 | { "layout", cmd_layout }, | 875 | { "layout", cmd_layout, 1 }, |
876 | { "log_colors", cmd_log_colors }, | 876 | { "log_colors", cmd_log_colors, 0 }, |
877 | { "move", cmd_move}, | 877 | { "move", cmd_move, 1 }, |
878 | { "output", cmd_output}, | 878 | { "output", cmd_output, 0 }, |
879 | { "reload", cmd_reload }, | 879 | { "reload", cmd_reload, 1 }, |
880 | { "resize", cmd_resize }, | 880 | { "resize", cmd_resize, 1 }, |
881 | { "scratchpad", cmd_scratchpad }, | 881 | { "scratchpad", cmd_scratchpad, 1 }, |
882 | { "set", cmd_set }, | 882 | { "set", cmd_set, 0 }, |
883 | { "split", cmd_split }, | 883 | { "split", cmd_split, 1 }, |
884 | { "splith", cmd_splith }, | 884 | { "splith", cmd_splith, 1 }, |
885 | { "splitv", cmd_splitv }, | 885 | { "splitv", cmd_splitv, 1 }, |
886 | { "workspace", cmd_workspace }, | 886 | { "workspace", cmd_workspace, -1 }, |
887 | { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth } | 887 | { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth, 0 } |
888 | }; | 888 | }; |
889 | 889 | ||
890 | static char **split_directive(char *line, int *argc) { | 890 | static char **split_directive(char *line, int *argc) { |
@@ -945,9 +945,11 @@ static int handler_compare(const void *_a, const void *_b) { | |||
945 | return strcasecmp(a->command, b->command); | 945 | return strcasecmp(a->command, b->command); |
946 | } | 946 | } |
947 | 947 | ||
948 | static struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) { | 948 | struct cmd_handler *find_handler(char *line) { |
949 | struct cmd_handler d = { .command=line }; | 949 | struct cmd_handler d = { .command=line }; |
950 | struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare); | 950 | struct cmd_handler *res = bsearch(&d, handlers, |
951 | sizeof(handlers) / sizeof(struct cmd_handler), | ||
952 | sizeof(struct cmd_handler), handler_compare); | ||
951 | return res; | 953 | return res; |
952 | } | 954 | } |
953 | 955 | ||
@@ -964,7 +966,7 @@ bool handle_command(struct sway_config *config, char *exec) { | |||
964 | strncpy(cmd, exec, index); | 966 | strncpy(cmd, exec, index); |
965 | cmd[index] = '\0'; | 967 | cmd[index] = '\0'; |
966 | } | 968 | } |
967 | struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd); | 969 | struct cmd_handler *handler = find_handler(cmd); |
968 | if (handler == NULL) { | 970 | if (handler == NULL) { |
969 | sway_log(L_ERROR, "Unknown command '%s'", cmd); | 971 | sway_log(L_ERROR, "Unknown command '%s'", cmd); |
970 | exec_success = false; // TODO: return error, probably | 972 | 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) { | |||
218 | // Any command which would require wlc to be initialized | 218 | // Any command which would require wlc to be initialized |
219 | // should be queued for later execution | 219 | // should be queued for later execution |
220 | list_t *args = split_string(line, " "); | 220 | list_t *args = split_string(line, " "); |
221 | if (!is_active && ( | 221 | struct cmd_handler *handler; |
222 | strcmp("exec", args->items[0]) == 0 || | 222 | if ((handler = find_handler(args->items[0]))) { |
223 | strcmp("exec_always", args->items[0]) == 0 )) { | 223 | if (handler->config_type > 0) { |
224 | sway_log(L_DEBUG, "Deferring command %s", line); | 224 | sway_log(L_ERROR, "Invalid command during config ``%s''", line); |
225 | 225 | } else if (handler->config_type < 0 && !is_active) { | |
226 | char *cmd = malloc(strlen(line) + 1); | 226 | sway_log(L_DEBUG, "Deferring command ``%s''", line); |
227 | strcpy(cmd, line); | 227 | char *cmd = malloc(strlen(line) + 1); |
228 | list_add(temp_config->cmd_queue, cmd); | 228 | strcpy(cmd, line); |
229 | } else if (!temp_depth && !handle_command(temp_config, line)) { | 229 | list_add(temp_config->cmd_queue, cmd); |
230 | sway_log(L_DEBUG, "Config load failed for line %s", line); | 230 | } else if (!temp_depth && !handle_command(temp_config, line)) { |
231 | success = false; | 231 | sway_log(L_DEBUG, "Config load failed for line ``%s''", line); |
232 | temp_config->failed = true; | 232 | success = false; |
233 | temp_config->failed = true; | ||
234 | } | ||
235 | } else { | ||
236 | sway_log(L_ERROR, "Invalid command %s",args->items[0]); | ||
233 | } | 237 | } |
234 | free_flat_list(args); | 238 | free_flat_list(args); |
235 | 239 | ||