Skip to content

Introduce enum for SDL_GetCameraPermissionState result #13756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion include/SDL3/SDL_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ typedef enum SDL_CameraPosition
SDL_CAMERA_POSITION_BACK_FACING
} 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.
Expand Down Expand Up @@ -368,7 +382,7 @@ extern SDL_DECLSPEC SDL_Camera * SDLCALL SDL_OpenCamera(SDL_CameraID instance_id
* \sa SDL_OpenCamera
* \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.
Expand Down
18 changes: 9 additions & 9 deletions src/camera/SDL_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ static void ClosePhysicalCamera(SDL_Camera *device)

SDL_aligned_free(device->zombie_pixels);

device->permission = 0;
device->permission = SDL_CAMERA_PERMISSION_STATE_PENDING;
device->zombie_pixels = NULL;
device->filled_output_surfaces.next = NULL;
device->empty_output_surfaces.next = NULL;
Expand Down Expand Up @@ -581,7 +581,7 @@ void SDL_CameraPermissionOutcome(SDL_Camera *device, bool approved)
pending.next = NULL;
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);
if (device->permission != permission) {
Expand Down Expand Up @@ -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.
ObtainPhysicalCameraObj(device);
if (device->permission > 0) {
if (device->permission > SDL_CAMERA_PERMISSION_STATE_PENDING) {
SDL_copyp(spec, &device->spec);
result = true;
} else {
Expand Down Expand Up @@ -808,9 +808,9 @@ bool SDL_CameraThreadIterate(SDL_Camera *device)
}

const int permission = device->permission;
if (permission <= 0) {
if (permission <= SDL_CAMERA_PERMISSION_STATE_PENDING) {
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.
Expand Down Expand Up @@ -1264,7 +1264,7 @@ SDL_Surface *SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS)

ObtainPhysicalCameraObj(device);

if (device->permission <= 0) {
if (device->permission <= SDL_CAMERA_PERMISSION_STATE_PENDING) {
ReleaseCamera(device);
SDL_SetError("Camera permission has not been granted");
return NULL;
Expand Down Expand Up @@ -1371,12 +1371,12 @@ SDL_PropertiesID SDL_GetCameraProperties(SDL_Camera *camera)
return result;
}

int SDL_GetCameraPermissionState(SDL_Camera *camera)
SDL_CameraPermissionState SDL_GetCameraPermissionState(SDL_Camera *camera)
{
int result;
SDL_CameraPermissionState result;
if (!camera) {
SDL_InvalidParamError("camera");
result = -1;
result = SDL_CAMERA_PERMISSION_STATE_DENIED;
} else {
SDL_Camera *device = camera; // currently there's no separation between physical and logical device.
ObtainPhysicalCameraObj(device);
Expand Down
4 changes: 2 additions & 2 deletions src/camera/SDL_syscamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ struct SDL_Camera
// Optional properties.
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.
struct SDL_PrivateCameraData *hidden;
Expand Down
4 changes: 2 additions & 2 deletions src/camera/coremedia/SDL_camera_coremedia.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ @implementation SDLPrivateCameraData

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, *)) {
const AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status != AVAuthorizationStatusNotDetermined) { // NotDetermined == still waiting for an answer from the user.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(SDL_CameraID,SDL_GetCameraID,(SDL_Camera *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_PropertiesID,SDL_GetCameraProperties,(SDL_Camera *a),(a),return)
SDL_DYNAPI_PROC(SDL_CameraSpec**,SDL_GetCameraSupportedFormats,(SDL_CameraID a, int *b),(a,b),return)
Expand Down
Loading