From 481ea0b776bbab92b500540f59c5a191c6e93cba Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 9 Sep 2017 22:54:01 +0300 Subject: Add workaround for fix alt-tab issue in SDL2. Also add option to enable/disable this workaround. --- src/input/inputmanager.cpp | 16 ++++++++++++++++ src/input/joystick.cpp | 6 ++++-- src/input/keyboardconfig.cpp | 37 ++++++++++++++++++++++++++++++++++++- src/input/keyboardconfig.h | 6 ++++++ 4 files changed, 62 insertions(+), 3 deletions(-) (limited to 'src/input') diff --git a/src/input/inputmanager.cpp b/src/input/inputmanager.cpp index dbe82d5da..42a4e3b25 100644 --- a/src/input/inputmanager.cpp +++ b/src/input/inputmanager.cpp @@ -594,6 +594,14 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 { case SDL_KEYDOWN: { +#ifdef USE_SDL2 + if (keyboard.ignoreKey(event)) + { + BLOCK_END("InputManager::handleEvent") + return true; + } +#endif // USE_SDL2 + keyboard.refreshActiveKeys(); updateConditionMask(); if (handleAssignKey(event, InputType::KEYBOARD)) @@ -620,6 +628,14 @@ bool InputManager::handleEvent(const SDL_Event &restrict event) restrict2 } case SDL_KEYUP: { +#ifdef USE_SDL2 + if (keyboard.ignoreKey(event)) + { + BLOCK_END("InputManager::handleEvent") + return true; + } +#endif // USE_SDL2 + keyboard.refreshActiveKeys(); updateConditionMask(); keyboard.handleDeActicateKey(event); diff --git a/src/input/joystick.cpp b/src/input/joystick.cpp index 1e53af5b1..10ed33924 100644 --- a/src/input/joystick.cpp +++ b/src/input/joystick.cpp @@ -180,7 +180,8 @@ void Joystick::logic() mDirection = 0; - if (mUseInactive || settings.inputFocused) + if (mUseInactive || + settings.inputFocused != KeyboardFocus::Unfocused) { // X-Axis int position = SDL_JoystickGetAxis(mJoystick, 0); @@ -350,7 +351,8 @@ bool Joystick::validate() const if (mCalibrating || !mEnabled || !mCalibrated) return false; - return mUseInactive || settings.inputFocused; + return mUseInactive || + settings.inputFocused != KeyboardFocus::Unfocused; } void Joystick::handleRepeat(const int time) diff --git a/src/input/keyboardconfig.cpp b/src/input/keyboardconfig.cpp index 1ce317db7..e90d126a5 100644 --- a/src/input/keyboardconfig.cpp +++ b/src/input/keyboardconfig.cpp @@ -23,6 +23,9 @@ #include "input/keyboardconfig.h" #include "configuration.h" +#ifdef USE_SDL2 +#include "settings.h" +#endif // USE_SDL2 #include "input/inputmanager.h" @@ -42,7 +45,8 @@ KeyboardConfig::KeyboardConfig() : mRepeatTime(0), mKeyToAction(), mKeyToId(), - mKeyTimeMap() + mKeyTimeMap(), + mBlockAltTab(true) { } @@ -52,6 +56,7 @@ void KeyboardConfig::init() delete [] mActiveKeys2; mActiveKeys2 = new uint8_t[500]; mRepeatTime = config.getIntValue("repeateInterval2") / 10; + mBlockAltTab = config.getBoolValue("blockAltTab"); } void KeyboardConfig::deinit() @@ -270,3 +275,33 @@ void KeyboardConfig::resetRepeat(const int key) if (it != mKeyTimeMap.end()) (*it).second = tick_time; } + +#ifdef USE_SDL2 + +bool KeyboardConfig::ignoreKey(const SDL_Event &restrict event) +{ + if (!mBlockAltTab || + mActiveKeys == nullptr) + { + return false; + } + const int key = event.key.keysym.scancode; + if (key == SDL_SCANCODE_TAB) + { +#if SDL_VERSION_ATLEAST(2, 0, 5) + // SDL_WINDOWEVENT_TAKE_FOCUS not triggered after focus restored + if (settings.inputFocused != KeyboardFocus::Focused2) + return true; +#endif // SDL_VERSION_ATLEAST(2, 0, 5) + + if (mActiveKeys[SDL_SCANCODE_LALT] != 0) + return true; + } + else if (key == SDL_SCANCODE_LALT) + { + if (mActiveKeys[SDL_SCANCODE_TAB] != 0) + return true; + } + return false; +} +#endif // USE_SDL2 diff --git a/src/input/keyboardconfig.h b/src/input/keyboardconfig.h index 1759e4bb9..ef3f056b7 100644 --- a/src/input/keyboardconfig.h +++ b/src/input/keyboardconfig.h @@ -129,6 +129,10 @@ class KeyboardConfig final void resetRepeat(const int key); +#ifdef USE_SDL2 + bool ignoreKey(const SDL_Event &restrict event) A_WARN_UNUSED; +#endif // USE_SDL2 + private: bool mEnabled; /**< Flag to respond to key input */ @@ -143,6 +147,8 @@ class KeyboardConfig final KeyToIdMap mKeyToId; KeyTimeMap mKeyTimeMap; + + bool mBlockAltTab; }; extern KeyboardConfig keyboard; -- cgit v1.2.3-70-g09d2