Skip to content

Commit 4961af4

Browse files
committed
SDL_GetJoysticks() follows the SDL_GetStringRule
1 parent b32c961 commit 4961af4

File tree

7 files changed

+15
-20
lines changed

7 files changed

+15
-20
lines changed

Diff for: docs/README-migration.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ Rather than iterating over joysticks using device index, there is a new function
823823
{
824824
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) == 0) {
825825
int i, num_joysticks;
826-
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
826+
const SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
827827
if (joysticks) {
828828
for (i = 0; i < num_joysticks; ++i) {
829829
SDL_JoystickID instance_id = joysticks[i];
@@ -833,7 +833,6 @@ Rather than iterating over joysticks using device index, there is a new function
833833
SDL_Log("Joystick %" SDL_PRIu32 ": %s%s%s VID 0x%.4x, PID 0x%.4x\n",
834834
instance_id, name ? name : "Unknown", path ? ", " : "", path ? path : "", SDL_GetJoystickVendorForID(instance_id), SDL_GetJoystickProductForID(instance_id));
835835
}
836-
SDL_free(joysticks);
837836
}
838837
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
839838
}

Diff for: include/SDL3/SDL_joystick.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,18 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasJoystick(void);
210210
/**
211211
* Get a list of currently connected joysticks.
212212
*
213-
* \param count a pointer filled in with the number of joysticks returned.
214-
* \returns a 0 terminated array of joystick instance IDs which should be
215-
* freed with SDL_free(), or NULL on failure; call SDL_GetError() for
213+
* The returned array follows the SDL_GetStringRule, and will be automatically freed later.
214+
*
215+
* \param count a pointer filled in with the number of joysticks returned, may be NULL.
216+
* \returns a 0 terminated array of joystick instance IDs or NULL on failure; call SDL_GetError() for
216217
* more information.
217218
*
218219
* \since This function is available since SDL 3.0.0.
219220
*
220221
* \sa SDL_HasJoystick
221222
* \sa SDL_OpenJoystick
222223
*/
223-
extern SDL_DECLSPEC SDL_JoystickID * SDLCALL SDL_GetJoysticks(int *count);
224+
extern SDL_DECLSPEC const SDL_JoystickID * SDLCALL SDL_GetJoysticks(int *count);
224225

