ソースを参照

Introduce enum for SDL_GetCameraPermissionState result

Merlyn Morgan-Graham 1 ヶ月 前
コミット
385715c0dd

+ 15 - 1
include/SDL3/SDL_camera.h

@@ -136,6 +136,20 @@ typedef enum SDL_CameraPosition
     SDL_CAMERA_POSITION_BACK_FACING
     SDL_CAMERA_POSITION_BACK_FACING
 } SDL_CameraPosition;
 } SDL_CameraPosition;
 
 
+/**
+ * The current state of a request for camera access.
+ *
+ * \since This enum is available since SDL 3.4.0.
+ *
+ * \sa SDL_GetCameraPermissionState
+ */
+typedef enum SDL_CameraPermissionState
+{
+    SDL_CAMERA_PERMISSION_STATE_DENIED = -1,
+    SDL_CAMERA_PERMISSION_STATE_PENDING,
+    SDL_CAMERA_PERMISSION_STATE_APPROVED,
+} SDL_CameraPermissionState;
+
 
 
 /**
 /**
  * Use this function to get the number of built-in camera drivers.
  * Use this function to get the number of built-in camera drivers.
@@ -368,7 +382,7 @@ extern SDL_DECLSPEC SDL_Camera * SDLCALL SDL_OpenCamera(SDL_CameraID instance_id
  * \sa SDL_OpenCamera
  * \sa SDL_OpenCamera
  * \sa SDL_CloseCamera
  * \sa SDL_CloseCamera
  */
  */
