From 61e451ea1b36435341d02ae34548bd0ea3abdd57 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sun, 24 Sep 2017 14:12:56 -0400 Subject: [PATCH] Move keyboard logic to wlr_{keyboard,seat} --- backend/libinput/keyboard.c | 2 +- backend/wayland/wl_seat.c | 6 +- examples/compositor.c | 98 ++++++----------------- examples/output-layout.c | 5 ++ examples/pointer.c | 5 ++ examples/rotation.c | 5 ++ examples/shared.c | 5 -- examples/simple.c | 6 ++ examples/tablet.c | 5 ++ examples/touch.c | 5 ++ include/wlr/interfaces/wlr_keyboard.h | 2 + include/wlr/types/wlr_keyboard.h | 14 +++- include/wlr/types/wlr_seat.h | 27 ++++++- types/wlr_keyboard.c | 50 ++++++++++++ types/wlr_seat.c | 109 +++++++++++++++++++++++++- 15 files changed, 257 insertions(+), 87 deletions(-) diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index 1c52a6b8..2a626148 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -68,5 +68,5 @@ void handle_keyboard_key(struct libinput_event *event, wlr_event.state = WLR_KEY_PRESSED; break; } - wl_signal_emit(&wlr_dev->keyboard->events.key, &wlr_event); + wlr_keyboard_update_state(wlr_dev->keyboard, &wlr_event); } diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index 3e6982a0..ba3feb8d 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -128,17 +128,15 @@ static const struct wl_pointer_listener pointer_listener = { static void keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, uint32_t format, int32_t fd, uint32_t size) { - + // TODO: set keymap } static void keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface, struct wl_array *keys) { - } static void keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial, struct wl_surface *surface) { - } static void keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, @@ -151,7 +149,7 @@ static void keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, wlr_event.state = state; wlr_event.time_sec = time / 1000; wlr_event.time_usec = time * 1000; - wl_signal_emit(&dev->keyboard->events.key, &wlr_event); + wlr_keyboard_update_state(dev->keyboard, &wlr_event); } static void keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, diff --git a/examples/compositor.c b/examples/compositor.c index 53b385b7..f4377f23 100644 --- a/examples/compositor.c +++ b/examples/compositor.c @@ -32,9 +32,6 @@ #include "shared.h" #include -// TODO: move to common header? -int os_create_anonymous_file(off_t size); - struct sample_state; struct example_xdg_surface_v6 { @@ -75,13 +72,9 @@ struct sample_state { struct wlr_xdg_shell_v6 *xdg_shell; struct wlr_data_device_manager *data_device_manager; struct wl_resource *focus; - struct wl_listener keyboard_bound; struct wlr_xwayland *xwayland; struct wlr_gamma_control_manager *gamma_control_manager; bool mod_down; - int keymap_fd; - size_t keymap_size; - uint32_t serial; struct motion_context motion_context; @@ -335,61 +328,11 @@ static void handle_keyboard_key(struct keyboard_state *keyboard, uint32_t keycode, xkb_keysym_t sym, enum wlr_key_state key_state) { struct compositor_state *state = keyboard->compositor; struct sample_state *sample = state->data; - - struct wl_resource *res = NULL; - struct wlr_seat_handle *seat_handle = NULL; - wl_list_for_each(res, &sample->wlr_compositor->surfaces, link) { - break; - } - - if (res) { - seat_handle = wlr_seat_handle_for_client(sample->wl_seat, - wl_resource_get_client(res)); - } - - if (res != sample->focus && seat_handle && seat_handle->keyboard) { - struct wl_array keys; - wl_array_init(&keys); - uint32_t serial = wl_display_next_serial(state->display); - wl_keyboard_send_enter(seat_handle->keyboard, serial, res, &keys); - sample->focus = res; - } - - if (seat_handle && seat_handle->keyboard) { - uint32_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_DEPRESSED); - uint32_t latched = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_LATCHED); - uint32_t locked = xkb_state_serialize_mods(keyboard->xkb_state, - XKB_STATE_MODS_LOCKED); - uint32_t group = xkb_state_serialize_layout(keyboard->xkb_state, - XKB_STATE_LAYOUT_EFFECTIVE); - uint32_t modifiers_serial = wl_display_next_serial(state->display); - uint32_t key_serial = wl_display_next_serial(state->display); - wl_keyboard_send_modifiers(seat_handle->keyboard, modifiers_serial, - depressed, latched, locked, group); - wl_keyboard_send_key(seat_handle->keyboard, key_serial, 0, keycode, - key_state); - } - if (sym == XKB_KEY_Super_L || sym == XKB_KEY_Super_R) { sample->mod_down = key_state == WLR_KEY_PRESSED; } } -static void handle_keyboard_bound(struct wl_listener *listener, void *data) { - struct wlr_seat_handle *handle = data; - struct sample_state *state = - wl_container_of(listener, state, keyboard_bound); - - wl_keyboard_send_keymap(handle->keyboard, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, - state->keymap_fd, state->keymap_size); - - if (wl_resource_get_version(handle->keyboard) >= 2) { - wl_keyboard_send_repeat_info(handle->keyboard, 25, 600); - } -} - static struct wlr_xdg_surface_v6 *example_xdg_surface_at( struct sample_state *sample, int lx, int ly) { struct wlr_xdg_surface_v6 *xdg_surface; @@ -550,6 +493,25 @@ static void handle_input_add(struct compositor_state *state, example_config_configure_cursor(sample->config, sample->cursor, sample->compositor); } + + if (device->type == WLR_INPUT_DEVICE_KEYBOARD) { + struct xkb_rule_names rules; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + wlr_log(L_ERROR, "Failed to create XKB context"); + exit(1); + } + wlr_keyboard_set_keymap(device->keyboard, xkb_map_new_from_names( + context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS)); + xkb_context_unref(context); + wlr_seat_attach_keyboard(sample->wl_seat, device); + } } static void handle_output_add(struct output_state *ostate) { @@ -669,35 +631,25 @@ int main(int argc, char *argv[]) { state.wl_seat = wlr_seat_create(compositor.display, "seat0"); assert(state.wl_seat); - state.keyboard_bound.notify = handle_keyboard_bound; - wl_signal_add(&state.wl_seat->events.keyboard_bound, &state.keyboard_bound); wlr_seat_set_capabilities(state.wl_seat, WL_SEAT_CAPABILITY_KEYBOARD | WL_SEAT_CAPABILITY_POINTER | WL_SEAT_CAPABILITY_TOUCH); - struct keyboard_state *kbstate; - wl_list_for_each(kbstate, &compositor.keyboards, link) { - char *keymap = xkb_keymap_get_as_string(kbstate->keymap, - XKB_KEYMAP_FORMAT_TEXT_V1); - state.keymap_size = strlen(keymap); - state.keymap_fd = os_create_anonymous_file(state.keymap_size); - void *ptr = - mmap(NULL, state.keymap_size, PROT_READ | PROT_WRITE, MAP_SHARED, - state.keymap_fd, 0); - strcpy(ptr, keymap); - free(keymap); - break; - } state.xwayland = wlr_xwayland_create(compositor.display, state.wlr_compositor); compositor.keyboard_key_cb = handle_keyboard_key; + if (!wlr_backend_start(compositor.backend)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(compositor.backend); + exit(1); + } + wl_display_run(compositor.display); wl_list_remove(&state.new_xdg_surface_v6.link); wlr_xwayland_destroy(state.xwayland); - close(state.keymap_fd); wlr_seat_destroy(state.wl_seat); wlr_gamma_control_manager_destroy(state.gamma_control_manager); wlr_data_device_manager_destroy(state.data_device_manager); diff --git a/examples/output-layout.c b/examples/output-layout.c index be542630..759bf048 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -203,6 +203,11 @@ int main(int argc, char *argv[]) { wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ABGR8888, cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data); + if (!wlr_backend_start(compositor.backend)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(compositor.backend); + exit(1); + } wl_display_run(compositor.display); wlr_texture_destroy(state.cat_texture); diff --git a/examples/pointer.c b/examples/pointer.c index 2b937540..11f67f9b 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -333,6 +333,11 @@ int main(int argc, char *argv[]) { wlr_cursor_set_xcursor(state.cursor, state.xcursor); compositor_init(&compositor); + if (!wlr_backend_start(compositor.backend)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(compositor.backend); + exit(1); + } wl_display_run(compositor.display); compositor_fini(&compositor); diff --git a/examples/rotation.c b/examples/rotation.c index 2596e492..4f8c7b45 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -146,6 +146,11 @@ int main(int argc, char *argv[]) { wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ABGR8888, cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data); + if (!wlr_backend_start(compositor.backend)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(compositor.backend); + exit(1); + } wl_display_run(compositor.display); wlr_texture_destroy(state.cat_texture); diff --git a/examples/shared.c b/examples/shared.c index 0346c96d..5814a20c 100644 --- a/examples/shared.c +++ b/examples/shared.c @@ -537,11 +537,6 @@ void compositor_init(struct compositor_state *state) { wlr_log(L_INFO, "Running compositor on wayland display '%s'", socket); setenv("_WAYLAND_DISPLAY", socket, true); - if (!wlr_backend_start(state->backend)) { - wlr_log(L_ERROR, "Failed to start backend"); - wlr_backend_destroy(wlr); - exit(1); - } } void compositor_fini(struct compositor_state *state) { diff --git a/examples/simple.c b/examples/simple.c index 7c3cc496..ba1ac289 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "shared.h" @@ -52,6 +53,11 @@ int main() { .output_frame_cb = handle_output_frame, }; compositor_init(&compositor); + if (!wlr_backend_start(compositor.backend)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(compositor.backend); + exit(1); + } wl_display_run(compositor.display); compositor_fini(&compositor); } diff --git a/examples/tablet.c b/examples/tablet.c index 4aec5a72..4b565d3d 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -158,6 +158,11 @@ int main(int argc, char *argv[]) { wlr_log(L_ERROR, "Could not start compositor, OOM"); exit(EXIT_FAILURE); } + if (!wlr_backend_start(compositor.backend)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(compositor.backend); + exit(1); + } wl_display_run(compositor.display); wlr_renderer_destroy(state.renderer); diff --git a/examples/touch.c b/examples/touch.c index 40fc4ca2..db025942 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -120,6 +120,11 @@ int main(int argc, char *argv[]) { wlr_texture_upload_pixels(state.cat_texture, WL_SHM_FORMAT_ARGB8888, cat_tex.width, cat_tex.width, cat_tex.height, cat_tex.pixel_data); + if (!wlr_backend_start(compositor.backend)) { + wlr_log(L_ERROR, "Failed to start backend"); + wlr_backend_destroy(compositor.backend); + exit(1); + } wl_display_run(compositor.display); wlr_texture_destroy(state.cat_texture); diff --git a/include/wlr/interfaces/wlr_keyboard.h b/include/wlr/interfaces/wlr_keyboard.h index 779b302f..ffa99b3b 100644 --- a/include/wlr/interfaces/wlr_keyboard.h +++ b/include/wlr/interfaces/wlr_keyboard.h @@ -10,5 +10,7 @@ struct wlr_keyboard_impl { void wlr_keyboard_init(struct wlr_keyboard *keyboard, struct wlr_keyboard_impl *impl); void wlr_keyboard_destroy(struct wlr_keyboard *keyboard); +void wlr_keyboard_update_state(struct wlr_keyboard *keyboard, + struct wlr_event_keyboard_key *event); #endif diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index ce7d6659..89e44f96 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -1,7 +1,8 @@ #ifndef _WLR_TYPES_KEYBOARD_H #define _WLR_TYPES_KEYBOARD_H -#include #include +#include +#include enum WLR_KEYBOARD_LED { WLR_LED_NUM_LOCK = 1, @@ -14,9 +15,17 @@ struct wlr_keyboard_impl; struct wlr_keyboard { struct wlr_keyboard_impl *impl; + // TODO: Should this store key repeat info too? + + int keymap_fd; + size_t keymap_size; + struct xkb_keymap *keymap; + struct xkb_state *xkb_state; + xkb_led_index_t leds[WLR_LED_LAST]; struct { struct wl_signal key; + struct wl_signal keymap; } events; void *data; @@ -36,4 +45,7 @@ struct wlr_event_keyboard_key { enum wlr_key_state state; }; +void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, + struct xkb_keymap *keymap); + #endif diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index aa17d650..dfb1d398 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -2,6 +2,7 @@ #define _WLR_TYPES_SEAT_H #include #include +#include #include /** @@ -12,6 +13,7 @@ struct wlr_seat_handle { struct wl_resource *wl_resource; struct wlr_seat *wlr_seat; + struct wlr_seat_keyboard *seat_keyboard; struct wl_resource *pointer; struct wl_resource *keyboard; @@ -30,10 +32,20 @@ struct wlr_seat_pointer_state { struct wl_listener focus_resource_destroy_listener; }; +struct wlr_seat_keyboard { + struct wlr_seat *seat; + struct wlr_keyboard *keyboard; + struct wl_listener key; + struct wl_listener keymap; + struct wl_listener destroy; + struct wl_list link; +}; + struct wlr_seat { struct wl_global *wl_global; struct wl_display *display; struct wl_list handles; + struct wl_list keyboards; char *name; uint32_t capabilities; struct wlr_data_device *data_device; @@ -43,7 +55,6 @@ struct wlr_seat { struct { struct wl_signal client_bound; struct wl_signal client_unbound; - struct wl_signal keyboard_bound; } events; void *data; @@ -112,4 +123,18 @@ uint32_t wlr_seat_pointer_send_button(struct wlr_seat *wlr_seat, uint32_t time, void wlr_seat_pointer_send_axis(struct wlr_seat *wlr_seat, uint32_t time, enum wlr_axis_orientation orientation, double value); +/** + * Attaches this keyboard to the seat. Key events from this keyboard will be + * propegated to the focused client. + */ +void wlr_seat_attach_keyboard(struct wlr_seat *seat, + struct wlr_input_device *dev); + +/** + * Detaches this keyboard from the seat. This is done automatically when the + * keyboard is destroyed; you only need to use this if you want to remove it for + * some other reason. + */ +void wlr_seat_detach_keyboard(struct wlr_seat *seat, struct wlr_keyboard *kb); + #endif diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index f20d2a73..08b098d2 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -1,13 +1,39 @@ +#include #include #include +#include +#include #include #include #include +#include + +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])) { + leds |= (1 << i); + } + } + wlr_keyboard_led_update(keyboard, leds); +} + +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); + keyboard_led_update(keyboard); + wl_signal_emit(&keyboard->events.key, &event); +} void wlr_keyboard_init(struct wlr_keyboard *kb, struct wlr_keyboard_impl *impl) { kb->impl = impl; wl_signal_init(&kb->events.key); + wl_signal_init(&kb->events.keymap); } void wlr_keyboard_destroy(struct wlr_keyboard *kb) { @@ -17,6 +43,7 @@ void wlr_keyboard_destroy(struct wlr_keyboard *kb) { wl_list_remove(&kb->events.key.listener_list); free(kb); } + close(kb->keymap_fd); } void wlr_keyboard_led_update(struct wlr_keyboard *kb, uint32_t leds) { @@ -24,3 +51,26 @@ void wlr_keyboard_led_update(struct wlr_keyboard *kb, uint32_t leds) { kb->impl->led_update(kb, leds); } } + +void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, + struct xkb_keymap *keymap) { + kb->keymap = keymap; + assert(kb->xkb_state = xkb_state_new(kb->keymap)); + const char *led_names[3] = { + 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]); + } + char *keymap_str = xkb_keymap_get_as_string(kb->keymap, + XKB_KEYMAP_FORMAT_TEXT_V1); + kb->keymap_size = strlen(keymap_str); + 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); + strcpy(ptr, keymap_str); + free(keymap_str); + wl_signal_emit(&kb->events.keymap, kb); +} diff --git a/types/wlr_seat.c b/types/wlr_seat.c index 24dbe19e..2ece197f 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -78,7 +78,6 @@ static void wl_seat_get_keyboard(struct wl_client *client, wl_resource_get_version(_handle), id); wl_resource_set_implementation(handle->keyboard, &wl_keyboard_impl, handle, &wl_keyboard_destroy); - wl_signal_emit(&handle->wlr_seat->events.keyboard_bound, handle); } static const struct wl_touch_interface wl_touch_impl = { @@ -180,10 +179,10 @@ struct wlr_seat *wlr_seat_create(struct wl_display *display, const char *name) { wlr_seat->display = display; wlr_seat->name = strdup(name); wl_list_init(&wlr_seat->handles); + wl_list_init(&wlr_seat->keyboards); wl_signal_init(&wlr_seat->events.client_bound); wl_signal_init(&wlr_seat->events.client_unbound); - wl_signal_init(&wlr_seat->events.keyboard_bound); wl_list_init(&wlr_seat->pointer_state.focus_resource_destroy_listener.link); wl_list_init(&wlr_seat->pointer_state.focus_surface_destroy_listener.link); @@ -295,6 +294,13 @@ void wlr_seat_pointer_enter(struct wlr_seat *wlr_seat, wl_pointer_send_frame(focused_handle->pointer); } + // TEMPORARY + if (focused_handle && focused_handle->pointer && focused_surface) { + uint32_t serial = wl_display_next_serial(wlr_seat->display); + wl_keyboard_send_leave(focused_handle->keyboard, + serial, focused_surface->resource); + } + // enter the current surface if (handle && handle->pointer) { uint32_t serial = wl_display_next_serial(wlr_seat->display); @@ -303,6 +309,15 @@ void wlr_seat_pointer_enter(struct wlr_seat *wlr_seat, wl_pointer_send_frame(handle->pointer); } + // TEMPORARY + if (handle && handle->keyboard) { + wlr_log(L_DEBUG, "Sending keyboard"); + struct wl_array keys; + wl_array_init(&keys); + uint32_t serial = wl_display_next_serial(wlr_seat->display); + wl_keyboard_send_enter(handle->keyboard, serial, surface->resource, &keys); + } + // reinitialize the focus destroy events wl_list_remove( &wlr_seat->pointer_state.focus_surface_destroy_listener.link); @@ -373,3 +388,93 @@ void wlr_seat_pointer_send_axis(struct wlr_seat *wlr_seat, uint32_t time, wl_pointer_send_frame(pointer); } + +static void keyboard_key_notify(struct wl_listener *listener, void *data) { + struct wlr_seat_keyboard *seat_kb = wl_container_of( + listener, seat_kb, key); + struct wlr_seat *seat = seat_kb->seat; + struct wlr_seat_handle *handle = seat->pointer_state.focused_handle; + if (!handle || !handle->keyboard) { + return; + } + struct wlr_keyboard *keyboard = seat_kb->keyboard; + struct wlr_event_keyboard_key *event = data; + enum wlr_key_state key_state = event->state; + if (handle->seat_keyboard != seat_kb) { + // TODO: We should probably lift all of the keys set by the other + // keyboard + wlr_log(L_DEBUG, "Sending key map"); + wl_keyboard_send_keymap(handle->keyboard, + WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, + seat_kb->keyboard->keymap_fd, + seat_kb->keyboard->keymap_size); + + if (wl_resource_get_version(handle->keyboard) >= 2) { + // TODO: Make this better + wl_keyboard_send_repeat_info(handle->keyboard, 25, 600); + } + handle->seat_keyboard = seat_kb; + } + + uint32_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_DEPRESSED); + uint32_t latched = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_LATCHED); + uint32_t locked = xkb_state_serialize_mods(keyboard->xkb_state, + XKB_STATE_MODS_LOCKED); + uint32_t group = xkb_state_serialize_layout(keyboard->xkb_state, + XKB_STATE_LAYOUT_EFFECTIVE); + + uint32_t modifiers_serial = wl_display_next_serial(seat->display); + uint32_t key_serial = wl_display_next_serial(seat->display); + wl_keyboard_send_modifiers(handle->keyboard, modifiers_serial, + depressed, latched, locked, group); + wl_keyboard_send_key(handle->keyboard, key_serial, + (uint32_t)event->time_usec, event->keycode, key_state); +} + +static void keyboard_keymap_notify(struct wl_listener *listener, void *data) { + struct wlr_seat_keyboard *seat_kb = wl_container_of( + listener, seat_kb, keymap); + wlr_log(L_DEBUG, "Keymap event for %p", seat_kb); +} + +static void keyboard_destroy_notify(struct wl_listener *listener, void *data) { + struct wlr_seat_keyboard *seat_kb = wl_container_of( + listener, seat_kb, destroy); + wlr_seat_detach_keyboard(seat_kb->seat, seat_kb->keyboard); +} + +void wlr_seat_attach_keyboard(struct wlr_seat *seat, + struct wlr_input_device *dev) { + assert(seat && dev && dev->type == WLR_INPUT_DEVICE_KEYBOARD); + struct wlr_keyboard *kb = dev->keyboard; + struct wlr_seat_keyboard *seat_kb = + calloc(1, sizeof(struct wlr_seat_keyboard)); + seat_kb->keyboard = kb; + seat_kb->seat = seat; + wl_list_init(&seat_kb->key.link); + seat_kb->key.notify = keyboard_key_notify; + wl_signal_add(&kb->events.key, &seat_kb->key); + wl_list_init(&seat_kb->keymap.link); + seat_kb->keymap.notify = keyboard_keymap_notify; + wl_signal_add(&kb->events.keymap, &seat_kb->keymap); + wl_list_init(&seat_kb->destroy.link); + seat_kb->destroy.notify = keyboard_destroy_notify; + wl_signal_add(&dev->events.destroy, &seat_kb->destroy); + wl_list_insert(&seat->keyboards, &seat_kb->link); +} + +void wlr_seat_detach_keyboard(struct wlr_seat *seat, struct wlr_keyboard *kb) { + struct wlr_seat_keyboard *seat_kb, *_tmp; + wl_list_for_each_safe(seat_kb, _tmp, &seat->keyboards, link) { + if (seat_kb->keyboard == kb) { + wl_list_remove(&seat_kb->link); + wl_list_remove(&seat_kb->key.link); + wl_list_remove(&seat_kb->keymap.link); + wl_list_remove(&seat_kb->destroy.link); + free(seat_kb); + break; + } + } +}