SDL: Fix 3DS Analog Values (#8581)

From 059e550e981e7b2a654b54f548c64a73f5a5fe13 Mon Sep 17 00:00:00 2001
From: zoeyjodon <[EMAIL REDACTED]>
Date: Wed, 22 Nov 2023 13:34:01 -0500
Subject: [PATCH] Fix 3DS Analog Values (#8581)

---
 src/joystick/n3ds/SDL_sysjoystick.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/joystick/n3ds/SDL_sysjoystick.c b/src/joystick/n3ds/SDL_sysjoystick.c
index e78d5a735e94..e7a3c43dcf30 100644
--- a/src/joystick/n3ds/SDL_sysjoystick.c
+++ b/src/joystick/n3ds/SDL_sysjoystick.c
@@ -31,18 +31,28 @@
 #define NB_BUTTONS 23
 
 /*
-  N3DS sticks values are roughly within +/-160
+  N3DS sticks values are roughly within +/-170
   which is too small to pass the jitter tolerance.
   This correction is applied to axis values
   so they fit better in SDL's value range.
 */
-#define CORRECT_AXIS_X(X) ((X * SDL_JOYSTICK_AXIS_MAX) / 160)
+static inline int Correct_Axis_X(int X) {
+    if (X > 160) {
+        return SDL_JOYSTICK_AXIS_MAX;
+    }
+    else if (X < -160) {
+        return -SDL_JOYSTICK_AXIS_MAX;
+    }
+    return (X * SDL_JOYSTICK_AXIS_MAX) / 160;
+}
 
 /*
   The Y axis needs to be flipped because SDL's "up"
   is reversed compared to libctru's "up"
 */
-#define CORRECT_AXIS_Y(Y) CORRECT_AXIS_X(-Y)
+static inline int Correct_Axis_Y(int Y) {
+    return Correct_Axis_X(-Y);
+}
 
 static void UpdateN3DSPressedButtons(Uint64 timestamp, SDL_Joystick *joystick);
 static void UpdateN3DSReleasedButtons(Uint64 timestamp, SDL_Joystick *joystick);
@@ -141,12 +151,12 @@ static void UpdateN3DSCircle(Uint64 timestamp, SDL_Joystick *joystick)
     if (previous_state.dx != current_state.dx) {
         SDL_SendJoystickAxis(timestamp, joystick,
                                 0,
-                                CORRECT_AXIS_X(current_state.dx));
+                                Correct_Axis_X(current_state.dx));
     }
     if (previous_state.dy != current_state.dy) {
         SDL_SendJoystickAxis(timestamp, joystick,
                                 1,
-                                CORRECT_AXIS_Y(current_state.dy));
+                                Correct_Axis_Y(current_state.dy));
     }
     previous_state = current_state;
 }
@@ -159,12 +169,12 @@ static void UpdateN3DSCStick(Uint64 timestamp, SDL_Joystick *joystick)
     if (previous_state.dx != current_state.dx) {
         SDL_SendJoystickAxis(timestamp, joystick,
                                 2,
-                                CORRECT_AXIS_X(current_state.dx));
+                                Correct_Axis_X(current_state.dx));
     }
     if (previous_state.dy != current_state.dy) {
         SDL_SendJoystickAxis(timestamp, joystick,
                                 3,
-                                CORRECT_AXIS_Y(current_state.dy));
+                                Correct_Axis_Y(current_state.dy));
     }
     previous_state = current_state;
 }