SDL: More than one binding might trigger a gamepad button

From 0bcd9944a389a76eaf316e483676122b80497b42 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sat, 15 Jul 2023 01:17:55 -0700
Subject: [PATCH] More than one binding might trigger a gamepad button

---
 src/joystick/SDL_gamepad.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c
index 5e81037d07de..509d54050c54 100644
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -269,7 +269,6 @@ static void HandleJoystickButton(Uint64 timestamp, SDL_Gamepad *gamepad, int but
             } else {
                 SDL_SendGamepadButton(timestamp, gamepad, binding->output.button, state);
             }
-            break;
         }
     }
 }
@@ -2424,24 +2423,28 @@ Uint8 SDL_GetGamepadButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
                     int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
                     if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
                         valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
-                        if (valid_input_range) {
-                            retval = (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
+                        if (valid_input_range && value >= threshold) {
+                            retval = SDL_PRESSED;
                             break;
                         }
                     } else {
                         valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
-                        if (valid_input_range) {
-                            retval = (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
+                        if (valid_input_range && value <= threshold) {
+                            retval = SDL_PRESSED;
                             break;
                         }
                     }
                 } else if (binding->inputType == SDL_GAMEPAD_BINDTYPE_BUTTON) {
-                    retval = SDL_GetJoystickButton(gamepad->joystick, binding->input.button);
-                    break;
+                    if (SDL_GetJoystickButton(gamepad->joystick, binding->input.button)) {
+                        retval = SDL_TRUE;
+                        break;
+                    }
                 } else if (binding->inputType == SDL_GAMEPAD_BINDTYPE_HAT) {
                     int hat_mask = SDL_GetJoystickHat(gamepad->joystick, binding->input.hat.hat);
-                    retval = (hat_mask & binding->input.hat.hat_mask) ? SDL_PRESSED : SDL_RELEASED;
-                    break;
+                    if (hat_mask & binding->input.hat.hat_mask) {
+                        retval = SDL_TRUE;
+                        break;
+                    }
                 }
             }
         }