Peekaboo/swift-cli/Sources/peekaboo/PermissionsChecker.swift
Peter Steinberger 2619f2a916 Add swift CLI
2025-05-23 05:40:27 +02:00

47 lines
No EOL
1.5 KiB
Swift

import Foundation
import CoreGraphics
import AVFoundation
class PermissionsChecker {
static func checkScreenRecordingPermission() -> Bool {
// Check if we can capture screen content by trying to get display bounds
var displayCount: UInt32 = 0
let result = CGGetActiveDisplayList(0, nil, &displayCount)
if result != .success || displayCount == 0 {
return false
}
// Try to capture a small image to test permissions
guard let mainDisplayID = CGMainDisplayID() as CGDirectDisplayID? else {
return false
}
// Test by trying to get display bounds - this requires screen recording permission
let bounds = CGDisplayBounds(mainDisplayID)
return bounds != CGRect.zero
}
static func checkAccessibilityPermission() -> Bool {
// Check if we have accessibility permission
let options = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: false]
return AXIsProcessTrustedWithOptions(options as CFDictionary)
}
static func requireScreenRecordingPermission() throws {
if !checkScreenRecordingPermission() {
throw CaptureError.capturePermissionDenied
}
}
static func requireAccessibilityPermission() throws {
if !checkAccessibilityPermission() {
throw CaptureError.capturePermissionDenied
}
}
}
enum PermissionError: Error {
case screenRecordingDenied
case accessibilityDenied
}