Altered screen recording check to Craig Hockenberry's solution

This commit is contained in:
Michael Starke
2019-11-27 12:02:45 +01:00
parent 9b77790ddc
commit 09a7a15141

View File

@@ -63,19 +63,41 @@
- (BOOL)hasScreenRecordingPermissions:(NSError *__autoreleasing*)error {
/* macos 10.14 and lower do not require screen recording permission to get window titles */
if (@available(macOS 10.15, *)) {
/*
Solution is heavily inspired by Craig Hockenberry's
https://stackoverflow.com/questions/56597221/detecting-screen-recording-settings-on-macos-catalina/58985069#58985069
*/
if(@available(macOS 10.15, *)) {
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSUInteger numberOfWindows = CFArrayGetCount(windowList);
NSUInteger numberOfWindowsWithName = 0;
BOOL canRecordScreen = NO;
for(int idx = 0; idx < numberOfWindows; idx++) {
NSDictionary *windowInfo = (NSDictionary *)CFArrayGetValueAtIndex(windowList, idx);
NSNumber *ownerPid = windowInfo[(id)kCGWindowOwnerPID];
/*
Skip over our own windows
*/
if(ownerPid.intValue == NSProcessInfo.processInfo.processIdentifier) {
continue;
}
/*
Skip applications we aren't allowed to access anyway
*/
NSRunningApplication *ownerApp = [NSRunningApplication runningApplicationWithProcessIdentifier:ownerPid.intValue];
if(!ownerApp) {
continue;
}
NSString *windowName = windowInfo[(id)kCGWindowName];
if(windowName) {
numberOfWindowsWithName++;
if([ownerApp.executableURL.lastPathComponent isEqualToString:@"Dock"]) {
continue;
}
canRecordScreen = YES;
break;
}
}
CFRelease(windowList);
BOOL canRecordScreen = (numberOfWindows == 0) || (numberOfWindowsWithName > 0);
if(!canRecordScreen && error) {
*error = [NSError errorInDomain:MPAutotypeErrorDomain withCode:MPErrorAutotypeIsMissingScreenRecordingPermissions description:NSLocalizedString(@"ERROR_NO_PERMISSION_TO_RECORD_SCREEN", "Error description for missing screen recording permissions")];
}