Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356public import Foundation
public enum DriverCommand: Codable, Sendable, Equatable { case preflight case inputState case releaseOwnedKeys case recoverKey(pid: Int32, windowID: UInt32, virtualKey: UInt16) case register(pid: Int32, windowID: UInt32) case standardWindows(pid: Int32, candidates: [UInt32]) case key(virtualKey: UInt16, flags: UInt64, expectation: DriverKeyExpectation = .shortcut) case heldKeys( first: UInt16, second: UInt16, flags: UInt64, holdMilliseconds: UInt16, secondExpectation: DriverKeyExpectation = .shortcut) case minimize(pid: Int32, windowID: UInt32) case drag(pid: Int32, windowID: UInt32, fromX: Double, fromY: Double, toX: Double, toY: Double, flags: UInt64) case move(pid: Int32, windowID: UInt32, x: Double, y: Double, width: Double, height: Double) case focus(pid: Int32, windowID: UInt32) case close(pid: Int32, windowID: UInt32) case increments(pid: Int32, windowID: UInt32) case pressTourTidy(pid: Int32, windowID: UInt32, launchDate: Double, control: DriverTourControl = .tidy) case clickTourTidy(pid: Int32, windowID: UInt32, launchDate: Double) case captureTour(pid: Int32, windowID: UInt32, launchDate: Double, directory: String, optedIn: Bool) case capture(directory: String, optedIn: Bool = false) case captureDesktop(directory: String, optedIn: Bool) case missionControlChords case missionControlSnapshot case quit}
public struct DriverRequest: Codable, Sendable, Equatable { public let id: UInt64 public let command: DriverCommand
public init(id: UInt64, command: DriverCommand) { self.id = id self.command = command }
public func validate() throws { guard id != 0 else { throw DriverError("Request ID 0 is reserved for malformed envelopes") } try command.validate() }}
public struct DriverResponse: Codable, Sendable, Equatable { public let id: UInt64 public let error: String? public let preflight: DriverPreflight? public let increments: DriverIncrements? public let chords: [DriverChord]? public let captures: [DriverCapture]? public let frame: DriverFrame? public let nodes: [DriverAXNode]? public let identificationWarnings: [String]? public let dragFrames: [DriverFrame]? public let windowIDs: [UInt32]? public let identifiedWindowIDs: [UInt32]? public let input: [DriverInputReceipt]? public let inputState: DriverInputState? public let tourPress: DriverTourPressReceipt?
public init( id: UInt64, error: String? = nil, preflight: DriverPreflight? = nil, increments: DriverIncrements? = nil, chords: [DriverChord]? = nil, captures: [DriverCapture]? = nil, frame: DriverFrame? = nil, nodes: [DriverAXNode]? = nil, identificationWarnings: [String]? = nil, dragFrames: [DriverFrame]? = nil, windowIDs: [UInt32]? = nil, input: [DriverInputReceipt]? = nil, inputState: DriverInputState? = nil, tourPress: DriverTourPressReceipt? = nil, identifiedWindowIDs: [UInt32]? = nil ) { self.id = id self.error = error self.preflight = preflight self.increments = increments self.chords = chords self.captures = captures self.frame = frame self.nodes = nodes self.identificationWarnings = identificationWarnings self.dragFrames = dragFrames self.windowIDs = windowIDs self.identifiedWindowIDs = identifiedWindowIDs self.input = input self.inputState = inputState self.tourPress = tourPress }}
public enum DriverTourControl: String, Codable, Sendable, CaseIterable { case tidy = "tour.tidy" case `continue` = "tour.continue" case back = "tour.back" case `defer` = "tour.defer" case readyRetry = "tour.ready.retry" case shortcutsEdit = "tour.shortcuts.edit" case automatic = "tour.mode.automatic" case manual = "tour.mode.manual" case referenceDisclosure = "tour.reference.disclosure"
public var role: String { switch self { case .referenceDisclosure: "AXDisclosureTriangle" default: "AXButton" } }}
public enum DriverTourInputMethod: String, Codable, Sendable { case ax, mouse }
public struct DriverTourPressReceipt: Codable, Sendable, Equatable { public let pid: Int32 public let windowID: UInt32 public let launchDate: Double public let identifier: String public let role: String public let action: String public let enabled: Bool public let visitedNodes: Int public let method: DriverTourInputMethod public let clickFrame: DriverFrame?
public init( pid: Int32, windowID: UInt32, launchDate: Double, identifier: String, role: String, action: String, enabled: Bool, visitedNodes: Int, method: DriverTourInputMethod = .ax, clickFrame: DriverFrame? = nil ) { self.pid = pid self.windowID = windowID self.launchDate = launchDate self.identifier = identifier self.role = role self.action = action self.enabled = enabled self.visitedNodes = visitedNodes self.method = method self.clickFrame = clickFrame }
public func requireMatch( pid: Int32, windowID: UInt32, launchDate: Double, control: DriverTourControl = .tidy, method: DriverTourInputMethod = .ax ) throws { guard pid > 0, windowID > 0, launchDate.isFinite, launchDate > 0, self.pid == pid, self.windowID == windowID, self.launchDate == launchDate, identifier == control.rawValue, role == control.role, self.method == method, enabled, (1...512).contains(visitedNodes) else { throw DriverError("Safety stop: external Tidy AX press receipt is incomplete or changed identity") } switch method { case .ax: guard action == "AXPress", clickFrame == nil else { throw DriverError("Safety stop: expected an AX press, not mouse delivery") } case .mouse: guard control == .tidy, action == "leftMouseDown/leftMouseUp", let clickFrame, [clickFrame.x, clickFrame.y, clickFrame.width, clickFrame.height].allSatisfy(\.isFinite), clickFrame.width > 0, clickFrame.height > 0 else { throw DriverError("Safety stop: mouse Tidy receipt lacks its verified button frame") } } }}
public struct DriverPreflight: Codable, Sendable, Equatable { public let axTrusted: Bool public let postEventAccess: Bool public let screenRecording: Bool public let processID: Int32? public let parentProcessID: Int32? public let protocolVersion: Int?
public init( axTrusted: Bool, postEventAccess: Bool, screenRecording: Bool, processID: Int32? = nil, parentProcessID: Int32? = nil, protocolVersion: Int? = DriverWire.protocolVersion ) { self.axTrusted = axTrusted self.postEventAccess = postEventAccess self.screenRecording = screenRecording self.processID = processID self.parentProcessID = parentProcessID self.protocolVersion = protocolVersion }}
public struct DriverIncrements: Codable, Sendable, Equatable { public let width: Double public let height: Double public let source: String
public init(width: Double, height: Double, source: String) { self.width = width self.height = height self.source = source }}
public struct DriverChord: Codable, Sendable, Equatable { public let symbolicID: Int public let virtualKey: UInt16 public let flags: UInt64 public let enabled: Bool
public init(symbolicID: Int, virtualKey: UInt16, flags: UInt64, enabled: Bool) { self.symbolicID = symbolicID self.virtualKey = virtualKey self.flags = flags self.enabled = enabled }}
public struct DriverFrame: Codable, Sendable, Equatable { public let x: Double public let y: Double public let width: Double public let height: Double
public init(x: Double, y: Double, width: Double, height: Double) { self.x = x self.y = y self.width = width self.height = height }}
public struct DriverCapture: Codable, Sendable, Equatable { public let path: String public let displayID: UInt32 public let frame: DriverFrame public let pixelWidth: Int public let pixelHeight: Int
public init(path: String, displayID: UInt32, frame: DriverFrame, pixelWidth: Int, pixelHeight: Int) { self.path = path self.displayID = displayID self.frame = frame self.pixelWidth = pixelWidth self.pixelHeight = pixelHeight }}
public struct DriverError: Error, Codable, Sendable, Equatable, CustomStringConvertible { public let description: String
public init(_ description: String) { self.description = description }}
extension DriverCommand { public static let allowedFlags: UInt64 = 0x009F_0000 public static let geometryLimit = 1_000_000.0
public func validate() throws { switch self { case .register(let pid, let id), .focus(let pid, let id), .close(let pid, let id), .increments(let pid, let id), .minimize(let pid, let id): try Self.validateIdentity(pid: pid, id: id) case .pressTourTidy(let pid, let id, let launchDate, _), .clickTourTidy(let pid, let id, let launchDate): try Self.validateIdentity(pid: pid, id: id) try Self.validateTourLaunch(launchDate) case .captureTour(let pid, let id, let launchDate, let directory, let optedIn): try Self.validateIdentity(pid: pid, id: id) try Self.validateTourLaunch(launchDate) try Self.capture(directory: directory, optedIn: optedIn).validate() case .recoverKey(let pid, let id, let key): try Self.validateIdentity(pid: pid, id: id) try Self.validateKey(key, flags: 0) case .standardWindows(let pid, let candidates): guard !candidates.isEmpty, candidates.count <= 64, Set(candidates).count == candidates.count else { throw DriverError("Standard window candidates require 1–64 unique CG IDs") } for id in candidates { try Self.validateIdentity(pid: pid, id: id) } case .move(let pid, let id, let x, let y, let width, let height): try Self.validateIdentity(pid: pid, id: id) try Self.validateCoordinates([x, y, width, height, x + width, y + height]) guard width >= 1, height >= 1 else { throw DriverError("Window dimensions must be at least one point") } case .drag(let pid, let windowID, let fromX, let fromY, let toX, let toY, let flags): try Self.validateIdentity(pid: pid, id: windowID) try Self.validateCoordinates([fromX, fromY, toX, toY]) try Self.validateFlags(flags) case .key(let key, let flags, _): try Self.validateKey(key, flags: flags) try Self.validateFlags(flags) case .heldKeys(let first, let second, let flags, let holdMilliseconds, _): try Self.validateKey(first, flags: flags) try Self.validateKey(second, flags: flags) guard (500...3000).contains(holdMilliseconds) else { throw DriverError("Held-key interval must be between 500 and 3000 milliseconds") } try Self.validateFlags(flags) case .capture(let directory, let optedIn), .captureDesktop(let directory, let optedIn): guard optedIn else { throw DriverError("Screen capture requires explicit opt-in") } guard directory.hasPrefix("/"), !directory.utf8.contains(0), directory.utf8.count <= 4096 else { throw DriverError("Capture directory must be an absolute path without NUL, at most 4096 bytes") } case .preflight, .inputState, .releaseOwnedKeys, .missionControlChords, .missionControlSnapshot, .quit: break } }
private static func validateTourLaunch(_ launchDate: Double) throws { guard launchDate.isFinite, launchDate > 0 else { throw DriverError("Tour operation requires the exact owned Parcel launch timestamp") } }
private static func validateIdentity(pid: Int32, id: UInt32) throws { guard pid > 0, id > 0 else { throw DriverError("Window identity requires a positive PID and CGWindowID") } }
private static func validateCoordinates(_ values: [Double]) throws { guard values.allSatisfy({ $0.isFinite && abs($0) <= geometryLimit }) else { throw DriverError("Geometry must be finite and within one million points of the display origin") } }
private static func validateKey(_ key: UInt16, flags: UInt64) throws { guard key <= 127 else { throw DriverError("Virtual key must be between 0 and 127") } guard !DriverModifierSequence.modifierKeys.contains(key), key != 57 else { throw DriverError("Modifier keys cannot be injected as ordinary keys") } guard !(key == 35 && flags & 0x0010_0000 != 0), !(key == 96 && flags & 0x0010_0000 != 0) else { throw DriverError("Global print and VoiceOver toggle chords are forbidden in the harness") } }
private static func validateFlags(_ flags: UInt64) throws { guard flags & ~allowedFlags == 0 else { throw DriverError("Unsupported event modifier bits: \(flags)") } }}
public enum DriverWire { public static let protocolVersion = 6
public static func requireProtocolVersion(_ version: Int?) throws { guard version == protocolVersion else { throw DriverError( "Safety stop: outdated reality driver protocol; rebuild the signed reality bundle before input") } }
public static func decodeRequest(_ data: Data) throws -> DriverRequest { let request = try JSONDecoder().decode(DriverRequest.self, from: data) try request.validate() return request }
private struct Envelope: Decodable { let id: UInt64 }
public static func failure(for data: Data, error: some Error) -> DriverResponse { let id: UInt64 do { id = try JSONDecoder().decode(Envelope.self, from: data).id } catch { id = 0 } return DriverResponse(id: id, error: String(describing: error)) }
public static func encodeResponse(_ response: DriverResponse) throws -> Data { var data = try JSONEncoder().encode(response) data.append(10) return data }}