summaryrefslogtreecommitdiffstats
path: root/sway/criteria.c
diff options
context:
space:
mode:
authorLibravatar Calvin Lee <cyrus296@gmail.com>2017-04-04 21:20:27 -0600
committerLibravatar Calvin Lee <cyrus296@gmail.com>2017-04-05 22:07:23 -0600
commit069d37f987c4e323cdb9396f0d80ac83d00566ff (patch)
treeb883a5553107ab24d5346db42062518f9e7bdca9 /sway/criteria.c
parentMerge pull request #1145 from 4e554c4c/marks (diff)
downloadsway-069d37f987c4e323cdb9396f0d80ac83d00566ff.tar.gz
sway-069d37f987c4e323cdb9396f0d80ac83d00566ff.tar.zst
sway-069d37f987c4e323cdb9396f0d80ac83d00566ff.zip
Improve criteria handling
This commit changes how commands decide what container to act on. Commands get the current container though `current_container`, a global defined in sway/commands.c. If a criteria is given before a command, then the following command will be run once for every container the criteria matches with a reference to the matching container in 'current_container'. Commands should use this instead of `get_focused_container()` from now on. This commit also fixes a few (minor) mistakes made in implementing marks such as non-escaped arrows in sway(5) and calling the "mark" command "floating" by accident. It also cleans up `criteria.c` in a few places.
Diffstat (limited to 'sway/criteria.c')
-rw-r--r--sway/criteria.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/sway/criteria.c b/sway/criteria.c
index 3ffc48f0..bd99461d 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -245,7 +245,7 @@ ect_cleanup:
245 return error; 245 return error;
246} 246}
247 247
248int regex_cmp(const char *item, const regex_t *regex) { 248static int regex_cmp(const char *item, const regex_t *regex) {
249 return regexec(regex, item, 0, NULL, 0); 249 return regexec(regex, item, 0, NULL, 0);
250} 250}
251 251
@@ -272,7 +272,10 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
272 break; 272 break;
273 case CRIT_CON_MARK: 273 case CRIT_CON_MARK:
274 if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) { 274 if (crit->regex && cont->marks && (list_seq_find(cont->marks, (int (*)(const void *, const void *))regex_cmp, crit->regex) != -1)) {
275 ++matches; 275 // Make sure it isn't matching the NUL string
276 if ((strcmp(crit->raw, "") == 0) == (list_seq_find(cont->marks, (int (*)(const void *, const void *))strcmp, "") != -1)) {
277 ++matches;
278 }
276 } 279 }
277 break; 280 break;
278 case CRIT_ID: 281 case CRIT_ID:
@@ -285,7 +288,7 @@ static bool criteria_test(swayc_t *cont, list_t *tokens) {
285 case CRIT_INSTANCE: 288 case CRIT_INSTANCE:
286 if (!cont->instance) { 289 if (!cont->instance) {
287 // ignore 290 // ignore
288 } else if (strcmp(crit->raw, "focused") == 0) { 291 } else if (crit_is_focused(crit->raw)) {
289 swayc_t *focused = get_focused_view(&root_container); 292 swayc_t *focused = get_focused_view(&root_container);
290 if (focused->instance && strcmp(cont->instance, focused->instance) == 0) { 293 if (focused->instance && strcmp(cont->instance, focused->instance) == 0) {
291 matches++; 294 matches++;
@@ -373,3 +376,21 @@ list_t *criteria_for(swayc_t *cont) {
373 } 376 }
374 return matches; 377 return matches;
375} 378}
379
380struct list_tokens {
381 list_t *list;
382 list_t *tokens;
383};
384
385static void container_match_add(swayc_t *container, struct list_tokens *list_tokens) {
386 if (criteria_test(container, list_tokens->tokens)) {
387 list_add(list_tokens->list, container);
388 }
389}
390list_t *container_for(list_t *tokens) {
391 struct list_tokens list_tokens = (struct list_tokens){create_list(), tokens};
392
393 container_map(&root_container, (void (*)(swayc_t *, void *))container_match_add, &list_tokens);
394
395 return list_tokens.list;
396}