2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * bindings.c: Functions for configuring, finding and, running bindings.
11 #include <xkbcommon/xkbcommon.h>
12 #include <xkbcommon/xkbcommon-x11.h>
14 static struct xkb_context *xkb_context;
15 static struct xkb_keymap *xkb_keymap;
17 pid_t command_error_nagbar_pid = -1;
20 * The name of the default mode.
23 const char *DEFAULT_BINDING_MODE = "default";
26 * Returns the mode specified by `name` or creates a new mode and adds it to
30 static struct Mode *mode_from_name(const char *name, bool pango_markup) {
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) {
40 /* If the mode was not found, create a new one */
41 mode = scalloc(1, sizeof(struct Mode));
42 mode->name = sstrdup(name);
43 mode->pango_markup = pango_markup;
44 mode->bindings = scalloc(1, sizeof(struct bindings_head));
45 TAILQ_INIT(mode->bindings);
46 SLIST_INSERT_HEAD(&modes, mode, modes);
52 * Adds a binding from config parameters given as strings and returns a
53 * pointer to the binding structure. Returns NULL if the input code could not
57 Binding *configure_binding(const char *bindtype, const char *modifiers, const char *input_code,
58 const char *release, const char *border, const char *whole_window,
59 const char *exclude_titlebar, const char *command, const char *modename,
61 Binding *new_binding = scalloc(1, sizeof(Binding));
62 DLOG("Binding %p bindtype %s, modifiers %s, input code %s, release %s\n", new_binding, bindtype, modifiers, input_code, release);
63 new_binding->release = (release != NULL ? B_UPON_KEYRELEASE : B_UPON_KEYPRESS);
64 new_binding->border = (border != NULL);
65 new_binding->whole_window = (whole_window != NULL);
66 new_binding->exclude_titlebar = (exclude_titlebar != NULL);
67 if (strcmp(bindtype, "bindsym") == 0) {
68 new_binding->input_type = (strncasecmp(input_code, "button", (sizeof("button") - 1)) == 0
72 new_binding->symbol = sstrdup(input_code);
75 if (!parse_long(input_code, &keycode, 10)) {
76 ELOG("Could not parse \"%s\" as an input code, ignoring this binding.\n", input_code);
81 new_binding->keycode = keycode;
82 new_binding->input_type = B_KEYBOARD;
84 new_binding->command = sstrdup(command);
85 new_binding->event_state_mask = event_state_from_str(modifiers);
86 int group_bits_set = 0;
87 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_1)
89 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
91 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
93 if ((new_binding->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
95 if (group_bits_set > 1)
96 ELOG("Keybinding has more than one Group specified, but your X server is always in precisely one group. The keybinding can never trigger.\n");
98 struct Mode *mode = mode_from_name(modename, pango_markup);
99 TAILQ_INSERT_TAIL(mode->bindings, new_binding, bindings);
101 TAILQ_INIT(&(new_binding->keycodes_head));
106 static bool binding_in_current_group(const Binding *bind) {
107 /* If no bits are set, the binding should be installed in every group. */
108 if ((bind->event_state_mask >> 16) == I3_XKB_GROUP_MASK_ANY)
110 switch (xkb_current_group) {
111 case XCB_XKB_GROUP_1:
112 return ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_1);
113 case XCB_XKB_GROUP_2:
114 return ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2);
115 case XCB_XKB_GROUP_3:
116 return ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3);
117 case XCB_XKB_GROUP_4:
118 return ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4);
120 ELOG("BUG: xkb_current_group (= %d) outside of [XCB_XKB_GROUP_1..XCB_XKB_GROUP_4]\n", xkb_current_group);
125 static void grab_keycode_for_binding(xcb_connection_t *conn, Binding *bind, uint32_t keycode) {
126 /* Grab the key in all combinations */
127 #define GRAB_KEY(modifier) \
129 xcb_grab_key(conn, 0, root, modifier, keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC); \
131 const int mods = (bind->event_state_mask & 0xFFFF);
132 DLOG("Binding %p Grabbing keycode %d with event state mask 0x%x (mods 0x%x)\n",
133 bind, keycode, bind->event_state_mask, mods);
135 /* Also bind the key with active NumLock */
136 GRAB_KEY(mods | xcb_numlock_mask);
137 /* Also bind the key with active CapsLock */
138 GRAB_KEY(mods | XCB_MOD_MASK_LOCK);
139 /* Also bind the key with active NumLock+CapsLock */
140 GRAB_KEY(mods | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
144 * Grab the bound keys (tell X to send us keypress events for those keycodes)
147 void grab_all_keys(xcb_connection_t *conn) {
149 TAILQ_FOREACH(bind, bindings, bindings) {
150 if (bind->input_type != B_KEYBOARD)
153 if (!binding_in_current_group(bind))
156 /* The easy case: the user specified a keycode directly. */
157 if (bind->keycode > 0) {
158 grab_keycode_for_binding(conn, bind, bind->keycode);
162 struct Binding_Keycode *binding_keycode;
163 TAILQ_FOREACH(binding_keycode, &(bind->keycodes_head), keycodes) {
164 const int keycode = binding_keycode->keycode;
165 const int mods = (binding_keycode->modifiers & 0xFFFF);
166 DLOG("Binding %p Grabbing keycode %d with mods %d\n", bind, keycode, mods);
167 xcb_grab_key(conn, 0, root, mods, keycode, XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
173 * Release the button grabs on all managed windows and regrab them,
174 * reevaluating which buttons need to be grabbed.
177 void regrab_all_buttons(xcb_connection_t *conn) {
178 int *buttons = bindings_get_buttons_to_grab();
179 xcb_grab_server(conn);
182 TAILQ_FOREACH(con, &all_cons, all_cons) {
183 if (con->window == NULL)
186 xcb_ungrab_button(conn, XCB_BUTTON_INDEX_ANY, con->window->id, XCB_BUTTON_MASK_ANY);
187 xcb_grab_buttons(conn, con->window->id, buttons);
191 xcb_ungrab_server(conn);
194 static bool modifiers_match(const uint32_t modifiers_mask, const uint32_t modifiers_state) {
195 /* modifiers_mask is a special case: a value of 0 does not mean “match
196 * all”, but rather “match exactly when no modifiers are present”. */
197 if (modifiers_mask == 0) {
198 /* Verify no modifiers are pressed. A bitwise AND would lead to
199 * false positives, see issue #2002. */
200 return (modifiers_state == 0);
202 return ((modifiers_state & modifiers_mask) == modifiers_mask);
206 * Returns a pointer to the Binding with the specified modifiers and
207 * keycode or NULL if no such binding exists.
210 static Binding *get_binding(i3_event_state_mask_t state_filtered, bool is_release, uint16_t input_code, input_type_t input_type) {
214 /* On a press event, we first reset all B_UPON_KEYRELEASE_IGNORE_MODS
215 * bindings back to B_UPON_KEYRELEASE */
216 TAILQ_FOREACH(bind, bindings, bindings) {
217 if (bind->input_type != input_type)
219 if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
220 bind->release = B_UPON_KEYRELEASE;
224 const uint32_t xkb_group_state = (state_filtered & 0xFFFF0000);
225 const uint32_t modifiers_state = (state_filtered & 0x0000FFFF);
226 TAILQ_FOREACH(bind, bindings, bindings) {
227 if (bind->input_type != input_type) {
231 const uint32_t xkb_group_mask = (bind->event_state_mask & 0xFFFF0000);
232 const bool groups_match = ((xkb_group_state & xkb_group_mask) == xkb_group_mask);
234 DLOG("skipping binding %p because XKB groups do not match\n", bind);
238 /* For keyboard bindings where a symbol was specified by the user, we
239 * need to look in the array of translated keycodes for the event’s
241 if (input_type == B_KEYBOARD && bind->symbol != NULL) {
242 xcb_keycode_t input_keycode = (xcb_keycode_t)input_code;
243 bool found_keycode = false;
244 struct Binding_Keycode *binding_keycode;
245 TAILQ_FOREACH(binding_keycode, &(bind->keycodes_head), keycodes) {
246 const uint32_t modifiers_mask = (binding_keycode->modifiers & 0x0000FFFF);
247 const bool mods_match = modifiers_match(modifiers_mask, modifiers_state);
248 DLOG("binding_keycode->modifiers = %d, modifiers_mask = %d, modifiers_state = %d, mods_match = %s\n",
249 binding_keycode->modifiers, modifiers_mask, modifiers_state, (mods_match ? "yes" : "no"));
250 if (binding_keycode->keycode == input_keycode && mods_match) {
251 found_keycode = true;
255 if (!found_keycode) {
259 /* This case is easier: The user specified a keycode */
260 if (bind->keycode != input_code) {
264 bool found_keycode = false;
265 struct Binding_Keycode *binding_keycode;
266 TAILQ_FOREACH(binding_keycode, &(bind->keycodes_head), keycodes) {
267 const uint32_t modifiers_mask = (binding_keycode->modifiers & 0x0000FFFF);
268 const bool mods_match = modifiers_match(modifiers_mask, modifiers_state);
269 DLOG("binding_keycode->modifiers = %d, modifiers_mask = %d, modifiers_state = %d, mods_match = %s\n",
270 binding_keycode->modifiers, modifiers_mask, modifiers_state, (mods_match ? "yes" : "no"));
271 if (mods_match || (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS && is_release)) {
272 found_keycode = true;
276 if (!found_keycode) {
281 /* If this binding is a release binding, it matches the key which the
282 * user pressed. We therefore mark it as B_UPON_KEYRELEASE_IGNORE_MODS
283 * for later, so that the user can release the modifiers before the
284 * actual key or button and the release event will still be matched. */
285 if (bind->release == B_UPON_KEYRELEASE && !is_release) {
286 bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
287 DLOG("marked bind %p as B_UPON_KEYRELEASE_IGNORE_MODS\n", bind);
288 /* The correct binding has been found, so abort the search, but
289 * also don’t return this binding, since it should not be executed
290 * yet (only when the keys are released). */
291 bind = TAILQ_END(bindings);
295 /* Check if the binding is for a press or a release event */
296 if ((bind->release == B_UPON_KEYPRESS && is_release) ||
297 (bind->release >= B_UPON_KEYRELEASE && !is_release)) {
304 return (bind == TAILQ_END(bindings) ? NULL : bind);
308 * Returns a pointer to the Binding that matches the given xcb button or key
309 * event or NULL if no such binding exists.
312 Binding *get_binding_from_xcb_event(xcb_generic_event_t *event) {
313 const bool is_release = (event->response_type == XCB_KEY_RELEASE ||
314 event->response_type == XCB_BUTTON_RELEASE);
316 const input_type_t input_type = ((event->response_type == XCB_BUTTON_RELEASE ||
317 event->response_type == XCB_BUTTON_PRESS)
321 const uint16_t event_state = ((xcb_key_press_event_t *)event)->state;
322 const uint16_t event_detail = ((xcb_key_press_event_t *)event)->detail;
324 /* Remove the CapsLock bit */
325 i3_event_state_mask_t state_filtered = event_state & ~XCB_MOD_MASK_LOCK;
326 DLOG("(removed capslock, state = 0x%x)\n", state_filtered);
327 /* Transform the keyboard_group from bit 13 and bit 14 into an
328 * i3_xkb_group_mask_t, so that get_binding() can just bitwise AND the
329 * configured bindings against |state_filtered|.
331 * These bits are only set because we set the XKB client flags
332 * XCB_XKB_PER_CLIENT_FLAG_GRABS_USE_XKB_STATE and
333 * XCB_XKB_PER_CLIENT_FLAG_LOOKUP_STATE_WHEN_GRABBED. See also doc/kbproto
335 * http://www.x.org/releases/X11R7.7/doc/kbproto/xkbproto.html#Computing_A_State_Field_from_an_XKB_State */
336 switch ((event_state & 0x6000) >> 13) {
337 case XCB_XKB_GROUP_1:
338 state_filtered |= (I3_XKB_GROUP_MASK_1 << 16);
340 case XCB_XKB_GROUP_2:
341 state_filtered |= (I3_XKB_GROUP_MASK_2 << 16);
343 case XCB_XKB_GROUP_3:
344 state_filtered |= (I3_XKB_GROUP_MASK_3 << 16);
346 case XCB_XKB_GROUP_4:
347 state_filtered |= (I3_XKB_GROUP_MASK_4 << 16);
350 state_filtered &= ~0x6000;
351 DLOG("(transformed keyboard group, state = 0x%x)\n", state_filtered);
352 return get_binding(state_filtered, is_release, event_detail, input_type);
356 /* The binding which we are resolving. */
359 /* |bind|’s keysym (translated to xkb_keysym_t), e.g. XKB_KEY_R. */
362 /* The xkb state built from the user-provided modifiers and group. */
363 struct xkb_state *xkb_state;
365 /* Like |xkb_state|, just without the shift modifier, if shift was specified. */
366 struct xkb_state *xkb_state_no_shift;
368 /* Like |xkb_state|, but with NumLock. */
369 struct xkb_state *xkb_state_numlock;
371 /* Like |xkb_state|, but with NumLock, just without the shift modifier, if shift was specified. */
372 struct xkb_state *xkb_state_numlock_no_shift;
376 * add_keycode_if_matches is called for each keycode in the keymap and will add
377 * the keycode to |data->bind| if the keycode can result in the keysym
381 static void add_keycode_if_matches(struct xkb_keymap *keymap, xkb_keycode_t key, void *data) {
382 const struct resolve *resolving = data;
383 struct xkb_state *numlock_state = resolving->xkb_state_numlock;
384 xkb_keysym_t sym = xkb_state_key_get_one_sym(resolving->xkb_state, key);
385 if (sym != resolving->keysym) {
386 /* Check if Shift was specified, and try resolving the symbol without
387 * shift, so that “bindsym $mod+Shift+a nop” actually works. */
388 const xkb_layout_index_t layout = xkb_state_key_get_layout(resolving->xkb_state, key);
389 if (layout == XKB_LAYOUT_INVALID)
391 if (xkb_state_key_get_level(resolving->xkb_state, key, layout) > 1)
393 /* Skip the Shift fallback for keypad keys, otherwise one cannot bind
394 * KP_1 independent of KP_End. */
395 if (sym >= XKB_KEY_KP_Space && sym <= XKB_KEY_KP_Equal)
397 numlock_state = resolving->xkb_state_numlock_no_shift;
398 sym = xkb_state_key_get_one_sym(resolving->xkb_state_no_shift, key);
399 if (sym != resolving->keysym)
402 Binding *bind = resolving->bind;
404 #define ADD_TRANSLATED_KEY(mods) \
406 struct Binding_Keycode *binding_keycode = smalloc(sizeof(struct Binding_Keycode)); \
407 binding_keycode->modifiers = (mods); \
408 binding_keycode->keycode = key; \
409 TAILQ_INSERT_TAIL(&(bind->keycodes_head), binding_keycode, keycodes); \
412 ADD_TRANSLATED_KEY(bind->event_state_mask);
414 /* Also bind the key with active CapsLock */
415 ADD_TRANSLATED_KEY(bind->event_state_mask | XCB_MOD_MASK_LOCK);
417 /* If this binding is not explicitly for NumLock, check whether we need to
419 if ((bind->event_state_mask & xcb_numlock_mask) != xcb_numlock_mask) {
420 /* Check whether the keycode results in the same keysym when NumLock is
421 * active. If so, grab the key with NumLock as well, so that users don’t
422 * need to duplicate every key binding with an additional Mod2 specified.
424 xkb_keysym_t sym_numlock = xkb_state_key_get_one_sym(numlock_state, key);
425 if (sym_numlock == resolving->keysym) {
426 /* Also bind the key with active NumLock */
427 ADD_TRANSLATED_KEY(bind->event_state_mask | xcb_numlock_mask);
429 /* Also bind the key with active NumLock+CapsLock */
430 ADD_TRANSLATED_KEY(bind->event_state_mask | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
432 DLOG("Skipping automatic numlock fallback, key %d resolves to 0x%x with numlock\n",
437 #undef ADD_TRANSLATED_KEY
441 * Translates keysymbols to keycodes for all bindings which use keysyms.
444 void translate_keysyms(void) {
445 struct xkb_state *dummy_state = xkb_state_new(xkb_keymap);
446 if (dummy_state == NULL) {
447 ELOG("Could not create XKB state, cannot translate keysyms.\n");
451 struct xkb_state *dummy_state_no_shift = xkb_state_new(xkb_keymap);
452 if (dummy_state_no_shift == NULL) {
453 ELOG("Could not create XKB state, cannot translate keysyms.\n");
457 struct xkb_state *dummy_state_numlock = xkb_state_new(xkb_keymap);
458 if (dummy_state_numlock == NULL) {
459 ELOG("Could not create XKB state, cannot translate keysyms.\n");
463 struct xkb_state *dummy_state_numlock_no_shift = xkb_state_new(xkb_keymap);
464 if (dummy_state_numlock_no_shift == NULL) {
465 ELOG("Could not create XKB state, cannot translate keysyms.\n");
469 bool has_errors = false;
471 TAILQ_FOREACH(bind, bindings, bindings) {
472 #define ADD_TRANSLATED_KEY(code, mods) \
474 struct Binding_Keycode *binding_keycode = smalloc(sizeof(struct Binding_Keycode)); \
475 binding_keycode->modifiers = (mods); \
476 binding_keycode->keycode = (code); \
477 TAILQ_INSERT_TAIL(&(bind->keycodes_head), binding_keycode, keycodes); \
480 if (bind->input_type == B_MOUSE) {
482 if (!parse_long(bind->symbol + (sizeof("button") - 1), &button, 10)) {
483 ELOG("Could not translate string to button: \"%s\"\n", bind->symbol);
486 bind->keycode = button;
487 ADD_TRANSLATED_KEY(button, bind->event_state_mask);
491 xkb_layout_index_t group = XCB_XKB_GROUP_1;
492 if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_2)
493 group = XCB_XKB_GROUP_2;
494 else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_3)
495 group = XCB_XKB_GROUP_3;
496 else if ((bind->event_state_mask >> 16) & I3_XKB_GROUP_MASK_4)
497 group = XCB_XKB_GROUP_4;
499 DLOG("Binding %p group = %d, event_state_mask = %d, &2 = %s, &3 = %s, &4 = %s\n",
502 bind->event_state_mask,
503 (bind->event_state_mask & I3_XKB_GROUP_MASK_2) ? "yes" : "no",
504 (bind->event_state_mask & I3_XKB_GROUP_MASK_3) ? "yes" : "no",
505 (bind->event_state_mask & I3_XKB_GROUP_MASK_4) ? "yes" : "no");
506 (void)xkb_state_update_mask(
508 (bind->event_state_mask & 0x1FFF) /* xkb_mod_mask_t base_mods, */,
509 0 /* xkb_mod_mask_t latched_mods, */,
510 0 /* xkb_mod_mask_t locked_mods, */,
511 0 /* xkb_layout_index_t base_group, */,
512 0 /* xkb_layout_index_t latched_group, */,
513 group /* xkb_layout_index_t locked_group, */);
515 (void)xkb_state_update_mask(
516 dummy_state_no_shift,
517 (bind->event_state_mask & 0x1FFF) ^ XCB_KEY_BUT_MASK_SHIFT /* xkb_mod_mask_t base_mods, */,
518 0 /* xkb_mod_mask_t latched_mods, */,
519 0 /* xkb_mod_mask_t locked_mods, */,
520 0 /* xkb_layout_index_t base_group, */,
521 0 /* xkb_layout_index_t latched_group, */,
522 group /* xkb_layout_index_t locked_group, */);
524 (void)xkb_state_update_mask(
526 (bind->event_state_mask & 0x1FFF) | xcb_numlock_mask /* xkb_mod_mask_t base_mods, */,
527 0 /* xkb_mod_mask_t latched_mods, */,
528 0 /* xkb_mod_mask_t locked_mods, */,
529 0 /* xkb_layout_index_t base_group, */,
530 0 /* xkb_layout_index_t latched_group, */,
531 group /* xkb_layout_index_t locked_group, */);
533 (void)xkb_state_update_mask(
534 dummy_state_numlock_no_shift,
535 ((bind->event_state_mask & 0x1FFF) | xcb_numlock_mask) ^ XCB_KEY_BUT_MASK_SHIFT /* xkb_mod_mask_t base_mods, */,
536 0 /* xkb_mod_mask_t latched_mods, */,
537 0 /* xkb_mod_mask_t locked_mods, */,
538 0 /* xkb_layout_index_t base_group, */,
539 0 /* xkb_layout_index_t latched_group, */,
540 group /* xkb_layout_index_t locked_group, */);
542 if (bind->keycode > 0) {
543 /* We need to specify modifiers for the keycode binding (numlock
545 while (!TAILQ_EMPTY(&(bind->keycodes_head))) {
546 struct Binding_Keycode *first = TAILQ_FIRST(&(bind->keycodes_head));
547 TAILQ_REMOVE(&(bind->keycodes_head), first, keycodes);
551 ADD_TRANSLATED_KEY(bind->keycode, bind->event_state_mask);
553 /* Also bind the key with active CapsLock */
554 ADD_TRANSLATED_KEY(bind->keycode, bind->event_state_mask | XCB_MOD_MASK_LOCK);
556 /* If this binding is not explicitly for NumLock, check whether we need to
558 if ((bind->event_state_mask & xcb_numlock_mask) != xcb_numlock_mask) {
559 /* Check whether the keycode results in the same keysym when NumLock is
560 * active. If so, grab the key with NumLock as well, so that users don’t
561 * need to duplicate every key binding with an additional Mod2 specified.
563 xkb_keysym_t sym = xkb_state_key_get_one_sym(dummy_state, bind->keycode);
564 xkb_keysym_t sym_numlock = xkb_state_key_get_one_sym(dummy_state_numlock, bind->keycode);
565 if (sym == sym_numlock) {
566 /* Also bind the key with active NumLock */
567 ADD_TRANSLATED_KEY(bind->keycode, bind->event_state_mask | xcb_numlock_mask);
569 /* Also bind the key with active NumLock+CapsLock */
570 ADD_TRANSLATED_KEY(bind->keycode, bind->event_state_mask | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
572 DLOG("Skipping automatic numlock fallback, key %d resolves to 0x%x with numlock\n",
573 bind->keycode, sym_numlock);
580 /* We need to translate the symbol to a keycode */
581 const xkb_keysym_t keysym = xkb_keysym_from_name(bind->symbol, XKB_KEYSYM_NO_FLAGS);
582 if (keysym == XKB_KEY_NoSymbol) {
583 ELOG("Could not translate string to key symbol: \"%s\"\n",
588 struct resolve resolving = {
591 .xkb_state = dummy_state,
592 .xkb_state_no_shift = dummy_state_no_shift,
593 .xkb_state_numlock = dummy_state_numlock,
594 .xkb_state_numlock_no_shift = dummy_state_numlock_no_shift,
596 while (!TAILQ_EMPTY(&(bind->keycodes_head))) {
597 struct Binding_Keycode *first = TAILQ_FIRST(&(bind->keycodes_head));
598 TAILQ_REMOVE(&(bind->keycodes_head), first, keycodes);
601 xkb_keymap_key_for_each(xkb_keymap, add_keycode_if_matches, &resolving);
602 char *keycodes = sstrdup("");
603 int num_keycodes = 0;
604 struct Binding_Keycode *binding_keycode;
605 TAILQ_FOREACH(binding_keycode, &(bind->keycodes_head), keycodes) {
607 sasprintf(&tmp, "%s %d", keycodes, binding_keycode->keycode);
612 /* check for duplicate bindings */
614 TAILQ_FOREACH(check, bindings, bindings) {
617 if (check->symbol != NULL)
619 if (check->keycode != binding_keycode->keycode ||
620 check->event_state_mask != binding_keycode->modifiers ||
621 check->release != bind->release)
624 ELOG("Duplicate keybinding in config file:\n keysym = %s, keycode = %d, state_mask = 0x%x\n", bind->symbol, check->keycode, bind->event_state_mask);
627 DLOG("state=0x%x, cfg=\"%s\", sym=0x%x → keycodes%s (%d)\n",
628 bind->event_state_mask, bind->symbol, keysym, keycodes, num_keycodes);
631 #undef ADD_TRANSLATED_KEY
634 xkb_state_unref(dummy_state);
635 xkb_state_unref(dummy_state_no_shift);
636 xkb_state_unref(dummy_state_numlock);
637 xkb_state_unref(dummy_state_numlock_no_shift);
640 start_config_error_nagbar(current_configpath, true);
645 * Switches the key bindings to the given mode, if the mode exists
648 void switch_mode(const char *new_mode) {
651 DLOG("Switching to mode %s\n", new_mode);
653 SLIST_FOREACH(mode, &modes, modes) {
654 if (strcasecmp(mode->name, new_mode) != 0)
657 ungrab_all_keys(conn);
658 bindings = mode->bindings;
663 sasprintf(&event_msg, "{\"change\":\"%s\", \"pango_markup\":%s}",
664 mode->name, (mode->pango_markup ? "true" : "false"));
666 ipc_send_event("mode", I3_IPC_EVENT_MODE, event_msg);
672 ELOG("ERROR: Mode not found\n");
675 static int reorder_binding_cmp(const void *a, const void *b) {
676 Binding *first = *((Binding **)a);
677 Binding *second = *((Binding **)b);
678 if (first->event_state_mask < second->event_state_mask) {
680 } else if (first->event_state_mask == second->event_state_mask) {
687 static void reorder_bindings_of_mode(struct Mode *mode) {
688 /* Copy the bindings into an array, so that we can use qsort(3). */
691 TAILQ_FOREACH(current, mode->bindings, bindings) {
694 Binding **tmp = scalloc(n, sizeof(Binding *));
696 TAILQ_FOREACH(current, mode->bindings, bindings) {
700 qsort(tmp, n, sizeof(Binding *), reorder_binding_cmp);
702 struct bindings_head *reordered = scalloc(1, sizeof(struct bindings_head));
703 TAILQ_INIT(reordered);
704 for (int i = 0; i < n; i++) {
706 TAILQ_REMOVE(mode->bindings, current, bindings);
707 TAILQ_INSERT_TAIL(reordered, current, bindings);
710 assert(TAILQ_EMPTY(mode->bindings));
711 /* Free the old bindings_head, which is now empty. */
712 free(mode->bindings);
713 mode->bindings = reordered;
717 * Reorders bindings by event_state_mask descendingly so that get_binding()
718 * correctly matches more specific bindings before more generic bindings. Take
719 * the following binding configuration as an example:
721 * bindsym n nop lower-case n pressed
722 * bindsym Shift+n nop upper-case n pressed
724 * Without reordering, the first binding’s event_state_mask of 0x0 would match
725 * the actual event_stat_mask of 0x1 and hence trigger instead of the second
729 void reorder_bindings(void) {
731 SLIST_FOREACH(mode, &modes, modes) {
732 const bool current_mode = (mode->bindings == bindings);
733 reorder_bindings_of_mode(mode);
735 bindings = mode->bindings;
740 * Checks for duplicate key bindings (the same keycode or keysym is configured
741 * more than once). If a duplicate binding is found, a message is printed to
742 * stderr and the has_errors variable is set to true, which will start
746 void check_for_duplicate_bindings(struct context *context) {
747 Binding *bind, *current;
748 TAILQ_FOREACH(current, bindings, bindings) {
749 TAILQ_FOREACH(bind, bindings, bindings) {
750 /* Abort when we reach the current keybinding, only check the
755 /* Check if the input types are different */
756 if (bind->input_type != current->input_type)
759 /* Check if one is using keysym while the other is using bindsym.
761 if ((bind->symbol == NULL && current->symbol != NULL) ||
762 (bind->symbol != NULL && current->symbol == NULL))
765 /* If bind is NULL, current has to be NULL, too (see above).
766 * If the keycodes differ, it can't be a duplicate. */
767 if (bind->symbol != NULL &&
768 strcasecmp(bind->symbol, current->symbol) != 0)
771 /* Check if the keycodes or modifiers are different. If so, they
772 * can't be duplicate */
773 if (bind->keycode != current->keycode ||
774 bind->event_state_mask != current->event_state_mask ||
775 bind->release != current->release)
778 context->has_errors = true;
779 if (current->keycode != 0) {
780 ELOG("Duplicate keybinding in config file:\n state mask 0x%x with keycode %d, command \"%s\"\n",
781 current->event_state_mask, current->keycode, current->command);
783 ELOG("Duplicate keybinding in config file:\n state mask 0x%x with keysym %s, command \"%s\"\n",
784 current->event_state_mask, current->symbol, current->command);
791 * Creates a dynamically allocated copy of bind.
793 static Binding *binding_copy(Binding *bind) {
794 Binding *ret = smalloc(sizeof(Binding));
796 if (bind->symbol != NULL)
797 ret->symbol = sstrdup(bind->symbol);
798 if (bind->command != NULL)
799 ret->command = sstrdup(bind->command);
800 TAILQ_INIT(&(ret->keycodes_head));
801 struct Binding_Keycode *binding_keycode;
802 TAILQ_FOREACH(binding_keycode, &(bind->keycodes_head), keycodes) {
803 struct Binding_Keycode *ret_binding_keycode = smalloc(sizeof(struct Binding_Keycode));
804 *ret_binding_keycode = *binding_keycode;
805 TAILQ_INSERT_TAIL(&(ret->keycodes_head), ret_binding_keycode, keycodes);
812 * Frees the binding. If bind is null, it simply returns.
814 void binding_free(Binding *bind) {
819 while (!TAILQ_EMPTY(&(bind->keycodes_head))) {
820 struct Binding_Keycode *first = TAILQ_FIRST(&(bind->keycodes_head));
821 TAILQ_REMOVE(&(bind->keycodes_head), first, keycodes);
831 * Runs the given binding and handles parse errors. If con is passed, it will
832 * execute the command binding with that container selected by criteria.
833 * Returns a CommandResult for running the binding's command. Free with
834 * command_result_free().
837 CommandResult *run_binding(Binding *bind, Con *con) {
840 /* We need to copy the binding and command since “reload” may be part of
841 * the command, and then the memory that bind points to may not contain the
842 * same data anymore. */
844 command = sstrdup(bind->command);
846 sasprintf(&command, "[con_id=\"%p\"] %s", con, bind->command);
848 Binding *bind_cp = binding_copy(bind);
849 CommandResult *result = parse_command(command, NULL);
852 if (result->needs_tree_render)
855 if (result->parse_error) {
857 sasprintf(&pageraction, "i3-sensible-pager \"%s\"\n", errorfilename);
859 NULL, /* will be replaced by the executable path */
865 "The configured command for this shortcut could not be run successfully.",
870 start_nagbar(&command_error_nagbar_pid, argv);
874 ipc_send_binding_event("run", bind_cp);
875 binding_free(bind_cp);
880 static int fill_rmlvo_from_root(struct xkb_rule_names *xkb_names) {
881 xcb_intern_atom_reply_t *atom_reply;
882 size_t content_max_words = 256;
884 xcb_window_t root = root_screen->root;
886 atom_reply = xcb_intern_atom_reply(
887 conn, xcb_intern_atom(conn, 0, strlen("_XKB_RULES_NAMES"), "_XKB_RULES_NAMES"), NULL);
888 if (atom_reply == NULL)
891 xcb_get_property_cookie_t prop_cookie;
892 xcb_get_property_reply_t *prop_reply;
893 prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
894 XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
895 prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
896 if (prop_reply == NULL) {
900 if (xcb_get_property_value_length(prop_reply) > 0 && prop_reply->bytes_after > 0) {
901 /* We received an incomplete value. Ask again but with a properly
903 content_max_words += ceil(prop_reply->bytes_after / 4.0);
904 /* Repeat the request, with adjusted size */
906 prop_cookie = xcb_get_property_unchecked(conn, false, root, atom_reply->atom,
907 XCB_GET_PROPERTY_TYPE_ANY, 0, content_max_words);
908 prop_reply = xcb_get_property_reply(conn, prop_cookie, NULL);
909 if (prop_reply == NULL) {
914 if (xcb_get_property_value_length(prop_reply) == 0) {
920 const char *walk = (const char *)xcb_get_property_value(prop_reply);
921 int remaining = xcb_get_property_value_length(prop_reply);
922 for (int i = 0; i < 5 && remaining > 0; i++) {
923 const int len = strnlen(walk, remaining);
927 sasprintf((char **)&(xkb_names->rules), "%.*s", len, walk);
930 sasprintf((char **)&(xkb_names->model), "%.*s", len, walk);
933 sasprintf((char **)&(xkb_names->layout), "%.*s", len, walk);
936 sasprintf((char **)&(xkb_names->variant), "%.*s", len, walk);
939 sasprintf((char **)&(xkb_names->options), "%.*s", len, walk);
942 DLOG("component %d of _XKB_RULES_NAMES is \"%.*s\"\n", i, len, walk);
952 * Loads the XKB keymap from the X11 server and feeds it to xkbcommon.
955 bool load_keymap(void) {
956 if (xkb_context == NULL) {
957 if ((xkb_context = xkb_context_new(0)) == NULL) {
958 ELOG("Could not create xkbcommon context\n");
963 struct xkb_keymap *new_keymap = NULL;
965 if (xkb_supported && (device_id = xkb_x11_get_core_keyboard_device_id(conn)) > -1) {
966 if ((new_keymap = xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, 0)) == NULL) {
967 ELOG("xkb_x11_keymap_new_from_device failed\n");
971 /* Likely there is no XKB support on this server, possibly because it
972 * is a VNC server. */
973 LOG("No XKB / core keyboard device? Assembling keymap from local RMLVO.\n");
974 struct xkb_rule_names names = {
980 if (fill_rmlvo_from_root(&names) == -1) {
981 ELOG("Could not get _XKB_RULES_NAMES atom from root window, falling back to defaults.\n");
982 if ((new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0)) == NULL) {
983 ELOG("xkb_keymap_new_from_names(NULL) failed\n");
987 new_keymap = xkb_keymap_new_from_names(xkb_context, &names, 0);
988 free((char *)names.rules);
989 free((char *)names.model);
990 free((char *)names.layout);
991 free((char *)names.variant);
992 free((char *)names.options);
993 if (new_keymap == NULL) {
994 ELOG("xkb_keymap_new_from_names(RMLVO) failed\n");
998 xkb_keymap_unref(xkb_keymap);
999 xkb_keymap = new_keymap;
1005 * Returns a list of buttons that should be grabbed on a window.
1006 * This list will always contain 1–3, all higher buttons will only be returned
1007 * if there is a whole-window binding for it on some window in the current
1009 * The list is terminated by a 0.
1011 int *bindings_get_buttons_to_grab(void) {
1012 /* Let's make the reasonable assumption that there's no more than 25
1016 int buffer[num_max];
1019 /* We always return buttons 1 through 3. */
1025 TAILQ_FOREACH(bind, bindings, bindings) {
1026 if (num + 1 == num_max)
1029 /* We are only interested in whole window mouse bindings. */
1030 if (bind->input_type != B_MOUSE || !bind->whole_window)
1034 if (!parse_long(bind->symbol + (sizeof("button") - 1), &button, 10)) {
1035 ELOG("Could not parse button number, skipping this binding. Please report this bug in i3.\n");
1039 /* Avoid duplicates. */
1040 for (int i = 0; i < num; i++) {
1041 if (buffer[i] == button)
1045 buffer[num++] = button;
1049 int *buttons = scalloc(num, sizeof(int));
1050 memcpy(buttons, buffer, num * sizeof(int));