Fixed bug 2823 - Release events for triggers receive wrong centered value

Ryochan7

I have been using SDL 2 for a little project that I have been developing for a while. My project is called antimicro and it takes gamepad input and then translates gamepad events into keyboard and mouse events. SDL is used to read the input from an XInput gamepad and it works great for the most part. However, there is one glaring problem that I have encountered.

When a device is unplugged and SDL sends the centered value release events for all axes, buttons, and hats, SDL does not use the proper centered value for the triggers. It pushes an SDL_JOYAXISMOTION event onto the queue with a value of 0 for all axes. That value is converted to around 16,000 for a Game Controller. That value is incorrect for triggers and, in my program, that causes any bindings that are assigned to the triggers to get activated. With most profiles, that will typically mean that a mouse right click and left click will be activated before the device is finally seen as removed and then those bindings are released by antimicro.
This commit is contained in:
Sam Lantinga 2016-10-07 16:04:15 -07:00
parent f674f2311a
commit 72164985b0
1 changed files with 24 additions and 3 deletions

View File

@ -156,8 +156,17 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
switch (axis) { switch (axis) {
case SDL_CONTROLLER_AXIS_TRIGGERLEFT: case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
/* Shift it to be 0 - 32767. */ /* Shift it to be 0 - 32767 */
value = value / 2 + 16384; if (controllerlist->joystick->force_recentering) {
int triggerMapping = controllerlist->mapping.axes[axis];
if (triggerMapping >= 0) {
controllerlist->joystick->axes[triggerMapping] = (Sint16)-32768;
}
value = 0;
} else {
value = value / 2 + 16384;
}
break;
default: default:
break; break;
} }
@ -1003,6 +1012,18 @@ SDL_GameControllerOpen(int device_index)
SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping); SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
/* The triggers are mapped from -32768 to 32767, where -32768 is the 'unpressed' value */
{
int leftTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERLEFT];
int rightTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERRIGHT];
if (leftTriggerMapping >= 0) {
gamecontroller->joystick->axes[leftTriggerMapping] = (Sint16)-32768;
}
if (rightTriggerMapping >= 0) {
gamecontroller->joystick->axes[rightTriggerMapping] = (Sint16)-32768;
}
}
/* Add joystick to list */ /* Add joystick to list */
++gamecontroller->ref_count; ++gamecontroller->ref_count;
/* Link the joystick in the list */ /* Link the joystick in the list */
@ -1039,7 +1060,7 @@ SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControlle
switch (axis) { switch (axis) {
case SDL_CONTROLLER_AXIS_TRIGGERLEFT: case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT: case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
/* Shift it to be 0 - 32767. */ /* Shift it to be 0 - 32767 */
value = value / 2 + 16384; value = value / 2 + 16384;
default: default:
break; break;