i3
bindings.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  * bindings.c: Functions for configuring, finding and, running bindings.
8  */
9 #include "all.h"
10 
11 #include <xkbcommon/xkbcommon.h>
12 #include <xkbcommon/xkbcommon-x11.h>
13 
14 static struct xkb_context *xkb_context;
15 static struct xkb_keymap *xkb_keymap;
16 
18 
19 /*
20  * The name of the default mode.
21  *
22  */
23 const char *DEFAULT_BINDING_MODE = "default";
24 
25 /*
26  * Returns the mode specified by `name` or creates a new mode and adds it to
27  * the list of modes.
28  *
29  */
30 static struct Mode *mode_from_name(const char *name, bool pango_markup) {
31  struct Mode *mode;
32 
33  /* Try to find the mode in the list of modes and return it */
34  SLIST_FOREACH(mode, &modes, modes) {
35  if (strcmp(mode->name, name) == 0)
36  return mode;
37  }
38 
39  /* If the mode was not found, create a new one */
40  mode = scalloc(1, sizeof(struct Mode));
41  mode->name = sstrdup(name);
42  mode->pango_markup = pango_markup;
43  mode->bindings = scalloc(1, sizeof(struct bindings_head));
44  TAILQ_INIT(mode->bindings);
45  SLIST_INSERT_HEAD(&modes, mode, modes);
46 
47  return mode;
48 }
49 
50 /*
51  * Adds a binding from config parameters given as strings and returns a
52  * pointer to the binding structure. Returns NULL if the input code could not
53  * be parsed.
54  *
55  */
56 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
57  const char *release, const char *border, const char *whole_window,
58  const char *command, const char *modename, bool pango_markup) {
59  Binding *new_binding = scalloc(1, sizeof(Binding));
60  DLOG("bindtype %s, modifiers %s, input code %s, release %s\n", bindtype, modifiers, input_code, release);
61  new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
62  new_binding->border = (border != NULL);
63  new_binding->whole_window = (whole_window != NULL);
64  if (strcmp(bindtype, "bindsym") == 0) {
65  new_binding->input_type = (strncasecmp(input_code, "button", (sizeof("button") - 1)) == 0
66  ? B_MOUSE
67  : B_KEYBOARD);
68 
69  new_binding->symbol = sstrdup(input_code);
70  } else {
71  char *endptr;
72  long keycode = strtol(input_code, &endptr, 10);
73  new_binding->keycode = keycode;
74  new_binding->input_type = B_KEYBOARD;
75  if (keycode == LONG_MAX || keycode == LONG_MIN || keycode < 0 || *endptr != '\0' || endptr == input_code) {
76  ELOG("Could not parse \"%s\" as an input code, ignoring this binding.\n", input_code);
77  FREE(new_binding);
78  return NULL;
79  }
80  }
81  new_binding->command = sstrdup(command);
82  new_binding->event_state_mask = event_state_from_str(modifiers);
83  int group_bits_set = 0;
84  if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_1)
85  group_bits_set++;
86  if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
87  group_bits_set++;
88  if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
89  group_bits_set++;
90  if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
91  group_bits_set++;
92  if (group_bits_set > 1)
93  ELOG("Keybinding has more than one Group specified, but your X server is always in precisely one group. The keybinding can never trigger.\n");
94 
95  struct Mode *mode = mode_from_name(modename, pango_markup);
96  TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
97 
98  return new_binding;
99 }
100 
101 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
102  if (bind->input_type != B_KEYBOARD)
103  return;
104 
105 /* Grab the key in all combinations */
106 #define GRAB_KEY(modifier) \
107  do { \
108  xcb_grab_key(conn, 0, root, modifier, keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
109  } while (0)
110  int mods = bind->event_state_mask;
111  if (((mods >> 16) & I3_XKB_GROUP_MASK_1) && xkb_current_group != XCB_XKB_GROUP_1)
112  return;
113  if (((mods >> 16) & I3_XKB_GROUP_MASK_2) && xkb_current_group != XCB_XKB_GROUP_2)
114  return;
115  if (((mods >> 16) & I3_XKB_GROUP_MASK_3) && xkb_current_group != XCB_XKB_GROUP_3)
116  return;
117  if (((mods >> 16) & I3_XKB_GROUP_MASK_4) && xkb_current_group != XCB_XKB_GROUP_4)
118  return;
119  mods &= 0xFFFF;
120  DLOG("Grabbing keycode %d with event state mask 0x%x (mods 0x%x)\n",
121  keycode, bind->event_state_mask, mods);
122  GRAB_KEY(mods);
123  GRAB_KEY(mods | xcb_numlock_mask);
124  GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
125  GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
126 }
127 
128 /*
129  * Grab the bound keys (tell X to send us keypress events for those keycodes)
130  *
131  */
132 void grab_all_keys(xcb_connection_t *conn) {
133  Binding *bind;
135  if (bind->input_type != B_KEYBOARD)
136  continue;
137 
138  /* The easy case: the user specified a keycode directly. */
139  if (bind->keycode > 0) {
140  grab_keycode_for_binding(conn, bind, bind->keycode);
141  continue;
142  }
143 
144  for (uint32_t i = 0; i < bind->number_keycodes; i++)
145  grab_keycode_for_binding(conn, bind, bind->translated_to[i]);
146  }
147 }
148 
149 /*
150  * Release the button grabs on all managed windows and regrab them,
151  * reevaluating which buttons need to be grabbed.
152  *
153  */
154 void regrab_all_buttons(xcb_connection_t *conn) {
155  bool grab_scrollwheel = bindings_should_grab_scrollwheel_buttons();
156  xcb_grab_server(conn);
157 
158  Con *con;
160  if (con->window == NULL)
161  continue;
162 
163  xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, con->window->id, XCB_BUTTON_MASK_ANY);
164  xcb_grab_buttons(conn, con->window->id, grab_scrollwheel);
165  }
166 
167  xcb_ungrab_server(conn);
168 }
169 
170 /*
171  * Returns a pointer to the Binding with the specified modifiers and
172  * keycode or NULL if no such binding exists.
173  *
174  */
175 static Binding *get_binding(i3_event_state_mask_t state_filtered, bool is_release, uint16_t input_code, input_type_t input_type) {
176  Binding *bind;
177 
178  if (!is_release) {
179  /* On a press event, we first reset all B_UPON_KEYRELEASE_IGNORE_MODS
180  * bindings back to B_UPON_KEYRELEASE */
182  if (bind->input_type != input_type)
183  continue;
184  if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
185  bind->release = B_UPON_KEYRELEASE;
186  }
187  }
188 
189  const uint32_t xkb_group_state = (state_filtered & 0xFFFF0000);
190  const uint32_t modifiers_state = (state_filtered & 0x0000FFFF);
192  const uint32_t xkb_group_mask = (bind->event_state_mask & 0xFFFF0000);
193  /* modifiers_mask is a special case: a value of 0 does not mean “match all”,
194  * but rather “match exactly when no modifiers are present”. */
195  const uint32_t modifiers_mask = (bind->event_state_mask & 0x0000FFFF);
196  const bool groups_match = ((xkb_group_state & xkb_group_mask) == xkb_group_mask);
197  bool mods_match;
198  if (modifiers_mask == 0) {
199  /* Verify no modifiers are pressed. A bitwise AND would lead to
200  * false positives, see issue #2002. */
201  mods_match = (modifiers_state == 0);
202  } else {
203  mods_match = ((modifiers_state & modifiers_mask) == modifiers_mask);
204  }
205  const bool state_matches = (groups_match && mods_match);
206 
207  DLOG("binding groups_match = %s, mods_match = %s, state_matches = %s\n",
208  (groups_match ? "yes" : "no"),
209  (mods_match ? "yes" : "no"),
210  (state_matches ? "yes" : "no"));
211  /* First compare the state_filtered (unless this is a
212  * B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
213  * event) */
214  if (bind->input_type != input_type)
215  continue;
216  if (!state_matches &&
217  (bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
218  !is_release))
219  continue;
220 
221  /* For keyboard bindings where a symbol was specified by the user, we
222  * need to look in the array of translated keycodes for the event’s
223  * keycode */
224  if (input_type == B_KEYBOARD && bind->symbol != NULL) {
225  xcb_keycode_t input_keycode = (xcb_keycode_t)input_code;
226  if (memmem(bind->translated_to,
227  bind->number_keycodes * sizeof(xcb_keycode_t),
228  &input_keycode, sizeof(xcb_keycode_t)) == NULL)
229  continue;
230  } else {
231  /* This case is easier: The user specified a keycode */
232  if (bind->keycode != input_code)
233  continue;
234  }
235 
236  /* If this binding is a release binding, it matches the key which the
237  * user pressed. We therefore mark it as B_UPON_KEYRELEASE_IGNORE_MODS
238  * for later, so that the user can release the modifiers before the
239  * actual key or button and the release event will still be matched. */
240  if (bind->release == B_UPON_KEYRELEASE && !is_release)
241  bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
242 
243  /* Check if the binding is for a press or a release event */
244  if ((bind->release == B_UPON_KEYPRESS && is_release) ||
245  (bind->release >= B_UPON_KEYRELEASE && !is_release))
246  continue;
247 
248  break;
249  }
250 
251  return (bind == TAILQ_END(bindings) ? NULL : bind);
252 }
253 
254 /*
255  * Returns a pointer to the Binding that matches the given xcb button or key
256  * event or NULL if no such binding exists.
257  *
258  */
259 Binding *get_binding_from_xcb_event(xcb_generic_event_t *event) {
260  const bool is_release = (event->response_type == XCB_KEY_RELEASE ||
261  event->response_type == XCB_BUTTON_RELEASE);
262 
263  const input_type_t input_type = ((event->response_type == XCB_BUTTON_RELEASE ||
264  event->response_type == XCB_BUTTON_PRESS)
265  ? B_MOUSE
266  : B_KEYBOARD);
267 
268  const uint16_t event_state = ((xcb_key_press_event_t *)event)->state;
269  const uint16_t event_detail = ((xcb_key_press_event_t *)event)->detail;
270 
271  /* Remove the numlock bit */
272  i3_event_state_mask_t state_filtered = event_state & ~(xcb_numlock_mask | XCB_MOD_MASK_LOCK);
273  DLOG("(removed numlock, state = 0x%x)\n", state_filtered);
274  /* Transform the keyboard_group from bit 13 and bit 14 into an
275  * i3_xkb_group_mask_t, so that get_binding() can just bitwise AND the
276  * configured bindings against |state_filtered|.
277  *
278  * These bits are only set because we set the XKB client flags
279  * XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE and
280  * XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED. See also doc/kbproto
281  * section 2.2.2:
282  * http://www.x.org/releases/X11R7.7/doc/kbproto/xkbproto.html#Computing_A_State_Field_from_an_XKB_State */
283  switch ((event_state & 0x6000) >> 13) {
284  case XCB_XKB_GROUP_1:
285  state_filtered |= (I3_XKB_GROUP_MASK_1 << 16);
286  break;
287  case XCB_XKB_GROUP_2:
288  state_filtered |= (I3_XKB_GROUP_MASK_2 << 16);
289  break;
290  case XCB_XKB_GROUP_3:
291  state_filtered |= (I3_XKB_GROUP_MASK_3 << 16);
292  break;
293  case XCB_XKB_GROUP_4:
294  state_filtered |= (I3_XKB_GROUP_MASK_4 << 16);
295  break;
296  }
297  state_filtered &= ~0x6000;
298  DLOG("(transformed keyboard group, state = 0x%x)\n", state_filtered);
299  return get_binding(state_filtered, is_release, event_detail, input_type);
300 }
301 
302 struct resolve {
303  /* The binding which we are resolving. */
305 
306  /* |bind|’s keysym (translated to xkb_keysym_t), e.g. XKB_KEY_R. */
307  xkb_keysym_t keysym;
308 
309  /* The xkb state built from the user-provided modifiers and group. */
311 
312  /* Like |xkb_state|, just without the shift modifier, if shift was specified. */
314 };
315 
316 /*
317  * add_keycode_if_matches is called for each keycode in the keymap and will add
318  * the keycode to |data->bind| if the keycode can result in the keysym
319  * |data->resolving|.
320  *
321  */
322 static void add_keycode_if_matches(struct xkb_keymap *keymap, xkb_keycode_t key, void *data) {
323  const struct resolve *resolving = data;
324  xkb_keysym_t sym = xkb_state_key_get_one_sym(resolving->xkb_state, key);
325  if (sym != resolving->keysym) {
326  /* Check if Shift was specified, and try resolving the symbol without
327  * shift, so that “bindsym $mod+Shift+a nop” actually works. */
328  const xkb_layout_index_t layout = xkb_state_key_get_layout(resolving->xkb_state, key);
329  if (layout == XKB_LAYOUT_INVALID)
330  return;
331  if (xkb_state_key_get_level(resolving->xkb_state, key, layout) > 1)
332  return;
333  sym = xkb_state_key_get_one_sym(resolving->xkb_state_no_shift, key);
334  if (sym != resolving->keysym)
335  return;
336  }
337  Binding *bind = resolving->bind;
338  bind->number_keycodes++;
339  bind->translated_to = srealloc(bind->translated_to,
340  (sizeof(xcb_keycode_t) *
341  bind->number_keycodes));
342  bind->translated_to[bind->number_keycodes - 1] = key;
343 }
344 
345 /*
346  * Translates keysymbols to keycodes for all bindings which use keysyms.
347  *
348  */
349 void translate_keysyms(void) {
350  struct xkb_state *dummy_state = xkb_state_new(xkb_keymap);
351  if (dummy_state == NULL) {
352  ELOG("Could not create XKB state, cannot translate keysyms.\n");
353  return;
354  }
355 
356  struct xkb_state *dummy_state_no_shift = xkb_state_new(xkb_keymap);
357  if (dummy_state_no_shift == NULL) {
358  ELOG("Could not create XKB state, cannot translate keysyms.\n");
359  return;
360  }
361 
362  bool has_errors = false;
363  Binding *bind;
365  if (bind->input_type == B_MOUSE) {
366  char *endptr;
367  long button = strtol(bind->symbol + (sizeof("button") - 1), &endptr, 10);
368  bind->keycode = button;
369 
370  if (button == LONG_MAX || button == LONG_MIN || button < 0 || *endptr != '\0' || endptr == bind->symbol)
371  ELOG("Could not translate string to button: \"%s\"\n", bind->symbol);
372 
373  continue;
374  }
375 
376  if (bind->keycode > 0)
377  continue;
378 
379  /* We need to translate the symbol to a keycode */
380  const xkb_keysym_t keysym = xkb_keysym_from_name(bind->symbol, XKB_KEYSYM_NO_FLAGS);
381  if (keysym == XKB_KEY_NoSymbol) {
382  ELOG("Could not translate string to key symbol: \"%s\"\n",
383  bind->symbol);
384  continue;
385  }
386 
387  xkb_layout_index_t group = XCB_XKB_GROUP_1;
388  if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
389  group = XCB_XKB_GROUP_2;
390  else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
391  group = XCB_XKB_GROUP_3;
392  else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
393  group = XCB_XKB_GROUP_4;
394 
395  DLOG("group = %d, event_state_mask = %d, &2 = %s, &3 = %s, &4 = %s\n", group,
396  bind->event_state_mask,
397  (bind->event_state_mask & I3_XKB_GROUP_MASK_2) ? "yes" : "no",
398  (bind->event_state_mask & I3_XKB_GROUP_MASK_3) ? "yes" : "no",
399  (bind->event_state_mask & I3_XKB_GROUP_MASK_4) ? "yes" : "no");
400  (void)xkb_state_update_mask(
401  dummy_state,
402  (bind->event_state_mask & 0x1FFF) /* xkb_mod_mask_t base_mods, */,
403  0 /* xkb_mod_mask_t latched_mods, */,
404  0 /* xkb_mod_mask_t locked_mods, */,
405  0 /* xkb_layout_index_t base_group, */,
406  0 /* xkb_layout_index_t latched_group, */,
407  group /* xkb_layout_index_t locked_group, */);
408 
409  (void)xkb_state_update_mask(
410  dummy_state_no_shift,
411  (bind->event_state_mask & 0x1FFF) ^ XCB_KEY_BUT_MASK_SHIFT /* xkb_mod_mask_t base_mods, */,
412  0 /* xkb_mod_mask_t latched_mods, */,
413  0 /* xkb_mod_mask_t locked_mods, */,
414  0 /* xkb_layout_index_t base_group, */,
415  0 /* xkb_layout_index_t latched_group, */,
416  group /* xkb_layout_index_t locked_group, */);
417 
418  struct resolve resolving = {
419  .bind = bind,
420  .keysym = keysym,
421  .xkb_state = dummy_state,
422  .xkb_state_no_shift = dummy_state_no_shift,
423  };
424  FREE(bind->translated_to);
425  bind->number_keycodes = 0;
426  xkb_keymap_key_for_each(xkb_keymap, add_keycode_if_matches, &resolving);
427  char *keycodes = sstrdup("");
428  for (uint32_t n = 0; n < bind->number_keycodes; n++) {
429  char *tmp;
430  sasprintf(&tmp, "%s %d", keycodes, bind->translated_to[n]);
431  free(keycodes);
432  keycodes = tmp;
433 
434  /* check for duplicate bindings */
435  Binding *check;
436  TAILQ_FOREACH(check, bindings, bindings) {
437  if (check == bind)
438  continue;
439  if (check->symbol != NULL)
440  continue;
441  if (check->keycode != bind->translated_to[n] ||
442  check->event_state_mask != bind->event_state_mask ||
443  check->release != bind->release)
444  continue;
445  has_errors = true;
446  ELOG("Duplicate keybinding in config file:\n keysym = %s, keycode = %d, state_mask = 0x%x\n", bind->symbol, check->keycode, bind->event_state_mask);
447  }
448  }
449  DLOG("state=0x%x, cfg=\"%s\", sym=0x%x → keycodes%s (%d)\n",
450  bind->event_state_mask, bind->symbol, keysym, keycodes, bind->number_keycodes);
451  free(keycodes);
452  }
453 
454  xkb_state_unref(dummy_state);
455  xkb_state_unref(dummy_state_no_shift);
456 
457  if (has_errors) {
459  }
460 }
461 
462 /*
463  * Switches the key bindings to the given mode, if the mode exists
464  *
465  */
466 void switch_mode(const char *new_mode) {
467  struct Mode *mode;
468 
469  DLOG("Switching to mode %s\n", new_mode);
470 
471  SLIST_FOREACH(mode, &modes, modes) {
472  if (strcasecmp(mode->name, new_mode) != 0)
473  continue;
474 
476  bindings = mode->bindings;
479 
480  char *event_msg;
481  sasprintf(&event_msg, "{\"change\":\"%s\", \"pango_markup\":%s}",
482  mode->name, (mode->pango_markup ? "true" : "false"));
483 
484  ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
485  FREE(event_msg);
486 
487  return;
488  }
489 
490  ELOG("ERROR: Mode not found\n");
491 }
492 
493 static int reorder_binding_cmp(const void *a, const void *b) {
494  Binding *first = *((Binding **)a);
495  Binding *second = *((Binding **)b);
496  if (first->event_state_mask < second->event_state_mask) {
497  return 1;
498  } else if (first->event_state_mask == second->event_state_mask) {
499  return 0;
500  } else {
501  return -1;
502  }
503 }
504 
505 static void reorder_bindings_of_mode(struct Mode *mode) {
506  /* Copy the bindings into an array, so that we can use qsort(3). */
507  int n = 0;
508  Binding *current;
509  TAILQ_FOREACH(current, mode->bindings, bindings) {
510  n++;
511  }
512  Binding **tmp = scalloc(n, sizeof(Binding *));
513  n = 0;
514  TAILQ_FOREACH(current, mode->bindings, bindings) {
515  tmp[n++] = current;
516  }
517 
518  qsort(tmp, n, sizeof(Binding *), reorder_binding_cmp);
519 
520  struct bindings_head *reordered = scalloc(1, sizeof(struct bindings_head));
521  TAILQ_INIT(reordered);
522  for (int i = 0; i < n; i++) {
523  current = tmp[i];
524  TAILQ_REMOVE(mode->bindings, current, bindings);
525  TAILQ_INSERT_TAIL(reordered, current, bindings);
526  }
527  free(tmp);
528  assert(TAILQ_EMPTY(mode->bindings));
529  /* Free the old bindings_head, which is now empty. */
530  free(mode->bindings);
531  mode->bindings = reordered;
532 }
533 
534 /*
535  * Reorders bindings by event_state_mask descendingly so that get_binding()
536  * correctly matches more specific bindings before more generic bindings. Take
537  * the following binding configuration as an example:
538  *
539  * bindsym n nop lower-case n pressed
540  * bindsym Shift+n nop upper-case n pressed
541  *
542  * Without reordering, the first binding’s event_state_mask of 0x0 would match
543  * the actual event_stat_mask of 0x1 and hence trigger instead of the second
544  * keybinding.
545  *
546  */
547 void reorder_bindings(void) {
548  struct Mode *mode;
549  SLIST_FOREACH(mode, &modes, modes) {
550  const bool current_mode = (mode->bindings == bindings);
552  if (current_mode)
553  bindings = mode->bindings;
554  }
555 }
556 
557 /*
558  * Checks for duplicate key bindings (the same keycode or keysym is configured
559  * more than once). If a duplicate binding is found, a message is printed to
560  * stderr and the has_errors variable is set to true, which will start
561  * i3-nagbar.
562  *
563  */
565  Binding *bind, *current;
566  TAILQ_FOREACH(current, bindings, bindings) {
568  /* Abort when we reach the current keybinding, only check the
569  * bindings before */
570  if (bind == current)
571  break;
572 
573  /* Check if the input types are different */
574  if (bind->input_type != current->input_type)
575  continue;
576 
577  /* Check if one is using keysym while the other is using bindsym.
578  * If so, skip. */
579  if ((bind->symbol == NULL && current->symbol != NULL) ||
580  (bind->symbol != NULL && current->symbol == NULL))
581  continue;
582 
583  /* If bind is NULL, current has to be NULL, too (see above).
584  * If the keycodes differ, it can't be a duplicate. */
585  if (bind->symbol != NULL &&
586  strcasecmp(bind->symbol, current->symbol) != 0)
587  continue;
588 
589  /* Check if the keycodes or modifiers are different. If so, they
590  * can't be duplicate */
591  if (bind->keycode != current->keycode ||
592  bind->event_state_mask != current->event_state_mask ||
593  bind->release != current->release)
594  continue;
595 
596  context->has_errors = true;
597  if (current->keycode != 0) {
598  ELOG("Duplicate keybinding in config file:\n state mask 0x%x with keycode %d, command \"%s\"\n",
599  current->event_state_mask, current->keycode, current->command);
600  } else {
601  ELOG("Duplicate keybinding in config file:\n state mask 0x%x with keysym %s, command \"%s\"\n",
602  current->event_state_mask, current->symbol, current->command);
603  }
604  }
605  }
606 }
607 
608 /*
609  * Creates a dynamically allocated copy of bind.
610  */
611 static Binding *binding_copy(Binding *bind) {
612  Binding *ret = smalloc(sizeof(Binding));
613  *ret = *bind;
614  if (bind->symbol != NULL)
615  ret->symbol = sstrdup(bind->symbol);
616  if (bind->command != NULL)
617  ret->command = sstrdup(bind->command);
618  if (bind->translated_to != NULL) {
619  ret->translated_to = smalloc(sizeof(xcb_keycode_t) * bind->number_keycodes);
620  memcpy(ret->translated_to, bind->translated_to, sizeof(xcb_keycode_t) * bind->number_keycodes);
621  }
622  return ret;
623 }
624 
625 /*
626  * Frees the binding. If bind is null, it simply returns.
627  */
628 void binding_free(Binding *bind) {
629  if (bind == NULL) {
630  return;
631  }
632 
633  FREE(bind->symbol);
634  FREE(bind->translated_to);
635  FREE(bind->command);
636  FREE(bind);
637 }
638 
639 /*
640  * Runs the given binding and handles parse errors. If con is passed, it will
641  * execute the command binding with that container selected by criteria.
642  * Returns a CommandResult for running the binding's command. Free with
643  * command_result_free().
644  *
645  */
647  char *command;
648 
649  /* We need to copy the binding and command since “reload” may be part of
650  * the command, and then the memory that bind points to may not contain the
651  * same data anymore. */
652  if (con == NULL)
653  command = sstrdup(bind->command);
654  else
655  sasprintf(&command, "[con_id=\"%p\"] %s", con, bind->command);
656 
657  Binding *bind_cp = binding_copy(bind);
658  CommandResult *result = parse_command(command, NULL);
659  free(command);
660 
661  if (result->needs_tree_render)
662  tree_render();
663 
664  if (result->parse_error) {
665  char *pageraction;
666  sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
667  char *argv[] = {
668  NULL, /* will be replaced by the executable path */
669  "-f",
671  "-t",
672  "error",
673  "-m",
674  "The configured command for this shortcut could not be run successfully.",
675  "-b",
676  "show errors",
677  pageraction,
678  NULL};
680  free(pageraction);
681  }
682 
683  ipc_send_binding_event("run", bind_cp);
684  binding_free(bind_cp);
685 
686  return result;
687 }
688 
689 static int fill_rmlvo_from_root(struct xkb_rule_names *xkb_names) {
690  xcb_intern_atom_reply_t *atom_reply;
691  size_t content_max_words = 256;
692 
693  xcb_window_t root = root_screen->root;
694 
695  atom_reply = xcb_intern_atom_reply(
696  conn, xcb_intern_atom(conn, 0, strlen("_XKB_RULES_NAMES"), "_XKB_RULES_NAMES"), NULL);
697  if (atom_reply == NULL)
698  return -1;
699 
700  xcb_get_property_cookie_t prop_cookie;
701  xcb_get_property_reply_t *prop_reply;
702  prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
703  XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
704  prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
705  if (prop_reply == NULL) {
706  free(atom_reply);
707  return -1;
708  }
709  if (xcb_get_property_value_length(prop_reply) > 0 && prop_reply->bytes_after > 0) {
710  /* We received an incomplete value. Ask again but with a properly
711  * adjusted size. */
712  content_max_words += ceil(prop_reply->bytes_after / 4.0);
713  /* Repeat the request, with adjusted size */
714  free(prop_reply);
715  prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
716  XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
717  prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
718  if (prop_reply == NULL) {
719  free(atom_reply);
720  return -1;
721  }
722  }
723  if (xcb_get_property_value_length(prop_reply) == 0) {
724  free(atom_reply);
725  free(prop_reply);
726  return -1;
727  }
728 
729  const char *walk = (const char *)xcb_get_property_value(prop_reply);
730  int remaining = xcb_get_property_value_length(prop_reply);
731  for (int i = 0; i < 5 && remaining > 0; i++) {
732  const int len = strnlen(walk, remaining);
733  remaining -= len;
734  switch (i) {
735  case 0:
736  sasprintf((char **)&(xkb_names->rules), "%.*s", len, walk);
737  break;
738  case 1:
739  sasprintf((char **)&(xkb_names->model), "%.*s", len, walk);
740  break;
741  case 2:
742  sasprintf((char **)&(xkb_names->layout), "%.*s", len, walk);
743  break;
744  case 3:
745  sasprintf((char **)&(xkb_names->variant), "%.*s", len, walk);
746  break;
747  case 4:
748  sasprintf((char **)&(xkb_names->options), "%.*s", len, walk);
749  break;
750  }
751  DLOG("component %d of _XKB_RULES_NAMES is \"%.*s\"\n", i, len, walk);
752  walk += (len + 1);
753  }
754 
755  free(atom_reply);
756  free(prop_reply);
757  return 0;
758 }
759 
760 /*
761  * Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
762  *
763  */
764 bool load_keymap(void) {
765  if (xkb_context == NULL) {
766  if ((xkb_context = xkb_context_new(0)) == NULL) {
767  ELOG("Could not create xkbcommon context\n");
768  return false;
769  }
770  }
771 
772  struct xkb_keymap *new_keymap = NULL;
773  int32_t device_id;
774  if (xkb_supported && (device_id = xkb_x11_get_core_keyboard_device_id(conn)) > -1) {
775  if ((new_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) {
776  ELOG("xkb_x11_keymap_new_from_device failed\n");
777  return false;
778  }
779  } else {
780  /* Likely there is no XKB support on this server, possibly because it
781  * is a VNC server. */
782  LOG("No XKB / core keyboard device? Assembling keymap from local RMLVO.\n");
783  struct xkb_rule_names names = {
784  .rules = NULL,
785  .model = NULL,
786  .layout = NULL,
787  .variant = NULL,
788  .options = NULL};
789  if (fill_rmlvo_from_root(&names) == -1) {
790  ELOG("Could not get _XKB_RULES_NAMES atom from root window, falling back to defaults.\n");
791  if ((new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0)) == NULL) {
792  ELOG("xkb_keymap_new_from_names(NULL) failed\n");
793  return false;
794  }
795  }
796  new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0);
797  free((char *)names.rules);
798  free((char *)names.model);
799  free((char *)names.layout);
800  free((char *)names.variant);
801  free((char *)names.options);
802  if (new_keymap == NULL) {
803  ELOG("xkb_keymap_new_from_names(RMLVO) failed\n");
804  return false;
805  }
806  }
807  xkb_keymap_unref(xkb_keymap);
808  xkb_keymap = new_keymap;
809 
810  return true;
811 }
812 
813 /*
814  * Returns true if the current config has any binding to a scroll wheel button
815  * (4 or 5) which is a whole-window binding.
816  * We need this to figure out whether we should grab all buttons or just 1-3
817  * when managing a window. See #2049.
818  *
819  */
821  Binding *bind;
823  /* We are only interested in whole window mouse bindings. */
824  if (bind->input_type != B_MOUSE || !bind->whole_window)
825  continue;
826 
827  char *endptr;
828  long button = strtol(bind->symbol + (sizeof("button") - 1), &endptr, 10);
829  if (button == LONG_MAX || button == LONG_MIN || button < 0 || *endptr != '\0' || endptr == bind->symbol) {
830  ELOG("Could not parse button number, skipping this binding. Please report this bug in i3.\n");
831  continue;
832  }
833 
834  /* If the binding is for either scrollwheel button, we need to grab everything. */
835  if (button == 4 || button == 5) {
836  return true;
837  }
838  }
839 
840  return false;
841 }
static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode)
Definition: bindings.c:101
struct modes_head modes
Definition: config.c:18
Binding * configure_binding(const char *bindtype, const char *modifiers, const char *input_code, const char *release, const char *border, const char *whole_window, const char *command, const char *modename, bool pango_markup)
Adds a binding from config parameters given as strings and returns a pointer to the binding structure...
Definition: bindings.c:56
static struct xkb_keymap * xkb_keymap
Definition: bindings.c:15
Binding * get_binding_from_xcb_event(xcb_generic_event_t *event)
Returns a pointer to the Binding that matches the given xcb event or NULL if no such binding exists...
Definition: bindings.c:259
struct Window * window
Definition: data.h:611
char * symbol
Symbol the user specified in configfile, if any.
Definition: data.h:291
static int fill_rmlvo_from_root(struct xkb_rule_names *xkb_names)
Definition: bindings.c:689
Binding * bind
Definition: bindings.c:304
static struct Mode * mode_from_name(const char *name, bool pango_markup)
Definition: bindings.c:30
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
void start_config_error_nagbar(const char *configpath, bool has_errors)
Launch nagbar to indicate errors in the configuration file.
#define TAILQ_EMPTY(head)
Definition: queue.h:344
xcb_window_t root
Definition: main.c:56
void reorder_bindings(void)
Reorders bindings by event_state_mask descendingly so that get_binding() correctly matches more speci...
Definition: bindings.c:547
struct bindings_head * bindings
Definition: main.c:73
void xcb_grab_buttons(xcb_connection_t *conn, xcb_window_t window, bool bind_scrollwheel)
Grab the specified buttons on a window when managing it.
Definition: xcb.c:349
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
unsigned int xcb_numlock_mask
Definition: xcb.c:14
void ipc_send_binding_event(const char *event_type, Binding *bind)
For the binding events, we send the serialized binding struct.
Definition: ipc.c:1297
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
struct xkb_state * xkb_state_no_shift
Definition: bindings.c:313
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:43
Config config
Definition: config.c:17
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition: tree.c:492
xkb_keysym_t keysym
Definition: bindings.c:307
i3Font font
Definition: config.h:94
int xkb_current_group
Definition: handlers.c:24
bool xkb_supported
Definition: main.c:90
bool has_errors
Definition: config.h:33
void start_nagbar(pid_t *nagbar_pid, char *argv[])
Starts an i3-nagbar instance with the given parameters.
Definition: util.c:393
void check_for_duplicate_bindings(struct context *context)
Checks for duplicate key bindings (the same keycode or keysym is configured more than once)...
Definition: bindings.c:564
bool pango_markup
Definition: config.h:81
#define GRAB_KEY(modifier)
#define ELOG(fmt,...)
Definition: libi3.h:93
char * command
Command, like in command mode.
Definition: data.h:301
bool whole_window
If this is true for a mouse binding, the binding should be executed when the button is pressed over a...
Definition: data.h:276
struct xkb_state * xkb_state
Definition: bindings.c:310
input_type_t input_type
Definition: data.h:253
bool bindings_should_grab_scrollwheel_buttons(void)
Returns true if the current config has any binding to a scroll wheel button (4 or 5) which is a whole...
Definition: bindings.c:820
CommandResult * parse_command(const char *input, yajl_gen gen)
Parses and executes the given command.
#define SLIST_FOREACH(var, head, field)
Definition: queue.h:114
#define LOG(fmt,...)
Definition: libi3.h:88
void binding_free(Binding *bind)
Frees the binding.
Definition: bindings.c:628
uint32_t number_keycodes
Definition: data.h:278
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
static struct xkb_context * xkb_context
Definition: bindings.c:14
#define TAILQ_INIT(head)
Definition: queue.h:360
uint32_t keycode
Keycode to bind.
Definition: data.h:281
The configuration file can contain multiple sets of bindings.
Definition: config.h:79
bool load_keymap(void)
Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
Definition: bindings.c:764
enum Binding::@12 release
If true, the binding should be executed upon a KeyRelease event, not a KeyPress (the default)...
#define TAILQ_END(head)
Definition: queue.h:337
uint32_t i3_event_state_mask_t
The lower 16 bits contain a xcb_key_but_mask_t, the higher 16 bits contain an i3_xkb_group_mask_t.
Definition: data.h:119
xcb_window_t id
Definition: data.h:362
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
void ipc_send_event(const char *event, uint32_t message_type, const char *payload)
Sends the specified event to all IPC clients which are currently connected and subscribed to this kin...
Definition: ipc.c:45
Definition: data.h:99
static void reorder_bindings_of_mode(struct Mode *mode)
Definition: bindings.c:505
struct bindings_head * bindings
Definition: config.h:82
static Binding * get_binding(i3_event_state_mask_t state_filtered, bool is_release, uint16_t input_code, input_type_t input_type)
Definition: bindings.c:175
CommandResult * run_binding(Binding *bind, Con *con)
Runs the given binding and handles parse errors.
Definition: bindings.c:646
pid_t command_error_nagbar_pid
Definition: bindings.c:17
i3_event_state_mask_t event_state_mask
Bitmask which is applied against event-&gt;state for KeyPress and KeyRelease events to determine whether...
Definition: data.h:286
#define FREE(pointer)
Definition: util.h:48
void switch_mode(const char *new_mode)
Switches the key bindings to the given mode, if the mode exists.
Definition: bindings.c:466
char * pattern
The pattern/name used to load the font.
Definition: libi3.h:62
#define DLOG(fmt,...)
Definition: libi3.h:98
void regrab_all_buttons(xcb_connection_t *conn)
Release the button grabs on all managed windows and regrab them, reevaluating which buttons need to b...
Definition: bindings.c:154
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:544
#define SLIST_INSERT_HEAD(head, elm, field)
Definition: queue.h:138
A struct that contains useful information about the result of a command as a whole (e...
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...
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
xcb_screen_t * root_screen
Definition: main.c:55
void ungrab_all_keys(xcb_connection_t *conn)
Ungrabs all keys, to be called before re-grabbing the keys because of a mapping_notify event or a con...
Definition: config.c:26
static void add_keycode_if_matches(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
Definition: bindings.c:322
static char * current_mode
void grab_all_keys(xcb_connection_t *conn)
Grab the bound keys (tell X to send us keypress events for those keycodes)
Definition: bindings.c:132
const char * DEFAULT_BINDING_MODE
The name of the default mode.
Definition: bindings.c:23
char * current_configpath
Definition: config.c:16
static int reorder_binding_cmp(const void *a, const void *b)
Definition: bindings.c:493
struct all_cons_head all_cons
Definition: tree.c:17
char * errorfilename
Definition: log.c:38
bool border
If this is true for a mouse binding, the binding should be executed when the button is pressed over t...
Definition: data.h:271
void translate_keysyms(void)
Translates keysymbols to keycodes for all bindings which use keysyms.
Definition: bindings.c:349
xcb_keycode_t * translated_to
Only in use if symbol != NULL.
Definition: data.h:298
i3_event_state_mask_t event_state_from_str(const char *str)
A utility function to convert a string containing the group and modifiers to the corresponding bit ma...
input_type_t
Binding input types.
Definition: data.h:97
static Binding * binding_copy(Binding *bind)
Definition: bindings.c:611
Used during the config file lexing/parsing to keep the state of the lexer in order to provide useful ...
Definition: config.h:32
char * name
Definition: config.h:80
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition: data.h:250