Move keyboard binding accessor to bindings.[ch]
Rename `get_binding` to `get_keyboard_binding` and ensure that this function only accesses bindings of type B_KEYBOARD. Other types of bindings (e.g. mouse bindings) will be accessed by a different function.
This commit is contained in:
parent
0f6c411f06
commit
3d6d0c134c
@ -29,3 +29,10 @@ Binding *configure_binding(const char *bindtype, const char *modifiers, const ch
|
||||
*
|
||||
*/
|
||||
void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch);
|
||||
|
||||
/**
|
||||
* Returns a pointer to the keyboard Binding with the specified modifiers and
|
||||
* keycode or NULL if no such binding exists.
|
||||
*
|
||||
*/
|
||||
Binding *get_keyboard_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode);
|
||||
|
@ -333,13 +333,6 @@ void switch_mode(const char *new_mode);
|
||||
*
|
||||
*/void update_barconfig();
|
||||
|
||||
/**
|
||||
* Returns a pointer to the Binding with the specified modifiers and keycode
|
||||
* or NULL if no such binding exists.
|
||||
*
|
||||
*/
|
||||
Binding *get_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode);
|
||||
|
||||
/**
|
||||
* Kills the configerror i3-nagbar process, if any.
|
||||
*
|
||||
|
@ -117,3 +117,65 @@ void grab_all_keys(xcb_connection_t *conn, bool bind_mode_switch) {
|
||||
grab_keycode_for_binding(conn, bind, *walk++);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a pointer to the keyboard Binding with the specified modifiers and
|
||||
* keycode or NULL if no such binding exists.
|
||||
*
|
||||
*/
|
||||
Binding *get_keyboard_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode) {
|
||||
Binding *bind;
|
||||
|
||||
if (!key_release) {
|
||||
/* On a KeyPress event, we first reset all
|
||||
* B_UPON_KEYRELEASE_IGNORE_MODS bindings back to B_UPON_KEYRELEASE */
|
||||
TAILQ_FOREACH(bind, bindings, bindings) {
|
||||
if (bind->input_type != B_KEYBOARD)
|
||||
continue;
|
||||
if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
|
||||
bind->release = B_UPON_KEYRELEASE;
|
||||
}
|
||||
}
|
||||
|
||||
TAILQ_FOREACH(bind, bindings, bindings) {
|
||||
/* First compare the modifiers (unless this is a
|
||||
* B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
|
||||
* event) */
|
||||
if (bind->input_type != B_KEYBOARD)
|
||||
continue;
|
||||
if (bind->mods != modifiers &&
|
||||
(bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
|
||||
!key_release))
|
||||
continue;
|
||||
|
||||
/* If a symbol was specified by the user, we need to look in
|
||||
* the array of translated keycodes for the event’s keycode */
|
||||
if (bind->symbol != NULL) {
|
||||
if (memmem(bind->translated_to,
|
||||
bind->number_keycodes * sizeof(xcb_keycode_t),
|
||||
&keycode, sizeof(xcb_keycode_t)) == NULL)
|
||||
continue;
|
||||
} else {
|
||||
/* This case is easier: The user specified a keycode */
|
||||
if (bind->keycode != keycode)
|
||||
continue;
|
||||
}
|
||||
|
||||
/* If this keybinding is a KeyRelease binding, it matches the key which
|
||||
* the user pressed. We therefore mark it as
|
||||
* B_UPON_KEYRELEASE_IGNORE_MODS for later, so that the user can
|
||||
* release the modifiers before the actual key and the KeyRelease will
|
||||
* still be matched. */
|
||||
if (bind->release == B_UPON_KEYRELEASE && !key_release)
|
||||
bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
|
||||
|
||||
/* Check if the binding is for a KeyPress or a KeyRelease event */
|
||||
if ((bind->release == B_UPON_KEYPRESS && key_release) ||
|
||||
(bind->release >= B_UPON_KEYRELEASE && !key_release))
|
||||
continue;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return (bind == TAILQ_END(bindings) ? NULL : bind);
|
||||
}
|
||||
|
58
src/config.c
58
src/config.c
@ -30,64 +30,6 @@ void ungrab_all_keys(xcb_connection_t *conn) {
|
||||
xcb_ungrab_key(conn, XCB_GRAB_ANY, root, XCB_BUTTON_MASK_ANY);
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a pointer to the Binding with the specified modifiers and keycode
|
||||
* or NULL if no such binding exists.
|
||||
*
|
||||
*/
|
||||
Binding *get_binding(uint16_t modifiers, bool key_release, xcb_keycode_t keycode) {
|
||||
Binding *bind;
|
||||
|
||||
if (!key_release) {
|
||||
/* On a KeyPress event, we first reset all
|
||||
* B_UPON_KEYRELEASE_IGNORE_MODS bindings back to B_UPON_KEYRELEASE */
|
||||
TAILQ_FOREACH(bind, bindings, bindings) {
|
||||
if (bind->release == B_UPON_KEYRELEASE_IGNORE_MODS)
|
||||
bind->release = B_UPON_KEYRELEASE;
|
||||
}
|
||||
}
|
||||
|
||||
TAILQ_FOREACH(bind, bindings, bindings) {
|
||||
/* First compare the modifiers (unless this is a
|
||||
* B_UPON_KEYRELEASE_IGNORE_MODS binding and this is a KeyRelease
|
||||
* event) */
|
||||
if (bind->mods != modifiers &&
|
||||
(bind->release != B_UPON_KEYRELEASE_IGNORE_MODS ||
|
||||
!key_release))
|
||||
continue;
|
||||
|
||||
/* If a symbol was specified by the user, we need to look in
|
||||
* the array of translated keycodes for the event’s keycode */
|
||||
if (bind->symbol != NULL) {
|
||||
if (memmem(bind->translated_to,
|
||||
bind->number_keycodes * sizeof(xcb_keycode_t),
|
||||
&keycode, sizeof(xcb_keycode_t)) == NULL)
|
||||
continue;
|
||||
} else {
|
||||
/* This case is easier: The user specified a keycode */
|
||||
if (bind->keycode != keycode)
|
||||
continue;
|
||||
}
|
||||
|
||||
/* If this keybinding is a KeyRelease binding, it matches the key which
|
||||
* the user pressed. We therefore mark it as
|
||||
* B_UPON_KEYRELEASE_IGNORE_MODS for later, so that the user can
|
||||
* release the modifiers before the actual key and the KeyRelease will
|
||||
* still be matched. */
|
||||
if (bind->release == B_UPON_KEYRELEASE && !key_release)
|
||||
bind->release = B_UPON_KEYRELEASE_IGNORE_MODS;
|
||||
|
||||
/* Check if the binding is for a KeyPress or a KeyRelease event */
|
||||
if ((bind->release == B_UPON_KEYPRESS && key_release) ||
|
||||
(bind->release >= B_UPON_KEYRELEASE && !key_release))
|
||||
continue;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return (bind == TAILQ_END(bindings) ? NULL : bind);
|
||||
}
|
||||
|
||||
/*
|
||||
* Translates keysymbols to keycodes for all bindings which use keysyms.
|
||||
*
|
||||
|
@ -84,7 +84,7 @@ void handle_key_press(xcb_key_press_event_t *event) {
|
||||
DLOG("(checked mode_switch, state %d)\n", state_filtered);
|
||||
|
||||
/* Find the binding */
|
||||
Binding *bind = get_binding(state_filtered, key_release, event->detail);
|
||||
Binding *bind = get_keyboard_binding(state_filtered, key_release, event->detail);
|
||||
|
||||
/* No match? Then the user has Mode_switch enabled but does not have a
|
||||
* specific keybinding. Fall back to the default keybindings (without
|
||||
@ -93,7 +93,7 @@ void handle_key_press(xcb_key_press_event_t *event) {
|
||||
if (bind == NULL) {
|
||||
state_filtered &= ~(BIND_MODE_SWITCH);
|
||||
DLOG("no match, new state_filtered = %d\n", state_filtered);
|
||||
if ((bind = get_binding(state_filtered, key_release, event->detail)) == NULL) {
|
||||
if ((bind = get_keyboard_binding(state_filtered, key_release, event->detail)) == NULL) {
|
||||
/* This is not a real error since we can have release and
|
||||
* non-release keybindings. On a KeyPress event for which there is
|
||||
* only a !release-binding, but no release-binding, the
|
||||
|
Loading…
Reference in New Issue
Block a user