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->hexid == 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 }
58 
59 /*
60  * Copies the data of a match from src to dest.
61  *
62  */
63 void match_copy(Match *dest, Match *src) {
64  memcpy(dest, src, sizeof(Match));
65 
66 /* The DUPLICATE_REGEX macro creates a new regular expression from the
67  * ->pattern of the old one. It therefore does use a little more memory then
68  * with a refcounting system, but it’s easier this way. */
69 #define DUPLICATE_REGEX(field) \
70  do { \
71  if (src->field != NULL) \
72  dest->field = regex_new(src->field->pattern); \
73  } while (0)
74 
75  DUPLICATE_REGEX(title);
76  DUPLICATE_REGEX(mark);
77  DUPLICATE_REGEX(application);
78  DUPLICATE_REGEX(class);
79  DUPLICATE_REGEX(instance);
80  DUPLICATE_REGEX(window_role);
81  DUPLICATE_REGEX(workspace);
82  DUPLICATE_REGEX(hexid);
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  if (window->window_field == NULL) { \
98  return false; \
99  } \
100  \
101  const char *window_field_str = GET_FIELD_##type(window->window_field); \
102  if (strcmp(match->match_field->pattern, "__focused__") == 0 && \
103  focused && focused->window && focused->window->window_field && \
104  strcmp(window_field_str, GET_FIELD_##type(focused->window->window_field)) == 0) { \
105  LOG("window " #match_field " matches focused window\n"); \
106  } else if (regex_matches(match->match_field, window_field_str)) { \
107  LOG("window " #match_field " matches (%s)\n", window_field_str); \
108  } else { \
109  return false; \
110  } \
111  } \
112  } while (0)
113 
114  CHECK_WINDOW_FIELD(class, class_class, str);
115  CHECK_WINDOW_FIELD(instance, class_instance, str);
116 
117  if (match->id != XCB_NONE) {
118  if (window->id == match->id) {
119  LOG("match made by window id (%d)\n", window->id);
120  } else {
121  LOG("window id does not match\n");
122  return false;
123  }
124  }
125 
126  if (match->hexid != NULL) {
127  if (window->name == NULL)
128  return false;
129  char *hexid;
130  sasprintf(&hexid, "0x%08x", window->id);
131  if (regex_matches(match->hexid, hexid)) {
132  LOG("hexid matches (%s)\n", hexid);
133  } else {
134  free(hexid);
135  return false;
136  }
137  free(hexid);
138  }
139 
140  CHECK_WINDOW_FIELD(title, name, i3string);
141  CHECK_WINDOW_FIELD(window_role, role, str);
142 
143  if (match->window_type != UINT32_MAX) {
144  if (window->window_type == match->window_type) {
145  LOG("window_type matches (%i)\n", match->window_type);
146  } else {
147  return false;
148  }
149  }
150 
151  Con *con = NULL;
152  if (match->urgent == U_LATEST) {
153  /* if the window isn't urgent, no sense in searching */
154  if (window->urgent.tv_sec == 0) {
155  return false;
156  }
157  /* if we find a window that is newer than this one, bail */
159  if ((con->window != NULL) &&
160  _i3_timercmp(con->window->urgent, window->urgent, >)) {
161  return false;
162  }
163  }
164  LOG("urgent matches latest\n");
165  }
166 
167  if (match->urgent == U_OLDEST) {
168  /* if the window isn't urgent, no sense in searching */
169  if (window->urgent.tv_sec == 0) {
170  return false;
171  }
172  /* if we find a window that is older than this one (and not 0), bail */
174  if ((con->window != NULL) &&
175  (con->window->urgent.tv_sec != 0) &&
176  _i3_timercmp(con->window->urgent, window->urgent, <)) {
177  return false;
178  }
179  }
180  LOG("urgent matches oldest\n");
181  }
182 
183  if (match->workspace != NULL) {
184  if ((con = con_by_window_id(window->id)) == NULL)
185  return false;
186 
187  Con *ws = con_get_workspace(con);
188  if (ws == NULL)
189  return false;
190 
191  if (strcmp(match->workspace->pattern, "__focused__") == 0 &&
192  strcmp(ws->name, con_get_workspace(focused)->name) == 0) {
193  LOG("workspace matches focused workspace\n");
194  } else if (regex_matches(match->workspace, ws->name)) {
195  LOG("workspace matches (%s)\n", ws->name);
196  } else {
197  return false;
198  }
199  }
200 
201  if (match->dock != M_DONTCHECK) {
202  if ((window->dock == W_DOCK_TOP && match->dock == M_DOCK_TOP) ||
203  (window->dock == W_DOCK_BOTTOM && match->dock == M_DOCK_BOTTOM) ||
204  ((window->dock == W_DOCK_TOP || window->dock == W_DOCK_BOTTOM) &&
205  match->dock == M_DOCK_ANY) ||
206  (window->dock == W_NODOCK && match->dock == M_NODOCK)) {
207  LOG("dock status matches\n");
208  } else {
209  LOG("dock status does not match\n");
210  return false;
211  }
212  }
213 
214  if (match->mark != NULL) {
215  if ((con = con_by_window_id(window->id)) == NULL)
216  return false;
217 
218  bool matched = false;
219  mark_t *mark;
220  TAILQ_FOREACH(mark, &(con->marks_head), marks) {
221  if (regex_matches(match->mark, mark->name)) {
222  matched = true;
223  break;
224  }
225  }
226 
227  if (matched) {
228  LOG("mark matches\n");
229  } else {
230  LOG("mark does not match\n");
231  return false;
232  }
233  }
234 
235  if (match->window_mode != WM_ANY) {
236  if ((con = con_by_window_id(window->id)) == NULL)
237  return false;
238 
239  const bool floating = (con_inside_floating(con) != NULL);
240 
241  if ((match->window_mode == WM_TILING && floating) ||
242  (match->window_mode == WM_FLOATING && !floating)) {
243  LOG("window_mode does not match\n");
244  return false;
245  }
246 
247  LOG("window_mode matches\n");
248  }
249 
250  return true;
251 }
252 
253 /*
254  * Frees the given match. It must not be used afterwards!
255  *
256  */
257 void match_free(Match *match) {
258  FREE(match->error);
259  regex_free(match->title);
260  regex_free(match->application);
261  regex_free(match->class);
262  regex_free(match->instance);
263  regex_free(match->mark);
264  regex_free(match->window_role);
265  regex_free(match->workspace);
266  regex_free(match->hexid);
267 }
268 
269 /*
270  * Interprets a ctype=cvalue pair and adds it to the given match specification.
271  *
272  */
273 void match_parse_property(Match *match, const char *ctype, const char *cvalue) {
274  assert(match != NULL);
275  DLOG("ctype=*%s*, cvalue=*%s*\n", ctype, cvalue);
276 
277  if (strcmp(ctype, "class") == 0) {
278  regex_free(match->class);
279  match->class = regex_new(cvalue);
280  return;
281  }
282 
283  if (strcmp(ctype, "instance") == 0) {
284  regex_free(match->instance);
285  match->instance = regex_new(cvalue);
286  return;
287  }
288 
289  if (strcmp(ctype, "window_role") == 0) {
290  regex_free(match->window_role);
291  match->window_role = regex_new(cvalue);
292  return;
293  }
294 
295  if (strcmp(ctype, "con_id") == 0) {
296  if (strcmp(cvalue, "__focused__") == 0) {
297  match->con_id = focused;
298  return;
299  }
300 
301  long parsed;
302  if (!parse_long(cvalue, &parsed, 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  long parsed;
314  if (!parse_long(cvalue, &parsed, 0)) {
315  ELOG("Could not parse window id \"%s\"\n", cvalue);
316  match->error = sstrdup("invalid id");
317  } else {
318  match->id = parsed;
319  DLOG("window id as int = %d\n", match->id);
320  }
321  return;
322  }
323 
324  if (strcmp(ctype, "hexid") == 0) {
325  regex_free(match->hexid);
326  match->hexid = regex_new(cvalue);
327  return;
328  }
329 
330  if (strcmp(ctype, "window_type") == 0) {
331  if (strcasecmp(cvalue, "normal") == 0) {
332  match->window_type = A__NET_WM_WINDOW_TYPE_NORMAL;
333  } else if (strcasecmp(cvalue, "dialog") == 0) {
334  match->window_type = A__NET_WM_WINDOW_TYPE_DIALOG;
335  } else if (strcasecmp(cvalue, "utility") == 0) {
336  match->window_type = A__NET_WM_WINDOW_TYPE_UTILITY;
337  } else if (strcasecmp(cvalue, "toolbar") == 0) {
338  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLBAR;
339  } else if (strcasecmp(cvalue, "splash") == 0) {
340  match->window_type = A__NET_WM_WINDOW_TYPE_SPLASH;
341  } else if (strcasecmp(cvalue, "menu") == 0) {
342  match->window_type = A__NET_WM_WINDOW_TYPE_MENU;
343  } else if (strcasecmp(cvalue, "dropdown_menu") == 0) {
344  match->window_type = A__NET_WM_WINDOW_TYPE_DROPDOWN_MENU;
345  } else if (strcasecmp(cvalue, "popup_menu") == 0) {
346  match->window_type = A__NET_WM_WINDOW_TYPE_POPUP_MENU;
347  } else if (strcasecmp(cvalue, "tooltip") == 0) {
348  match->window_type = A__NET_WM_WINDOW_TYPE_TOOLTIP;
349  } else if (strcasecmp(cvalue, "notification") == 0) {
350  match->window_type = A__NET_WM_WINDOW_TYPE_NOTIFICATION;
351  } else {
352  ELOG("unknown window_type value \"%s\"\n", cvalue);
353  match->error = sstrdup("unknown window_type value");
354  }
355 
356  return;
357  }
358 
359  if (strcmp(ctype, "con_mark") == 0) {
360  regex_free(match->mark);
361  match->mark = regex_new(cvalue);
362  return;
363  }
364 
365  if (strcmp(ctype, "title") == 0) {
366  regex_free(match->title);
367  match->title = regex_new(cvalue);
368  return;
369  }
370 
371  if (strcmp(ctype, "urgent") == 0) {
372  if (strcasecmp(cvalue, "latest") == 0 ||
373  strcasecmp(cvalue, "newest") == 0 ||
374  strcasecmp(cvalue, "recent") == 0 ||
375  strcasecmp(cvalue, "last") == 0) {
376  match->urgent = U_LATEST;
377  } else if (strcasecmp(cvalue, "oldest") == 0 ||
378  strcasecmp(cvalue, "first") == 0) {
379  match->urgent = U_OLDEST;
380  }
381  return;
382  }
383 
384  if (strcmp(ctype, "workspace") == 0) {
385  regex_free(match->workspace);
386  match->workspace = regex_new(cvalue);
387  return;
388  }
389 
390  if (strcmp(ctype, "tiling") == 0) {
391  match->window_mode = WM_TILING;
392  return;
393  }
394 
395  if (strcmp(ctype, "floating") == 0) {
396  match->window_mode = WM_FLOATING;
397  return;
398  }
399 
400  ELOG("Unknown criterion: %s\n", ctype);
401 }
struct regex * instance
Definition: data.h:528
#define FREE(pointer)
Definition: util.h:47
#define CHECK_WINDOW_FIELD(match_field, window_field, type)
enum Match::@16 window_mode
enum Match::@14 urgent
enum Window::@13 dock
Whether the window says it is a dock window.
char * name
Definition: data.h:623
bool parse_long(const char *str, long *out, int base)
Converts a string into a long using strtol().
Definition: util.c:467
struct regex * workspace
Definition: data.h:531
#define DLOG(fmt,...)
Definition: libi3.h:104
void match_free(Match *match)
Frees the given match.
Definition: match.c:257
char * pattern
Definition: data.h:273
struct regex * class
Definition: data.h:527
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition: con.c:564
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:612
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:273
xcb_window_t id
Definition: data.h:546
char * error
Definition: data.h:523
#define _i3_timercmp(a, b, CMP)
Definition: match.c:17
char * name
Definition: data.h:682
Definition: data.h:622
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:418
xcb_atom_t window_type
The _NET_WM_WINDOW_TYPE for this window.
Definition: data.h:467
bool match_is_empty(Match *match)
Check if a match is empty.
Definition: match.c:39
char * class_class
Definition: data.h:442
xcb_window_t id
Definition: data.h:430
struct pending_marks * marks
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition: match.c:89
struct regex * title
Definition: data.h:525
#define LOG(fmt,...)
Definition: libi3.h:94
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
Definition: data.h:521
struct regex * window_role
Definition: data.h:530
struct Con * focused
Definition: tree.c:13
struct timeval urgent
When this window was marked urgent.
Definition: data.h:478
struct all_cons_head all_cons
Definition: tree.c:15
marks_head
Definition: data.h:694
void regex_free(struct regex *regex)
Frees the given regular expression.
Definition: regex.c:58
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition: data.h:429
struct regex * hexid
Definition: data.h:532
#define ELOG(fmt,...)
Definition: libi3.h:99
struct regex * mark
Definition: data.h:529
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition: data.h:633
void match_copy(Match *dest, Match *src)
Copies the data of a match from src to dest.
Definition: match.c:63
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
struct Window * window
Definition: data.h:704
void match_init(Match *match)
Initializes the Match data structure.
Definition: match.c:26
xcb_atom_t window_type
Definition: data.h:533
struct regex * application
Definition: data.h:526
Con * con_id
Definition: data.h:550
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
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
#define DUPLICATE_REGEX(field)
i3String * name
The name of the window.
Definition: data.h:446
enum Match::@15 dock