aboutsummaryrefslogtreecommitdiff
path: root/sway/commands/for_window.c
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2016-09-02 19:47:37 -0400
committerGitHub <noreply@github.com>2016-09-02 19:47:37 -0400
commit29820ff826013b595e8c15d9e933767b0c965beb (patch)
treee5bae8ecc19c438091478ad878a8297252c72a81 /sway/commands/for_window.c
parent4e6d7b125895955e3a84583c6d61f3eb2f8a4fe9 (diff)
parent65ace5dec5c24695501056376e227fb9b1f84a3a (diff)
Merge pull request #879 from zandrmartin/commands-refactor
refactor commands.c
Diffstat (limited to 'sway/commands/for_window.c')
-rw-r--r--sway/commands/for_window.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c
new file mode 100644
index 00000000..2ba0ea6c
--- /dev/null
+++ b/sway/commands/for_window.c
@@ -0,0 +1,37 @@
+#include <string.h>
+#include "sway/commands.h"
+#include "sway/criteria.h"
+#include "list.h"
+#include "log.h"
+#include "stringop.h"
+
+struct cmd_results *cmd_for_window(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) {
+ return error;
+ }
+ // add command to a criteria/command pair that is run against views when they appear.
+ char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1);
+
+ struct criteria *crit = malloc(sizeof(struct criteria));
+ crit->crit_raw = strdup(criteria);
+ crit->cmdlist = cmdlist;
+ crit->tokens = create_list();
+ char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw);
+
+ if (err_str) {
+ error = cmd_results_new(CMD_INVALID, "for_window", err_str);
+ free(err_str);
+ free_criteria(crit);
+ } else if (crit->tokens->length == 0) {
+ error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria");
+ free_criteria(crit);
+ } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) {
+ sway_log(L_DEBUG, "for_window: Duplicate, skipping.");
+ free_criteria(crit);
+ } else {
+ sway_log(L_DEBUG, "for_window: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist);
+ list_add(config->criteria, crit);
+ }
+ return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}