Skip to content

Commit 385715c

Browse files
kavika13icculus
authored andcommitted
Introduce enum for SDL_GetCameraPermissionState result
1 parent 5be0848 commit 385715c

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

include/SDL3/SDL_camera.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ typedef enum SDL_CameraPosition
136136
SDL_CAMERA_POSITION_BACK_FACING
137137
} SDL_CameraPosition;
138138

139+
/**
140+
* The current state of a request for camera access.
141+
*
142+
* \since This enum is available since SDL 3.4.0.
143+
*
144+
* \sa SDL_GetCameraPermissionState
145+
*/
146+
typedef enum SDL_CameraPermissionState
147+
{
148+
SDL_CAMERA_PERMISSION_STATE_DENIED = -1,
149+
SDL_CAMERA_PERMISSION_STATE_PENDING,
150+
SDL_CAMERA_PERMISSION_STATE_APPROVED,
151+
} SDL_CameraPermissionState;
152+
139153

140154
/**
141155
* 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
368382
* \sa SDL_OpenCamera
369383
* \sa SDL_CloseCamera
370384
*/
371-
extern SDL_DECLSPEC int SDLCALL SDL_GetCameraPermissionState(SDL_Camera *camera);
385+
extern SDL_DECLSPEC SDL_CameraPermissionState SDLCALL SDL_GetCameraPermissionState(SDL_Camera *camera);
372386

373387
/**
374388
* Get the instance ID of an opened camera.

src/camera/SDL_camera.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ static void ClosePhysicalCamera(SDL_Camera *device)
270270

271271
SDL_aligned_free(device->zombie_pixels);
272272

273-
device->permission = 0;
273+
device->permission = SDL_CAMERA_PERMISSION_STATE_PENDING;
274274
device->zombie_pixels = NULL;
275275
device->filled_output_surfaces.next = NULL;
276276
device->empty_output_surfaces.next = NULL;
@@ -581,7 +581,7 @@ void SDL_CameraPermissionOutcome(SDL_Camera *device, bool approved)
581581
pending.next = NULL;
582582
SDL_PendingCameraEvent *pending_tail = &pending;
583583

584-
const int permission = approved ? 1 : -1;
584+
const SDL_CameraPermissionState permission = approved ? SDL_CAMERA_PERMISSION_STATE_APPROVED : SDL_CAMERA_PERMISSION_STATE_DENIED;
585585

586586
ObtainPhysicalCameraObj(device);
587587
if (device->permission != permission) {
@@ -665,7 +665,7 @@ bool SDL_GetCameraFormat(SDL_Camera *camera, SDL_CameraSpec *spec)
665665

666666
SDL_Camera *device = camera; // currently there's no separation between physical and logical device.
667667
ObtainPhysicalCameraObj(device);
668-
if (device->permission > 0) {
668+
if (device->permission > SDL_CAMERA_PERMISSION_STATE_PENDING) {
669669
SDL_copyp(spec, &device->spec);
670670
result = true;
671671
} else {
@@ -808,9 +808,9 @@ bool SDL_CameraThreadIterate(SDL_Camera *device)
808808
}
809809

810810
const int permission = device->permission;
811-
if (permission <= 0) {
811+
if (permission <= SDL_CAMERA_PERMISSION_STATE_PENDING) {
812812
SDL_UnlockMutex(device->lock);
813-
return (permission < 0) ? false : true; // if permission was denied, shut it down. if undecided, we're done for now.
813+
return (permission < SDL_CAMERA_PERMISSION_STATE_PENDING) ? false : true; // if permission was denied, shut it down. if undecided, we're done for now.
814814
}
815815

816816
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)
12641264

12651265
ObtainPhysicalCameraObj(device);
12661266

1267-
if (device->permission <= 0) {
1267+
if (device->permission <= SDL_CAMERA_PERMISSION_STATE_PENDING) {
12681268
ReleaseCamera(device);
12691269
SDL_SetError("Camera permission has not been granted");
12701270
return NULL;
@@ -1371,12 +1371,12 @@ SDL_PropertiesID SDL_GetCameraProperties(SDL_Camera *camera)
13711371
return result;
13721372
}
13731373

1374-
int SDL_GetCameraPermissionState(SDL_Camera *camera)
1374+
SDL_CameraPermissionState SDL_GetCameraPermissionState(SDL_Camera *camera)
13751375
{
1376-
int result;
1376+
SDL_CameraPermissionState result;
13771377
if (!camera) {
13781378
SDL_InvalidParamError("camera");
1379-
result = -1;
1379+
result = SDL_CAMERA_PERMISSION_STATE_DENIED;
13801380
} else {
13811381
SDL_Camera *device = camera; // currently there's no separation between physical and logical device.
13821382
ObtainPhysicalCameraObj(device);

src/camera/SDL_syscamera.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ struct SDL_Camera
160160
// Optional properties.
161161
SDL_PropertiesID props;
162162

163-
// -1: user denied permission, 0: waiting for user response, 1: user approved permission.
164-
int permission;
163+
// Current state of user permission check.
164+
SDL_CameraPermissionState permission;
165165

166166
// Data private to this driver, used when device is opened and running.
167167
struct SDL_PrivateCameraData *hidden;

src/camera/coremedia/SDL_camera_coremedia.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ @implementation SDLPrivateCameraData
8585

8686
static bool CheckCameraPermissions(SDL_Camera *device)
8787
{
88-
if (device->permission == 0) { // still expecting a permission result.
88+
if (device->permission == SDL_CAMERA_PERMISSION_STATE_PENDING) { // still expecting a permission result.
8989
if (@available(macOS 14, *)) {
9090
const AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
9191
if (status != AVAuthorizationStatusNotDetermined) { // NotDetermined == still waiting for an answer from the user.
@@ -96,7 +96,7 @@ static bool CheckCameraPermissions(SDL_Camera *device)
9696
}
9797
}
9898

99-
return (device->permission > 0);
99+
return (device->permission > SDL_CAMERA_PERMISSION_STATE_PENDING);
100100
}
101101

102102
// this delegate just receives new video frames on a Grand Central Dispatch queue, and fires off the

src/dynapi/SDL_dynapi_procs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ SDL_DYNAPI_PROC(const char*,SDL_GetCameraDriver,(int a),(a),return)
288288
SDL_DYNAPI_PROC(bool,SDL_GetCameraFormat,(SDL_Camera *a, SDL_CameraSpec *b),(a,b),return)
289289
SDL_DYNAPI_PROC(SDL_CameraID,SDL_GetCameraID,(SDL_Camera *a),(a),return)
290290
SDL_DYNAPI_PROC(const char*,SDL_GetCameraName,(SDL_CameraID a),(a),return)
291-
SDL_DYNAPI_PROC(int,SDL_GetCameraPermissionState,(SDL_Camera *a),(a),return)
291+
SDL_DYNAPI_PROC(SDL_CameraPermissionState,SDL_GetCameraPermissionState,(SDL_Camera *a),(a),return)
292292
SDL_DYNAPI_PROC(SDL_CameraPosition,SDL_GetCameraPosition,(SDL_CameraID a),(a),return)
293293
SDL_DYNAPI_PROC(SDL_PropertiesID,SDL_GetCameraProperties,(SDL_Camera *a),(a),return)
294294
SDL_DYNAPI_PROC(SDL_CameraSpec**,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return)

0 commit comments

Comments
 (0)