Skip to content

Commit

Permalink
SDL_GetJoysticks() follows the SDL_GetStringRule
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jul 19, 2024
1 parent b32c961 commit 4961af4
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 20 deletions.
3 changes: 1 addition & 2 deletions docs/README-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ Rather than iterating over joysticks using device index, there is a new function
{
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0) {
int i, num_joysticks;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
const SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
if (joysticks) {
for (i = 0; i < num_joysticks; ++i) {
SDL_JoystickID instance_id = joysticks[i];
Expand All @@ -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",
instance_id, name ? name : "Unknown", path ? ", " : "", path ? path : "", SDL_GetJoystickVendorForID(instance_id), SDL_GetJoystickProductForID(instance_id));
}
SDL_free(joysticks);
}
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
}
Expand Down
9 changes: 5 additions & 4 deletions include/SDL3/SDL_joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,17 +210,18 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasJoystick(void);
/**
* Get a list of currently connected joysticks.
*
* \param count a pointer filled in with the number of joysticks returned.
* \returns a 0 terminated array of joystick instance IDs which should be
* freed with SDL_free(), or NULL on failure; call SDL_GetError() for
* The returned array follows the SDL_GetStringRule, and will be automatically freed later.
*
* \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.
*
* \since This function is available since SDL 3.0.0.
*
* \sa SDL_HasJoystick
* \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.
Expand Down
2 changes: 1 addition & 1 deletion src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(Uint16,SDL_GetJoystickVendor,(SDL_Joystick *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_GetKeyFromScancode,(SDL_Scancode a, SDL_Keymod b),(a,b),return)
SDL_DYNAPI_PROC(const char*,SDL_GetKeyName,(SDL_Keycode a),(a),return)
Expand Down
9 changes: 3 additions & 6 deletions src/joystick/SDL_gamepad.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ typedef struct GamepadMapping_t
typedef struct
{
int refcount _guarded;
SDL_JoystickID *joysticks _guarded;
const SDL_JoystickID *joysticks _guarded;
GamepadMapping_t **joystick_mappings _guarded;

int num_changed_mappings _guarded;
Expand Down Expand Up @@ -579,7 +579,6 @@ static void PopMappingChangeTracking(void)
}
}

SDL_free(tracker->joysticks);
SDL_free(tracker->joystick_mappings);
SDL_free(tracker->changed_mappings);
SDL_free(tracker);
Expand Down Expand Up @@ -2358,7 +2357,7 @@ int SDL_InitGamepadMappings(void)
int SDL_InitGamepads(void)
{
int i;
SDL_JoystickID *joysticks;
const SDL_JoystickID *joysticks;

SDL_gamepads_initialized = SDL_TRUE;

Expand All @@ -2373,7 +2372,6 @@ int SDL_InitGamepads(void)
SDL_PrivateGamepadAdded(joysticks[i]);
}
}
SDL_free(joysticks);
}

return 0;
Expand All @@ -2383,15 +2381,14 @@ SDL_bool SDL_HasGamepad(void)
{
int num_joysticks = 0;
int num_gamepads = 0;
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
const SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
if (joysticks) {
int i;
for (i = num_joysticks - 1; i >= 0 && num_gamepads == 0; --i) {
if (SDL_IsGamepad(joysticks[i])) {
++num_gamepads;
}
}
SDL_free(joysticks);
}
if (num_gamepads > 0) {
return SDL_TRUE;
Expand Down
7 changes: 3 additions & 4 deletions src/joystick/SDL_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ SDL_bool SDL_HasJoystick(void)
return SDL_FALSE;
}

SDL_JoystickID *SDL_GetJoysticks(int *count)
const SDL_JoystickID *SDL_GetJoysticks(int *count)
{
int i, num_joysticks, device_index;
int joystick_index = 0, total_joysticks = 0;
Expand Down Expand Up @@ -751,7 +751,7 @@ SDL_JoystickID *SDL_GetJoysticks(int *count)
}
SDL_UnlockJoysticks();

return joysticks;
return SDL_FreeLater(joysticks);
}

const SDL_SteamVirtualGamepadInfo *SDL_GetJoystickVirtualGamepadInfoForID(SDL_JoystickID instance_id)
Expand Down Expand Up @@ -1905,7 +1905,7 @@ void SDL_CloseJoystick(SDL_Joystick *joystick)
void SDL_QuitJoysticks(void)
{
int i;
SDL_JoystickID *joysticks;
const SDL_JoystickID *joysticks;

SDL_LockJoysticks();

Expand All @@ -1916,7 +1916,6 @@ void SDL_QuitJoysticks(void)
for (i = 0; joysticks[i]; ++i) {
SDL_PrivateJoystickRemoved(joysticks[i]);
}
SDL_free(joysticks);
}

while (SDL_joysticks) {
Expand Down
3 changes: 1 addition & 2 deletions test/testcontroller.c
Original file line number Diff line number Diff line change
Expand Up @@ -1187,15 +1187,14 @@ static void OpenVirtualGamepad(void)
static void CloseVirtualGamepad(void)
{
int i;
SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
const SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
if (joysticks) {
for (i = 0; joysticks[i]; ++i) {
SDL_JoystickID instance_id = joysticks[i];
if (SDL_IsJoystickVirtual(instance_id)) {
SDL_DetachVirtualJoystick(instance_id);
}
}
SDL_free(joysticks);
}

if (virtual_joystick) {
Expand Down
2 changes: 1 addition & 1 deletion test/testhotplug.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main(int argc, char *argv[])
SDL_free(SDL_GetMice(&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);

if (enable_haptic) {
Expand Down

0 comments on commit 4961af4

Please sign in to comment.