From 7c9b273f705b71dadc587f23f3dac839eb2b5ccc Mon Sep 17 00:00:00 2001 From: Tudor Brindus Date: Sun, 17 Jan 2021 21:20:42 -0500 Subject: [PATCH] [client] app: add and expose app_handleKeyPress and app_handleKeyRelease --- client/include/app.h | 2 + client/src/main.c | 166 +++++++++++++++++++++++-------------------- 2 files changed, 90 insertions(+), 78 deletions(-) diff --git a/client/include/app.h b/client/include/app.h index 72d92744..2ae2ad37 100644 --- a/client/include/app.h +++ b/client/include/app.h @@ -37,6 +37,8 @@ void app_handleMouseNormal(double ex, double ey); void app_handleMouseBasic(void); void app_handleButtonPress(int button); void app_handleButtonRelease(int button); +void app_handleKeyPress(int scancode); +void app_handleKeyRelease(int scancode); void app_handleWindowEnter(void); void app_handleWindowLeave(void); void app_handleFocusEvent(bool focused); diff --git a/client/src/main.c b/client/src/main.c index 13578b14..4976821e 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -1031,6 +1031,92 @@ void app_handleButtonRelease(int button) DEBUG_ERROR("SDL_MOUSEBUTTONUP: failed to send message"); } +void app_handleKeyPress(int sc) +{ + if (sc == params.escapeKey && !g_state.escapeActive) + { + g_state.escapeActive = true; + g_state.escapeAction = -1; + return; + } + + if (g_state.escapeActive) + { + g_state.escapeAction = sc; + return; + } + + if (!app_inputEnabled()) + return; + + if (params.ignoreWindowsKeys && + (sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI)) + return; + + + uint32_t scancode = mapScancode(sc); + if (scancode == 0) + return; + + if (!g_state.keyDown[sc]) + { + if (spice_key_down(scancode)) + g_state.keyDown[sc] = true; + else + { + DEBUG_ERROR("SDL_KEYDOWN: failed to send message"); + return; + } + } +} + +void app_handleKeyRelease(int sc) +{ + if (g_state.escapeActive) + { + if (g_state.escapeAction == -1) + { + if (params.useSpiceInput) + setGrab(!g_cursor.grab); + } + else + { + KeybindHandle handle = g_state.bindings[sc]; + if (handle) + { + handle->callback(sc, handle->opaque); + return; + } + } + + if (sc == params.escapeKey) + g_state.escapeActive = false; + } + + if (!app_inputEnabled()) + return; + + // avoid sending key up events when we didn't send a down + if (!g_state.keyDown[sc]) + return; + + if (params.ignoreWindowsKeys && + (sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI)) + return; + + uint32_t scancode = mapScancode(sc); + if (scancode == 0) + return; + + if (spice_key_up(scancode)) + g_state.keyDown[sc] = false; + else + { + DEBUG_ERROR("SDL_KEYUP: failed to send message"); + return; + } +} + static void guestCurToLocal(struct DoublePoint *local) { local->x = g_state.dstRect.x + @@ -1374,90 +1460,14 @@ int eventFilter(void * userdata, SDL_Event * event) case SDL_KEYDOWN: { SDL_Scancode sc = event->key.keysym.scancode; - if (sc == params.escapeKey && !g_state.escapeActive) - { - g_state.escapeActive = true; - g_state.escapeAction = -1; - break; - } - - if (g_state.escapeActive) - { - g_state.escapeAction = sc; - break; - } - - if (!app_inputEnabled()) - break; - - if (params.ignoreWindowsKeys && - (sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI)) - break; - - - uint32_t scancode = mapScancode(sc); - if (scancode == 0) - break; - - if (!g_state.keyDown[sc]) - { - if (spice_key_down(scancode)) - g_state.keyDown[sc] = true; - else - { - DEBUG_ERROR("SDL_KEYDOWN: failed to send message"); - break; - } - } + app_handleKeyPress(sc); break; } case SDL_KEYUP: { SDL_Scancode sc = event->key.keysym.scancode; - if (g_state.escapeActive) - { - if (g_state.escapeAction == -1) - { - if (params.useSpiceInput) - setGrab(!g_cursor.grab); - } - else - { - KeybindHandle handle = g_state.bindings[sc]; - if (handle) - { - handle->callback(sc, handle->opaque); - break; - } - } - - if (sc == params.escapeKey) - g_state.escapeActive = false; - } - - if (!app_inputEnabled()) - break; - - // avoid sending key up events when we didn't send a down - if (!g_state.keyDown[sc]) - break; - - if (params.ignoreWindowsKeys && - (sc == SDL_SCANCODE_LGUI || sc == SDL_SCANCODE_RGUI)) - break; - - uint32_t scancode = mapScancode(sc); - if (scancode == 0) - break; - - if (spice_key_up(scancode)) - g_state.keyDown[sc] = false; - else - { - DEBUG_ERROR("SDL_KEYUP: failed to send message"); - break; - } + app_handleKeyRelease(sc); break; } }