]> git.sur5r.net Git - i3/i3/blob - src/match.c
Merge branch 'release-4.16.1'
[i3/i3] / src / match.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * A "match" is a data structure which acts like a mask or expression to match
8  * certain windows or not. For example, when using commands, you can specify a
9  * command like this: [title="*Firefox*"] kill. The title member of the match
10  * data structure will then be filled and i3 will check each window using
11  * match_matches_window() to find the windows affected by this command.
12  *
13  */
14 #include "all.h"
15
16 /* From sys/time.h, not sure if it’s available on all systems. */
17 #define _i3_timercmp(a, b, CMP) \
18     (((a).tv_sec == (b).tv_sec) ? ((a).tv_usec CMP(b).tv_usec) : ((a).tv_sec CMP(b).tv_sec))
19
20 /*
21  * Initializes the Match data structure. This function is necessary because the
22  * members representing boolean values (like dock) need to be initialized with
23  * -1 instead of 0.
24  *
25  */
26 void match_init(Match *match) {
27     memset(match, 0, sizeof(Match));
28     match->urgent = U_DONTCHECK;
29     match->window_mode = WM_ANY;
30     /* we use this as the placeholder value for "not set". */
31     match->window_type = UINT32_MAX;
32 }
33
34 /*
35  * Check if a match is empty. This is necessary while parsing commands to see
36  * whether the user specified a match at all.
37  *
38  */
39 bool match_is_empty(Match *match) {
40     /* we cannot simply use memcmp() because the structure is part of a
41      * TAILQ and I don’t want to start with things like assuming that the
42      * last member of a struct really is at the end in memory… */
43     return (match->title == NULL &&
44             match->mark == NULL &&
45             match->application == NULL &&
46             match->class == NULL &&
47             match->instance == NULL &&
48             match->window_role == NULL &&
49             match->workspace == NULL &&
50             match->urgent == U_DONTCHECK &&
51             match->id == XCB_NONE &&
52             match->window_type == UINT32_MAX &&
53             match->con_id == NULL &&
54             match->dock == M_NODOCK &&
55             match->window_mode == WM_ANY);
56 }
57
58 /*
59  * Copies the data of a match from src to dest.
60  *
61  */
62 void match_copy(Match *dest, Match *src) {
63     memcpy(dest, src, sizeof(Match));
64
65 /* The DUPLICATE_REGEX macro creates a new regular expression from the
66  * ->pattern of the old one. It therefore does use a little more memory then
67  *  with a refcounting system, but it’s easier this way. */
68 #define DUPLICATE_REGEX(field)                            \
69     do {                                                  \
70         if (src->field != NULL)                           \
71             dest->field = regex_new(src->field->pattern); \
72     } while (0)
73
74     DUPLICATE_REGEX(title);
75     DUPLICATE_REGEX(mark);
76     DUPLICATE_REGEX(application);
77     DUPLICATE_REGEX(class);
78     DUPLICATE_REGEX(instance);
79     DUPLICATE_REGEX(window_role);
80     DUPLICATE_REGEX(workspace);
81 }
82
83 /*
84  * Check if a match data structure matches the given window.
85  *
86  */
87 bool match_matches_window(Match *match, i3Window *window) {
88     LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
89
90 #define GET_FIELD_str(field) (field)
91 #define GET_FIELD_i3string(field) (i3string_as_utf8(field))
92 #define CHECK_WINDOW_FIELD(match_field, window_field, type)                                       \
93     do {                                                                                          \
94         if (match->match_field != NULL) {                                                         \
95             if (window->window_field == NULL) {                                                   \
96                 return false;                                                                     \
97             }                                                                                     \
98                                                                                                   \
99             const char *window_field_str = GET_FIELD_##type(window->window_field);                \
100             if (strcmp(match->match_field->pattern, "__focused__") == 0 &&                        \
101                 focused && focused->window && focused->window->window_field &&                    \
102                 strcmp(window_field_str, GET_FIELD_##type(focused->window->window_field)) == 0) { \
103                 LOG("window " #match_field " matches focused window\n");                          \
104             } else if (regex_matches(match->match_field, window_field_str)) {                     \
105                 LOG("window " #match_field " matches (%s)\n", window_field_str);                  \
106             } else {                                                                              \
107                 return false;                                                                     \
108             }                                                                                     \
109         }                                                                                         \
110     } while (0)
111
112     CHECK_WINDOW_FIELD(class, class_class, str);
113     CHECK_WINDOW_FIELD(instance, class_instance, str);
114
115     if (match->id != XCB_NONE) {
116         if (window->id == match->id) {
117             LOG("match made by window id (%d)\n", window->id);
118         } else {
119             LOG("window id does not match\n");
120             return false;
121         }
122     }
123
124     CHECK_WINDOW_FIELD(title, name, i3string);
125     CHECK_WINDOW_FIELD(window_role, role, str);
126
127     if (match->window_type != UINT32_MAX) {
128         if (window->window_type == match->window_type) {
129             LOG("window_type matches (%i)\n", match->window_type);
130         } else {
131             return false;
132         }
133     }
134
135     Con *con = NULL;
136     if (match->urgent == U_LATEST) {
137         /* if the window isn't urgent, no sense in searching */
138         if (window->urgent.tv_sec == 0) {
139             return false;
140         }
141         /* if we find a window that is newer than this one, bail */
142         TAILQ_FOREACH(con, &all_cons, all_cons) {
143             if ((con->window != NULL) &&
144                 _i3_timercmp(con->window->urgent, window->urgent, >)) {
145                 return false;
146             }
147         }
148         LOG("urgent matches latest\n");
149     }
150
151     if (match->urgent == U_OLDEST) {
152         /* if the window isn't urgent, no sense in searching */
153         if (window->urgent.tv_sec == 0) {
154             return false;
155         }
156         /* if we find a window that is older than this one (and not 0), bail */
157         TAILQ_FOREACH(con, &all_cons, all_cons) {
158             if ((con->window != NULL) &&
159                 (con->window->urgent.tv_sec != 0) &&
160                 _i3_timercmp(con->window->urgent, window->urgent, <)) {
161                 return false;
162             }
163         }
164         LOG("urgent matches oldest\n");
165     }
166
167     if (match->workspace != NULL) {
168         if ((con = con_by_window_id(window->id)) == NULL)
169             return false;
170
171         Con *ws = con_get_workspace(con);
172         if (ws == NULL)
173             return false;
174
175         if (strcmp(match->workspace->pattern, "__focused__") == 0 &&
176             strcmp(ws->name, con_get_workspace(focused)->name) == 0) {
177             LOG("workspace matches focused workspace\n");
178         } else if (regex_matches(match->workspace, ws->name)) {
179             LOG("workspace matches (%s)\n", ws->name);
180         } else {
181             return false;
182         }
183     }
184
185     if (match->dock != M_DONTCHECK) {
186         if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
187             (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
188             ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
189              match->dock == M_DOCK_ANY) ||
190             (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
191             LOG("dock status matches\n");
192         } else {
193             LOG("dock status does not match\n");
194             return false;
195         }
196     }
197
198     if (match->mark != NULL) {
199         if ((con = con_by_window_id(window->id)) == NULL)
200             return false;
201
202         bool matched = false;
203         mark_t *mark;
204         TAILQ_FOREACH(mark, &(con->marks_head), marks) {
205             if (regex_matches(match->mark, mark->name)) {
206                 matched = true;
207                 break;
208             }
209         }
210
211         if (matched) {
212             LOG("mark matches\n");
213         } else {
214             LOG("mark does not match\n");
215             return false;
216         }
217     }
218
219     if (match->window_mode != WM_ANY) {
220         if ((con = con_by_window_id(window->id)) == NULL)
221             return false;
222
223         const bool floating = (con_inside_floating(con) != NULL);
224
225         if ((match->window_mode == WM_TILING && floating) ||
226             (match->window_mode == WM_FLOATING && !floating)) {
227             LOG("window_mode does not match\n");
228             return false;
229         }
230
231         LOG("window_mode matches\n");
232     }
233
234     return true;
235 }
236
237 /*
238  * Frees the given match. It must not be used afterwards!
239  *
240  */
241 void match_free(Match *match) {
242     FREE(match->error);
243     regex_free(match->title);
244     regex_free(match->application);
245     regex_free(match->class);
246     regex_free(match->instance);
247     regex_free(match->mark);
248     regex_free(match->window_role);
249     regex_free(match->workspace);
250 }
251
252 /*
253  * Interprets a ctype=cvalue pair and adds it to the given match specification.
254  *
255  */
256 void match_parse_property(Match *match, const char *ctype, const char *cvalue) {
257     assert(match != NULL);
258     DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
259
260     if (strcmp(ctype, "class") == 0) {
261         regex_free(match->class);
262         match->class = regex_new(cvalue);
263         return;
264     }
265
266     if (strcmp(ctype, "instance") == 0) {
267         regex_free(match->instance);
268         match->instance = regex_new(cvalue);
269         return;
270     }
271
272     if (strcmp(ctype, "window_role") == 0) {
273         regex_free(match->window_role);
274         match->window_role = regex_new(cvalue);
275         return;
276     }
277
278     if (strcmp(ctype, "con_id") == 0) {
279         if (strcmp(cvalue, "__focused__") == 0) {
280             match->con_id = focused;
281             return;
282         }
283
284         long parsed;
285         if (!parse_long(cvalue, &parsed, 0)) {
286             ELOG("Could not parse con id \"%s\"\n", cvalue);
287             match->error = sstrdup("invalid con_id");
288         } else {
289             match->con_id = (Con *)parsed;
290             DLOG("id as int = %p\n", match->con_id);
291         }
292         return;
293     }
294
295     if (strcmp(ctype, "id") == 0) {
296         long parsed;
297         if (!parse_long(cvalue, &parsed, 0)) {
298             ELOG("Could not parse window id \"%s\"\n", cvalue);
299             match->error = sstrdup("invalid id");
300         } else {
301             match->id = parsed;
302             DLOG("window id as int = %d\n", match->id);
303         }
304         return;
305     }
306
307     if (strcmp(ctype, "window_type") == 0) {
308         if (strcasecmp(cvalue, "normal") == 0) {
309             match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
310         } else if (strcasecmp(cvalue, "dialog") == 0) {
311             match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
312         } else if (strcasecmp(cvalue, "utility") == 0) {
313             match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
314         } else if (strcasecmp(cvalue, "toolbar") == 0) {
315             match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
316         } else if (strcasecmp(cvalue, "splash") == 0) {
317             match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
318         } else if (strcasecmp(cvalue, "menu") == 0) {
319             match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
320         } else if (strcasecmp(cvalue, "dropdown_menu") == 0) {
321             match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
322         } else if (strcasecmp(cvalue, "popup_menu") == 0) {
323             match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
324         } else if (strcasecmp(cvalue, "tooltip") == 0) {
325             match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
326         } else if (strcasecmp(cvalue, "notification") == 0) {
327             match->window_type = A__NET_WM_WINDOW_TYPE_NOTIFICATION;
328         } else {
329             ELOG("unknown window_type value \"%s\"\n", cvalue);
330             match->error = sstrdup("unknown window_type value");
331         }
332
333         return;
334     }
335
336     if (strcmp(ctype, "con_mark") == 0) {
337         regex_free(match->mark);
338         match->mark = regex_new(cvalue);
339         return;
340     }
341
342     if (strcmp(ctype, "title") == 0) {
343         regex_free(match->title);
344         match->title = regex_new(cvalue);
345         return;
346     }
347
348     if (strcmp(ctype, "urgent") == 0) {
349         if (strcasecmp(cvalue, "latest") == 0 ||
350             strcasecmp(cvalue, "newest") == 0 ||
351             strcasecmp(cvalue, "recent") == 0 ||
352             strcasecmp(cvalue, "last") == 0) {
353             match->urgent = U_LATEST;
354         } else if (strcasecmp(cvalue, "oldest") == 0 ||
355                    strcasecmp(cvalue, "first") == 0) {
356             match->urgent = U_OLDEST;
357         }
358         return;
359     }
360
361     if (strcmp(ctype, "workspace") == 0) {
362         regex_free(match->workspace);
363         match->workspace = regex_new(cvalue);
364         return;
365     }
366
367     if (strcmp(ctype, "tiling") == 0) {
368         match->window_mode = WM_TILING;
369         return;
370     }
371
372     if (strcmp(ctype, "floating") == 0) {
373         match->window_mode = WM_FLOATING;
374         return;
375     }
376
377     ELOG("Unknown criterion: %s\n", ctype);
378 }