From c2c86a3dcdf6d5e790981bdf115ef05c4897ebfe Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sun, 25 Oct 2020 18:46:26 +0200 Subject: Window: Workaround for a key handling issue Likely a rare situation, but this workaround should be relatively safe -- ignore keydown events if they occur less than 10 milliseconds after the window has received input focus. IssueID #10 --- src/ui/window.c | 13 +++++++++++++ src/ui/window.h | 1 + 2 files changed, 14 insertions(+) (limited to 'src/ui') diff --git a/src/ui/window.c b/src/ui/window.c index 1599d372..105dcfb3 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -525,6 +525,7 @@ void init_Window(iWindow *d, iRect rect) { d->pendingCursor = NULL; d->isDrawFrozen = iTrue; d->isMouseInside = iTrue; + d->focusGainedAt = 0; uint32_t flags = 0; #if defined (iPlatformApple) SDL_SetHint(SDL_HINT_RENDER_DRIVER, shouldDefaultToMetalRenderer_MacOS() ? "metal" : "opengl"); @@ -668,6 +669,12 @@ static iBool handleWindowEvent_Window_(iWindow *d, const SDL_WindowEvent *ev) { d->isMouseInside = iTrue; postCommand_App("window.mouse.entered"); return iTrue; + case SDL_WINDOWEVENT_TAKE_FOCUS: + SDL_SetWindowInputFocus(d->win); + return iTrue; + case SDL_WINDOWEVENT_FOCUS_GAINED: + d->focusGainedAt = SDL_GetTicks(); + return iFalse; default: break; } @@ -699,6 +706,12 @@ iBool processEvent_Window(iWindow *d, const SDL_Event *ev) { postRefresh_App(); return iTrue; } + if (event.type == SDL_KEYDOWN && SDL_GetTicks() - d->focusGainedAt < 10) { + /* Suspiciously close to when input focus was received. For example under openbox, + closing xterm with Ctrl+D will cause the keydown event to "spill" over to us. + As a workaround, ignore these events. */ + return iFalse; + } /* Map mouse pointer coordinate to our coordinate system. */ if (event.type == SDL_MOUSEMOTION) { setCursor_Window(d, SDL_SYSTEM_CURSOR_ARROW); /* default cursor */ diff --git a/src/ui/window.h b/src/ui/window.h index 3ede1578..18df0cf9 100644 --- a/src/ui/window.h +++ b/src/ui/window.h @@ -38,6 +38,7 @@ struct Impl_Window { iRect lastRect; /* updated when window is moved/resized */ iBool isDrawFrozen; /* avoids premature draws while restoring window state */ iBool isMouseInside; + uint32_t focusGainedAt; SDL_Renderer *render; iWidget * root; float pixelRatio; -- cgit v1.2.3