SDL_GetJoysticks() follows the SDL_GetStringRule

This commit is contained in:
Sam Lantinga 2024-07-18 17:13:33 -07:00
parent b32c9615a7
commit 4961af4569
7 changed files with 15 additions and 20 deletions

View File

@ -823,7 +823,7 @@ Rather than iterating over joysticks using device index, there is a new function
{ {
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0) { if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0) {
int i, num_joysticks; int i, num_joysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks); const SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
if (joysticks) { if (joysticks) {
for (i = 0; i < num_joysticks; ++i) { for (i = 0; i < num_joysticks; ++i) {
SDL_JoystickID instance_id = joysticks[i]; SDL_JoystickID instance_id = joysticks[i];
@ -833,7 +833,6 @@ Rather than iterating over joysticks using device index, there is a new function
SDL_Log("Joystick %" SDL_PRIu32 ": %s%s%s VID 0x%.4x, PID 0x%.4x\n", SDL_Log("Joystick %" SDL_PRIu32 ": %s%s%s VID 0x%.4x, PID 0x%.4x\n",
instance_id, name ? name : "Unknown", path ? ", " : "", path ? path : "", SDL_GetJoystickVendorForID(instance_id), SDL_GetJoystickProductForID(instance_id)); instance_id, name ? name : "Unknown", path ? ", " : "", path ? path : "", SDL_GetJoystickVendorForID(instance_id), SDL_GetJoystickProductForID(instance_id));
} }
SDL_free(joysticks);
} }
SDL_QuitSubSystem(SDL_INIT_JOYSTICK); SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
} }

View File

