aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/sway/criteria.h3
-rw-r--r--sway/commands/for_window.c8
-rw-r--r--sway/commands/no_focus.c9
-rw-r--r--sway/criteria.c33
4 files changed, 53 insertions, 0 deletions
diff --git a/include/sway/criteria.h b/include/sway/criteria.h
index 8a1d9e5e..dc8dcb98 100644
--- a/include/sway/criteria.h
+++ b/include/sway/criteria.h
@@ -40,6 +40,9 @@ struct criteria {
};
bool criteria_is_empty(struct criteria *criteria);
+bool criteria_is_equal(struct criteria *left, struct criteria *right);
+
+bool criteria_already_exists(struct criteria *criteria);
void criteria_destroy(struct criteria *criteria);
diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c
index 63cba099..ee9f4647 100644
--- a/sway/commands/for_window.c
+++ b/sway/commands/for_window.c
@@ -22,6 +22,14 @@ struct cmd_results *cmd_for_window(int argc, char **argv) {
criteria->type = CT_COMMAND;
criteria->cmdlist = join_args(argv + 1, argc - 1);
+ // Check if it already exists
+ if (criteria_already_exists(criteria)) {
+ sway_log(SWAY_DEBUG, "for_window already exists: '%s' -> '%s'",
+ criteria->raw, criteria->cmdlist);
+ criteria_destroy(criteria);
+ return cmd_results_new(CMD_SUCCESS, NULL);
+ }
+
list_add(config->criteria, criteria);
sway_log(SWAY_DEBUG, "for_window: '%s' -> '%s' added", criteria->raw, criteria->cmdlist);
diff --git a/sway/commands/no_focus.c b/sway/commands/no_focus.c
index 07e824a1..2001e04f 100644
--- a/sway/commands/no_focus.c
+++ b/sway/commands/no_focus.c
@@ -18,7 +18,16 @@ struct cmd_results *cmd_no_focus(int argc, char **argv) {
return error;
}
+
criteria->type = CT_NO_FOCUS;
+
+ // Check if it already exists
+ if (criteria_already_exists(criteria)) {
+ sway_log(SWAY_DEBUG, "no_focus already exists: '%s'", criteria->raw);
+ criteria_destroy(criteria);
+ return cmd_results_new(CMD_SUCCESS, NULL);
+ }
+
list_add(config->criteria, criteria);
return cmd_results_new(CMD_SUCCESS, NULL);
diff --git a/sway/criteria.c b/sway/criteria.c
index 11b41f35..b2582851 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -685,3 +685,36 @@ cleanup:
criteria_destroy(criteria);
return NULL;
}
+
+bool criteria_is_equal(struct criteria *left, struct criteria *right) {
+ if (left->type != right->type) {
+ return false;
+ }
+ // XXX Only implemented for CT_NO_FOCUS for now.
+ if (left->type == CT_NO_FOCUS) {
+ return strcmp(left->raw, right->raw) == 0;
+ }
+ if (left->type == CT_COMMAND) {
+ return strcmp(left->raw, right->raw) == 0
+ && strcmp(left->cmdlist, right->cmdlist) == 0;
+ }
+ return false;
+}
+
+bool criteria_already_exists(struct criteria *criteria) {
+ // XXX Only implemented for CT_NO_FOCUS and CT_COMMAND for now.
+ // While criteria_is_equal also obeys this limitation, this is a shortcut
+ // to avoid processing the list.
+ if (criteria->type != CT_NO_FOCUS && criteria->type != CT_COMMAND) {
+ return false;
+ }
+
+ list_t *criterias = config->criteria;
+ for (int i = 0; i < criterias->length; ++i) {
+ struct criteria *existing = criterias->items[i];
+ if (criteria_is_equal(criteria, existing)) {
+ return true;
+ }
+ }
+ return false;
+}