aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c88
1 files changed, 33 insertions, 55 deletions
diff --git a/sway/commands.c b/sway/commands.c
index fe1e98b5..5a1fd32e 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -42,7 +42,7 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type
42} 42}
43 43
44/* Keep alphabetized */ 44/* Keep alphabetized */
45static struct cmd_handler handlers[] = { 45static const struct cmd_handler handlers[] = {
46 { "assign", cmd_assign }, 46 { "assign", cmd_assign },
47 { "bar", cmd_bar }, 47 { "bar", cmd_bar },
48 { "bindcode", cmd_bindcode }, 48 { "bindcode", cmd_bindcode },
@@ -51,6 +51,7 @@ static struct cmd_handler handlers[] = {
51 { "client.background", cmd_client_noop }, 51 { "client.background", cmd_client_noop },
52 { "client.focused", cmd_client_focused }, 52 { "client.focused", cmd_client_focused },
53 { "client.focused_inactive", cmd_client_focused_inactive }, 53 { "client.focused_inactive", cmd_client_focused_inactive },
54 { "client.focused_tab_title", cmd_client_focused_tab_title },
54 { "client.placeholder", cmd_client_noop }, 55 { "client.placeholder", cmd_client_noop },
55 { "client.unfocused", cmd_client_unfocused }, 56 { "client.unfocused", cmd_client_unfocused },
56 { "client.urgent", cmd_client_urgent }, 57 { "client.urgent", cmd_client_urgent },
@@ -98,7 +99,7 @@ static struct cmd_handler handlers[] = {
98}; 99};
99 100
100/* Config-time only commands. Keep alphabetized */ 101/* Config-time only commands. Keep alphabetized */
101static struct cmd_handler config_handlers[] = { 102static const struct cmd_handler config_handlers[] = {
102 { "default_orientation", cmd_default_orientation }, 103 { "default_orientation", cmd_default_orientation },
103 { "include", cmd_include }, 104 { "include", cmd_include },
104 { "swaybg_command", cmd_swaybg_command }, 105 { "swaybg_command", cmd_swaybg_command },
@@ -108,7 +109,7 @@ static struct cmd_handler config_handlers[] = {
108}; 109};
109 110
110/* Runtime-only commands. Keep alphabetized */ 111/* Runtime-only commands. Keep alphabetized */
111static struct cmd_handler command_handlers[] = { 112static const struct cmd_handler command_handlers[] = {
112 { "border", cmd_border }, 113 { "border", cmd_border },
113 { "create_output", cmd_create_output }, 114 { "create_output", cmd_create_output },
114 { "exit", cmd_exit }, 115 { "exit", cmd_exit },
@@ -144,22 +145,22 @@ static int handler_compare(const void *_a, const void *_b) {
144 return strcasecmp(a->command, b->command); 145 return strcasecmp(a->command, b->command);
145} 146}
146 147
147struct cmd_handler *find_handler(char *line, struct cmd_handler *handlers, 148const struct cmd_handler *find_handler(char *line,
148 size_t handlers_size) { 149 const struct cmd_handler *handlers, size_t handlers_size) {
149 if (!handlers || !handlers_size) { 150 if (!handlers || !handlers_size) {
150 return NULL; 151 return NULL;
151 } 152 }
152 struct cmd_handler query = { .command = line }; 153 const struct cmd_handler query = { .command = line };
153 return bsearch(&query, handlers, 154 return bsearch(&query, handlers,
154 handlers_size / sizeof(struct cmd_handler), 155 handlers_size / sizeof(struct cmd_handler),
155 sizeof(struct cmd_handler), handler_compare); 156 sizeof(struct cmd_handler), handler_compare);
156} 157}
157 158
158static struct cmd_handler *find_handler_ex(char *line, 159static const struct cmd_handler *find_handler_ex(char *line,
159 struct cmd_handler *config_handlers, size_t config_handlers_size, 160 const struct cmd_handler *config_handlers, size_t config_handlers_size,
160 struct cmd_handler *command_handlers, size_t command_handlers_size, 161 const struct cmd_handler *command_handlers, size_t command_handlers_size,
161 struct cmd_handler *handlers, size_t handlers_size) { 162 const struct cmd_handler *handlers, size_t handlers_size) {
162 struct cmd_handler *handler = NULL; 163 const struct cmd_handler *handler = NULL;
163 if (config->reading) { 164 if (config->reading) {
164 handler = find_handler(line, config_handlers, config_handlers_size); 165 handler = find_handler(line, config_handlers, config_handlers_size);
165 } else if (config->active) { 166 } else if (config->active) {
@@ -168,16 +169,17 @@ static struct cmd_handler *find_handler_ex(char *line,
168 return handler ? handler : find_handler(line, handlers, handlers_size); 169 return handler ? handler : find_handler(line, handlers, handlers_size);
169} 170}
170 171
171static struct cmd_handler *find_core_handler(char *line) { 172static const struct cmd_handler *find_core_handler(char *line) {
172 return find_handler_ex(line, config_handlers, sizeof(config_handlers), 173 return find_handler_ex(line, config_handlers, sizeof(config_handlers),
173 command_handlers, sizeof(command_handlers), 174 command_handlers, sizeof(command_handlers),
174 handlers, sizeof(handlers)); 175 handlers, sizeof(handlers));
175} 176}
176 177
177static void set_config_node(struct sway_node *node) { 178static void set_config_node(struct sway_node *node, bool node_overridden) {
178 config->handler_context.node = node; 179 config->handler_context.node = node;
179 config->handler_context.container = NULL; 180 config->handler_context.container = NULL;
180 config->handler_context.workspace = NULL; 181 config->handler_context.workspace = NULL;
182 config->handler_context.node_overridden = node_overridden;
181 183
182 if (node == NULL) { 184 if (node == NULL) {
183 return; 185 return;
@@ -186,7 +188,7 @@ static void set_config_node(struct sway_node *node) {
186 switch (node->type) { 188 switch (node->type) {
187 case N_CONTAINER: 189 case N_CONTAINER:
188 config->handler_context.container = node->sway_container; 190 config->handler_context.container = node->sway_container;
189 config->handler_context.workspace = node->sway_container->workspace; 191 config->handler_context.workspace = node->sway_container->pending.workspace;
190 break; 192 break;
191 case N_WORKSPACE: 193 case N_WORKSPACE:
192 config->handler_context.workspace = node->sway_workspace; 194 config->handler_context.workspace = node->sway_workspace;
@@ -202,6 +204,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
202 char *cmd; 204 char *cmd;
203 char matched_delim = ';'; 205 char matched_delim = ';';
204 list_t *containers = NULL; 206 list_t *containers = NULL;
207 bool using_criteria = false;
205 208
206 if (seat == NULL) { 209 if (seat == NULL) {
207 // passing a NULL seat means we just pick the default seat 210 // passing a NULL seat means we just pick the default seat
@@ -225,7 +228,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
225 for (; isspace(*head); ++head) {} 228 for (; isspace(*head); ++head) {}
226 // Extract criteria (valid for this command list only). 229 // Extract criteria (valid for this command list only).
227 if (matched_delim == ';') { 230 if (matched_delim == ';') {
228 config->handler_context.using_criteria = false; 231 using_criteria = false;
229 if (*head == '[') { 232 if (*head == '[') {
230 char *error = NULL; 233 char *error = NULL;
231 struct criteria *criteria = criteria_parse(head, &error); 234 struct criteria *criteria = criteria_parse(head, &error);
@@ -239,7 +242,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
239 containers = criteria_get_containers(criteria); 242 containers = criteria_get_containers(criteria);
240 head += strlen(criteria->raw); 243 head += strlen(criteria->raw);
241 criteria_destroy(criteria); 244 criteria_destroy(criteria);
242 config->handler_context.using_criteria = true; 245 using_criteria = true;
243 // Skip leading whitespace 246 // Skip leading whitespace
244 for (; isspace(*head); ++head) {} 247 for (; isspace(*head); ++head) {}
245 } 248 }
@@ -265,7 +268,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
265 } 268 }
266 } 269 }
267 } 270 }
268 struct cmd_handler *handler = find_core_handler(argv[0]); 271 const struct cmd_handler *handler = find_core_handler(argv[0]);
269 if (!handler) { 272 if (!handler) {
270 list_add(res_list, cmd_results_new(CMD_INVALID, 273 list_add(res_list, cmd_results_new(CMD_INVALID,
271 "Unknown/invalid command '%s'", argv[0])); 274 "Unknown/invalid command '%s'", argv[0]));
@@ -278,11 +281,14 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
278 argv[i] = do_var_replacement(argv[i]); 281 argv[i] = do_var_replacement(argv[i]);
279 } 282 }
280 283
281 if (!config->handler_context.using_criteria) { 284
282 // The container or workspace which this command will run on. 285 if (!using_criteria) {
283 struct sway_node *node = con ? &con->node : 286 if (con) {
284 seat_get_focus_inactive(seat, &root->node); 287 set_config_node(&con->node, true);
285 set_config_node(node); 288 } else {
289 set_config_node(seat_get_focus_inactive(seat, &root->node),
290 false);
291 }
286 struct cmd_results *res = handler->handle(argc-1, argv+1); 292 struct cmd_results *res = handler->handle(argc-1, argv+1);
287 list_add(res_list, res); 293 list_add(res_list, res);
288 if (res->status == CMD_INVALID) { 294 if (res->status == CMD_INVALID) {
@@ -296,7 +302,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
296 struct cmd_results *fail_res = NULL; 302 struct cmd_results *fail_res = NULL;
297 for (int i = 0; i < containers->length; ++i) { 303 for (int i = 0; i < containers->length; ++i) {
298 struct sway_container *container = containers->items[i]; 304 struct sway_container *container = containers->items[i];
299 set_config_node(&container->node); 305 set_config_node(&container->node, true);
300 struct cmd_results *res = handler->handle(argc-1, argv+1); 306 struct cmd_results *res = handler->handle(argc-1, argv+1);
301 if (res->status == CMD_SUCCESS) { 307 if (res->status == CMD_SUCCESS) {
302 free_cmd_results(res); 308 free_cmd_results(res);
@@ -370,7 +376,7 @@ struct cmd_results *config_command(char *exec, char **new_block) {
370 376
371 // Determine the command handler 377 // Determine the command handler
372 sway_log(SWAY_INFO, "Config command: %s", exec); 378 sway_log(SWAY_INFO, "Config command: %s", exec);
373 struct cmd_handler *handler = find_core_handler(argv[0]); 379 const struct cmd_handler *handler = find_core_handler(argv[0]);
374 if (!handler || !handler->handle) { 380 if (!handler || !handler->handle) {
375 const char *error = handler 381 const char *error = handler
376 ? "Command '%s' is shimmed, but unimplemented" 382 ? "Command '%s' is shimmed, but unimplemented"
@@ -418,12 +424,12 @@ cleanup:
418} 424}
419 425
420struct cmd_results *config_subcommand(char **argv, int argc, 426struct cmd_results *config_subcommand(char **argv, int argc,
421 struct cmd_handler *handlers, size_t handlers_size) { 427 const struct cmd_handler *handlers, size_t handlers_size) {
422 char *command = join_args(argv, argc); 428 char *command = join_args(argv, argc);
423 sway_log(SWAY_DEBUG, "Subcommand: %s", command); 429 sway_log(SWAY_DEBUG, "Subcommand: %s", command);
424 free(command); 430 free(command);
425 431
426 struct cmd_handler *handler = find_handler(argv[0], handlers, 432 const struct cmd_handler *handler = find_handler(argv[0], handlers,
427 handlers_size); 433 handlers_size);
428 if (!handler) { 434 if (!handler) {
429 return cmd_results_new(CMD_INVALID, 435 return cmd_results_new(CMD_INVALID,
@@ -453,41 +459,13 @@ struct cmd_results *config_commands_command(char *exec) {
453 goto cleanup; 459 goto cleanup;
454 } 460 }
455 461
456 struct cmd_handler *handler = find_handler(cmd, NULL, 0); 462 const struct cmd_handler *handler = find_handler(cmd, NULL, 0);
457 if (!handler && strcmp(cmd, "*") != 0) { 463 if (!handler && strcmp(cmd, "*") != 0) {
458 results = cmd_results_new(CMD_INVALID, 464 results = cmd_results_new(CMD_INVALID,
459 "Unknown/invalid command '%s'", cmd); 465 "Unknown/invalid command '%s'", cmd);
460 goto cleanup; 466 goto cleanup;
461 } 467 }
462 468
463 enum command_context context = 0;
464
465 struct {
466 char *name;
467 enum command_context context;
468 } context_names[] = {
469 { "config", CONTEXT_CONFIG },
470 { "binding", CONTEXT_BINDING },
471 { "ipc", CONTEXT_IPC },
472 { "criteria", CONTEXT_CRITERIA },
473 { "all", CONTEXT_ALL },
474 };
475
476 for (int i = 1; i < argc; ++i) {
477 size_t j;
478 for (j = 0; j < sizeof(context_names) / sizeof(context_names[0]); ++j) {
479 if (strcmp(context_names[j].name, argv[i]) == 0) {
480 break;
481 }
482 }
483 if (j == sizeof(context_names) / sizeof(context_names[0])) {
484 results = cmd_results_new(CMD_INVALID,
485 "Invalid command context %s", argv[i]);
486 goto cleanup;
487 }
488 context |= context_names[j].context;
489 }
490
491 results = cmd_results_new(CMD_SUCCESS, NULL); 469 results = cmd_results_new(CMD_SUCCESS, NULL);
492 470
493cleanup: 471cleanup: