Skip to content

Commit

Permalink
Fixed recentering triggers when the application doesn't have focus
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 7, 2016
1 parent 7216498 commit 8bc5c57
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
17 changes: 5 additions & 12 deletions src/joystick/SDL_gamecontroller.c
Expand Up @@ -156,16 +156,7 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
switch (axis) {
case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
/* Shift it to be 0 - 32767 */
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;
}
value = value / 2 + 16384;
break;
default:
break;
Expand Down Expand Up @@ -1017,10 +1008,12 @@ SDL_GameControllerOpen(int device_index)
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;
gamecontroller->joystick->axes[leftTriggerMapping] =
gamecontroller->joystick->axes_zero[leftTriggerMapping] = (Sint16)-32768;
}
if (rightTriggerMapping >= 0) {
gamecontroller->joystick->axes[rightTriggerMapping] = (Sint16)-32768;
gamecontroller->joystick->axes[rightTriggerMapping] =
gamecontroller->joystick->axes_zero[rightTriggerMapping] = (Sint16)-32768;
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/joystick/SDL_joystick.c
Expand Up @@ -142,8 +142,8 @@ SDL_JoystickOpen(int device_index)
joystick->name = NULL;

if (joystick->naxes > 0) {
joystick->axes = (Sint16 *) SDL_malloc
(joystick->naxes * sizeof(Sint16));
joystick->axes = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16));
joystick->axes_zero = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16));
}
if (joystick->nhats > 0) {
joystick->hats = (Uint8 *) SDL_malloc
Expand All @@ -167,6 +167,7 @@ SDL_JoystickOpen(int device_index)
}
if (joystick->axes) {
SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
SDL_memset(joystick->axes_zero, 0, joystick->naxes * sizeof(Sint16));
}
if (joystick->hats) {
SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
Expand Down Expand Up @@ -579,8 +580,8 @@ SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
* events.
*/
if (SDL_PrivateJoystickShouldIgnoreEvent()) {
if ((value > 0 && value >= joystick->axes[axis]) ||
(value < 0 && value <= joystick->axes[axis])) {
if ((value > joystick->axes_zero[axis] && value >= joystick->axes[axis]) ||
(value < joystick->axes_zero[axis] && value <= joystick->axes[axis])) {
return 0;
}
}
Expand Down Expand Up @@ -753,7 +754,7 @@ SDL_JoystickUpdate(void)

/* Tell the app that everything is centered/unpressed... */
for (i = 0; i < joystick->naxes; i++) {
SDL_PrivateJoystickAxis(joystick, i, 0);
SDL_PrivateJoystickAxis(joystick, i, joystick->axes_zero[i]);
}

for (i = 0; i < joystick->nbuttons; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/joystick/SDL_sysjoystick.h
Expand Up @@ -36,6 +36,7 @@ struct _SDL_Joystick

int naxes; /* Number of axis controls on the joystick */
Sint16 *axes; /* Current axis states */
Sint16 *axes_zero; /* Zero point on the axis (-32768 for triggers) */

int nhats; /* Number of hats on the joystick */
Uint8 *hats; /* Current hat states */
Expand Down

0 comments on commit 8bc5c57

Please sign in to comment.