-extern SDL_DECLSPEC int SDLCALL SDL_GetCameraPermissionState(SDL_Camera *camera);
+extern SDL_DECLSPEC SDL_CameraPermissionState SDLCALL SDL_GetCameraPermissionState(SDL_Camera *camera);
 
 
 /**
 /**
  * Get the instance ID of an opened camera.
  * Get the instance ID of an opened camera.

+ 9 - 9
src/camera/SDL_camera.c

@@ -270,7 +270,7 @@ static void ClosePhysicalCamera(SDL_Camera *device)
 
 
     SDL_aligned_free(device->zombie_pixels);
     SDL_aligned_free(device->zombie_pixels);
 
 
-    device->permission = 0;
+    device->permission = SDL_CAMERA_PERMISSION_STATE_PENDING;
     device->zombie_pixels = NULL;
     device->zombie_pixels = NULL;
     device->filled_output_surfaces.next = NULL;
     device->filled_output_surfaces.next = NULL;
     device->empty_output_surfaces.next = NULL;
     device->empty_output_surfaces.next = NULL;
@@ -581,7 +581,7 @@ void SDL_CameraPermissionOutcome(SDL_Camera *device, bool approved)
     pending.next = NULL;
     pending.next = NULL;
     SDL_PendingCameraEvent *pending_tail = &pending;
     SDL_PendingCameraEvent *pending_tail = &pending;
 
 
-    const int permission = approved ? 1 : -1;
+    const SDL_CameraPermissionState permission = approved ? SDL_CAMERA_PERMISSION_STATE_APPROVED : SDL_CAMERA_PERMISSION_STATE_DENIED;
 
 
     ObtainPhysicalCameraObj(device);
     ObtainPhysicalCameraObj(device);
     if (device->permission != permission) {
     if (device->permission != permission) {
@@ -665,7 +665,7 @@ bool SDL_GetCameraFormat(SDL_Camera *camera, SDL_CameraSpec *spec)
 
 
     SDL_Camera *device = camera;  // currently there's no separation between physical and logical device.
     SDL_Camera *device = camera;  // currently there's no separation between physical and logical device.
     ObtainPhysicalCameraObj(device);
     ObtainPhysicalCameraObj(device);
-    if (device->permission > 0) {
+    if (device->permission > SDL_CAMERA_PERMISSION_STATE_PENDING) {
         SDL_copyp(spec, &device->spec);
         SDL_copyp(spec, &device->spec);
         result = true;
         result = true;
     } else {
     } else {
@@ -808,9 +808,9 @@ bool SDL_CameraThreadIterate(SDL_Camera *device)
     }
     }
 
 
     const int permission = device->permission;
     const int permission = device->permission;
-    if (permission <= 0) {
+    if (permission <= SDL_CAMERA_PERMISSION_STATE_PENDING) {
         SDL_UnlockMutex(device->lock);
         SDL_UnlockMutex(device->lock);
-        return (permission < 0) ? false : true;  // if permission was denied, shut it down. if undecided, we're done for now.
+        return (permission < SDL_CAMERA_PERMISSION_STATE_PENDING) ? false : true;  // if permission was denied, shut it down. if undecided, we're done for now.
     }
     }
 
 
     bool failed = false;  // set to true if disaster worthy of treating the device as lost has happened.
     bool failed = false;  // set to true if disaster worthy of treating the device as lost has happened.
@@ -1264,7 +1264,7 @@ SDL_Surface *SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS)
 
 
     ObtainPhysicalCameraObj(device);
     ObtainPhysicalCameraObj(device);
 
 
-    if (device->permission <= 0) {
+    if (device->permission <= SDL_CAMERA_PERMISSION_STATE_PENDING) {
         ReleaseCamera(device);
         ReleaseCamera(device);
         SDL_SetError("Camera permission has not been granted");
         SDL_SetError("Camera permission has not been granted");
         return NULL;
         return NULL;
@@ -1371,12 +1371,12 @@ SDL_PropertiesID SDL_GetCameraProperties(SDL_Camera *camera)
     return result;
     return result;
 }
 }
 
 
-int SDL_GetCameraPermissionState(SDL_Camera *camera)
+SDL_CameraPermissionState SDL_GetCameraPermissionState(SDL_Camera *camera)
 {
 {
-    int result;
+    SDL_CameraPermissionState result;
     if (!camera) {
     if (!camera) {
         SDL_InvalidParamError("camera");
         SDL_InvalidParamError("camera");
-        result = -1;
+        result = SDL_CAMERA_PERMISSION_STATE_DENIED;
     } else {
     } else {
         SDL_Camera *device = camera;  // currently there's no separation between physical and logical device.
         SDL_Camera *device = camera;  // currently there's no separation between physical and logical device.
         ObtainPhysicalCameraObj(device);
         ObtainPhysicalCameraObj(device);

+ 2 - 2
src/camera/SDL_syscamera.h

@@ -160,8 +160,8 @@ struct SDL_Camera
     // Optional properties.
     // Optional properties.
     SDL_PropertiesID props;
     SDL_PropertiesID props;
 
 
-    // -1: user denied permission, 0: waiting for user response, 1: user approved permission.
-    int permission;
+    // Current state of user permission check.
+    SDL_CameraPermissionState permission;
 
 
     // Data private to this driver, used when device is opened and running.
     // Data private to this driver, used when device is opened and running.
     struct SDL_PrivateCameraData *hidden;
     struct SDL_PrivateCameraData *hidden;

+ 2 - 2
src/camera/coremedia/SDL_camera_coremedia.m

@@ -85,7 +85,7 @@ static void CoreMediaFormatToSDL(FourCharCode fmt, SDL_PixelFormat *pixel_format
 
 
 static bool CheckCameraPermissions(SDL_Camera *device)
 static bool CheckCameraPermissions(SDL_Camera *device)
 {
 {
-    if (device->permission == 0) {  // still expecting a permission result.
+    if (device->permission == SDL_CAMERA_PERMISSION_STATE_PENDING) {  // still expecting a permission result.
         if (@available(macOS 14, *)) {
         if (@available(macOS 14, *)) {
             const AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
             const AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
             if (status != AVAuthorizationStatusNotDetermined) {   // NotDetermined == still waiting for an answer from the user.
             if (status != AVAuthorizationStatusNotDetermined) {   // NotDetermined == still waiting for an answer from the user.
@@ -96,7 +96,7 @@ static bool CheckCameraPermissions(SDL_Camera *device)
         }
         }
     }
     }
 
 
-    return (device->permission > 0);
+    return (device->permission > SDL_CAMERA_PERMISSION_STATE_PENDING);
 }
 }
 
 
 // this delegate just receives new video frames on a Grand Central Dispatch queue, and fires off the
 // this delegate just receives new video frames on a Grand Central Dispatch queue, and fires off the

+ 1 - 1
src/dynapi/SDL_dynapi_procs.h

@@ -288,7 +288,7 @@ SDL_DYNAPI_PROC(const char*,SDL_GetCameraDriver,(int a),(a),return)
 SDL_DYNAPI_PROC(bool,SDL_GetCameraFormat,(SDL_Camera *a, SDL_CameraSpec *b),(a,b),return)
 SDL_DYNAPI_PROC(bool,SDL_GetCameraFormat,(SDL_Camera *a, SDL_CameraSpec *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_CameraID,SDL_GetCameraID,(SDL_Camera *a),(a),return)
 SDL_DYNAPI_PROC(SDL_CameraID,SDL_GetCameraID,(SDL_Camera *a),(a),return)
 SDL_DYNAPI_PROC(const char*,SDL_GetCameraName,(SDL_CameraID a),(a),return)
 SDL_DYNAPI_PROC(const char*,SDL_GetCameraName,(SDL_CameraID a),(a),return)
-SDL_DYNAPI_PROC(int,SDL_GetCameraPermissionState,(SDL_Camera *a),(a),return)
+SDL_DYNAPI_PROC(SDL_CameraPermissionState,SDL_GetCameraPermissionState,(SDL_Camera *a),(a),return)
 SDL_DYNAPI_PROC(SDL_CameraPosition,SDL_GetCameraPosition,(SDL_CameraID 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(SDL_PropertiesID,SDL_GetCameraProperties,(SDL_Camera *a),(a),return)
 SDL_DYNAPI_PROC(SDL_CameraSpec**,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_CameraSpec**,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return)