aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c110
1 files changed, 45 insertions, 65 deletions
diff --git a/sway/commands.c b/sway/commands.c
index fe1e98b5..8d003dfa 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -1,4 +1,3 @@
1#define _POSIX_C_SOURCE 200809
2#include <ctype.h> 1#include <ctype.h>
3#include <stdarg.h> 2#include <stdarg.h>
4#include <stdlib.h> 3#include <stdlib.h>
@@ -42,15 +41,17 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type
42} 41}
43 42
44/* Keep alphabetized */ 43/* Keep alphabetized */
45static struct cmd_handler handlers[] = { 44static const struct cmd_handler handlers[] = {
46 { "assign", cmd_assign }, 45 { "assign", cmd_assign },
47 { "bar", cmd_bar }, 46 { "bar", cmd_bar },
48 { "bindcode", cmd_bindcode }, 47 { "bindcode", cmd_bindcode },
48 { "bindgesture", cmd_bindgesture },
49 { "bindswitch", cmd_bindswitch }, 49 { "bindswitch", cmd_bindswitch },
50 { "bindsym", cmd_bindsym }, 50 { "bindsym", cmd_bindsym },
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 },
@@ -91,6 +92,7 @@ static struct cmd_handler handlers[] = {
91 { "titlebar_border_thickness", cmd_titlebar_border_thickness }, 92 { "titlebar_border_thickness", cmd_titlebar_border_thickness },
92 { "titlebar_padding", cmd_titlebar_padding }, 93 { "titlebar_padding", cmd_titlebar_padding },
93 { "unbindcode", cmd_unbindcode }, 94 { "unbindcode", cmd_unbindcode },
95 { "unbindgesture", cmd_unbindgesture },
94 { "unbindswitch", cmd_unbindswitch }, 96 { "unbindswitch", cmd_unbindswitch },
95 { "unbindsym", cmd_unbindsym }, 97 { "unbindsym", cmd_unbindsym },
96 { "workspace", cmd_workspace }, 98 { "workspace", cmd_workspace },
@@ -98,9 +100,10 @@ static struct cmd_handler handlers[] = {
98}; 100};
99 101
100/* Config-time only commands. Keep alphabetized */ 102/* Config-time only commands. Keep alphabetized */
101static struct cmd_handler config_handlers[] = { 103static const struct cmd_handler config_handlers[] = {
102 { "default_orientation", cmd_default_orientation }, 104 { "default_orientation", cmd_default_orientation },
103 { "include", cmd_include }, 105 { "include", cmd_include },
106 { "primary_selection", cmd_primary_selection },
104 { "swaybg_command", cmd_swaybg_command }, 107 { "swaybg_command", cmd_swaybg_command },
105 { "swaynag_command", cmd_swaynag_command }, 108 { "swaynag_command", cmd_swaynag_command },
106 { "workspace_layout", cmd_workspace_layout }, 109 { "workspace_layout", cmd_workspace_layout },
@@ -108,7 +111,7 @@ static struct cmd_handler config_handlers[] = {
108}; 111};
109 112
110/* Runtime-only commands. Keep alphabetized */ 113/* Runtime-only commands. Keep alphabetized */
111static struct cmd_handler command_handlers[] = { 114static const struct cmd_handler command_handlers[] = {
112 { "border", cmd_border }, 115 { "border", cmd_border },
113 { "create_output", cmd_create_output }, 116 { "create_output", cmd_create_output },
114 { "exit", cmd_exit }, 117 { "exit", cmd_exit },
@@ -144,22 +147,22 @@ static int handler_compare(const void *_a, const void *_b) {
144 return strcasecmp(a->command, b->command); 147 return strcasecmp(a->command, b->command);
145} 148}
146 149
147struct cmd_handler *find_handler(char *line, struct cmd_handler *handlers, 150const struct cmd_handler *find_handler(const char *line,
148 size_t handlers_size) { 151 const struct cmd_handler *handlers, size_t handlers_size) {
149 if (!handlers || !handlers_size) { 152 if (!handlers || !handlers_size) {
150 return NULL; 153 return NULL;
151 } 154 }
152 struct cmd_handler query = { .command = line }; 155 const struct cmd_handler query = { .command = line };
153 return bsearch(&query, handlers, 156 return bsearch(&query, handlers,
154 handlers_size / sizeof(struct cmd_handler), 157 handlers_size / sizeof(struct cmd_handler),
155 sizeof(struct cmd_handler), handler_compare); 158 sizeof(struct cmd_handler), handler_compare);
156} 159}
157 160
158static struct cmd_handler *find_handler_ex(char *line, 161static const struct cmd_handler *find_handler_ex(char *line,
159 struct cmd_handler *config_handlers, size_t config_handlers_size, 162 const struct cmd_handler *config_handlers, size_t config_handlers_size,
160 struct cmd_handler *command_handlers, size_t command_handlers_size, 163 const struct cmd_handler *command_handlers, size_t command_handlers_size,
161 struct cmd_handler *handlers, size_t handlers_size) { 164 const struct cmd_handler *handlers, size_t handlers_size) {
162 struct cmd_handler *handler = NULL; 165 const struct cmd_handler *handler = NULL;
163 if (config->reading) { 166 if (config->reading) {
164 handler = find_handler(line, config_handlers, config_handlers_size); 167 handler = find_handler(line, config_handlers, config_handlers_size);
165 } else if (config->active) { 168 } else if (config->active) {
@@ -168,16 +171,17 @@ static struct cmd_handler *find_handler_ex(char *line,
168 return handler ? handler : find_handler(line, handlers, handlers_size); 171 return handler ? handler : find_handler(line, handlers, handlers_size);
169} 172}
170 173
171static struct cmd_handler *find_core_handler(char *line) { 174static const struct cmd_handler *find_core_handler(char *line) {
172 return find_handler_ex(line, config_handlers, sizeof(config_handlers), 175 return find_handler_ex(line, config_handlers, sizeof(config_handlers),
173 command_handlers, sizeof(command_handlers), 176 command_handlers, sizeof(command_handlers),
174 handlers, sizeof(handlers)); 177 handlers, sizeof(handlers));
175} 178}
176 179
177static void set_config_node(struct sway_node *node) { 180static void set_config_node(struct sway_node *node, bool node_overridden) {
178 config->handler_context.node = node; 181 config->handler_context.node = node;
179 config->handler_context.container = NULL; 182 config->handler_context.container = NULL;
180 config->handler_context.workspace = NULL; 183 config->handler_context.workspace = NULL;
184 config->handler_context.node_overridden = node_overridden;
181 185
182 if (node == NULL) { 186 if (node == NULL) {
183 return; 187 return;
@@ -186,7 +190,7 @@ static void set_config_node(struct sway_node *node) {
186 switch (node->type) { 190 switch (node->type) {
187 case N_CONTAINER: 191 case N_CONTAINER:
188 config->handler_context.container = node->sway_container; 192 config->handler_context.container = node->sway_container;
189 config->handler_context.workspace = node->sway_container->workspace; 193 config->handler_context.workspace = node->sway_container->pending.workspace;
190 break; 194 break;
191 case N_WORKSPACE: 195 case N_WORKSPACE:
192 config->handler_context.workspace = node->sway_workspace; 196 config->handler_context.workspace = node->sway_workspace;
@@ -202,6 +206,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
202 char *cmd; 206 char *cmd;
203 char matched_delim = ';'; 207 char matched_delim = ';';
204 list_t *containers = NULL; 208 list_t *containers = NULL;
209 bool using_criteria = false;
205 210
206 if (seat == NULL) { 211 if (seat == NULL) {
207 // passing a NULL seat means we just pick the default seat 212 // passing a NULL seat means we just pick the default seat
@@ -225,7 +230,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
225 for (; isspace(*head); ++head) {} 230 for (; isspace(*head); ++head) {}
226 // Extract criteria (valid for this command list only). 231 // Extract criteria (valid for this command list only).
227 if (matched_delim == ';') { 232 if (matched_delim == ';') {
228 config->handler_context.using_criteria = false; 233 using_criteria = false;
229 if (*head == '[') { 234 if (*head == '[') {
230 char *error = NULL; 235 char *error = NULL;
231 struct criteria *criteria = criteria_parse(head, &error); 236 struct criteria *criteria = criteria_parse(head, &error);
@@ -239,7 +244,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
239 containers = criteria_get_containers(criteria); 244 containers = criteria_get_containers(criteria);
240 head += strlen(criteria->raw); 245 head += strlen(criteria->raw);
241 criteria_destroy(criteria); 246 criteria_destroy(criteria);
242 config->handler_context.using_criteria = true; 247 using_criteria = true;
243 // Skip leading whitespace 248 // Skip leading whitespace
244 for (; isspace(*head); ++head) {} 249 for (; isspace(*head); ++head) {}
245 } 250 }
@@ -265,7 +270,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
265 } 270 }
266 } 271 }
267 } 272 }
268 struct cmd_handler *handler = find_core_handler(argv[0]); 273 const struct cmd_handler *handler = find_core_handler(argv[0]);
269 if (!handler) { 274 if (!handler) {
270 list_add(res_list, cmd_results_new(CMD_INVALID, 275 list_add(res_list, cmd_results_new(CMD_INVALID,
271 "Unknown/invalid command '%s'", argv[0])); 276 "Unknown/invalid command '%s'", argv[0]));
@@ -278,11 +283,14 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
278 argv[i] = do_var_replacement(argv[i]); 283 argv[i] = do_var_replacement(argv[i]);
279 } 284 }
280 285
281 if (!config->handler_context.using_criteria) { 286
282 // The container or workspace which this command will run on. 287 if (!using_criteria) {
283 struct sway_node *node = con ? &con->node : 288 if (con) {
284 seat_get_focus_inactive(seat, &root->node); 289 set_config_node(&con->node, true);
285 set_config_node(node); 290 } else {
291 set_config_node(seat_get_focus_inactive(seat, &root->node),
292 false);
293 }
286 struct cmd_results *res = handler->handle(argc-1, argv+1); 294 struct cmd_results *res = handler->handle(argc-1, argv+1);
287 list_add(res_list, res); 295 list_add(res_list, res);
288 if (res->status == CMD_INVALID) { 296 if (res->status == CMD_INVALID) {
@@ -296,7 +304,7 @@ list_t *execute_command(char *_exec, struct sway_seat *seat,
296 struct cmd_results *fail_res = NULL; 304 struct cmd_results *fail_res = NULL;
297 for (int i = 0; i < containers->length; ++i) { 305 for (int i = 0; i < containers->length; ++i) {
298 struct sway_container *container = containers->items[i]; 306 struct sway_container *container = containers->items[i];
299 set_config_node(&container->node); 307 set_config_node(&container->node, true);
300 struct cmd_results *res = handler->handle(argc-1, argv+1); 308 struct cmd_results *res = handler->handle(argc-1, argv+1);
301 if (res->status == CMD_SUCCESS) { 309 if (res->status == CMD_SUCCESS) {
302 free_cmd_results(res); 310 free_cmd_results(res);
@@ -370,12 +378,15 @@ struct cmd_results *config_command(char *exec, char **new_block) {
370 378
371 // Determine the command handler 379 // Determine the command handler
372 sway_log(SWAY_INFO, "Config command: %s", exec); 380 sway_log(SWAY_INFO, "Config command: %s", exec);
373 struct cmd_handler *handler = find_core_handler(argv[0]); 381 const struct cmd_handler *handler = find_core_handler(argv[0]);
374 if (!handler || !handler->handle) { 382 if (!handler || !handler->handle) {
375 const char *error = handler 383 if (handler) {
376 ? "Command '%s' is shimmed, but unimplemented" 384 results = cmd_results_new(CMD_INVALID,
377 : "Unknown/invalid command '%s'"; 385 "Command '%s' is shimmed, but unimplemented", argv[0]);
378 results = cmd_results_new(CMD_INVALID, error, argv[0]); 386 } else {
387 results = cmd_results_new(CMD_INVALID,
388 "Unknown/invalid command '%s'", argv[0]);
389 }
379 goto cleanup; 390 goto cleanup;
380 } 391 }
381 392
@@ -401,6 +412,7 @@ struct cmd_results *config_command(char *exec, char **new_block) {
401 && handler->handle != cmd_bindsym 412 && handler->handle != cmd_bindsym
402 && handler->handle != cmd_bindcode 413 && handler->handle != cmd_bindcode
403 && handler->handle != cmd_bindswitch 414 && handler->handle != cmd_bindswitch
415 && handler->handle != cmd_bindgesture
404 && handler->handle != cmd_set 416 && handler->handle != cmd_set
405 && handler->handle != cmd_for_window 417 && handler->handle != cmd_for_window
406 && (*argv[i] == '\"' || *argv[i] == '\'')) { 418 && (*argv[i] == '\"' || *argv[i] == '\'')) {
@@ -418,12 +430,12 @@ cleanup:
418} 430}
419 431
420struct cmd_results *config_subcommand(char **argv, int argc, 432struct cmd_results *config_subcommand(char **argv, int argc,
421 struct cmd_handler *handlers, size_t handlers_size) { 433 const struct cmd_handler *handlers, size_t handlers_size) {
422 char *command = join_args(argv, argc); 434 char *command = join_args(argv, argc);
423 sway_log(SWAY_DEBUG, "Subcommand: %s", command); 435 sway_log(SWAY_DEBUG, "Subcommand: %s", command);
424 free(command); 436 free(command);
425 437
426 struct cmd_handler *handler = find_handler(argv[0], handlers, 438 const struct cmd_handler *handler = find_handler(argv[0], handlers,
427 handlers_size); 439 handlers_size);
428 if (!handler) { 440 if (!handler) {
429 return cmd_results_new(CMD_INVALID, 441 return cmd_results_new(CMD_INVALID,
@@ -453,41 +465,13 @@ struct cmd_results *config_commands_command(char *exec) {
453 goto cleanup; 465 goto cleanup;
454 } 466 }
455 467
456 struct cmd_handler *handler = find_handler(cmd, NULL, 0); 468 const struct cmd_handler *handler = find_handler(cmd, NULL, 0);
457 if (!handler && strcmp(cmd, "*") != 0) { 469 if (!handler && strcmp(cmd, "*") != 0) {
458 results = cmd_results_new(CMD_INVALID, 470 results = cmd_results_new(CMD_INVALID,
459 "Unknown/invalid command '%s'", cmd); 471 "Unknown/invalid command '%s'", cmd);
460 goto cleanup; 472 goto cleanup;
461 } 473 }
462 474
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); 475 results = cmd_results_new(CMD_SUCCESS, NULL);
492 476
493cleanup: 477cleanup:
@@ -504,14 +488,10 @@ struct cmd_results *cmd_results_new(enum cmd_status status,
504 } 488 }
505 results->status = status; 489 results->status = status;
506 if (format) { 490 if (format) {
507 char *error = malloc(256);
508 va_list args; 491 va_list args;
509 va_start(args, format); 492 va_start(args, format);
510 if (error) { 493 results->error = vformat_str(format, args);
511 vsnprintf(error, 256, format, args);
512 }
513 va_end(args); 494 va_end(args);
514 results->error = error;
515 } else { 495 } else {
516 results->error = NULL; 496 results->error = NULL;
517 } 497 }