import CoreGraphics import Foundation import ImageIO import RealityDriverSupport enum DriverDesktopCapture { static func capture(directory: String, optedIn: Bool) throws -> [DriverCapture] { try DriverCommand.captureDesktop(directory: directory, optedIn: optedIn).validate() try DriverConsoleState.requireCurrent() guard CGPreflightScreenCaptureAccess() else { throw DriverError("Full-desktop capture requires an existing Screen Recording grant for Parcel") } let destination = URL(fileURLWithPath: directory) guard destination.resolvingSymlinksInPath().path == destination.standardizedFileURL.path, try FileManager.default.contentsOfDirectory(atPath: directory).isEmpty else { throw DriverError("Desktop screenshots require an empty, unredirected evidence directory") } var displayCount: UInt32 = 0 guard CGGetActiveDisplayList(0, nil, &displayCount) == .success, displayCount > 0 else { throw DriverError("Cannot enumerate displays for full-desktop capture") } let paths = (1...displayCount).map { destination.appendingPathComponent("display-\($0).png").path } let process = Process() process.executableURL = URL(fileURLWithPath: "/usr/sbin/screencapture") process.arguments = ["-x", "-t", "png"] + paths process.standardOutput = FileHandle.nullDevice process.standardError = FileHandle.nullDevice try process.run() let deadline = ContinuousClock.now.advanced(by: .seconds(10)) while process.isRunning, ContinuousClock.now < deadline { Thread.sleep(forTimeInterval: 0.025) } if process.isRunning { process.terminate() let stopDeadline = ContinuousClock.now.advanced(by: .seconds(1)) while process.isRunning, ContinuousClock.now < stopDeadline { Thread.sleep(forTimeInterval: 0.025) } if process.isRunning { kill(process.processIdentifier, SIGKILL) } let killedDeadline = ContinuousClock.now.advanced(by: .seconds(2)) while process.isRunning, ContinuousClock.now < killedDeadline { Thread.sleep(forTimeInterval: 0.025) } guard !process.isRunning else { throw DriverError("screencapture child still running: \(process.processIdentifier)") } throw DriverError("Full-desktop screencapture timed out") } guard process.terminationStatus == 0 else { throw DriverError("Full-desktop screencapture failed") } let files = try FileManager.default.contentsOfDirectory(at: destination, includingPropertiesForKeys: nil) .filter { $0.pathExtension == "png" }.sorted { $0.lastPathComponent < $1.lastPathComponent } guard files.count == Int(displayCount) else { throw DriverError("screencapture did not produce one image per active display") } return try files.map { file in guard let source = CGImageSourceCreateWithURL(file as CFURL, nil), let image = CGImageSourceCreateImageAtIndex(source, 0, nil), image.width > 0, image.height > 0 else { throw DriverError("screencapture produced an unreadable PNG") } // screencapture chooses display ordering; zero avoids inventing a display identity. return DriverCapture( path: file.path, displayID: 0, frame: DriverFrame(x: 0, y: 0, width: Double(image.width), height: Double(image.height)), pixelWidth: image.width, pixelHeight: image.height) } } }