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 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->workspace == NULL &&
52  match->urgent == U_DONTCHECK &&
53  match->id == XCB_NONE &&
54  match->window_type == UINT32_MAX &&
55  match->con_id == NULL &&
56  match->dock == -1 &&
57  match->floating == M_ANY);
58 }
59 
60 /*
61  * Copies the data of a match from src to dest.
62  *
63  */
64 void match_copy(Match *dest, Match *src) {
65  memcpy(dest, src, sizeof(Match));
66 
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) \
71  do { \
72  if (src->field != NULL) \
73  dest->field = regex_new(src->field->pattern); \
74  } while (0)
75 
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);
83 }
84 
85 /*
86  * Check if a match data structure matches the given window.
87  *
88  */
89 bool match_matches_window(Match *match, i3Window *window) {
90  LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
91 
92  if (match->class != NULL) {
93  if (window->class_class == NULL)
94  return false;
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);
100  } else {
101  return false;
102  }
103  }
104 
105  if (match->instance != NULL) {
106  if (window->class_instance == NULL)
107  return false;
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);
113  } else {
114  return false;
115  }
116  }
117 
118  if (match->id != XCB_NONE) {
119  if (window->id == match->id) {
120  LOG("match made by window id (%d)\n", window->id);
121  } else {
122  LOG("window id does not match\n");
123  return false;
124  }
125  }
126 
127  if (match->title != NULL) {
128  if (window->name == NULL)
129  return false;
130 
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);
137  } else {
138  return false;
139  }
140  }
141 
142  if (match->window_role != NULL) {
143  if (window->role == NULL)
144  return false;
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);
150  } else {
151  return false;
152  }
153  }
154 
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);
158  } else {
159  return false;
160  }
161  }
162 
163  Con *con = NULL;
164  if (match->urgent == U_LATEST) {
165  /* if the window isn't urgent, no sense in searching */
166  if (window->urgent.tv_sec == 0) {
167  return false;
168  }
169  /* if we find a window that is newer than this one, bail */
171  if ((con->window != NULL) &&
172  _i3_timercmp(con->window->urgent, window->urgent, > )) {
173  return false;
174  }
175  }
176  LOG("urgent matches latest\n");
177  }
178 
179  if (match->urgent == U_OLDEST) {
180  /* if the window isn't urgent, no sense in searching */
181  if (window->urgent.tv_sec == 0) {
182  return false;
183  }
184  /* if we find a window that is older than this one (and not 0), bail */
186  if ((con->window != NULL) &&
187  (con->window->urgent.tv_sec != 0) &&
188  _i3_timercmp(con->window->urgent, window->urgent, < )) {
189  return false;
190  }
191  }
192  LOG("urgent matches oldest\n");
193  }
194 
195  if (match->workspace != NULL) {
196  if ((con = con_by_window_id(window->id)) == NULL)
197  return false;
198 
199  Con *ws = con_get_workspace(con);
200  if (ws == NULL)
201  return false;
202 
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);
208  } else {
209  return false;
210  }
211  }
212 
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");
220  } else {
221  LOG("dock status does not match\n");
222  return false;
223  }
224  }
225 
226  if (match->mark != NULL) {
227  if ((con = con_by_window_id(window->id)) == NULL)
228  return false;
229 
230  bool matched = false;
231  mark_t *mark;
232  TAILQ_FOREACH(mark, &(con->marks_head), marks) {
233  if (regex_matches(match->mark, mark->name)) {
234  matched = true;
235  break;
236  }
237  }
238 
239  if (matched) {
240  LOG("mark matches\n");
241  } else {
242  LOG("mark does not match\n");
243  return false;
244  }
245  }
246 
247  return true;
248 }
249 
250 /*
251  * Frees the given match. It must not be used afterwards!
252  *
253  */
254 void match_free(Match *match) {
255  FREE(match->error);
256  regex_free(match->title);
257  regex_free(match->application);
258  regex_free(match->class);
259  regex_free(match->instance);
260  regex_free(match->mark);
261  regex_free(match->window_role);
262  regex_free(match->workspace);
263 }
264 
265 /*
266  * Interprets a ctype=cvalue pair and adds it to the given match specification.
267  *
268  */
269 void match_parse_property(Match *match, const char *ctype, const char *cvalue) {
270  assert(match != NULL);
271  DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
272 
273  if (strcmp(ctype, "class") == 0) {
274  regex_free(match->class);
275  match->class = regex_new(cvalue);
276  return;
277  }
278 
279  if (strcmp(ctype, "instance") == 0) {
280  regex_free(match->instance);
281  match->instance = regex_new(cvalue);
282  return;
283  }
284 
285  if (strcmp(ctype, "window_role") == 0) {
286  regex_free(match->window_role);
287  match->window_role = regex_new(cvalue);
288  return;
289  }
290 
291  if (strcmp(ctype, "con_id") == 0) {
292  if (strcmp(cvalue, "__focused__") == 0) {
293  match->con_id = focused;
294  return;
295  }
296 
297  char *end;
298  long parsed = strtol(cvalue, &end, 0);
299  if (parsed == LONG_MIN ||
300  parsed == LONG_MAX ||
301  parsed < 0 ||
302  (end && *end != '\0')) {
303  ELOG("Could not parse con id \"%s\"\n", cvalue);
304  match->error = sstrdup("invalid con_id");
305  } else {
306  match->con_id = (Con *)parsed;
307  DLOG("id as int = %p\n", match->con_id);
308  }
309  return;
310  }
311 
312  if (strcmp(ctype, "id") == 0) {
313  char *end;
314  long parsed = strtol(cvalue, &end, 0);
315  if (parsed == LONG_MIN ||
316  parsed == LONG_MAX ||
317  parsed < 0 ||
318  (end && *end != '\0')) {
319  ELOG("Could not parse window id \"%s\"\n", cvalue);
320  match->error = sstrdup("invalid id");
321  } else {
322  match->id = parsed;
323  DLOG("window id as int = %d\n", match->id);
324  }
325  return;
326  }
327 
328  if (strcmp(ctype, "window_type") == 0) {
329  if (strcasecmp(cvalue, "normal") == 0) {
330  match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
331  } else if (strcasecmp(cvalue, "dialog") == 0) {
332  match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
333  } else if (strcasecmp(cvalue, "utility") == 0) {
334  match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
335  } else if (strcasecmp(cvalue, "toolbar") == 0) {
336  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
337  } else if (strcasecmp(cvalue, "splash") == 0) {
338  match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
339  } else if (strcasecmp(cvalue, "menu") == 0) {
340  match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
341  } else if (strcasecmp(cvalue, "dropdown_menu") == 0) {
342  match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
343  } else if (strcasecmp(cvalue, "popup_menu") == 0) {
344  match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
345  } else if (strcasecmp(cvalue, "tooltip") == 0) {
346  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
347  } else if (strcasecmp(cvalue, "notification") == 0) {
348  match->window_type = A__NET_WM_WINDOW_TYPE_NOTIFICATION;
349  } else {
350  ELOG("unknown window_type value \"%s\"\n", cvalue);
351  match->error = sstrdup("unknown window_type value");
352  }
353 
354  return;
355  }
356 
357  if (strcmp(ctype, "con_mark") == 0) {
358  regex_free(match->mark);
359  match->mark = regex_new(cvalue);
360  return;
361  }
362 
363  if (strcmp(ctype, "title") == 0) {
364  regex_free(match->title);
365  match->title = regex_new(cvalue);
366  return;
367  }
368 
369  if (strcmp(ctype, "urgent") == 0) {
370  if (strcasecmp(cvalue, "latest") == 0 ||
371  strcasecmp(cvalue, "newest") == 0 ||
372  strcasecmp(cvalue, "recent") == 0 ||
373  strcasecmp(cvalue, "last") == 0) {
374  match->urgent = U_LATEST;
375  } else if (strcasecmp(cvalue, "oldest") == 0 ||
376  strcasecmp(cvalue, "first") == 0) {
377  match->urgent = U_OLDEST;
378  }
379  return;
380  }
381 
382  if (strcmp(ctype, "workspace") == 0) {
383  regex_free(match->workspace);
384  match->workspace = regex_new(cvalue);
385  return;
386  }
387 
388  ELOG("Unknown criterion: %s\n", ctype);
389 }
char * class_instance
Definition: data.h:375
xcb_window_t id
Definition: data.h:463
enum Match::@15 dock
char * name
Definition: data.h:535
bool match_is_empty(Match *match)
Check if a match is empty.
Definition: match.c:41
Definition: data.h:534
struct Window * window
Definition: data.h:611
char * name
Definition: data.h:590
i3String * name
The name of the window.
Definition: data.h:378
enum Match::@16 floating
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition: match.c:89
Con * con_by_window_id(xcb_window_t window)
Returns the container with the given client window ID or NULL if no such container exists...
Definition: con.c:531
void match_parse_property(Match *match, const char *ctype, const char *cvalue)
Interprets a ctype=cvalue pair and adds it to the given match specification.
Definition: match.c:269
void match_init(Match *match)
Definition: match.c:28
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
struct timeval urgent
When this window was marked urgent.
Definition: data.h:410
void match_free(Match *match)
Frees the given match.
Definition: match.c:254
char * error
Definition: data.h:441
struct regex * title
Definition: data.h:443
#define DUPLICATE_REGEX(field)
#define ELOG(fmt,...)
Definition: libi3.h:93
struct regex * class
Definition: data.h:445
#define LOG(fmt,...)
Definition: libi3.h:88
struct regex * workspace
Definition: data.h:449
#define _i3_timercmp(a, b, CMP)
Definition: match.c:19
void regex_free(struct regex *regex)
Frees the given regular expression.
Definition: regex.c:61
xcb_window_t id
Definition: data.h:362
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:373
char * class_class
Definition: data.h:374
A &#39;Window&#39; is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:361
void match_copy(Match *dest, Match *src)
Copies the data of a match from src to dest.
Definition: match.c:64
struct regex * window_role
Definition: data.h:448
#define FREE(pointer)
Definition: util.h:48
#define DLOG(fmt,...)
Definition: libi3.h:98
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:544
xcb_atom_t window_type
Definition: data.h:450
char * pattern
Definition: data.h:235
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
struct regex * mark
Definition: data.h:447
struct regex * instance
Definition: data.h:446
struct regex * application
Definition: data.h:444
struct regex * regex_new(const char *pattern)
Creates a new &#39;regex&#39; struct containing the given pattern and a PCRE compiled regular expression...
Definition: regex.c:24
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:76
A &quot;match&quot; is a data structure which acts like a mask or expression to match certain windows or not...
Definition: data.h:439
struct all_cons_head all_cons
Definition: tree.c:17
enum Window::@13 dock
Whether the window says it is a dock window.
enum Match::@14 urgent
Con * focused
Definition: tree.c:15
Con * con_id
Definition: data.h:467
char * role
The WM_WINDOW_ROLE of this window (for example, the pidgin buddy window sets &quot;buddy list&quot;)...
Definition: data.h:383
xcb_atom_t window_type
The _NET_WM_WINDOW_TYPE for this window.
Definition: data.h:399