diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2018-05-30 13:20:02 -0400 |
---|---|---|
committer | Brian Ashworth <bosrsf04@gmail.com> | 2018-06-02 08:07:44 -0400 |
commit | 7c810dc344c28d1876c5ee158cb0806289d0f813 (patch) | |
tree | dbe756bceca42ea6f9a6cf5e5771037417bb64c3 /sway/commands/mode.c | |
parent | Merge pull request #2080 from frsfnrrg/keyboard-remodeling (diff) | |
download | sway-7c810dc344c28d1876c5ee158cb0806289d0f813.tar.gz sway-7c810dc344c28d1876c5ee158cb0806289d0f813.tar.zst sway-7c810dc344c28d1876c5ee158cb0806289d0f813.zip |
Make command block implementation generic
Diffstat (limited to 'sway/commands/mode.c')
-rw-r--r-- | sway/commands/mode.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/sway/commands/mode.c b/sway/commands/mode.c index c30a8bac..31d0f251 100644 --- a/sway/commands/mode.c +++ b/sway/commands/mode.c | |||
@@ -7,6 +7,13 @@ | |||
7 | #include "sway/ipc-server.h" | 7 | #include "sway/ipc-server.h" |
8 | #include "list.h" | 8 | #include "list.h" |
9 | #include "log.h" | 9 | #include "log.h" |
10 | #include "stringop.h" | ||
11 | |||
12 | // Must be in order for the bsearch | ||
13 | static struct cmd_handler mode_handlers[] = { | ||
14 | { "bindcode", cmd_bindcode }, | ||
15 | { "bindsym", cmd_bindsym } | ||
16 | }; | ||
10 | 17 | ||
11 | struct cmd_results *cmd_mode(int argc, char **argv) { | 18 | struct cmd_results *cmd_mode(int argc, char **argv) { |
12 | struct cmd_results *error = NULL; | 19 | struct cmd_results *error = NULL; |
@@ -14,12 +21,12 @@ struct cmd_results *cmd_mode(int argc, char **argv) { | |||
14 | return error; | 21 | return error; |
15 | } | 22 | } |
16 | 23 | ||
17 | const char *mode_name = argv[0]; | 24 | if (argc > 1 && !config->reading) { |
18 | bool new_mode = (argc == 2 && strcmp(argv[1], "{") == 0); | ||
19 | if (new_mode && !config->reading) { | ||
20 | return cmd_results_new(CMD_FAILURE, | 25 | return cmd_results_new(CMD_FAILURE, |
21 | "mode", "Can only be used in config file."); | 26 | "mode", "Can only be used in config file."); |
22 | } | 27 | } |
28 | |||
29 | const char *mode_name = argv[0]; | ||
23 | struct sway_mode *mode = NULL; | 30 | struct sway_mode *mode = NULL; |
24 | // Find mode | 31 | // Find mode |
25 | for (int i = 0; i < config->modes->length; ++i) { | 32 | for (int i = 0; i < config->modes->length; ++i) { |
@@ -30,7 +37,7 @@ struct cmd_results *cmd_mode(int argc, char **argv) { | |||
30 | } | 37 | } |
31 | } | 38 | } |
32 | // Create mode if it doesn't exist | 39 | // Create mode if it doesn't exist |
33 | if (!mode && new_mode) { | 40 | if (!mode && argc > 1) { |
34 | mode = calloc(1, sizeof(struct sway_mode)); | 41 | mode = calloc(1, sizeof(struct sway_mode)); |
35 | if (!mode) { | 42 | if (!mode) { |
36 | return cmd_results_new(CMD_FAILURE, | 43 | return cmd_results_new(CMD_FAILURE, |
@@ -46,14 +53,21 @@ struct cmd_results *cmd_mode(int argc, char **argv) { | |||
46 | "mode", "Unknown mode `%s'", mode_name); | 53 | "mode", "Unknown mode `%s'", mode_name); |
47 | return error; | 54 | return error; |
48 | } | 55 | } |
49 | if ((config->reading && new_mode) || (!config->reading && !new_mode)) { | 56 | if ((config->reading && argc > 1) || (!config->reading && argc == 1)) { |
50 | wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name); | 57 | wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name); |
51 | } | 58 | } |
52 | // Set current mode | 59 | // Set current mode |
53 | config->current_mode = mode; | 60 | config->current_mode = mode; |
54 | if (!new_mode) { | 61 | if (argc == 1) { |
55 | // trigger IPC mode event | 62 | // trigger IPC mode event |
56 | ipc_event_mode(config->current_mode->name); | 63 | ipc_event_mode(config->current_mode->name); |
64 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
57 | } | 65 | } |
58 | return cmd_results_new(new_mode ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); | 66 | |
67 | // Create binding | ||
68 | struct cmd_results *result = subcommand(argv + 1, argc - 1, mode_handlers, | ||
69 | sizeof(mode_handlers)); | ||
70 | config->current_mode = config->modes->items[0]; | ||
71 | |||
72 | return result; | ||
59 | } | 73 | } |