From 87eb7753774b4375498d5fe5a68de96b50002173 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Tue, 6 Oct 2020 09:53:02 +0000 Subject: [PATCH] keymap: Keep keymap fd management in one place At the same time, reduce the distance between this and the Xwayland handling branch. --- eek/eek-keyboard.c | 66 ++++++++++++++++++++++++++++------------------ eek/eek-keyboard.h | 23 ++++++++-------- src/wayland.c | 2 +- 3 files changed, 53 insertions(+), 38 deletions(-) diff --git a/eek/eek-keyboard.c b/eek/eek-keyboard.c index 16a6198c..8afe1057 100644 --- a/eek/eek-keyboard.c +++ b/eek/eek-keyboard.c @@ -28,33 +28,24 @@ #include // TODO: this is Linux-specific #include - #include "eek-keyboard.h" -void level_keyboard_free(LevelKeyboard *self) { - xkb_keymap_unref(self->keymap); - close(self->keymap_fd); - squeek_layout_free(self->layout); - g_free(self); + +static void eek_key_map_deinit(struct keymap *self) { + if (self->fd < 0) { + g_error("Deinit called multiple times on KeyMap"); + } else { + close(self->fd); + } + self->fd = -1; } -LevelKeyboard* -level_keyboard_new (struct squeek_layout *layout) -{ - LevelKeyboard *keyboard = g_new0(LevelKeyboard, 1); - - if (!keyboard) { - g_error("Failed to create a keyboard"); - } - keyboard->layout = layout; - +static struct keymap eek_key_map_from_str(const char *keymap_str) { struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); if (!context) { g_error("No context created"); } - const gchar *keymap_str = squeek_layout_get_keymap(keyboard->layout); - struct xkb_keymap *keymap = xkb_keymap_new_from_string(context, keymap_str, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS); @@ -62,10 +53,9 @@ level_keyboard_new (struct squeek_layout *layout) g_error("Bad keymap:\n%s", keymap_str); xkb_context_unref(context); - keyboard->keymap = keymap; - char * xkb_keymap_str = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1); - keyboard->keymap_len = strlen(xkb_keymap_str) + 1; + char *xkb_keymap_str = xkb_keymap_get_as_string(keymap, XKB_KEYMAP_FORMAT_TEXT_V1); + size_t keymap_len = strlen(xkb_keymap_str) + 1; g_autofree char *path = strdup("/eek_keymap-XXXXXX"); char *r = &path[strlen(path) - 6]; @@ -79,18 +69,42 @@ level_keyboard_new (struct squeek_layout *layout) if (keymap_fd < 0) { g_error("Failed to set up keymap fd"); } - keyboard->keymap_fd = keymap_fd; + shm_unlink(path); - if (ftruncate(keymap_fd, (off_t)keyboard->keymap_len)) { + if (ftruncate(keymap_fd, (off_t)keymap_len)) { g_error("Failed to increase keymap fd size"); } - char *ptr = mmap(NULL, keyboard->keymap_len, PROT_WRITE, MAP_SHARED, + char *ptr = mmap(NULL, keymap_len, PROT_WRITE, MAP_SHARED, keymap_fd, 0); if ((void*)ptr == (void*)-1) { g_error("Failed to set up mmap"); } - strncpy(ptr, xkb_keymap_str, keyboard->keymap_len); + strncpy(ptr, xkb_keymap_str, keymap_len); + munmap(ptr, keymap_len); free(xkb_keymap_str); - munmap(ptr, keyboard->keymap_len); + xkb_keymap_unref(keymap); + struct keymap km = { + .fd = keymap_fd, + .fd_len = keymap_len, + }; + return km; +} + +void level_keyboard_free(LevelKeyboard *self) { + eek_key_map_deinit(&self->keymap); + squeek_layout_free(self->layout); + g_free(self); +} + +LevelKeyboard* +level_keyboard_new (struct squeek_layout *layout) +{ + LevelKeyboard *keyboard = g_new0(LevelKeyboard, 1); + if (!keyboard) { + g_error("Failed to create a keyboard"); + } + keyboard->layout = layout; + const gchar *keymap_str = squeek_layout_get_keymap(keyboard->layout); + keyboard->keymap = eek_key_map_from_str(keymap_str); return keyboard; } diff --git a/eek/eek-keyboard.h b/eek/eek-keyboard.h index b1377c47..f70a268a 100644 --- a/eek/eek-keyboard.h +++ b/eek/eek-keyboard.h @@ -1,17 +1,17 @@ -/* +/* * Copyright (C) 2010-2011 Daiki Ueno * Copyright (C) 2010-2011 Red Hat, Inc. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA @@ -32,19 +32,20 @@ G_BEGIN_DECLS +/// Keymap container for Rust interoperability. +struct keymap { + uint32_t fd; // keymap formatted as XKB string + size_t fd_len; // length of the data inside keymap_fd +}; + /// Keyboard state holder struct _LevelKeyboard { struct squeek_layout *layout; // owned - struct xkb_keymap *keymap; // owned - int keymap_fd; // keymap formatted as XKB string - size_t keymap_len; // length of the data inside keymap_fd - - guint id; // as a key to layout choices + struct keymap keymap; // owned }; typedef struct _LevelKeyboard LevelKeyboard; -gchar * eek_keyboard_get_keymap - (LevelKeyboard *keyboard); +gchar *eek_keyboard_get_keymap(LevelKeyboard *keyboard); LevelKeyboard* level_keyboard_new (struct squeek_layout *layout); diff --git a/src/wayland.c b/src/wayland.c index a0f7e7b7..30955810 100644 --- a/src/wayland.c +++ b/src/wayland.c @@ -17,7 +17,7 @@ eek_virtual_keyboard_v1_key(struct zwp_virtual_keyboard_v1 *zwp_virtual_keyboard void eek_virtual_keyboard_update_keymap(struct zwp_virtual_keyboard_v1 *zwp_virtual_keyboard_v1, const LevelKeyboard *keyboard) { zwp_virtual_keyboard_v1_keymap(zwp_virtual_keyboard_v1, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, - keyboard->keymap_fd, keyboard->keymap_len); + keyboard->keymap.fd, keyboard->keymap.fd_len); } void