From 2da3d3e6ba282d437c69a8d80b0a2e0f46214a39 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Mon, 23 Jan 2017 23:09:31 -0800 Subject: [PATCH] SDL: Automatically map controllers when plugged in --- CHANGES | 1 + src/platform/qt/InputController.cpp | 10 ++++++---- src/platform/qt/InputController.h | 9 ++++----- src/platform/sdl/sdl-events.c | 26 +++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/CHANGES b/CHANGES index 633dff928..dd5a6f692 100644 --- a/CHANGES +++ b/CHANGES @@ -53,6 +53,7 @@ Misc: - All: Move time.h include to common.h - CMake: Add ability to just print version string - Qt: Merge "Save" and "OK" buttons in shader options + - SDL: Automatically map controllers when plugged in 0.5.2: (2016-12-31) Bugfixes: diff --git a/src/platform/qt/InputController.cpp b/src/platform/qt/InputController.cpp index c27aa92ae..e5e53050c 100644 --- a/src/platform/qt/InputController.cpp +++ b/src/platform/qt/InputController.cpp @@ -48,14 +48,16 @@ InputController::InputController(int playerId, QWidget* topLevel, QObject* paren updateJoysticks(); #endif - m_gamepadTimer = new QTimer(this); #ifdef BUILD_SDL - connect(m_gamepadTimer, &QTimer::timeout, [this]() { + connect(&m_gamepadTimer, &QTimer::timeout, [this]() { testGamepad(SDL_BINDING_BUTTON); + if (m_playerId == 0) { + updateJoysticks(); + } }); #endif - m_gamepadTimer->setInterval(50); - m_gamepadTimer->start(); + m_gamepadTimer.setInterval(50); + m_gamepadTimer.start(); mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_X, GBA_KEY_A); mInputBindKey(&m_inputMap, KEYBOARD, Qt::Key_Z, GBA_KEY_B); diff --git a/src/platform/qt/InputController.h b/src/platform/qt/InputController.h index 1572b03e2..b2ad1a715 100644 --- a/src/platform/qt/InputController.h +++ b/src/platform/qt/InputController.h @@ -11,10 +11,9 @@ #include #include +#include #include -class QTimer; - #include #ifdef BUILD_SDL @@ -54,7 +53,6 @@ public: const mInputMap* map() const { return &m_inputMap; } - void updateJoysticks(); int pollEvents(); static const int32_t AXIS_THRESHOLD = 0x3000; @@ -66,7 +64,7 @@ public: void bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction, GBAKey); void unbindAllAxes(uint32_t type); - void bindHat(uint32_t type, int hat, GamepadHatEvent::Direction, GBAKey); + void bindHat(uint32_t type, int hat, GamepadHatEvent::Direction, GBAKey); QStringList connectedGamepads(uint32_t type) const; int gamepad(uint32_t type) const; @@ -92,6 +90,7 @@ signals: public slots: void testGamepad(int type); + void updateJoysticks(); // TODO: Move these to somewhere that makes sense void suspendScreensaver(); @@ -123,7 +122,7 @@ private: QSet m_activeButtons; QSet> m_activeAxes; QSet> m_activeHats; - QTimer* m_gamepadTimer; + QTimer m_gamepadTimer; QSet m_pendingEvents; }; diff --git a/src/platform/sdl/sdl-events.c b/src/platform/sdl/sdl-events.c index 08efac664..5acf28c38 100644 --- a/src/platform/sdl/sdl-events.c +++ b/src/platform/sdl/sdl-events.c @@ -339,8 +339,32 @@ void mSDLUpdateJoysticks(struct mSDLEvents* events) { #if SDL_VERSION_ATLEAST(2, 0, 0) joystick->haptic = SDL_HapticOpenFromJoystick(joystick->joystick); #endif + size_t i; + for (i = 0; (int) i < events->playersAttached; ++i) { + if (events->players[i]->joystick) { + continue; + } + + const char* joystickName; +#if SDL_VERSION_ATLEAST(2, 0, 0) + joystickName = SDL_JoystickName(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick); +#else + joystickName = SDL_JoystickName(SDL_JoystickIndex(SDL_JoystickListGetPointer(&events->joysticks, i)->joystick)); +#endif + if (events->preferredJoysticks[i] && strcmp(events->preferredJoysticks[i], joystickName) == 0) { + events->players[i]->joystick = joystick; + return; + } + } + for (i = 0; (int) i < events->playersAttached; ++i) { + if (events->players[i]->joystick) { + continue; + } + events->players[i]->joystick = joystick; + break; + } } else if (event.type == SDL_JOYDEVICEREMOVED) { - SDL_JoystickID ids[MAX_PLAYERS]; + SDL_JoystickID ids[MAX_PLAYERS] = { 0 }; size_t i; for (i = 0; (int) i < events->playersAttached; ++i) { if (events->players[i]->joystick) {