From 01199469de4458347a3ec999a14cac72ecaf46b5 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 18 Jul 2024 16:33:52 -0700
Subject: [PATCH] SDL_GetCameras() follows the SDL_GetStringRule
---
include/SDL3/SDL_camera.h | 9 +++++----
src/camera/SDL_camera.c | 10 +++++-----
src/dynapi/SDL_dynapi_procs.h | 2 +-
test/testcamera.c | 4 +---
4 files changed, 12 insertions(+), 13 deletions(-)
diff --git a/include/SDL3/SDL_camera.h b/include/SDL3/SDL_camera.h
index 1114043d077d0..04db2eabff740 100644
--- a/include/SDL3/SDL_camera.h
+++ b/include/SDL3/SDL_camera.h
@@ -170,10 +170,11 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentCameraDriver(void);
/**
* Get a list of currently connected camera devices.
*
- * \param count a pointer filled in with the number of camera devices. Can be
+ * The returned array follows the SDL_GetStringRule, and will be automatically freed later.
+ *
+ * \param count a pointer filled in with the number of cameras returned, may be
* NULL.
- * \returns a 0 terminated array of camera instance IDs which should be freed
- * with SDL_free() or NULL on failure; call SDL_GetError() for more
+ * \returns a 0 terminated array of camera instance IDs or NULL on failure; call SDL_GetError() for more
* information.
*
* \threadsafety It is safe to call this function from any thread.
@@ -182,7 +183,7 @@ extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentCameraDriver(void);
*
* \sa SDL_OpenCamera
*/
-extern SDL_DECLSPEC SDL_CameraID * SDLCALL SDL_GetCameras(int *count);
+extern SDL_DECLSPEC const SDL_CameraID * SDLCALL SDL_GetCameras(int *count);
/**
* Get the list of native formats/sizes a camera supports.
diff --git a/src/camera/SDL_camera.c b/src/camera/SDL_camera.c
index 756f3f7983703..ae1b1f9285664 100644
--- a/src/camera/SDL_camera.c
+++ b/src/camera/SDL_camera.c
@@ -695,7 +695,7 @@ SDL_CameraPosition SDL_GetCameraPosition(SDL_CameraID instance_id)
}
-SDL_CameraID *SDL_GetCameras(int *count)
+const SDL_CameraID *SDL_GetCameras(int *count)
{
int dummy_count;
if (!count) {
@@ -731,7 +731,7 @@ SDL_CameraID *SDL_GetCameras(int *count)
*count = num_devices;
- return retval;
+ return SDL_FreeLater(retval);
}
const SDL_CameraSpec * const *SDL_GetCameraSupportedFormats(SDL_CameraID instance_id, int *count)
@@ -747,10 +747,10 @@ const SDL_CameraSpec * const *SDL_GetCameraSupportedFormats(SDL_CameraID instanc
int i;
int num_specs = device->num_specs;
- const SDL_CameraSpec **retval = (const SDL_CameraSpec **) SDL_malloc(((num_specs + 1) * sizeof(SDL_CameraSpec *)) + (num_specs * sizeof (SDL_CameraSpec)));
+ const SDL_CameraSpec **retval = (const SDL_CameraSpec **) SDL_malloc(((num_specs + 1) * sizeof(*retval)) + (num_specs * sizeof (**retval)));
if (retval) {
- SDL_CameraSpec *specs = (SDL_CameraSpec *)((Uint8 *)retval + ((num_specs + 1) * sizeof(SDL_CameraSpec *)));
- SDL_memcpy(specs, device->all_specs, sizeof (SDL_CameraSpec) * num_specs);
+ SDL_CameraSpec *specs = (SDL_CameraSpec *)((Uint8 *)retval + ((num_specs + 1) * sizeof(*retval)));
+ SDL_memcpy(specs, device->all_specs, num_specs * sizeof(*specs));
for (i = 0; i < num_specs; ++i) {
retval[i] = specs++;
}
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 54608dff344f3..aec04c3ddab4c 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -233,7 +233,7 @@ SDL_DYNAPI_PROC(int,SDL_GetCameraPermissionState,(SDL_Camera *a),(a),return)
SDL_DYNAPI_PROC(SDL_CameraPosition,SDL_GetCameraPosition,(SDL_CameraID a),(a),return)
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetCameraProperties,(SDL_Camera *a),(a),return)
SDL_DYNAPI_PROC(const SDL_CameraSpec* const*,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return)
-SDL_DYNAPI_PROC(SDL_CameraID*,SDL_GetCameras,(int *a),(a),return)
+SDL_DYNAPI_PROC(const SDL_CameraID*,SDL_GetCameras,(int *a),(a),return)
SDL_DYNAPI_PROC(void*,SDL_GetClipboardData,(const char *a, size_t *b),(a,b),return)
SDL_DYNAPI_PROC(const char*,SDL_GetClipboardText,(void),(),return)
SDL_DYNAPI_PROC(const SDL_DisplayMode*,SDL_GetClosestFullscreenDisplayMode,(SDL_DisplayID a, int b, int c, float d, SDL_bool e),(a,b,c,d,e),return)
diff --git a/test/testcamera.c b/test/testcamera.c
index 17fc5d3acd6e0..25dbaa51e65a7 100644
--- a/test/testcamera.c
+++ b/test/testcamera.c
@@ -101,7 +101,7 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
return SDL_APP_FAILURE;
}
- SDL_CameraID *devices = SDL_GetCameras(&devcount);
+ const SDL_CameraID *devices = SDL_GetCameras(&devcount);
if (!devices) {
SDL_Log("SDL_GetCameras failed: %s", SDL_GetError());
return SDL_APP_FAILURE;
@@ -140,8 +140,6 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
}
}
- SDL_free(devices);
-
if (!camera_id) {
SDL_Log("No cameras available?");
return SDL_APP_FAILURE;