all repos — mgba @ 75fb2548bbb6dea49e6d1040e3b59d76f5548fa6

mGBA Game Boy Advance Emulator

Qt: Bind controllers to specific windows
Jeffrey Pfau jeffrey@endrift.com
Sat, 18 Jul 2015 00:20:54 -0700
commit

75fb2548bbb6dea49e6d1040e3b59d76f5548fa6

parent

85c4162ad1203c40d930f04b0b8ae550d7d2e353

M src/platform/qt/InputController.cppsrc/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 @@ #ifdef BUILD_SDL

, m_playerAttached(false) #endif , m_allowOpposing(false) + , m_topLevel(topLevel) { GBAInputMapInit(&m_inputMap);

@@ -432,7 +433,7 @@ bool newlyAboveThreshold = activeAxes.contains(axis);

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 @@ }

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 @@

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,8 +463,21 @@ }

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 src/platform/qt/InputController.hsrc/platform/qt/InputController.h

@@ -32,7 +32,7 @@

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;
M src/platform/qt/Window.cppsrc/platform/qt/Window.cpp

@@ -53,7 +53,7 @@ , m_stateWindow(nullptr)

, 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