diff options
Diffstat (limited to 'sway/commands/mode.c')
-rw-r--r-- | sway/commands/mode.c | 38 |
1 files changed, 20 insertions, 18 deletions
diff --git a/sway/commands/mode.c b/sway/commands/mode.c index d2985c54..c30a8bac 100644 --- a/sway/commands/mode.c +++ b/sway/commands/mode.c | |||
@@ -15,43 +15,45 @@ struct cmd_results *cmd_mode(int argc, char **argv) { | |||
15 | } | 15 | } |
16 | 16 | ||
17 | const char *mode_name = argv[0]; | 17 | const char *mode_name = argv[0]; |
18 | bool mode_make = (argc == 2 && strcmp(argv[1], "{") == 0); | 18 | bool new_mode = (argc == 2 && strcmp(argv[1], "{") == 0); |
19 | if (mode_make) { | 19 | if (new_mode && !config->reading) { |
20 | if (!config->reading) | 20 | return cmd_results_new(CMD_FAILURE, |
21 | return cmd_results_new(CMD_FAILURE, "mode", "Can only be used in config file."); | 21 | "mode", "Can only be used in config file."); |
22 | } | 22 | } |
23 | struct sway_mode *mode = NULL; | 23 | struct sway_mode *mode = NULL; |
24 | // Find mode | 24 | // Find mode |
25 | int i, len = config->modes->length; | 25 | for (int i = 0; i < config->modes->length; ++i) { |
26 | for (i = 0; i < len; ++i) { | 26 | struct sway_mode *test = config->modes->items[i]; |
27 | struct sway_mode *find = config->modes->items[i]; | 27 | if (strcasecmp(test->name, mode_name) == 0) { |
28 | if (strcasecmp(find->name, mode_name) == 0) { | 28 | mode = test; |
29 | mode = find; | ||
30 | break; | 29 | break; |
31 | } | 30 | } |
32 | } | 31 | } |
33 | // Create mode if it doesn't exist | 32 | // Create mode if it doesn't exist |
34 | if (!mode && mode_make) { | 33 | if (!mode && new_mode) { |
35 | mode = malloc(sizeof(struct sway_mode)); | 34 | mode = calloc(1, sizeof(struct sway_mode)); |
36 | if (!mode) { | 35 | if (!mode) { |
37 | return cmd_results_new(CMD_FAILURE, "mode", "Unable to allocate mode"); | 36 | return cmd_results_new(CMD_FAILURE, |
37 | "mode", "Unable to allocate mode"); | ||
38 | } | 38 | } |
39 | mode->name = strdup(mode_name); | 39 | mode->name = strdup(mode_name); |
40 | mode->bindings = create_list(); | 40 | mode->keysym_bindings = create_list(); |
41 | mode->keycode_bindings = create_list(); | ||
41 | list_add(config->modes, mode); | 42 | list_add(config->modes, mode); |
42 | } | 43 | } |
43 | if (!mode) { | 44 | if (!mode) { |
44 | error = cmd_results_new(CMD_INVALID, "mode", "Unknown mode `%s'", mode_name); | 45 | error = cmd_results_new(CMD_INVALID, |
46 | "mode", "Unknown mode `%s'", mode_name); | ||
45 | return error; | 47 | return error; |
46 | } | 48 | } |
47 | if ((config->reading && mode_make) || (!config->reading && !mode_make)) { | 49 | if ((config->reading && new_mode) || (!config->reading && !new_mode)) { |
48 | sway_log(L_DEBUG, "Switching to mode `%s'",mode->name); | 50 | wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name); |
49 | } | 51 | } |
50 | // Set current mode | 52 | // Set current mode |
51 | config->current_mode = mode; | 53 | config->current_mode = mode; |
52 | if (!mode_make) { | 54 | if (!new_mode) { |
53 | // trigger IPC mode event | 55 | // trigger IPC mode event |
54 | ipc_event_mode(config->current_mode->name); | 56 | ipc_event_mode(config->current_mode->name); |
55 | } | 57 | } |
56 | return cmd_results_new(mode_make ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); | 58 | return cmd_results_new(new_mode ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); |
57 | } | 59 | } |