diff --git a/Common/UI/Screen.cpp b/Common/UI/Screen.cpp index 53de5bbea1..586bf147ae 100644 --- a/Common/UI/Screen.cpp +++ b/Common/UI/Screen.cpp @@ -108,6 +108,7 @@ void ScreenManager::cancelScreensAbove(Screen *screen) { void ScreenManager::update() { std::lock_guard guard(inputLock_); + if (cancelScreensAbove_) { bool found = false; for (int i = (int)stack_.size() - 1; i >= 0; i--) { @@ -129,9 +130,11 @@ void ScreenManager::update() { // NOTE: This is not a full UIScreen update, to avoid double global event processing. overlayScreen_->update(); } - // The background screen doesn't need updating. + + // Only the front screen gets update(). if (!stack_.empty()) { stack_.back().screen->update(); + passInputToMapper_ = stack_.back().screen->PassInputToMapper(); } } diff --git a/Common/UI/Screen.h b/Common/UI/Screen.h index 4542b9e7e0..22a66dc6e3 100644 --- a/Common/UI/Screen.h +++ b/Common/UI/Screen.h @@ -75,6 +75,15 @@ enum class ViewLayoutMode { IgnoreBottomInset, }; +enum class InputMode { + None = 0, + Keyboard = 1, + Mouse = 2, + Other = 4, + ImDebuggerToggle = 8, // ugly hack +}; +ENUM_CLASS_BITOPS(InputMode); + class Screen { public: Screen() = default; @@ -102,6 +111,10 @@ public: virtual void RecreateViews() {} + // NOTE: This is polled every frame, not called on every input event. This is + // a step towards eventually removing the ScreenManager input mutex. + virtual InputMode PassInputToMapper() const { return InputMode::None; } + ScreenManager *screenManager() { return screenManager_; } const ScreenManager *screenManager() const { return screenManager_; } void setScreenManager(ScreenManager *sm) { screenManager_ = sm; } @@ -136,7 +149,7 @@ enum { LAYER_TRANSPARENT = 2, }; -typedef void(*PostRenderCallback)(UIContext *ui, void *userdata); +typedef void (*PostRenderCallback)(UIContext *ui, void *userdata); class ScreenManager { public: @@ -192,6 +205,10 @@ public: // Will delete any existing overlay screen. void SetBackgroundOverlayScreens(Screen *backgroundScreen, Screen *overlayScreen); + InputMode PassInputToMapper() const { + return passInputToMapper_; + } + std::recursive_mutex inputLock_; private: @@ -225,4 +242,6 @@ private: std::vector nextStack_; std::unordered_map lastAxis_; + + InputMode passInputToMapper_ = InputMode::None; }; diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index cc9cda268d..2cfa064173 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -1170,13 +1170,27 @@ void EmuScreen::OnVKeyAnalog(VirtKey virtualKeyCode, float value) { limitMode = PSP_CoreParameter().analogFpsLimit == 60 ? FPSLimit::NORMAL : FPSLimit::ANALOG; } -bool EmuScreen::AllowKeyboardNavigation() const { - return true; +InputMode EmuScreen::PassInputToMapper() const { + const bool chatMenuOpen = chatMenu_ && chatMenu_->GetVisibility() == UI::V_VISIBLE; + if (chatMenuOpen) { + return InputMode::None; + } + + InputMode modes = InputMode::Keyboard | InputMode::Mouse | InputMode::Other | InputMode::ImDebuggerToggle; + if (g_Config.bShowImDebugger && imguiInited_) { + if (ImGui::GetIO().WantCaptureKeyboard) { + modes &= ~InputMode::Keyboard; + } + if (ImGui::GetIO().WantCaptureMouse) { + modes &= ~InputMode::Mouse; + } + return modes; + } + + return modes; } bool EmuScreen::UnsyncKey(const KeyInput &key) { - System_Notify(SystemNotification::ACTIVITY); - // Update imgui modifier flags if (key.flags & (KeyInputFlags::DOWN | KeyInputFlags::UP)) { bool down = (key.flags & KeyInputFlags::DOWN) != 0; @@ -1191,58 +1205,12 @@ bool EmuScreen::UnsyncKey(const KeyInput &key) { } } - const bool chatMenuOpen = chatMenu_ && chatMenu_->GetVisibility() == UI::V_VISIBLE; - if (chatMenuOpen) { - // Let up-events through to the controlMapper_ so input doesn't get stuck. - if (key.flags & KeyInputFlags::UP) { - g_controlMapper.Key(key); - } - return UIScreen::UnsyncKey(key); - } - - if (g_Config.bShowImDebugger && imguiInited_) { - // Note: Allow some Vkeys through, so we can toggle the imgui for example (since we actually block the control mapper otherwise in imgui mode). - // We need to manually implement it here :/ - if (key.flags & (KeyInputFlags::UP | KeyInputFlags::DOWN)) { - InputMapping mapping(key.deviceId, key.keyCode); - std::vector pspButtons; - bool mappingFound = KeyMap::InputMappingToPspButton(mapping, &pspButtons); - if (mappingFound) { - for (auto b : pspButtons) { - if (b == VIRTKEY_TOGGLE_DEBUGGER || b == VIRTKEY_PAUSE) { - return g_controlMapper.Key(key); - } - } - } - } - UI::EnableFocusMovement(false); - // Enable gamepad controls while running imgui (but ignore mouse/keyboard). - switch (key.deviceId) { - case DEVICE_ID_KEYBOARD: - if (!ImGui::GetIO().WantCaptureKeyboard) { - g_controlMapper.Key(key); - } - break; - case DEVICE_ID_MOUSE: - if (!ImGui::GetIO().WantCaptureMouse) { - g_controlMapper.Key(key); - } - break; - default: - g_controlMapper.Key(key); - break; - } - - return UIScreen::UnsyncKey(key); - } - - return g_controlMapper.Key(key); + return UIScreen::UnsyncKey(key); } void EmuScreen::UnsyncAxis(const AxisInput *axes, size_t count) { - System_Notify(SystemNotification::ACTIVITY); - if (UI::IsFocusMovementEnabled()) { + // Otherwise no use for axis events - we send them to the control mapper. return UIScreen::UnsyncAxis(axes, count); } diff --git a/UI/EmuScreen.h b/UI/EmuScreen.h index c9c951f124..deeda676fa 100644 --- a/UI/EmuScreen.h +++ b/UI/EmuScreen.h @@ -52,6 +52,8 @@ public: void resized() override; ScreenRenderRole renderRole(bool isTop) const override; + InputMode PassInputToMapper() const override; + // Note: Unlike your average boring UIScreen, here we override the Unsync* functions // to get minimal latency and full control. We forward to UIScreen when needed. bool UnsyncKey(const KeyInput &key) override; @@ -69,8 +71,6 @@ public: } protected: - bool AllowKeyboardNavigation() const override; - void darken(); void focusChanged(ScreenFocusChange focusChange) override; ScreenRenderFlags PreRender(ScreenRenderMode mode) override; diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index d4265aa738..576fa113c2 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1386,6 +1386,8 @@ static Modifier g_modifiersPressed{}; bool NativeKey(const KeyInput &key) { double now = time_now_d(); + System_Notify(SystemNotification::ACTIVITY); + // VR actions if ((IsVREnabled() || g_Config.bForceVR) && !UpdateVRKeys(key)) { return false; @@ -1437,6 +1439,46 @@ bool NativeKey(const KeyInput &key) { return false; } + // Filtering, detailed rules needed for good imgui behavior without having to ask the screen about what to do. + InputMode inputMode = g_screenManager->PassInputToMapper(); + bool passKeyThrough = false; + if (inputMode != InputMode::None) { + if (key.flags & (KeyInputFlags::UP | KeyInputFlags::DOWN)) { + InputMapping mapping(key.deviceId, key.keyCode); + std::vector pspButtons; + bool mappingFound = KeyMap::InputMappingToPspButton(mapping, &pspButtons); + if (mappingFound) { + for (auto b : pspButtons) { + if (b == VIRTKEY_TOGGLE_DEBUGGER || b == VIRTKEY_PAUSE) { + // TRUE + passKeyThrough = true; + } + } + } + } + if (key.deviceId == DEVICE_ID_MOUSE) { + if (inputMode & InputMode::Mouse) { + passKeyThrough = true; + } + } else if (key.deviceId == DEVICE_ID_KEYBOARD) { + if (inputMode & InputMode::Keyboard) { + passKeyThrough = true; + } + } else if (inputMode & InputMode::Other) { + // yes this is different + passKeyThrough = true; + } + } else { + // Pass through only up events. + if (key.flags & KeyInputFlags::UP) { + passKeyThrough = true; + } + } + + if (passKeyThrough) { + g_controlMapper.Key(key); + } + // Handle releases of mousewheel keys. if ((key.flags & KeyInputFlags::DOWN) && key.deviceId == DEVICE_ID_MOUSE && (key.keyCode == NKCODE_EXT_MOUSEWHEEL_UP || key.keyCode == NKCODE_EXT_MOUSEWHEEL_DOWN)) { ProcessWheelRelease(key.keyCode, now, true); @@ -1512,11 +1554,17 @@ void NativeAxis(const AxisInput *axes, size_t count) { return; } + System_Notify(SystemNotification::ACTIVITY); + if (!g_screenManager) { // Too early. return; } + if (g_screenManager->PassInputToMapper() & InputMode::Other) { + g_controlMapper.Axis(axes, count); + } + g_screenManager->axis(axes, count); for (size_t i = 0; i < count; i++) {