2 #define I3__FILE__ "match.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
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.
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))
23 * Initializes the Match data structure. This function is necessary because the
24 * members representing boolean values (like dock) need to be initialized with
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;
37 * Check if a match is empty. This is necessary while parsing commands to see
38 * whether the user specified a match at all.
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 &&
57 match->floating == M_ANY);
61 * Copies the data of a match from src to dest.
64 void match_copy(Match *dest, Match *src) {
65 memcpy(dest, src, sizeof(Match));
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) \
72 if (src->field != NULL) \
73 dest->field = regex_new(src->field->pattern); \
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);
86 * Check if a match data structure matches the given window.
89 bool match_matches_window(Match *match, i3Window *window) {
90 LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
92 if (match->class != NULL) {
93 if (window->class_class == NULL)
95 if (strcmp(match->class->pattern, "__focused__") == 0 &&
96 strcmp(window->class_class, focused->window->class_class) == 0) {
97 LOG("window class matches focused window\n");
98 } else if (regex_matches(match->class, window->class_class)) {
99 LOG("window class matches (%s)\n", window->class_class);
105 if (match->instance != NULL) {
106 if (window->class_instance == NULL)
108 if (strcmp(match->instance->pattern, "__focused__") == 0 &&
109 strcmp(window->class_instance, focused->window->class_instance) == 0) {
110 LOG("window instance matches focused window\n");
111 } else if (regex_matches(match->instance, window->class_instance)) {
112 LOG("window instance matches (%s)\n", window->class_instance);
118 if (match->id != XCB_NONE) {
119 if (window->id == match->id) {
120 LOG("match made by window id (%d)\n", window->id);
122 LOG("window id does not match\n");
127 if (match->title != NULL) {
128 if (window->name == NULL)
131 const char *title = i3string_as_utf8(window->name);
132 if (strcmp(match->title->pattern, "__focused__") == 0 &&
133 strcmp(title, i3string_as_utf8(focused->window->name)) == 0) {
134 LOG("window title matches focused window\n");
135 } else if (regex_matches(match->title, title)) {
136 LOG("title matches (%s)\n", title);
142 if (match->window_role != NULL) {
143 if (window->role == NULL)
145 if (strcmp(match->window_role->pattern, "__focused__") == 0 &&
146 strcmp(window->role, focused->window->role) == 0) {
147 LOG("window role matches focused window\n");
148 } else if (regex_matches(match->window_role, window->role)) {
149 LOG("window_role matches (%s)\n", window->role);
155 if (match->window_type != UINT32_MAX) {
156 if (window->window_type == match->window_type) {
157 LOG("window_type matches (%i)\n", match->window_type);
164 if (match->urgent == U_LATEST) {
165 /* if the window isn't urgent, no sense in searching */
166 if (window->urgent.tv_sec == 0) {
169 /* if we find a window that is newer than this one, bail */
170 TAILQ_FOREACH(con, &all_cons, all_cons) {
171 if ((con->window != NULL) &&
172 _i3_timercmp(con->window->urgent, window->urgent, > )) {
176 LOG("urgent matches latest\n");
179 if (match->urgent == U_OLDEST) {
180 /* if the window isn't urgent, no sense in searching */
181 if (window->urgent.tv_sec == 0) {
184 /* if we find a window that is older than this one (and not 0), bail */
185 TAILQ_FOREACH(con, &all_cons, all_cons) {
186 if ((con->window != NULL) &&
187 (con->window->urgent.tv_sec != 0) &&
188 _i3_timercmp(con->window->urgent, window->urgent, < )) {
192 LOG("urgent matches oldest\n");
195 if (match->workspace != NULL) {
196 if ((con = con_by_window_id(window->id)) == NULL)
199 Con *ws = con_get_workspace(con);
203 if (strcmp(match->workspace->pattern, "__focused__") == 0 &&
204 strcmp(ws->name, con_get_workspace(focused)->name) == 0) {
205 LOG("workspace matches focused workspace\n");
206 } else if (regex_matches(match->workspace, ws->name)) {
207 LOG("workspace matches (%s)\n", ws->name);
213 if (match->dock != M_DONTCHECK) {
214 if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
215 (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
216 ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
217 match->dock == M_DOCK_ANY) ||
218 (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
219 LOG("dock status matches\n");
221 LOG("dock status does not match\n");
226 /* We don’t check the mark because this function is not even called when
227 * the mark would have matched - it is checked in cmdparse.y itself */
228 if (match->mark != NULL) {
229 LOG("mark does not match\n");
237 * Frees the given match. It must not be used afterwards!
240 void match_free(Match *match) {
242 regex_free(match->title);
243 regex_free(match->application);
244 regex_free(match->class);
245 regex_free(match->instance);
246 regex_free(match->mark);
247 regex_free(match->window_role);
248 regex_free(match->workspace);
252 * Interprets a ctype=cvalue pair and adds it to the given match specification.
255 void match_parse_property(Match *match, const char *ctype, const char *cvalue) {
256 assert(match != NULL);
257 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
259 if (strcmp(ctype, "class") == 0) {
260 regex_free(match->class);
261 match->class = regex_new(cvalue);
265 if (strcmp(ctype, "instance") == 0) {
266 regex_free(match->instance);
267 match->instance = regex_new(cvalue);
271 if (strcmp(ctype, "window_role") == 0) {
272 regex_free(match->window_role);
273 match->window_role = regex_new(cvalue);
277 if (strcmp(ctype, "con_id") == 0) {
278 if (strcmp(cvalue, "__focused__") == 0) {
279 match->con_id = focused;
284 long parsed = strtol(cvalue, &end, 0);
285 if (parsed == LONG_MIN ||
286 parsed == LONG_MAX ||
288 (end && *end != '\0')) {
289 ELOG("Could not parse con id \"%s\"\n", cvalue);
290 match->error = sstrdup("invalid con_id");
292 match->con_id = (Con *)parsed;
293 DLOG("id as int = %p\n", match->con_id);
298 if (strcmp(ctype, "id") == 0) {
300 long parsed = strtol(cvalue, &end, 10);
301 if (parsed == LONG_MIN ||
302 parsed == LONG_MAX ||
304 (end && *end != '\0')) {
305 ELOG("Could not parse window id \"%s\"\n", cvalue);
306 match->error = sstrdup("invalid id");
309 DLOG("window id as int = %d\n", match->id);
314 if (strcmp(ctype, "window_type") == 0) {
315 if (strcasecmp(cvalue, "normal") == 0) {
316 match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
317 } else if (strcasecmp(cvalue, "dialog") == 0) {
318 match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
319 } else if (strcasecmp(cvalue, "utility") == 0) {
320 match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
321 } else if (strcasecmp(cvalue, "toolbar") == 0) {
322 match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
323 } else if (strcasecmp(cvalue, "splash") == 0) {
324 match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
325 } else if (strcasecmp(cvalue, "menu") == 0) {
326 match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
327 } else if (strcasecmp(cvalue, "dropdown_menu") == 0) {
328 match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
329 } else if (strcasecmp(cvalue, "popup_menu") == 0) {
330 match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
331 } else if (strcasecmp(cvalue, "tooltip") == 0) {
332 match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
333 } else if (strcasecmp(cvalue, "notification") == 0) {
334 match->window_type = A__NET_WM_WINDOW_TYPE_NOTIFICATION;
336 ELOG("unknown window_type value \"%s\"\n", cvalue);
337 match->error = sstrdup("unknown window_type value");
343 if (strcmp(ctype, "con_mark") == 0) {
344 regex_free(match->mark);
345 match->mark = regex_new(cvalue);
349 if (strcmp(ctype, "title") == 0) {
350 regex_free(match->title);
351 match->title = regex_new(cvalue);
355 if (strcmp(ctype, "urgent") == 0) {
356 if (strcasecmp(cvalue, "latest") == 0 ||
357 strcasecmp(cvalue, "newest") == 0 ||
358 strcasecmp(cvalue, "recent") == 0 ||
359 strcasecmp(cvalue, "last") == 0) {
360 match->urgent = U_LATEST;
361 } else if (strcasecmp(cvalue, "oldest") == 0 ||
362 strcasecmp(cvalue, "first") == 0) {
363 match->urgent = U_OLDEST;
368 if (strcmp(ctype, "workspace") == 0) {
369 regex_free(match->workspace);
370 match->workspace = regex_new(cvalue);
374 ELOG("Unknown criterion: %s\n", ctype);