diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index d32d6e96b..c67cc4c26 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -32,6 +32,13 @@ enum wlr_keyboard_modifier { struct wlr_keyboard_impl; +struct wlr_keyboard_modifiers { + xkb_mod_mask_t depressed; + xkb_mod_mask_t latched; + xkb_mod_mask_t locked; + xkb_mod_mask_t group; +}; + struct wlr_keyboard { struct wlr_keyboard_impl *impl; // TODO: Should this store key repeat info too? @@ -45,12 +52,7 @@ struct wlr_keyboard { uint32_t keycodes[WLR_KEYBOARD_KEYS_CAP]; size_t num_keycodes; - struct { - xkb_mod_mask_t depressed; - xkb_mod_mask_t latched; - xkb_mod_mask_t locked; - xkb_mod_mask_t group; - } modifiers; + struct wlr_keyboard_modifiers modifiers; struct { int32_t rate; diff --git a/include/wlr/types/wlr_seat.h b/include/wlr/types/wlr_seat.h index 432e5dc3f..c2a89f33c 100644 --- a/include/wlr/types/wlr_seat.h +++ b/include/wlr/types/wlr_seat.h @@ -68,10 +68,12 @@ struct wlr_seat_keyboard_grab; struct wlr_keyboard_grab_interface { void (*enter)(struct wlr_seat_keyboard_grab *grab, - struct wlr_surface *surface); + struct wlr_surface *surface, uint32_t keycodes[], + size_t num_keycodes, struct wlr_keyboard_modifiers *modifiers); void (*key)(struct wlr_seat_keyboard_grab *grab, uint32_t time, uint32_t key, uint32_t state); - void (*modifiers)(struct wlr_seat_keyboard_grab *grab); + void (*modifiers)(struct wlr_seat_keyboard_grab *grab, + struct wlr_keyboard_modifiers *modifiers); void (*cancel)(struct wlr_seat_keyboard_grab *grab); }; @@ -344,6 +346,11 @@ bool wlr_seat_pointer_has_grab(struct wlr_seat *seat); */ void wlr_seat_set_keyboard(struct wlr_seat *seat, struct wlr_input_device *dev); +/** + * Get the active keyboard for the seat. + */ +struct wlr_keyboard *wlr_seat_get_keyboard(struct wlr_seat *seat); + /** * Start a grab of the keyboard of this seat. The grabber is responsible for * handling all keyboard events until the grab ends. @@ -375,21 +382,24 @@ void wlr_seat_keyboard_notify_key(struct wlr_seat *seat, uint32_t time, * Send the modifier state to focused keyboard resources. Compositors should use * `wlr_seat_keyboard_notify_modifiers()` to respect any keyboard grabs. */ -void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat); +void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat, + struct wlr_keyboard_modifiers *modifiers); /** * Notify the seat that the modifiers for the keyboard have changed. Defers to * any keyboard grabs. */ -void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat); +void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat, + struct wlr_keyboard_modifiers *modifiers); /** * Notify the seat that the keyboard focus has changed and request it to be the * focused surface for this keyboard. Defers to any current grab of the seat's * keyboard. */ -void wlr_seat_keyboard_notify_enter(struct wlr_seat *wlr_seat, - struct wlr_surface *surface); +void wlr_seat_keyboard_notify_enter(struct wlr_seat *seat, + struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes, + struct wlr_keyboard_modifiers *modifiers); /** * Send a keyboard enter event to the given surface and consider it to be the @@ -398,8 +408,9 @@ void wlr_seat_keyboard_notify_enter(struct wlr_seat *wlr_seat, * `wlr_seat_keyboard_notify_enter()` to change keyboard focus to respect * keyboard grabs. */ -void wlr_seat_keyboard_enter(struct wlr_seat *wlr_seat, - struct wlr_surface *surface); +void wlr_seat_keyboard_enter(struct wlr_seat *seat, + struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes, + struct wlr_keyboard_modifiers *modifiers); /** * Clear the focused surface for the keyboard and leave all entered surfaces. diff --git a/rootston/keyboard.c b/rootston/keyboard.c index 14199afd1..f1123599a 100644 --- a/rootston/keyboard.c +++ b/rootston/keyboard.c @@ -288,7 +288,8 @@ void roots_keyboard_handle_key(struct roots_keyboard *keyboard, void roots_keyboard_handle_modifiers(struct roots_keyboard *r_keyboard) { struct wlr_seat *seat = r_keyboard->seat->seat; wlr_seat_set_keyboard(seat, r_keyboard->device); - wlr_seat_keyboard_notify_modifiers(seat); + wlr_seat_keyboard_notify_modifiers(seat, + &r_keyboard->device->keyboard->modifiers); } static void keyboard_config_merge(struct roots_keyboard_config *config, diff --git a/rootston/seat.c b/rootston/seat.c index a61057bd8..1a0e6253c 100644 --- a/rootston/seat.c +++ b/rootston/seat.c @@ -662,7 +662,16 @@ void roots_seat_set_focus(struct roots_seat *seat, struct roots_view *view) { seat->has_focus = true; wl_list_remove(&seat_view->link); wl_list_insert(&seat->views, &seat_view->link); - wlr_seat_keyboard_notify_enter(seat->seat, view->wlr_surface); + + struct wlr_keyboard *keyboard = wlr_seat_get_keyboard(seat->seat); + if (keyboard != NULL) { + wlr_seat_keyboard_notify_enter(seat->seat, view->wlr_surface, + keyboard->keycodes, keyboard->num_keycodes, + &keyboard->modifiers); + } else { + wlr_seat_keyboard_notify_enter(seat->seat, view->wlr_surface, + NULL, 0, NULL); + } } void roots_seat_cycle_focus(struct roots_seat *seat) { diff --git a/types/wlr_data_device.c b/types/wlr_data_device.c index a4eb233b4..5a6bc1980 100644 --- a/types/wlr_data_device.c +++ b/types/wlr_data_device.c @@ -585,7 +585,8 @@ const struct wlr_touch_grab_interface wlr_data_device_touch_drag_interface = { }; static void keyboard_drag_enter(struct wlr_seat_keyboard_grab *grab, - struct wlr_surface *surface) { + struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes, + struct wlr_keyboard_modifiers *modifiers) { // nothing has keyboard focus during drags } @@ -594,7 +595,8 @@ static void keyboard_drag_key(struct wlr_seat_keyboard_grab *grab, // no keyboard input during drags } -static void keyboard_drag_modifiers(struct wlr_seat_keyboard_grab *grab) { +static void keyboard_drag_modifiers(struct wlr_seat_keyboard_grab *grab, + struct wlr_keyboard_modifiers *modifiers) { //struct wlr_keyboard *keyboard = grab->seat->keyboard_state.keyboard; // TODO change the dnd action based on what modifier is pressed on the // keyboard diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index 6caf27f5b..b0dc97aaf 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -11,6 +11,10 @@ int os_create_anonymous_file(off_t size); static void keyboard_led_update(struct wlr_keyboard *keyboard) { + if (keyboard->xkb_state == NULL) { + return; + } + uint32_t leds = 0; for (uint32_t i = 0; i < WLR_LED_COUNT; ++i) { if (xkb_state_led_index_is_active(keyboard->xkb_state, @@ -21,7 +25,15 @@ static void keyboard_led_update(struct wlr_keyboard *keyboard) { wlr_keyboard_led_update(keyboard, leds); } -static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { +/** + * Update the modifier state of the wlr-keyboard. Returns true if the modifier + * state changed. + */ +static bool keyboard_modifier_update(struct wlr_keyboard *keyboard) { + if (keyboard->xkb_state == NULL) { + return false; + } + xkb_mod_mask_t depressed = xkb_state_serialize_mods(keyboard->xkb_state, XKB_STATE_MODS_DEPRESSED); xkb_mod_mask_t latched = xkb_state_serialize_mods(keyboard->xkb_state, @@ -34,7 +46,7 @@ static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { latched == keyboard->modifiers.latched && locked == keyboard->modifiers.locked && group == keyboard->modifiers.group) { - return; + return false; } keyboard->modifiers.depressed = depressed; @@ -42,7 +54,7 @@ static void keyboard_modifier_update(struct wlr_keyboard *keyboard) { keyboard->modifiers.locked = locked; keyboard->modifiers.group = group; - wl_signal_emit(&keyboard->events.modifiers, keyboard); + return true; } // https://www.geeksforgeeks.org/move-zeroes-end-array/ @@ -90,17 +102,21 @@ static void keyboard_key_update(struct wlr_keyboard *keyboard, void wlr_keyboard_notify_modifiers(struct wlr_keyboard *keyboard, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { - if (!keyboard->xkb_state) { + if (keyboard->xkb_state == NULL) { return; } xkb_state_update_mask(keyboard->xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group); - keyboard_modifier_update(keyboard); + + bool updated = keyboard_modifier_update(keyboard); + if (updated) { + wl_signal_emit(&keyboard->events.modifiers, keyboard); + } } void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard, struct wlr_event_keyboard_key *event) { - if (!keyboard->xkb_state) { + if (keyboard->xkb_state == NULL) { return; } if (event->update_state) { @@ -109,7 +125,12 @@ void wlr_keyboard_notify_key(struct wlr_keyboard *keyboard, event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); } keyboard_led_update(keyboard); - keyboard_modifier_update(keyboard); + + bool updated = keyboard_modifier_update(keyboard); + if (updated) { + wl_signal_emit(&keyboard->events.modifiers, keyboard); + } + keyboard_key_update(keyboard, event); wl_signal_emit(&keyboard->events.key, event); } @@ -150,20 +171,16 @@ void wlr_keyboard_led_update(struct wlr_keyboard *kb, uint32_t leds) { void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, struct xkb_keymap *keymap) { - if (kb->keymap) { - xkb_keymap_unref(kb->keymap); - } - xkb_keymap_ref(keymap); - kb->keymap = keymap; + char *keymap_str = NULL; - if (kb->xkb_state) { - xkb_state_unref(kb->xkb_state); - } + xkb_keymap_unref(kb->keymap); + kb->keymap = xkb_keymap_ref(keymap); + xkb_state_unref(kb->xkb_state); kb->xkb_state = xkb_state_new(kb->keymap); if (kb->xkb_state == NULL) { wlr_log(L_ERROR, "Failed to create XKB state"); - return; + goto err; } const char *led_names[WLR_LED_COUNT] = { @@ -190,7 +207,7 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]); } - char *keymap_str = xkb_keymap_get_as_string(kb->keymap, + keymap_str = xkb_keymap_get_as_string(kb->keymap, XKB_KEYMAP_FORMAT_TEXT_V1); kb->keymap_size = strlen(keymap_str) + 1; if (kb->keymap_fd) { @@ -199,16 +216,34 @@ void wlr_keyboard_set_keymap(struct wlr_keyboard *kb, kb->keymap_fd = os_create_anonymous_file(kb->keymap_size); if (kb->keymap_fd < 0) { wlr_log(L_ERROR, "creating a keymap file for %lu bytes failed", kb->keymap_size); + goto err; } void *ptr = mmap(NULL, kb->keymap_size, PROT_READ | PROT_WRITE, MAP_SHARED, kb->keymap_fd, 0); if (ptr == (void*)-1) { wlr_log(L_ERROR, "failed to mmap() %lu bytes", kb->keymap_size); + goto err; } strcpy(ptr, keymap_str); free(keymap_str); + for (size_t i = 0; i < kb->num_keycodes; ++i) { + xkb_keycode_t keycode = kb->keycodes[i] + 8; + xkb_state_update_key(kb->xkb_state, keycode, XKB_KEY_DOWN); + } + + keyboard_modifier_update(kb); + wl_signal_emit(&kb->events.keymap, kb); + return; + +err: + xkb_state_unref(kb->xkb_state); + kb->xkb_state = NULL; + xkb_keymap_unref(keymap); + kb->keymap = NULL; + close(kb->keymap_fd); + free(keymap_str); } void wlr_keyboard_set_repeat_info(struct wlr_keyboard *kb, int32_t rate, diff --git a/types/wlr_seat.c b/types/wlr_seat.c index fd76775da..019cb5671 100644 --- a/types/wlr_seat.c +++ b/types/wlr_seat.c @@ -282,8 +282,9 @@ static const struct wlr_pointer_grab_interface default_pointer_grab_impl = { }; static void default_keyboard_enter(struct wlr_seat_keyboard_grab *grab, - struct wlr_surface *surface) { - wlr_seat_keyboard_enter(grab->seat, surface); + struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes, + struct wlr_keyboard_modifiers *modifiers) { + wlr_seat_keyboard_enter(grab->seat, surface, keycodes, num_keycodes, modifiers); } static void default_keyboard_key(struct wlr_seat_keyboard_grab *grab, @@ -291,8 +292,9 @@ static void default_keyboard_key(struct wlr_seat_keyboard_grab *grab, wlr_seat_keyboard_send_key(grab->seat, time, key, state); } -static void default_keyboard_modifiers(struct wlr_seat_keyboard_grab *grab) { - wlr_seat_keyboard_send_modifiers(grab->seat); +static void default_keyboard_modifiers(struct wlr_seat_keyboard_grab *grab, + struct wlr_keyboard_modifiers *modifiers) { + wlr_seat_keyboard_send_modifiers(grab->seat, modifiers); } static void default_keyboard_cancel(struct wlr_seat_keyboard_grab *grab) { @@ -773,6 +775,7 @@ void wlr_seat_set_keyboard(struct wlr_seat *seat, if (keyboard) { assert(device->type == WLR_INPUT_DEVICE_KEYBOARD); + seat->keyboard_state.keyboard = keyboard; wl_signal_add(&device->events.destroy, &seat->keyboard_state.keyboard_destroy); @@ -787,14 +790,18 @@ void wlr_seat_set_keyboard(struct wlr_seat *seat, struct wlr_seat_client *client; wl_list_for_each(client, &seat->clients, link) { - seat_client_send_keymap(client, device->keyboard); - seat_client_send_repeat_info(client, device->keyboard); + seat_client_send_keymap(client, keyboard); + seat_client_send_repeat_info(client, keyboard); } + wlr_seat_keyboard_send_modifiers(seat, &keyboard->modifiers); + } else { + seat->keyboard_state.keyboard = NULL; } +} - seat->keyboard_state.keyboard = keyboard; - wlr_seat_keyboard_send_modifiers(seat); +struct wlr_keyboard *wlr_seat_get_keyboard(struct wlr_seat *seat) { + return seat->keyboard_state.keyboard; } void wlr_seat_keyboard_start_grab(struct wlr_seat *wlr_seat, @@ -835,28 +842,29 @@ static void keyboard_resource_destroy_notify(struct wl_listener *listener, wlr_seat_keyboard_clear_focus(state->seat); } -void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat) { +void wlr_seat_keyboard_send_modifiers(struct wlr_seat *seat, + struct wlr_keyboard_modifiers *modifiers) { struct wlr_seat_client *client = seat->keyboard_state.focused_client; if (client == NULL) { return; } - struct wlr_keyboard *keyboard = seat->keyboard_state.keyboard; - if (keyboard == NULL) { - return; - } - uint32_t serial = wl_display_next_serial(seat->display); struct wl_resource *resource; wl_resource_for_each(resource, &client->keyboards) { - wl_keyboard_send_modifiers(resource, serial, - keyboard->modifiers.depressed, keyboard->modifiers.latched, - keyboard->modifiers.locked, keyboard->modifiers.group); + if (modifiers == NULL) { + wl_keyboard_send_modifiers(resource, serial, 0, 0, 0, 0); + } else { + wl_keyboard_send_modifiers(resource, serial, + modifiers->depressed, modifiers->latched, + modifiers->locked, modifiers->group); + } } } void wlr_seat_keyboard_enter(struct wlr_seat *seat, - struct wlr_surface *surface) { + struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes, + struct wlr_keyboard_modifiers *modifiers) { if (seat->keyboard_state.focused_surface == surface) { // this surface already got an enter notify return; @@ -884,19 +892,17 @@ void wlr_seat_keyboard_enter(struct wlr_seat *seat, } // enter the current surface - if (client != NULL && seat->keyboard_state.keyboard != NULL) { - struct wlr_keyboard *keyboard = seat->keyboard_state.keyboard; - + if (client != NULL) { struct wl_array keys; wl_array_init(&keys); - for (size_t i = 0; i < keyboard->num_keycodes; ++i) { + for (size_t i = 0; i < num_keycodes; ++i) { uint32_t *p = wl_array_add(&keys, sizeof(uint32_t)); if (!p) { wlr_log(L_ERROR, "Cannot allocate memory, skipping keycode: %d\n", - keyboard->keycodes[i]); + keycodes[i]); continue; } - *p = keyboard->keycodes[i]; + *p = keycodes[i]; } uint32_t serial = wl_display_next_serial(seat->display); struct wl_resource *resource; @@ -928,33 +934,34 @@ void wlr_seat_keyboard_enter(struct wlr_seat *seat, seat->keyboard_state.focused_client = client; seat->keyboard_state.focused_surface = surface; - if (client != NULL && seat->keyboard_state.keyboard != NULL) { + if (client != NULL) { // tell new client about any modifier change last, // as it targets seat->keyboard_state.focused_client - wlr_seat_keyboard_send_modifiers(seat); + wlr_seat_keyboard_send_modifiers(seat, modifiers); } } -void wlr_seat_keyboard_notify_enter(struct wlr_seat *seat, struct - wlr_surface *surface) { +void wlr_seat_keyboard_notify_enter(struct wlr_seat *seat, + struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes, + struct wlr_keyboard_modifiers *modifiers) { struct wlr_seat_keyboard_grab *grab = seat->keyboard_state.grab; - grab->interface->enter(grab, surface); + grab->interface->enter(grab, surface, keycodes, num_keycodes, modifiers); } void wlr_seat_keyboard_clear_focus(struct wlr_seat *seat) { - struct wl_array keys; - wl_array_init(&keys); - wlr_seat_keyboard_enter(seat, NULL); + // TODO respect grabs here? + wlr_seat_keyboard_enter(seat, NULL, NULL, 0, NULL); } bool wlr_seat_keyboard_has_grab(struct wlr_seat *seat) { return seat->keyboard_state.grab->interface != &default_keyboard_grab_impl; } -void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat) { +void wlr_seat_keyboard_notify_modifiers(struct wlr_seat *seat, + struct wlr_keyboard_modifiers *modifiers) { clock_gettime(CLOCK_MONOTONIC, &seat->last_event); struct wlr_seat_keyboard_grab *grab = seat->keyboard_state.grab; - grab->interface->modifiers(grab); + grab->interface->modifiers(grab, modifiers); } void wlr_seat_keyboard_notify_key(struct wlr_seat *seat, uint32_t time, diff --git a/types/wlr_xdg_shell_v6.c b/types/wlr_xdg_shell_v6.c index 1c46197e4..59b152bdf 100644 --- a/types/wlr_xdg_shell_v6.c +++ b/types/wlr_xdg_shell_v6.c @@ -103,7 +103,9 @@ static const struct wlr_pointer_grab_interface xdg_pointer_grab_impl = { .axis = xdg_pointer_grab_axis, }; -static void xdg_keyboard_grab_enter(struct wlr_seat_keyboard_grab *grab, struct wlr_surface *surface) { +static void xdg_keyboard_grab_enter(struct wlr_seat_keyboard_grab *grab, + struct wlr_surface *surface, uint32_t keycodes[], size_t num_keycodes, + struct wlr_keyboard_modifiers *modifiers) { // keyboard focus should remain on the popup } @@ -112,8 +114,9 @@ static void xdg_keyboard_grab_key(struct wlr_seat_keyboard_grab *grab, uint32_t wlr_seat_keyboard_send_key(grab->seat, time, key, state); } -static void xdg_keyboard_grab_modifiers(struct wlr_seat_keyboard_grab *grab) { - wlr_seat_keyboard_send_modifiers(grab->seat); +static void xdg_keyboard_grab_modifiers(struct wlr_seat_keyboard_grab *grab, + struct wlr_keyboard_modifiers *modifiers) { + wlr_seat_keyboard_send_modifiers(grab->seat, modifiers); } static void xdg_keyboard_grab_cancel(struct wlr_seat_keyboard_grab *grab) {