From 859dc14addcce248ff6085c7f842ebb3a2001eff Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 13 Sep 2023 23:01:28 -0700
Subject: [PATCH] Replaced SDL_GetGamepadBindForAxis() and
SDL_GetGamepadBindForButton() with SDL_GetGamepadBindings()
---
build-scripts/SDL_migration.cocci | 15 ---
docs/README-migration.md | 5 +-
include/SDL3/SDL_gamepad.h | 79 +++++++-------
include/SDL3/SDL_oldnames.h | 6 --
src/dynapi/SDL_dynapi.sym | 3 +-
src/dynapi/SDL_dynapi_overrides.h | 3 +-
src/dynapi/SDL_dynapi_procs.h | 3 +-
src/joystick/SDL_gamepad.c | 174 ++++++++++--------------------
8 files changed, 104 insertions(+), 184 deletions(-)
diff --git a/build-scripts/SDL_migration.cocci b/build-scripts/SDL_migration.cocci
index 1ade7e891f8f..df9acc0070e6 100644
--- a/build-scripts/SDL_migration.cocci
+++ b/build-scripts/SDL_migration.cocci
@@ -1069,11 +1069,6 @@ typedef SDL_GameControllerButton, SDL_GamepadButton;
- SDL_GameControllerButton
+ SDL_GamepadButton
@@
-typedef SDL_GameControllerButtonBind, SDL_GamepadBinding;
-@@
-- SDL_GameControllerButtonBind
-+ SDL_GamepadBinding
-@@
@@
- SDL_GameControllerClose
+ SDL_CloseGamepad
@@ -1115,16 +1110,6 @@ typedef SDL_GameControllerButtonBind, SDL_GamepadBinding;
(...)
@@
@@
-- SDL_GameControllerGetBindForAxis
-+ SDL_GetGamepadBindForAxis
- (...)
-@@
-@@
-- SDL_GameControllerGetBindForButton
-+ SDL_GetGamepadBindForButton
- (...)
-@@
-@@
- SDL_GameControllerGetButton
+ SDL_GetGamepadButton
(...)
diff --git a/docs/README-migration.md b/docs/README-migration.md
index 52e4b0b93b2b..c4dd52b5f6e4 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -411,7 +411,6 @@ The following enums have been renamed:
The following structures have been renamed:
* SDL_GameController => SDL_Gamepad
-* SDL_GameControllerButtonBind => SDL_GamepadBinding
The following functions have been renamed:
* SDL_GameControllerAddMapping() => SDL_AddGamepadMapping()
@@ -425,8 +424,6 @@ The following functions have been renamed:
* SDL_GameControllerGetAttached() => SDL_GamepadConnected()
* SDL_GameControllerGetAxis() => SDL_GetGamepadAxis()
* SDL_GameControllerGetAxisFromString() => SDL_GetGamepadAxisFromString()
-* SDL_GameControllerGetBindForAxis() => SDL_GetGamepadBindForAxis()
-* SDL_GameControllerGetBindForButton() => SDL_GetGamepadBindForButton()
* SDL_GameControllerGetButton() => SDL_GetGamepadButton()
* SDL_GameControllerGetButtonFromString() => SDL_GetGamepadButtonFromString()
* SDL_GameControllerGetFirmwareVersion() => SDL_GetGamepadFirmwareVersion()
@@ -469,6 +466,8 @@ The following functions have been renamed:
The following functions have been removed:
* SDL_GameControllerEventState() - replaced with SDL_SetGamepadEventsEnabled() and SDL_GamepadEventsEnabled()
+* SDL_GameControllerGetBindForAxis() - replaced with SDL_GetGamepadBindings()
+* SDL_GameControllerGetBindForButton() - replaced with SDL_GetGamepadBindings()
* SDL_GameControllerMappingForDeviceIndex() - replaced with SDL_GetGamepadInstanceMapping()
* SDL_GameControllerNameForIndex() - replaced with SDL_GetGamepadInstanceName()
* SDL_GameControllerPathForIndex() - replaced with SDL_GetGamepadInstancePath()
diff --git a/include/SDL3/SDL_gamepad.h b/include/SDL3/SDL_gamepad.h
index a804ed650497..b9c31e8d4d3d 100644
--- a/include/SDL3/SDL_gamepad.h
+++ b/include/SDL3/SDL_gamepad.h
@@ -133,21 +133,41 @@ typedef enum
SDL_GAMEPAD_BINDTYPE_HAT
} SDL_GamepadBindingType;
-/**
- * Get the SDL joystick layer binding for this gamepad button/axis mapping
- */
-typedef struct SDL_GamepadBinding
+typedef struct
{
- SDL_GamepadBindingType bindType;
+ SDL_GamepadBindingType inputType;
union
{
int button;
- int axis;
- struct {
+
+ struct
+ {
+ int axis;
+ int axis_min;
+ int axis_max;
+ } axis;
+
+ struct
+ {
int hat;
int hat_mask;
} hat;
- } value;
+
+ } input;
+
+ SDL_GamepadBindingType outputType;
+ union
+ {
+ SDL_GamepadButton button;
+
+ struct
+ {
+ SDL_GamepadAxis axis;
+ int axis_min;
+ int axis_max;
+ } axis;
+
+ } output;
} SDL_GamepadBinding;
@@ -763,6 +783,19 @@ extern DECLSPEC void SDLCALL SDL_SetGamepadEventsEnabled(SDL_bool enabled);
*/
extern DECLSPEC SDL_bool SDLCALL SDL_GamepadEventsEnabled(void);
+/**
+ * Get the SDL joystick layer bindings for a gamepad
+ *
+ * \param gamepad a gamepad
+ * \param count a pointer filled in with the number of bindings returned
+ * \returns a NULL terminated array of pointers to bindings which should be
+ * freed with SDL_free(), or NULL on error; call SDL_GetError() for
+ * more details.
+ *
+ * \since This function is available since SDL 3.0.0.
+ */
+extern DECLSPEC SDL_GamepadBinding **SDLCALL SDL_GetGamepadBindings(SDL_Gamepad *gamepad, int *count);
+
/**
* Manually pump gamepad updates if not using the loop.
*
@@ -846,21 +879,6 @@ extern DECLSPEC SDL_GamepadAxis SDLCALL SDL_GetGamepadAxisFromString(const char
*/
extern DECLSPEC const char* SDLCALL SDL_GetGamepadStringForAxis(SDL_GamepadAxis axis);
-/**
- * Get the SDL joystick layer binding for a gamepad axis mapping.
- *
- * \param gamepad a gamepad
- * \param axis an axis enum value (one of the SDL_GamepadAxis values)
- * \returns a SDL_GamepadBinding describing the bind. On failure (like the
- * given Controller axis doesn't exist on the device), its
- * `.bindType` will be `SDL_GAMEPAD_BINDTYPE_NONE`.
- *
- * \since This function is available since SDL 3.0.0.
- *
- * \sa SDL_GetGamepadBindForButton
- */
-extern DECLSPEC SDL_GamepadBinding SDLCALL SDL_GetGamepadBindForAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis);
-
/**
* Query whether a gamepad has a given axis.
*
@@ -926,21 +944,6 @@ extern DECLSPEC SDL_GamepadButton SDLCALL SDL_GetGamepadButtonFromString(const c
*/
extern DECLSPEC const char* SDLCALL SDL_GetGamepadStringForButton(SDL_GamepadButton button);
-/**
- * Get the SDL joystick layer binding for a gamepad button mapping.
- *
- * \param gamepad a gamepad
- * \param button an button enum value (an SDL_GamepadButton value)
- * \returns a SDL_GamepadBinding describing the bind. On failure (like the
- * given Controller button doesn't exist on the device), its
- * `.bindType` will be `SDL_GAMEPAD_BINDTYPE_NONE`.
- *
- * \since This function is available since SDL 3.0.0.
- *
- * \sa SDL_GetGamepadBindForAxis
- */
-extern DECLSPEC SDL_GamepadBinding SDLCALL SDL_GetGamepadBindForButton(SDL_Gamepad *gamepad, SDL_GamepadButton button);
-
/**
* Query whether a gamepad has a given button.
*
diff --git a/include/SDL3/SDL_oldnames.h b/include/SDL3/SDL_oldnames.h
index 0ecea4073e6b..e5b5c9b567c4 100644
--- a/include/SDL3/SDL_oldnames.h
+++ b/include/SDL3/SDL_oldnames.h
@@ -202,7 +202,6 @@
#define SDL_GameControllerAxis SDL_GamepadAxis
#define SDL_GameControllerBindType SDL_GamepadBindingType
#define SDL_GameControllerButton SDL_GamepadButton
-#define SDL_GameControllerButtonBind SDL_GamepadBinding
#define SDL_GameControllerClose SDL_CloseGamepad
#define SDL_GameControllerFromInstanceID SDL_GetGamepadFromInstanceID
#define SDL_GameControllerFromPlayerIndex SDL_GetGamepadFromPlayerIndex
@@ -211,8 +210,6 @@
#define SDL_GameControllerGetAttached SDL_GamepadConnected
#define SDL_GameControllerGetAxis SDL_GetGamepadAxis
#define SDL_GameControllerGetAxisFromString SDL_GetGamepadAxisFromString
-#define SDL_GameControllerGetBindForAxis SDL_GetGamepadBindForAxis
-#define SDL_GameControllerGetBindForButton SDL_GetGamepadBindForButton
#define SDL_GameControllerGetButton SDL_GetGamepadButton
#define SDL_GameControllerGetButtonFromString SDL_GetGamepadButtonFromString
#define SDL_GameControllerGetFirmwareVersion SDL_GetGamepadFirmwareVersion
@@ -653,7 +650,6 @@
#define SDL_GameControllerAxis SDL_GameControllerAxis_renamed_SDL_GamepadAxis
#define SDL_GameControllerBindType SDL_GameControllerBindType_renamed_SDL_GamepadBindingType
#define SDL_GameControllerButton SDL_GameControllerButton_renamed_SDL_GamepadButton
-#define SDL_GameControllerButtonBind SDL_GameControllerButtonBind_renamed_SDL_GamepadBinding
#define SDL_GameControllerClose SDL_GameControllerClose_renamed_SDL_CloseGamepad
#define SDL_GameControllerFromInstanceID SDL_GameControllerFromInstanceID_renamed_SDL_GetGamepadFromInstanceID
#define SDL_GameControllerFromPlayerIndex SDL_GameControllerFromPlayerIndex_renamed_SDL_GetGamepadFromPlayerIndex
@@ -662,8 +658,6 @@
#define SDL_GameControllerGetAttached SDL_GameControllerGetAttached_renamed_SDL_GamepadConnected
#define SDL_GameControllerGetAxis SDL_GameControllerGetAxis_renamed_SDL_GetGamepadAxis
#define SDL_GameControllerGetAxisFromString SDL_GameControllerGetAxisFromString_renamed_SDL_GetGamepadAxisFromString
-#define SDL_GameControllerGetBindForAxis SDL_GameControllerGetBindForAxis_renamed_SDL_GetGamepadBindForAxis
-#define SDL_GameControllerGetBindForButton SDL_GameControllerGetBindForButton_renamed_SDL_GetGamepadBindForButton
#define SDL_GameControllerGetButton SDL_GameControllerGetButton_renamed_SDL_GetGamepadButton
#define SDL_GameControllerGetButtonFromString SDL_GameControllerGetButtonFromString_renamed_SDL_GetGamepadButtonFromString
#define SDL_GameControllerGetFirmwareVersion SDL_GameControllerGetFirmwareVersion_renamed_SDL_GetGamepadFirmwareVersion
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index ac044bf10d46..f1d418eb6579 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -172,8 +172,7 @@ SDL3_0.0.0 {
SDL_GetGamepadAppleSFSymbolsNameForButton;
SDL_GetGamepadAxis;
SDL_GetGamepadAxisFromString;
- SDL_GetGamepadBindForAxis;
- SDL_GetGamepadBindForButton;
+ SDL_GetGamepadBindings;
SDL_GetGamepadButton;
SDL_GetGamepadButtonFromString;
SDL_GetGamepadFirmwareVersion;
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index d5237abe2241..ffb08c079d45 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -196,8 +196,7 @@
#define SDL_GetGamepadAppleSFSymbolsNameForButton SDL_GetGamepadAppleSFSymbolsNameForButton_REAL
#define SDL_GetGamepadAxis SDL_GetGamepadAxis_REAL
#define SDL_GetGamepadAxisFromString SDL_GetGamepadAxisFromString_REAL
-#define SDL_GetGamepadBindForAxis SDL_GetGamepadBindForAxis_REAL
-#define SDL_GetGamepadBindForButton SDL_GetGamepadBindForButton_REAL
+#define SDL_GetGamepadBindings SDL_GetGamepadBindings_REAL
#define SDL_GetGamepadButton SDL_GetGamepadButton_REAL
#define SDL_GetGamepadButtonFromString SDL_GetGamepadButtonFromString_REAL
#define SDL_GetGamepadFirmwareVersion SDL_GetGamepadFirmwareVersion_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 4f2212bcf3ff..71bda6eff3fa 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -271,8 +271,7 @@ SDL_DYNAPI_PROC(const char*,SDL_GetGamepadAppleSFSymbolsNameForAxis,(SDL_Gamepad
SDL_DYNAPI_PROC(const char*,SDL_GetGamepadAppleSFSymbolsNameForButton,(SDL_Gamepad *a, SDL_GamepadButton b),(a,b),return)
SDL_DYNAPI_PROC(Sint16,SDL_GetGamepadAxis,(SDL_Gamepad *a, SDL_GamepadAxis b),(a,b),return)
SDL_DYNAPI_PROC(SDL_GamepadAxis,SDL_GetGamepadAxisFromString,(const char *a),(a),return)
-SDL_DYNAPI_PROC(SDL_GamepadBinding,SDL_GetGamepadBindForAxis,(SDL_Gamepad *a, SDL_GamepadAxis b),(a,b),return)
-SDL_DYNAPI_PROC(SDL_GamepadBinding,SDL_GetGamepadBindForButton,(SDL_Gamepad *a, SDL_GamepadButton b),(a,b),return)
+SDL_DYNAPI_PROC(SDL_GamepadBinding **,SDL_GetGamepadBindings,(SDL_Gamepad *a, int *b),(a,b),return)
SDL_DYNAPI_PROC(Uint8,SDL_GetGamepadButton,(SDL_Gamepad *a, SDL_GamepadButton b),(a,b),return)
SDL_DYNAPI_PROC(SDL_GamepadButton,SDL_GetGamepadButtonFromString,(const char *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadFirmwareVersion,(SDL_Gamepad *a),(a),return)
diff --git a/src/joystick/SDL_gamepad.c b/src/joystick/SDL_gamepad.c
index 2dfbcdea2021..4cb4a1481da2 100644
--- a/src/joystick/SDL_gamepad.c
+++ b/src/joystick/SDL_gamepad.c
@@ -57,44 +57,6 @@
static SDL_bool SDL_gamepads_initialized;
static SDL_Gamepad *SDL_gamepads SDL_GUARDED_BY(SDL_joystick_lock) = NULL;
-typedef struct
-{
- SDL_GamepadBindingType inputType;
- union
- {
- int button;
-
- struct
- {
- int axis;
- int axis_min;
- int axis_max;
- } axis;
-
- struct
- {
- int hat;
- int hat_mask;
- } hat;
-
- } input;
-
- SDL_GamepadBindingType outputType;
- union
- {
- SDL_GamepadButton button;
-
- struct
- {
- SDL_GamepadAxis axis;
- int axis_min;
- int axis_max;
- } axis;
-
- } output;
-
-} SDL_ExtendedGamepadBind;
-
/* our hard coded list of mapping support */
typedef enum
{
@@ -147,8 +109,8 @@ struct SDL_Gamepad
const char *name _guarded;
GamepadMapping_t *mapping _guarded;
int num_bindings _guarded;
- SDL_ExtendedGamepadBind *bindings _guarded;
- SDL_ExtendedGamepadBind **last_match_axis _guarded;
+ SDL_GamepadBinding *bindings _guarded;
+ SDL_GamepadBinding **last_match_axis _guarded;
Uint8 *last_hat_mask _guarded;
Uint64 guide_button_down _guarded;
@@ -184,7 +146,7 @@ static GamepadMapping_t *SDL_PrivateGetGamepadMapping(SDL_JoystickID instance_id
static int SDL_SendGamepadAxis(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadAxis axis, Sint16 value);
static int SDL_SendGamepadButton(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadButton button, Uint8 state);
-static SDL_bool HasSameOutput(SDL_ExtendedGamepadBind *a, SDL_ExtendedGamepadBind *b)
+static SDL_bool HasSameOutput(SDL_GamepadBinding *a, SDL_GamepadBinding *b)
{
if (a->outputType != b->outputType) {
return SDL_FALSE;
@@ -197,7 +159,7 @@ static SDL_bool HasSameOutput(SDL_ExtendedGamepadBind *a, SDL_ExtendedGamepadBin
}
}
-static void ResetOutput(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_ExtendedGamepadBind *bind)
+static void ResetOutput(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_GamepadBinding *bind)
{
if (bind->outputType == SDL_GAMEPAD_BINDTYPE_AXIS) {
SDL_SendGamepadAxis(timestamp, gamepad, bind->output.axis.axis, 0);
@@ -209,14 +171,14 @@ static void ResetOutput(Uint64 timestamp, SDL_Gamepad *gamepad, SDL_ExtendedGame
static void HandleJoystickAxis(Uint64 timestamp, SDL_Gamepad *gamepad, int axis, int value)
{
int i;
- SDL_ExtendedGamepadBind *last_match;
- SDL_ExtendedGamepadBind *match = NULL;
+ SDL_GamepadBinding *last_match;
+ SDL_GamepadBinding *match = NULL;
SDL_AssertJoysticksLocked();
last_match = gamepad->last_match_axis[axis];
for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
if (binding->inputType == SDL_GAMEPAD_BINDTYPE_AXIS &&
axis == binding->input.axis.axis) {
if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
@@ -268,7 +230,7 @@ static void HandleJoystickButton(Uint64 timestamp, SDL_Gamepad *gamepad, int but
SDL_AssertJoysticksLocked();
for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
if (binding->inputType == SDL_GAMEPAD_BINDTYPE_BUTTON &&
button == binding->input.button) {
if (binding->outputType == SDL_GAMEPAD_BINDTYPE_AXIS) {
@@ -292,7 +254,7 @@ static void HandleJoystickHat(Uint64 timestamp, SDL_Gamepad *gamepad, int hat, U
last_mask = gamepad->last_hat_mask[hat];
changed_mask = (last_mask ^ value);
for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
if (binding->inputType == SDL_GAMEPAD_BINDTYPE_HAT && hat == binding->input.hat.hat) {
if ((changed_mask & binding->input.hat.hat_mask) != 0) {
if (value & binding->input.hat.hat_mask) {
@@ -1156,7 +1118,7 @@ const char *SDL_GetGamepadStringForButton(SDL_GamepadButton button)
*/
static int SDL_PrivateParseGamepadElement(SDL_Gamepad *gamepad, const char *szGameButton, const char *szJoystickButton)
{
- SDL_ExtendedGamepadBind bind;
+ SDL_GamepadBinding bind;
SDL_GamepadButton button;
SDL_GamepadAxis axis;
SDL_bool invert_input = SDL_FALSE;
@@ -1236,7 +1198,7 @@ static int SDL_PrivateParseGamepadElement(SDL_Gamepad *gamepad, const char *szGa
}
++gamepad->num_bindings;
- gamepad->bindings = (SDL_ExtendedGamepadBind *)SDL_realloc(gamepad->bindings, gamepad->num_bindings * sizeof(*gamepad->bindings));
+ gamepad->bindings = (SDL_GamepadBinding *)SDL_realloc(gamepad->bindings, gamepad->num_bindings * sizeof(*gamepad->bindings));
if (!gamepad->bindings) {
gamepad->num_bindings = 0;
return SDL_OutOfMemory();
@@ -1315,7 +1277,7 @@ static void SDL_PrivateLoadButtonMapping(SDL_Gamepad *gamepad, GamepadMapping_t
/* Set the zero point for triggers */
for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
if (binding->inputType == SDL_GAMEPAD_BINDTYPE_AXIS &&
binding->outputType == SDL_GAMEPAD_BINDTYPE_AXIS &&
(binding->output.axis.axis == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
@@ -2478,7 +2440,7 @@ SDL_Gamepad *SDL_OpenGamepad(SDL_JoystickID instance_id)
}
if (gamepad->joystick->naxes) {
- gamepad->last_match_axis = (SDL_ExtendedGamepadBind **)SDL_calloc(gamepad->joystick->naxes, sizeof(*gamepad->last_match_axis));
+ gamepad->last_match_axis = (SDL_GamepadBinding **)SDL_calloc(gamepad->joystick->naxes, sizeof(*gamepad->last_match_axis));
if (!gamepad->last_match_axis) {
SDL_OutOfMemory();
SDL_CloseJoystick(gamepad->joystick);
@@ -2526,17 +2488,25 @@ void SDL_UpdateGamepads(void)
*/
SDL_bool SDL_GamepadHasAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
{
- SDL_GamepadBinding bind;
+ SDL_bool retval = SDL_FALSE;
SDL_LockJoysticks();
{
+ int i;
+
CHECK_GAMEPAD_MAGIC(gamepad, SDL_FALSE);
- bind = SDL_GetGamepadBindForAxis(gamepad, axis);
+ for (i = 0; i < gamepad->num_bindings; ++i) {
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
+ if (binding->outputType == SDL_GAMEPAD_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
+ retval = SDL_TRUE;
+ break;
+ }
+ }
}
SDL_UnlockJoysticks();
- return (bind.bindType != SDL_GAMEPAD_BINDTYPE_NONE) ? SDL_TRUE : SDL_FALSE;
+ return retval;
}
/*
@@ -2553,7 +2523,7 @@ Sint16 SDL_GetGamepadAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
CHECK_GAMEPAD_MAGIC(gamepad, 0);
for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
if (binding->outputType == SDL_GAMEPAD_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
int value = 0;
SDL_bool valid_input_range;
@@ -2609,17 +2579,25 @@ Sint16 SDL_GetGamepadAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
*/
SDL_bool SDL_GamepadHasButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
{
- SDL_GamepadBinding bind;
+ SDL_bool retval = SDL_FALSE;
SDL_LockJoysticks();
{
+ int i;
+
CHECK_GAMEPAD_MAGIC(gamepad, SDL_FALSE);
- bind = SDL_GetGamepadBindForButton(gamepad, button);
+ for (i = 0; i < gamepad->num_bindings; ++i) {
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
+ if (binding->outputType == SDL_GAMEPAD_BINDTYPE_BUTTON && binding->output.button == button) {
+ retval = SDL_TRUE;
+ break;
+ }
+ }
}
SDL_UnlockJoysticks();
- return (bind.bindType != SDL_GAMEPAD_BINDTYPE_NONE) ? SDL_TRUE : SDL_FALSE;
+ return retval;
}
/*
@@ -2636,7 +2614,7 @@ Uint8 SDL_GetGamepadButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
CHECK_GAMEPAD_MAGIC(gamepad, 0);
for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
+ SDL_GamepadBinding *binding = &gamepad->bindings[i];
if (binding->outputType == SDL_GAMEPAD_BINDTYPE_BUTTON && binding->output.button == button) {
if (binding->inputType == SDL_GAMEPAD_BINDTYPE_AXIS) {
SDL_bool valid_input_range;
@@ -3157,78 +3135,42 @@ SDL_Gamepad *SDL_GetGamepadFromPlayerIndex(int player_index)
}
/*
- * Get the SDL joystick layer binding for this gamepad axis mapping
+ * Get the SDL joystick layer bindings for this gamepad
*/
-SDL_GamepadBinding SDL_GetGamepadBindForAxis(SDL_Gamepad *gamepad, SDL_GamepadAxis axis)
+SDL_GamepadBinding **SDL_GetGamepadBindings(SDL_Gamepad *gamepad, int *count)
{
- SDL_GamepadBinding bind;
+ SDL_GamepadBinding **bindings = NULL;
- SDL_zero(bind);
+ if (count) {
+ *count = 0;
+ }
SDL_LockJoysticks();
{
- CHECK_GAMEPAD_MAGIC(gamepad, bind);
+ CHECK_GAMEPAD_MAGIC(gamepad, NULL);
- if (axis != SDL_GAMEPAD_AXIS_INVALID) {
+ size_t pointers_size = ((gamepad->num_bindings + 1) * sizeof(SDL_GamepadBinding *));
+ size_t elements_size = (gamepad->num_bindings * sizeof(SDL_GamepadBinding));
+ bindings = (SDL_GamepadBinding **)SDL_malloc(pointers_size + elements_size);
+ if (bindings) {
+ SDL_GamepadBinding *binding = (SDL_GamepadBinding *)((Uint8 *)bindings + pointers_size);
int i;
- for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
- if (binding->outputType == SDL_GAMEPAD_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
- bind.bindType = binding->inputType;
- if (binding->inputType == SDL_GAMEPAD_BINDTYPE_AXIS) {
- /* FIXME: There might be multiple axes bound now that we have axis ranges... */
- bind.value.axis = binding->input.axis.axis;
- } else if (binding->inputType == SDL_GAMEPAD_BINDTYPE_BUTTON) {
- bind.value.button = binding->input.button;
- } else if (binding->inputType == SDL_GAMEPAD_BINDTYPE_HAT) {
- bind.value.hat.hat = binding->input.hat.hat;
- bind.value.hat.hat_mask = binding->input.hat.hat_mask;
- }
- break;
- }
+ for (i = 0; i < gamepad->num_bindings; ++i, ++binding) {
+ bindings[i] = binding;
+ SDL_copyp(binding, &gamepad->bindings[i]);
}
- }
- }
- SDL_UnlockJoysticks();
-
- return bind;
-}
+ bindings[i] = NULL;
-/*
- * Get the SDL joystick layer binding for this gamepad button mapping
- */
-SDL_GamepadBinding SDL_GetGamepadBindForButton(SDL_Gamepad *gamepad, SDL_GamepadButton button)
-{
- SDL_GamepadBinding bind;
-
- SDL_zero(bind);
-
- SDL_LockJoysticks();
- {
- CHECK_GAMEPAD_MAGIC(gamepad, bind);
-
- if (button != SDL_GAMEPAD_BUTTON_INVALID) {
- int i;
- for (i = 0; i < gamepad->num_bindings; ++i) {
- SDL_ExtendedGamepadBind *binding = &gamepad->bindings[i];
- if (binding->outputType == SDL_GAMEPAD_BINDTYPE_BUTTON && binding->output.button == button) {
- bind.bindType = binding->inputType;
- if (binding->inputType == SDL_GAMEPAD_BINDTYPE_AXIS) {
- bind.value.axis = binding->input.axis.axis;
- } else if (binding->inputType == SDL_GAMEPAD_BINDTYPE_BUTTON) {
- bind.value.button = binding->input.button;
- } else if (binding->inputType == SDL_GAMEPAD_BINDTYPE_HAT) {
- bind.value.hat.hat = binding->input.hat.hat;
- bind.value.hat.hat_mask = binding->input.hat.hat_mask;
- }
- break;
- }
+ if (count) {
+ *count = gamepad->num_bindings;
}
+ } else {
+ SDL_OutOfMemory();
}
}
SDL_UnlockJoysticks();
- return bind;
+ return bindings;
}
int SDL_RumbleGamepad(SDL_Gamepad *gamepad, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)