2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 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) ? \
19 ((a).tv_usec CMP (b).tv_usec) : \
20 ((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));
31 match->urgent = U_DONTCHECK;
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->role == NULL &&
49 match->urgent == U_DONTCHECK &&
50 match->id == XCB_NONE &&
51 match->con_id == NULL &&
53 match->floating == M_ANY);
57 * Copies the data of a match from src to dest.
60 void match_copy(Match *dest, Match *src) {
61 memcpy(dest, src, sizeof(Match));
63 /* The DUPLICATE_REGEX macro creates a new regular expression from the
64 * ->pattern of the old one. It therefore does use a little more memory then
65 * with a refcounting system, but it’s easier this way. */
66 #define DUPLICATE_REGEX(field) do { \
67 if (src->field != NULL) \
68 dest->field = regex_new(src->field->pattern); \
71 DUPLICATE_REGEX(title);
72 DUPLICATE_REGEX(mark);
73 DUPLICATE_REGEX(application);
74 DUPLICATE_REGEX(class);
75 DUPLICATE_REGEX(instance);
76 DUPLICATE_REGEX(role);
80 * Check if a match data structure matches the given window.
83 bool match_matches_window(Match *match, i3Window *window) {
84 LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
86 if (match->class != NULL) {
87 if (window->class_class != NULL &&
88 regex_matches(match->class, window->class_class)) {
89 LOG("window class matches (%s)\n", window->class_class);
95 if (match->instance != NULL) {
96 if (window->class_instance != NULL &&
97 regex_matches(match->instance, window->class_instance)) {
98 LOG("window instance matches (%s)\n", window->class_instance);
104 if (match->id != XCB_NONE) {
105 if (window->id == match->id) {
106 LOG("match made by window id (%d)\n", window->id);
108 LOG("window id does not match\n");
113 if (match->title != NULL) {
114 if (window->name_json != NULL &&
115 regex_matches(match->title, window->name_json)) {
116 LOG("title matches (%s)\n", window->name_json);
122 if (match->role != NULL) {
123 if (window->role != NULL &&
124 regex_matches(match->role, window->role)) {
125 LOG("window_role matches (%s)\n", window->role);
132 if (match->urgent == U_LATEST) {
133 /* if the window isn't urgent, no sense in searching */
134 if (window->urgent.tv_sec == 0) {
137 /* if we find a window that is newer than this one, bail */
138 TAILQ_FOREACH(con, &all_cons, all_cons) {
139 if ((con->window != NULL) &&
140 _i3_timercmp(con->window->urgent, window->urgent, >)) {
144 LOG("urgent matches latest\n");
147 if (match->urgent == U_OLDEST) {
148 /* if the window isn't urgent, no sense in searching */
149 if (window->urgent.tv_sec == 0) {
152 /* if we find a window that is older than this one (and not 0), bail */
153 TAILQ_FOREACH(con, &all_cons, all_cons) {
154 if ((con->window != NULL) &&
155 (con->window->urgent.tv_sec != 0) &&
156 _i3_timercmp(con->window->urgent, window->urgent, <)) {
160 LOG("urgent matches oldest\n");
163 if (match->dock != -1) {
164 if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
165 (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
166 ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
167 match->dock == M_DOCK_ANY) ||
168 (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
169 LOG("dock status matches\n");
171 LOG("dock status does not match\n");
176 /* We don’t check the mark because this function is not even called when
177 * the mark would have matched - it is checked in cmdparse.y itself */
178 if (match->mark != NULL) {
179 LOG("mark does not match\n");
187 * Frees the given match. It must not be used afterwards!
190 void match_free(Match *match) {
191 /* First step: free the regex fields / patterns */
192 regex_free(match->title);
193 regex_free(match->application);
194 regex_free(match->class);
195 regex_free(match->instance);
196 regex_free(match->mark);
197 regex_free(match->role);
199 /* Second step: free the regex helper struct itself */
201 FREE(match->application);
203 FREE(match->instance);