- bind->number_keycodes++;
- bind->translated_to = srealloc(bind->translated_to,
- (sizeof(xcb_keycode_t) *
- bind->number_keycodes));
- bind->translated_to[bind->number_keycodes - 1] = key;
+
+#define ADD_TRANSLATED_KEY(mods) \
+ do { \
+ struct Binding_Keycode *binding_keycode = smalloc(sizeof(struct Binding_Keycode)); \
+ binding_keycode->modifiers = (mods); \
+ binding_keycode->keycode = key; \
+ TAILQ_INSERT_TAIL(&(bind->keycodes_head), binding_keycode, keycodes); \
+ } while (0)
+
+ ADD_TRANSLATED_KEY(bind->event_state_mask);
+
+ /* Also bind the key with active CapsLock */
+ ADD_TRANSLATED_KEY(bind->event_state_mask | XCB_MOD_MASK_LOCK);
+
+ /* If this binding is not explicitly for NumLock, check whether we need to
+ * add a fallback. */
+ if ((bind->event_state_mask & xcb_numlock_mask) != xcb_numlock_mask) {
+ /* Check whether the keycode results in the same keysym when NumLock is
+ * active. If so, grab the key with NumLock as well, so that users don’t
+ * need to duplicate every key binding with an additional Mod2 specified.
+ */
+ xkb_keysym_t sym_numlock = xkb_state_key_get_one_sym(numlock_state, key);
+ if (sym_numlock == resolving->keysym) {
+ /* Also bind the key with active NumLock */
+ ADD_TRANSLATED_KEY(bind->event_state_mask | xcb_numlock_mask);
+
+ /* Also bind the key with active NumLock+CapsLock */
+ ADD_TRANSLATED_KEY(bind->event_state_mask | xcb_numlock_mask | XCB_MOD_MASK_LOCK);
+ } else {
+ DLOG("Skipping automatic numlock fallback, key %d resolves to 0x%x with numlock\n",
+ key, sym_numlock);
+ }
+ }
+
+#undef ADD_TRANSLATED_KEY