From 2fccff16eb48fbf57f9ad237e3dc748676d9fb03 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 2 Oct 2017 19:23:30 +0200 Subject: [PATCH] Begin modifiers support --- include/wlr/types/wlr_keyboard.h | 26 +++++++++++++-- types/wlr_keyboard.c | 56 ++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index af205c63..97517cbe 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -6,13 +6,25 @@ #include #include -enum WLR_KEYBOARD_LED { +enum wlr_keyboard_led { WLR_LED_NUM_LOCK = 1, WLR_LED_CAPS_LOCK = 2, WLR_LED_SCROLL_LOCK = 4, WLR_LED_LAST }; +enum wlr_keyboard_modifier { + WLR_MODIFIER_SHIFT = 1, + WLR_MODIFIER_CAPS = 2, + WLR_MODIFIER_CTRL = 4, + WLR_MODIFIER_ALT = 8, + WLR_MODIFIER_MOD2 = 16, + WLR_MODIFIER_MOD3 = 32, + WLR_MODIFIER_LOGO = 64, + WLR_MODIFIER_MOD5 = 128, + WLR_MODIFIER_LAST +}; + struct wlr_keyboard_impl; struct wlr_keyboard { @@ -23,7 +35,15 @@ struct wlr_keyboard { size_t keymap_size; struct xkb_keymap *keymap; struct xkb_state *xkb_state; - xkb_led_index_t leds[WLR_LED_LAST]; + xkb_led_index_t led_indexes[WLR_LED_LAST]; + xkb_mod_index_t mod_indexes[WLR_MODIFIER_LAST]; + + struct { + xkb_mod_mask_t depressed; + xkb_mod_mask_t latched; + xkb_mod_mask_t locked; + xkb_mod_mask_t group; + } modifiers; struct { struct wl_signal key; @@ -48,6 +68,6 @@ struct wlr_event_keyboard_key { }; void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, - struct xkb_keymap *keymap); + struct xkb_keymap *keymap); #endif diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index b356f63a..dc3b8f8e 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -13,19 +13,45 @@ int os_create_anonymous_file(off_t size); static void keyboard_led_update(struct wlr_keyboard *keyboard) { uint32_t leds = 0; for (uint32_t i = 0; i < WLR_LED_LAST; ++i) { - if (xkb_state_led_index_is_active(keyboard->xkb_state, keyboard->leds[i])) { + if (xkb_state_led_index_is_active(keyboard->xkb_state, + keyboard->led_indexes[i])) { leds |= (1 << i); } } wlr_keyboard_led_update(keyboard, leds); } +static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { + xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_DEPRESSED); + xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_LATCHED); + xkb_mod_mask_t locked = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_LOCKED); + xkb_mod_mask_t group = xkb_state_serialize_layout(keyboard->xkb_state, + XKB_STATE_LAYOUT_EFFECTIVE); + if (depressed == keyboard->modifiers.depressed && + latched == keyboard->modifiers.latched && + locked == keyboard->modifiers.locked && + group == keyboard->modifiers.group) { + return; + } + + keyboard->modifiers.depressed = depressed; + keyboard->modifiers.latched = latched; + keyboard->modifiers.locked = locked; + keyboard->modifiers.group = group; + + // TODO: wl_keyboard_send_modifiers +} + void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, struct wlr_event_keyboard_key *event) { uint32_t keycode = event->keycode + 8; xkb_state_update_key(keyboard->xkb_state, keycode, - event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); + event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); keyboard_led_update(keyboard); + keyboard_modifier_update(keyboard); wl_signal_emit(&keyboard->events.key, event); } @@ -57,21 +83,39 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, wlr_log(L_DEBUG, "Keymap set"); kb->keymap = keymap; assert(kb->xkb_state = xkb_state_new(kb->keymap)); - const char *led_names[3] = { + + const char *led_names[WLR_LED_LAST] = { XKB_LED_NAME_NUM, XKB_LED_NAME_CAPS, XKB_LED_NAME_SCROLL }; - for (uint32_t i = 0; i < 3; ++i) { - kb->leds[i] = xkb_map_led_get_index(kb->keymap, led_names[i]); + for (size_t i = 0; i < WLR_LED_LAST; ++i) { + kb->led_indexes[i] = xkb_map_led_get_index(kb->keymap, led_names[i]); } + + const char *mod_names[WLR_MODIFIER_LAST] = { + XKB_MOD_NAME_SHIFT, + XKB_MOD_NAME_CAPS, + XKB_MOD_NAME_CTRL, // "Control" + XKB_MOD_NAME_ALT, // "Mod1" + XKB_MOD_NAME_NUM, // "Mod2" + "Mod3", + XKB_MOD_NAME_LOGO, // "Mod4" + "Mod5", + }; + // TODO: there's also "Ctrl", "Alt"? + for (size_t i = 0; i < WLR_LED_LAST; ++i) { + kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]); + } + char *keymap_str = xkb_keymap_get_as_string(kb->keymap, XKB_KEYMAP_FORMAT_TEXT_V1); kb->keymap_size = strlen(keymap_str) + 1; kb->keymap_fd = os_create_anonymous_file(kb->keymap_size); void *ptr = mmap(NULL, kb->keymap_size, - PROT_READ | PROT_WRITE, MAP_SHARED, kb->keymap_fd, 0); + PROT_READ | PROT_WRITE, MAP_SHARED, kb->keymap_fd, 0); strcpy(ptr, keymap_str); free(keymap_str); + wl_signal_emit(&kb->events.keymap, kb); }