// SPDX-License-Identifier: GPL-3.0-or-later // A real AppKit file drag into an isolated VCC validation window. // swiftc MediaDragProbe.swift -o /tmp/vcc-media-drag // /tmp/vcc-media-drag PID /absolute/authored/file.dsk [accessibility label] import AppKit import ApplicationServices let args = CommandLine.arguments func fail(_ text: String) -> Never { fputs(text + "\n", stderr); exit(1) } guard args.count >= 3, let pid = Int32(args[1]), AXIsProcessTrusted() else { fail("Supply a validation PID and authored file; Accessibility access is required") } let targetApp = AXUIElementCreateApplication(pid) func value(_ e: AXUIElement, _ key: String) -> Any? { var v: CFTypeRef?; AXUIElementCopyAttributeValue(e,key as CFString,&v);return v } func elements(_ e: AXUIElement, _ depth: Int = 0) -> [AXUIElement] { depth > 15 ? [] : [e] + ((value(e,kAXChildrenAttribute) as? [AXUIElement]) ?? []).flatMap { elements($0,depth+1) } } let all=elements(targetApp) guard all.contains(where:{value($0,kAXTitleAttribute) as? String == "VCC Startup Validation" || value($0,kAXTitleAttribute) as? String == "VCC UI Tests"}) else { fail("Refusing a normal emulator session") } let label=args.count > 3 ? args[3] : "CoCo display" guard let target=all.first(where:{value($0,kAXDescriptionAttribute) as? String == label}), let position=value(target,kAXPositionAttribute),let size=value(target,kAXSizeAttribute) else {fail("Drop target unavailable")} var point=CGPoint.zero,extent=CGSize.zero AXValueGetValue(position as! AXValue,.cgPoint,&point);AXValueGetValue(size as! AXValue,.cgSize,&extent) let destination=CGPoint(x:point.x+extent.width/2,y:point.y+extent.height/2) let file=URL(fileURLWithPath:args[2]) guard FileManager.default.fileExists(atPath:file.path) else {fail("Authored file missing")} class Source: NSView, NSDraggingSource { override func draw(_ dirtyRect: NSRect) { NSColor.windowBackgroundColor.setFill();bounds.fill();("Drag authored media" as NSString).draw(at:NSPoint(x:15,y:25),withAttributes:[.foregroundColor:NSColor.labelColor]) } override func mouseDown(with event: NSEvent) { let item=NSDraggingItem(pasteboardWriter:file as NSURL) item.setDraggingFrame(NSRect(x:15,y:15,width:48,height:48),contents:NSWorkspace.shared.icon(forFile:file.path)) beginDraggingSession(with:[item],event:event,source:self) } func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor context: NSDraggingContext) -> NSDragOperation { .copy } func draggingSession(_ session: NSDraggingSession, endedAt point: NSPoint, operation: NSDragOperation) { print(operation == .copy ? "PASS: native file drop accepted" : "Drop did not complete") DispatchQueue.main.asyncAfter(deadline:.now()+0.2) { NSApp.terminate(nil) } } } let app=NSApplication.shared;app.setActivationPolicy(.accessory) let window=NSWindow(contentRect:NSRect(x:20,y:40,width:210,height:80),styleMask:[.titled],backing:.buffered,defer:false) window.title="VCC Drag Probe";window.contentView=Source();window.makeKeyAndOrderFront(nil);app.activate(ignoringOtherApps:true) DispatchQueue.main.asyncAfter(deadline:.now()+0.5) { let screenHeight=NSScreen.screens[0].frame.height let origin=window.convertPoint(toScreen:NSPoint(x:60,y:35)) let source=CGPoint(x:origin.x,y:screenHeight-origin.y) func event(_ type:CGEventType,_ point:CGPoint) { CGEvent(mouseEventSource:nil,mouseType:type,mouseCursorPosition:point,mouseButton:.left)?.post(tap:.cghidEventTap) } event(.mouseMoved,source);event(.leftMouseDown,source) for i in 1...24 { DispatchQueue.main.asyncAfter(deadline:.now()+Double(i)*0.04) { let f=Double(i)/24 event(.leftMouseDragged,CGPoint(x:source.x+(destination.x-source.x)*f,y:source.y+(destination.y-source.y)*f)) if i==24 {event(.leftMouseUp,destination)} } } } DispatchQueue.main.asyncAfter(deadline:.now()+12) { NSApp.terminate(nil) } app.run()