Commit 8bc5c57d2e7a4181343c13f175c3faffc5bbdb50

Sam Lantinga 2016-10-07T16:13:37

Fixed recentering triggers when the application doesn't have focus

diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 4bcb69a..41af66d 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -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;
@@ -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;
         }
     }
 
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 49b8cbc..c426a39 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -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
@@ -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));
@@ -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;
         }
     }
@@ -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++) {
diff --git a/src/joystick/SDL_sysjoystick.h b/src/joystick/SDL_sysjoystick.h
index 1015840..f4cad05 100644
--- a/src/joystick/SDL_sysjoystick.h
+++ b/src/joystick/SDL_sysjoystick.h
@@ -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 */