diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-03-29 22:10:33 -0400 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-03-29 22:11:08 -0400 |
commit | 8efee109ad2ab4861f25e54e9f6d1ceb06203791 (patch) | |
tree | 9cd16bb63d16285620dc16a09da5582f211c0a87 /sway/commands | |
parent | Clean up imported bar commands (diff) | |
download | sway-8efee109ad2ab4861f25e54e9f6d1ceb06203791.tar.gz sway-8efee109ad2ab4861f25e54e9f6d1ceb06203791.tar.zst sway-8efee109ad2ab4861f25e54e9f6d1ceb06203791.zip |
Implement modes
Diffstat (limited to 'sway/commands')
-rw-r--r-- | sway/commands/mode.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/sway/commands/mode.c b/sway/commands/mode.c new file mode 100644 index 00000000..c30a8bac --- /dev/null +++ b/sway/commands/mode.c | |||
@@ -0,0 +1,59 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <stdbool.h> | ||
3 | #include <string.h> | ||
4 | #include <strings.h> | ||
5 | #include "sway/commands.h" | ||
6 | #include "sway/config.h" | ||
7 | #include "sway/ipc-server.h" | ||
8 | #include "list.h" | ||
9 | #include "log.h" | ||
10 | |||
11 | struct cmd_results *cmd_mode(int argc, char **argv) { | ||
12 | struct cmd_results *error = NULL; | ||
13 | if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) { | ||
14 | return error; | ||
15 | } | ||
16 | |||
17 | const char *mode_name = argv[0]; | ||
18 | bool new_mode = (argc == 2 && strcmp(argv[1], "{") == 0); | ||
19 | if (new_mode && !config->reading) { | ||
20 | return cmd_results_new(CMD_FAILURE, | ||
21 | "mode", "Can only be used in config file."); | ||
22 | } | ||
23 | struct sway_mode *mode = NULL; | ||
24 | // Find mode | ||
25 | for (int i = 0; i < config->modes->length; ++i) { | ||
26 | struct sway_mode *test = config->modes->items[i]; | ||
27 | if (strcasecmp(test->name, mode_name) == 0) { | ||
28 | mode = test; | ||
29 | break; | ||
30 | } | ||
31 | } | ||
32 | // Create mode if it doesn't exist | ||
33 | if (!mode && new_mode) { | ||
34 | mode = calloc(1, sizeof(struct sway_mode)); | ||
35 | if (!mode) { | ||
36 | return cmd_results_new(CMD_FAILURE, | ||
37 | "mode", "Unable to allocate mode"); | ||
38 | } | ||
39 | mode->name = strdup(mode_name); | ||
40 | mode->keysym_bindings = create_list(); | ||
41 | mode->keycode_bindings = create_list(); | ||
42 | list_add(config->modes, mode); | ||
43 | } | ||
44 | if (!mode) { | ||
45 | error = cmd_results_new(CMD_INVALID, | ||
46 | "mode", "Unknown mode `%s'", mode_name); | ||
47 | return error; | ||
48 | } | ||
49 | if ((config->reading && new_mode) || (!config->reading && !new_mode)) { | ||
50 | wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name); | ||
51 | } | ||
52 | // Set current mode | ||
53 | config->current_mode = mode; | ||
54 | if (!new_mode) { | ||
55 | // trigger IPC mode event | ||
56 | ipc_event_mode(config->current_mode->name); | ||
57 | } | ||
58 | return cmd_results_new(new_mode ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL); | ||
59 | } | ||