diff options
Diffstat (limited to 'sway')
-rw-r--r-- | sway/commands.c | 34 | ||||
-rw-r--r-- | sway/config.c | 7 | ||||
-rw-r--r-- | sway/criteria.c | 349 | ||||
-rw-r--r-- | sway/handlers.c | 16 |
4 files changed, 406 insertions, 0 deletions
diff --git a/sway/commands.c b/sway/commands.c index 42105d5f..6a4af43c 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -23,6 +23,7 @@ | |||
23 | #include "sway.h" | 23 | #include "sway.h" |
24 | #include "resize.h" | 24 | #include "resize.h" |
25 | #include "input_state.h" | 25 | #include "input_state.h" |
26 | #include "criteria.h" | ||
26 | 27 | ||
27 | typedef struct cmd_results *sway_cmd(int argc, char **argv); | 28 | typedef struct cmd_results *sway_cmd(int argc, char **argv); |
28 | 29 | ||
@@ -41,6 +42,7 @@ static sway_cmd cmd_floating; | |||
41 | static sway_cmd cmd_floating_mod; | 42 | static sway_cmd cmd_floating_mod; |
42 | static sway_cmd cmd_focus; | 43 | static sway_cmd cmd_focus; |
43 | static sway_cmd cmd_focus_follows_mouse; | 44 | static sway_cmd cmd_focus_follows_mouse; |
45 | static sway_cmd cmd_for_window; | ||
44 | static sway_cmd cmd_fullscreen; | 46 | static sway_cmd cmd_fullscreen; |
45 | static sway_cmd cmd_gaps; | 47 | static sway_cmd cmd_gaps; |
46 | static sway_cmd cmd_kill; | 48 | static sway_cmd cmd_kill; |
@@ -1241,6 +1243,37 @@ static struct cmd_results *cmd_log_colors(int argc, char **argv) { | |||
1241 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | 1243 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); |
1242 | } | 1244 | } |
1243 | 1245 | ||
1246 | static struct cmd_results *cmd_for_window(int argc, char **argv) { | ||
1247 | struct cmd_results *error = NULL; | ||
1248 | if ((error = checkarg(argc, "for_window", EXPECTED_AT_LEAST, 2))) { | ||
1249 | return error; | ||
1250 | } | ||
1251 | // add command to a criteria/command pair that is run against views when they appear. | ||
1252 | char *criteria = argv[0], *cmdlist = join_args(argv + 1, argc - 1); | ||
1253 | |||
1254 | struct criteria *crit = malloc(sizeof(struct criteria)); | ||
1255 | crit->crit_raw = strdup(criteria); | ||
1256 | crit->cmdlist = cmdlist; | ||
1257 | crit->tokens = create_list(); | ||
1258 | char *err_str = extract_crit_tokens(crit->tokens, crit->crit_raw); | ||
1259 | |||
1260 | if (err_str) { | ||
1261 | error = cmd_results_new(CMD_INVALID, "for_window", err_str); | ||
1262 | free(err_str); | ||
1263 | free_criteria(crit); | ||
1264 | } else if (crit->tokens->length == 0) { | ||
1265 | error = cmd_results_new(CMD_INVALID, "for_window", "Found no name/value pairs in criteria"); | ||
1266 | free_criteria(crit); | ||
1267 | } else if (list_seq_find(config->criteria, criteria_cmp, crit) != -1) { | ||
1268 | sway_log(L_DEBUG, "for_window: Duplicate, skipping."); | ||
1269 | free_criteria(crit); | ||
1270 | } else { | ||
1271 | sway_log(L_DEBUG, "for_window: '%s' -> '%s' added", crit->crit_raw, crit->cmdlist); | ||
1272 | list_add(config->criteria, crit); | ||
1273 | } | ||
1274 | return error ? error : cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
1275 | } | ||
1276 | |||
1244 | static struct cmd_results *cmd_fullscreen(int argc, char **argv) { | 1277 | static struct cmd_results *cmd_fullscreen(int argc, char **argv) { |
1245 | struct cmd_results *error = NULL; | 1278 | struct cmd_results *error = NULL; |
1246 | if (config->reading) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can't be used in config file."); | 1279 | if (config->reading) return cmd_results_new(CMD_FAILURE, "fullscreen", "Can't be used in config file."); |
@@ -1353,6 +1386,7 @@ static struct cmd_handler handlers[] = { | |||
1353 | { "floating_modifier", cmd_floating_mod }, | 1386 | { "floating_modifier", cmd_floating_mod }, |
1354 | { "focus", cmd_focus }, | 1387 | { "focus", cmd_focus }, |
1355 | { "focus_follows_mouse", cmd_focus_follows_mouse }, | 1388 | { "focus_follows_mouse", cmd_focus_follows_mouse }, |
1389 | { "for_window", cmd_for_window }, | ||
1356 | { "fullscreen", cmd_fullscreen }, | 1390 | { "fullscreen", cmd_fullscreen }, |
1357 | { "gaps", cmd_gaps }, | 1391 | { "gaps", cmd_gaps }, |
1358 | { "kill", cmd_kill }, | 1392 | { "kill", cmd_kill }, |
diff --git a/sway/config.c b/sway/config.c index 13865058..fc21b2a9 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -10,6 +10,7 @@ | |||
10 | #include "config.h" | 10 | #include "config.h" |
11 | #include "layout.h" | 11 | #include "layout.h" |
12 | #include "input_state.h" | 12 | #include "input_state.h" |
13 | #include "criteria.h" | ||
13 | 14 | ||
14 | struct sway_config *config = NULL; | 15 | struct sway_config *config = NULL; |
15 | 16 | ||
@@ -66,6 +67,11 @@ static void free_config(struct sway_config *config) { | |||
66 | } | 67 | } |
67 | list_free(config->workspace_outputs); | 68 | list_free(config->workspace_outputs); |
68 | 69 | ||
70 | for (i = 0; i < config->criteria->length; ++i) { | ||
71 | free_criteria(config->criteria->items[i]); | ||
72 | } | ||
73 | list_free(config->criteria); | ||
74 | |||
69 | for (i = 0; i < config->output_configs->length; ++i) { | 75 | for (i = 0; i < config->output_configs->length; ++i) { |
70 | free_output_config(config->output_configs->items[i]); | 76 | free_output_config(config->output_configs->items[i]); |
71 | } | 77 | } |
@@ -82,6 +88,7 @@ static void config_defaults(struct sway_config *config) { | |||
82 | config->symbols = create_list(); | 88 | config->symbols = create_list(); |
83 | config->modes = create_list(); | 89 | config->modes = create_list(); |
84 | config->workspace_outputs = create_list(); | 90 | config->workspace_outputs = create_list(); |
91 | config->criteria = create_list(); | ||
85 | config->output_configs = create_list(); | 92 | config->output_configs = create_list(); |
86 | 93 | ||
87 | config->cmd_queue = create_list(); | 94 | config->cmd_queue = create_list(); |
diff --git a/sway/criteria.c b/sway/criteria.c new file mode 100644 index 00000000..51779590 --- /dev/null +++ b/sway/criteria.c | |||
@@ -0,0 +1,349 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <stdio.h> | ||
3 | #include <stdbool.h> | ||
4 | #include <regex.h> | ||
5 | #include "criteria.h" | ||
6 | #include "stringop.h" | ||
7 | #include "list.h" | ||
8 | #include "log.h" | ||
9 | #include "container.h" | ||
10 | #include "config.h" | ||
11 | |||
12 | enum criteria_type { // *must* keep in sync with criteria_strings[] | ||
13 | CRIT_CLASS, | ||
14 | CRIT_ID, | ||
15 | CRIT_INSTANCE, | ||
16 | CRIT_TITLE, | ||
17 | CRIT_URGENT, | ||
18 | CRIT_WINDOW_ROLE, | ||
19 | CRIT_WINDOW_TYPE, | ||
20 | CRIT_WORKSPACE, | ||
21 | CRIT_LAST | ||
22 | }; | ||
23 | |||
24 | // this *must* match the ordering in criteria_type enum | ||
25 | static const char * const criteria_strings[] = { | ||
26 | "class", | ||
27 | "id", | ||
28 | "instance", | ||
29 | "title", | ||
30 | "urgent", // either "latest" or "oldest" ... | ||
31 | "window_role", | ||
32 | "window_type", | ||
33 | "workspace" | ||
34 | }; | ||
35 | |||
36 | /** | ||
37 | * A single criteria token (ie. value/regex pair), | ||
38 | * e.g. 'class="some class regex"'. | ||
39 | */ | ||
40 | struct crit_token { | ||
41 | enum criteria_type type; | ||
42 | regex_t *regex; | ||
43 | char *raw; | ||
44 | }; | ||
45 | |||
46 | static void free_crit_token(struct crit_token *crit) { | ||
47 | if (crit->regex) { | ||
48 | regfree(crit->regex); | ||
49 | free(crit->regex); | ||
50 | } | ||
51 | if (crit->raw) { | ||
52 | free(crit->raw); | ||
53 | } | ||
54 | free(crit); | ||
55 | } | ||
56 | |||
57 | static void free_crit_tokens(list_t *crit_tokens) { | ||
58 | for (int i = 0; i < crit_tokens->length; i++) { | ||
59 | free_crit_token(crit_tokens->items[i]); | ||
60 | } | ||
61 | list_free(crit_tokens); | ||
62 | } | ||
63 | |||
64 | // Extracts criteria string from its brackets. Returns new (duplicate) | ||
65 | // substring. | ||
66 | static char *criteria_from(const char *arg) { | ||
67 | char *criteria = NULL; | ||
68 | if (*arg == '[') { | ||
69 | criteria = strdup(arg + 1); | ||
70 | } else { | ||
71 | criteria = strdup(arg); | ||
72 | } | ||
73 | |||
74 | int last = strlen(criteria) - 1; | ||
75 | if (criteria[last] == ']') { | ||
76 | criteria[last] = '\0'; | ||
77 | } | ||
78 | return criteria; | ||
79 | } | ||
80 | |||
81 | // Return instances of c found in str. | ||
82 | static int countchr(char *str, char c) { | ||
83 | int found = 0; | ||
84 | for (int i = 0; str[i]; i++) { | ||
85 | if (str[i] == c) { | ||
86 | ++found; | ||
87 | } | ||
88 | } | ||
89 | return found; | ||
90 | } | ||
91 | |||
92 | // criteria_str is e.g. '[class="some class regex" instance="instance name"]'. | ||
93 | // | ||
94 | // Will create array of pointers in buf, where first is duplicate of given | ||
95 | // string (must be freed) and the rest are pointers to names and values in the | ||
96 | // base string (every other, naturally). argc will be populated with the length | ||
97 | // of buf. | ||
98 | // | ||
99 | // Returns error string or NULL if successful. | ||
100 | static char *crit_tokens(int *argc, char ***buf, const char * const criteria_str) { | ||
101 | sway_log(L_DEBUG, "Parsing criteria: '%s'", criteria_str); | ||
102 | char *base = criteria_from(criteria_str); | ||
103 | char *head = base; | ||
104 | char *namep = head; // start of criteria name | ||
105 | char *valp = NULL; // start of value | ||
106 | |||
107 | // We're going to place EOS markers where we need to and fill up an array | ||
108 | // of pointers to the start of each token (either name or value). | ||
109 | int pairs = countchr(base, '='); | ||
110 | int max_tokens = pairs * 2 + 1; // this gives us at least enough slots | ||
111 | |||
112 | char **argv = *buf = calloc(max_tokens, sizeof(char*)); | ||
113 | argv[0] = base; // this needs to be freed by caller | ||
114 | |||
115 | *argc = 1; // uneven = name, even = value | ||
116 | while (*head && *argc < max_tokens) { | ||
117 | if (namep != head && *(head - 1) == '\\') { | ||
118 | // escaped character: don't try to parse this | ||
119 | } else if (*head == '=' && namep != head) { | ||
120 | if (*argc % 2 != 1) { | ||
121 | // we're not expecting a name | ||
122 | return strdup("Unable to parse criteria: " | ||
123 | "Found out of place equal sign"); | ||
124 | } else { | ||
125 | // name ends here | ||
126 | char *end = head; // don't want to rewind the head | ||
127 | while (*(end - 1) == ' ') { | ||
128 | --end; | ||
129 | } | ||
130 | *end = '\0'; | ||
131 | if (*(namep) == ' ') { | ||
132 | namep = strrchr(namep, ' ') + 1; | ||
133 | } | ||
134 | argv[(*argc)++] = namep; | ||
135 | } | ||
136 | } else if (*head == '"') { | ||
137 | if (*argc % 2 != 0) { | ||
138 | // we're not expecting a value | ||
139 | return strdup("Unable to parse criteria: " | ||
140 | "Found quoted value where it was not expected"); | ||
141 | } else if (!valp) { // value starts here | ||
142 | valp = head + 1; | ||
143 | } else { | ||
144 | // value ends here | ||
145 | argv[(*argc)++] = valp; | ||
146 | *head = '\0'; | ||
147 | valp = NULL; | ||
148 | namep = head + 1; | ||
149 | } | ||
150 | } else if (*argc % 2 == 0 && !valp && *head != ' ') { | ||
151 | // We're expecting a quoted value, haven't found one yet, and this | ||
152 | // is not an empty space. | ||
153 | return strdup("Unable to parse criteria: " | ||
154 | "Names must be unquoted, values must be quoted"); | ||
155 | } | ||
156 | head++; | ||
157 | } | ||
158 | return NULL; | ||
159 | } | ||
160 | |||
161 | // Returns error string on failure or NULL otherwise. | ||
162 | static char *parse_criteria_name(enum criteria_type *type, char *name) { | ||
163 | *type = CRIT_LAST; | ||
164 | for (int i = 0; i < CRIT_LAST; i++) { | ||
165 | if (strcmp(criteria_strings[i], name) == 0) { | ||
166 | *type = (enum criteria_type) i; | ||
167 | break; | ||
168 | } | ||
169 | } | ||
170 | if (*type == CRIT_LAST) { | ||
171 | const char *fmt = "Criteria type '%s' is invalid or unsupported."; | ||
172 | int len = strlen(name) + strlen(fmt) - 1; | ||
173 | char *error = malloc(len); | ||
174 | snprintf(error, len, fmt, name); | ||
175 | return error; | ||
176 | } else if (*type == CRIT_INSTANCE || *type == CRIT_URGENT || | ||
177 | *type == CRIT_WINDOW_ROLE || *type == CRIT_WINDOW_TYPE) { | ||
178 | |||
179 | // (we're just being helpful here) | ||
180 | const char *fmt = "\"%s\" criteria currently unsupported, " | ||
181 | "no window will match this"; | ||
182 | int len = strlen(fmt) + strlen(name) - 1; | ||
183 | char *error = malloc(len); | ||
184 | snprintf(error, len, fmt, name); | ||
185 | return error; | ||
186 | } | ||
187 | return NULL; | ||
188 | } | ||
189 | |||
190 | // Returns error string on failure or NULL otherwise. | ||
191 | static char *generate_regex(regex_t **regex, char *value) { | ||
192 | *regex = calloc(1, sizeof(regex_t)); | ||
193 | int err = regcomp(*regex, value, REG_NOSUB); | ||
194 | if (err != 0) { | ||
195 | char *reg_err = malloc(64); | ||
196 | regerror(err, *regex, reg_err, 64); | ||
197 | |||
198 | const char *fmt = "Regex compilation (for '%s') failed: %s"; | ||
199 | int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3; | ||
200 | char *error = malloc(len); | ||
201 | snprintf(error, len, fmt, value, reg_err); | ||
202 | free(reg_err); | ||
203 | return error; | ||
204 | } | ||
205 | return NULL; | ||
206 | } | ||
207 | |||
208 | // Pouplate list with crit_tokens extracted from criteria string, returns error | ||
209 | // string or NULL if successful. | ||
210 | char *extract_crit_tokens(list_t *tokens, const char * const criteria) { | ||
211 | int argc; | ||
212 | char **argv = NULL, *error = NULL; | ||
213 | if ((error = crit_tokens(&argc, &argv, criteria))) { | ||
214 | goto ect_cleanup; | ||
215 | } | ||
216 | for (int i = 1; i + 1 < argc; i += 2) { | ||
217 | char* name = argv[i], *value = argv[i + 1]; | ||
218 | struct crit_token *token = calloc(1, sizeof(struct crit_token)); | ||
219 | token->raw = strdup(value); | ||
220 | |||
221 | if ((error = parse_criteria_name(&token->type, name))) { | ||
222 | free_crit_token(token); | ||
223 | goto ect_cleanup; | ||
224 | } else if (token->type == CRIT_URGENT || strcmp(value, "focused") == 0) { | ||
225 | sway_log(L_DEBUG, "%s -> \"%s\"", name, value); | ||
226 | list_add(tokens, token); | ||
227 | } else if((error = generate_regex(&token->regex, value))) { | ||
228 | free_crit_token(token); | ||
229 | goto ect_cleanup; | ||
230 | } else { | ||
231 | sway_log(L_DEBUG, "%s -> /%s/", name, value); | ||
232 | list_add(tokens, token); | ||
233 | } | ||
234 | } | ||
235 | ect_cleanup: | ||
236 | free(argv[0]); // base string | ||
237 | free(argv); | ||
238 | return error; | ||
239 | } | ||
240 | |||
241 | // test a single view if it matches list of criteria tokens (all of them). | ||
242 | static bool criteria_test(swayc_t *cont, list_t *tokens) { | ||
243 | if (cont->type != C_VIEW) { | ||
244 | return false; | ||
245 | } | ||
246 | int matches = 0; | ||
247 | for (int i = 0; i < tokens->length; i++) { | ||
248 | struct crit_token *crit = tokens->items[i]; | ||
249 | switch (crit->type) { | ||
250 | case CRIT_CLASS: | ||
251 | if (!cont->class) { | ||
252 | // ignore | ||
253 | } else if (strcmp(crit->raw, "focused") == 0) { | ||
254 | swayc_t *focused = get_focused_view(&root_container); | ||
255 | if (focused->class && strcmp(cont->class, focused->class) == 0) { | ||
256 | matches++; | ||
257 | } | ||
258 | } else if (crit->regex && regexec(crit->regex, cont->class, 0, NULL, 0) == 0) { | ||
259 | matches++; | ||
260 | } | ||
261 | break; | ||
262 | case CRIT_ID: | ||
263 | if (!cont->app_id) { | ||
264 | // ignore | ||
265 | } else if (crit->regex && regexec(crit->regex, cont->app_id, 0, NULL, 0) == 0) { | ||
266 | matches++; | ||
267 | } | ||
268 | break; | ||
269 | case CRIT_INSTANCE: | ||
270 | break; | ||
271 | case CRIT_TITLE: | ||
272 | if (!cont->name) { | ||
273 | // ignore | ||
274 | } else if (strcmp(crit->raw, "focused") == 0) { | ||
275 | swayc_t *focused = get_focused_view(&root_container); | ||
276 | if (focused->name && strcmp(cont->name, focused->name) == 0) { | ||
277 | matches++; | ||
278 | } | ||
279 | } else if (crit->regex && regexec(crit->regex, cont->name, 0, NULL, 0) == 0) { | ||
280 | matches++; | ||
281 | } | ||
282 | break; | ||
283 | case CRIT_URGENT: // "latest" or "oldest" | ||
284 | break; | ||
285 | case CRIT_WINDOW_ROLE: | ||
286 | break; | ||
287 | case CRIT_WINDOW_TYPE: | ||
288 | // TODO wlc indeed exposes this information | ||
289 | break; | ||
290 | case CRIT_WORKSPACE: ; | ||
291 | swayc_t *cont_ws = swayc_parent_by_type(cont, C_WORKSPACE); | ||
292 | if (!cont_ws || !cont_ws->name) { | ||
293 | // ignore | ||
294 | } else if (strcmp(crit->raw, "focused") == 0) { | ||
295 | swayc_t *focused_ws = swayc_active_workspace(); | ||
296 | if (focused_ws->name && strcmp(cont_ws->name, focused_ws->name) == 0) { | ||
297 | matches++; | ||
298 | } | ||
299 | } else if (crit->regex && regexec(crit->regex, cont_ws->name, 0, NULL, 0) == 0) { | ||
300 | matches++; | ||
301 | } | ||
302 | break; | ||
303 | default: | ||
304 | sway_abort("Invalid criteria type (%i)", crit->type); | ||
305 | break; | ||
306 | } | ||
307 | } | ||
308 | return matches == tokens->length; | ||
309 | } | ||
310 | |||
311 | int criteria_cmp(const void *a, const void *b) { | ||
312 | if (a == b) { | ||
313 | return 0; | ||
314 | } else if (!a) { | ||
315 | return -1; | ||
316 | } else if (!b) { | ||
317 | return 1; | ||
318 | } | ||
319 | const struct criteria *crit_a = a, *crit_b = b; | ||
320 | int cmp = lenient_strcmp(crit_a->cmdlist, crit_b->cmdlist); | ||
321 | if (cmp != 0) { | ||
322 | return cmp; | ||
323 | } | ||
324 | return lenient_strcmp(crit_a->crit_raw, crit_b->crit_raw); | ||
325 | } | ||
326 | |||
327 | void free_criteria(struct criteria *crit) { | ||
328 | if (crit->tokens) { | ||
329 | free_crit_tokens(crit->tokens); | ||
330 | } | ||
331 | if (crit->cmdlist) { | ||
332 | free(crit->cmdlist); | ||
333 | } | ||
334 | if (crit->crit_raw) { | ||
335 | free(crit->crit_raw); | ||
336 | } | ||
337 | free(crit); | ||
338 | } | ||
339 | |||
340 | list_t *criteria_for(swayc_t *cont) { | ||
341 | list_t *criteria = config->criteria, *matches = create_list(); | ||
342 | for (int i = 0; i < criteria->length; i++) { | ||
343 | struct criteria *bc = criteria->items[i]; | ||
344 | if (criteria_test(cont, bc->tokens)) { | ||
345 | list_add(matches, bc); | ||
346 | } | ||
347 | } | ||
348 | return matches; | ||
349 | } | ||
diff --git a/sway/handlers.c b/sway/handlers.c index 28fa9564..267a8f3a 100644 --- a/sway/handlers.c +++ b/sway/handlers.c | |||
@@ -19,6 +19,7 @@ | |||
19 | #include "input_state.h" | 19 | #include "input_state.h" |
20 | #include "resize.h" | 20 | #include "resize.h" |
21 | #include "extensions.h" | 21 | #include "extensions.h" |
22 | #include "criteria.h" | ||
22 | 23 | ||
23 | // Event should be sent to client | 24 | // Event should be sent to client |
24 | #define EVENT_PASSTHROUGH false | 25 | #define EVENT_PASSTHROUGH false |
@@ -172,6 +173,21 @@ static bool handle_view_created(wlc_handle handle) { | |||
172 | set_focused_container(newview); | 173 | set_focused_container(newview); |
173 | swayc_t *output = swayc_parent_by_type(newview, C_OUTPUT); | 174 | swayc_t *output = swayc_parent_by_type(newview, C_OUTPUT); |
174 | arrange_windows(output, -1, -1); | 175 | arrange_windows(output, -1, -1); |
176 | // check if it matches for_window in config and execute if so | ||
177 | list_t *criteria = criteria_for(newview); | ||
178 | for (int i = 0; i < criteria->length; i++) { | ||
179 | struct criteria *crit = criteria->items[i]; | ||
180 | sway_log(L_DEBUG, "for_window '%s' matches new view %p, cmd: '%s'", | ||
181 | crit->crit_raw, newview, crit->cmdlist); | ||
182 | struct cmd_results *res = handle_command(crit->cmdlist); | ||
183 | if (res->status != CMD_SUCCESS) { | ||
184 | sway_log(L_ERROR, "Command '%s' failed: %s", res->input, res->error); | ||
185 | } | ||
186 | free_cmd_results(res); | ||
187 | // view must be focused for commands to affect it, so always | ||
188 | // refocus in-between command lists | ||
189 | set_focused_container(newview); | ||
190 | } | ||
175 | } | 191 | } |
176 | return true; | 192 | return true; |
177 | } | 193 | } |