-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
fix(macos): properly handle accessibility permission #2508
Draft
Hazer
wants to merge
4
commits into
LizardByte:master
Choose a base branch
from
Hazer:fix/macos-accessibility-request
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+178
−48
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,8 @@ | |||||
#define __APPLE_USE_RFC_3542 1 | ||||||
#endif | ||||||
|
||||||
#include <Carbon/Carbon.h> | ||||||
|
||||||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#include <Foundation/Foundation.h> | ||||||
#include <arpa/inet.h> | ||||||
#include <dlfcn.h> | ||||||
|
@@ -32,55 +34,11 @@ | |||||
|
||||||
namespace platf { | ||||||
|
||||||
// Even though the following two functions are available starting in macOS 10.15, they weren't | ||||||
// actually in the Mac SDK until Xcode 12.2, the first to include the SDK for macOS 11 | ||||||
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 110000 // __MAC_11_0 | ||||||
// If they're not in the SDK then we can use our own function definitions. | ||||||
// Need to use weak import so that this will link in macOS 10.14 and earlier | ||||||
extern "C" bool | ||||||
CGPreflightScreenCaptureAccess(void) __attribute__((weak_import)); | ||||||
extern "C" bool | ||||||
CGRequestScreenCaptureAccess(void) __attribute__((weak_import)); | ||||||
#endif | ||||||
|
||||||
namespace { | ||||||
auto screen_capture_allowed = std::atomic<bool> { false }; | ||||||
} // namespace | ||||||
|
||||||
// Return whether screen capture is allowed for this process. | ||||||
bool | ||||||
is_screen_capture_allowed() { | ||||||
return screen_capture_allowed; | ||||||
} | ||||||
|
||||||
std::unique_ptr<deinit_t> | ||||||
init() { | ||||||
// This will generate a warning about CGPreflightScreenCaptureAccess and | ||||||
// CGRequestScreenCaptureAccess being unavailable before macOS 10.15, but | ||||||
// we have a guard to prevent it from being called on those earlier systems. | ||||||
// Unfortunately the supported way to silence this warning, using @available, | ||||||
// produces linker errors for __isPlatformVersionAtLeast, so we have to use | ||||||
// a different method. | ||||||
// We also ignore "tautological-pointer-compare" because when compiling with | ||||||
// Xcode 12.2 and later, these functions are not weakly linked and will never | ||||||
// be null, and therefore generate this warning. Since we are weakly linking | ||||||
// when compiling with earlier Xcode versions, the check for null is | ||||||
// necessary, and so we ignore the warning. | ||||||
#pragma clang diagnostic push | ||||||
#pragma clang diagnostic ignored "-Wunguarded-availability-new" | ||||||
#pragma clang diagnostic ignored "-Wtautological-pointer-compare" | ||||||
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:((NSOperatingSystemVersion) { 10, 15, 0 })] && | ||||||
// Double check that these weakly-linked symbols have been loaded: | ||||||
CGPreflightScreenCaptureAccess != nullptr && CGRequestScreenCaptureAccess != nullptr && | ||||||
!CGPreflightScreenCaptureAccess()) { | ||||||
BOOST_LOG(error) << "No screen capture permission!"sv; | ||||||
BOOST_LOG(error) << "Please activate it in 'System Preferences' -> 'Privacy' -> 'Screen Recording'"sv; | ||||||
CGRequestScreenCaptureAccess(); | ||||||
if (permissions_manager.request_screen_capture_permission()) { | ||||||
return nullptr; | ||||||
} | ||||||
#pragma clang diagnostic pop | ||||||
// Record that we determined that we have the screen capture permission. | ||||||
screen_capture_allowed = true; | ||||||
return std::make_unique<deinit_t>(); | ||||||
} | ||||||
|
||||||
|
@@ -566,6 +524,7 @@ operator bool() override { | |||||
create_high_precision_timer() { | ||||||
return std::make_unique<macos_high_precision_timer>(); | ||||||
} | ||||||
|
||||||
} // namespace platf | ||||||
|
||||||
namespace dyn { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* @file src/platform/macos/permissions_manager.h | ||
* @brief Handles macOS platform permissions. | ||
*/ | ||
#pragma once | ||
|
||
#include <Carbon/Carbon.h> | ||
|
||
#include <atomic> | ||
#include <string> | ||
|
||
namespace platf { | ||
class PermissionsManager { | ||
public: | ||
static std::string | ||
default_accessibility_log_msg() { | ||
return "Accessibility permission is not enabled," | ||
" please enable sunshine in " | ||
"[System Settings > Privacy & Security > Privacy > Accessibility]" | ||
", then please restart Sunshine for it to take effect"; | ||
} | ||
|
||
PermissionsManager() = default; | ||
|
||
bool | ||
is_screen_capture_allowed(); | ||
|
||
bool | ||
request_screen_capture_permission(); | ||
|
||
/** | ||
* Checks for Accessibility permission | ||
* @return returns true if sunshine has Accessibility permission enabled | ||
*/ | ||
bool | ||
has_accessibility_permission(); | ||
|
||
/** | ||
* Checks for Accessibility permission | ||
* @return returns true if sunshine has Accessibility permission enabled | ||
*/ | ||
bool | ||
has_accessibility_permission_cached(); | ||
|
||
/** | ||
* Prompts the user for Accessibility permission | ||
* @return returns true if requested permission, false if already has permission | ||
*/ | ||
bool | ||
request_accessibility_permission(); | ||
|
||
/** | ||
* Prompts the user for Accessibility permission | ||
* @return returns true if requested permission, false if already has permission | ||
*/ | ||
bool | ||
request_accessibility_permission_once(); | ||
|
||
/** | ||
* Prints the accessibility status based on the input event type and release status | ||
* @param is_keyboard_event indicates if the event is a keyboard event | ||
* @param release indicates if the event is a release event | ||
*/ | ||
void | ||
print_accessibility_status(const bool is_keyboard_event, const bool release); | ||
}; | ||
} // namespace platf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* @file src/platform/macos/permissions_manager.mm | ||
* @brief Handles macOS platform permissions. | ||
*/ | ||
|
||
#include "permissions_manager.h" | ||
|
||
#include <Foundation/Foundation.h> | ||
|
||
#include "src/logging.h" | ||
|
||
namespace platf { | ||
namespace { | ||
auto | ||
screen_capture_allowed = std::atomic { false }; | ||
/** | ||
* Used to avoid spamming permission requests when the user receives an input event | ||
*/ | ||
bool | ||
accessibility_permission_requested = std::atomic { false }; | ||
bool | ||
has_accessibility = std::atomic { false }; | ||
} // namespace | ||
|
||
// Return whether screen capture is allowed for this process. | ||
bool | ||
PermissionsManager::is_screen_capture_allowed() { | ||
return screen_capture_allowed; | ||
} | ||
|
||
bool | ||
PermissionsManager::request_screen_capture_permission() { | ||
if (!CGPreflightScreenCaptureAccess()) { | ||
BOOST_LOG(error) << "No screen capture permission!"; | ||
BOOST_LOG(error) << "Please activate it in 'System Preferences' -> 'Privacy' -> 'Screen Recording'"; | ||
CGRequestScreenCaptureAccess(); | ||
return true; | ||
} | ||
// Record that we determined that we have the screen capture permission. | ||
screen_capture_allowed = true; | ||
return false; | ||
} | ||
|
||
bool | ||
PermissionsManager::has_accessibility_permission() { | ||
NSDictionary *options = @{static_cast<id>(kAXTrustedCheckOptionPrompt): @NO}; | ||
// We use kAXTrustedCheckOptionPrompt == NO here, | ||
// instead of using XIsProcessTrusted(), | ||
// because this will update the accessibility list with sunshine current path | ||
return AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef>(options)); | ||
} | ||
|
||
bool | ||
PermissionsManager::has_accessibility_permission_cached() { | ||
if (has_accessibility) return true; | ||
if (accessibility_permission_requested) return has_accessibility; | ||
has_accessibility = has_accessibility_permission(); | ||
return has_accessibility; | ||
} | ||
|
||
bool | ||
PermissionsManager::request_accessibility_permission() { | ||
if (!has_accessibility_permission()) { | ||
NSDictionary *options = @{static_cast<id>(kAXTrustedCheckOptionPrompt): @YES}; | ||
return !AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef>(options)); | ||
} | ||
return false; | ||
} | ||
|
||
bool | ||
PermissionsManager::request_accessibility_permission_once() { | ||
if (!accessibility_permission_requested) { | ||
accessibility_permission_requested = true; | ||
return request_accessibility_permission(); | ||
} | ||
return false; | ||
} | ||
|
||
void | ||
PermissionsManager::print_accessibility_status(const bool is_keyboard_event, const bool release) { | ||
if (!release) return; | ||
|
||
if (!has_accessibility_permission_cached()) { | ||
request_accessibility_permission_once(); | ||
BOOST_LOG(info) << "Received " << (is_keyboard_event ? "keyboard" : "mouse") << " event but " | ||
<< default_accessibility_log_msg(); | ||
} | ||
} | ||
|
||
} // namespace platf |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be more explicit?