aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands/ipc.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2016-12-02 17:34:26 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2016-12-02 17:34:26 -0500
commitc8dc4925d1e0f5d5086a4c15415ee9fb0b7e6155 (patch)
treee4019d0e2408f00d054907c35202a6104c6bb272 /sway/commands/ipc.c
parentAdd IPC policy to config (diff)
downloadsway-c8dc4925d1e0f5d5086a4c15415ee9fb0b7e6155.tar.gz
sway-c8dc4925d1e0f5d5086a4c15415ee9fb0b7e6155.tar.zst
sway-c8dc4925d1e0f5d5086a4c15415ee9fb0b7e6155.zip
Add IPC security policy command handlers
Diffstat (limited to 'sway/commands/ipc.c')
-rw-r--r--sway/commands/ipc.c140
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..e6ae27a4
--- /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
9struct 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
27struct 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
45struct 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_command_type type;
64 } types[] = {
65 { "command", IPC_COMMAND },
66 { "workspaces", IPC_GET_WORKSPACES },
67 { "outputs", IPC_GET_OUTPUTS },
68 { "tree", IPC_GET_TREE },
69 { "marks", IPC_GET_MARKS },
70 { "bar-config", IPC_GET_BAR_CONFIG },
71 { "inputs", IPC_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
94struct 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_command_type type;
113 } types[] = {
114 { "workspace", event_mask(IPC_EVENT_WORKSPACE) },
115 { "output", event_mask(IPC_EVENT_OUTPUT) },
116 { "mode", event_mask(IPC_EVENT_MODE) },
117 { "window", event_mask(IPC_EVENT_WINDOW) },
118 { "binding", event_mask(IPC_EVENT_BINDING) },
119 { "input", event_mask(IPC_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}