diff --git a/src/platform/qt/InputController.cpp b/src/platform/qt/InputController.cpp index e17cd16e6..93a8d58ef 100644 --- a/src/platform/qt/InputController.cpp +++ b/src/platform/qt/InputController.cpp @@ -25,7 +25,7 @@ int InputController::s_sdlInited = 0; GBASDLEvents InputController::s_sdlEvents; #endif -InputController::InputController(int playerId, QObject* parent) +InputController::InputController(int playerId, QWidget* topLevel, QObject* parent) : QObject(parent) , m_playerId(playerId) , m_config(nullptr) @@ -34,6 +34,7 @@ InputController::InputController(int playerId, QObject* parent) , m_playerAttached(false) #endif , m_allowOpposing(false) + , m_topLevel(topLevel) { GBAInputMapInit(&m_inputMap); @@ -432,7 +433,7 @@ void InputController::testGamepad(int type) { if (newlyAboveThreshold) { GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, newlyAboveThreshold, type, this); postPendingEvent(event->gbaKey()); - QApplication::sendEvent(QApplication::focusWidget(), event); + sendGamepadEvent(event); if (!event->isAccepted()) { clearPendingEvent(event->gbaKey()); } @@ -441,7 +442,7 @@ void InputController::testGamepad(int type) { for (auto axis : oldAxes) { GamepadAxisEvent* event = new GamepadAxisEvent(axis.first, axis.second, false, type, this); clearPendingEvent(event->gbaKey()); - QApplication::sendEvent(QApplication::focusWidget(), event); + sendGamepadEvent(event); } if (!QApplication::focusWidget()) { @@ -454,7 +455,7 @@ void InputController::testGamepad(int type) { for (int button : activeButtons) { GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Down(), button, type, this); postPendingEvent(event->gbaKey()); - QApplication::sendEvent(QApplication::focusWidget(), event); + sendGamepadEvent(event); if (!event->isAccepted()) { clearPendingEvent(event->gbaKey()); } @@ -462,10 +463,23 @@ void InputController::testGamepad(int type) { for (int button : oldButtons) { GamepadButtonEvent* event = new GamepadButtonEvent(GamepadButtonEvent::Up(), button, type, this); clearPendingEvent(event->gbaKey()); - QApplication::sendEvent(QApplication::focusWidget(), event); + sendGamepadEvent(event); } } +void InputController::sendGamepadEvent(QEvent* event) { + QWidget* focusWidget = nullptr; + if (m_topLevel) { + focusWidget = m_topLevel->focusWidget(); + if (!focusWidget) { + focusWidget = m_topLevel; + } + } else { + focusWidget = QApplication::focusWidget(); + } + QApplication::sendEvent(focusWidget, event); +} + void InputController::postPendingEvent(GBAKey key) { m_pendingEvents.insert(key); } diff --git a/src/platform/qt/InputController.h b/src/platform/qt/InputController.h index 65bdec0f0..4a8501dd3 100644 --- a/src/platform/qt/InputController.h +++ b/src/platform/qt/InputController.h @@ -32,7 +32,7 @@ Q_OBJECT public: static const uint32_t KEYBOARD = 0x51545F4B; - InputController(int playerId = 0, QObject* parent = nullptr); + InputController(int playerId = 0, QWidget* topLevel = nullptr, QObject* parent = nullptr); ~InputController(); void setConfiguration(ConfigController* config); @@ -94,11 +94,13 @@ private: void postPendingEvent(GBAKey); void clearPendingEvent(GBAKey); bool hasPendingEvent(GBAKey) const; + void sendGamepadEvent(QEvent*); GBAInputMap m_inputMap; ConfigController* m_config; int m_playerId; bool m_allowOpposing; + QWidget* m_topLevel; #ifdef BUILD_SDL static int s_sdlInited; diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index 0a31f17ec..d0e1e2fd3 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -53,7 +53,7 @@ Window::Window(ConfigController* config, int playerId, QWidget* parent) , m_screenWidget(new WindowBackground()) , m_logo(":/res/mgba-1024.png") , m_config(config) - , m_inputController(playerId) + , m_inputController(playerId, this) #ifdef USE_FFMPEG , m_videoView(nullptr) #endif