From b097648dde9ed6c431592f3ca6e7c34e08d7575c Mon Sep 17 00:00:00 2001 From: Conn O'Griofa Date: Fri, 8 Jun 2018 04:40:02 +0000 Subject: [PATCH] InputManager: improve trigger axis calibration Some drivers (such as hid-sony) configure analog triggers (L2/R2) as axes with axis range -32768-32767, but the resting value starts at -32768, causing an initial or light press to register erroneously as a minus axis value. Fix by shifting ABS_Z and ABS_RZ axes that are detected to rest at -32768 so that they will range from 0-32767 instead. Patch based on Jools Wills' earlier commit: https://github.com/RetroPie/EmulationStation/pull/58 --- es-core/src/InputManager.cpp | 24 +++++++++++++++++++----- es-core/src/InputManager.h | 1 + 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/es-core/src/InputManager.cpp b/es-core/src/InputManager.cpp index 08c16865c..dbf6e5788 100644 --- a/es-core/src/InputManager.cpp +++ b/es-core/src/InputManager.cpp @@ -101,7 +101,14 @@ void InputManager::addJoystickByDeviceIndex(int id) // set up the prevAxisValues int numAxes = SDL_JoystickNumAxes(joy); mPrevAxisValues[joyId] = new int[numAxes]; - std::fill(mPrevAxisValues[joyId], mPrevAxisValues[joyId] + numAxes, 0); //initialize array to 0 + mInitAxisValues[joyId] = new int[numAxes]; + + int axis; + for (int i = 0; i< numAxes; i++) { + axis = SDL_JoystickGetAxis(joy, i); + mInitAxisValues[joyId][i] = axis; + mPrevAxisValues[joyId][i] = axis; + } } void InputManager::removeJoystickByJoystickID(SDL_JoystickID joyId) @@ -198,21 +205,28 @@ InputConfig* InputManager::getInputConfigByDevice(int device) bool InputManager::parseEvent(const SDL_Event& ev, Window* window) { bool causedEvent = false; + int axis; switch(ev.type) { case SDL_JOYAXISMOTION: + axis = ev.jaxis.value; + // Check for ABS_Z/ABS_RZ trigger axes which rest at -32768 + if ((ev.jaxis.axis == 2 || ev.jaxis.axis == 5) && mInitAxisValues[ev.jaxis.which][ev.jaxis.axis] == -32768) + { + // shift to 0 - 32767. + axis = axis / 2 + 16384; + } //if it switched boundaries - if((abs(ev.jaxis.value) > DEADZONE) != (abs(mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis]) > DEADZONE)) + if((abs(axis) > DEADZONE) != (abs(mPrevAxisValues[ev.jaxis.which][ev.jaxis.axis]) > DEADZONE)) { int normValue; - if(abs(ev.jaxis.value) <= DEADZONE) + if(abs(axis) <= DEADZONE) normValue = 0; else - if(ev.jaxis.value > 0) + if(axis > 0) normValue = 1; else normValue = -1; - window->input(getInputConfigByDevice(ev.jaxis.which), Input(ev.jaxis.which, TYPE_AXIS, ev.jaxis.axis, normValue, false)); causedEvent = true; } diff --git a/es-core/src/InputManager.h b/es-core/src/InputManager.h index fde96f092..74c4c3f86 100644 --- a/es-core/src/InputManager.h +++ b/es-core/src/InputManager.h @@ -27,6 +27,7 @@ private: InputConfig* mCECInputConfig; std::map mPrevAxisValues; + std::map mInitAxisValues; bool initialized() const;