Browse Source

SDL_GetJoysticks() follows the SDL_GetStringRule

Sam Lantinga 9 months ago
parent
commit
4961af4569

+ 1 - 2
docs/README-migration.md

@@ -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];
@@ -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);
     }

+ 5 - 4
include/SDL3/SDL_joystick.h

@@ -210,9 +210,10 @@ 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.
@@ -220,7 +221,7 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasJoystick(void);
  * \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.

+ 1 - 1
src/dynapi/SDL_dynapi_procs.h

@@ -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)

+ 3 - 6
src/joystick/SDL_gamepad.c

@@ -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;
@@ -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);
@@ -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;
 
@@ -2373,7 +2372,6 @@ int SDL_InitGamepads(void)
                 SDL_PrivateGamepadAdded(joysticks[i]);
             }
         }
-        SDL_free(joysticks);
     }
 
     return 0;
@@ -2383,7 +2381,7 @@ 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) {
@@ -2391,7 +2389,6 @@ SDL_bool SDL_HasGamepad(void)
                 ++num_gamepads;
             }
         }
-        SDL_free(joysticks);
     }
     if (num_gamepads > 0) {
         return SDL_TRUE;

+ 3 - 4
src/joystick/SDL_joystick.c

@@ -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;
@@ -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)
@@ -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();
 
@@ -1916,7 +1916,6 @@ void SDL_QuitJoysticks(void)
         for (i = 0; joysticks[i]; ++i) {
             SDL_PrivateJoystickRemoved(joysticks[i]);
         }
-        SDL_free(joysticks);
     }
 
     while (SDL_joysticks) {

+ 1 - 2
test/testcontroller.c

@@ -1187,7 +1187,7 @@ 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];
@@ -1195,7 +1195,6 @@ static void CloseVirtualGamepad(void)
                 SDL_DetachVirtualJoystick(instance_id);
             }
         }
-        SDL_free(joysticks);
     }
 
     if (virtual_joystick) {

+ 1 - 1
test/testhotplug.c

@@ -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) {