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