2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
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.
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))
21 * Initializes the Match data structure. This function is necessary because the
22 * members representing boolean values (like dock) need to be initialized with
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;
35 * Check if a match is empty. This is necessary while parsing commands to see
36 * whether the user specified a match at all.
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);
59 * Copies the data of a match from src to dest.
62 void match_copy(Match *dest, Match *src) {
63 memcpy(dest, src, sizeof(Match));
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) \
70 if (src->field != NULL) \
71 dest->field = regex_new(src->field->pattern); \
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);
84 * Check if a match data structure matches the given window.
87 bool match_matches_window(Match *match, i3Window *window) {
88 LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
90 if (match->class != NULL) {
91 if (window->class_class == NULL)
93 if (strcmp(match->class->pattern, "__focused__") == 0 &&
94 strcmp(window->class_class, focused->window->class_class) == 0) {
95 LOG("window class matches focused window\n");
96 } else if (regex_matches(match->class, window->class_class)) {
97 LOG("window class matches (%s)\n", window->class_class);
103 if (match->instance != NULL) {
104 if (window->class_instance == NULL)
106 if (strcmp(match->instance->pattern, "__focused__") == 0 &&
107 strcmp(window->class_instance, focused->window->class_instance) == 0) {
108 LOG("window instance matches focused window\n");
109 } else if (regex_matches(match->instance, window->class_instance)) {
110 LOG("window instance matches (%s)\n", window->class_instance);
116 if (match->id != XCB_NONE) {
117 if (window->id == match->id) {
118 LOG("match made by window id (%d)\n", window->id);
120 LOG("window id does not match\n");
125 if (match->title != NULL) {
126 if (window->name == NULL)
129 const char *title = i3string_as_utf8(window->name);
130 if (strcmp(match->title->pattern, "__focused__") == 0 &&
131 strcmp(title, i3string_as_utf8(focused->window->name)) == 0) {
132 LOG("window title matches focused window\n");
133 } else if (regex_matches(match->title, title)) {
134 LOG("title matches (%s)\n", title);
140 if (match->window_role != NULL) {
141 if (window->role == NULL)
143 if (strcmp(match->window_role->pattern, "__focused__") == 0 &&
144 strcmp(window->role, focused->window->role) == 0) {
145 LOG("window role matches focused window\n");
146 } else if (regex_matches(match->window_role, window->role)) {
147 LOG("window_role matches (%s)\n", window->role);
153 if (match->window_type != UINT32_MAX) {
154 if (window->window_type == match->window_type) {
155 LOG("window_type matches (%i)\n", match->window_type);
162 if (match->urgent == U_LATEST) {
163 /* if the window isn't urgent, no sense in searching */
164 if (window->urgent.tv_sec == 0) {
167 /* if we find a window that is newer than this one, bail */
168 TAILQ_FOREACH(con, &all_cons, all_cons) {
169 if ((con->window != NULL) &&
170 _i3_timercmp(con->window->urgent, window->urgent, >)) {
174 LOG("urgent matches latest\n");
177 if (match->urgent == U_OLDEST) {
178 /* if the window isn't urgent, no sense in searching */
179 if (window->urgent.tv_sec == 0) {
182 /* if we find a window that is older than this one (and not 0), bail */
183 TAILQ_FOREACH(con, &all_cons, all_cons) {
184 if ((con->window != NULL) &&
185 (con->window->urgent.tv_sec != 0) &&
186 _i3_timercmp(con->window->urgent, window->urgent, <)) {
190 LOG("urgent matches oldest\n");
193 if (match->workspace != NULL) {
194 if ((con = con_by_window_id(window->id)) == NULL)
197 Con *ws = con_get_workspace(con);
201 if (strcmp(match->workspace->pattern, "__focused__") == 0 &&
202 strcmp(ws->name, con_get_workspace(focused)->name) == 0) {
203 LOG("workspace matches focused workspace\n");
204 } else if (regex_matches(match->workspace, ws->name)) {
205 LOG("workspace matches (%s)\n", ws->name);
211 if (match->dock != M_DONTCHECK) {
212 if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
213 (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
214 ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
215 match->dock == M_DOCK_ANY) ||
216 (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
217 LOG("dock status matches\n");
219 LOG("dock status does not match\n");
224 if (match->mark != NULL) {
225 if ((con = con_by_window_id(window->id)) == NULL)
228 bool matched = false;
230 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
231 if (regex_matches(match->mark, mark->name)) {
238 LOG("mark matches\n");
240 LOG("mark does not match\n");
245 if (match->window_mode != WM_ANY) {
246 if ((con = con_by_window_id(window->id)) == NULL)
249 const bool floating = (con_inside_floating(con) != NULL);
251 if ((match->window_mode == WM_TILING && floating) ||
252 (match->window_mode == WM_FLOATING && !floating)) {
253 LOG("window_mode does not match\n");
257 LOG("window_mode matches\n");
264 * Frees the given match. It must not be used afterwards!
267 void match_free(Match *match) {
269 regex_free(match->title);
270 regex_free(match->application);
271 regex_free(match->class);
272 regex_free(match->instance);
273 regex_free(match->mark);
274 regex_free(match->window_role);
275 regex_free(match->workspace);
279 * Interprets a ctype=cvalue pair and adds it to the given match specification.
282 void match_parse_property(Match *match, const char *ctype, const char *cvalue) {
283 assert(match != NULL);
284 DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
286 if (strcmp(ctype, "class") == 0) {
287 regex_free(match->class);
288 match->class = regex_new(cvalue);
292 if (strcmp(ctype, "instance") == 0) {
293 regex_free(match->instance);
294 match->instance = regex_new(cvalue);
298 if (strcmp(ctype, "window_role") == 0) {
299 regex_free(match->window_role);
300 match->window_role = regex_new(cvalue);
304 if (strcmp(ctype, "con_id") == 0) {
305 if (strcmp(cvalue, "__focused__") == 0) {
306 match->con_id = focused;
311 if (!parse_long(cvalue, &parsed, 0)) {
312 ELOG("Could not parse con id \"%s\"\n", cvalue);
313 match->error = sstrdup("invalid con_id");
315 match->con_id = (Con *)parsed;
316 DLOG("id as int = %p\n", match->con_id);
321 if (strcmp(ctype, "id") == 0) {
323 if (!parse_long(cvalue, &parsed, 0)) {
324 ELOG("Could not parse window id \"%s\"\n", cvalue);
325 match->error = sstrdup("invalid id");
328 DLOG("window id as int = %d\n", match->id);
333 if (strcmp(ctype, "window_type") == 0) {
334 if (strcasecmp(cvalue, "normal") == 0) {
335 match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
336 } else if (strcasecmp(cvalue, "dialog") == 0) {
337 match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
338 } else if (strcasecmp(cvalue, "utility") == 0) {
339 match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
340 } else if (strcasecmp(cvalue, "toolbar") == 0) {
341 match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
342 } else if (strcasecmp(cvalue, "splash") == 0) {
343 match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
344 } else if (strcasecmp(cvalue, "menu") == 0) {
345 match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
346 } else if (strcasecmp(cvalue, "dropdown_menu") == 0) {
347 match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
348 } else if (strcasecmp(cvalue, "popup_menu") == 0) {
349 match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
350 } else if (strcasecmp(cvalue, "tooltip") == 0) {
351 match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
352 } else if (strcasecmp(cvalue, "notification") == 0) {
353 match->window_type = A__NET_WM_WINDOW_TYPE_NOTIFICATION;
355 ELOG("unknown window_type value \"%s\"\n", cvalue);
356 match->error = sstrdup("unknown window_type value");
362 if (strcmp(ctype, "con_mark") == 0) {
363 regex_free(match->mark);
364 match->mark = regex_new(cvalue);
368 if (strcmp(ctype, "title") == 0) {
369 regex_free(match->title);
370 match->title = regex_new(cvalue);
374 if (strcmp(ctype, "urgent") == 0) {
375 if (strcasecmp(cvalue, "latest") == 0 ||
376 strcasecmp(cvalue, "newest") == 0 ||
377 strcasecmp(cvalue, "recent") == 0 ||
378 strcasecmp(cvalue, "last") == 0) {
379 match->urgent = U_LATEST;
380 } else if (strcasecmp(cvalue, "oldest") == 0 ||
381 strcasecmp(cvalue, "first") == 0) {
382 match->urgent = U_OLDEST;
387 if (strcmp(ctype, "workspace") == 0) {
388 regex_free(match->workspace);
389 match->workspace = regex_new(cvalue);
393 if (strcmp(ctype, "tiling") == 0) {
394 match->window_mode = WM_TILING;
398 if (strcmp(ctype, "floating") == 0) {
399 match->window_mode = WM_FLOATING;
403 ELOG("Unknown criterion: %s\n", ctype);