aboutsummaryrefslogtreecommitdiff
path: root/sway/commands
diff options
context:
space:
mode:
authorRyan Dwyer <ryandwyer1@gmail.com>2018-04-24 14:59:49 +1000
committerRyan Dwyer <ryandwyer1@gmail.com>2018-04-24 20:08:32 +1000
commit72767e1cc302e7aee9645c1eae3f51abe53fb378 (patch)
tree5220706816f7e4601f4213812ef1eb3e4cc7fa4b /sway/commands
parent38c44f2f27f218ed7e61c9910491eef78551d8ea (diff)
Implement criteria commands
Implements the following commands: * for_window [...] <cmdlist> * assign [...] <workspace>
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/assign.c57
-rw-r--r--sway/commands/for_window.c41
2 files changed, 98 insertions, 0 deletions
diff --git a/sway/commands/assign.c b/sway/commands/assign.c
new file mode 100644
index 00000000..bf0b1f63
--- /dev/null
+++ b/sway/commands/assign.c
@@ -0,0 +1,57 @@
+#define _XOPEN_SOURCE 700
+#include <stdio.h>
+#include <string.h>
+#include "sway/commands.h"
+#include "sway/criteria.h"
+#include "list.h"
+#include "log.h"
+
+struct cmd_results *cmd_assign(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "assign", EXPECTED_AT_LEAST, 2))) {
+ return error;
+ }
+
+ char *criteria = *argv++;
+
+ if (strncmp(*argv, "→", strlen("→")) == 0) {
+ if (argc < 3) {
+ return cmd_results_new(CMD_INVALID, "assign", "Missing workspace");
+ }
+ argv++;
+ }
+
+ char *movecmd = "move container to workspace ";
+ int arglen = strlen(movecmd) + strlen(*argv) + 1;
+ char *cmdlist = calloc(1, arglen);
+ if (!cmdlist) {
+ return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate command list");
+ }
+ snprintf(cmdlist, arglen, "%s%s", movecmd, *argv);
+
+ struct criteria *crit = malloc(sizeof(struct criteria));
+ if (!crit) {
+ free(cmdlist);
+ return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate 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, "assign", err_str);
+ free(err_str);
+ free_criteria(crit);
+ } else if (crit->tokens->length == 0) {
+ error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria");
+ free_criteria(crit);
+ } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) {
+ wlr_log(L_DEBUG, "assign: Duplicate, skipping.");
+ free_criteria(crit);
+ } else {
+ wlr_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist);
+ list_add(config->criteria, crit);
+ }
+ return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL);
+}
diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c
new file mode 100644
index 00000000..a6aa7187
--- /dev/null
+++ b/sway/commands/for_window.c
@@ -0,0 +1,41 @@
+#define _XOPEN_SOURCE 500
+#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));
+ if (!crit) {
+ return cmd_results_new(CMD_FAILURE, "for_window", "Unable to allocate 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) {
+ wlr_log(L_DEBUG, "for_window: Duplicate, skipping.");
+ free_criteria(crit);
+ } else {
+ wlr_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);
+}