-
Notifications
You must be signed in to change notification settings - Fork 136
/
criteria.c
573 lines (505 loc) · 15.6 KB
/
criteria.c
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
#include <assert.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <wayland-client.h>
#include "enum.h"
#include "mako.h"
#include "config.h"
#include "criteria.h"
#include "mode.h"
#include "notification.h"
#include "surface.h"
#include "wayland.h"
struct mako_criteria *create_criteria(struct mako_config *config) {
struct mako_criteria *criteria = calloc(1, sizeof(struct mako_criteria));
if (criteria == NULL) {
fprintf(stderr, "allocation failed\n");
return NULL;
}
wl_list_insert(config->criteria.prev, &criteria->link);
return criteria;
}
void destroy_criteria(struct mako_criteria *criteria) {
wl_list_remove(&criteria->link);
finish_style(&criteria->style);
free(criteria->app_name);
free(criteria->app_icon);
free(criteria->category);
free(criteria->desktop_entry);
free(criteria->summary);
regfree(&criteria->summary_pattern);
free(criteria->body);
regfree(&criteria->body_pattern);
free(criteria->raw_string);
free(criteria->output);
free(criteria->mode);
free(criteria);
}
static bool match_regex_criteria(regex_t *pattern, char *value) {
int ret = regexec(pattern, value, 0, NULL, 0);
if (ret != 0) {
if (ret != REG_NOMATCH) {
size_t errlen = regerror(ret, pattern, NULL, 0);
char errbuf[errlen];
regerror(ret, pattern, errbuf, sizeof(errbuf));
fprintf(stderr, "failed to match regex: %s\n", errbuf);
}
return false;
}
return true;
}
bool match_criteria(struct mako_criteria *criteria,
struct mako_notification *notif) {
struct mako_criteria_spec spec = criteria->spec;
if (spec.none) {
// `none` short-circuits all other criteria.
return false;
}
if (spec.hidden &&
criteria->hidden != notif->hidden) {
return false;
}
if (spec.app_name &&
strcmp(criteria->app_name, notif->app_name) != 0) {
return false;
}
if (spec.app_icon &&
strcmp(criteria->app_icon, notif->app_icon) != 0) {
return false;
}
if (spec.actionable &&
criteria->actionable == wl_list_empty(¬if->actions)) {
return false;
}
if (spec.expiring &&
criteria->expiring != (notif->requested_timeout != 0)) {
return false;
}
if (spec.urgency &&
criteria->urgency != notif->urgency) {
return false;
}
if (spec.category &&
strcmp(criteria->category, notif->category) != 0) {
return false;
}
if (spec.desktop_entry &&
strcmp(criteria->desktop_entry, notif->desktop_entry) != 0) {
return false;
}
if (spec.summary &&
strcmp(criteria->summary, notif->summary) != 0) {
return false;
}
if (spec.summary_pattern) {
bool ret = match_regex_criteria(&criteria->summary_pattern, notif->summary);
if (!ret) {
return false;
}
}
if (spec.body &&
strcmp(criteria->body, notif->body) != 0) {
return false;
}
if (spec.body_pattern) {
bool ret = match_regex_criteria(&criteria->body_pattern, notif->body);
if (!ret) {
return false;
}
}
if (spec.group_index &&
criteria->group_index != notif->group_index) {
return false;
}
if (spec.grouped &&
criteria->grouped != (notif->group_index >= 0)) {
return false;
}
if (spec.anchor && (notif->surface == NULL ||
criteria->anchor != notif->surface->anchor)) {
return false;
}
if (spec.output && (notif->surface == NULL ||
notif->surface->surface_output == NULL ||
strcmp(criteria->output, notif->surface->surface_output->name) != 0)) {
return false;
}
if (spec.mode && !has_mode(notif->state, criteria->mode)) {
return false;
}
return true;
}
bool parse_criteria(const char *string, struct mako_criteria *criteria) {
// Create space to build up the current token that we're reading. We know
// that no single token can ever exceed the length of the entire criteria
// string, so that's a safe length to use for the buffer.
int token_max_length = strlen(string) + 1;
char token[token_max_length];
memset(token, 0, token_max_length);
size_t token_location = 0;
enum mako_parse_state state = MAKO_PARSE_STATE_NORMAL;
const char *location = string;
char ch;
while ((ch = *location++) != '\0') {
switch (state) {
case MAKO_PARSE_STATE_ESCAPE:
case MAKO_PARSE_STATE_QUOTE_ESCAPE:
token[token_location] = ch;
++token_location;
state &= ~MAKO_PARSE_STATE_ESCAPE; // These work as a bitmask.
break;
case MAKO_PARSE_STATE_QUOTE:
switch (ch) {
case '\\':
state = MAKO_PARSE_STATE_QUOTE_ESCAPE;
break;
case '"':
state = MAKO_PARSE_STATE_NORMAL;
break;
case ' ':
default:
token[token_location] = ch;
++token_location;
}
break;
case MAKO_PARSE_STATE_NORMAL:
switch (ch) {
case '\\':
state = MAKO_PARSE_STATE_ESCAPE;
break;
case '"':
state = MAKO_PARSE_STATE_QUOTE;
break;
case ' ':
// New token, apply the old one and reset our state.
if (!apply_criteria_field(criteria, token)) {
// An error should have been printed already.
return false;
}
memset(token, 0, token_max_length);
token_location = 0;
break;
default:
token[token_location] = ch;
++token_location;
}
break;
case MAKO_PARSE_STATE_FORMAT:
// Unsupported state for this parser.
abort();
}
}
if (state != MAKO_PARSE_STATE_NORMAL) {
if (state & MAKO_PARSE_STATE_QUOTE) {
fprintf(stderr, "Unmatched quote in criteria definition\n");
return false;
} else if (state & MAKO_PARSE_STATE_ESCAPE) {
fprintf(stderr, "Trailing backslash in criteria definition\n");
return false;
} else {
fprintf(stderr, "Got confused parsing criteria definition\n");
return false;
}
}
// Apply the last token, which will be left in the buffer after we hit the
// final NULL. We know it's valid since we just checked for that.
if (!apply_criteria_field(criteria, token)) {
// An error should have been printed by this point, we don't need to.
return false;
}
// All user-specified criteria are implicitly unhidden by default. This
// prevents any criteria sections that don't explicitly set `hidden` from
// styling the hidden pseudo-notification.
if (!criteria->spec.hidden) {
criteria->hidden = false;
criteria->spec.hidden = true;
}
criteria->raw_string = strdup(string);
return true;
}
// Takes a token from the criteria string that looks like "key=value", figures
// out which field of the criteria "key" refers to, and sets it to "value".
// Any further equal signs are assumed to be part of the value. If there is no .
// equal sign present, the field is treated as a boolean, with a leading
// exclamation point signifying negation.
//
// Note that the token will be consumed.
bool apply_criteria_field(struct mako_criteria *criteria, char *token) {
char *key = token;
char *value = strstr(key, "=");
bool bare_key = !value;
if (*key == '\0') {
return true;
}
if (value) {
// Skip past the equal sign to the value itself.
*value = '\0';
++value;
} else {
// If there's no value, assume it's a boolean, and set the value
// appropriately. This allows uniform parsing later on.
if (*key == '!') {
// Negated boolean, skip past the exclamation point.
++key;
value = "false";
} else {
value = "true";
}
}
// Now apply the value to the appropriate member of the criteria.
// If the value was omitted, only try to match against boolean fields.
// Otherwise, anything is fair game. This helps to return a better error
// message.
if (!bare_key) {
if (strcmp(key, "app-name") == 0) {
criteria->app_name = strdup(value);
criteria->spec.app_name = true;
return true;
} else if (strcmp(key, "app-icon") == 0) {
criteria->app_icon = strdup(value);
criteria->spec.app_icon = true;
return true;
} else if (strcmp(key, "urgency") == 0) {
if (!parse_urgency(value, &criteria->urgency)) {
fprintf(stderr, "Invalid urgency value '%s'", value);
return false;
}
criteria->spec.urgency = true;
return true;
} else if (strcmp(key, "category") == 0) {
criteria->category = strdup(value);
criteria->spec.category = true;
return true;
} else if (strcmp(key, "desktop-entry") == 0) {
criteria->desktop_entry = strdup(value);
criteria->spec.desktop_entry = true;
return true;
} else if (strcmp(key, "group-index") == 0) {
if (!parse_int(value, &criteria->group_index)) {
fprintf(stderr, "Invalid group-index value '%s'", value);
return false;
}
criteria->spec.group_index = true;
return true;
} else if (strcmp(key, "summary") == 0) {
criteria->summary = strdup(value);
criteria->spec.summary = true;
return true;
} else if (strcmp(key, "summary~") == 0) {
if (regcomp(&criteria->summary_pattern, value,
REG_EXTENDED | REG_NOSUB)) {
fprintf(stderr, "Invalid summary~ regex '%s'\n", value);
return false;
}
criteria->spec.summary_pattern = true;
return true;
} else if (strcmp(key, "body") == 0) {
criteria->body = strdup(value);
criteria->spec.body = true;
return true;
} else if (strcmp(key, "body~") == 0) {
if (regcomp(&criteria->body_pattern, value,
REG_EXTENDED | REG_NOSUB)) {
fprintf(stderr, "Invalid body~ regex '%s'\n", value);
return false;
}
criteria->spec.body_pattern = true;
return true;
} else if (strcmp(key, "anchor") == 0) {
return criteria->spec.anchor =
parse_anchor(value, &criteria->anchor);
} else if (strcmp(key, "output") == 0) {
criteria->output = strdup(value);
criteria->spec.output = true;
return true;
} else if (strcmp(key, "mode") == 0) {
criteria->mode = strdup(value);
criteria->spec.mode = true;
return true;
} else {
// Anything left must be one of the boolean fields, defined using
// standard syntax. Continue on.
}
}
if (strcmp(key, "actionable") == 0) {
if (!parse_boolean(value, &criteria->actionable)) {
fprintf(stderr, "Invalid value '%s' for boolean field '%s'\n",
value, key);
return false;
}
criteria->spec.actionable = true;
return true;
} else if (strcmp(key, "expiring") == 0){
if (!parse_boolean(value, &criteria->expiring)) {
fprintf(stderr, "Invalid value '%s' for boolean field '%s'\n",
value, key);
return false;
}
criteria->spec.expiring = true;
return true;
} else if (strcmp(key, "grouped") == 0) {
if (!parse_boolean(value, &criteria->grouped)) {
fprintf(stderr, "Invalid value '%s' for boolean field '%s'\n",
value, key);
return false;
}
criteria->spec.grouped = true;
return true;
} else if (strcmp(key, "hidden") == 0) {
if (!parse_boolean(value, &criteria->hidden)) {
fprintf(stderr, "Invalid value '%s' for boolean field '%s'\n",
value, key);
return false;
}
criteria->spec.hidden = true;
return true;
} else {
if (bare_key) {
fprintf(stderr, "Invalid boolean criteria field '%s'\n", key);
} else {
fprintf(stderr, "Invalid criteria field '%s'\n", key);
}
return false;
}
assert(false && "Criteria parser fell through");
}
// Retrieve the global criteria from a given mako_config. This just so happens
// to be the first criteria in the list.
struct mako_criteria *global_criteria(struct mako_config *config) {
struct mako_criteria *criteria =
wl_container_of(config->criteria.next, criteria, link);
return criteria;
}
// Iterate through `criteria_list`, applying the style from each matching
// criteria to `notif`. Returns the number of criteria that matched, or -1 if
// a failure occurs.
ssize_t apply_each_criteria(struct wl_list *criteria_list,
struct mako_notification *notif) {
ssize_t match_count = 0;
struct mako_criteria *criteria;
wl_list_for_each(criteria, criteria_list, link) {
if (!match_criteria(criteria, notif)) {
continue;
}
++match_count;
if (!apply_style(¬if->style, &criteria->style)) {
return -1;
}
}
struct mako_surface *surface;
wl_list_for_each(surface, ¬if->state->surfaces, link) {
if (!strcmp(surface->configured_output, notif->style.output) &&
surface->anchor == notif->style.anchor &&
surface->layer == notif->style.layer) {
notif->surface = surface;
break;
}
}
if (!notif->surface) {
notif->surface = create_surface(notif->state, notif->style.output,
notif->style.layer, notif->style.anchor);
}
return match_count;
}
// Given a notification and a criteria spec, create a criteria that matches the
// specified fields of that notification. Unlike create_criteria, this new
// criteria will not be automatically inserted into the configuration. It is
// instead intended to be used for comparing notifications. The spec will be
// copied, so the caller is responsible for doing whatever it needs to do with
// the original after the call completes.
struct mako_criteria *create_criteria_from_notification(
struct mako_notification *notif, struct mako_criteria_spec *spec) {
struct mako_criteria *criteria = calloc(1, sizeof(struct mako_criteria));
if (criteria == NULL) {
fprintf(stderr, "allocation failed\n");
return NULL;
}
wl_list_init(&criteria->link);
memcpy(&criteria->spec, spec, sizeof(struct mako_criteria_spec));
// We only really need to copy the ones that are in the spec, but it
// doesn't hurt anything to do the rest and it makes this code much nicer
// to look at.
criteria->app_name = strdup(notif->app_name);
criteria->app_icon = strdup(notif->app_icon);
criteria->actionable = !wl_list_empty(¬if->actions);
criteria->expiring = (notif->requested_timeout != 0);
criteria->urgency = notif->urgency;
criteria->category = strdup(notif->category);
criteria->desktop_entry = strdup(notif->desktop_entry);
criteria->summary = strdup(notif->summary);
criteria->body = strdup(notif->body);
criteria->group_index = notif->group_index;
criteria->grouped = (notif->group_index >= 0);
criteria->hidden = notif->hidden;
return criteria;
}
// To keep the behavior of criteria predictable, there are a few rules that we
// have to impose on what can be modified depending on what was matched.
bool validate_criteria(struct mako_criteria *criteria) {
char * invalid_option = NULL;
if (criteria->spec.grouped ||
criteria->spec.group_index ||
criteria->spec.output ||
criteria->spec.anchor) {
if (criteria->style.spec.anchor) {
invalid_option = "anchor";
} else if (criteria->style.spec.output) {
invalid_option = "output";
} else if (criteria->style.spec.group_criteria_spec) {
invalid_option = "group-by";
}
if (invalid_option) {
fprintf(stderr,
"Setting `%s` is not allowed when matching `grouped`, "
"`group-index`, `output`, or `anchor`\n",
invalid_option);
return false;
}
}
struct mako_criteria_spec copy = {0};
memcpy(©, &criteria->spec, sizeof(struct mako_criteria_spec));
copy.output = false;
copy.anchor = false;
copy.hidden = false;
bool any_but_surface = mako_criteria_spec_any(©);
if (criteria->style.max_visible && any_but_surface) {
fprintf(stderr, "Setting `max_visible` is allowed only for `output` "
"and/or `anchor`\n");
return false;
}
// Hidden is almost always specified, need to look at the actual value.
if (criteria->hidden && any_but_surface) {
fprintf(stderr, "Can only set `hidden` along with `output` "
"and/or `anchor`\n");
return false;
}
if (criteria->spec.summary && criteria->spec.summary_pattern) {
fprintf(stderr, "Cannot set both `summary` and `summary~`\n");
return false;
}
if (criteria->spec.body && criteria->spec.body_pattern) {
fprintf(stderr, "Cannot set both `body` and `body~`\n");
return false;
}
if (criteria->style.spec.group_criteria_spec) {
struct mako_criteria_spec *spec = &criteria->style.group_criteria_spec;
if (spec->group_index) {
invalid_option = "group-index";
} else if (spec->grouped) {
invalid_option = "grouped";
} else if (spec->anchor) {
invalid_option = "anchor";
} else if (spec->output) {
invalid_option = "output";
}
if (invalid_option) {
fprintf(stderr, "`%s` cannot be used in `group-by`\n",
invalid_option);
return false;
}
}
return true;
}