macOS: Check for Auto-Type permissions on use instead of at launch

* Fix #3689 - link the use of Auto-Type with the permissions required to use it
This commit is contained in:
Jonathan White
2019-11-03 23:00:03 -05:00
committed by Janek Bevendorff
parent 440331d319
commit 7ba9fcc0e5
7 changed files with 76 additions and 14 deletions

View File

@@ -38,6 +38,7 @@ public:
bool isHidden(pid_t pid);
bool isDarkMode();
bool enableAccessibility();
bool enableScreenRecording();
signals:
void lockDatabases();

View File

@@ -37,5 +37,6 @@
- (bool) isDarkMode;
- (void) userSwitchHandler:(NSNotification*) notification;
- (bool) enableAccessibility;
- (bool) enableScreenRecording;
@end

View File

@@ -19,6 +19,7 @@
#import "AppKitImpl.h"
#import <AppKit/NSWorkspace.h>
#import <CoreVideo/CVPixelBuffer.h>
#import <Availability.h>
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101200
@@ -128,21 +129,36 @@ static const NSEventMask NSEventMaskKeyDown = NSKeyDownMask;
//
- (bool) enableAccessibility
{
// Request a 1 pixel screenshot to trigger the permissions
// required for screen reader access. These are necessary
// for Auto-Type to find the window titles in macOS 10.15+
CGImageRef screenshot = CGWindowListCreateImage(
CGRectMake(0, 0, 1, 1),
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID,
kCGWindowImageDefault);
CFRelease(screenshot);
// Request accessibility permissions for Auto-Type type on behalf of the user
NSDictionary* opts = @{static_cast<id>(kAXTrustedCheckOptionPrompt): @YES};
return AXIsProcessTrustedWithOptions(static_cast<CFDictionaryRef>(opts));
}
//
// Check if screen recording is enabled, may show an popup asking for permissions
//
- (bool) enableScreenRecording
{
if (@available(macOS 10.15, *)) {
// Request screen recording permission on macOS 10.15+
// This is necessary to get the current window title
CGDisplayStreamRef stream = CGDisplayStreamCreate(CGMainDisplayID(), 1, 1, kCVPixelFormatType_32BGRA, nil,
^(CGDisplayStreamFrameStatus status, uint64_t displayTime,
IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
Q_UNUSED(status);
Q_UNUSED(displayTime);
Q_UNUSED(frameSurface);
Q_UNUSED(updateRef);
});
if (stream) {
CFRelease(stream);
} else {
return NO;
}
}
return YES;
}
@end
//
@@ -198,4 +214,9 @@ bool AppKit::isDarkMode()
bool AppKit::enableAccessibility()
{
return [static_cast<id>(self) enableAccessibility];
}
}
bool AppKit::enableScreenRecording()
{
return [static_cast<id>(self) enableScreenRecording];
}

View File

@@ -80,3 +80,8 @@ bool MacUtils::enableAccessibility()
{
return m_appkit->enableAccessibility();
}
bool MacUtils::enableScreenRecording()
{
return m_appkit->enableScreenRecording();
}

View File

@@ -39,6 +39,7 @@ public:
bool isHidden();
bool isDarkMode();
bool enableAccessibility();
bool enableScreenRecording();
signals:
void lockDatabases();