mirror of
				https://github.com/PabloMK7/citra.git
				synced 2025-10-30 21:30:04 +00:00 
			
		
		
		
	common: Add C++ version of Apple authorization logic. (#6616)
This commit is contained in:
		
							parent
							
								
									03dbdfc12f
								
							
						
					
					
						commit
						bfb6a5b5de
					
				
					 10 changed files with 104 additions and 116 deletions
				
			
		|  | @ -254,12 +254,7 @@ if (APPLE) | |||
|         "${DIST_DIR}/LaunchScreen.storyboard" | ||||
|         "${DIST_DIR}/launch_logo.png" | ||||
|     ) | ||||
| 
 | ||||
|     target_sources(citra-qt PRIVATE | ||||
|         ${APPLE_RESOURCES} | ||||
|         macos_authorization.h | ||||
|         macos_authorization.mm | ||||
|     ) | ||||
|     target_sources(citra-qt PRIVATE ${APPLE_RESOURCES}) | ||||
| 
 | ||||
|     # Define app bundle metadata. | ||||
|     include(GenerateBuildInfo) | ||||
|  |  | |||
|  | @ -10,7 +10,7 @@ | |||
| #include "citra_qt/main.h" | ||||
| 
 | ||||
| #if defined(__APPLE__) | ||||
| #include "citra_qt/macos_authorization.h" | ||||
| #include "common/apple_authorization.h" | ||||
| #endif | ||||
| 
 | ||||
| namespace Camera { | ||||
|  |  | |||
|  | @ -14,7 +14,7 @@ | |||
| #include "ui_configure_audio.h" | ||||
| 
 | ||||
| #if defined(__APPLE__) | ||||
| #include "citra_qt/macos_authorization.h" | ||||
| #include "common/apple_authorization.h" | ||||
| #endif | ||||
| 
 | ||||
| ConfigureAudio::ConfigureAudio(QWidget* parent) | ||||
|  |  | |||
|  | @ -16,7 +16,7 @@ | |||
| #include "ui_configure_camera.h" | ||||
| 
 | ||||
| #if defined(__APPLE__) | ||||
| #include "citra_qt/macos_authorization.h" | ||||
| #include "common/apple_authorization.h" | ||||
| #endif | ||||
| 
 | ||||
| const std::array<std::string, 3> ConfigureCamera::Implementations = { | ||||
|  | @ -264,6 +264,9 @@ void ConfigureCamera::SetConfiguration() { | |||
|         } | ||||
|     } | ||||
|     if (camera_name[index] == "qt") { | ||||
| #ifdef __APPLE__ | ||||
|         AppleAuthorization::CheckAuthorizationForCamera(); | ||||
| #endif | ||||
|         ui->system_camera->setCurrentIndex(0); | ||||
|         if (!camera_config[index].empty()) { | ||||
|             ui->system_camera->setCurrentText(QString::fromStdString(camera_config[index])); | ||||
|  |  | |||
|  | @ -1,12 +0,0 @@ | |||
| // Copyright 2020 Citra Emulator Project
 | ||||
| // Licensed under GPLv2 or any later version
 | ||||
| // Refer to the license.txt file included.
 | ||||
| 
 | ||||
| #pragma once | ||||
| 
 | ||||
| namespace AppleAuthorization { | ||||
| 
 | ||||
| bool CheckAuthorizationForCamera(); | ||||
| bool CheckAuthorizationForMicrophone(); | ||||
| 
 | ||||
| } // namespace AppleAuthorization
 | ||||
|  | @ -1,93 +0,0 @@ | |||
| // 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/macos_authorization.h" | ||||
| #include "common/logging/log.h" | ||||
| 
 | ||||
| namespace AppleAuthorization { | ||||
| 
 | ||||
| static bool authorized_camera = false; | ||||
| static bool authorized_microphone = false; | ||||
| 
 | ||||
| static bool authorized = false; | ||||
| 
 | ||||
| enum class AuthMediaType { Camera, Microphone }; | ||||
| 
 | ||||
| // Based on | ||||
| // https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_macos | ||||
| // TODO: This could be rewritten to return the authorization state, having pure c++ code deal with | ||||
| // it, log information and possibly wait for the camera access request. | ||||
| void CheckAuthorization(AuthMediaType type) { | ||||
|     authorized = false; | ||||
|     if (@available(macOS 10.14, *)) { | ||||
|         NSString* media_type; | ||||
|         if (type == AuthMediaType::Camera) { | ||||
|             media_type = AVMediaTypeVideo; | ||||
|         } else { | ||||
|             media_type = AVMediaTypeAudio; | ||||
|         } | ||||
| 
 | ||||
|         // Request permission to access the camera and microphone. | ||||
|         switch ([AVCaptureDevice authorizationStatusForMediaType:media_type]) { | ||||
|         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 granted) { | ||||
|                                        authorized = granted; | ||||
|                                      }]; | ||||
|             if (type == AuthMediaType::Camera) { | ||||
|                 LOG_INFO(Frontend, "Camera access requested."); | ||||
|             } else { // AuthMediaType::Microphone | ||||
|                 LOG_INFO(Frontend, "Microphone access requested."); | ||||
|             } | ||||
|             break; | ||||
|         } | ||||
|         case AVAuthorizationStatusDenied: { | ||||
|             // The user has previously denied access. | ||||
|             authorized = false; | ||||
|             if (type == AuthMediaType::Camera) { | ||||
|                 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'"); | ||||
|             } | ||||
|             return; | ||||
|         } | ||||
|         case AVAuthorizationStatusRestricted: { | ||||
|             // The user can't grant access due to restrictions. | ||||
|             authorized = false; | ||||
|             return; | ||||
|         } | ||||
|         } | ||||
|     } else { | ||||
|         authorized = true; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| bool CheckAuthorizationForCamera() { | ||||
|     if (!authorized_camera) { | ||||
|         CheckAuthorization(AuthMediaType::Camera); | ||||
|         authorized_camera = authorized; | ||||
|     } | ||||
|     return authorized_camera; | ||||
| } | ||||
| 
 | ||||
| bool CheckAuthorizationForMicrophone() { | ||||
|     if (!authorized_microphone) { | ||||
|         CheckAuthorization(AuthMediaType::Microphone); | ||||
|         authorized_microphone = authorized; | ||||
|     } | ||||
|     return authorized_microphone; | ||||
| } | ||||
| 
 | ||||
| } // namespace AppleAuthorization | ||||
|  | @ -96,7 +96,7 @@ | |||
| #include "video_core/video_core.h" | ||||
| 
 | ||||
| #ifdef __APPLE__ | ||||
| #include "macos_authorization.h" | ||||
| #include "common/apple_authorization.h" | ||||
| #endif | ||||
| 
 | ||||
| #ifdef USE_DISCORD_PRESENCE | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue