aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/mode.c
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
committerLibravatar Zandr Martin <zandrmartin@gmail.com>2016-09-01 21:39:08 -0500
commitb374c35758777f98e5ddbe4b0dc43bd7c80f36d7 (patch)
tree04bb4cfc3da7d2e0de7fbc38db42f65c66d2df4c /sway/commands/mode.c
parentMerge pull request #874 from yohanesu75/ipc-client-fix (diff)
downloadsway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.gz
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.zst
sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.zip
refactor commands.c
Diffstat (limited to 'sway/commands/mode.c')
-rw-r--r--sway/commands/mode.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/sway/commands/mode.c b/sway/commands/mode.c
new file mode 100644
index 00000000..05636c61
--- /dev/null
+++ b/sway/commands/mode.c
@@ -0,0 +1,52 @@
1#include <stdbool.h>
2#include <string.h>
3#include "commands.h"
4#include "config.h"
5#include "ipc-server.h"
6#include "list.h"
7#include "log.h"
8
9struct cmd_results *cmd_mode(int argc, char **argv) {
10 struct cmd_results *error = NULL;
11 if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) {
12 return error;
13 }
14
15 const char *mode_name = argv[0];
16 bool mode_make = (argc == 2 && strcmp(argv[1], "{") == 0);
17 if (mode_make) {
18 if (!config->reading)
19 return cmd_results_new(CMD_FAILURE, "mode", "Can only be used in config file.");
20 }
21 struct sway_mode *mode = NULL;
22 // Find mode
23 int i, len = config->modes->length;
24 for (i = 0; i < len; ++i) {
25 struct sway_mode *find = config->modes->items[i];
26 if (strcasecmp(find->name, mode_name) == 0) {
27 mode = find;
28 break;
29 }
30 }
31 // Create mode if it doesn't exist
32 if (!mode && mode_make) {
33 mode = malloc(sizeof*mode);
34 mode->name = strdup(mode_name);
35 mode->bindings = create_list();
36 list_add(config->modes, mode);
37 }
38 if (!mode) {
39 error = cmd_results_new(CMD_INVALID, "mode", "Unknown mode `%s'", mode_name);
40 return error;
41 }
42 if ((config->reading && mode_make) || (!config->reading && !mode_make)) {
43 sway_log(L_DEBUG, "Switching to mode `%s'",mode->name);
44 }
45 // Set current mode
46 config->current_mode = mode;
47 if (!mode_make) {
48 // trigger IPC mode event
49 ipc_event_mode(config->current_mode->name);
50 }
51 return cmd_results_new(mode_make ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL);
52}