@ -210,9 +210,10 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasJoystick(void);
/** /**
* Get a list of currently connected joysticks. * Get a list of currently connected joysticks.
* *
* \param count a pointer filled in with the number of joysticks returned. * The returned array follows the SDL_GetStringRule, and will be automatically freed later.
* \returns a 0 terminated array of joystick instance IDs which should be *
* freed with SDL_free(), or NULL on failure; call SDL_GetError() for * \param count a pointer filled in with the number of joysticks returned, may be NULL.
* \returns a 0 terminated array of joystick instance IDs or NULL on failure; call SDL_GetError() for
* more information. * more information.
* *
* \since This function is available since SDL 3.0.0. * \since This function is available since SDL 3.0.0.
@ -220,7 +221,7 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasJoystick(void);
* \sa SDL_HasJoystick * \sa SDL_HasJoystick
* \sa SDL_OpenJoystick * \sa SDL_OpenJoystick
*/ */
extern SDL_DECLSPEC SDL_JoystickID * SDLCALL SDL_GetJoysticks(int *count); extern SDL_DECLSPEC const SDL_JoystickID * SDLCALL SDL_GetJoysticks(int *count);
/** /**
* Get the implementation dependent name of a joystick. * Get the implementation dependent name of a joystick.

View File

@ -365,7 +365,7 @@ SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickType,(SDL_Joystick *a),(a),retur
SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickTypeForID,(SDL_JoystickID a),(a),return) SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickTypeForID,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickVendor,(SDL_Joystick *a),(a),return) SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickVendor,(SDL_Joystick *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickVendorForID,(SDL_JoystickID a),(a),return) SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickVendorForID,(SDL_JoystickID a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickID*,SDL_GetJoysticks,(int *a),(a),return) SDL_DYNAPI_PROC(const SDL_JoystickID*,SDL_GetJoysticks,(int *a),(a),return)
SDL_DYNAPI_PROC(SDL_Keycode,SDL_GetKeyFromName,(const char *a),(a),return) SDL_DYNAPI_PROC(SDL_Keycode,SDL_GetKeyFromName,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_Keycode,SDL_GetKeyFromScancode,(SDL_Scancode a, SDL_Keymod b),(a,b),return) SDL_DYNAPI_PROC(SDL_Keycode,SDL_GetKeyFromScancode,(SDL_Scancode a, SDL_Keymod b),(a,b),return)
SDL_DYNAPI_PROC(const char*,SDL_GetKeyName,(SDL_Keycode a),(a),return) SDL_DYNAPI_PROC(const char*,SDL_GetKeyName,(SDL_Keycode a),(a),return)

View File

@ -89,7 +89,7 @@ typedef struct GamepadMapping_t
typedef struct typedef struct
{ {
int refcount _guarded; int refcount _guarded;
SDL_JoystickID *joysticks _guarded; const SDL_JoystickID *joysticks _guarded;
GamepadMapping_t **joystick_mappings _guarded; GamepadMapping_t **joystick_mappings _guarded;
int num_changed_mappings _guarded; int num_changed_mappings _guarded;
@ -579,7 +579,6 @@ static void PopMappingChangeTracking(void)
} }
} }
SDL_free(tracker->joysticks);
SDL_free(tracker->joystick_mappings); SDL_free(tracker->joystick_mappings);
SDL_free(tracker->changed_mappings); SDL_free(tracker->changed_mappings);
SDL_free(tracker); SDL_free(tracker);
@ -2358,7 +2357,7 @@ int SDL_InitGamepadMappings(void)
int SDL_InitGamepads(void) int SDL_InitGamepads(void)
{ {
int i; int i;
SDL_JoystickID *joysticks; const SDL_JoystickID *joysticks;
SDL_gamepads_initialized = SDL_TRUE; SDL_gamepads_initialized = SDL_TRUE;
@ -2373,7 +2372,6 @@ int SDL_InitGamepads(void)
SDL_PrivateGamepadAdded(joysticks[i]); SDL_PrivateGamepadAdded(joysticks[i]);
} }
} }
SDL_free(joysticks);
} }
return 0; return 0;
@ -2383,7 +2381,7 @@ SDL_bool SDL_HasGamepad(void)
{ {
int num_joysticks = 0; int num_joysticks = 0;
int num_gamepads = 0; int num_gamepads = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks); const SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
if (joysticks) { if (joysticks) {
int i; int i;
for (i = num_joysticks - 1; i >= 0 && num_gamepads == 0; --i) { for (i = num_joysticks - 1; i >= 0 && num_gamepads == 0; --i) {
@ -2391,7 +2389,6 @@ SDL_bool SDL_HasGamepad(void)
++num_gamepads; ++num_gamepads;
} }
} }
SDL_free(joysticks);
} }
if (num_gamepads > 0) { if (num_gamepads > 0) {
return SDL_TRUE; return SDL_TRUE;

View File

@ -714,7 +714,7 @@ SDL_bool SDL_HasJoystick(void)
return SDL_FALSE; return SDL_FALSE;
} }
SDL_JoystickID *SDL_GetJoysticks(int *count) const SDL_JoystickID *SDL_GetJoysticks(int *count)
{ {
int i, num_joysticks, device_index; int i, num_joysticks, device_index;
int joystick_index = 0, total_joysticks = 0; int joystick_index = 0, total_joysticks = 0;
@ -751,7 +751,7 @@ SDL_JoystickID *SDL_GetJoysticks(int *count)
} }
SDL_UnlockJoysticks(); SDL_UnlockJoysticks();
return joysticks; return SDL_FreeLater(joysticks);
} }
const SDL_SteamVirtualGamepadInfo *SDL_GetJoystickVirtualGamepadInfoForID(SDL_JoystickID instance_id) const SDL_SteamVirtualGamepadInfo *SDL_GetJoystickVirtualGamepadInfoForID(SDL_JoystickID instance_id)
@ -1905,7 +1905,7 @@ void SDL_CloseJoystick(SDL_Joystick *joystick)
void SDL_QuitJoysticks(void) void SDL_QuitJoysticks(void)
{ {
int i; int i;
SDL_JoystickID *joysticks; const SDL_JoystickID *joysticks;
SDL_LockJoysticks(); SDL_LockJoysticks();
@ -1916,7 +1916,6 @@ void SDL_QuitJoysticks(void)
for (i = 0; joysticks[i]; ++i) { for (i = 0; joysticks[i]; ++i) {
SDL_PrivateJoystickRemoved(joysticks[i]); SDL_PrivateJoystickRemoved(joysticks[i]);
} }
SDL_free(joysticks);
} }
while (SDL_joysticks) { while (SDL_joysticks) {

View File

@ -1187,7 +1187,7 @@ static void OpenVirtualGamepad(void)
static void CloseVirtualGamepad(void) static void CloseVirtualGamepad(void)
{ {
int i; int i;
SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL); const SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
if (joysticks) { if (joysticks) {
for (i = 0; joysticks[i]; ++i) { for (i = 0; joysticks[i]; ++i) {
SDL_JoystickID instance_id = joysticks[i]; SDL_JoystickID instance_id = joysticks[i];
@ -1195,7 +1195,6 @@ static void CloseVirtualGamepad(void)
SDL_DetachVirtualJoystick(instance_id); SDL_DetachVirtualJoystick(instance_id);
} }
} }
SDL_free(joysticks);
} }
if (virtual_joystick) { if (virtual_joystick) {

View File

@ -86,7 +86,7 @@ int main(int argc, char *argv[])
SDL_free(SDL_GetMice(&num_mice)); SDL_free(SDL_GetMice(&num_mice));
SDL_Log("There are %d mice at startup\n", num_mice); SDL_Log("There are %d mice at startup\n", num_mice);
SDL_free(SDL_GetJoysticks(&num_joysticks)); SDL_GetJoysticks(&num_joysticks);
SDL_Log("There are %d joysticks at startup\n", num_joysticks); SDL_Log("There are %d joysticks at startup\n", num_joysticks);
if (enable_haptic) { if (enable_haptic) {