all repos — mgba @ 2da3d3e6ba282d437c69a8d80b0a2e0f46214a39

mGBA Game Boy Advance Emulator

SDL: Automatically map controllers when plugged in
Vicki Pfau vi@endrift.com
Mon, 23 Jan 2017 23:09:31 -0800
commit

2da3d3e6ba282d437c69a8d80b0a2e0f46214a39

parent

2f14f58911539fd67228c9113665c5df26061154

M CHANGESCHANGES

@@ -53,6 +53,7 @@ - Feature: Support ImageMagick 7

- 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:
M src/platform/qt/InputController.cppsrc/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.hsrc/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.csrc/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) {