Skip to content

Commit

Permalink
fix: app would sometimes quit while in the background (closes #704)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Jan 25, 2021
1 parent 56d47fc commit d621ce5
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions src/logic/SystemPermissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,19 @@ class SystemPermissions {
return true
}

// there is no official API to check the status of the Screen Recording permission
// there is the private API SLSRequestScreenCaptureAccess, but its value is not updated during the app lifetime
// workaround: we check if we can get the title of at least one window, except from AltTab or the Dock
// 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 {
let appPid = NSRunningApplication.current.processIdentifier
if let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as? [CGWindow],
let _ = windows.first(where: { (window) -> Bool in
if let windowPid = window.ownerPID(),
windowPid != appPid,
let windowRunningApplication = NSRunningApplication(processIdentifier: windowPid),
windowRunningApplication.executableURL?.lastPathComponent != "Dock",
let _ = window.title() {
return true
}
return false
}) {
return true
}
return false
return CGDisplayStream(
dispatchQueueDisplay: CGMainDisplayID(),
outputWidth: 1,
outputHeight: 1,
pixelFormat: Int32(kCVPixelFormatType_32BGRA),
properties: nil,
queue: .global(),
handler: { _, _, _, _ in }
) != nil
}

static func observePermissionsPostStartup() {
Expand Down

0 comments on commit d621ce5

Please sign in to comment.