diff --git a/input.h b/input.h index 39a3af3..39ec62a 100644 --- a/input.h +++ b/input.h @@ -45,10 +45,11 @@ typedef enum { } mouse_keysym_t; #define IME_STR_MAX 200 // TODO: check if this limit is sensible +#define KEYBOARD_KEY_TOTAL typedef struct { uint8_t keyboard_state; // mostly used to check if we are using the IME - uint8_t keys[256]; // the state of the full keyboard + uint8_t keys[KEYBOARD_KEY_TOTAL]; // the state of the full keyboard uint8_t mouse_state; int32_t mouse_x; int32_t mouse_y; diff --git a/wayland.c b/wayland.c index 60aa62e..a86066e 100644 --- a/wayland.c +++ b/wayland.c @@ -197,16 +197,28 @@ void keymap_callback(void *data, struct wl_keyboard *kb, uint format, int fd, ui fprintf(stderr, "Could not initialize XKB state machine\n"); } -void enter_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_surface* surface, struct wl_array *keys) +void enter_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_surface* surface, struct wl_array *arr) { fprintf(stdout, "KBMAP with serial %d entered surface\n", serial); - user_input.keyboard_state = KEYBOARD_STATE_UNFOCUSED; + user_input.keyboard_state = KEYBOARD_STATE_FOCUSED; + + // NOTE: wl_array is a dynamic array as specified in wayland-util.h + int32_t *pos = arr->data; + // wl_keyboard is giving us all the currenly pressed keys anyways + for (int i = 0; i < KEYBOARD_KEY_TOTAL) + user_input.keys[i] = KEY_STATE_UNPRESSED; + + wl_array_for_each(pos, arr) { + xkb_keysym_t sym = xkb_state_key_get_one_sym(xkb_state, (*pos) + 8); + // TODO: Decide if we should also set KEY_STATE_HELD + user_input.keys[sym] = KEY_STATE_PRESSED; + } } void leave_callback(void *data, struct wl_keyboard* kb, uint serial, struct wl_surface* surface) { fprintf(stdout, "KBMAP with serial %d left surface\n", serial); - user_input.keyboard_state = KEYBOARD_STATE_FOCUSED; + user_input.keyboard_state = KEYBOARD_STATE_UNFOCUSED; } void key_callback(void *data, struct wl_keyboard *kb, uint serial, uint time, uint key, uint state)