i3
match.c
Go to the documentation of this file.
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 = -1;
31  match->urgent = U_DONTCHECK;
32 }
33 
34 /*
35  * Check if a match is empty. This is necessary while parsing commands to see
36  * whether the user specified a match at all.
37  *
38  */
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->urgent == U_DONTCHECK &&
50  match->id == XCB_NONE &&
51  match->con_id == NULL &&
52  match->dock == -1 &&
53  match->floating == M_ANY);
54 }
55 
56 /*
57  * Copies the data of a match from src to dest.
58  *
59  */
60 void match_copy(Match *dest, Match *src) {
61  memcpy(dest, src, sizeof(Match));
62 
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) \
67  do { \
68  if (src->field != NULL) \
69  dest->field = regex_new(src->field->pattern); \
70  } while (0)
71 
72  DUPLICATE_REGEX(title);
73  DUPLICATE_REGEX(mark);
74  DUPLICATE_REGEX(application);
75  DUPLICATE_REGEX(class);
76  DUPLICATE_REGEX(instance);
77  DUPLICATE_REGEX(window_role);
78 }
79 
80 /*
81  * Check if a match data structure matches the given window.
82  *
83  */
84 bool match_matches_window(Match *match, i3Window *window) {
85  LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
86 
87  if (match->class != NULL) {
88  if (window->class_class != NULL &&
89  regex_matches(match->class, window->class_class)) {
90  LOG("window class matches (%s)\n", window->class_class);
91  } else {
92  return false;
93  }
94  }
95 
96  if (match->instance != NULL) {
97  if (window->class_instance != NULL &&
98  regex_matches(match->instance, window->class_instance)) {
99  LOG("window instance matches (%s)\n", window->class_instance);
100  } else {
101  return false;
102  }
103  }
104 
105  if (match->id != XCB_NONE) {
106  if (window->id == match->id) {
107  LOG("match made by window id (%d)\n", window->id);
108  } else {
109  LOG("window id does not match\n");
110  return false;
111  }
112  }
113 
114  if (match->title != NULL) {
115  if (window->name != NULL &&
116  regex_matches(match->title, i3string_as_utf8(window->name))) {
117  LOG("title matches (%s)\n", i3string_as_utf8(window->name));
118  } else {
119  return false;
120  }
121  }
122 
123  if (match->window_role != NULL) {
124  if (window->role != NULL &&
125  regex_matches(match->window_role, window->role)) {
126  LOG("window_role matches (%s)\n", window->role);
127  } else {
128  return false;
129  }
130  }
131 
132  Con *con = NULL;
133  if (match->urgent == U_LATEST) {
134  /* if the window isn't urgent, no sense in searching */
135  if (window->urgent.tv_sec == 0) {
136  return false;
137  }
138  /* if we find a window that is newer than this one, bail */
139  TAILQ_FOREACH (con, &all_cons, all_cons) {
140  if ((con->window != NULL) &&
141  _i3_timercmp(con->window->urgent, window->urgent, > )) {
142  return false;
143  }
144  }
145  LOG("urgent matches latest\n");
146  }
147 
148  if (match->urgent == U_OLDEST) {
149  /* if the window isn't urgent, no sense in searching */
150  if (window->urgent.tv_sec == 0) {
151  return false;
152  }
153  /* if we find a window that is older than this one (and not 0), bail */
154  TAILQ_FOREACH (con, &all_cons, all_cons) {
155  if ((con->window != NULL) &&
156  (con->window->urgent.tv_sec != 0) &&
157  _i3_timercmp(con->window->urgent, window->urgent, < )) {
158  return false;
159  }
160  }
161  LOG("urgent matches oldest\n");
162  }
163 
164  if (match->dock != -1) {
165  if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
166  (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
167  ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
168  match->dock == M_DOCK_ANY) ||
169  (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
170  LOG("dock status matches\n");
171  } else {
172  LOG("dock status does not match\n");
173  return false;
174  }
175  }
176 
177  /* We don’t check the mark because this function is not even called when
178  * the mark would have matched - it is checked in cmdparse.y itself */
179  if (match->mark != NULL) {
180  LOG("mark does not match\n");
181  return false;
182  }
183 
184  return true;
185 }
186 
187 /*
188  * Frees the given match. It must not be used afterwards!
189  *
190  */
191 void match_free(Match *match) {
192  /* First step: free the regex fields / patterns */
193  regex_free(match->title);
194  regex_free(match->application);
195  regex_free(match->class);
196  regex_free(match->instance);
197  regex_free(match->mark);
198  regex_free(match->window_role);
199 
200  /* Second step: free the regex helper struct itself */
201  FREE(match->title);
202  FREE(match->application);
203  FREE(match->class);
204  FREE(match->instance);
205  FREE(match->mark);
206  FREE(match->window_role);
207 }
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
#define DUPLICATE_REGEX(field)
#define LOG(fmt,...)
Definition: libi3.h:76
struct all_cons_head all_cons
Definition: tree.c:17
struct regex * window_role
Definition: data.h:396
struct regex * mark
Definition: data.h:395
struct Window * window
Definition: data.h:547
char * class_instance
Definition: data.h:346
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:332
void match_copy(Match *dest, Match *src)
Copies the data of a match from src to dest.
Definition: match.c:60
struct regex * instance
Definition: data.h:394
struct regex * application
Definition: data.h:392
xcb_window_t id
Definition: data.h:409
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition: match.c:84
void regex_free(struct regex *regex)
Frees the given regular expression.
Definition: regex.c:61
struct timeval urgent
When this window was marked urgent.
Definition: data.h:373
Con * con_id
Definition: data.h:411
enum Window::@11 dock
Whether the window says it is a dock window.
enum Match::@13 dock
A "match" is a data structure which acts like a mask or expression to match certain windows or not...
Definition: data.h:390
char * role
The WM_WINDOW_ROLE of this window (for example, the pidgin buddy window sets "buddy list")...
Definition: data.h:354
enum Match::@12 urgent
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:479
struct regex * title
Definition: data.h:391
#define _i3_timercmp(a, b, CMP)
Definition: match.c:19
enum Match::@14 floating
bool regex_matches(struct regex *regex, const char *input)
Checks if the given regular expression matches the given input and returns true if it does...
Definition: regex.c:75
bool match_is_empty(Match *match)
Check if a match is empty.
Definition: match.c:39
i3String * name
The name of the window.
Definition: data.h:349
xcb_window_t id
Definition: data.h:333
char * class_class
Definition: data.h:345
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:334
#define FREE(pointer)
Definition: util.h:46
void match_init(Match *match)
Definition: match.c:28
struct regex * class
Definition: data.h:393
void match_free(Match *match)
Frees the given match.
Definition: match.c:191