i3
match.c
Go to the documentation of this file.
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6  *
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.
12  *
13  */
14 #include "all.h"
15 
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))
19 
20 /*
21  * Initializes the Match data structure. This function is necessary because the
22  * members representing boolean values (like dock) need to be initialized with
23  * -1 instead of 0.
24  *
25  */
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;
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->workspace == NULL &&
50  match->machine == NULL &&
51  match->urgent == U_DONTCHECK &&
52  match->id == XCB_NONE &&
53  match->window_type == UINT32_MAX &&
54  match->con_id == NULL &&
55  match->dock == M_NODOCK &&
56  match->window_mode == WM_ANY &&
57  match->match_all_windows == false);
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 #define GET_FIELD_str(field) (field)
93 #define GET_FIELD_i3string(field) (i3string_as_utf8(field))
94 #define CHECK_WINDOW_FIELD(match_field, window_field, type) \
95  do { \
96  if (match->match_field != NULL) { \
97  const char *window_field_str = window->window_field == NULL \
98  ? "" \
99  : GET_FIELD_##type(window->window_field); \
100  if (strcmp(match->match_field->pattern, "__focused__") == 0 && \
101  focused && focused->window && focused->window->window_field && \
102  strcmp(window_field_str, GET_FIELD_##type(focused->window->window_field)) == 0) { \
103  LOG("window " #match_field " matches focused window\n"); \
104  } else if (regex_matches(match->match_field, window_field_str)) { \
105  LOG("window " #match_field " matches (%s)\n", window_field_str); \
106  } else { \
107  return false; \
108  } \
109  } \
110  } while (0)
111 
112  CHECK_WINDOW_FIELD(class, class_class, str);
113  CHECK_WINDOW_FIELD(instance, class_instance, str);
114 
115  if (match->id != XCB_NONE) {
116  if (window->id == match->id) {
117  LOG("match made by window id (%d)\n", window->id);
118  } else {
119  LOG("window id does not match\n");
120  return false;
121  }
122  }
123 
124  CHECK_WINDOW_FIELD(title, name, i3string);
125  CHECK_WINDOW_FIELD(window_role, role, str);
126 
127  if (match->window_type != UINT32_MAX) {
128  if (window->window_type == match->window_type) {
129  LOG("window_type matches (%i)\n", match->window_type);
130  } else {
131  return false;
132  }
133  }
134 
135  CHECK_WINDOW_FIELD(machine, machine, str);
136 
137  Con *con = NULL;
138  if (match->urgent == U_LATEST) {
139  /* if the window isn't urgent, no sense in searching */
140  if (window->urgent.tv_sec == 0) {
141  return false;
142  }
143  /* if we find a window that is newer than this one, bail */
144  TAILQ_FOREACH (con, &all_cons, all_cons) {
145  if ((con->window != NULL) &&
146  _i3_timercmp(con->window->urgent, window->urgent, >)) {
147  return false;
148  }
149  }
150  LOG("urgent matches latest\n");
151  }
152 
153  if (match->urgent == U_OLDEST) {
154  /* if the window isn't urgent, no sense in searching */
155  if (window->urgent.tv_sec == 0) {
156  return false;
157  }
158  /* if we find a window that is older than this one (and not 0), bail */
159  TAILQ_FOREACH (con, &all_cons, all_cons) {
160  if ((con->window != NULL) &&
161  (con->window->urgent.tv_sec != 0) &&
162  _i3_timercmp(con->window->urgent, window->urgent, <)) {
163  return false;
164  }
165  }
166  LOG("urgent matches oldest\n");
167  }
168 
169  if (match->workspace != NULL) {
170  if ((con = con_by_window_id(window->id)) == NULL)
171  return false;
172 
173  Con *ws = con_get_workspace(con);
174  if (ws == NULL)
175  return false;
176 
177  if (strcmp(match->workspace->pattern, "__focused__") == 0 &&
178  strcmp(ws->name, con_get_workspace(focused)->name) == 0) {
179  LOG("workspace matches focused workspace\n");
180  } else if (regex_matches(match->workspace, ws->name)) {
181  LOG("workspace matches (%s)\n", ws->name);
182  } else {
183  return false;
184  }
185  }
186 
187  if (match->dock != M_DONTCHECK) {
188  if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
189  (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
190  ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
191  match->dock == M_DOCK_ANY) ||
192  (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
193  LOG("dock status matches\n");
194  } else {
195  LOG("dock status does not match\n");
196  return false;
197  }
198  }
199 
200  if (match->mark != NULL) {
201  if ((con = con_by_window_id(window->id)) == NULL)
202  return false;
203 
204  bool matched = false;
205  mark_t *mark;
206  TAILQ_FOREACH (mark, &(con->marks_head), marks) {
207  if (regex_matches(match->mark, mark->name)) {
208  matched = true;
209  break;
210  }
211  }
212 
213  if (matched) {
214  LOG("mark matches\n");
215  } else {
216  LOG("mark does not match\n");
217  return false;
218  }
219  }
220 
221  if (match->window_mode != WM_ANY) {
222  if ((con = con_by_window_id(window->id)) == NULL) {
223  return false;
224  }
225 
226  switch (match->window_mode) {
227  case WM_TILING_AUTO:
228  if (con->floating != FLOATING_AUTO_OFF) {
229  return false;
230  }
231  break;
232  case WM_TILING_USER:
233  if (con->floating != FLOATING_USER_OFF) {
234  return false;
235  }
236  break;
237  case WM_TILING:
238  if (con_inside_floating(con) != NULL) {
239  return false;
240  }
241  break;
242  case WM_FLOATING_AUTO:
243  if (con->floating != FLOATING_AUTO_ON) {
244  return false;
245  }
246  break;
247  case WM_FLOATING_USER:
248  if (con->floating != FLOATING_USER_ON) {
249  return false;
250  }
251  break;
252  case WM_FLOATING:
253  if (con_inside_floating(con) == NULL) {
254  return false;
255  }
256  break;
257  case WM_ANY:
258  assert(false);
259  }
260 
261  LOG("window_mode matches\n");
262  }
263 
264  /* NOTE: See the comment regarding 'all' in match_parse_property()
265  * for an explanation of why match_all_windows isn't explicitly
266  * checked. */
267 
268  return true;
269 }
270 
271 /*
272  * Frees the given match. It must not be used afterwards!
273  *
274  */
275 void match_free(Match *match) {
276  FREE(match->error);
277  regex_free(match->title);
278  regex_free(match->application);
279  regex_free(match->class);
280  regex_free(match->instance);
281  regex_free(match->mark);
282  regex_free(match->window_role);
283  regex_free(match->workspace);
284  regex_free(match->machine);
285 }
286 
287 /*
288  * Interprets a ctype=cvalue pair and adds it to the given match specification.
289  *
290  */
291 void match_parse_property(Match *match, const char *ctype, const char *cvalue) {
292  assert(match != NULL);
293  DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
294 
295  if (strcmp(ctype, "class") == 0) {
296  regex_free(match->class);
297  match->class = regex_new(cvalue);
298  return;
299  }
300 
301  if (strcmp(ctype, "instance") == 0) {
302  regex_free(match->instance);
303  match->instance = regex_new(cvalue);
304  return;
305  }
306 
307  if (strcmp(ctype, "window_role") == 0) {
308  regex_free(match->window_role);
309  match->window_role = regex_new(cvalue);
310  return;
311  }
312 
313  if (strcmp(ctype, "con_id") == 0) {
314  if (strcmp(cvalue, "__focused__") == 0) {
315  match->con_id = focused;
316  return;
317  }
318 
319  long parsed;
320  if (!parse_long(cvalue, &parsed, 0)) {
321  ELOG("Could not parse con id \"%s\"\n", cvalue);
322  match->error = sstrdup("invalid con_id");
323  } else {
324  match->con_id = (Con *)parsed;
325  DLOG("id as int = %p\n", match->con_id);
326  }
327  return;
328  }
329 
330  if (strcmp(ctype, "id") == 0) {
331  long parsed;
332  if (!parse_long(cvalue, &parsed, 0)) {
333  ELOG("Could not parse window id \"%s\"\n", cvalue);
334  match->error = sstrdup("invalid id");
335  } else {
336  match->id = parsed;
337  DLOG("window id as int = %d\n", match->id);
338  }
339  return;
340  }
341 
342  if (strcmp(ctype, "window_type") == 0) {
343  if (strcasecmp(cvalue, "normal") == 0) {
344  match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
345  } else if (strcasecmp(cvalue, "dialog") == 0) {
346  match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
347  } else if (strcasecmp(cvalue, "utility") == 0) {
348  match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
349  } else if (strcasecmp(cvalue, "toolbar") == 0) {
350  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
351  } else if (strcasecmp(cvalue, "splash") == 0) {
352  match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
353  } else if (strcasecmp(cvalue, "menu") == 0) {
354  match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
355  } else if (strcasecmp(cvalue, "dropdown_menu") == 0) {
356  match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
357  } else if (strcasecmp(cvalue, "popup_menu") == 0) {
358  match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
359  } else if (strcasecmp(cvalue, "tooltip") == 0) {
360  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
361  } else if (strcasecmp(cvalue, "notification") == 0) {
362  match->window_type = A__NET_WM_WINDOW_TYPE_NOTIFICATION;
363  } else {
364  ELOG("unknown window_type value \"%s\"\n", cvalue);
365  match->error = sstrdup("unknown window_type value");
366  }
367 
368  return;
369  }
370 
371  if (strcmp(ctype, "con_mark") == 0) {
372  regex_free(match->mark);
373  match->mark = regex_new(cvalue);
374  return;
375  }
376 
377  if (strcmp(ctype, "title") == 0) {
378  regex_free(match->title);
379  match->title = regex_new(cvalue);
380  return;
381  }
382 
383  if (strcmp(ctype, "urgent") == 0) {
384  if (strcasecmp(cvalue, "latest") == 0 ||
385  strcasecmp(cvalue, "newest") == 0 ||
386  strcasecmp(cvalue, "recent") == 0 ||
387  strcasecmp(cvalue, "last") == 0) {
388  match->urgent = U_LATEST;
389  } else if (strcasecmp(cvalue, "oldest") == 0 ||
390  strcasecmp(cvalue, "first") == 0) {
391  match->urgent = U_OLDEST;
392  }
393  return;
394  }
395 
396  if (strcmp(ctype, "workspace") == 0) {
397  regex_free(match->workspace);
398  match->workspace = regex_new(cvalue);
399  return;
400  }
401 
402  if (strcmp(ctype, "machine") == 0) {
403  regex_free(match->machine);
404  match->machine = regex_new(cvalue);
405  return;
406  }
407 
408  if (strcmp(ctype, "tiling") == 0) {
409  match->window_mode = WM_TILING;
410  return;
411  }
412 
413  if (strcmp(ctype, "tiling_from") == 0 &&
414  cvalue != NULL &&
415  strcmp(cvalue, "auto") == 0) {
416  match->window_mode = WM_TILING_AUTO;
417  return;
418  }
419 
420  if (strcmp(ctype, "tiling_from") == 0 &&
421  cvalue != NULL &&
422  strcmp(cvalue, "user") == 0) {
423  match->window_mode = WM_TILING_USER;
424  return;
425  }
426 
427  if (strcmp(ctype, "floating") == 0) {
428  match->window_mode = WM_FLOATING;
429  return;
430  }
431 
432  if (strcmp(ctype, "floating_from") == 0 &&
433  cvalue != NULL &&
434  strcmp(cvalue, "auto") == 0) {
435  match->window_mode = WM_FLOATING_AUTO;
436  return;
437  }
438 
439  if (strcmp(ctype, "floating_from") == 0 &&
440  cvalue != NULL &&
441  strcmp(cvalue, "user") == 0) {
442  match->window_mode = WM_FLOATING_USER;
443  return;
444  }
445 
446  /* match_matches_window() only checks negatively, so match_all_windows
447  * won't actually be used there, but that's OK because if no negative
448  * match is found (e.g. because of a more restrictive criterion) the
449  * return value of match_matches_window() is true.
450  * Setting it here only serves to cause match_is_empty() to return false,
451  * otherwise empty criteria rules apply, and that's not what we want. */
452  if (strcmp(ctype, "all") == 0) {
453  match->match_all_windows = true;
454  return;
455  }
456 
457  ELOG("Unknown criterion: %s\n", ctype);
458 }
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:477
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:668
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:620
struct pending_marks * marks
#define _i3_timercmp(a, b, CMP)
Definition: match.c:17
#define DUPLICATE_REGEX(field)
bool match_is_empty(Match *match)
Check if a match is empty.
Definition: match.c:39
void match_init(Match *match)
Initializes the Match data structure.
Definition: match.c:26
void match_copy(Match *dest, Match *src)
Copies the data of a match from src to dest.
Definition: match.c:64
void match_free(Match *match)
Frees the given match.
Definition: match.c:275
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition: match.c:89
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:291
#define CHECK_WINDOW_FIELD(match_field, window_field, type)
struct regex * regex_new(const char *pattern)
Creates a new 'regex' struct containing the given pattern and a PCRE compiled regular expression.
Definition: regex.c:22
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:73
void regex_free(struct regex *regex)
Frees the given regular expression.
Definition: regex.c:58
struct Con * focused
Definition: tree.c:13
struct all_cons_head all_cons
Definition: tree.c:15
bool parse_long(const char *str, long *out, int base)
Converts a string into a long using strtol().
Definition: util.c:409
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define DLOG(fmt,...)
Definition: libi3.h:105
#define LOG(fmt,...)
Definition: libi3.h:95
#define ELOG(fmt,...)
Definition: libi3.h:100
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define FREE(pointer)
Definition: util.h:47
char * pattern
Definition: data.h:250
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:394
struct timeval urgent
When this window was marked urgent.
Definition: data.h:446
xcb_window_t id
Definition: data.h:395
enum Window::@11 dock
Whether the window says it is a dock window.
xcb_atom_t window_type
The _NET_WM_WINDOW_TYPE for this window.
Definition: data.h:435
char * class_class
Definition: data.h:407
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
Definition: data.h:499
struct regex * window_role
Definition: data.h:508
bool match_all_windows
Definition: data.h:533
xcb_atom_t window_type
Definition: data.h:511
struct regex * title
Definition: data.h:503
struct regex * instance
Definition: data.h:506
enum Match::@12 urgent
struct regex * application
Definition: data.h:504
enum Match::@14 window_mode
struct regex * mark
Definition: data.h:507
struct regex * machine
Definition: data.h:510
struct regex * workspace
Definition: data.h:509
xcb_window_t id
Definition: data.h:524
Con * con_id
Definition: data.h:532
char * error
Definition: data.h:501
struct regex * class
Definition: data.h:505
enum Match::@13 dock
Definition: data.h:603
char * name
Definition: data.h:604
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:613
struct Window * window
Definition: data.h:685
char * name
Definition: data.h:659
enum Con::@19 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...