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