From 0f2b95af263a4e0eef672a92db099b98bd798821 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Sat, 7 Aug 2021 14:04:24 +0200 Subject: [PATCH] keyboard: generalize keymap data Add a keymap_format field and rename keymap_string to keymap_data. --- include/wlr/types/wlr_keyboard.h | 3 ++- types/seat/wlr_seat_keyboard.c | 8 +++++++- types/wlr_input_method_v2.c | 7 ++++--- types/wlr_keyboard.c | 19 ++++++++++--------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/include/wlr/types/wlr_keyboard.h b/include/wlr/types/wlr_keyboard.h index d53d3c304..6bb0632f3 100644 --- a/include/wlr/types/wlr_keyboard.h +++ b/include/wlr/types/wlr_keyboard.h @@ -54,7 +54,8 @@ struct wlr_keyboard { const struct wlr_keyboard_impl *impl; struct wlr_keyboard_group *group; - char *keymap_string; + enum xkb_keymap_format keymap_format; + char *keymap_data; size_t keymap_size; int keymap_fd; struct xkb_keymap *keymap; diff --git a/types/seat/wlr_seat_keyboard.c b/types/seat/wlr_seat_keyboard.c index 186b8251a..c453a8249 100644 --- a/types/seat/wlr_seat_keyboard.c +++ b/types/seat/wlr_seat_keyboard.c @@ -357,7 +357,13 @@ static void seat_client_send_keymap(struct wlr_seat_client *client, continue; } - wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, + enum wl_keyboard_keymap_format format = WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP; + if (keyboard->keymap != NULL) { + assert(keyboard->keymap_format == XKB_KEYMAP_FORMAT_TEXT_V1); + format = WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1; + } + + wl_keyboard_send_keymap(resource, format, keyboard->keymap_fd, keyboard->keymap_size); } } diff --git a/types/wlr_input_method_v2.c b/types/wlr_input_method_v2.c index 6d2e49f98..ebdea5dd5 100644 --- a/types/wlr_input_method_v2.c +++ b/types/wlr_input_method_v2.c @@ -335,7 +335,7 @@ static bool keyboard_grab_send_keymap( return false; } - memcpy(ptr, keyboard->keymap_string, keyboard->keymap_size); + memcpy(ptr, keyboard->keymap_data, keyboard->keymap_size); munmap(ptr, keyboard->keymap_size); zwp_input_method_keyboard_grab_v2_send_keymap(keyboard_grab->resource, @@ -389,8 +389,9 @@ void wlr_input_method_keyboard_grab_v2_set_keyboard( if (keyboard) { if (keyboard_grab->keyboard == NULL || - strcmp(keyboard_grab->keyboard->keymap_string, - keyboard->keymap_string) != 0) { + keyboard_grab->keyboard->keymap_size != keyboard->keymap_size || + memcmp(keyboard_grab->keyboard->keymap_data, + keyboard->keymap_data, keyboard->keymap_size) != 0) { // send keymap only if it is changed, or if input method is not // aware that it did not change and blindly send it back with // virtual keyboard, it may cause an infinite recursion. diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index 5150af877..e19a046db 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -156,7 +156,7 @@ void wlr_keyboard_finish(struct wlr_keyboard *kb) { /* Finish xkbcommon resources */ xkb_state_unref(kb->xkb_state); xkb_keymap_unref(kb->keymap); - free(kb->keymap_string); + free(kb->keymap_data); if (kb->keymap_fd >= 0) { close(kb->keymap_fd); } @@ -204,15 +204,16 @@ bool wlr_keyboard_set_keymap(struct wlr_keyboard *kb, kb->mod_indexes[i] = xkb_map_mod_get_index(kb->keymap, mod_names[i]); } - char *tmp_keymap_string = xkb_keymap_get_as_string(kb->keymap, + char *keymap_data = xkb_keymap_get_as_string(kb->keymap, XKB_KEYMAP_FORMAT_TEXT_V1); - if (tmp_keymap_string == NULL) { + if (keymap_data == NULL) { wlr_log(WLR_ERROR, "Failed to get string version of keymap"); goto err; } - free(kb->keymap_string); - kb->keymap_string = tmp_keymap_string; - kb->keymap_size = strlen(kb->keymap_string) + 1; + free(kb->keymap_data); + kb->keymap_format = XKB_KEYMAP_FORMAT_TEXT_V1; + kb->keymap_data = keymap_data; + kb->keymap_size = strlen(kb->keymap_data) + 1; int rw_fd = -1, ro_fd = -1; if (!allocate_shm_file_pair(kb->keymap_size, &rw_fd, &ro_fd)) { @@ -229,7 +230,7 @@ bool wlr_keyboard_set_keymap(struct wlr_keyboard *kb, goto err; } - memcpy(dst, kb->keymap_string, kb->keymap_size); + memcpy(dst, kb->keymap_data, kb->keymap_size); munmap(dst, kb->keymap_size); close(rw_fd); @@ -253,8 +254,8 @@ err: kb->xkb_state = NULL; xkb_keymap_unref(keymap); kb->keymap = NULL; - free(kb->keymap_string); - kb->keymap_string = NULL; + free(kb->keymap_data); + kb->keymap_data = NULL; return false; }