From da04a6dd5470506375d236f8392dbfd8ca921d81 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Wed, 29 Jun 2022 18:24:53 +1000 Subject: [PATCH] [client] all: use the defined keyboard mapping for keybinds Fixes #1007 --- client/displayservers/Wayland/input.c | 16 ++++-- client/displayservers/X11/x11.c | 32 ++++++++++-- client/displayservers/X11/x11.h | 4 ++ client/include/app.h | 8 +-- client/renderers/EGL/desktop.c | 2 +- client/src/app.c | 71 ++++++++++++++++++--------- client/src/keybind.c | 54 ++++++++++---------- client/src/main.c | 3 ++ client/src/main.h | 11 +++-- client/src/overlay/fps.c | 2 +- client/src/overlay/graphs.c | 2 +- client/src/overlay/help.c | 22 ++++++--- 12 files changed, 151 insertions(+), 76 deletions(-) diff --git a/client/displayservers/Wayland/input.c b/client/displayservers/Wayland/input.c index 2f02c45c..9be2c2ff 100644 --- a/client/displayservers/Wayland/input.c +++ b/client/displayservers/Wayland/input.c @@ -209,6 +209,16 @@ done: close(fd); } +static int getCharcode(uint32_t key) +{ + xkb_keysym_t sym = xkb_state_key_get_one_sym(wlWm.xkbState, key); + if (sym == XKB_KEY_NoSymbol) + return 0; + + sym = xkb_keysym_to_upper(sym); + return xkb_keysym_to_utf32(sym); +} + static void keyboardEnterHandler(void * data, struct wl_keyboard * keyboard, uint32_t serial, struct wl_surface * surface, struct wl_array * keys) { @@ -221,7 +231,7 @@ static void keyboardEnterHandler(void * data, struct wl_keyboard * keyboard, uint32_t * key; wl_array_for_each(key, keys) - app_handleKeyPress(*key); + app_handleKeyPress(*key, getCharcode(*key)); } static void keyboardLeaveHandler(void * data, struct wl_keyboard * keyboard, @@ -242,9 +252,9 @@ static void keyboardKeyHandler(void * data, struct wl_keyboard * keyboard, return; if (state == WL_KEYBOARD_KEY_STATE_PRESSED) - app_handleKeyPress(key); + app_handleKeyPress(key, getCharcode(key)); else - app_handleKeyRelease(key); + app_handleKeyRelease(key, getCharcode(key)); if (!wlWm.xkbState || !app_isOverlayMode() || state != WL_KEYBOARD_KEY_STATE_PRESSED) return; diff --git a/client/displayservers/X11/x11.c b/client/displayservers/X11/x11.c index 68776526..cfc15c79 100644 --- a/client/displayservers/X11/x11.c +++ b/client/displayservers/X11/x11.c @@ -36,6 +36,8 @@ #include #include +#include + #include #include @@ -514,6 +516,10 @@ static bool x11Init(const LG_DSInitParams params) goto fail_window; } + XDisplayKeycodes(x11.display, &x11.minKeycode, &x11.maxKeycode); + x11.keysyms = XGetKeyboardMapping(x11.display, x11.minKeycode, + x11.maxKeycode - x11.minKeycode, &x11.symsPerKeycode); + XIFreeDeviceInfo(devinfo); XQueryExtension(x11.display, "XInputExtension", &x11.xinputOp, &event, &error); @@ -738,6 +744,9 @@ static void x11Free(void) if (x11.cursors[i]) XFreeCursor(x11.display, x11.cursors[i]); + if (x11.keysyms) + XFree(x11.keysyms); + XCloseDisplay(x11.display); } @@ -1035,6 +1044,17 @@ static void setFocus(bool focused, double x, double y) app_handleFocusEvent(focused); } +static int getCharcode(int detail) +{ + if (detail < x11.minKeycode || detail > x11.maxKeycode) + return 0; + + KeySym sym = x11.keysyms[(detail - x11.minKeycode) * + x11.symsPerKeycode]; + sym = xkb_keysym_to_upper(sym); + return xkb_keysym_to_utf32(sym); +} + static void x11XInputEvent(XGenericEventCookie *cookie) { static int button_state = 0; @@ -1126,7 +1146,8 @@ static void x11XInputEvent(XGenericEventCookie *cookie) return; XIDeviceEvent *device = cookie->data; - app_handleKeyPress(device->detail - 8); + app_handleKeyPress(device->detail - x11.minKeycode, + getCharcode(device->detail)); if (!x11.xic || !app_isOverlayMode()) return; @@ -1176,7 +1197,8 @@ static void x11XInputEvent(XGenericEventCookie *cookie) return; XIDeviceEvent *device = cookie->data; - app_handleKeyRelease(device->detail - 8); + app_handleKeyRelease(device->detail - x11.minKeycode, + getCharcode(device->detail)); if (!x11.xic || !app_isOverlayMode()) return; @@ -1205,7 +1227,8 @@ static void x11XInputEvent(XGenericEventCookie *cookie) return; XIRawEvent *raw = cookie->data; - app_handleKeyPress(raw->detail - 8); + app_handleKeyPress(raw->detail - x11.minKeycode, + getCharcode(raw->detail)); return; } @@ -1215,7 +1238,8 @@ static void x11XInputEvent(XGenericEventCookie *cookie) return; XIRawEvent *raw = cookie->data; - app_handleKeyRelease(raw->detail - 8); + app_handleKeyRelease(raw->detail - x11.minKeycode, + getCharcode(raw->detail)); return; } diff --git a/client/displayservers/X11/x11.h b/client/displayservers/X11/x11.h index 6de1efc6..acde1937 100644 --- a/client/displayservers/X11/x11.h +++ b/client/displayservers/X11/x11.h @@ -54,6 +54,10 @@ struct X11DSState Window window; XVisualInfo * visual; + int minKeycode, maxKeycode; + int symsPerKeycode; + KeySym * keysyms; + //Extended Window Manager Hints //ref: https://specifications.freedesktop.org/wm-spec/latest/ bool ewmhSupport; diff --git a/client/include/app.h b/client/include/app.h index 0a9a7ed9..5fe941b2 100644 --- a/client/include/app.h +++ b/client/include/app.h @@ -58,9 +58,9 @@ void app_resyncMouseBasic(void); void app_handleButtonPress(int button); void app_handleButtonRelease(int button); void app_handleWheelMotion(double motion); -void app_handleKeyPress(int scancode); -void app_handleKeyRelease(int scancode); void app_handleKeyboardTyped(const char * typed); +void app_handleKeyPress(int scancode, int charcode); +void app_handleKeyRelease(int scancode, int charcode); void app_handleKeyboardModifiers(bool ctrl, bool shift, bool alt, bool super); void app_handleKeyboardLEDs(bool numLock, bool capsLock, bool scrollLock); void app_handleEnterEvent(bool entered); @@ -155,12 +155,14 @@ void app_showRecord(bool show); /** * Register a handler for the + combination * @param sc The scancode to register + * @param charcode The charcode to register (used instead of sc if non zero) * @param callback The function to be called when the combination is pressed * @param opaque A pointer to be passed to the callback, may be NULL * @retval A handle for the binding or NULL on failure. * The caller is required to release the handle via `app_releaseKeybind` when it is no longer required */ -KeybindHandle app_registerKeybind(int sc, KeybindFn callback, void * opaque, const char * description); +KeybindHandle app_registerKeybind(int sc, int charcode, KeybindFn callback, + void * opaque, const char * description); /** * Release an existing key binding diff --git a/client/renderers/EGL/desktop.c b/client/renderers/EGL/desktop.c index 35b5366c..ba2ad761 100644 --- a/client/renderers/EGL/desktop.c +++ b/client/renderers/EGL/desktop.c @@ -157,7 +157,7 @@ bool egl_desktopInit(EGL * egl, EGL_Desktop ** desktop_, EGLDisplay * display, return false; } - app_registerKeybind(KEY_N, toggleNV, desktop, + app_registerKeybind(0, 'N', toggleNV, desktop, "Toggle night vision mode"); desktop->nvMax = option_get_int("egl", "nvGainMax"); diff --git a/client/src/app.c b/client/src/app.c index 6fca05fc..1c6acd99 100644 --- a/client/src/app.c +++ b/client/src/app.c @@ -38,6 +38,7 @@ #include #include #include +#include bool app_isRunning(void) { @@ -115,7 +116,7 @@ void app_handleFocusEvent(bool focused) if (g_params.releaseKeysOnFocusLoss) for (int key = 0; key < KEY_MAX; key++) if (g_state.keyDown[key]) - app_handleKeyRelease(key); + app_handleKeyRelease(key, 0); g_state.escapeActive = false; @@ -304,7 +305,7 @@ void app_handleWheelMotion(double motion) g_state.io->MouseWheel -= motion; } -void app_handleKeyPress(int sc) +void app_handleKeyPress(int sc, int charcode) { if (!app_isOverlayMode() || !g_state.io->WantCaptureKeyboard) { @@ -319,9 +320,16 @@ void app_handleKeyPress(int sc) if (g_state.escapeActive) { g_state.escapeAction = sc; - KeybindHandle handle = g_state.bindings[sc]; - if (handle) - handle->callback(sc, handle->opaque); + KeybindHandle handle; + ll_forEachNL(g_state.bindings, item, handle) + { + if ((handle->sc && handle->sc == sc ) || + (handle->charcode && handle->charcode == charcode)) + { + handle->callback(sc, handle->opaque); + break; + } + } return; } } @@ -357,7 +365,7 @@ void app_handleKeyPress(int sc) } } -void app_handleKeyRelease(int sc) +void app_handleKeyRelease(int sc, int charcode) { if (g_state.escapeActive) { @@ -679,28 +687,48 @@ void app_showRecord(bool show) overlayStatus_set(LG_USER_STATUS_RECORDING, show); } -KeybindHandle app_registerKeybind(int sc, KeybindFn callback, void * opaque, const char * description) +KeybindHandle app_registerKeybind(int sc, int charcode, KeybindFn callback, + void * opaque, const char * description) { - // don't allow duplicate binds - if (g_state.bindings[sc]) + if (charcode != 0 && sc != 0) { - DEBUG_INFO("Key already bound"); + DEBUG_ERROR("invalid keybind, one of scancode or charcode must be 0"); return NULL; } - KeybindHandle handle = malloc(sizeof(*handle)); + if (charcode && islower(charcode)) + { + DEBUG_ERROR("invalid keybind, charcode must be uppercase"); + return NULL; + } + + KeybindHandle handle; + + // don't allow duplicate binds + ll_forEachNL(g_state.bindings, item, handle) + { + if ((sc && handle->sc == sc ) || + (charcode && handle->charcode == charcode)) + { + DEBUG_INFO("Key already bound"); + return NULL; + } + } + + handle = malloc(sizeof(*handle)); if (!handle) { DEBUG_ERROR("out of memory"); return NULL; } - handle->sc = sc; - handle->callback = callback; - handle->opaque = opaque; + handle->sc = sc; + handle->charcode = charcode; + handle->callback = callback; + handle->description = description; + handle->opaque = opaque; - g_state.bindings[sc] = handle; - g_state.keyDescription[sc] = description; + ll_push(g_state.bindings, handle); return handle; } @@ -709,19 +737,16 @@ void app_releaseKeybind(KeybindHandle * handle) if (!*handle) return; - g_state.bindings[(*handle)->sc] = NULL; + ll_removeData(g_state.bindings, *handle); free(*handle); *handle = NULL; } void app_releaseAllKeybinds(void) { - for(int i = 0; i < KEY_MAX; ++i) - if (g_state.bindings[i]) - { - free(g_state.bindings[i]); - g_state.bindings[i] = NULL; - } + KeybindHandle * handle; + while(ll_shift(g_state.bindings, (void **)&handle)) + free(handle); } GraphHandle app_registerGraph(const char * name, RingBuffer buffer, diff --git a/client/src/keybind.c b/client/src/keybind.c index ad7d2ed7..16b60bae 100644 --- a/client/src/keybind.c +++ b/client/src/keybind.c @@ -127,15 +127,15 @@ static void bind_toggleKey(int sc, void * opaque) void keybind_commonRegister(void) { - app_registerKeybind(KEY_F, bind_fullscreen , NULL, + app_registerKeybind(0, 'F', bind_fullscreen , NULL, "Full screen toggle"); - app_registerKeybind(KEY_V, bind_video , NULL, + app_registerKeybind(0, 'V', bind_video , NULL, "Video stream toggle"); - app_registerKeybind(KEY_R, bind_rotate , NULL, + app_registerKeybind(0, 'R', bind_rotate , NULL, "Rotate the output clockwise by 90° increments"); - app_registerKeybind(KEY_Q, bind_quit , NULL, + app_registerKeybind(0, 'Q', bind_quit , NULL, "Quit"); - app_registerKeybind(KEY_O, bind_toggleOverlay, NULL, + app_registerKeybind(0, 'O', bind_toggleOverlay, NULL, "Toggle overlay"); } @@ -145,30 +145,30 @@ void keybind_spiceRegister(void) static bool firstTime = true; if (firstTime) { - app_registerKeybind(KEY_I, bind_input, NULL, + app_registerKeybind(0, 'I', bind_input, NULL, "Spice keyboard & mouse toggle"); - app_registerKeybind(KEY_INSERT, bind_mouseSens, (void *) true , - "Increase mouse sensitivity in capture mode"); - app_registerKeybind(KEY_DELETE, bind_mouseSens, (void *) false, + app_registerKeybind(KEY_INSERT, 0, bind_mouseSens, (void *) true , + "Increase mouse sensitivity 0, in capture mode"); + app_registerKeybind(KEY_DELETE, 0, bind_mouseSens, (void *) false, "Descrease mouse sensitivity in capture mode"); - app_registerKeybind(KEY_UP , bind_toggleKey, (void *) PS2_VOLUME_UP , + app_registerKeybind(KEY_UP , 0 , bind_toggleKey, (void *) PS2_VOLUME_UP , "Send volume up to the guest"); - app_registerKeybind(KEY_DOWN, bind_toggleKey, (void *) PS2_VOLUME_DOWN, + app_registerKeybind(KEY_DOWN, 0 , bind_toggleKey, (void *) PS2_VOLUME_DOWN, "Send volume down to the guest"); - app_registerKeybind(KEY_M , bind_toggleKey, (void *) PS2_MUTE , + app_registerKeybind(0 , 'M', bind_toggleKey, (void *) PS2_MUTE , "Send mute to the guest"); - app_registerKeybind(KEY_LEFTMETA , bind_passthrough, NULL, + app_registerKeybind(KEY_LEFTMETA , 0, bind_passthrough, NULL, "Send LWin to the guest"); - app_registerKeybind(KEY_RIGHTMETA, bind_passthrough, NULL, + app_registerKeybind(KEY_RIGHTMETA, 0, bind_passthrough, NULL, "Send RWin to the guest"); #if ENABLE_AUDIO if (audio_supportsRecord()) { - app_registerKeybind(KEY_E, audio_recordToggleKeybind, NULL, + app_registerKeybind(0, 'E', audio_recordToggleKeybind, NULL, "Toggle audio recording"); } #endif @@ -186,29 +186,29 @@ void keybind_spiceRegister(void) /* register OS based keybinds */ if (app_guestIsLinux()) { - handles[handleCount++] = app_registerKeybind(KEY_F1 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F1 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F1 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F2 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F2 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F2 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F3 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F3 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F3 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F4 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F4 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F4 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F5 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F5 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F5 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F6 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F6 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F6 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F7 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F7 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F7 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F8 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F8 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F8 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F9 , bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F9 , 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F9 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F10, bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F10, 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F10 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F11, bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F11, 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F11 to the guest"); - handles[handleCount++] = app_registerKeybind(KEY_F12, bind_ctrlAltFn, NULL, + handles[handleCount++] = app_registerKeybind(KEY_F12, 0, bind_ctrlAltFn, NULL, "Send Ctrl+Alt+F12 to the guest"); } } diff --git a/client/src/main.c b/client/src/main.c index 5a422f15..5087e71e 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -1693,6 +1693,7 @@ static void lg_shutdown(void) } app_releaseAllKeybinds(); + ll_free(g_state.bindings); if (g_state.dsInitialized) g_state.ds->free(); @@ -1742,6 +1743,8 @@ int main(int argc, char * argv[]) egl_dynProcsInit(); gl_dynProcsInit(); + g_state.bindings = ll_new(); + g_state.overlays = ll_new(); app_registerOverlay(&LGOverlaySplash, NULL); app_registerOverlay(&LGOverlayConfig, NULL); diff --git a/client/src/main.h b/client/src/main.h index 4d1b23d9..0b8b1077 100644 --- a/client/src/main.h +++ b/client/src/main.h @@ -90,8 +90,7 @@ struct AppState uint64_t escapeTime; int escapeAction; bool escapeHelp; - KeybindHandle bindings[KEY_MAX]; - const char * keyDescription[KEY_MAX]; + struct ll * bindings; bool keyDown[KEY_MAX]; bool haveSrcSize; @@ -227,9 +226,11 @@ struct CBRequest struct KeybindHandle { - int sc; - KeybindFn callback; - void * opaque; + int sc; + int charcode; + KeybindFn callback; + const char * description; + void * opaque; }; enum WarpState diff --git a/client/src/overlay/fps.c b/client/src/overlay/fps.c index 2aff484d..bc78e921 100644 --- a/client/src/overlay/fps.c +++ b/client/src/overlay/fps.c @@ -53,7 +53,7 @@ static void fps_earlyInit(void) static bool fps_init(void ** udata, const void * params) { - app_registerKeybind(KEY_D, showFPSKeybind, NULL, "FPS display toggle"); + app_registerKeybind(0, 'D', showFPSKeybind, NULL, "FPS display toggle"); showFPS = option_get_bool("win", "showFPS"); return true; } diff --git a/client/src/overlay/graphs.c b/client/src/overlay/graphs.c index 5f2b5a52..9130b449 100644 --- a/client/src/overlay/graphs.c +++ b/client/src/overlay/graphs.c @@ -78,7 +78,7 @@ static void graphs_earlyInit(void) static bool graphs_init(void ** udata, const void * params) { app_overlayConfigRegister("Performance Metrics", configCallback, NULL); - app_registerKeybind(KEY_T, showTimingKeybind, NULL, + app_registerKeybind(0, 'T', showTimingKeybind, NULL, "Show frame timing information"); return true; } diff --git a/client/src/overlay/help.c b/client/src/overlay/help.c index 157a2602..973cc035 100644 --- a/client/src/overlay/help.c +++ b/client/src/overlay/help.c @@ -63,14 +63,20 @@ static int help_render(void * udata, bool interactive, struct Rect * windowRects igTableNextColumn(); igText("Toggle capture mode"); - for (int i = 0; i < KEY_MAX; ++i) - if (g_state.keyDescription[i]) - { - igTableNextColumn(); - igText("%s+%s", escapeName, linux_to_display[i]); - igTableNextColumn(); - igText(g_state.keyDescription[i]); - } + KeybindHandle handle; + ll_forEachNL(g_state.bindings, item, handle) + { + if (!handle->description) + continue; + + igTableNextColumn(); + if (handle->sc) + igText("%s+%s", escapeName, linux_to_display[handle->sc]); + else + igText("%s+%c", escapeName, handle->charcode); + igTableNextColumn(); + igText(handle->description); + } igEndTable(); }