Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394import AppKitimport CoreGraphicsimport Dispatchimport Foundationimport OSLogimport ParcelCoreimport ParcelPlatform
struct AppFixtureMouseRequest: Sendable { enum Action: String, Sendable { case down, drag, up, escape, restore } enum Button: String, Sendable { case left, right } let action: Action let button: Button let window: UInt32? let point: CGPoint? let flags: CGEventFlags let clickCount: Int64
init(arguments: [String], environment: [String: String]) throws { guard environment["PARCEL_E2E"] == "1" else { throw AppFixtureDrag.rejected("Synthetic input is only available to the fixture-only e2e app") } guard arguments.allSatisfy({ $0.utf8.count <= 32 }), let name = arguments.first, let action = Action(rawValue: name) else { throw AppFixtureDrag.rejected("Unknown fixture mouse action") } var window: UInt32? var point: CGPoint? var flags: CGEventFlags = [] var clickCount: Int64 = 1 var button = Button.left switch action { case .down: guard (6...7).contains(arguments.count), let selected = UInt32(arguments[1]), selected > 0 else { throw AppFixtureDrag.rejected( "Mouse-down needs a positive fixture window ID, point, modifiers and click count") } window = selected point = try Self.point(x: arguments[2], y: arguments[3]) flags = try Self.flags(arguments[4]) clickCount = try Self.clickCount(arguments[5]) if arguments.count == 7 { guard let chosen = Button(rawValue: arguments[6]) else { throw AppFixtureDrag.rejected("Fixture input accepts only left or right mouse buttons") } button = chosen } case .drag: guard arguments.count == 4 else { throw AppFixtureDrag.rejected("A fixture drag needs a point and modifiers") } point = try Self.point(x: arguments[1], y: arguments[2]) flags = try Self.flags(arguments[3]) case .up: guard arguments.count == 3 else { throw AppFixtureDrag.rejected("Mouse-up needs modifiers and a click count") } flags = try Self.flags(arguments[1]) clickCount = try Self.clickCount(arguments[2]) case .escape, .restore: guard arguments.count == 1 else { throw AppFixtureDrag.rejected("This fixture action takes no arguments") } } self.button = button self.action = action self.window = window self.point = point self.flags = flags self.clickCount = clickCount }
private static func point(x: String, y: String) throws -> CGPoint { guard let x = Double(x), let y = Double(y), x.isFinite, y.isFinite, abs(x) <= 100_000, abs(y) <= 100_000 else { throw AppFixtureDrag.rejected("Fixture input needs a finite, bounded point") } return CGPoint(x: x, y: y) }
private static func flags(_ value: String) throws -> CGEventFlags { let supported: CGEventFlags = [.maskCommand, .maskAlternate, .maskControl, .maskShift] guard let raw = UInt64(value), raw & ~supported.rawValue == 0 else { throw AppFixtureDrag.rejected("Fixture input only accepts Command, Option, Control and Shift") } return CGEventFlags(rawValue: raw) }
private static func clickCount(_ value: String) throws -> Int64 { guard let count = Int64(value), (1...2).contains(count) else { throw AppFixtureDrag.rejected("Fixture input accepts only single or double clicks") } return count }}
struct AppFixtureMouseGeometry: Sendable { let window: UInt32 let pid: Int32 let bounds: CGRect let regions: [CGRect] let point: CGPoint
init(window: UInt32, point: CGPoint, state: State) throws { let id = WindowID(rawValue: window) guard let selected = state.windows[id], selected.app.pid > 0, let key = state.workspace(containing: id), state.isActive(key), let workspace = state.workspaces[key], let frame = state.frames[id] else { throw AppFixtureDrag.rejected("Mouse input needs a managed window in the active fixture workspace") } for other in workspace.order { guard let managed = state.windows[other], managed.app.pid == selected.app.pid, !(managed.isFloating && workspace.tree?.windows.contains(other) == true), let frame = state.frames[other], Self.valid(frame) else { throw AppFixtureDrag.rejected("Mouse input must own every window in its fixture workspace") } } let regions = try state.displays.keys.sorted().compactMap { display -> CGRect? in guard let space = state.activeSpaces[display], let display = state.displays[display], display.isValid else { return nil } let screen = display.physicalFrame ?? display.frame let destination = WorkspaceKey(display: display.id, space: space) guard state.isActive(destination), Self.valid(screen) else { return nil } for other in state.workspaces[destination]?.order ?? [] { guard state.windows[other]?.app.pid == selected.app.pid else { throw AppFixtureDrag.rejected("Fixture input cannot enter a display with unrelated managed windows") } } return CGRect(x: screen.minX, y: screen.minY, width: screen.size.width, height: screen.size.height) } let bounds = regions.reduce(CGRect.null) { $0.union($1) } let selectedFrame = CGRect(x: frame.minX, y: frame.minY, width: frame.size.width, height: frame.size.height) guard Self.valid(frame), regions.contains(where: { $0.contains(point) }), selectedFrame.insetBy(dx: -16, dy: -16).contains(point) else { throw AppFixtureDrag.rejected("Mouse-down must land on the fixture tile or its shared edge") } self.window = window pid = selected.app.pid self.bounds = bounds self.regions = regions self.point = point }
func validate(_ point: CGPoint) throws { guard point.x.isFinite, point.y.isFinite, regions.contains(where: { $0.contains(point) }) else { throw AppFixtureDrag.rejected("Fixture input cannot leave its known active display bounds") } }
private static func valid(_ frame: Rect) -> Bool { [frame.minX, frame.minY, frame.maxX, frame.maxY].allSatisfy { $0.isFinite && abs($0) <= 100_000 } && frame.size.width >= 100 && frame.size.height >= 24 }}
@MainActorfinal class AppFixtureMouseInput { struct Operations { var requireFixture: (Int32) throws -> Void var windowAt: @MainActor (CGPoint) throws -> DragWindow? var post: (CGEvent) -> Void var restoreCursor: (CGPoint) -> CGError
static var system: Self { Self( requireFixture: { pid in guard let app = NSRunningApplication(processIdentifier: pid), !app.isTerminated, app.executableURL?.lastPathComponent == "FixtureApp", NSWorkspace.shared.frontmostApplication?.processIdentifier == pid else { throw AppFixtureDrag.rejected("Synthetic input must target the frontmost FixtureApp") } }, windowAt: DragWindowLookup.window(at:), post: AppFixtureEventPosting.post, restoreCursor: CGWarpMouseCursorPosition) } }
private let operations: Operations private let sampleDelay: Duration private let sampleSleep: @MainActor (Duration) async throws -> Void private let idleTimeout: Duration private let idleSleep: @MainActor (Duration) async throws -> Void private var isRestored = false private let source: CGEventSource private let originalCursor: CGPoint private let release: CGEvent private let rightRelease: CGEvent private var button = AppFixtureMouseRequest.Button.left private let escapeRelease: CGEvent private var geometry: AppFixtureMouseGeometry? private var location: CGPoint private var escapeIsDown = false private var expiry: Task<Void, Never>? private let logger = Logger(subsystem: "dev.vinnymac.parcel", category: "fixture-input")
convenience init() throws { guard ProcessInfo.processInfo.environment["PARCEL_E2E"] == "1" else { throw AppFixtureDrag.rejected("Synthetic input is only available to the fixture-only e2e app") } guard CGPreflightPostEventAccess() else { throw AppFixtureDrag.rejected("The harness-launched Parcel app lacks synthetic event-posting access") } guard !CGEventSource.buttonState(.combinedSessionState, button: .left), !CGEventSource.buttonState(.combinedSessionState, button: .right), !CGEventSource.buttonState(.combinedSessionState, button: .center), !CGEventSource.keyState(.combinedSessionState, key: 53), CGEventSource.flagsState(.combinedSessionState) .intersection([.maskCommand, .maskAlternate, .maskControl, .maskShift, .maskSecondaryFn]).isEmpty else { throw AppFixtureDrag.rejected("Release mouse buttons, Escape and modifier keys before fixture input") } guard let cursor = CGEvent(source: nil) else { throw AppFixtureDrag.rejected("Could not read the fixture-input cursor") } try self.init(operations: .system, cursor: cursor.location) }
init( operations: Operations, cursor: CGPoint, sampleDelay: Duration = .milliseconds(40), sampleSleep: @escaping @MainActor (Duration) async throws -> Void = { try await Task.sleep(for: $0) }, idleTimeout: Duration = .seconds(30), idleSleep: @escaping @MainActor (Duration) async throws -> Void = { try await Task.sleep(for: $0) } ) throws { guard sampleDelay >= .zero, idleTimeout > .zero, let source = CGEventSource(stateID: .privateState), let release = CGEvent( mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: cursor, mouseButton: .left), let rightRelease = CGEvent( mouseEventSource: source, mouseType: .rightMouseUp, mouseCursorPosition: cursor, mouseButton: .right), let escapeRelease = CGEvent(keyboardEventSource: source, virtualKey: 53, keyDown: false) else { throw AppFixtureDrag.rejected("Could not create fixture input and cleanup events") } self.operations = operations self.sampleDelay = sampleDelay self.sampleSleep = sampleSleep self.idleTimeout = idleTimeout self.idleSleep = idleSleep self.source = source originalCursor = cursor location = cursor self.release = release self.rightRelease = rightRelease self.escapeRelease = escapeRelease }
func post(_ request: AppFixtureMouseRequest, state: State) async throws { do { try await perform(request, state: state) } catch { let failure = error do { try restore() } catch { throw AppFixtureDrag.rejected("Fixture cleanup failed: \(error); input error: \(failure)") } throw failure } }
private func perform(_ request: AppFixtureMouseRequest, state: State) async throws { guard !isRestored || request.action == .restore else { throw AppFixtureDrag.rejected("Fixture input has expired or was restored") } expiry?.cancel() switch request.action { case .down: guard geometry == nil, let window = request.window, let point = request.point else { throw AppFixtureDrag.rejected("Cannot begin fixture input with a held mouse button") } let geometry = try AppFixtureMouseGeometry(window: window, point: point, state: state) try operations.requireFixture(geometry.pid) if let hit = try operations.windowAt(point), hit.pid != geometry.pid { throw AppFixtureDrag.rejected("An unrelated window covers the fixture tile or shared edge") } let event = try Self.mouseEvent( request.button == .right ? .rightMouseDown : .leftMouseDown, source: source, geometry: geometry, point: point, previous: point, flags: request.flags, clickCount: request.clickCount) self.geometry = geometry button = request.button location = point operations.post(event) case .drag: let geometry = try activeGeometry() guard let destination = request.point else { throw AppFixtureDrag.rejected("Missing fixture drag point") } try geometry.validate(destination) let start = location for step in 1...8 { try operations.requireFixture(geometry.pid) let fraction = Double(step) / 8 let point = CGPoint( x: start.x + (destination.x - start.x) * fraction, y: start.y + (destination.y - start.y) * fraction) let event = try Self.mouseEvent( button == .right ? .rightMouseDragged : .leftMouseDragged, source: source, geometry: geometry, point: point, previous: location, flags: request.flags, clickCount: 1) location = point operations.post(event) try await sampleSleep(sampleDelay) } case .up: let geometry = try activeGeometry() try operations.requireFixture(geometry.pid) let event = try Self.mouseEvent( button == .right ? .rightMouseUp : .leftMouseUp, source: source, geometry: geometry, point: location, previous: location, flags: request.flags, clickCount: request.clickCount) operations.post(event) self.geometry = nil case .escape: let geometry = try activeGeometry() try operations.requireFixture(geometry.pid) guard let event = CGEvent(keyboardEventSource: source, virtualKey: 53, keyDown: true) else { throw AppFixtureDrag.rejected("Could not create Escape for fixture input") } event.flags = [] event.setIntegerValueField(.eventTargetUnixProcessID, value: Int64(geometry.pid)) escapeRelease.setIntegerValueField(.eventTargetUnixProcessID, value: Int64(geometry.pid)) escapeIsDown = true operations.post(event) try await sampleSleep(sampleDelay) operations.post(escapeRelease) escapeIsDown = false try await sampleSleep(sampleDelay) case .restore: try restore() return } let timeout = idleTimeout let sleep = idleSleep expiry = Task { @MainActor [weak self] in do { try await sleep(timeout) } catch is CancellationError { return } catch { self?.logger.error("Fixture input timer failed: \(error.localizedDescription, privacy: .public)") } guard let self else { return } do { try self.restore() } catch { self.logger.error("Fixture input cleanup failed: \(error.localizedDescription, privacy: .public)") } } }
func restore() throws { expiry?.cancel() expiry = nil guard !isRestored else { return } if escapeIsDown { operations.post(escapeRelease) escapeIsDown = false } if let geometry { let release = button == .right ? rightRelease : release release.location = location release.flags = [] release.setIntegerValueField(.eventTargetUnixProcessID, value: Int64(geometry.pid)) operations.post(release) self.geometry = nil } let result = operations.restoreCursor(originalCursor) guard result == .success else { throw AppFixtureDrag.rejected("Could not restore the fixture-input cursor: \(result)") } isRestored = true }
static func mouseEvent( _ type: CGEventType, source: CGEventSource, geometry: AppFixtureMouseGeometry, point: CGPoint, previous: CGPoint, flags: CGEventFlags, clickCount: Int64 ) throws -> CGEvent { try geometry.validate(point) let rightTypes: [CGEventType] = [.rightMouseDown, .rightMouseDragged, .rightMouseUp] let button: CGMouseButton = rightTypes.contains(type) ? .right : .left guard [previous.x, previous.y].allSatisfy({ $0.isFinite && abs($0) <= 100_000 }), ([.leftMouseDown, .leftMouseDragged, .leftMouseUp] + rightTypes).contains(type), let event = CGEvent( mouseEventSource: source, mouseType: type, mouseCursorPosition: point, mouseButton: button) else { throw AppFixtureDrag.rejected("Could not create a fixture mouse event") } event.flags = flags event.setIntegerValueField(.mouseEventClickState, value: clickCount) event.setIntegerValueField(.mouseEventNumber, value: 1) event.setIntegerValueField(.eventTargetUnixProcessID, value: Int64(geometry.pid)) event.setIntegerValueField(.mouseEventDeltaX, value: Int64((point.x - previous.x).rounded())) event.setIntegerValueField(.mouseEventDeltaY, value: Int64((point.y - previous.y).rounded())) event.setDoubleValueField(.mouseEventPressure, value: type == .leftMouseUp || type == .rightMouseUp ? 0 : 1) return event }
private func activeGeometry() throws -> AppFixtureMouseGeometry { guard let geometry else { throw AppFixtureDrag.rejected("Fixture input needs a mouse-down event") } return geometry }
isolated deinit { do { try restore() } catch { logger.error("Fixture input cleanup failed: \(error.localizedDescription, privacy: .public)") } }}
enum AppFixtureEventPosting { static func post(_ event: CGEvent) { event.timestamp = DispatchTime.now().uptimeNanoseconds // Posting from the granted app through HID includes the fixture's events in its global monitor. event.post(tap: .cghidEventTap) }}