gri3-wm/src/key_press.c

53 lines
1.6 KiB
C
Raw Normal View History

/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
* key_press.c: key press handler
*
*/
#include "all.h"
2020-05-11 17:44:53 +02:00
#include "guile.h"
2020-05-12 14:21:16 +02:00
#include <xkbcommon/xkbcommon.h>
/*
* There was a KeyPress or KeyRelease (both events have the same fields). We
* compare this key code with our bindings table and pass the bound action to
* parse_command().
*
*/
void handle_key_press(xcb_key_press_event_t *event) {
Use libxkbcommon for translating keysyms, support all XKB groups. fixes #1835 This commit improves the translation of keysyms to keycodes by loading keymaps using libxkbcommon-x11 and using libxkbcommon for figuring out the keymap, depending on each keybinding’s modifiers. This way, the upper layers of complex layouts are now usable with i3’s bindsym directive, such as de_neo’s layer 3 and higher. Furthermore, the commit generalizes the handling of different XKB groups. We formerly had support only for two separate groups, the default group 1, and group 2. While Mode_switch is only one way to switch to group 2, we called the binding option Mode_switch. With this commit, the new names Group1, Group2 (an alias for Mode_switch), Group3 and Group4 are introduced for configuring bindings. This is only useful for advanced keyboard layouts, such as people loading two keyboard layouts and switching between them (us, ru seems to be a popular combination). When grabbing keys, one can only specify the modifier mask, but not an XKB state mask (or value), so we still dynamically unbind and re-bind keys whenever the XKB group changes. The commit was manually tested using the following i3 config: bindsym Group4+n nop heya from group 4 bindsym Group3+n nop heya from group 3 bindsym Group2+n nop heya from group 2 bindsym n nop heya bindsym shift+N nop explicit shift binding bindsym shift+r nop implicit shift binding bindcode Group2+38 nop fallback overwritten in group 2 only bindcode 38 nop fallback …with the following layout: setxkbmap -layout "us,ua,ru,de" -variant ",winkeys,,neo" \ -option "grp:shift_caps_toggle,grp_led:scroll" \ -model pc104 -rules evdev By default (xkb group 1, us layout), pressing “n” will result in the “heya” message appearing. Pressing “a” will result in the “fallback” message appearing. “j” is not triggered. By pressing Shift+CapsLock you switch to the next group (xkb group 2, ua layout). Pressing “a” will result in the “fallback overwritten in group 2 only” message, pressing “n” will still result in “heya”. “j” is not triggered. In the next group (xkb group 3, ru layout), pressing “a” will result in the “fallback” message again, pressing “n” will result in “heya”, “j” is not triggered. In the last group (xkb group 4, de_neo layout), pressing “a” will still result in “fallback”, pressing “n” will result in “heya”, pressing “j” will result in “heya from group 4”. Pressing shift+n results in “explicit shift binding”, pressing shift+r results in “implicit shift binding”. This ensures that keysym translation falls back to looking at non-shift keys (“r” can be used instead of ”R”) and that the order of keybindings doesn’t play a role (“bindsym n” does not override “bindsym shift+n”, even though it’s specified earlier in the config). The fallback behavior ensures use-cases such as ticket #1775 are still covered. Only binding keys when the X server is in the corresponding XKB group ensures use-cases such as ticket #585 are still covered.
2015-08-23 22:49:32 +02:00
const bool key_release = (event->response_type == XCB_KEY_RELEASE);
last_timestamp = event->time;
2020-05-12 14:21:16 +02:00
/* printf("%s %d, state raw = 0x%x\n", (key_release ? "KeyRelease" : "KeyPress"), event->detail, event->state); */
2020-05-12 14:56:06 +02:00
/* int col = (event->state & XCB_MOD_MASK_SHIFT); */
int col = (event->state & XCB_MOD_MASK_ANY);
2020-05-12 14:21:16 +02:00
xcb_keysym_t sym = xcb_key_press_lookup_keysym(keysyms, event, col);
if (sym == XKB_KEY_NoSymbol) {
ELOG("Could not translate string to key symbol: \"%s\"\n", sym);
return;
}
char buf[7];
xkb_keysym_to_utf8(sym, buf, 7);
2020-05-11 17:44:53 +02:00
// Let guile resolve the binding for us
guile_hook("key-press-hook",
scm_list_3(scm_from_int(event->response_type),
2020-05-12 14:21:16 +02:00
scm_from_int(event->state),
scm_from_utf8_string(buf)));
2020-05-11 17:44:53 +02:00
/* Binding *bind = get_binding_from_xcb_event((xcb_generic_event_t *)event); */
/* /\* if we couldn't find a binding, we are done *\/ */
/* if (bind == NULL) */
/* return; */
2020-05-11 17:44:53 +02:00
/* CommandResult *result = run_binding(bind, NULL); */
/* command_result_free(result); */
}