From 6750e7775e1c5d6cf191f05219bfb57f5fff19fe Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Fri, 17 Apr 2015 23:55:38 -0700 Subject: [PATCH] Qt: Rough deadzone estimation --- CHANGES | 1 + src/platform/qt/GBAKeyEditor.cpp | 6 ++++-- src/platform/qt/InputController.cpp | 21 +++++++++++++++++---- src/platform/qt/InputController.h | 3 +++ 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGES b/CHANGES index c54452833..a14061e4a 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,7 @@ Features: - Rewind now shows the frame after rewinding - Import/Export of GameShark/Action Replay snapshots - Add "Step backwards" item for single increment rewind + - Deadzone estimation for game controllers Bugfixes: - GBA: Fix timers not updating timing when writing to only the reload register - All: Fix sanitize-deb script not cleaning up after itself diff --git a/src/platform/qt/GBAKeyEditor.cpp b/src/platform/qt/GBAKeyEditor.cpp index 97158af94..66a9160b3 100644 --- a/src/platform/qt/GBAKeyEditor.cpp +++ b/src/platform/qt/GBAKeyEditor.cpp @@ -47,9 +47,10 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, const QString& refresh(); #ifdef BUILD_SDL - lookupAxes(map); - if (type == SDL_BINDING_BUTTON) { + controller->recalibrateAxes(); + lookupAxes(map); + m_profileSelect = new QComboBox(this); m_profileSelect->addItems(controller->connectedGamepads(type)); int activeGamepad = controller->gamepad(type); @@ -61,6 +62,7 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, const QString& m_controller->setGamepad(m_type, i); m_profile = m_profileSelect->currentText(); m_controller->loadProfile(m_type, m_profile); + m_controller->recalibrateAxes(); refresh(); }); } diff --git a/src/platform/qt/InputController.cpp b/src/platform/qt/InputController.cpp index 4ec263108..c8e96e2c6 100644 --- a/src/platform/qt/InputController.cpp +++ b/src/platform/qt/InputController.cpp @@ -226,14 +226,27 @@ QSet InputController::activeGamepadButtons() { return activeButtons; } +void InputController::recalibrateAxes() { + SDL_Joystick* joystick = m_sdlPlayer.joystick; + SDL_JoystickUpdate(); + int numAxes = SDL_JoystickNumAxes(joystick); + m_deadzones.resize(numAxes); + int i; + for (i = 0; i < numAxes; ++i) { + m_deadzones[i] = SDL_JoystickGetAxis(joystick, i); + } +} + QSet> InputController::activeGamepadAxes() { SDL_Joystick* joystick = m_sdlPlayer.joystick; SDL_JoystickUpdate(); - int numButtons = SDL_JoystickNumAxes(joystick); + int numAxes = SDL_JoystickNumAxes(joystick); + m_deadzones.resize(numAxes); QSet> activeAxes; int i; - for (i = 0; i < numButtons; ++i) { + for (i = 0; i < numAxes; ++i) { int32_t axis = SDL_JoystickGetAxis(joystick, i); + axis -= m_deadzones[i]; if (axis >= AXIS_THRESHOLD || axis <= -AXIS_THRESHOLD) { activeAxes.insert(qMakePair(i, axis > 0 ? GamepadAxisEvent::POSITIVE : GamepadAxisEvent::NEGATIVE)); } @@ -250,11 +263,11 @@ void InputController::bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direct switch (direction) { case GamepadAxisEvent::NEGATIVE: description.lowDirection = key; - description.deadLow = -AXIS_THRESHOLD; + description.deadLow = m_deadzones[axis] - AXIS_THRESHOLD; break; case GamepadAxisEvent::POSITIVE: description.highDirection = key; - description.deadHigh = AXIS_THRESHOLD; + description.deadHigh = m_deadzones[axis] + AXIS_THRESHOLD; break; default: return; diff --git a/src/platform/qt/InputController.h b/src/platform/qt/InputController.h index b2864d382..c9a07f9fa 100644 --- a/src/platform/qt/InputController.h +++ b/src/platform/qt/InputController.h @@ -10,6 +10,7 @@ #include #include +#include class QTimer; @@ -56,6 +57,7 @@ public: int testSDLEvents(); QSet activeGamepadButtons(); QSet> activeGamepadAxes(); + void recalibrateAxes(); void bindAxis(uint32_t type, int axis, GamepadAxisEvent::Direction, GBAKey); @@ -84,6 +86,7 @@ private: static GBASDLEvents s_sdlEvents; GBASDLPlayer m_sdlPlayer; bool m_playerAttached; + QVector m_deadzones; #endif QSet m_activeButtons;