225226
/**
226227
* Get the implementation dependent name of a joystick.

Diff for: src/dynapi/SDL_dynapi_procs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickType,(SDL_Joystick *a),(a),retur
365365
SDL_DYNAPI_PROC(SDL_JoystickType,SDL_GetJoystickTypeForID,(SDL_JoystickID a),(a),return)
366366
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickVendor,(SDL_Joystick *a),(a),return)
367367
SDL_DYNAPI_PROC(Uint16,SDL_GetJoystickVendorForID,(SDL_JoystickID a),(a),return)
368-
SDL_DYNAPI_PROC(SDL_JoystickID*,SDL_GetJoysticks,(int *a),(a),return)
368+
SDL_DYNAPI_PROC(const SDL_JoystickID*,SDL_GetJoysticks,(int *a),(a),return)
369369
SDL_DYNAPI_PROC(SDL_Keycode,SDL_GetKeyFromName,(const char *a),(a),return)
370370
SDL_DYNAPI_PROC(SDL_Keycode,SDL_GetKeyFromScancode,(SDL_Scancode a, SDL_Keymod b),(a,b),return)
371371
SDL_DYNAPI_PROC(const char*,SDL_GetKeyName,(SDL_Keycode a),(a),return)

Diff for: src/joystick/SDL_gamepad.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ typedef struct GamepadMapping_t
8989
typedef struct
9090
{
9191
int refcount _guarded;
92-
SDL_JoystickID *joysticks _guarded;
92+
const SDL_JoystickID *joysticks _guarded;
9393
GamepadMapping_t **joystick_mappings _guarded;
9494

9595
int num_changed_mappings _guarded;
@@ -579,7 +579,6 @@ static void PopMappingChangeTracking(void)
579579
}
580580
}
581581

582-
SDL_free(tracker->joysticks);
583582
SDL_free(tracker->joystick_mappings);
584583
SDL_free(tracker->changed_mappings);
585584
SDL_free(tracker);
@@ -2358,7 +2357,7 @@ int SDL_InitGamepadMappings(void)
23582357
int SDL_InitGamepads(void)
23592358
{
23602359
int i;
2361-
SDL_JoystickID *joysticks;
2360+
const SDL_JoystickID *joysticks;
23622361

23632362
SDL_gamepads_initialized = SDL_TRUE;
23642363

@@ -2373,7 +2372,6 @@ int SDL_InitGamepads(void)
23732372
SDL_PrivateGamepadAdded(joysticks[i]);
23742373
}
23752374
}
2376-
SDL_free(joysticks);
23772375
}
23782376

23792377
return 0;
@@ -2383,15 +2381,14 @@ SDL_bool SDL_HasGamepad(void)
23832381
{
23842382
int num_joysticks = 0;
23852383
int num_gamepads = 0;
2386-
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
2384+
const SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
23872385
if (joysticks) {
23882386
int i;
23892387
for (i = num_joysticks - 1; i >= 0 && num_gamepads == 0; --i) {
23902388
if (SDL_IsGamepad(joysticks[i])) {
23912389
++num_gamepads;
23922390
}
23932391
}
2394-
SDL_free(joysticks);
23952392
}
23962393
if (num_gamepads > 0) {
23972394
return SDL_TRUE;

Diff for: src/joystick/SDL_joystick.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ SDL_bool SDL_HasJoystick(void)
714714
return SDL_FALSE;
715715
}
716716

717-
SDL_JoystickID *SDL_GetJoysticks(int *count)
717+
const SDL_JoystickID *SDL_GetJoysticks(int *count)
718718
{
719719
int i, num_joysticks, device_index;
720720
int joystick_index = 0, total_joysticks = 0;
@@ -751,7 +751,7 @@ SDL_JoystickID *SDL_GetJoysticks(int *count)
751751
}
752752
SDL_UnlockJoysticks();
753753

754-
return joysticks;
754+
return SDL_FreeLater(joysticks);
755755
}
756756

757757
const SDL_SteamVirtualGamepadInfo *SDL_GetJoystickVirtualGamepadInfoForID(SDL_JoystickID instance_id)
@@ -1905,7 +1905,7 @@ void SDL_CloseJoystick(SDL_Joystick *joystick)
19051905
void SDL_QuitJoysticks(void)
19061906
{
19071907
int i;
1908-
SDL_JoystickID *joysticks;
1908+
const SDL_JoystickID *joysticks;
19091909

19101910
SDL_LockJoysticks();
19111911

@@ -1916,7 +1916,6 @@ void SDL_QuitJoysticks(void)
19161916
for (i = 0; joysticks[i]; ++i) {
19171917
SDL_PrivateJoystickRemoved(joysticks[i]);
19181918
}
1919-
SDL_free(joysticks);
19201919
}
19211920

19221921
while (SDL_joysticks) {

Diff for: test/testcontroller.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1187,15 +1187,14 @@ static void OpenVirtualGamepad(void)
11871187
static void CloseVirtualGamepad(void)
11881188
{
11891189
int i;
1190-
SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
1190+
const SDL_JoystickID *joysticks = SDL_GetJoysticks(NULL);
11911191
if (joysticks) {
11921192
for (i = 0; joysticks[i]; ++i) {
11931193
SDL_JoystickID instance_id = joysticks[i];
11941194
if (SDL_IsJoystickVirtual(instance_id)) {
11951195
SDL_DetachVirtualJoystick(instance_id);
11961196
}
11971197
}
1198-
SDL_free(joysticks);
11991198
}
12001199

12011200
if (virtual_joystick) {

Diff for: test/testhotplug.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int main(int argc, char *argv[])
8686
SDL_free(SDL_GetMice(&num_mice));
8787
SDL_Log("There are %d mice at startup\n", num_mice);
8888

89-
SDL_free(SDL_GetJoysticks(&num_joysticks));
89+
SDL_GetJoysticks(&num_joysticks);
9090
SDL_Log("There are %d joysticks at startup\n", num_joysticks);
9191

9292
if (enable_haptic) {

0 commit comments

Comments
 (0)