]> git.sur5r.net Git - i3/i3/blob - src/match.c
Implement new criterion 'window_type = normal|dialog|utility|toolbar|splash|menu...
[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-2011 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->urgent == U_DONTCHECK &&
52             match->id == XCB_NONE &&
53             match->window_type == UINT32_MAX &&
54             match->con_id == NULL &&
55             match->dock == -1 &&
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 }
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     if (match->class != NULL) {
91         if (window->class_class != NULL &&
92             regex_matches(match->class, window->class_class)) {
93             LOG("window class matches (%s)\n", window->class_class);
94         } else {
95             return false;
96         }
97     }
98
99     if (match->instance != NULL) {
100         if (window->class_instance != NULL &&
101             regex_matches(match->instance, window->class_instance)) {
102             LOG("window instance matches (%s)\n", window->class_instance);
103         } else {
104             return false;
105         }
106     }
107
108     if (match->id != XCB_NONE) {
109         if (window->id == match->id) {
110             LOG("match made by window id (%d)\n", window->id);
111         } else {
112             LOG("window id does not match\n");
113             return false;
114         }
115     }
116
117     if (match->title != NULL) {
118         if (window->name != NULL &&
119             regex_matches(match->title, i3string_as_utf8(window->name))) {
120             LOG("title matches (%s)\n", i3string_as_utf8(window->name));
121         } else {
122             return false;
123         }
124     }
125
126     if (match->window_role != NULL) {
127         if (window->role != NULL &&
128             regex_matches(match->window_role, window->role)) {
129             LOG("window_role matches (%s)\n", window->role);
130         } else {
131             return false;
132         }
133     }
134
135     if (match->window_type != UINT32_MAX) {
136         if (window->window_type == match->window_type) {
137             LOG("window_type matches (%i)\n", match->window_type);
138         } else {
139             return false;
140         }
141     }
142
143     Con *con = NULL;
144     if (match->urgent == U_LATEST) {
145         /* if the window isn't urgent, no sense in searching */
146         if (window->urgent.tv_sec == 0) {
147             return false;
148         }
149         /* if we find a window that is newer than this one, bail */
150         TAILQ_FOREACH(con, &all_cons, all_cons) {
151             if ((con->window != NULL) &&
152                 _i3_timercmp(con->window->urgent, window->urgent, > )) {
153                 return false;
154             }
155         }
156         LOG("urgent matches latest\n");
157     }
158
159     if (match->urgent == U_OLDEST) {
160         /* if the window isn't urgent, no sense in searching */
161         if (window->urgent.tv_sec == 0) {
162             return false;
163         }
164         /* if we find a window that is older than this one (and not 0), bail */
165         TAILQ_FOREACH(con, &all_cons, all_cons) {
166             if ((con->window != NULL) &&
167                 (con->window->urgent.tv_sec != 0) &&
168                 _i3_timercmp(con->window->urgent, window->urgent, < )) {
169                 return false;
170             }
171         }
172         LOG("urgent matches oldest\n");
173     }
174
175     if (match->dock != M_DONTCHECK) {
176         if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
177             (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
178             ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
179              match->dock == M_DOCK_ANY) ||
180             (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
181             LOG("dock status matches\n");
182         } else {
183             LOG("dock status does not match\n");
184             return false;
185         }
186     }
187
188     /* We don’t check the mark because this function is not even called when
189      * the mark would have matched - it is checked in cmdparse.y itself */
190     if (match->mark != NULL) {
191         LOG("mark does not match\n");
192         return false;
193     }
194
195     return true;
196 }
197
198 /*
199  * Frees the given match. It must not be used afterwards!
200  *
201  */
202 void match_free(Match *match) {
203     /* First step: free the regex fields / patterns */
204     regex_free(match->title);
205     regex_free(match->application);
206     regex_free(match->class);
207     regex_free(match->instance);
208     regex_free(match->mark);
209     regex_free(match->window_role);
210
211     /* Second step: free the regex helper struct itself */
212     FREE(match->title);
213     FREE(match->application);
214     FREE(match->class);
215     FREE(match->instance);
216     FREE(match->mark);
217     FREE(match->window_role);
218 }