From c5b74947a0f85c2cababf790e02ed781eb48e557 Mon Sep 17 00:00:00 2001 From: Jools Wills Date: Sat, 13 Aug 2016 01:40:50 +0100 Subject: [PATCH] workaround for analogue trigger configuration - eg with xpad. Unlike other axis, they don't default to 0, but instead start at -32768 and go to +32767 when pressed. This confuses the current ES code axis code. As a workaround, we get the initial value and if it is -32767, we add 32767 and divide by two. This gives it a range that can be handled with the current code (from 0 to 32767). Note on my X11 machine, I had to plug the joystick in after ES was loaded or it get 0 as the initial axis value for the triggers. This seems ok on the RPI though, so possible some SDL issue. With this change on a 360 controller the triggers should be correctly detected as +2 and +5 without seeing two presses for each trigger press. --- 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 f03fbc14e..c1d2db5a9 100644 --- a/es-core/src/InputManager.cpp +++ b/es-core/src/InputManager.cpp @@ -89,7 +89,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) @@ -170,21 +177,28 @@ InputConfig* InputManager::getInputConfigByDevice(int device) bool InputManager::parseEvent(const SDL_Event& ev, Window* window) { bool causedEvent = false; + int axis; + int deadzone = DEADZONE; switch(ev.type) { case SDL_JOYAXISMOTION: + axis = ev.jaxis.value; + if (mInitAxisValues[ev.jaxis.which][ev.jaxis.axis] == -32768) + { + deadzone /= 2; + axis = (axis + 32767) / 2; + } //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 740835634..295fa87f7 100644 --- a/es-core/src/InputManager.h +++ b/es-core/src/InputManager.h @@ -26,6 +26,7 @@ private: InputConfig* mKeyboardInputConfig; std::map mPrevAxisValues; + std::map mInitAxisValues; bool initialized() const;