import CoreGraphics import Foundation import ImageIO import RealityDriverSupport import ScreenCaptureKit private struct DriverScreenshot: Sendable { let png: Data let width: Int let height: Int let frame: DriverFrame } enum DriverScreenCapture { static func capture(directory: String, optedIn: Bool, windowID: UInt32? = nil) throws -> DriverCapture { guard optedIn else { throw DriverError("Screen capture is opt-in; set PARCEL_REALITY_SCREEN_CAPTURE=1") } guard CGPreflightScreenCaptureAccess() else { throw DriverError( "Opt-in capture requires an existing Screen Recording grant for the responsible Parcel host") } let displayID = CGMainDisplayID() let frame = CGDisplayBounds(displayID) let completion = DriverCompletion() SCShareableContent.getExcludingDesktopWindows(false, onScreenWindowsOnly: true) { content, error in if let error { completion.complete(.failure(DriverError("ScreenCaptureKit display discovery failed: \(error)"))) return } guard let display = content?.displays.first(where: { $0.displayID == displayID }) else { completion.complete(.failure(DriverError("ScreenCaptureKit did not expose the primary display"))) return } let filter: SCContentFilter let capturedFrame: CGRect let configuration = SCStreamConfiguration() if let windowID { guard let window = content?.windows.first(where: { $0.windowID == windowID }) else { completion.complete(.failure(DriverError("ScreenCaptureKit did not expose the owned tour window"))) return } capturedFrame = window.frame filter = SCContentFilter(desktopIndependentWindow: window) configuration.width = Int((window.frame.width * Double(filter.pointPixelScale)).rounded()) configuration.height = Int((window.frame.height * Double(filter.pointPixelScale)).rounded()) configuration.ignoreShadowsSingleWindow = true } else { capturedFrame = frame filter = SCContentFilter(display: display, excludingWindows: []) configuration.width = Int(CGDisplayPixelsWide(displayID)) configuration.height = Int(CGDisplayPixelsHigh(displayID)) } configuration.showsCursor = false configuration.captureResolution = .best SCScreenshotManager.captureImage(contentFilter: filter, configuration: configuration) { image, error in if let error { completion.complete(.failure(DriverError("ScreenCaptureKit screenshot failed: \(error)"))) return } guard let image else { completion.complete(.failure(DriverError("ScreenCaptureKit returned no image"))) return } guard let data = CFDataCreateMutable(nil, 0), let destination = CGImageDestinationCreateWithData(data, "public.png" as CFString, 1, nil) else { completion.complete(.failure(DriverError("Could not create PNG encoder"))) return } CGImageDestinationAddImage(destination, image, nil) guard CGImageDestinationFinalize(destination) else { completion.complete(.failure(DriverError("PNG encoding failed"))) return } completion.complete( .success( DriverScreenshot( png: data as Data, width: image.width, height: image.height, frame: DriverFrame( x: capturedFrame.minX, y: capturedFrame.minY, width: capturedFrame.width, height: capturedFrame.height)))) } } let screenshot = try completion.wait(seconds: 15, operation: "ScreenCaptureKit screenshot") let directoryURL = URL(fileURLWithPath: directory, isDirectory: true) var isDirectory = ObjCBool(false) guard FileManager.default.fileExists(atPath: directory, isDirectory: &isDirectory), isDirectory.boolValue else { throw DriverError("Capture directory must already exist: \(directory)") } let path = directoryURL.appendingPathComponent("screen.png") guard !FileManager.default.fileExists(atPath: path.path) else { throw DriverError("Refusing to overwrite an existing capture: \(path.path)") } try screenshot.png.write(to: path, options: [.atomic]) return DriverCapture( path: path.path, displayID: displayID, frame: screenshot.frame, pixelWidth: screenshot.width, pixelHeight: screenshot.height) } }