]> git.sur5r.net Git - i3/i3/blob - src/match.c
Merge branch 'master' into next
[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->dock = M_DONTCHECK;
31     match->urgent = U_DONTCHECK;
32     /* we use this as the placeholder value for "not set". */
33     match->window_type = UINT32_MAX;
34 }
35
36 /*
37  * Check if a match is empty. This is necessary while parsing commands to see
38  * whether the user specified a match at all.
39  *
40  */
41 bool match_is_empty(Match *match) {
42     /* we cannot simply use memcmp() because the structure is part of a
43      * TAILQ and I don’t want to start with things like assuming that the
44      * last member of a struct really is at the end in memory… */
45     return (match->title == NULL &&
46             match->mark == NULL &&
47             match->application == NULL &&
48             match->class == NULL &&
49             match->instance == NULL &&
50             match->window_role == NULL &&
51             match->workspace == NULL &&
52             match->urgent == U_DONTCHECK &&
53             match->id == XCB_NONE &&
54             match->window_type == UINT32_MAX &&
55             match->con_id == NULL &&
56             match->dock == -1 &&
57             match->floating == M_ANY);
58 }
59
60 /*
61  * Copies the data of a match from src to dest.
62  *
63  */
64 void match_copy(Match *dest, Match *src) {
65     memcpy(dest, src, sizeof(Match));
66
67 /* The DUPLICATE_REGEX macro creates a new regular expression from the
68  * ->pattern of the old one. It therefore does use a little more memory then
69  *  with a refcounting system, but it’s easier this way. */
70 #define DUPLICATE_REGEX(field)                            \
71     do {                                                  \
72         if (src->field != NULL)                           \
73             dest->field = regex_new(src->field->pattern); \
74     } while (0)
75
76     DUPLICATE_REGEX(title);
77     DUPLICATE_REGEX(mark);
78     DUPLICATE_REGEX(application);
79     DUPLICATE_REGEX(class);
80     DUPLICATE_REGEX(instance);
81     DUPLICATE_REGEX(window_role);
82     DUPLICATE_REGEX(workspace);
83 }
84
85 /*
86  * Check if a match data structure matches the given window.
87  *
88  */
89 bool match_matches_window(Match *match, i3Window *window) {
90     LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
91
92     if (match->class != NULL) {
93         if (window->class_class != NULL &&
94             regex_matches(match->class, window->class_class)) {
95             LOG("window class matches (%s)\n", window->class_class);
96         } else {
97             return false;
98         }
99     }
100
101     if (match->instance != NULL) {
102         if (window->class_instance != NULL &&
103             regex_matches(match->instance, window->class_instance)) {
104             LOG("window instance matches (%s)\n", window->class_instance);
105         } else {
106             return false;
107         }
108     }
109
110     if (match->id != XCB_NONE) {
111         if (window->id == match->id) {
112             LOG("match made by window id (%d)\n", window->id);
113         } else {
114             LOG("window id does not match\n");
115             return false;
116         }
117     }
118
119     if (match->title != NULL) {
120         if (window->name != NULL &&
121             regex_matches(match->title, i3string_as_utf8(window->name))) {
122             LOG("title matches (%s)\n", i3string_as_utf8(window->name));
123         } else {
124             return false;
125         }
126     }
127
128     if (match->window_role != NULL) {
129         if (window->role != NULL &&
130             regex_matches(match->window_role, window->role)) {
131             LOG("window_role matches (%s)\n", window->role);
132         } else {
133             return false;
134         }
135     }
136
137     if (match->window_type != UINT32_MAX) {
138         if (window->window_type == match->window_type) {
139             LOG("window_type matches (%i)\n", match->window_type);
140         } else {
141             return false;
142         }
143     }
144
145     Con *con = NULL;
146     if (match->urgent == U_LATEST) {
147         /* if the window isn't urgent, no sense in searching */
148         if (window->urgent.tv_sec == 0) {
149             return false;
150         }
151         /* if we find a window that is newer than this one, bail */
152         TAILQ_FOREACH(con, &all_cons, all_cons) {
153             if ((con->window != NULL) &&
154                 _i3_timercmp(con->window->urgent, window->urgent, > )) {
155                 return false;
156             }
157         }
158         LOG("urgent matches latest\n");
159     }
160
161     if (match->urgent == U_OLDEST) {
162         /* if the window isn't urgent, no sense in searching */
163         if (window->urgent.tv_sec == 0) {
164             return false;
165         }
166         /* if we find a window that is older than this one (and not 0), bail */
167         TAILQ_FOREACH(con, &all_cons, all_cons) {
168             if ((con->window != NULL) &&
169                 (con->window->urgent.tv_sec != 0) &&
170                 _i3_timercmp(con->window->urgent, window->urgent, < )) {
171                 return false;
172             }
173         }
174         LOG("urgent matches oldest\n");
175     }
176
177     if (match->workspace != NULL) {
178         if ((con = con_by_window_id(window->id)) == NULL)
179             return false;
180
181         Con *ws = con_get_workspace(con);
182         if (ws == NULL)
183             return false;
184
185         if (regex_matches(match->workspace, ws->name)) {
186             LOG("workspace matches (%s)\n", ws->name);
187         } else {
188             return false;
189         }
190     }
191
192     if (match->dock != M_DONTCHECK) {
193         if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
194             (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
195             ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
196              match->dock == M_DOCK_ANY) ||
197             (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
198             LOG("dock status matches\n");
199         } else {
200             LOG("dock status does not match\n");
201             return false;
202         }
203     }
204
205     /* We don’t check the mark because this function is not even called when
206      * the mark would have matched - it is checked in cmdparse.y itself */
207     if (match->mark != NULL) {
208         LOG("mark does not match\n");
209         return false;
210     }
211
212     return true;
213 }
214
215 /*
216  * Frees the given match. It must not be used afterwards!
217  *
218  */
219 void match_free(Match *match) {
220     /* First step: free the regex fields / patterns */
221     regex_free(match->title);
222     regex_free(match->application);
223     regex_free(match->class);
224     regex_free(match->instance);
225     regex_free(match->mark);
226     regex_free(match->window_role);
227
228     /* Second step: free the regex helper struct itself */
229     FREE(match->title);
230     FREE(match->application);
231     FREE(match->class);
232     FREE(match->instance);
233     FREE(match->mark);
234     FREE(match->window_role);
235 }