Skip to content

Commit

Permalink
fix: more robust screen-recording permission check
Browse files Browse the repository at this point in the history
  • Loading branch information
louis.pontoise authored and lwouis committed Mar 10, 2020
1 parent 34a32f3 commit ce574a2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 6 additions & 0 deletions alt-tab-macos/api-wrappers/PrivateApis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ func CGSCopySpacesForWindows(_ cid: CGSConnectionID, _ mask: CGSSpaceMask.RawVal
@_silgen_name("CGSGetWindowLevel") @discardableResult
func CGSGetWindowLevel(_ cid: CGSConnectionID, _ wid: CGWindowID, _ level: inout CGWindowLevel) -> CGError

// returns status of the checkbox in System Preferences > Security & Privacy > Privacy > Screen Recording
// returns 1 if checked or 0 if unchecked; also prompts the user the first time if unchecked
@_silgen_name("SLSRequestScreenCaptureAccess") @discardableResult
func SLSRequestScreenCaptureAccess() -> UInt8




// ------------------------------------------------------------
Expand Down
4 changes: 1 addition & 3 deletions alt-tab-macos/logic/SystemPermissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ class SystemPermissions {

static func ensureScreenRecordingCheckboxIsChecked() {
guard #available(OSX 10.15, *) else { return }
// there is no API to check this permission; we try to get a screenshot to check indirectly
let firstWindow = CGWindow.windows(.optionOnScreenOnly)[0]
if let cgId = firstWindow.id(), cgId.screenshot() == nil {
if SLSRequestScreenCaptureAccess() != 1 {
debugPrint("Before using this app, you need to give permission in System Preferences > Security & Privacy > Privacy > Screen Recording.",
"Please authorize and re-launch.",
"See https://dropshare.zendesk.com/hc/en-us/articles/360033453434-Enabling-Screen-Recording-Permission-on-macOS-Catalina-10-15-",
Expand Down
2 changes: 1 addition & 1 deletion alt-tab-macos/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class App: NSApplication, NSApplicationDelegate, NSWindowDelegate {
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
SystemPermissions.ensureScreenRecordingCheckboxIsChecked()
SystemPermissions.ensureAccessibilityCheckboxIsChecked()
SystemPermissions.ensureScreenRecordingCheckboxIsChecked()
Preferences.loadFromDiskAndUpdateValues()
statusItem = StatusItem.make(self)
initPreferencesDependentComponents()
Expand Down

0 comments on commit ce574a2

Please sign in to comment.