From 03f8528315fa46c95991a34f3325d7b33ae5538c Mon Sep 17 00:00:00 2001
From: Edward Rudd <urkle@outoforder.cc>
Date: Sat, 02 May 2020 21:48:36 +0000
Subject: [PATCH] Update source to SDL2 2.0.12

---
 source/test/controllermap.c |   34 +++++++++++++++++++++-------------
 1 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/source/test/controllermap.c b/source/test/controllermap.c
index 2ca5351..6ef360a 100644
--- a/source/test/controllermap.c
+++ b/source/test/controllermap.c
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
+  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
 
   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
@@ -20,6 +20,9 @@
 #include "SDL.h"
 
 #ifndef SDL_JOYSTICK_DISABLED
+
+/* Define this for verbose output while mapping controllers */
+#define DEBUG_CONTROLLERMAP
 
 #ifdef __IPHONEOS__
 #define SCREEN_WIDTH    320
@@ -140,6 +143,7 @@
 typedef struct
 {
     SDL_bool m_bMoving;
+    int m_nLastValue;
     int m_nStartingValue;
     int m_nFarthestValue;
 } AxisState;
@@ -363,7 +367,6 @@
     Uint8 alpha=200, alpha_step = -1;
     Uint32 alpha_ticks = 0;
     SDL_JoystickID nJoystickID;
-    int iIndex;
 
     /* Create a window to display joystick axis position */
     window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
@@ -410,13 +413,11 @@
 
     s_nNumAxes = SDL_JoystickNumAxes(joystick);
     s_arrAxisState = (AxisState *)SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
-    for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
-        AxisState *pAxisState = &s_arrAxisState[iIndex];
-        Sint16 nInitialValue;
-        pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, iIndex, &nInitialValue);
-        pAxisState->m_nStartingValue = nInitialValue;
-        pAxisState->m_nFarthestValue = nInitialValue;
-    }
+
+	/* Skip any spurious events at start */
+	while (SDL_PollEvent(&event) > 0) {
+		continue;
+	}
 
     /* Loop, getting joystick events! */
     while (!done && !s_bBindingComplete) {
@@ -465,13 +466,20 @@
                 break;
             case SDL_JOYAXISMOTION:
                 if (event.jaxis.which == nJoystickID) {
+                    const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80;  /* ShanWan PS3 controller needed 96 */
                     AxisState *pAxisState = &s_arrAxisState[event.jaxis.axis];
                     int nValue = event.jaxis.value;
                     int nCurrentDistance, nFarthestDistance;
                     if (!pAxisState->m_bMoving) {
-                        pAxisState->m_bMoving = SDL_TRUE;
-                        pAxisState->m_nStartingValue = nValue;
-                        pAxisState->m_nFarthestValue = nValue;
+                        Sint16 nInitialValue;
+                        pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, event.jaxis.axis, &nInitialValue);
+                        pAxisState->m_nLastValue = nInitialValue;
+                        pAxisState->m_nStartingValue = nInitialValue;
+                        pAxisState->m_nFarthestValue = nInitialValue;
+                    } else if (SDL_abs(nValue - pAxisState->m_nLastValue) <= MAX_ALLOWED_JITTER) {
+                        break;
+                    } else {
+                        pAxisState->m_nLastValue = nValue;
                     }
                     nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
                     nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
@@ -773,7 +781,7 @@
 main(int argc, char *argv[])
 {
     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Joystick support.\n");
-    exit(1);
+    return 1;
 }
 
 #endif

--
Gitblit v1.9.3