import CoreGraphics import Foundation import ParcelConfig enum RealityGeometry { // These are explicit lane stimuli, independent of the product's defaults. static let defaultGaps = LayoutGaps(inner: 12, outer: 16) } struct RealityError: Error, CustomStringConvertible, Sendable { let description: String init(_ description: String) { self.description = description } } struct ScreenRect: Codable, Equatable, Sendable, CustomStringConvertible { var x: Double var y: Double var width: Double var height: Double init(_ rect: CGRect) { x = rect.minX y = rect.minY width = rect.width height = rect.height } init(x: Double, y: Double, width: Double, height: Double) { self.x = x self.y = y self.width = width self.height = height } var maxX: Double { x + width } var maxY: Double { y + height } var area: Double { width * height } var description: String { "(\(x), \(y), \(width), \(height))" } var isValid: Bool { [x, y, width, height].allSatisfy(\.isFinite) && width > 0 && height > 0 } func inset(_ gap: Double) -> Self { Self(x: x + gap, y: y + gap, width: width - 2 * gap, height: height - 2 * gap) } func matches(_ other: Self, tolerance: Double = 3) -> Bool { abs(x - other.x) <= tolerance && abs(y - other.y) <= tolerance && abs(width - other.width) <= tolerance && abs(height - other.height) <= tolerance } func contains(_ other: Self, tolerance: Double = 3) -> Bool { other.x >= x - tolerance && other.y >= y - tolerance && other.maxX <= maxX + tolerance && other.maxY <= maxY + tolerance } func separated(from other: Self, gap: Double, tolerance: Double = 3) -> Bool { maxX + gap <= other.x + tolerance || other.maxX + gap <= x + tolerance || maxY + gap <= other.y + tolerance || other.maxY + gap <= y + tolerance } } struct RealWindow: Codable, Equatable, Sendable { let id: UInt32 let pid: Int32 let frame: ScreenRect let layer: Int let onscreen: Bool let alpha: Double } private struct WindowServerRow: Decodable { struct Bounds: Decodable { let X: Double let Y: Double let Width: Double let Height: Double } let kCGWindowNumber: UInt32 let kCGWindowOwnerPID: Int32 let kCGWindowBounds: Bounds let kCGWindowLayer: Int let kCGWindowIsOnscreen: Bool? let kCGWindowAlpha: Double? var window: RealWindow { RealWindow( id: kCGWindowNumber, pid: kCGWindowOwnerPID, frame: ScreenRect( x: kCGWindowBounds.X, y: kCGWindowBounds.Y, width: kCGWindowBounds.Width, height: kCGWindowBounds.Height), layer: kCGWindowLayer, onscreen: kCGWindowIsOnscreen ?? false, alpha: kCGWindowAlpha ?? 1) } } enum FrameOracle { static func decode(_ data: Data) throws -> [RealWindow] { try JSONDecoder().decode([WindowServerRow].self, from: data).map(\.window) .filter { $0.layer == 0 && $0.frame.isValid } } static func read(allLayers: Bool = false, onScreenOnly: Bool = false) throws -> [RealWindow] { let options: CGWindowListOption = onScreenOnly ? [.optionOnScreenOnly, .excludeDesktopElements] : .optionAll guard let rows = CGWindowListCopyWindowInfo(options, kCGNullWindowID) else { throw RealityError("Window server did not return a window list") } var windows: [RealWindow] = [] for entry in rows as NSArray { guard let row = entry as? NSDictionary, let layer = Int(exactly: try number(kCGWindowLayer as String, in: row)) else { throw RealityError("Window server returned invalid layer metadata") } if !allLayers && layer != 0 { continue } guard let id = UInt32(exactly: try number(kCGWindowNumber as String, in: row)), let pid = Int32(exactly: try number(kCGWindowOwnerPID as String, in: row)), let bounds = row[kCGWindowBounds] as? NSDictionary else { throw RealityError("Window server returned invalid window metadata") } let frame = try ScreenRect( x: number("X", in: bounds), y: number("Y", in: bounds), width: number("Width", in: bounds), height: number("Height", in: bounds)) guard frame.isValid else { continue } let onscreen: Bool if let value = row[kCGWindowIsOnscreen] { guard let flag = value as? Bool else { throw RealityError("Window server returned invalid onscreen metadata") } onscreen = flag } else { onscreen = false } let alpha = row[kCGWindowAlpha] == nil ? 1 : try number(kCGWindowAlpha as String, in: row) windows.append(RealWindow(id: id, pid: pid, frame: frame, layer: layer, onscreen: onscreen, alpha: alpha)) } return windows } private static func number(_ key: String, in row: NSDictionary) throws -> Double { guard let value = row[key] as? NSNumber else { throw RealityError("Window server is missing numeric \(key) metadata") } return value.doubleValue } static func protectedWindows(_ windows: [RealWindow], ownedIDs: Set, ownedPIDs: Set) -> [RealWindow] { windows.filter { $0.onscreen && !ownedIDs.contains($0.id) && !ownedPIDs.contains($0.pid) } } static func frames(_ windows: [RealWindow], ids: [UInt32]) throws -> [ScreenRect] { try ids.map { id in guard let window = windows.first(where: { $0.id == id }) else { throw RealityError("Owned window \(id) disappeared from the window server") } return window.frame } } static func tiles( _ frames: [ScreenRect], visible: ScreenRect, inner: Double = RealityGeometry.defaultGaps.inner, outer: Double = RealityGeometry.defaultGaps.outer ) -> Bool { guard !frames.isEmpty, frames.allSatisfy({ $0.isValid && visible.inset(outer).contains($0) }) else { return false } for (index, frame) in frames.enumerated() { if frames.dropFirst(index + 1).contains(where: { !frame.separated(from: $0, gap: inner) }) { return false } } let usable = visible.inset(outer) return frames.map(\.x).min().map { abs($0 - usable.x) <= 3 } == true && frames.map(\.y).min().map { abs($0 - usable.y) <= 3 } == true && frames.map(\.maxX).max().map { abs($0 - usable.maxX) <= 3 } == true && frames.map(\.maxY).max().map { abs($0 - usable.maxY) <= 3 } == true && frames.reduce(0) { $0 + $1.area } >= usable.area * 0.7 } static func stripeShape( _ frames: [ScreenRect], visible: ScreenRect, vertical: Bool, inner: Double = RealityGeometry.defaultGaps.inner, outer: Double = RealityGeometry.defaultGaps.outer ) -> Bool { guard tiles(frames, visible: visible, inner: inner, outer: outer) else { return false } let usable = visible.inset(outer) return frames.allSatisfy { vertical ? abs($0.y - usable.y) <= 3 && abs($0.height - usable.height) <= 3 : abs($0.x - usable.x) <= 3 && abs($0.width - usable.width) <= 3 } } static func stripes( count: Int, visible: ScreenRect, vertical: Bool, ratio: Double? = nil, gaps: LayoutGaps = RealityGeometry.defaultGaps ) -> [ScreenRect] { precondition(count > 0) let rect = visible.inset(gaps.outer) let length = (vertical ? rect.width : rect.height) - Double(count - 1) * gaps.inner let sizes: [Double] = if let ratio, count == 2 { [length * ratio, length * (1 - ratio)] } else { Array(repeating: length / Double(count), count: count) } var position = vertical ? rect.x : rect.y return sizes.map { size in defer { position += size + gaps.inner } return ScreenRect( x: vertical ? position : rect.x, y: vertical ? rect.y : position, width: vertical ? size : rect.width, height: vertical ? rect.height : size) } } static func balancedColumns(count: Int, visible: ScreenRect) -> [ScreenRect] { stripes(count: count, visible: visible, vertical: true).map { frame in let x = frame.x.rounded() let y = frame.y.rounded() return ScreenRect( x: x, y: y, width: frame.maxX.rounded() - x, height: frame.maxY.rounded() - y) } } static func matchesUnordered(_ real: [ScreenRect], _ expected: [ScreenRect]) -> Bool { guard real.count == expected.count else { return false } var remaining = expected for frame in real { guard let index = remaining.firstIndex(where: { frame.matches($0) }) else { return false } remaining.remove(at: index) } return remaining.isEmpty } } enum CloseWindowOracle { static func expanded( _ rows: [RealWindow], closed: OwnedWindow, survivors: [OwnedWindow], before: [ScreenRect] ) throws -> Bool { let ids = survivors.map(\.id) guard !survivors.isEmpty, survivors.count == before.count, Set(ids).count == ids.count, !ids.contains(closed.id), before.allSatisfy(\.isValid) else { throw RealityError("Close oracle requires distinct surviving IDs and their original frames") } for window in [closed] + survivors { let matches = rows.filter { $0.id == window.id } guard matches.count <= 1, matches.allSatisfy({ $0.pid == window.pid }) else { throw RealityError("Safety stop: close oracle CG ID \(window.id) changed ownership or is ambiguous") } } guard !rows.contains(where: { $0.id == closed.id }), survivors.allSatisfy({ survivor in rows.contains { $0.id == survivor.id && $0.pid == survivor.pid } }) else { return false } let frames = try FrameOracle.frames(rows, ids: ids) return frames.reduce(0) { $0 + $1.area } > before.reduce(0) { $0 + $1.area } } } struct Observation: Codable, Sendable { let elapsedSeconds: Double let windows: [RealWindow] } @MainActor func pollRealFrames( timeout: Duration = .seconds(4), samples: inout [Observation], predicate: @MainActor ([RealWindow]) async throws -> Bool ) async throws -> [RealWindow] { let clock = ContinuousClock() let start = clock.now var stable = 0 repeat { let windows = try FrameOracle.read() let elapsed = start.duration(to: clock.now).components samples.append( Observation(elapsedSeconds: Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18, windows: windows)) stable = try await predicate(windows) ? stable + 1 : 0 if stable >= 3 { return windows } try await Task.sleep(for: .milliseconds(40)) } while start.duration(to: clock.now) < timeout throw RealityError("Real window frames did not meet the expectation within \(timeout)") }