aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/criteria.h73
-rw-r--r--include/sway/tree/view.h23
-rw-r--r--sway/commands.c42
-rw-r--r--sway/commands/assign.c58
-rw-r--r--sway/commands/for_window.c35
-rw-r--r--sway/criteria.c794
-rw-r--r--sway/desktop/wl_shell.c4
-rw-r--r--sway/desktop/xdg_shell_v6.c4
-rw-r--r--sway/desktop/xwayland.c69
-rw-r--r--sway/sway.5.txt43
-rw-r--r--sway/tree/view.c104
11 files changed, 512 insertions, 737 deletions
diff --git a/include/sway/criteria.h b/include/sway/criteria.h
index 74da132c..ec256ddb 100644
--- a/include/sway/criteria.h
+++ b/include/sway/criteria.h
@@ -1,61 +1,42 @@
1#ifndef _SWAY_CRITERIA_H 1#ifndef _SWAY_CRITERIA_H
2#define _SWAY_CRITERIA_H 2#define _SWAY_CRITERIA_H
3 3
4#include <pcre.h> 4#include "tree/container.h"
5#include "list.h" 5#include "list.h"
6#include "tree/view.h"
7
8enum criteria_type {
9 CT_COMMAND = 1 << 0,
10 CT_ASSIGN_OUTPUT = 1 << 1,
11 CT_ASSIGN_WORKSPACE = 1 << 2,
12};
13 6
7/**
8 * Maps criteria (as a list of criteria tokens) to a command list.
9 *
10 * A list of tokens together represent a single criteria string (e.g.
11 * '[class="abc" title="xyz"]' becomes two criteria tokens).
12 *
13 * for_window: Views matching all criteria will have the bound command list
14 * executed on them.
15 *
16 * Set via `for_window <criteria> <cmd list>`.
17 */
14struct criteria { 18struct criteria {
15 enum criteria_type type; 19 list_t *tokens; // struct crit_token, contains compiled regex.
16 char *raw; // entire criteria string (for logging) 20 char *crit_raw; // entire criteria string (for logging)
21
17 char *cmdlist; 22 char *cmdlist;
18 char *target; // workspace or output name for `assign` criteria
19
20 pcre *title;
21 pcre *app_id;
22 pcre *class;
23 pcre *instance;
24 pcre *con_mark;
25 uint32_t con_id; // internal ID
26 uint32_t id; // X11 window ID
27 pcre *window_role;
28 uint32_t window_type;
29 bool floating;
30 bool tiling;
31 char urgent; // 'l' for latest or 'o' for oldest
32 char *workspace;
33}; 23};
34 24
35bool criteria_is_empty(struct criteria *criteria); 25int criteria_cmp(const void *item, const void *data);
26void free_criteria(struct criteria *crit);
36 27
37void criteria_destroy(struct criteria *criteria); 28// Pouplate list with crit_tokens extracted from criteria string, returns error
29// string or NULL if successful.
30char *extract_crit_tokens(list_t *tokens, const char *criteria);
38 31
39/** 32// Returns list of criteria that match given container. These criteria have
40 * Generate a criteria struct from a raw criteria string such as 33// been set with `for_window` commands and have an associated cmdlist.
41 * [class="foo" instance="bar"] (brackets inclusive). 34list_t *criteria_for(struct sway_container *cont);
42 *
43 * The error argument is expected to be an address of a null pointer. If an
44 * error is encountered, the function will return NULL and the pointer will be
45 * changed to point to the error string. This string should be freed afterwards.
46 */
47struct criteria *criteria_parse(char *raw, char **error);
48 35
49/** 36// Returns a list of all containers that match the given list of tokens.
50 * Compile a list of criterias matching the given view. 37list_t *container_for_crit_tokens(list_t *tokens);
51 *
52 * Criteria types can be bitwise ORed.
53 */
54list_t *criteria_for_view(struct sway_view *view, enum criteria_type types);
55 38
56/** 39// Returns true if any criteria in the given list matches this container
57 * Compile a list of views matching the given criteria. 40bool criteria_any(struct sway_container *cont, list_t *criteria);
58 */
59list_t *criteria_get_views(struct criteria *criteria);
60 41
61#endif 42#endif
diff --git a/include/sway/tree/view.h b/include/sway/tree/view.h
index 65cadea1..4ecd8c44 100644
--- a/include/sway/tree/view.h
+++ b/include/sway/tree/view.h
@@ -20,15 +20,11 @@ enum sway_view_prop {
20 VIEW_PROP_APP_ID, 20 VIEW_PROP_APP_ID,
21 VIEW_PROP_CLASS, 21 VIEW_PROP_CLASS,
22 VIEW_PROP_INSTANCE, 22 VIEW_PROP_INSTANCE,
23 VIEW_PROP_WINDOW_TYPE,
24 VIEW_PROP_WINDOW_ROLE,
25 VIEW_PROP_X11_WINDOW_ID,
26}; 23};
27 24
28struct sway_view_impl { 25struct sway_view_impl {
29 const char *(*get_string_prop)(struct sway_view *view, 26 const char *(*get_prop)(struct sway_view *view,
30 enum sway_view_prop prop); 27 enum sway_view_prop prop);
31 uint32_t (*get_int_prop)(struct sway_view *view, enum sway_view_prop prop);
32 void (*configure)(struct sway_view *view, double ox, double oy, int width, 28 void (*configure)(struct sway_view *view, double ox, double oy, int width,
33 int height); 29 int height);
34 void (*set_activated)(struct sway_view *view, bool activated); 30 void (*set_activated)(struct sway_view *view, bool activated);
@@ -56,8 +52,6 @@ struct sway_view {
56 enum sway_container_border border; 52 enum sway_container_border border;
57 int border_thickness; 53 int border_thickness;
58 54
59 list_t *executed_criteria;
60
61 union { 55 union {
62 struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6; 56 struct wlr_xdg_surface_v6 *wlr_xdg_surface_v6;
63 struct wlr_xwayland_surface *wlr_xwayland_surface; 57 struct wlr_xwayland_surface *wlr_xwayland_surface;
@@ -97,9 +91,6 @@ struct sway_xwayland_view {
97 struct wl_listener request_maximize; 91 struct wl_listener request_maximize;
98 struct wl_listener request_configure; 92 struct wl_listener request_configure;
99 struct wl_listener request_fullscreen; 93 struct wl_listener request_fullscreen;
100 struct wl_listener set_title;
101 struct wl_listener set_class;
102 struct wl_listener set_window_type;
103 struct wl_listener map; 94 struct wl_listener map;
104 struct wl_listener unmap; 95 struct wl_listener unmap;
105 struct wl_listener destroy; 96 struct wl_listener destroy;
@@ -174,12 +165,6 @@ const char *view_get_class(struct sway_view *view);
174 165
175const char *view_get_instance(struct sway_view *view); 166const char *view_get_instance(struct sway_view *view);
176 167
177uint32_t view_get_x11_window_id(struct sway_view *view);
178
179const char *view_get_window_role(struct sway_view *view);
180
181uint32_t view_get_window_type(struct sway_view *view);
182
183const char *view_get_type(struct sway_view *view); 168const char *view_get_type(struct sway_view *view);
184 169
185void view_configure(struct sway_view *view, double ox, double oy, int width, 170void view_configure(struct sway_view *view, double ox, double oy, int width,
@@ -232,10 +217,4 @@ void view_child_destroy(struct sway_view_child *child);
232 */ 217 */
233void view_update_title(struct sway_view *view, bool force); 218void view_update_title(struct sway_view *view, bool force);
234 219
235/**
236 * Run any criteria that match the view and haven't been run on this view
237 * before.
238 */
239void view_execute_criteria(struct sway_view *view);
240
241#endif 220#endif
diff --git a/sway/commands.c b/sway/commands.c
index 811f6cfa..2e1cdc2c 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -12,7 +12,6 @@
12#include "sway/security.h" 12#include "sway/security.h"
13#include "sway/input/input-manager.h" 13#include "sway/input/input-manager.h"
14#include "sway/input/seat.h" 14#include "sway/input/seat.h"
15#include "sway/tree/view.h"
16#include "stringop.h" 15#include "stringop.h"
17#include "log.h" 16#include "log.h"
18 17
@@ -284,7 +283,7 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat) {
284 char *head = exec; 283 char *head = exec;
285 char *cmdlist; 284 char *cmdlist;
286 char *cmd; 285 char *cmd;
287 list_t *views = NULL; 286 list_t *containers = NULL;
288 287
289 if (seat == NULL) { 288 if (seat == NULL) {
290 // passing a NULL seat means we just pick the default seat 289 // passing a NULL seat means we just pick the default seat
@@ -301,18 +300,31 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat) {
301 // Extract criteria (valid for this command list only). 300 // Extract criteria (valid for this command list only).
302 bool has_criteria = false; 301 bool has_criteria = false;
303 if (*head == '[') { 302 if (*head == '[') {
304 char *error = NULL; 303 has_criteria = true;
305 struct criteria *criteria = criteria_parse(head, &error); 304 ++head;
306 if (!criteria) { 305 char *criteria_string = argsep(&head, "]");
307 results = cmd_results_new(CMD_INVALID, head, 306 if (head) {
308 "%s", error); 307 ++head;
309 free(error); 308 list_t *tokens = create_list();
309 char *error;
310
311 if ((error = extract_crit_tokens(tokens, criteria_string))) {
312 wlr_log(L_DEBUG, "criteria string parse error: %s", error);
313 results = cmd_results_new(CMD_INVALID, criteria_string,
314 "Can't parse criteria string: %s", error);
315 free(error);
316 free(tokens);
317 goto cleanup;
318 }
319 containers = container_for_crit_tokens(tokens);
320
321 free(tokens);
322 } else {
323 if (!results) {
324 results = cmd_results_new(CMD_INVALID, criteria_string, "Unmatched [");
325 }
310 goto cleanup; 326 goto cleanup;
311 } 327 }
312 views = criteria_get_views(criteria);
313 head += strlen(criteria->raw);
314 criteria_destroy(criteria);
315 has_criteria = true;
316 // Skip leading whitespace 328 // Skip leading whitespace
317 head += strspn(head, whitespace); 329 head += strspn(head, whitespace);
318 } 330 }
@@ -369,9 +381,8 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat) {
369 } 381 }
370 free_cmd_results(res); 382 free_cmd_results(res);
371 } else { 383 } else {
372 for (int i = 0; i < views->length; ++i) { 384 for (int i = 0; i < containers->length; ++i) {
373 struct sway_view *view = views->items[i]; 385 config->handler_context.current_container = containers->items[i];
374 config->handler_context.current_container = view->swayc;
375 struct cmd_results *res = handler->handle(argc-1, argv+1); 386 struct cmd_results *res = handler->handle(argc-1, argv+1);
376 if (res->status != CMD_SUCCESS) { 387 if (res->status != CMD_SUCCESS) {
377 free_argv(argc, argv); 388 free_argv(argc, argv);
@@ -389,7 +400,6 @@ struct cmd_results *execute_command(char *_exec, struct sway_seat *seat) {
389 } while(head); 400 } while(head);
390cleanup: 401cleanup:
391 free(exec); 402 free(exec);
392 free(views);
393 if (!results) { 403 if (!results) {
394 results = cmd_results_new(CMD_SUCCESS, NULL, NULL); 404 results = cmd_results_new(CMD_SUCCESS, NULL, NULL);
395 } 405 }
diff --git a/sway/commands/assign.c b/sway/commands/assign.c
index 9d15e166..eb7329aa 100644
--- a/sway/commands/assign.c
+++ b/sway/commands/assign.c
@@ -5,7 +5,6 @@
5#include "sway/criteria.h" 5#include "sway/criteria.h"
6#include "list.h" 6#include "list.h"
7#include "log.h" 7#include "log.h"
8#include "stringop.h"
9 8
10struct cmd_results *cmd_assign(int argc, char **argv) { 9struct cmd_results *cmd_assign(int argc, char **argv) {
11 struct cmd_results *error = NULL; 10 struct cmd_results *error = NULL;
@@ -13,39 +12,46 @@ struct cmd_results *cmd_assign(int argc, char **argv) {
13 return error; 12 return error;
14 } 13 }
15 14
16 // Create criteria 15 char *criteria = *argv++;
17 char *err_str = NULL;
18 struct criteria *criteria = criteria_parse(argv[0], &err_str);
19 if (!criteria) {
20 error = cmd_results_new(CMD_INVALID, "assign", err_str);
21 free(err_str);
22 return error;
23 }
24
25 ++argv;
26 int target_len = argc - 1;
27 16
28 if (strncmp(*argv, "→", strlen("→")) == 0) { 17 if (strncmp(*argv, "→", strlen("→")) == 0) {
29 if (argc < 3) { 18 if (argc < 3) {
30 return cmd_results_new(CMD_INVALID, "assign", "Missing workspace"); 19 return cmd_results_new(CMD_INVALID, "assign", "Missing workspace");
31 } 20 }
32 ++argv; 21 argv++;
33 --target_len;
34 } 22 }
35 23
36 if (strcmp(*argv, "output") == 0) { 24 char *movecmd = "move container to workspace ";
37 criteria->type = CT_ASSIGN_OUTPUT; 25 size_t arglen = strlen(movecmd) + strlen(*argv) + 1;
38 ++argv; 26 char *cmdlist = calloc(1, arglen);
39 --target_len; 27 if (!cmdlist) {
40 } else { 28 return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate command list");
41 criteria->type = CT_ASSIGN_WORKSPACE;
42 } 29 }
30 snprintf(cmdlist, arglen, "%s%s", movecmd, *argv);
43 31
44 criteria->target = join_args(argv, target_len); 32 struct criteria *crit = malloc(sizeof(struct criteria));
45 33 if (!crit) {
46 list_add(config->criteria, criteria); 34 free(cmdlist);
47 wlr_log(L_DEBUG, "assign: '%s' -> '%s' added", criteria->raw, 35 return cmd_results_new(CMD_FAILURE, "assign", "Unable to allocate criteria");
48 criteria->target); 36 }
37 crit->crit_raw = strdup(criteria);
38 crit->cmdlist = cmdlist;
39 crit->tokens = create_list();
40 char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw);
49 41
50 return cmd_results_new(CMD_SUCCESS, NULL, NULL); 42 if (err_str) {
43 error = cmd_results_new(CMD_INVALID, "assign", err_str);
44 free(err_str);
45 free_criteria(crit);
46 } else if (crit->tokens->length == 0) {
47 error = cmd_results_new(CMD_INVALID, "assign", "Found no name/value pairs in criteria");
48 free_criteria(crit);
49 } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) {
50 wlr_log(L_DEBUG, "assign: Duplicate, skipping.");
51 free_criteria(crit);
52 } else {
53 wlr_log(L_DEBUG, "assign: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist);
54 list_add(config->criteria, crit);
55 }
56 return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL);
51} 57}
diff --git a/sway/commands/for_window.c b/sway/commands/for_window.c
index 8c425a1d..dd5461f0 100644
--- a/sway/commands/for_window.c
+++ b/sway/commands/for_window.c
@@ -11,20 +11,31 @@ struct cmd_results *cmd_for_window(int argc, char **argv) {
11 if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) { 11 if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) {
12 return error; 12 return error;
13 } 13 }
14 // add command to a criteria/command pair that is run against views when they appear.
15 char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1);
14 16
15 char *err_str = NULL; 17 struct criteria *crit = calloc(sizeof(struct criteria), 1);
16 struct criteria *criteria = criteria_parse(argv[0], &err_str); 18 if (!crit) {
17 if (!criteria) { 19 return cmd_results_new(CMD_FAILURE, "for_window", "Unable to allocate criteria");
20 }
21 crit->crit_raw = strdup(criteria);
22 crit->cmdlist = cmdlist;
23 crit->tokens = create_list();
24 char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw);
25
26 if (err_str) {
18 error = cmd_results_new(CMD_INVALID, "for_window", err_str); 27 error = cmd_results_new(CMD_INVALID, "for_window", err_str);
19 free(err_str); 28 free(err_str);
20 return error; 29 free_criteria(crit);
30 } else if (crit->tokens->length == 0) {
31 error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria");
32 free_criteria(crit);
33 } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) {
34 wlr_log(L_DEBUG, "for_window: Duplicate, skipping.");
35 free_criteria(crit);
36 } else {
37 wlr_log(L_DEBUG, "for_window: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist);
38 list_add(config->criteria, crit);
21 } 39 }
22 40 return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL);
23 criteria->type = CT_COMMAND;
24 criteria->cmdlist = join_args(argv + 1, argc - 1);
25
26 list_add(config->criteria, criteria);
27 wlr_log(L_DEBUG, "for_window: '%s' -> '%s' added", criteria->raw, criteria->cmdlist);
28
29 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
30} 41}
diff --git a/sway/criteria.c b/sway/criteria.c
index 294b2922..22e9a49b 100644
--- a/sway/criteria.c
+++ b/sway/criteria.c
@@ -11,501 +11,435 @@
11#include "list.h" 11#include "list.h"
12#include "log.h" 12#include "log.h"
13 13
14bool criteria_is_empty(struct criteria *criteria) { 14enum criteria_type { // *must* keep in sync with criteria_strings[]
15 return !criteria->title 15 CRIT_APP_ID,
16 && !criteria->app_id 16 CRIT_CLASS,
17 && !criteria->class 17 CRIT_CON_ID,
18 && !criteria->instance 18 CRIT_CON_MARK,
19 && !criteria->con_mark 19 CRIT_FLOATING,
20 && !criteria->con_id 20 CRIT_ID,
21 && !criteria->id 21 CRIT_INSTANCE,
22 && !criteria->window_role 22 CRIT_TILING,
23 && !criteria->window_type 23 CRIT_TITLE,
24 && !criteria->floating 24 CRIT_URGENT,
25 && !criteria->tiling 25 CRIT_WINDOW_ROLE,
26 && !criteria->urgent 26 CRIT_WINDOW_TYPE,
27 && !criteria->workspace; 27 CRIT_WORKSPACE,
28} 28 CRIT_LAST
29 29};
30void criteria_destroy(struct criteria *criteria) {
31 pcre_free(criteria->title);
32 pcre_free(criteria->app_id);
33 pcre_free(criteria->class);
34 pcre_free(criteria->instance);
35 pcre_free(criteria->con_mark);
36 pcre_free(criteria->window_role);
37 free(criteria->workspace);
38
39 free(criteria->raw);
40 free(criteria);
41}
42
43static int regex_cmp(const char *item, const pcre *regex) {
44 return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
45}
46 30
47static bool criteria_matches_view(struct criteria *criteria, 31static const char * const criteria_strings[CRIT_LAST] = {
48 struct sway_view *view) { 32 [CRIT_APP_ID] = "app_id",
49 if (criteria->title) { 33 [CRIT_CLASS] = "class",
50 const char *title = view_get_title(view); 34 [CRIT_CON_ID] = "con_id",
51 if (!title || regex_cmp(title, criteria->title) != 0) { 35 [CRIT_CON_MARK] = "con_mark",
52 return false; 36 [CRIT_FLOATING] = "floating",
53 } 37 [CRIT_ID] = "id",
54 } 38 [CRIT_INSTANCE] = "instance",
39 [CRIT_TILING] = "tiling",
40 [CRIT_TITLE] = "title",
41 [CRIT_URGENT] = "urgent", // either "latest" or "oldest" ...
42 [CRIT_WINDOW_ROLE] = "window_role",
43 [CRIT_WINDOW_TYPE] = "window_type",
44 [CRIT_WORKSPACE] = "workspace"
45};
55 46
56 if (criteria->app_id) { 47/**
57 const char *app_id = view_get_app_id(view); 48 * A single criteria token (ie. value/regex pair),
58 if (!app_id || regex_cmp(app_id, criteria->app_id) != 0) { 49 * e.g. 'class="some class regex"'.
59 return false; 50 */
60 } 51struct crit_token {
61 } 52 enum criteria_type type;
53 pcre *regex;
54 char *raw;
55};
62 56
63 if (criteria->class) { 57static void free_crit_token(struct crit_token *crit) {
64 const char *class = view_get_class(view); 58 pcre_free(crit->regex);
65 if (!class || regex_cmp(class, criteria->class) != 0) { 59 free(crit->raw);
66 return false; 60 free(crit);
67 } 61}
68 }
69 62
70 if (criteria->instance) { 63static void free_crit_tokens(list_t *crit_tokens) {
71 const char *instance = view_get_instance(view); 64 for (int i = 0; i < crit_tokens->length; i++) {
72 if (!instance || regex_cmp(instance, criteria->instance) != 0) { 65 free_crit_token(crit_tokens->items[i]);
73 return false;
74 }
75 } 66 }
67 list_free(crit_tokens);
68}
76 69
77 if (criteria->con_mark) { 70// Extracts criteria string from its brackets. Returns new (duplicate)
78 // TODO 71// substring.
79 return false; 72static char *criteria_from(const char *arg) {
73 char *criteria = NULL;
74 if (*arg == '[') {
75 criteria = strdup(arg + 1);
76 } else {
77 criteria = strdup(arg);
80 } 78 }
81 79
82 if (criteria->con_id) { // Internal ID 80 int last = strlen(criteria) - 1;
83 if (!view->swayc || view->swayc->id != criteria->con_id) { 81 if (criteria[last] == ']') {
84 return false; 82 criteria[last] = '\0';
85 }
86 } 83 }
84 return criteria;
85}
87 86
88 if (criteria->id) { // X11 window ID 87// Return instances of c found in str.
89 uint32_t x11_window_id = view_get_x11_window_id(view); 88static int countchr(char *str, char c) {
90 if (!x11_window_id || x11_window_id != criteria->id) { 89 int found = 0;
91 return false; 90 for (int i = 0; str[i]; i++) {
91 if (str[i] == c) {
92 ++found;
92 } 93 }
93 } 94 }
95 return found;
96}
94 97
95 if (criteria->window_role) { 98// criteria_str is e.g. '[class="some class regex" instance="instance name"]'.
96 // TODO 99//
97 } 100// Will create array of pointers in buf, where first is duplicate of given
98 101// string (must be freed) and the rest are pointers to names and values in the
99 if (criteria->window_type) { 102// base string (every other, naturally). argc will be populated with the length
100 uint32_t type = view_get_window_type(view); 103// of buf.
101 if (!type || type != criteria->window_type) { 104//
102 return false; 105// Returns error string or NULL if successful.
106static char *crit_tokens(int *argc, char ***buf,
107 const char * const criteria_str) {
108 wlr_log(L_DEBUG, "Parsing criteria: '%s'", criteria_str);
109 char *base = criteria_from(criteria_str);
110 char *head = base;
111 char *namep = head; // start of criteria name
112 char *valp = NULL; // start of value
113
114 // We're going to place EOS markers where we need to and fill up an array
115 // of pointers to the start of each token (either name or value).
116 int pairs = countchr(base, '=');
117 int max_tokens = pairs * 2 + 1; // this gives us at least enough slots
118
119 char **argv = *buf = calloc(max_tokens, sizeof(char*));
120 argv[0] = base; // this needs to be freed by caller
121 bool quoted = true;
122
123 *argc = 1; // uneven = name, even = value
124 while (*head && *argc < max_tokens) {
125 if (namep != head && *(head - 1) == '\\') {
126 // escaped character: don't try to parse this
127 } else if (*head == '=' && namep != head) {
128 if (*argc % 2 != 1) {
129 // we're not expecting a name
130 return strdup("Unable to parse criteria: "
131 "Found out of place equal sign");
132 } else {
133 // name ends here
134 char *end = head; // don't want to rewind the head
135 while (*(end - 1) == ' ') {
136 --end;
137 }
138 *end = '\0';
139 if (*(namep) == ' ') {
140 namep = strrchr(namep, ' ') + 1;
141 }
142 argv[*argc] = namep;
143 *argc += 1;
144 }
145 } else if (*head == '"') {
146 if (*argc % 2 != 0) {
147 // we're not expecting a value
148 return strdup("Unable to parse criteria: "
149 "Found quoted value where it was not expected");
150 } else if (!valp) { // value starts here
151 valp = head + 1;
152 quoted = true;
153 } else {
154 // value ends here
155 argv[*argc] = valp;
156 *argc += 1;
157 *head = '\0';
158 valp = NULL;
159 namep = head + 1;
160 }
161 } else if (*argc % 2 == 0 && *head != ' ') {
162 // parse unquoted values
163 if (!valp) {
164 quoted = false;
165 valp = head; // value starts here
166 }
167 } else if (valp && !quoted && *head == ' ') {
168 // value ends here
169 argv[*argc] = valp;
170 *argc += 1;
171 *head = '\0';
172 valp = NULL;
173 namep = head + 1;
103 } 174 }
175 head++;
104 } 176 }
105 177
106 if (criteria->floating) { 178 // catch last unquoted value if needed
107 // TODO 179 if (valp && !quoted && !*head) {
108 return false; 180 argv[*argc] = valp;
181 *argc += 1;
109 } 182 }
110 183
111 if (criteria->tiling) { 184 return NULL;
112 // TODO
113 }
114
115 if (criteria->urgent) {
116 // TODO
117 return false;
118 }
119
120 if (criteria->workspace) {
121 if (!view->swayc) {
122 return false;
123 }
124 struct sway_container *ws = container_parent(view->swayc, C_WORKSPACE);
125 if (!ws || strcmp(ws->name, criteria->workspace) != 0) {
126 return false;
127 }
128 }
129
130 return true;
131} 185}
132 186
133list_t *criteria_for_view(struct sway_view *view, enum criteria_type types) { 187// Returns error string on failure or NULL otherwise.
134 list_t *criterias = config->criteria; 188static char *parse_criteria_name(enum criteria_type *type, char *name) {
135 list_t *matches = create_list(); 189 *type = CRIT_LAST;
136 for (int i = 0; i < criterias->length; ++i) { 190 for (int i = 0; i < CRIT_LAST; i++) {
137 struct criteria *criteria = criterias->items[i]; 191 if (strcmp(criteria_strings[i], name) == 0) {
138 if ((criteria->type & types) && criteria_matches_view(criteria, view)) { 192 *type = (enum criteria_type) i;
139 list_add(matches, criteria); 193 break;
140 } 194 }
141 } 195 }
142 return matches; 196 if (*type == CRIT_LAST) {
143} 197 const char *fmt = "Criteria type '%s' is invalid or unsupported.";
144 198 int len = strlen(name) + strlen(fmt) - 1;
145struct match_data { 199 char *error = malloc(len);
146 struct criteria *criteria; 200 snprintf(error, len, fmt, name);
147 list_t *matches; 201 return error;
148}; 202 } else if (*type == CRIT_URGENT || *type == CRIT_WINDOW_ROLE ||
149 203 *type == CRIT_WINDOW_TYPE) {
150static void criteria_get_views_iterator(struct sway_container *container, 204 // (we're just being helpful here)
151 void *data) { 205 const char *fmt = "\"%s\" criteria currently unsupported, "
152 struct match_data *match_data = data; 206 "no window will match this";
153 if (container->type == C_VIEW) { 207 int len = strlen(fmt) + strlen(name) - 1;
154 if (criteria_matches_view(match_data->criteria, container->sway_view)) { 208 char *error = malloc(len);
155 list_add(match_data->matches, container->sway_view); 209 snprintf(error, len, fmt, name);
156 } 210 return error;
157 } 211 }
212 return NULL;
158} 213}
159 214
160list_t *criteria_get_views(struct criteria *criteria) {
161 list_t *matches = create_list();
162 struct match_data data = {
163 .criteria = criteria,
164 .matches = matches,
165 };
166 container_for_each_descendant_dfs(&root_container,
167 criteria_get_views_iterator, &data);
168 return matches;
169}
170
171// The error pointer is used for parsing functions, and saves having to pass it
172// as an argument in several places.
173char *error = NULL;
174
175// Returns error string on failure or NULL otherwise. 215// Returns error string on failure or NULL otherwise.
176static bool generate_regex(pcre **regex, char *value) { 216static char *generate_regex(pcre **regex, char *value) {
177 const char *reg_err; 217 const char *reg_err;
178 int offset; 218 int offset;
179 219
180 *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, &reg_err, &offset, NULL); 220 *regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, &reg_err, &offset, NULL);
181 221
182 if (!*regex) { 222 if (!*regex) {
183 const char *fmt = "Regex compilation for '%s' failed: %s"; 223 const char *fmt = "Regex compilation (for '%s') failed: %s";
184 int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; 224 int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3;
185 error = malloc(len); 225 char *error = malloc(len);
186 snprintf(error, len, fmt, value, reg_err); 226 snprintf(error, len, fmt, value, reg_err);
187 return false; 227 return error;
188 } 228 }
189 229 return NULL;
190 return true;
191} 230}
192 231
193enum criteria_token { 232// Test whether the criterion corresponds to the currently focused window
194 T_APP_ID, 233static bool crit_is_focused(const char *value) {
195 T_CLASS, 234 return !strcmp(value, "focused") || !strcmp(value, "__focused__");
196 T_CON_ID,
197 T_CON_MARK,
198 T_FLOATING,
199 T_ID,
200 T_INSTANCE,
201 T_TILING,
202 T_TITLE,
203 T_URGENT,
204 T_WINDOW_ROLE,
205 T_WINDOW_TYPE,
206 T_WORKSPACE,
207
208 T_INVALID,
209};
210
211static enum criteria_token token_from_name(char *name) {
212 if (strcmp(name, "app_id") == 0) {
213 return T_APP_ID;
214 } else if (strcmp(name, "class") == 0) {
215 return T_CLASS;
216 } else if (strcmp(name, "con_id") == 0) {
217 return T_CON_ID;
218 } else if (strcmp(name, "con_mark") == 0) {
219 return T_CON_MARK;
220 } else if (strcmp(name, "id") == 0) {
221 return T_ID;
222 } else if (strcmp(name, "instance") == 0) {
223 return T_INSTANCE;
224 } else if (strcmp(name, "title") == 0) {
225 return T_TITLE;
226 } else if (strcmp(name, "urgent") == 0) {
227 return T_URGENT;
228 } else if (strcmp(name, "window_role") == 0) {
229 return T_WINDOW_ROLE;
230 } else if (strcmp(name, "window_type") == 0) {
231 return T_WINDOW_TYPE;
232 } else if (strcmp(name, "workspace") == 0) {
233 return T_WORKSPACE;
234 }
235 return T_INVALID;
236} 235}
237 236
238/** 237// Populate list with crit_tokens extracted from criteria string, returns error
239 * Get a property of the focused view. 238// string or NULL if successful.
240 * 239char *extract_crit_tokens(list_t *tokens, const char * const criteria) {
241 * Note that we are taking the focused view at the time of criteria parsing, not 240 int argc;
242 * at the time of execution. This is because __focused__ only makes sense when 241 char **argv = NULL, *error = NULL;
243 * using criteria via IPC. Using __focused__ in config is not useful because 242 if ((error = crit_tokens(&argc, &argv, criteria))) {
244 * criteria is only executed once per view. 243 goto ect_cleanup;
245 */ 244 }
246static char *get_focused_prop(enum criteria_token token) { 245 for (int i = 1; i + 1 < argc; i += 2) {
247 struct sway_seat *seat = input_manager_current_seat(input_manager); 246 char* name = argv[i], *value = argv[i + 1];
248 struct sway_container *focus = seat_get_focus(seat); 247 struct crit_token *token = calloc(1, sizeof(struct crit_token));
249 248 token->raw = strdup(value);
250 if (!focus || focus->type != C_VIEW) { 249
251 return NULL; 250 if ((error = parse_criteria_name(&token->type, name))) {
252 } 251 free_crit_token(token);
253 struct sway_view *view = focus->sway_view; 252 goto ect_cleanup;
254 const char *value = NULL; 253 } else if (token->type == CRIT_URGENT || crit_is_focused(value)) {
255 254 wlr_log(L_DEBUG, "%s -> \"%s\"", name, value);
256 switch (token) { 255 list_add(tokens, token);
257 case T_APP_ID: 256 } else if((error = generate_regex(&token->regex, value))) {
258 value = view_get_app_id(view); 257 free_crit_token(token);
259 break; 258 goto ect_cleanup;
260 case T_CLASS: 259 } else {
261 value = view_get_class(view); 260 wlr_log(L_DEBUG, "%s -> /%s/", name, value);
262 break; 261 list_add(tokens, token);
263 case T_INSTANCE:
264 value = view_get_instance(view);
265 break;
266 case T_TITLE:
267 value = view_get_class(view);
268 break;
269 case T_WINDOW_ROLE:
270 value = view_get_class(view);
271 break;
272 case T_WORKSPACE:
273 {
274 struct sway_container *ws = container_parent(focus, C_WORKSPACE);
275 if (ws) {
276 value = ws->name;
277 }
278 } 262 }
279 break;
280 case T_CON_ID: // These do not support __focused__
281 case T_CON_MARK:
282 case T_FLOATING:
283 case T_ID:
284 case T_TILING:
285 case T_URGENT:
286 case T_WINDOW_TYPE:
287 case T_INVALID:
288 break;
289 } 263 }
290 if (value) { 264ect_cleanup:
291 return strdup(value); 265 free(argv[0]); // base string
292 } 266 free(argv);
293 return NULL; 267 return error;
294} 268}
295 269
296static bool parse_token(struct criteria *criteria, char *name, char *value) { 270static int regex_cmp(const char *item, const pcre *regex) {
297 enum criteria_token token = token_from_name(name); 271 return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
298 if (token == T_INVALID) { 272}
299 const char *fmt = "Token '%s' is not recognized"; 273
300 int len = strlen(fmt) + strlen(name) - 1; 274// test a single view if it matches list of criteria tokens (all of them).
301 error = malloc(len); 275static bool criteria_test(struct sway_container *cont, list_t *tokens) {
302 snprintf(error, len, fmt, name); 276 if (cont->type != C_CONTAINER && cont->type != C_VIEW) {
303 return false; 277 return false;
304 } 278 }
279 int matches = 0;
280 for (int i = 0; i < tokens->length; i++) {
281 struct crit_token *crit = tokens->items[i];
282 switch (crit->type) {
283 case CRIT_CLASS:
284 {
285 const char *class = view_get_class(cont->sway_view);
286 if (!class) {
287 break;
288 }
289 if (crit->regex && regex_cmp(class, crit->regex) == 0) {
290 matches++;
291 }
292 break;
293 }
294 case CRIT_CON_ID:
295 {
296 char *endptr;
297 size_t crit_id = strtoul(crit->raw, &endptr, 10);
305 298
306 char *effective_value = NULL; 299 if (*endptr == 0 && cont->id == crit_id) {
307 if (value && strcmp(value, "__focused__") == 0) { 300 ++matches;
308 effective_value = get_focused_prop(token); 301 }
309 } else if (value) { 302 break;
310 effective_value = strdup(value); 303 }
311 } 304 case CRIT_CON_MARK:
305 // TODO
306 break;
307 case CRIT_FLOATING:
308 // TODO
309 break;
310 case CRIT_ID:
311 // TODO
312 break;
313 case CRIT_APP_ID:
314 {
315 const char *app_id = view_get_app_id(cont->sway_view);
316 if (!app_id) {
317 break;
318 }
312 319
313 // Require value, unless token is floating or tiled 320 if (crit->regex && regex_cmp(app_id, crit->regex) == 0) {
314 if (!effective_value && token != T_FLOATING && token != T_TILING) { 321 matches++;
315 const char *fmt = "Token '%s' requires a value"; 322 }
316 int len = strlen(fmt) + strlen(name) - 1; 323 break;
317 error = malloc(len); 324 }
318 snprintf(error, len, fmt, name); 325 case CRIT_INSTANCE:
319 return false; 326 {
320 } 327 const char *instance = view_get_instance(cont->sway_view);
328 if (!instance) {
329 break;
330 }
321 331
322 char *endptr = NULL; 332 if (crit->regex && regex_cmp(instance, crit->regex) == 0) {
323 switch (token) { 333 matches++;
324 case T_TITLE: 334 }
325 generate_regex(&criteria->title, effective_value); 335 break;
326 break; 336 }
327 case T_APP_ID: 337 case CRIT_TILING:
328 generate_regex(&criteria->app_id, effective_value); 338 // TODO
329 break; 339 break;
330 case T_CLASS: 340 case CRIT_TITLE:
331 generate_regex(&criteria->class, effective_value); 341 {
332 break; 342 const char *title = view_get_title(cont->sway_view);
333 case T_INSTANCE: 343 if (!title) {
334 generate_regex(&criteria->instance, effective_value); 344 break;
335 break; 345 }
336 case T_CON_ID: 346
337 criteria->con_id = strtoul(effective_value, &endptr, 10); 347 if (crit->regex && regex_cmp(title, crit->regex) == 0) {
338 if (*endptr != 0) { 348 matches++;
339 error = strdup("The value for 'con_id' should be numeric"); 349 }
340 } 350 break;
341 break; 351 }
342 case T_CON_MARK: 352 case CRIT_URGENT:
343 generate_regex(&criteria->con_mark, effective_value); 353 // TODO "latest" or "oldest"
344 break; 354 break;
345 case T_WINDOW_ROLE: 355 case CRIT_WINDOW_ROLE:
346 generate_regex(&criteria->window_role, effective_value); 356 // TODO
347 break; 357 break;
348 case T_WINDOW_TYPE: 358 case CRIT_WINDOW_TYPE:
349 // TODO: This is a string but will be stored as an enum or integer 359 // TODO
350 break; 360 break;
351 case T_ID: 361 case CRIT_WORKSPACE:
352 criteria->id = strtoul(effective_value, &endptr, 10); 362 // TODO
353 if (*endptr != 0) { 363 break;
354 error = strdup("The value for 'id' should be numeric"); 364 default:
355 } 365 sway_abort("Invalid criteria type (%i)", crit->type);
356 break; 366 break;
357 case T_FLOATING:
358 criteria->floating = true;
359 break;
360 case T_TILING:
361 criteria->tiling = true;
362 break;
363 case T_URGENT:
364 if (strcmp(effective_value, "latest") == 0) {
365 criteria->urgent = 'l';
366 } else if (strcmp(effective_value, "oldest") == 0) {
367 criteria->urgent = 'o';
368 } else {
369 error =
370 strdup("The value for 'urgent' must be 'latest' or 'oldest'");
371 } 367 }
372 break;
373 case T_WORKSPACE:
374 criteria->workspace = strdup(effective_value);
375 break;
376 case T_INVALID:
377 break;
378 } 368 }
379 free(effective_value); 369 return matches == tokens->length;
370}
380 371
381 if (error) { 372int criteria_cmp(const void *a, const void *b) {
382 return false; 373 if (a == b) {
374 return 0;
375 } else if (!a) {
376 return -1;
377 } else if (!b) {
378 return 1;
383 } 379 }
384 380 const struct criteria *crit_a = a, *crit_b = b;
385 return true; 381 int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist);
382 if (cmp != 0) {
383 return cmp;
384 }
385 return lenient_strcmp(crit_a->crit_raw, crit_b->crit_raw);
386} 386}
387 387
388static void skip_spaces(char **head) { 388void free_criteria(struct criteria *crit) {
389 while (**head == ' ') { 389 if (crit->tokens) {
390 ++*head; 390 free_crit_tokens(crit->tokens);
391 }
392 if (crit->cmdlist) {
393 free(crit->cmdlist);
391 } 394 }
395 if (crit->crit_raw) {
396 free(crit->crit_raw);
397 }
398 free(crit);
392} 399}
393 400
394// Remove escaping slashes from value 401bool criteria_any(struct sway_container *cont, list_t *criteria) {
395static void unescape(char *value) { 402 for (int i = 0; i < criteria->length; i++) {
396 if (!strchr(value, '\\')) { 403 struct criteria *bc = criteria->items[i];
397 return; 404 if (criteria_test(cont, bc->tokens)) {
398 } 405 return true;
399 char *copy = calloc(strlen(value) + 1, 1);
400 char *readhead = value;
401 char *writehead = copy;
402 while (*readhead) {
403 if (*readhead == '\\' && *(readhead + 1) == '"') {
404 // skip the slash
405 ++readhead;
406 } 406 }
407 *writehead = *readhead;
408 ++writehead;
409 ++readhead;
410 } 407 }
411 strcpy(value, copy); 408 return false;
412 free(copy);
413} 409}
414 410
415/** 411list_t *criteria_for(struct sway_container *cont) {
416 * Parse a raw criteria string such as [class="foo" instance="bar"] into a 412 list_t *criteria = config->criteria, *matches = create_list();
417 * criteria struct. 413 for (int i = 0; i < criteria->length; i++) {
418 * 414 struct criteria *bc = criteria->items[i];
419 * If errors are found, NULL will be returned and the error argument will be 415 if (criteria_test(cont, bc->tokens)) {
420 * populated with an error string. 416 list_add(matches, bc);
421 */
422struct criteria *criteria_parse(char *raw, char **error_arg) {
423 free(error);
424 error = NULL;
425
426 char *head = raw;
427 skip_spaces(&head);
428 if (*head != '[') {
429 *error_arg = strdup("No criteria");
430 return NULL;
431 }
432 ++head;
433
434 struct criteria *criteria = calloc(sizeof(struct criteria), 1);
435 char *name = NULL, *value = NULL;
436 bool in_quotes = false;
437
438 while (*head && *head != ']') {
439 skip_spaces(&head);
440 // Parse token name
441 char *namestart = head;
442 while ((*head >= 'a' && *head <= 'z') || *head == '_') {
443 ++head;
444 } 417 }
445 name = calloc(head - namestart + 1, 1);
446 strncpy(name, namestart, head - namestart);
447 // Parse token value
448 skip_spaces(&head);
449 value = NULL;
450 if (*head == '=') {
451 ++head;
452 skip_spaces(&head);
453 if (*head == '"') {
454 in_quotes = true;
455 ++head;
456 }
457 char *valuestart = head;
458 if (in_quotes) {
459 while (*head && (*head != '"' || *(head - 1) == '\\')) {
460 ++head;
461 }
462 if (!*head) {
463 *error_arg = strdup("Quote mismatch in criteria");
464 goto cleanup;
465 }
466 } else {
467 while (*head && *head != ' ' && *head != ']') {
468 ++head;
469 }
470 }
471 value = calloc(head - valuestart + 1, 1);
472 strncpy(value, valuestart, head - valuestart);
473 if (in_quotes) {
474 ++head;
475 in_quotes = false;
476 }
477 unescape(value);
478 }
479 wlr_log(L_DEBUG, "Found pair: %s=%s", name, value);
480 if (!parse_token(criteria, name, value)) {
481 *error_arg = error;
482 goto cleanup;
483 }
484 skip_spaces(&head);
485 free(name);
486 free(value);
487 name = NULL;
488 value = NULL;
489 }
490 if (*head != ']') {
491 *error_arg = strdup("No closing brace found in criteria");
492 goto cleanup;
493 } 418 }
419 return matches;
420}
421
422struct list_tokens {
423 list_t *list;
424 list_t *tokens;
425};
494 426
495 if (criteria_is_empty(criteria)) { 427static void container_match_add(struct sway_container *container,
496 *error_arg = strdup("Criteria is empty"); 428 struct list_tokens *list_tokens) {
497 goto cleanup; 429 if (criteria_test(container, list_tokens->tokens)) {
430 list_add(list_tokens->list, container);
498 } 431 }
432}
499 433
500 ++head; 434list_t *container_for_crit_tokens(list_t *tokens) {
501 int len = head - raw; 435 struct list_tokens list_tokens =
502 criteria->raw = calloc(len + 1, 1); 436 (struct list_tokens){create_list(), tokens};
503 strncpy(criteria->raw, raw, len);
504 return criteria;
505 437
506cleanup: 438 container_for_each_descendant_dfs(&root_container,
507 free(name); 439 (void (*)(struct sway_container *, void *))container_match_add,
508 free(value); 440 &list_tokens);
509 criteria_destroy(criteria); 441
510 return NULL; 442 // TODO look in the scratchpad
443
444 return list_tokens.list;
511} 445}
diff --git a/sway/desktop/wl_shell.c b/sway/desktop/wl_shell.c
index cb3774f7..99e8947b 100644
--- a/sway/desktop/wl_shell.c
+++ b/sway/desktop/wl_shell.c
@@ -20,7 +20,7 @@ static struct sway_wl_shell_view *wl_shell_view_from_view(
20 return (struct sway_wl_shell_view *)view; 20 return (struct sway_wl_shell_view *)view;
21} 21}
22 22
23static const char *get_string_prop(struct sway_view *view, enum sway_view_prop prop) { 23static const char *get_prop(struct sway_view *view, enum sway_view_prop prop) {
24 if (wl_shell_view_from_view(view) == NULL) { 24 if (wl_shell_view_from_view(view) == NULL) {
25 return NULL; 25 return NULL;
26 } 26 }
@@ -70,7 +70,7 @@ static void set_fullscreen(struct sway_view *view, bool fullscreen) {
70} 70}
71 71
72static const struct sway_view_impl view_impl = { 72static const struct sway_view_impl view_impl = {
73 .get_string_prop = get_string_prop, 73 .get_prop = get_prop,
74 .configure = configure, 74 .configure = configure,
75 .close = _close, 75 .close = _close,
76 .destroy = destroy, 76 .destroy = destroy,
diff --git a/sway/desktop/xdg_shell_v6.c b/sway/desktop/xdg_shell_v6.c
index f685ef71..8ecb330d 100644
--- a/sway/desktop/xdg_shell_v6.c
+++ b/sway/desktop/xdg_shell_v6.c
@@ -80,7 +80,7 @@ static struct sway_xdg_shell_v6_view *xdg_shell_v6_view_from_view(
80 return (struct sway_xdg_shell_v6_view *)view; 80 return (struct sway_xdg_shell_v6_view *)view;
81} 81}
82 82
83static const char *get_string_prop(struct sway_view *view, enum sway_view_prop prop) { 83static const char *get_prop(struct sway_view *view, enum sway_view_prop prop) {
84 if (xdg_shell_v6_view_from_view(view) == NULL) { 84 if (xdg_shell_v6_view_from_view(view) == NULL) {
85 return NULL; 85 return NULL;
86 } 86 }
@@ -158,7 +158,7 @@ static void destroy(struct sway_view *view) {
158} 158}
159 159
160static const struct sway_view_impl view_impl = { 160static const struct sway_view_impl view_impl = {
161 .get_string_prop = get_string_prop, 161 .get_prop = get_prop,
162 .configure = configure, 162 .configure = configure,
163 .set_activated = set_activated, 163 .set_activated = set_activated,
164 .set_fullscreen = set_fullscreen, 164 .set_fullscreen = set_fullscreen,
diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c
index 554c070e..8f935760 100644
--- a/sway/desktop/xwayland.c
+++ b/sway/desktop/xwayland.c
@@ -126,7 +126,7 @@ static struct sway_xwayland_view *xwayland_view_from_view(
126 return (struct sway_xwayland_view *)view; 126 return (struct sway_xwayland_view *)view;
127} 127}
128 128
129static const char *get_string_prop(struct sway_view *view, enum sway_view_prop prop) { 129static const char *get_prop(struct sway_view *view, enum sway_view_prop prop) {
130 if (xwayland_view_from_view(view) == NULL) { 130 if (xwayland_view_from_view(view) == NULL) {
131 return NULL; 131 return NULL;
132 } 132 }
@@ -135,27 +135,11 @@ static const char *get_string_prop(struct sway_view *view, enum sway_view_prop p
135 return view->wlr_xwayland_surface->title; 135 return view->wlr_xwayland_surface->title;
136 case VIEW_PROP_CLASS: 136 case VIEW_PROP_CLASS:
137 return view->wlr_xwayland_surface->class; 137 return view->wlr_xwayland_surface->class;
138 case VIEW_PROP_INSTANCE:
139 return view->wlr_xwayland_surface->instance;
140 default: 138 default:
141 return NULL; 139 return NULL;
142 } 140 }
143} 141}
144 142
145static uint32_t get_int_prop(struct sway_view *view, enum sway_view_prop prop) {
146 if (xwayland_view_from_view(view) == NULL) {
147 return 0;
148 }
149 switch (prop) {
150 case VIEW_PROP_X11_WINDOW_ID:
151 return view->wlr_xwayland_surface->window_id;
152 case VIEW_PROP_WINDOW_TYPE:
153 return *view->wlr_xwayland_surface->window_type;
154 default:
155 return 0;
156 }
157}
158
159static void configure(struct sway_view *view, double ox, double oy, int width, 143static void configure(struct sway_view *view, double ox, double oy, int width,
160 int height) { 144 int height) {
161 struct sway_xwayland_view *xwayland_view = xwayland_view_from_view(view); 145 struct sway_xwayland_view *xwayland_view = xwayland_view_from_view(view);
@@ -216,17 +200,13 @@ static void destroy(struct sway_view *view) {
216 wl_list_remove(&xwayland_view->destroy.link); 200 wl_list_remove(&xwayland_view->destroy.link);
217 wl_list_remove(&xwayland_view->request_configure.link); 201 wl_list_remove(&xwayland_view->request_configure.link);
218 wl_list_remove(&xwayland_view->request_fullscreen.link); 202 wl_list_remove(&xwayland_view->request_fullscreen.link);
219 wl_list_remove(&xwayland_view->set_title.link);
220 wl_list_remove(&xwayland_view->set_class.link);
221 wl_list_remove(&xwayland_view->set_window_type.link);
222 wl_list_remove(&xwayland_view->map.link); 203 wl_list_remove(&xwayland_view->map.link);
223 wl_list_remove(&xwayland_view->unmap.link); 204 wl_list_remove(&xwayland_view->unmap.link);
224 free(xwayland_view); 205 free(xwayland_view);
225} 206}
226 207
227static const struct sway_view_impl view_impl = { 208static const struct sway_view_impl view_impl = {
228 .get_string_prop = get_string_prop, 209 .get_prop = get_prop,
229 .get_int_prop = get_int_prop,
230 .configure = configure, 210 .configure = configure,
231 .set_activated = set_activated, 211 .set_activated = set_activated,
232 .set_fullscreen = set_fullscreen, 212 .set_fullscreen = set_fullscreen,
@@ -243,6 +223,7 @@ static void handle_commit(struct wl_listener *listener, void *data) {
243 view_update_size(view, xwayland_view->pending_width, 223 view_update_size(view, xwayland_view->pending_width,
244 xwayland_view->pending_height); 224 xwayland_view->pending_height);
245 view_damage_from(view); 225 view_damage_from(view);
226 view_update_title(view, false);
246} 227}
247 228
248static void handle_unmap(struct wl_listener *listener, void *data) { 229static void handle_unmap(struct wl_listener *listener, void *data) {
@@ -304,40 +285,6 @@ static void handle_request_fullscreen(struct wl_listener *listener, void *data)
304 view_set_fullscreen(view, xsurface->fullscreen); 285 view_set_fullscreen(view, xsurface->fullscreen);
305} 286}
306 287
307static void handle_set_title(struct wl_listener *listener, void *data) {
308 struct sway_xwayland_view *xwayland_view =
309 wl_container_of(listener, xwayland_view, set_title);
310 struct sway_view *view = &xwayland_view->view;
311 struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface;
312 if (!xsurface->mapped) {
313 return;
314 }
315 view_update_title(view, false);
316 view_execute_criteria(view);
317}
318
319static void handle_set_class(struct wl_listener *listener, void *data) {
320 struct sway_xwayland_view *xwayland_view =
321 wl_container_of(listener, xwayland_view, set_class);
322 struct sway_view *view = &xwayland_view->view;
323 struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface;
324 if (!xsurface->mapped) {
325 return;
326 }
327 view_execute_criteria(view);
328}
329
330static void handle_set_window_type(struct wl_listener *listener, void *data) {
331 struct sway_xwayland_view *xwayland_view =
332 wl_container_of(listener, xwayland_view, set_window_type);
333 struct sway_view *view = &xwayland_view->view;
334 struct wlr_xwayland_surface *xsurface = view->wlr_xwayland_surface;
335 if (!xsurface->mapped) {
336 return;
337 }
338 view_execute_criteria(view);
339}
340
341void handle_xwayland_surface(struct wl_listener *listener, void *data) { 288void handle_xwayland_surface(struct wl_listener *listener, void *data) {
342 struct sway_server *server = wl_container_of(listener, server, 289 struct sway_server *server = wl_container_of(listener, server,
343 xwayland_surface); 290 xwayland_surface);
@@ -376,16 +323,6 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) {
376 &xwayland_view->request_fullscreen); 323 &xwayland_view->request_fullscreen);
377 xwayland_view->request_fullscreen.notify = handle_request_fullscreen; 324 xwayland_view->request_fullscreen.notify = handle_request_fullscreen;
378 325
379 wl_signal_add(&xsurface->events.set_title, &xwayland_view->set_title);
380 xwayland_view->set_title.notify = handle_set_title;
381
382 wl_signal_add(&xsurface->events.set_class, &xwayland_view->set_class);
383 xwayland_view->set_class.notify = handle_set_class;
384
385 wl_signal_add(&xsurface->events.set_window_type,
386 &xwayland_view->set_window_type);
387 xwayland_view->set_window_type.notify = handle_set_window_type;
388
389 wl_signal_add(&xsurface->events.unmap, &xwayland_view->unmap); 326 wl_signal_add(&xsurface->events.unmap, &xwayland_view->unmap);
390 xwayland_view->unmap.notify = handle_unmap; 327 xwayland_view->unmap.notify = handle_unmap;
391 328
diff --git a/sway/sway.5.txt b/sway/sway.5.txt
index 704bb699..03975349 100644
--- a/sway/sway.5.txt
+++ b/sway/sway.5.txt
@@ -485,15 +485,10 @@ Mark all Firefox windows with "Browser":
485 485
486Currently supported attributes: 486Currently supported attributes:
487 487
488**app_id**::
489 Compare value against the app id. Can be a regular expression. If value is
490 __focused__, then the app id must be the same as that of the currently
491 focused window.
492
493**class**:: 488**class**::
494 Compare value against the window class. Can be a regular expression. If 489 Compare value against the window class. Can be a regular expression. If value
495 value is __focused__, then the window class must be the same as that of the 490 is _focused_, then the window class must be the same as that of the currently
496 currently focused window. 491 focused window.
497 492
498**con_id**:: 493**con_id**::
499 Compare against the internal container ID, which you can find via IPC. 494 Compare against the internal container ID, which you can find via IPC.
@@ -505,38 +500,20 @@ Currently supported attributes:
505 Matches against floating windows. 500 Matches against floating windows.
506 501
507**id**:: 502**id**::
508 Compare value against the X11 window id. Must be numeric. 503 Compare value against the app id. Can be a regular expression.
509
510**instance**::
511 Compare value against the window instance. Can be a regular expression. If
512 value is __focused__, then the window instance must be the same as that of
513 the currently focused window.
514
515**tiling**::
516 Matches against tiling windows.
517 504
518**title**:: 505**title**::
519 Compare against the window title. Can be a regular expression. If value is 506 Compare against the window title. Can be a regular expression. If value is
520 __focused__, then the window title must be the same as that of the currently 507 _focused_ then the window title must be the same as that of the currently
521 focused window. 508 focused window.
522 509
523**urgent**:: 510**tiling**::
524 Compares the urgent state of the window. Can be "latest" or "oldest". 511 Matches against tiling windows.
525
526**window_role**::
527 Compare against the window role (WM_WINDOW_ROLE). Can be a regular
528 expression. If value is __focused__, then the window role must be the same
529 as that of the currently focused window.
530
531**window_type**::
532 Compare against the window type (_NET_WM_WINDOW_TYPE). Possible values are
533 normal, dialog, utility, toolbar, splash, menu, dropdown_menu, popup_menu,
534 tooltip and notification.
535 512
536**workspace**:: 513**workspace**::
537 Compare against the workspace name for this view. Can be a regular 514 Compare against the workspace name for this view. Can be a regular expression.
538 expression. If the value is __focused__, then all the views on the currently 515 If the value is _focused_, then all the views on the currently focused workspace
539 focused workspace matches. 516 matches.
540 517
541See Also 518See Also
542-------- 519--------
diff --git a/sway/tree/view.c b/sway/tree/view.c
index 3b3b6eaf..424c1084 100644
--- a/sway/tree/view.c
+++ b/sway/tree/view.c
@@ -3,7 +3,6 @@
3#include <wayland-server.h> 3#include <wayland-server.h>
4#include <wlr/render/wlr_renderer.h> 4#include <wlr/render/wlr_renderer.h>
5#include <wlr/types/wlr_output_layout.h> 5#include <wlr/types/wlr_output_layout.h>
6#include "list.h"
7#include "log.h" 6#include "log.h"
8#include "sway/criteria.h" 7#include "sway/criteria.h"
9#include "sway/commands.h" 8#include "sway/commands.h"
@@ -20,7 +19,6 @@ void view_init(struct sway_view *view, enum sway_view_type type,
20 const struct sway_view_impl *impl) { 19 const struct sway_view_impl *impl) {
21 view->type = type; 20 view->type = type;
22 view->impl = impl; 21 view->impl = impl;
23 view->executed_criteria = create_list();
24 wl_signal_init(&view->events.unmap); 22 wl_signal_init(&view->events.unmap);
25} 23}
26 24
@@ -33,8 +31,6 @@ void view_destroy(struct sway_view *view) {
33 view_unmap(view); 31 view_unmap(view);
34 } 32 }
35 33
36 list_free(view->executed_criteria);
37
38 container_destroy(view->swayc); 34 container_destroy(view->swayc);
39 35
40 if (view->impl->destroy) { 36 if (view->impl->destroy) {
@@ -45,54 +41,33 @@ void view_destroy(struct sway_view *view) {
45} 41}
46 42
47const char *view_get_title(struct sway_view *view) { 43const char *view_get_title(struct sway_view *view) {
48 if (view->impl->get_string_prop) { 44 if (view->impl->get_prop) {
49 return view->impl->get_string_prop(view, VIEW_PROP_TITLE); 45 return view->impl->get_prop(view, VIEW_PROP_TITLE);
50 } 46 }
51 return NULL; 47 return NULL;
52} 48}
53 49
54const char *view_get_app_id(struct sway_view *view) { 50const char *view_get_app_id(struct sway_view *view) {
55 if (view->impl->get_string_prop) { 51 if (view->impl->get_prop) {
56 return view->impl->get_string_prop(view, VIEW_PROP_APP_ID); 52 return view->impl->get_prop(view, VIEW_PROP_APP_ID);
57 } 53 }
58 return NULL; 54 return NULL;
59} 55}
60 56
61const char *view_get_class(struct sway_view *view) { 57const char *view_get_class(struct sway_view *view) {
62 if (view->impl->get_string_prop) { 58 if (view->impl->get_prop) {
63 return view->impl->get_string_prop(view, VIEW_PROP_CLASS); 59 return view->impl->get_prop(view, VIEW_PROP_CLASS);
64 } 60 }
65 return NULL; 61 return NULL;
66} 62}
67 63
68const char *view_get_instance(struct sway_view *view) { 64const char *view_get_instance(struct sway_view *view) {
69 if (view->impl->get_string_prop) { 65 if (view->impl->get_prop) {
70 return view->impl->get_string_prop(view, VIEW_PROP_INSTANCE); 66 return view->impl->get_prop(view, VIEW_PROP_INSTANCE);
71 } 67 }
72 return NULL; 68 return NULL;
73} 69}
74 70
75uint32_t view_get_x11_window_id(struct sway_view *view) {
76 if (view->impl->get_int_prop) {
77 return view->impl->get_int_prop(view, VIEW_PROP_X11_WINDOW_ID);
78 }
79 return 0;
80}
81
82const char *view_get_window_role(struct sway_view *view) {
83 if (view->impl->get_string_prop) {
84 return view->impl->get_string_prop(view, VIEW_PROP_WINDOW_ROLE);
85 }
86 return NULL;
87}
88
89uint32_t view_get_window_type(struct sway_view *view) {
90 if (view->impl->get_int_prop) {
91 return view->impl->get_int_prop(view, VIEW_PROP_WINDOW_TYPE);
92 }
93 return 0;
94}
95
96const char *view_get_type(struct sway_view *view) { 71const char *view_get_type(struct sway_view *view) {
97 switch(view->type) { 72 switch(view->type) {
98 case SWAY_VIEW_WL_SHELL: 73 case SWAY_VIEW_WL_SHELL:
@@ -309,36 +284,19 @@ static void view_handle_container_reparent(struct wl_listener *listener,
309 } 284 }
310} 285}
311 286
312static bool view_has_executed_criteria(struct sway_view *view, 287static void view_execute_criteria(struct sway_view *view) {
313 struct criteria *criteria) { 288 if (!sway_assert(view->swayc, "cannot run criteria for unmapped view")) {
314 for (int i = 0; i < view->executed_criteria->length; ++i) {
315 struct criteria *item = view->executed_criteria->items[i];
316 if (item == criteria) {
317 return true;
318 }
319 }
320 return false;
321}
322
323void view_execute_criteria(struct sway_view *view) {
324 if (!view->swayc) {
325 return; 289 return;
326 } 290 }
327 struct sway_seat *seat = input_manager_current_seat(input_manager); 291 struct sway_seat *seat = input_manager_current_seat(input_manager);
328 struct sway_container *prior_workspace = 292 struct sway_container *prior_workspace =
329 container_parent(view->swayc, C_WORKSPACE); 293 container_parent(view->swayc, C_WORKSPACE);
330 list_t *criterias = criteria_for_view(view, CT_COMMAND); 294 list_t *criteria = criteria_for(view->swayc);
331 for (int i = 0; i < criterias->length; i++) { 295 for (int i = 0; i < criteria->length; i++) {
332 struct criteria *criteria = criterias->items[i]; 296 struct criteria *crit = criteria->items[i];
333 wlr_log(L_DEBUG, "Checking criteria %s", criteria->raw); 297 wlr_log(L_DEBUG, "for_window '%s' matches new view %p, cmd: '%s'",
334 if (view_has_executed_criteria(view, criteria)) { 298 crit->crit_raw, view, crit->cmdlist);
335 wlr_log(L_DEBUG, "Criteria already executed"); 299 struct cmd_results *res = execute_command(crit->cmdlist, NULL);
336 continue;
337 }
338 wlr_log(L_DEBUG, "for_window '%s' matches view %p, cmd: '%s'",
339 criteria->raw, view, criteria->cmdlist);
340 list_add(view->executed_criteria, criteria);
341 struct cmd_results *res = execute_command(criteria->cmdlist, NULL);
342 if (res->status != CMD_SUCCESS) { 300 if (res->status != CMD_SUCCESS) {
343 wlr_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); 301 wlr_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error);
344 } 302 }
@@ -347,7 +305,7 @@ void view_execute_criteria(struct sway_view *view) {
347 // so always refocus in-between command lists 305 // so always refocus in-between command lists
348 seat_set_focus(seat, view->swayc); 306 seat_set_focus(seat, view->swayc);
349 } 307 }
350 list_free(criterias); 308 list_free(criteria);
351 seat_set_focus(seat, seat_get_focus_inactive(seat, prior_workspace)); 309 seat_set_focus(seat, seat_get_focus_inactive(seat, prior_workspace));
352} 310}
353 311
@@ -357,26 +315,9 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
357 } 315 }
358 316
359 struct sway_seat *seat = input_manager_current_seat(input_manager); 317 struct sway_seat *seat = input_manager_current_seat(input_manager);
360 struct sway_container *focus = seat_get_focus(seat); 318 struct sway_container *focus = seat_get_focus_inactive(seat,
361 struct sway_container *cont = NULL; 319 &root_container);
362 320 struct sway_container *cont = container_view_create(focus, view);
363 // Check if there's any `assign` criteria for the view
364 list_t *criterias = criteria_for_view(view,
365 CT_ASSIGN_WORKSPACE | CT_ASSIGN_OUTPUT);
366 if (criterias->length) {
367 struct criteria *criteria = criterias->items[0];
368 if (criteria->type == CT_ASSIGN_WORKSPACE) {
369 struct sway_container *workspace = workspace_by_name(criteria->target);
370 if (!workspace) {
371 workspace = workspace_create(NULL, criteria->target);
372 }
373 focus = seat_get_focus_inactive(seat, workspace);
374 } else {
375 // TODO: CT_ASSIGN_OUTPUT
376 }
377 }
378 free(criterias);
379 cont = container_view_create(focus, view);
380 321
381 view->surface = wlr_surface; 322 view->surface = wlr_surface;
382 view->swayc = cont; 323 view->swayc = cont;
@@ -394,11 +335,10 @@ void view_map(struct sway_view *view, struct wlr_surface *wlr_surface) {
394 arrange_children_of(cont->parent); 335 arrange_children_of(cont->parent);
395 input_manager_set_focus(input_manager, cont); 336 input_manager_set_focus(input_manager, cont);
396 337
397 view_update_title(view, false);
398 view_execute_criteria(view);
399
400 container_damage_whole(cont); 338 container_damage_whole(cont);
401 view_handle_container_reparent(&view->container_reparent, NULL); 339 view_handle_container_reparent(&view->container_reparent, NULL);
340
341 view_execute_criteria(view);
402} 342}
403 343
404void view_unmap(struct sway_view *view) { 344void view_unmap(struct sway_view *view) {