aboutsummaryrefslogtreecommitdiffstats
path: root/sway/commands.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2016-12-02 08:10:03 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2016-12-02 08:10:03 -0500
commitf23880b1fdd70a21b04317c18208a1f3ce356839 (patch)
tree51a54d43531ac28ef014193abef8f4ccd7a78332 /sway/commands.c
parentEnforce mouse permissions (diff)
downloadsway-f23880b1fdd70a21b04317c18208a1f3ce356839.tar.gz
sway-f23880b1fdd70a21b04317c18208a1f3ce356839.tar.zst
sway-f23880b1fdd70a21b04317c18208a1f3ce356839.zip
Add support for command policies in config file
Diffstat (limited to 'sway/commands.c')
-rw-r--r--sway/commands.c82
1 files changed, 81 insertions, 1 deletions
diff --git a/sway/commands.c b/sway/commands.c
index e2bafcb2..0bfe9d13 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -26,6 +26,7 @@
26#include "sway/input_state.h" 26#include "sway/input_state.h"
27#include "sway/criteria.h" 27#include "sway/criteria.h"
28#include "sway/ipc-server.h" 28#include "sway/ipc-server.h"
29#include "sway/security.h"
29#include "sway/input.h" 30#include "sway/input.h"
30#include "sway/border.h" 31#include "sway/border.h"
31#include "stringop.h" 32#include "stringop.h"
@@ -158,6 +159,7 @@ static struct cmd_handler handlers[] = {
158 { "client.placeholder", cmd_client_placeholder }, 159 { "client.placeholder", cmd_client_placeholder },
159 { "client.unfocused", cmd_client_unfocused }, 160 { "client.unfocused", cmd_client_unfocused },
160 { "client.urgent", cmd_client_urgent }, 161 { "client.urgent", cmd_client_urgent },
162 { "commands", cmd_commands },
161 { "debuglog", cmd_debuglog }, 163 { "debuglog", cmd_debuglog },
162 { "default_orientation", cmd_orientation }, 164 { "default_orientation", cmd_orientation },
163 { "exec", cmd_exec }, 165 { "exec", cmd_exec },
@@ -460,7 +462,85 @@ struct cmd_results *config_command(char *exec, enum cmd_status block) {
460 } else { 462 } else {
461 results = cmd_results_new(CMD_INVALID, argv[0], "This command is shimmed, but unimplemented"); 463 results = cmd_results_new(CMD_INVALID, argv[0], "This command is shimmed, but unimplemented");
462 } 464 }
463 cleanup: 465
466cleanup:
467 free_argv(argc, argv);
468 return results;
469}
470
471struct cmd_results *config_commands_command(char *exec) {
472 struct cmd_results *results = NULL;
473 int argc;
474 char **argv = split_args(exec, &argc);
475 if (!argc) {
476 results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
477 goto cleanup;
478 }
479
480 // Find handler for the command this is setting a policy for
481 char *cmd = argv[0];
482
483 if (strcmp(cmd, "}") == 0) {
484 results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
485 goto cleanup;
486 }
487
488 struct cmd_handler *handler = find_handler(cmd, CMD_BLOCK_END);
489 if (!handler) {
490 char *input = cmd ? cmd : "(empty)";
491 results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");
492 goto cleanup;
493 }
494
495 enum command_context context = 0;
496
497 struct {
498 char *name;
499 enum command_context context;
500 } context_names[] = {
501 { "config", CONTEXT_CONFIG },
502 { "binding", CONTEXT_BINDING },
503 { "ipc", CONTEXT_IPC },
504 { "criteria", CONTEXT_CRITERIA },
505 { "all", CONTEXT_ALL },
506 };
507 size_t names_len = 5;
508
509 for (int i = 1; i < argc; ++i) {
510 size_t j;
511 for (j = 0; j < names_len; ++j) {
512 if (strcmp(context_names[j].name, argv[i]) == 0) {
513 break;
514 }
515 }
516 if (j == names_len) {
517 results = cmd_results_new(CMD_INVALID, cmd,
518 "Invalid command context %s", argv[i]);
519 goto cleanup;
520 }
521 context |= context_names[j].context;
522 }
523
524 struct command_policy *policy = NULL;
525 for (int i = 0; i < config->command_policies->length; ++i) {
526 struct command_policy *p = config->command_policies->items[i];
527 if (strcmp(p->command, cmd) == 0) {
528 policy = p;
529 break;
530 }
531 }
532 if (!policy) {
533 policy = alloc_command_policy(cmd);
534 list_add(config->command_policies, policy);
535 }
536 policy->context = context;
537
538 sway_log(L_INFO, "Set command policy for %s to %d",
539 policy->command, policy->context);
540
541 results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
542
543cleanup:
464 free_argv(argc, argv); 544 free_argv(argc, argv);
465 return results; 545 return results;
466} 546}