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->urgent == U_DONTCHECK &&
51  match->id == XCB_NONE &&
52  match->window_type == UINT32_MAX &&
53  match->con_id == NULL &&
54  match->dock == M_NODOCK &&
55  match->window_mode == WM_ANY);
56 }
57 
58 /*
59  * Copies the data of a match from src to dest.
60  *
61  */
62 void match_copy(Match *dest, Match *src) {
63  memcpy(dest, src, sizeof(Match));
64 
65 /* The DUPLICATE_REGEX macro creates a new regular expression from the
66  * ->pattern of the old one. It therefore does use a little more memory then
67  * with a refcounting system, but it’s easier this way. */
68 #define DUPLICATE_REGEX(field) \
69  do { \
70  if (src->field != NULL) \
71  dest->field = regex_new(src->field->pattern); \
72  } while (0)
73 
74  DUPLICATE_REGEX(title);
75  DUPLICATE_REGEX(mark);
76  DUPLICATE_REGEX(application);
77  DUPLICATE_REGEX(class);
78  DUPLICATE_REGEX(instance);
79  DUPLICATE_REGEX(window_role);
80  DUPLICATE_REGEX(workspace);
81 }
82 
83 /*
84  * Check if a match data structure matches the given window.
85  *
86  */
87 bool match_matches_window(Match *match, i3Window *window) {
88  LOG("Checking window 0x%08x (class %s)\n", window->id, window->class_class);
89 
90 #define GET_FIELD_str(field) (field)
91 #define GET_FIELD_i3string(field) (i3string_as_utf8(field))
92 #define CHECK_WINDOW_FIELD(match_field, window_field, type) \
93  do { \
94  if (match->match_field != NULL) { \
95  const char *window_field_str = window->window_field == NULL \
96  ? "" \
97  : GET_FIELD_##type(window->window_field); \
98  if (strcmp(match->match_field->pattern, "__focused__") == 0 && \
99  focused && focused->window && focused->window->window_field && \
100  strcmp(window_field_str, GET_FIELD_##type(focused->window->window_field)) == 0) { \
101  LOG("window " #match_field " matches focused window\n"); \
102  } else if (regex_matches(match->match_field, window_field_str)) { \
103  LOG("window " #match_field " matches (%s)\n", window_field_str); \
104  } else { \
105  return false; \
106  } \
107  } \
108  } while (0)
109 
110  CHECK_WINDOW_FIELD(class, class_class, str);
111  CHECK_WINDOW_FIELD(instance, class_instance, str);
112 
113  if (match->id != XCB_NONE) {
114  if (window->id == match->id) {
115  LOG("match made by window id (%d)\n", window->id);
116  } else {
117  LOG("window id does not match\n");
118  return false;
119  }
120  }
121 
122  CHECK_WINDOW_FIELD(title, name, i3string);
123  CHECK_WINDOW_FIELD(window_role, role, str);
124 
125  if (match->window_type != UINT32_MAX) {
126  if (window->window_type == match->window_type) {
127  LOG("window_type matches (%i)\n", match->window_type);
128  } else {
129  return false;
130  }
131  }
132 
133  Con *con = NULL;
134  if (match->urgent == U_LATEST) {
135  /* if the window isn't urgent, no sense in searching */
136  if (window->urgent.tv_sec == 0) {
137  return false;
138  }
139  /* if we find a window that is newer than this one, bail */
140  TAILQ_FOREACH (con, &all_cons, all_cons) {
141  if ((con->window != NULL) &&
142  _i3_timercmp(con->window->urgent, window->urgent, >)) {
143  return false;
144  }
145  }
146  LOG("urgent matches latest\n");
147  }
148 
149  if (match->urgent == U_OLDEST) {
150  /* if the window isn't urgent, no sense in searching */
151  if (window->urgent.tv_sec == 0) {
152  return false;
153  }
154  /* if we find a window that is older than this one (and not 0), bail */
155  TAILQ_FOREACH (con, &all_cons, all_cons) {
156  if ((con->window != NULL) &&
157  (con->window->urgent.tv_sec != 0) &&
158  _i3_timercmp(con->window->urgent, window->urgent, <)) {
159  return false;
160  }
161  }
162  LOG("urgent matches oldest\n");
163  }
164 
165  if (match->workspace != NULL) {
166  if ((con = con_by_window_id(window->id)) == NULL)
167  return false;
168 
169  Con *ws = con_get_workspace(con);
170  if (ws == NULL)
171  return false;
172 
173  if (strcmp(match->workspace->pattern, "__focused__") == 0 &&
174  strcmp(ws->name, con_get_workspace(focused)->name) == 0) {
175  LOG("workspace matches focused workspace\n");
176  } else if (regex_matches(match->workspace, ws->name)) {
177  LOG("workspace matches (%s)\n", ws->name);
178  } else {
179  return false;
180  }
181  }
182 
183  if (match->dock != M_DONTCHECK) {
184  if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
185  (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
186  ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
187  match->dock == M_DOCK_ANY) ||
188  (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
189  LOG("dock status matches\n");
190  } else {
191  LOG("dock status does not match\n");
192  return false;
193  }
194  }
195 
196  if (match->mark != NULL) {
197  if ((con = con_by_window_id(window->id)) == NULL)
198  return false;
199 
200  bool matched = false;
201  mark_t *mark;
202  TAILQ_FOREACH (mark, &(con->marks_head), marks) {
203  if (regex_matches(match->mark, mark->name)) {
204  matched = true;
205  break;
206  }
207  }
208 
209  if (matched) {
210  LOG("mark matches\n");
211  } else {
212  LOG("mark does not match\n");
213  return false;
214  }
215  }
216 
217  if (match->window_mode != WM_ANY) {
218  if ((con = con_by_window_id(window->id)) == NULL) {
219  return false;
220  }
221 
222  switch (match->window_mode) {
223  case WM_TILING_AUTO:
224  if (con->floating != FLOATING_AUTO_OFF) {
225  return false;
226  }
227  break;
228  case WM_TILING_USER:
229  if (con->floating != FLOATING_USER_OFF) {
230  return false;
231  }
232  break;
233  case WM_TILING:
234  if (con_inside_floating(con) != NULL) {
235  return false;
236  }
237  break;
238  case WM_FLOATING_AUTO:
239  if (con->floating != FLOATING_AUTO_ON) {
240  return false;
241  }
242  break;
243  case WM_FLOATING_USER:
244  if (con->floating != FLOATING_USER_ON) {
245  return false;
246  }
247  break;
248  case WM_FLOATING:
249  if (con_inside_floating(con) == NULL) {
250  return false;
251  }
252  break;
253  case WM_ANY:
254  assert(false);
255  }
256 
257  LOG("window_mode matches\n");
258  }
259 
260  return true;
261 }
262 
263 /*
264  * Frees the given match. It must not be used afterwards!
265  *
266  */
267 void match_free(Match *match) {
268  FREE(match->error);
269  regex_free(match->title);
270  regex_free(match->application);
271  regex_free(match->class);
272  regex_free(match->instance);
273  regex_free(match->mark);
274  regex_free(match->window_role);
275  regex_free(match->workspace);
276 }
277 
278 /*
279  * Interprets a ctype=cvalue pair and adds it to the given match specification.
280  *
281  */
282 void match_parse_property(Match *match, const char *ctype, const char *cvalue) {
283  assert(match != NULL);
284  DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
285 
286  if (strcmp(ctype, "class") == 0) {
287  regex_free(match->class);
288  match->class = regex_new(cvalue);
289  return;
290  }
291 
292  if (strcmp(ctype, "instance") == 0) {
293  regex_free(match->instance);
294  match->instance = regex_new(cvalue);
295  return;
296  }
297 
298  if (strcmp(ctype, "window_role") == 0) {
299  regex_free(match->window_role);
300  match->window_role = regex_new(cvalue);
301  return;
302  }
303 
304  if (strcmp(ctype, "con_id") == 0) {
305  if (strcmp(cvalue, "__focused__") == 0) {
306  match->con_id = focused;
307  return;
308  }
309 
310  long parsed;
311  if (!parse_long(cvalue, &parsed, 0)) {
312  ELOG("Could not parse con id \"%s\"\n", cvalue);
313  match->error = sstrdup("invalid con_id");
314  } else {
315  match->con_id = (Con *)parsed;
316  DLOG("id as int = %p\n", match->con_id);
317  }
318  return;
319  }
320 
321  if (strcmp(ctype, "id") == 0) {
322  long parsed;
323  if (!parse_long(cvalue, &parsed, 0)) {
324  ELOG("Could not parse window id \"%s\"\n", cvalue);
325  match->error = sstrdup("invalid id");
326  } else {
327  match->id = parsed;
328  DLOG("window id as int = %d\n", match->id);
329  }
330  return;
331  }
332 
333  if (strcmp(ctype, "window_type") == 0) {
334  if (strcasecmp(cvalue, "normal") == 0) {
335  match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
336  } else if (strcasecmp(cvalue, "dialog") == 0) {
337  match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
338  } else if (strcasecmp(cvalue, "utility") == 0) {
339  match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
340  } else if (strcasecmp(cvalue, "toolbar") == 0) {
341  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
342  } else if (strcasecmp(cvalue, "splash") == 0) {
343  match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
344  } else if (strcasecmp(cvalue, "menu") == 0) {
345  match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
346  } else if (strcasecmp(cvalue, "dropdown_menu") == 0) {
347  match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
348  } else if (strcasecmp(cvalue, "popup_menu") == 0) {
349  match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
350  } else if (strcasecmp(cvalue, "tooltip") == 0) {
351  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
352  } else if (strcasecmp(cvalue, "notification") == 0) {
353  match->window_type = A__NET_WM_WINDOW_TYPE_NOTIFICATION;
354  } else {
355  ELOG("unknown window_type value \"%s\"\n", cvalue);
356  match->error = sstrdup("unknown window_type value");
357  }
358 
359  return;
360  }
361 
362  if (strcmp(ctype, "con_mark") == 0) {
363  regex_free(match->mark);
364  match->mark = regex_new(cvalue);
365  return;
366  }
367 
368  if (strcmp(ctype, "title") == 0) {
369  regex_free(match->title);
370  match->title = regex_new(cvalue);
371  return;
372  }
373 
374  if (strcmp(ctype, "urgent") == 0) {
375  if (strcasecmp(cvalue, "latest") == 0 ||
376  strcasecmp(cvalue, "newest") == 0 ||
377  strcasecmp(cvalue, "recent") == 0 ||
378  strcasecmp(cvalue, "last") == 0) {
379  match->urgent = U_LATEST;
380  } else if (strcasecmp(cvalue, "oldest") == 0 ||
381  strcasecmp(cvalue, "first") == 0) {
382  match->urgent = U_OLDEST;
383  }
384  return;
385  }
386 
387  if (strcmp(ctype, "workspace") == 0) {
388  regex_free(match->workspace);
389  match->workspace = regex_new(cvalue);
390  return;
391  }
392 
393  if (strcmp(ctype, "tiling") == 0) {
394  match->window_mode = WM_TILING;
395  return;
396  }
397 
398  if (strcmp(ctype, "tiling_from") == 0 &&
399  cvalue != NULL &&
400  strcmp(cvalue, "auto") == 0) {
401  match->window_mode = WM_TILING_AUTO;
402  return;
403  }
404 
405  if (strcmp(ctype, "tiling_from") == 0 &&
406  cvalue != NULL &&
407  strcmp(cvalue, "user") == 0) {
408  match->window_mode = WM_TILING_USER;
409  return;
410  }
411 
412  if (strcmp(ctype, "floating") == 0) {
413  match->window_mode = WM_FLOATING;
414  return;
415  }
416 
417  if (strcmp(ctype, "floating_from") == 0 &&
418  cvalue != NULL &&
419  strcmp(cvalue, "auto") == 0) {
420  match->window_mode = WM_FLOATING_AUTO;
421  return;
422  }
423 
424  if (strcmp(ctype, "floating_from") == 0 &&
425  cvalue != NULL &&
426  strcmp(cvalue, "user") == 0) {
427  match->window_mode = WM_FLOATING_USER;
428  return;
429  }
430 
431  ELOG("Unknown criterion: %s\n", ctype);
432 }
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:476
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:667
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:619
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:62
void match_free(Match *match)
Frees the given match.
Definition: match.c:267
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition: match.c:87
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:282
#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:418
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:104
#define LOG(fmt,...)
Definition: libi3.h:94
#define ELOG(fmt,...)
Definition: libi3.h:99
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define FREE(pointer)
Definition: util.h:47
char * pattern
Definition: data.h:249
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:393
struct timeval urgent
When this window was marked urgent.
Definition: data.h:442
xcb_window_t id
Definition: data.h:394
xcb_atom_t window_type
The _NET_WM_WINDOW_TYPE for this window.
Definition: data.h:431
char * class_class
Definition: data.h:406
enum Window::@13 dock
Whether the window says it is a dock window.
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
Definition: data.h:492
struct regex * window_role
Definition: data.h:501
xcb_atom_t window_type
Definition: data.h:503
struct regex * title
Definition: data.h:496
struct regex * instance
Definition: data.h:499
enum Match::@15 dock
struct regex * application
Definition: data.h:497
struct regex * mark
Definition: data.h:500
struct regex * workspace
Definition: data.h:502
enum Match::@16 window_mode
xcb_window_t id
Definition: data.h:516
Con * con_id
Definition: data.h:524
enum Match::@14 urgent
char * error
Definition: data.h:494
struct regex * class
Definition: data.h:498
Definition: data.h:594
char * name
Definition: data.h:595
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:604
enum Con::@21 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
struct Window * window
Definition: data.h:671
char * name
Definition: data.h:650