2021-05-13 05:41:12 +00:00
|
|
|
// Copyright 2020 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#import <AVFoundation/AVFoundation.h>
|
|
|
|
|
|
|
|
#include "citra_qt/usage_authorization.h"
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
|
|
|
|
namespace AppleAuthorization {
|
|
|
|
|
|
|
|
static bool authorized = false;
|
|
|
|
|
2022-01-10 12:52:39 +00:00
|
|
|
enum class AuthMediaType { Camera, Microphone };
|
2021-05-13 05:41:12 +00:00
|
|
|
|
2022-01-10 12:52:39 +00:00
|
|
|
// Based on
|
|
|
|
// https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos
|
2021-05-13 05:41:12 +00:00
|
|
|
void CheckAuthorization(AuthMediaType type) {
|
|
|
|
if (@available(macOS 10.14, *)) {
|
2022-01-10 12:52:39 +00:00
|
|
|
NSString* media_type;
|
|
|
|
if (type == AuthMediaType::Camera) {
|
2021-05-13 05:41:12 +00:00
|
|
|
media_type = AVMediaTypeVideo;
|
2022-01-10 12:52:39 +00:00
|
|
|
} else {
|
2021-05-13 05:41:12 +00:00
|
|
|
media_type = AVMediaTypeAudio;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request permission to access the camera and microphone.
|
|
|
|
switch ([AVCaptureDevice authorizationStatusForMediaType:media_type]) {
|
2022-01-10 12:52:39 +00:00
|
|
|
case AVAuthorizationStatusAuthorized:
|
|
|
|
// The user has previously granted access to the camera.
|
|
|
|
authorized = true;
|
|
|
|
break;
|
|
|
|
case AVAuthorizationStatusNotDetermined: {
|
|
|
|
// The app hasn't yet asked the user for camera access.
|
|
|
|
[AVCaptureDevice requestAccessForMediaType:media_type
|
|
|
|
completionHandler:^(BOOL) {
|
|
|
|
authorized = true;
|
|
|
|
}];
|
|
|
|
if (type == AuthMediaType::Camera) {
|
|
|
|
LOG_INFO(Frontend, "Camera access requested.");
|
2022-01-10 13:38:49 +00:00
|
|
|
} else { // AuthMediaType::Microphone
|
|
|
|
LOG_INFO(Frontend, "Microphone access requested.");
|
2021-05-13 05:41:12 +00:00
|
|
|
}
|
2022-01-10 12:52:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case AVAuthorizationStatusDenied: {
|
|
|
|
// The user has previously denied access.
|
|
|
|
authorized = false;
|
|
|
|
if (type == AuthMediaType::Camera) {
|
2022-01-10 13:38:49 +00:00
|
|
|
LOG_WARNING(Frontend, "Camera access denied. To change this you may modify the "
|
|
|
|
"macOS system permission settings "
|
|
|
|
"for Citra at 'System Preferences -> Security & Privacy'");
|
|
|
|
} else { // AuthMediaType::Microphone
|
|
|
|
LOG_WARNING(Frontend, "Microphone access denied. To change this you may modify the "
|
|
|
|
"macOS system permission settings "
|
|
|
|
"for Citra at 'System Preferences -> Security & Privacy'");
|
2021-05-13 05:41:12 +00:00
|
|
|
}
|
2022-01-10 12:52:39 +00:00
|
|
|
return;
|
2021-05-13 05:41:12 +00:00
|
|
|
}
|
2022-01-10 12:52:39 +00:00
|
|
|
case AVAuthorizationStatusRestricted: {
|
|
|
|
// The user can't grant access due to restrictions.
|
|
|
|
authorized = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-13 05:41:12 +00:00
|
|
|
authorized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckAuthorizationForCamera() {
|
|
|
|
CheckAuthorization(AuthMediaType::Camera);
|
|
|
|
return authorized;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckAuthorizationForMicrophone() {
|
|
|
|
CheckAuthorization(AuthMediaType::Microphone);
|
|
|
|
return authorized;
|
|
|
|
}
|
|
|
|
|
2022-01-10 12:52:39 +00:00
|
|
|
} // AppleAuthorization
|