SDL: Automatically map controllers when plugged in
Vicki Pfau vi@endrift.com
Mon, 23 Jan 2017 23:09:31 -0800
4 files changed,
36 insertions(+),
10 deletions(-)
M
src/platform/qt/InputController.cpp
→
src/platform/qt/InputController.cpp
@@ -48,14 +48,16 @@ mSDLInitBindingsGBA(&m_inputMap);
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);
M
src/platform/qt/InputController.h
→
src/platform/qt/InputController.h
@@ -11,9 +11,8 @@ #include "GamepadHatEvent.h"
#include <QObject> #include <QSet> +#include <QTimer> #include <QVector> - -class QTimer; #include <mgba/internal/gba/input.h>@@ -54,7 +53,6 @@ void bindKey(uint32_t type, int key, GBAKey);
const mInputMap* map() const { return &m_inputMap; } - void updateJoysticks(); int pollEvents(); static const int32_t AXIS_THRESHOLD = 0x3000;@@ -66,7 +64,7 @@
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 @@ void profileLoaded(const QString& profile);
public slots: void testGamepad(int type); + void updateJoysticks(); // TODO: Move these to somewhere that makes sense void suspendScreensaver();@@ -123,7 +122,7 @@
QSet<int> m_activeButtons; QSet<QPair<int, GamepadAxisEvent::Direction>> m_activeAxes; QSet<QPair<int, GamepadHatEvent::Direction>> m_activeHats; - QTimer* m_gamepadTimer; + QTimer m_gamepadTimer; QSet<GBAKey> m_pendingEvents; };
M
src/platform/sdl/sdl-events.c
→
src/platform/sdl/sdl-events.c
@@ -339,8 +339,32 @@ joystick->index = SDL_JoystickListSize(&events->joysticks) - 1;
#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) {