Browse Source

SDL_GetCameras() follows the SDL_GetStringRule

Sam Lantinga 9 months ago
parent
commit
01199469de
4 changed files with 12 additions and 13 deletions
  1. 5 4
      include/SDL3/SDL_camera.h
  2. 5 5
      src/camera/SDL_camera.c
  3. 1 1
      src/dynapi/SDL_dynapi_procs.h
  4. 1 3
      test/testcamera.c

+ 5 - 4
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.

+ 5 - 5
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++;
         }

+ 1 - 1
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)

+ 1 - 3
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;