SDL: Automatically map controllers when plugged in

This commit is contained in:
Vicki Pfau 2017-01-23 23:09:31 -08:00
parent 2f14f58911
commit 2da3d3e6ba
4 changed files with 36 additions and 10 deletions

View File

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

View File

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

View File

@ -11,10 +11,9 @@
#include <QObject>
#include <QSet>
#include <QTimer>
#include <QVector>
class QTimer;
#include <mgba/internal/gba/input.h>
#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<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;
};

View File

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