diff options
Diffstat (limited to 'sway/commands/ipc.c')
-rw-r--r-- | sway/commands/ipc.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/sway/commands/ipc.c b/sway/commands/ipc.c new file mode 100644 index 00000000..222be0dd --- /dev/null +++ b/sway/commands/ipc.c | |||
@@ -0,0 +1,140 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <string.h> | ||
3 | #include "sway/commands.h" | ||
4 | #include "sway/config.h" | ||
5 | #include "ipc.h" | ||
6 | #include "log.h" | ||
7 | #include "util.h" | ||
8 | |||
9 | struct cmd_results *cmd_ipc(int argc, char **argv) { | ||
10 | struct cmd_results *error = NULL; | ||
11 | if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) { | ||
12 | return error; | ||
13 | } | ||
14 | |||
15 | if (config->reading && strcmp("{", argv[0]) != 0) { | ||
16 | return cmd_results_new(CMD_INVALID, "ipc", | ||
17 | "Expected '{' at start of IPC config definition."); | ||
18 | } | ||
19 | |||
20 | if (!config->reading) { | ||
21 | return cmd_results_new(CMD_FAILURE, "ipc", "Can only be used in config file."); | ||
22 | } | ||
23 | |||
24 | return cmd_results_new(CMD_BLOCK_IPC, NULL, NULL); | ||
25 | } | ||
26 | |||
27 | struct cmd_results *cmd_ipc_events(int argc, char **argv) { | ||
28 | struct cmd_results *error = NULL; | ||
29 | if ((error = checkarg(argc, "events", EXPECTED_EQUAL_TO, 1))) { | ||
30 | return error; | ||
31 | } | ||
32 | |||
33 | if (config->reading && strcmp("{", argv[0]) != 0) { | ||
34 | return cmd_results_new(CMD_INVALID, "events", | ||
35 | "Expected '{' at start of IPC event config definition."); | ||
36 | } | ||
37 | |||
38 | if (!config->reading) { | ||
39 | return cmd_results_new(CMD_FAILURE, "events", "Can only be used in config file."); | ||
40 | } | ||
41 | |||
42 | return cmd_results_new(CMD_BLOCK_IPC_EVENTS, NULL, NULL); | ||
43 | } | ||
44 | |||
45 | struct cmd_results *cmd_ipc_cmd(int argc, char **argv) { | ||
46 | struct cmd_results *error = NULL; | ||
47 | if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) { | ||
48 | return error; | ||
49 | } | ||
50 | |||
51 | bool enabled; | ||
52 | if (strcmp(argv[0], "enabled") == 0) { | ||
53 | enabled = true; | ||
54 | } else if (strcmp(argv[0], "disabled") == 0) { | ||
55 | enabled = false; | ||
56 | } else { | ||
57 | return cmd_results_new(CMD_INVALID, argv[-1], | ||
58 | "Argument must be one of 'enabled' or 'disabled'"); | ||
59 | } | ||
60 | |||
61 | struct { | ||
62 | char *name; | ||
63 | enum ipc_feature type; | ||
64 | } types[] = { | ||
65 | { "command", IPC_FEATURE_COMMAND }, | ||
66 | { "workspaces", IPC_FEATURE_GET_WORKSPACES }, | ||
67 | { "outputs", IPC_FEATURE_GET_OUTPUTS }, | ||
68 | { "tree", IPC_FEATURE_GET_TREE }, | ||
69 | { "marks", IPC_FEATURE_GET_MARKS }, | ||
70 | { "bar-config", IPC_FEATURE_GET_BAR_CONFIG }, | ||
71 | { "inputs", IPC_FEATURE_GET_INPUTS }, | ||
72 | }; | ||
73 | |||
74 | uint32_t type = 0; | ||
75 | |||
76 | for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) { | ||
77 | if (strcmp(types[i].name, argv[-1]) == 0) { | ||
78 | type = types[i].type; | ||
79 | break; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | if (enabled) { | ||
84 | config->ipc_policy |= type; | ||
85 | sway_log(L_DEBUG, "Enabled IPC %s feature", argv[-1]); | ||
86 | } else { | ||
87 | config->ipc_policy &= ~type; | ||
88 | sway_log(L_DEBUG, "Disabled IPC %s feature", argv[-1]); | ||
89 | } | ||
90 | |||
91 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
92 | } | ||
93 | |||
94 | struct cmd_results *cmd_ipc_event_cmd(int argc, char **argv) { | ||
95 | struct cmd_results *error = NULL; | ||
96 | if ((error = checkarg(argc, "ipc", EXPECTED_EQUAL_TO, 1))) { | ||
97 | return error; | ||
98 | } | ||
99 | |||
100 | bool enabled; | ||
101 | if (strcmp(argv[0], "enabled") == 0) { | ||
102 | enabled = true; | ||
103 | } else if (strcmp(argv[0], "disabled") == 0) { | ||
104 | enabled = false; | ||
105 | } else { | ||
106 | return cmd_results_new(CMD_INVALID, argv[-1], | ||
107 | "Argument must be one of 'enabled' or 'disabled'"); | ||
108 | } | ||
109 | |||
110 | struct { | ||
111 | char *name; | ||
112 | enum ipc_feature type; | ||
113 | } types[] = { | ||
114 | { "workspace", IPC_FEATURE_EVENT_WORKSPACE }, | ||
115 | { "output", IPC_FEATURE_EVENT_OUTPUT }, | ||
116 | { "mode", IPC_FEATURE_EVENT_MODE }, | ||
117 | { "window", IPC_FEATURE_EVENT_WINDOW }, | ||
118 | { "binding", IPC_FEATURE_EVENT_BINDING }, | ||
119 | { "input", IPC_FEATURE_EVENT_INPUT }, | ||
120 | }; | ||
121 | |||
122 | uint32_t type = 0; | ||
123 | |||
124 | for (size_t i = 0; i < sizeof(types) / sizeof(types[0]); ++i) { | ||
125 | if (strcmp(types[i].name, argv[-1]) == 0) { | ||
126 | type = types[i].type; | ||
127 | break; | ||
128 | } | ||
129 | } | ||
130 | |||
131 | if (enabled) { | ||
132 | config->ipc_policy |= type; | ||
133 | sway_log(L_DEBUG, "Enabled IPC %s event", argv[-1]); | ||
134 | } else { | ||
135 | config->ipc_policy &= ~type; | ||
136 | sway_log(L_DEBUG, "Disabled IPC %s event", argv[-1]); | ||
137 | } | ||
138 | |||
139 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
140 | } | ||