// SPDX-License-Identifier: GPL-3.0-or-later // Native accessibility probe for the opt-in Debug --interface-validation session. // Compile: swiftc InterfaceUIProbe.swift -o /tmp/vcc-ui-probe // Usage: vcc-ui-probe PID inspect | press LABEL | menu MENU ITEM | // click LABEL | hover LABEL | close WINDOW | resize WINDOW WIDTH HEIGHT | // capture WINDOW OUTPUT.png | type BASIC_COMMAND | set-text LABEL INPUT_FILE import AppKit import ApplicationServices let arguments = Array(CommandLine.arguments.dropFirst()) guard arguments.count >= 2, let pid = Int32(arguments[0]), AXIsProcessTrusted() else { fputs("Supply the validation PID and action; macOS Accessibility access is required.\n", stderr); exit(2) } let app = AXUIElementCreateApplication(pid) func value(_ element: AXUIElement, _ key: String) -> Any? { var result: CFTypeRef? return AXUIElementCopyAttributeValue(element,key as CFString,&result) == .success ? result : nil } func text(_ element: AXUIElement, _ key: String) -> String { value(element,key) as? String ?? "" } func children(_ element: AXUIElement) -> [AXUIElement] { value(element,kAXChildrenAttribute) as? [AXUIElement] ?? [] } func descendants(_ element: AXUIElement, depth: Int = 0) -> [AXUIElement] { guard depth < 14 else { return [] } return [element] + children(element).flatMap { descendants($0,depth:depth+1) } } func fail(_ message: String) -> Never { fputs(message+"\n",stderr);exit(1) } func window(_ title: String) -> AXUIElement { guard let w=(value(app,kAXWindowsAttribute) as? [AXUIElement])?.first(where: { text($0,kAXTitleAttribute)==title }) else { fail("Window unavailable: \(title)") };return w } func press(_ e: AXUIElement) { guard AXUIElementPerformAction(e,kAXPressAction as CFString) == .success else { fail("Control did not accept Press") } } // Require the authored validation media to be visible somewhere in the app. // The UITesting app always installs an isolated fixture, including real-media // opt-in validation. Refuse an ordinary user's VCC instance accidentally. let all=descendants(app) guard NSRunningApplication(processIdentifier: pid)?.bundleIdentifier == "com.brianholman.vccplus.oss.mac.uitesting" || all.contains(where: { e in [kAXTitleAttribute,kAXDescriptionAttribute,kAXValueAttribute].contains { text(e,$0).contains("Authored") || text(e,$0) == "VCC Startup Validation" } }) else { fail("Refusing a non-validation session: authored fixture not found") } NSRunningApplication(processIdentifier:pid)?.activate() switch arguments[1] { case "inspect": for e in all { let role=text(e,kAXRoleAttribute),label=text(e,kAXDescriptionAttribute),title=text(e,kAXTitleAttribute) if !label.isEmpty || !title.isEmpty || role == "AXStaticText" || role == "AXPopUpButton" { print("\(role) | \(label.isEmpty ? title : label) | \(value(e,kAXValueAttribute) ?? "")") } } case "press": guard arguments.count==3, let e=all.first(where: { ["AXButton","AXCheckBox","AXRadioButton","AXMenuButton","AXDisclosureTriangle","AXMenuItem","AXLink","AXPopUpButton"].contains(text($0,kAXRoleAttribute)) && (text($0,kAXDescriptionAttribute)==arguments[2] || text($0,kAXTitleAttribute)==arguments[2] || text($0,kAXValueAttribute)==arguments[2]) }) else { fail("Control unavailable") };press(e) case "click", "hover": guard arguments.count == 3, let element = all.first(where: { text($0,kAXDescriptionAttribute) == arguments[2] || text($0,kAXTitleAttribute) == arguments[2] || text($0,kAXValueAttribute) == arguments[2] }) else { fail("Control unavailable") } var point = CGPoint.zero, size = CGSize.zero guard let p = value(element,kAXPositionAttribute), let s = value(element,kAXSizeAttribute), AXValueGetValue(unsafeBitCast(p as CFTypeRef,to:AXValue.self),.cgPoint,&point), AXValueGetValue(unsafeBitCast(s as CFTypeRef,to:AXValue.self),.cgSize,&size), size.width > 0, size.height > 0 else { fail("Visible control geometry unavailable") } point.x += size.width/2; point.y += size.height/2 CGWarpMouseCursorPosition(point) CGEvent(mouseEventSource:nil,mouseType:.mouseMoved,mouseCursorPosition:point,mouseButton:.left)?.postToPid(pid) if arguments[1] == "click" { CGEvent(mouseEventSource:nil,mouseType:.leftMouseDown,mouseCursorPosition:point,mouseButton:.left)?.postToPid(pid) CGEvent(mouseEventSource:nil,mouseType:.leftMouseUp,mouseCursorPosition:point,mouseButton:.left)?.postToPid(pid) } case "set-text": guard arguments.count == 4, let element = all.first(where: { ["AXTextArea", "AXTextField", "AXComboBox"].contains(text($0,kAXRoleAttribute)) && text($0,kAXDescriptionAttribute) == arguments[2] }) else { fail("Editable field unavailable") } let input = try String(contentsOfFile: arguments[3], encoding: .utf8) guard AXUIElementSetAttributeValue(element, kAXValueAttribute as CFString, input as CFString) == .success else { fail("Cannot set field text") } case "type": guard arguments.count == 3, let display = all.first(where: { text($0,kAXDescriptionAttribute) == "CoCo display" }) else { fail("CoCo display unavailable") } var point = CGPoint.zero, size = CGSize.zero guard let p = value(display,kAXPositionAttribute), let s = value(display,kAXSizeAttribute), AXValueGetValue(unsafeBitCast(p as CFTypeRef,to:AXValue.self),.cgPoint,&point), AXValueGetValue(unsafeBitCast(s as CFTypeRef,to:AXValue.self),.cgSize,&size) else { fail("Display geometry unavailable") } let center = CGPoint(x:point.x+size.width/2,y:point.y+size.height/2) CGEvent(mouseEventSource:nil,mouseType:.leftMouseDown,mouseCursorPosition:center,mouseButton:.left)?.post(tap:.cghidEventTap) CGEvent(mouseEventSource:nil,mouseType:.leftMouseUp,mouseCursorPosition:center,mouseButton:.left)?.post(tap:.cghidEventTap) Thread.sleep(forTimeInterval:0.15) for character in arguments[2] + "\r" { let code: CGKeyCode = character == "\r" ? 36 : 0 for down in [true,false] { let event = CGEvent(keyboardEventSource:nil,virtualKey:code,keyDown:down)! let utf16 = Array(String(character).utf16) utf16.withUnsafeBufferPointer { event.keyboardSetUnicodeString(stringLength:$0.count,unicodeString:$0.baseAddress!) } event.postToPid(pid);Thread.sleep(forTimeInterval:0.08) } } case "menu": guard arguments.count==4,let bar=value(app,kAXMenuBarAttribute) else { fail("Menu unavailable") } let root=unsafeBitCast(bar as CFTypeRef,to:AXUIElement.self) guard let menu=children(root).first(where:{text($0,kAXTitleAttribute)==arguments[2]}), let item=descendants(menu).first(where:{text($0,kAXRoleAttribute)=="AXMenuItem" && text($0,kAXTitleAttribute)==arguments[3]}) else { fail("Menu item unavailable") } press(item) case "close": guard arguments.count==3,let button=value(window(arguments[2]),kAXCloseButtonAttribute) else { fail("Close unavailable") } press(unsafeBitCast(button as CFTypeRef,to:AXUIElement.self)) case "resize": guard arguments.count==5,let width=Double(arguments[3]),let height=Double(arguments[4]) else { fail("Supply width and height") } var size=CGSize(width:width,height:height) guard AXUIElementSetAttributeValue(window(arguments[2]),kAXSizeAttribute as CFString,AXValueCreate(.cgSize,&size)!) == .success else { fail("Resize failed") } case "capture": guard arguments.count==4 else { fail("Supply window title and output PNG") } let windows=CGWindowListCopyWindowInfo([.optionOnScreenOnly,.excludeDesktopElements],kCGNullWindowID) as? [[String:Any]] ?? [] guard let w=windows.first(where: { ($0[kCGWindowOwnerPID as String] as? Int32)==pid && ($0[kCGWindowName as String] as? String)==arguments[2] }),let number=w[kCGWindowNumber as String] as? Int else { fail("Visible window unavailable") } let process=Process();process.executableURL=URL(fileURLWithPath:"/usr/sbin/screencapture") process.arguments=["-x","-o","-l",String(number),arguments[3]] try process.run();process.waitUntilExit();if process.terminationStatus != 0 {fail("Capture failed")} default:fail("Unknown action") }