diff options
-rw-r--r-- | include/sway/criteria.h | 3 | ||||
-rw-r--r-- | sway/commands/for_window.c | 8 | ||||
-rw-r--r-- | sway/commands/no_focus.c | 9 | ||||
-rw-r--r-- | sway/criteria.c | 33 |
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 { | |||
40 | }; | 40 | }; |
41 | 41 | ||
42 | bool criteria_is_empty(struct criteria *criteria); | 42 | bool criteria_is_empty(struct criteria *criteria); |
43 | bool criteria_is_equal(struct criteria *left, struct criteria *right); | ||
44 | |||
45 | bool criteria_already_exists(struct criteria *criteria); | ||
43 | 46 | ||
44 | void criteria_destroy(struct criteria *criteria); | 47 | void criteria_destroy(struct criteria *criteria); |
45 | 48 | ||
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) { | |||
22 | criteria->type = CT_COMMAND; | 22 | criteria->type = CT_COMMAND; |
23 | criteria->cmdlist = join_args(argv + 1, argc - 1); | 23 | criteria->cmdlist = join_args(argv + 1, argc - 1); |
24 | 24 | ||
25 | // Check if it already exists | ||
26 | if (criteria_already_exists(criteria)) { | ||
27 | sway_log(SWAY_DEBUG, "for_window already exists: '%s' -> '%s'", | ||
28 | criteria->raw, criteria->cmdlist); | ||
29 | criteria_destroy(criteria); | ||
30 | return cmd_results_new(CMD_SUCCESS, NULL); | ||
31 | } | ||
32 | |||
25 | list_add(config->criteria, criteria); | 33 | list_add(config->criteria, criteria); |
26 | sway_log(SWAY_DEBUG, "for_window: '%s' -> '%s' added", criteria->raw, criteria->cmdlist); | 34 | sway_log(SWAY_DEBUG, "for_window: '%s' -> '%s' added", criteria->raw, criteria->cmdlist); |
27 | 35 | ||
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) { | |||
18 | return error; | 18 | return error; |
19 | } | 19 | } |
20 | 20 | ||
21 | |||
21 | criteria->type = CT_NO_FOCUS; | 22 | criteria->type = CT_NO_FOCUS; |
23 | |||
24 | // Check if it already exists | ||
25 | if (criteria_already_exists(criteria)) { | ||
26 | sway_log(SWAY_DEBUG, "no_focus already exists: '%s'", criteria->raw); | ||
27 | criteria_destroy(criteria); | ||
28 | return cmd_results_new(CMD_SUCCESS, NULL); | ||
29 | } | ||
30 | |||
22 | list_add(config->criteria, criteria); | 31 | list_add(config->criteria, criteria); |
23 | 32 | ||
24 | return cmd_results_new(CMD_SUCCESS, NULL); | 33 | 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: | |||
685 | criteria_destroy(criteria); | 685 | criteria_destroy(criteria); |
686 | return NULL; | 686 | return NULL; |
687 | } | 687 | } |
688 | |||
689 | bool criteria_is_equal(struct criteria *left, struct criteria *right) { | ||
690 | if (left->type != right->type) { | ||
691 | return false; | ||
692 | } | ||
693 | // XXX Only implemented for CT_NO_FOCUS for now. | ||
694 | if (left->type == CT_NO_FOCUS) { | ||
695 | return strcmp(left->raw, right->raw) == 0; | ||
696 | } | ||
697 | if (left->type == CT_COMMAND) { | ||
698 | return strcmp(left->raw, right->raw) == 0 | ||
699 | && strcmp(left->cmdlist, right->cmdlist) == 0; | ||
700 | } | ||
701 | return false; | ||
702 | } | ||
703 | |||
704 | bool criteria_already_exists(struct criteria *criteria) { | ||
705 | // XXX Only implemented for CT_NO_FOCUS and CT_COMMAND for now. | ||
706 | // While criteria_is_equal also obeys this limitation, this is a shortcut | ||
707 | // to avoid processing the list. | ||
708 | if (criteria->type != CT_NO_FOCUS && criteria->type != CT_COMMAND) { | ||
709 | return false; | ||
710 | } | ||
711 | |||
712 | list_t *criterias = config->criteria; | ||
713 | for (int i = 0; i < criterias->length; ++i) { | ||
714 | struct criteria *existing = criterias->items[i]; | ||
715 | if (criteria_is_equal(criteria, existing)) { | ||
716 | return true; | ||
717 | } | ||
718 | } | ||
719 | return false; | ||
720 | } | ||