Skip to content

Commit

Permalink
fix: more robust screen-recording permission detection
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Jan 5, 2025
1 parent 2855b1b commit d85ec7b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/logic/SystemPermissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class SystemPermissions {

private static func detectScreenRecordingIsGranted() -> PermissionStatus {
if #available(macOS 10.15, *) {
return screenRecordingIsGranted_() ? .granted :
return screenRecordingIsGrantedOnSomeDisplay() ? .granted :
(Preferences.screenRecordingPermissionSkipped ? .skipped : .notGranted)
}
return .granted
Expand Down Expand Up @@ -117,9 +117,26 @@ class SystemPermissions {
// workaround: public API CGPreflightScreenCaptureAccess and private API SLSRequestScreenCaptureAccess exist, but
// their return value is not updated during the app lifetime
// note: shows the system prompt if there's no permission
private static func screenRecordingIsGranted_() -> Bool {
private static func screenRecordingIsGrantedOnSomeDisplay() -> Bool {
let mainDisplayID = CGMainDisplayID()
if screenRecordingIsGrantedOnDisplay(mainDisplayID) {
return true
}
// maybe the main screen can't produce a CGDisplayStream, but another screen can
// a positive on any screen must mean that the permission is granted; we try on the other screens
for screen in NSScreen.screens {
if let id = screen.number(), id != mainDisplayID {
if screenRecordingIsGrantedOnDisplay(id) {
return true
}
}
}
return false
}

private static func screenRecordingIsGrantedOnDisplay(_ displayId: CGDirectDisplayID) -> Bool {
return CGDisplayStream(
dispatchQueueDisplay: CGMainDisplayID(),
dispatchQueueDisplay: displayId,
outputWidth: 1,
outputHeight: 1,
pixelFormat: Int32(kCVPixelFormatType_32BGRA),
Expand Down

0 comments on commit d85ec7b

Please sign in to comment.