1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <strings.h>
#include <pcre.h>
#include "sway/criteria.h"
#include "sway/tree/container.h"
#include "sway/config.h"
#include "sway/tree/root.h"
#include "sway/tree/view.h"
#include "sway/tree/workspace.h"
#include "stringop.h"
#include "list.h"
#include "log.h"
#include "config.h"
bool criteria_is_empty(struct criteria *criteria) {
return !criteria->title
&& !criteria->shell
&& !criteria->app_id
&& !criteria->con_mark
&& !criteria->con_id
#if HAVE_XWAYLAND
&& !criteria->class
&& !criteria->id
&& !criteria->instance
&& !criteria->window_role
&& criteria->window_type == ATOM_LAST
#endif
&& !criteria->floating
&& !criteria->tiling
&& !criteria->urgent
&& !criteria->workspace;
}
void criteria_destroy(struct criteria *criteria) {
pcre_free(criteria->title);
pcre_free(criteria->shell);
pcre_free(criteria->app_id);
#if HAVE_XWAYLAND
pcre_free(criteria->class);
pcre_free(criteria->instance);
pcre_free(criteria->window_role);
#endif
pcre_free(criteria->con_mark);
free(criteria->workspace);
free(criteria->cmdlist);
free(criteria->raw);
free(criteria);
}
static int regex_cmp(const char *item, const pcre *regex) {
return pcre_exec(regex, NULL, item, strlen(item), 0, 0, NULL, 0);
}
#if HAVE_XWAYLAND
static bool view_has_window_type(struct sway_view *view, enum atom_name name) {
if (view->type != SWAY_VIEW_XWAYLAND) {
return false;
}
struct wlr_xwayland_surface *surface = view->wlr_xwayland_surface;
struct sway_xwayland *xwayland = &server.xwayland;
xcb_atom_t desired_atom = xwayland->atoms[name];
for (size_t i = 0; i < surface->window_type_len; ++i) {
if (surface->window_type[i] == desired_atom) {
return true;
}
}
return false;
}
#endif
static int cmp_urgent(const void *_a, const void *_b) {
struct sway_view *a = *(void **)_a;
struct sway_view *b = *(void **)_b;
if (a->urgent.tv_sec < b->urgent.tv_sec) {
return -1;
} else if (a->urgent.tv_sec > b->urgent.tv_sec) {
return 1;
}
if (a->urgent.tv_nsec < b->urgent.tv_nsec) {
return -1;
} else if (a->urgent.tv_nsec > b->urgent.tv_nsec) {
return 1;
}
return 0;
}
static void find_urgent_iterator(struct sway_container *con, void *data) {
if (!con->view || !view_is_urgent(con->view)) {
return;
}
list_t *urgent_views = data;
list_add(urgent_views, con->view);
}
static bool criteria_matches_view(struct criteria *criteria,
struct sway_view *view) {
if (criteria->title) {
const char *title = view_get_title(view);
if (!title || regex_cmp(title, criteria->title) != 0) {
return false;
}
}
if (criteria->shell) {
const char *shell = view_get_shell(view);
if (!shell || regex_cmp(shell, criteria->shell) != 0) {
return false;
}
}
if (criteria->app_id) {
const char *app_id = view_get_app_id(view);
if (!app_id || regex_cmp(app_id, criteria->app_id) != 0) {
return false;
}
}
if (criteria->con_mark) {
bool exists = false;
struct sway_container *con = view->container;
for (int i = 0; i < con->marks->length; ++i) {
if (regex_cmp(con->marks->items[i], criteria->con_mark) == 0) {
exists = true;
break;
}
}
if (!exists) {
return false;
}
}
if (criteria->con_id) { // Internal ID
if (!view->container || view->container->node.id != criteria->con_id) {
return false;
}
}
#if HAVE_XWAYLAND
if (criteria->id) { // X11 window ID
uint32_t x11_window_id = view_get_x11_window_id(view);
if (!x11_window_id || x11_window_id != criteria->id) {
return false;
}
}
if (criteria->class) {
const char *class = view_get_class(view);
if (!class || regex_cmp(class, criteria->class) != 0) {
return false;
}
}
if (criteria->instance) {
const char *instance = view_get_instance(view);
if (!instance || regex_cmp(instance, criteria->instance) != 0) {
return false;
}
}
if (criteria->window_role) {
const char *role = view_get_window_role(view);
if (!role || regex_cmp(role, criteria->window_role) != 0) {
return false;
}
}
if (criteria->window_type != ATOM_LAST) {
if (!view_has_window_type(view, criteria->window_type)) {
return false;
}
}
#endif
if (criteria->floating) {
if (!container_is_floating(view->container)) {
return false;
}
}
if (criteria->tiling) {
if (container_is_floating(view->container)) {
return false;
}
}
if (criteria->urgent) {
if (!view_is_urgent(view)) {
return false;
}
list_t *urgent_views = create_list();
root_for_each_container(find_urgent_iterator, urgent_views);
list_stable_sort(urgent_views, cmp_urgent);
struct sway_view *target;
if (criteria->urgent == 'o') { // oldest
target = urgent_views->items[0];
} else { // latest
target = urgent_views->items[urgent_views->length - 1];
}
list_free(urgent_views);
if (view != target) {
return false;
}
}
if (criteria->workspace) {
struct sway_workspace *ws = view->container->workspace;
if (!ws || regex_cmp(ws->name, criteria->workspace) != 0) {
return false;
}
}
return true;
}
list_t *criteria_for_view(struct sway_view *view, enum criteria_type types) {
list_t *criterias = config->criteria;
list_t *matches = create_list();
for (int i = 0; i < criterias->length; ++i) {
struct criteria *criteria = criterias->items[i];
if ((criteria->type & types) && criteria_matches_view(criteria, view)) {
list_add(matches, criteria);
}
}
return matches;
}
struct match_data {
struct criteria *criteria;
list_t *matches;
};
static void criteria_get_views_iterator(struct sway_container *container,
void *data) {
struct match_data *match_data = data;
if (container->view) {
if (criteria_matches_view(match_data->criteria, container->view)) {
list_add(match_data->matches, container->view);
}
}
}
list_t *criteria_get_views(struct criteria *criteria) {
list_t *matches = create_list();
struct match_data data = {
.criteria = criteria,
.matches = matches,
};
root_for_each_container(criteria_get_views_iterator, &data);
return matches;
}
// The error pointer is used for parsing functions, and saves having to pass it
// as an argument in several places.
char *error = NULL;
// Returns error string on failure or NULL otherwise.
static bool generate_regex(pcre **regex, char *value) {
const char *reg_err;
int offset;
*regex = pcre_compile(value, PCRE_UTF8 | PCRE_UCP, ®_err, &offset, NULL);
if (!*regex) {
const char *fmt = "Regex compilation for '%s' failed: %s";
int len = strlen(fmt) + strlen(value) + strlen(reg_err) - 3;
error = malloc(len);
snprintf(error, len, fmt, value, reg_err);
return false;
}
return true;
}
#if HAVE_XWAYLAND
static enum atom_name parse_window_type(const char *type) {
if (strcasecmp(type, "normal") == 0) {
return NET_WM_WINDOW_TYPE_NORMAL;
} else if (strcasecmp(type, "dialog") == 0) {
return NET_WM_WINDOW_TYPE_DIALOG;
} else if (strcasecmp(type, "utility") == 0) {
return NET_WM_WINDOW_TYPE_UTILITY;
} else if (strcasecmp(type, "toolbar") == 0) {
return NET_WM_WINDOW_TYPE_TOOLBAR;
} else if (strcasecmp(type, "splash") == 0) {
return NET_WM_WINDOW_TYPE_SPLASH;
} else if (strcasecmp(type, "menu") == 0) {
return NET_WM_WINDOW_TYPE_MENU;
} else if (strcasecmp(type, "dropdown_menu") == 0) {
return NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
} else if (strcasecmp(type, "popup_menu") == 0) {
return NET_WM_WINDOW_TYPE_POPUP_MENU;
} else if (strcasecmp(type, "tooltip") == 0) {
return NET_WM_WINDOW_TYPE_TOOLTIP;
} else if (strcasecmp(type, "notification") == 0) {
return NET_WM_WINDOW_TYPE_NOTIFICATION;
}
return ATOM_LAST; // ie. invalid
}
#endif
enum criteria_token {
T_APP_ID,
T_CON_ID,
T_CON_MARK,
T_FLOATING,
#if HAVE_XWAYLAND
T_CLASS,
T_ID,
T_INSTANCE,
T_WINDOW_ROLE,
T_WINDOW_TYPE,
#endif
T_SHELL,
T_TILING,
T_TITLE,
T_URGENT,
T_WORKSPACE,
T_INVALID,
};
static enum criteria_token token_from_name(char *name) {
if (strcmp(name, "app_id") == 0) {
return T_APP_ID;
} else if (strcmp(name, "con_id") == 0) {
return T_CON_ID;
} else if (strcmp(name, "con_mark") == 0) {
return T_CON_MARK;
#if HAVE_XWAYLAND
} else if (strcmp(name, "class") == 0) {
return T_CLASS;
} else if (strcmp(name, "id") == 0) {
return T_ID;
} else if (strcmp(name, "instance") == 0) {
return T_INSTANCE;
} else if (strcmp(name, "window_role") == 0) {
return T_WINDOW_ROLE;
} else if (strcmp(name, "window_type") == 0) {
return T_WINDOW_TYPE;
#endif
} else if (strcmp(name, "shell") == 0) {
return T_SHELL;
} else if (strcmp(name, "title") == 0) {
return T_TITLE;
} else if (strcmp(name, "urgent") == 0) {
return T_URGENT;
} else if (strcmp(name, "workspace") == 0) {
return T_WORKSPACE;
} else if (strcmp(name, "tiling") == 0) {
return T_TILING;
} else if (strcmp(name, "floating") == 0) {
return T_FLOATING;
}
return T_INVALID;
}
/**
* Get a property of the focused view.
*
* Note that we are taking the focused view at the time of criteria parsing, not
* at the time of execution. This is because __focused__ only makes sense when
* using criteria via IPC. Using __focused__ in config is not useful because
* criteria is only executed once per view.
*/
static char *get_focused_prop(enum criteria_token token) {
struct sway_seat *seat = input_manager_current_seat();
struct sway_container *focus = seat_get_focused_container(seat);
if (!focus || !focus->view) {
return NULL;
}
struct sway_view *view = focus->view;
const char *value = NULL;
switch (token) {
case T_APP_ID:
value = view_get_app_id(view);
break;
case T_SHELL:
value = view_get_shell(view);
break;
case T_TITLE:
value = view_get_title(view);
break;
case T_WORKSPACE:
if (focus->workspace) {
value = focus->workspace->name;
}
break;
case T_CON_ID:
if (view->container == NULL) {
return NULL;
}
size_t id = view->container->node.id;
size_t id_size = snprintf(NULL, 0, "%zu", id) + 1;
char *id_str = malloc(id_size);
snprintf(id_str, id_size, "%zu", id);
value = id_str;
break;
#if HAVE_XWAYLAND
case T_CLASS:
value = view_get_class(view);
break;
case T_INSTANCE:
value = view_get_instance(view);
break;
case T_WINDOW_ROLE:
value = view_get_window_role(view);
break;
case T_WINDOW_TYPE: // These do not support __focused__
case T_ID:
#endif
case T_CON_MARK:
case T_FLOATING:
case T_TILING:
case T_URGENT:
case T_INVALID:
break;
}
if (value) {
return strdup(value);
}
return NULL;
}
static bool parse_token(struct criteria *criteria, char *name, char *value) {
enum criteria_token token = token_from_name(name);
if (token == T_INVALID) {
const char *fmt = "Token '%s' is not recognized";
int len = strlen(fmt) + strlen(name) - 1;
error = malloc(len);
snprintf(error, len, fmt, name);
return false;
}
char *effective_value = NULL;
if (value && strcmp(value, "__focused__") == 0) {
effective_value = get_focused_prop(token);
} else if (value) {
effective_value = strdup(value);
}
// Require value, unless token is floating or tiled
if (!effective_value && token != T_FLOATING && token != T_TILING) {
const char *fmt = "Token '%s' requires a value";
int len = strlen(fmt) + strlen(name) - 1;
error = malloc(len);
snprintf(error, len, fmt, name);
return false;
}
char *endptr = NULL;
switch (token) {
case T_TITLE:
generate_regex(&criteria->title, effective_value);
break;
case T_SHELL:
generate_regex(&criteria->shell, effective_value);
break;
case T_APP_ID:
generate_regex(&criteria->app_id, effective_value);
break;
case T_CON_ID:
criteria->con_id = strtoul(effective_value, &endptr, 10);
if (*endptr != 0) {
error = strdup("The value for 'con_id' should be '__focused__' or numeric");
}
break;
case T_CON_MARK:
generate_regex(&criteria->con_mark, effective_value);
break;
#if HAVE_XWAYLAND
case T_CLASS:
generate_regex(&criteria->class, effective_value);
break;
case T_ID:
criteria->id = strtoul(effective_value, &endptr, 10);
if (*endptr != 0) {
error = strdup("The value for 'id' should be numeric");
}
break;
case T_INSTANCE:
generate_regex(&criteria->instance, effective_value);
break;
case T_WINDOW_ROLE:
generate_regex(&criteria->window_role, effective_value);
break;
case T_WINDOW_TYPE:
criteria->window_type = parse_window_type(effective_value);
break;
#endif
case T_FLOATING:
criteria->floating = true;
break;
case T_TILING:
criteria->tiling = true;
break;
case T_URGENT:
if (strcmp(effective_value, "latest") == 0 ||
strcmp(effective_value, "newest") == 0 ||
strcmp(effective_value, "last") == 0 ||
strcmp(effective_value, "recent") == 0) {
criteria->urgent = 'l';
} else if (strcmp(effective_value, "oldest") == 0 ||
strcmp(effective_value, "first") == 0) {
criteria->urgent = 'o';
} else {
error =
strdup("The value for 'urgent' must be 'first', 'last', "
"'latest', 'newest', 'oldest' or 'recent'");
}
break;
case T_WORKSPACE:
generate_regex(&criteria->workspace, effective_value);
break;
case T_INVALID:
break;
}
free(effective_value);
if (error) {
return false;
}
return true;
}
static void skip_spaces(char **head) {
while (**head == ' ') {
++*head;
}
}
// Remove escaping slashes from value
static void unescape(char *value) {
if (!strchr(value, '\\')) {
return;
}
char *copy = calloc(strlen(value) + 1, 1);
char *readhead = value;
char *writehead = copy;
while (*readhead) {
if (*readhead == '\\' && *(readhead + 1) == '"') {
// skip the slash
++readhead;
}
*writehead = *readhead;
++writehead;
++readhead;
}
strcpy(value, copy);
free(copy);
}
/**
* Parse a raw criteria string such as [class="foo" instance="bar"] into a
* criteria struct.
*
* If errors are found, NULL will be returned and the error argument will be
* populated with an error string. It is up to the caller to free the error.
*/
struct criteria *criteria_parse(char *raw, char **error_arg) {
*error_arg = NULL;
error = NULL;
char *head = raw;
skip_spaces(&head);
if (*head != '[') {
*error_arg = strdup("No criteria");
return NULL;
}
++head;
struct criteria *criteria = calloc(1, sizeof(struct criteria));
#if HAVE_XWAYLAND
criteria->window_type = ATOM_LAST; // default value
#endif
char *name = NULL, *value = NULL;
bool in_quotes = false;
while (*head && *head != ']') {
skip_spaces(&head);
// Parse token name
char *namestart = head;
while ((*head >= 'a' && *head <= 'z') || *head == '_') {
++head;
}
name = calloc(head - namestart + 1, 1);
if (head != namestart) {
strncpy(name, namestart, head - namestart);
}
// Parse token value
skip_spaces(&head);
value = NULL;
if (*head == '=') {
++head;
skip_spaces(&head);
if (*head == '"') {
in_quotes = true;
++head;
}
char *valuestart = head;
if (in_quotes) {
while (*head && (*head != '"' || *(head - 1) == '\\')) {
++head;
}
if (!*head) {
*error_arg = strdup("Quote mismatch in criteria");
goto cleanup;
}
} else {
while (*head && *head != ' ' && *head != ']') {
++head;
}
}
value = calloc(head - valuestart + 1, 1);
strncpy(value, valuestart, head - valuestart);
if (in_quotes) {
++head;
in_quotes = false;
}
unescape(value);
sway_log(SWAY_DEBUG, "Found pair: %s=%s", name, value);
}
if (!parse_token(criteria, name, value)) {
*error_arg = error;
goto cleanup;
}
skip_spaces(&head);
free(name);
free(value);
name = NULL;
value = NULL;
}
if (*head != ']') {
*error_arg = strdup("No closing brace found in criteria");
goto cleanup;
}
if (criteria_is_empty(criteria)) {
*error_arg = strdup("Criteria is empty");
goto cleanup;
}
++head;
int len = head - raw;
criteria->raw = calloc(len + 1, 1);
strncpy(criteria->raw, raw, len);
return criteria;
cleanup:
free(name);
free(value);
criteria_destroy(criteria);
return NULL;
}
|