From 4cd4113221b45ce21867769508eda80b4f927c8b Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Thu, 19 Nov 2026 23:33:24 +0800 Subject: [PATCH 1/3] simulator: iPhone 7, turning the device, keyboard stays on the guest - Device menu: iPad 6 or iPhone 7, each with its own realistic frame and panel size (`simulator/vm device`); the VM keeps the choice. - Rotate Left/Right (Ctrl+Cmd+Left/Right) turns the whole device frame. A VM-only IIO accelerometer (orchard-vm-accel) reports the gravity the turned device would feel, so iio-sensor-proxy and the desktop's own rotation path do the rest, as on a tablet. - The frame's buttons send their key without taking the Mac keyboard away from the guest. - The display reconnects after the VM restarts instead of staying black. - ORCHARD_SIMULATOR_BACKGROUND=1 opens the window without activating it. Assisted-by: LLM --- CHANGELOG.md | 8 + simulator/GuestDisplay.swift | 12 +- simulator/build-vm-backlight | 4 +- simulator/main.swift | 373 ++++++++++++++++++---- simulator/vm | 34 +- simulator/vm-support/Makefile | 2 +- simulator/vm-support/install-linux.sh | 6 +- simulator/vm-support/orchard-vm-accel.c | 92 ++++++ simulator/vm-support/orchard-vm-bridge.py | 18 +- 9 files changed, 474 insertions(+), 75 deletions(-) create mode 100644 simulator/vm-support/orchard-vm-accel.c diff --git a/CHANGELOG.md b/CHANGELOG.md index 4df8a9d2..6562a1f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,14 @@ is missing or empty. `orchard launch` starts a system from internal storage: m1n1 freed its storage command queue twice while shutting storage down. +### Development + +- Orchard Simulator: an iPhone 7 alongside the iPad 6 (Device menu), each in + a realistic frame. Rotate Left and Right (⌃⌘← and ⌃⌘→) turn the device, + and the desktop follows through the same rotation-sensor path as a real + tablet. The frame's buttons no longer take the keyboard away from the + guest, and the screen comes back by itself after the VM restarts. + ## [0.3.2] - 2026-11-19 ### Devices diff --git a/simulator/GuestDisplay.swift b/simulator/GuestDisplay.swift index d8a79618..2fe18dbc 100644 --- a/simulator/GuestDisplay.swift +++ b/simulator/GuestDisplay.swift @@ -14,7 +14,13 @@ final class GuestDisplay { init(qmpPath: String) { self.qmpPath = qmpPath } + // Set up once per connection; when the VM goes away (a restart for + // another device, say) the window connects to the next one by itself. + private var connecting = false + func start() { + guard !connecting else { return } + connecting = true Thread.detachNewThread { [weak self] in while let self { var error = [CChar](repeating: 0, count: 512) @@ -38,8 +44,12 @@ final class GuestDisplay { display.pixels = nil display.lock.unlock() DispatchQueue.main.async { display.onConnection?(false) } + DispatchQueue.main.asyncAfter(deadline: .now() + 1) { display.start() } }) - if qd_start(self.qmpPath, callbacks, &error, Int32(error.count)) == 0 { return } + if qd_start(self.qmpPath, callbacks, &error, Int32(error.count)) == 0 { + DispatchQueue.main.async { self.connecting = false } + return + } Thread.sleep(forTimeInterval: 1) } } diff --git a/simulator/build-vm-backlight b/simulator/build-vm-backlight index 7925531a..1409955c 100755 --- a/simulator/build-vm-backlight +++ b/simulator/build-vm-backlight @@ -37,6 +37,6 @@ podman run --rm -v "$ROOT:/work" localhost/orchard-hardware-kernel-toolchain:boo set -e cp -r /work/simulator/vm-support /tmp/module make -C "/work/out/simulator/kernel-build/usr/src/kernels/'"$KVER"'" M=/tmp/module CC=/work/out/simulator/kernel-build/gcc-compat modules - cp /tmp/module/orchard-vm-backlight.ko /work/out/simulator/vm/orchard-vm-backlight.ko + cp /tmp/module/orchard-vm-backlight.ko /tmp/module/orchard-vm-accel.ko /work/out/simulator/vm/ ' -echo "VM backlight module built for $KVER" +echo "VM backlight and accelerometer modules built for $KVER" diff --git a/simulator/main.swift b/simulator/main.swift index 72621383..13eba560 100644 --- a/simulator/main.swift +++ b/simulator/main.swift @@ -1,13 +1,66 @@ import AppKit -// iPad (6th generation), Wi-Fi: 169.5 × 240 mm, 9.7-inch 4:3 display. -// Coordinates are expressed in device-relative units, with a 240-unit body height. -enum Device { - static let width: CGFloat = 169.5 - static let height: CGFloat = 240 - static let scale: CGFloat = 3.0 +// A simulated device, upright, in millimetres from its top left corner: the +// body, the display, the buttons. Its panel (the VM's screen) is fixed when +// the VM starts; the frame is drawn from these numbers. +struct DeviceProfile { + let name: String // the VM script's name for it + let title: String + let width: CGFloat + let height: CGFloat + let cornerRadius: CGFloat + let display: NSRect + let displayRadius: CGFloat + let home: NSRect + // Drawn button, and the larger area a click finds it in. + let buttons: [(button: HardwareButton, drawn: NSRect, hit: NSRect)] + let muteSwitch: NSRect? + let camera: NSPoint + let earpiece: NSRect? + let darkFront: Bool + // Window pixels per millimetre: both devices open about as tall. + var scale: CGFloat { 720 / height } static let margin: CGFloat = 24 - static let canvas = NSSize(width: width * scale + margin * 2, height: height * scale + margin * 2) + + // iPad (6th generation), Wi-Fi: 169.5 × 240 mm, 9.7-inch 4:3 display. + static let ipad6 = DeviceProfile( + name: "ipad6", title: "iPad (6th generation)", width: 169.5, height: 240, cornerRadius: 10, + display: NSRect(x: 10.835, y: 21.45, width: 147.83, height: 197.10), displayRadius: 0.12, + home: NSRect(x: 79.2, y: 223.15, width: 11.1, height: 11.1), + buttons: [(.power, NSRect(x: 128.5, y: -1.1, width: 19, height: 2.3), NSRect(x: 125, y: -5, width: 28, height: 10)), + (.volumeUp, NSRect(x: 168.4, y: 34.5, width: 2.3, height: 12), NSRect(x: 164, y: 31, width: 11, height: 19)), + (.volumeDown, NSRect(x: 168.4, y: 53, width: 2.3, height: 12), NSRect(x: 164, y: 50, width: 11, height: 19))], + muteSwitch: nil, camera: NSPoint(x: 84.75, y: 10.65), earpiece: nil, darkFront: false) + + // iPhone 7: 67.1 × 138.3 mm, 4.7-inch 16:9 display (750 × 1334 pixels), + // black front; the side button on the right, volume and the ring/silent + // switch on the left, the camera beside the earpiece. + static let iphone7 = DeviceProfile( + name: "iphone7", title: "iPhone 7", width: 67.1, height: 138.3, cornerRadius: 9.4, + display: NSRect(x: 4.3, y: 17.15, width: 58.5, height: 104.0), displayRadius: 0.2, + home: NSRect(x: 28.1, y: 124.3, width: 10.9, height: 10.9), + buttons: [(.power, NSRect(x: 66.75, y: 33, width: 1.6, height: 10.5), NSRect(x: 63, y: 30, width: 9, height: 16)), + (.volumeUp, NSRect(x: -1.25, y: 33.5, width: 1.6, height: 8.5), NSRect(x: -5, y: 31, width: 9, height: 12)), + (.volumeDown, NSRect(x: -1.25, y: 45, width: 1.6, height: 8.5), NSRect(x: -5, y: 43, width: 9, height: 12))], + muteSwitch: NSRect(x: -1.1, y: 22, width: 1.4, height: 5), camera: NSPoint(x: 23.2, y: 8.7), + earpiece: NSRect(x: 28.6, y: 8.05, width: 9.9, height: 1.3), darkFront: true) + + static let all = [ipad6, iphone7] + static func named(_ name: String) -> DeviceProfile { all.first { $0.name == name } ?? ipad6 } +} + +// How the window holds the device: the edge that is up, named as the +// accelerometer (iio-sensor-proxy) names it, and the clockwise turn that +// draws the device so. +enum Orientation: String, CaseIterable { + case normal, leftUp = "left-up", bottomUp = "bottom-up", rightUp = "right-up" + var degrees: CGFloat { CGFloat(Orientation.allCases.firstIndex(of: self)!) * 90 } + var quarterTurned: Bool { self == .leftUp || self == .rightUp } + // A clockwise quarter turn brings the left edge up. + func turned(clockwise: Bool) -> Orientation { + let all = Orientation.allCases + return all[(all.firstIndex(of: self)! + (clockwise ? 1 : 3)) % 4] + } } enum HardwareButton: String, CaseIterable { @@ -44,7 +97,60 @@ final class SimulatorView: NSView { private var resignObserver: NSObjectProtocol? private var appResignObserver: NSObjectProtocol? private var commandKeyUpMonitor: Any? - private let displayRect = NSRect(x: 10.835, y: 21.45, width: 147.83, height: 197.10) + private(set) var profile = DeviceProfile.ipad6 + private(set) var orientation = Orientation.normal + private var displayRect: NSRect { profile.display } + private var margin: CGFloat { DeviceProfile.margin } + + // The device as the window holds it, in millimetres, and the window's + // canvas around it. + private var heldSize: NSSize { + orientation.quarterTurned ? NSSize(width: profile.height, height: profile.width) + : NSSize(width: profile.width, height: profile.height) + } + var canvas: NSSize { + NSSize(width: heldSize.width * profile.scale + margin * 2, height: heldSize.height * profile.scale + margin * 2) + } + + // Device millimetres (upright) to the held device's millimetres, and back. + private func held(_ p: NSPoint) -> NSPoint { + let w = profile.width, h = profile.height + switch orientation { + case .normal: return p + case .leftUp: return NSPoint(x: h - p.y, y: p.x) + case .bottomUp: return NSPoint(x: w - p.x, y: h - p.y) + case .rightUp: return NSPoint(x: p.y, y: w - p.x) + } + } + private func upright(_ p: NSPoint) -> NSPoint { + let w = profile.width, h = profile.height + switch orientation { + case .normal: return p + case .leftUp: return NSPoint(x: p.y, y: h - p.x) + case .bottomUp: return NSPoint(x: w - p.x, y: h - p.y) + case .rightUp: return NSPoint(x: w - p.y, y: p.x) + } + } + // A device point to view coordinates. + private func viewPoint(_ p: NSPoint) -> NSPoint { + let q = held(p) + return NSPoint(x: (margin + q.x * profile.scale) * viewScale, y: (margin + q.y * profile.scale) * viewScale) + } + + func setDevice(_ next: DeviceProfile) { + releaseGuestInput() + profile = next + orientation = .normal + needsLayout = true + needsDisplay = true + } + + func setOrientation(_ next: Orientation) { + releaseGuestInput() + orientation = next + needsLayout = true + needsDisplay = true + } // The inner half of the bezel is the screen's edge: a drag that starts // there is a finger sliding onto the screen (the keyboard from the // bottom, panels from the sides and the top). The outer half moves the @@ -168,13 +274,13 @@ final class SimulatorView: NSView { hardware?.pointer(x: touchPosition.x, y: touchPosition.y, buttons: buttons) } - private var viewScale: CGFloat { bounds.width / Device.canvas.width } + private var viewScale: CGFloat { bounds.width / canvas.width } private func corner(at point: NSPoint) -> (right: Bool, top: Bool)? { let s = viewScale - let left = Device.margin * s - let right = (Device.margin + Device.width * Device.scale) * s - let top = Device.margin * s - let bottom = (Device.margin + Device.height * Device.scale) * s + let left = margin * s + let right = (margin + heldSize.width * profile.scale) * s + let top = margin * s + let bottom = (margin + heldSize.height * profile.scale) * s let reach: CGFloat = 17 * s if abs(point.x - left) < reach && abs(point.y - top) < reach { return (false, true) } if abs(point.x - right) < reach && abs(point.y - top) < reach { return (true, true) } @@ -198,14 +304,12 @@ final class SimulatorView: NSView { private func devicePoint(_ event: NSEvent) -> NSPoint { let p = convert(event.locationInWindow, from: nil) let s = viewScale - return NSPoint(x: (p.x / s - Device.margin) / Device.scale, y: (p.y / s - Device.margin) / Device.scale) + return upright(NSPoint(x: (p.x / s - margin) / profile.scale, y: (p.y / s - margin) / profile.scale)) } private func button(at p: NSPoint) -> HardwareButton? { - if NSRect(x: 125, y: -5, width: 28, height: 10).contains(p) { return .power } - if NSRect(x: 164, y: 31, width: 11, height: 19).contains(p) { return .volumeUp } - if NSRect(x: 164, y: 50, width: 11, height: 19).contains(p) { return .volumeDown } - if NSPoint(x: p.x - 84.75, y: p.y - 228.7).length < 7.3 { return .home } + for entry in profile.buttons where entry.hit.contains(p) { return entry.button } + if NSPoint(x: p.x - profile.home.midX, y: p.y - profile.home.midY).length < profile.home.width * 0.66 { return .home } return nil } @@ -250,10 +354,18 @@ final class SimulatorView: NSView { sendTouch(event, phase: "begin") return } - releaseGuestKeys() - guestFocused = false resizeCorner = corner(at: convert(event.locationInWindow, from: nil)) pressed = resizeCorner == nil ? button(at: devicePoint(event)) : nil + // A hardware button is part of the tablet: the Mac keyboard keeps + // typing into the guest after it (Home, then type to search). Only + // moving or resizing the frame hands the keyboard back to macOS. + if pressed == nil { + releaseGuestKeys() + guestFocused = false + } else if guestConnected { + guestFocused = true + window?.makeFirstResponder(self) + } if let pressed { hardware?.key(hardwareKey(pressed), down: true) } if pressed == nil { mouseDownScreen = NSEvent.mouseLocation @@ -270,7 +382,7 @@ final class SimulatorView: NSView { if let start = mouseDownScreen, let origin = windowOrigin { let now = NSEvent.mouseLocation if let corner = resizeCorner, let frame = resizeFrame { - let aspect = Device.canvas.width / Device.canvas.height + let aspect = canvas.width / canvas.height let dx = (now.x - start.x) * (corner.right ? 1 : -1) let dy = (now.y - start.y) * (corner.top ? 1 : -1) * aspect let delta = abs(dx) >= abs(dy) ? dx : dy @@ -305,8 +417,7 @@ final class SimulatorView: NSView { guard let window else { completion(); return } let device = NSPoint(x: displayRect.minX + displayRect.width * x / 768, y: displayRect.minY + displayRect.height * y / 1024) - let location = convert(NSPoint(x: (Device.margin + device.x * Device.scale) * viewScale, - y: (Device.margin + device.y * Device.scale) * viewScale), to: nil) + let location = convert(viewPoint(device), to: nil) func event(_ type: NSEvent.EventType) -> NSEvent? { NSEvent.mouseEvent(with: type, location: location, modifierFlags: [], timestamp: 0, windowNumber: window.windowNumber, context: nil, eventNumber: 1, clickCount: 1, pressure: 1) @@ -323,8 +434,7 @@ final class SimulatorView: NSView { func event(_ type: NSEvent.EventType, _ y: CGFloat) -> NSEvent? { let device = NSPoint(x: displayRect.minX + displayRect.width * 620 / 768, y: displayRect.minY + displayRect.height * y / 1024) - let location = convert(NSPoint(x: (Device.margin + device.x * Device.scale) * viewScale, - y: (Device.margin + device.y * Device.scale) * viewScale), to: nil) + let location = convert(viewPoint(device), to: nil) return NSEvent.mouseEvent(with: type, location: location, modifierFlags: [], timestamp: 0, windowNumber: window.windowNumber, context: nil, eventNumber: 1, clickCount: 1, pressure: 1) } @@ -343,8 +453,8 @@ final class SimulatorView: NSView { func testPowerButton(completion: @escaping (String) -> Void) { guard let window else { completion("FAIL: no window"); return } let initial = brightness - let p = convert(NSPoint(x: (Device.margin + 138 * Device.scale) * viewScale, - y: (Device.margin - 0.2 * Device.scale) * viewScale), to: nil) + let power = profile.buttons.first { $0.button == .power }!.drawn + let p = convert(viewPoint(NSPoint(x: power.midX, y: power.midY)), to: nil) func click() { for type in [NSEvent.EventType.leftMouseDown, .leftMouseUp] { guard let event = NSEvent.mouseEvent(with: type, location: p, modifierFlags: [], timestamp: 0, windowNumber: window.windowNumber, context: nil, eventNumber: 1, clickCount: 1, pressure: 1) else { continue } @@ -466,76 +576,99 @@ final class SimulatorView: NSView { } // The guest screen is its own layer above the drawn frame, so a guest - // frame never redraws the device around it. + // frame never redraws the device around it. It keeps the panel's own + // upright shape and turns with the device, as the real panel does; the + // guest turns its picture inside it. override func layout() { super.layout() - let s = viewScale * Device.scale + let a = viewPoint(NSPoint(x: displayRect.minX, y: displayRect.minY)) + let b = viewPoint(NSPoint(x: displayRect.maxX, y: displayRect.maxY)) // On whole device pixels, so drawing the frame adds no half-pixel blur. - screen.frame = backingAlignedRect(NSRect(x: (Device.margin + displayRect.minX * Device.scale) * viewScale, - y: (Device.margin + displayRect.minY * Device.scale) * viewScale, - width: displayRect.width * s, height: displayRect.height * s), + let heldRect = backingAlignedRect(NSRect(x: min(a.x, b.x), y: min(a.y, b.y), width: abs(b.x - a.x), height: abs(b.y - a.y)), options: .alignAllEdgesNearest) + let size = orientation.quarterTurned ? NSSize(width: heldRect.height, height: heldRect.width) : heldRect.size + screen.frameCenterRotation = 0 + screen.frame = NSRect(x: heldRect.midX - size.width / 2, y: heldRect.midY - size.height / 2, width: size.width, height: size.height) + screen.frameCenterRotation = orientation.degrees } override func setFrameSize(_ newSize: NSSize) { super.setFrameSize(newSize); needsLayout = true } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) + let device = profile NSGraphicsContext.saveGraphicsState() let transform = NSAffineTransform() transform.scale(by: viewScale) - transform.translateX(by: Device.margin, yBy: Device.margin) - transform.scale(by: Device.scale) + transform.translateX(by: margin, yBy: margin) + transform.scale(by: device.scale) + // Held as the window holds it: turned clockwise about its corner. + switch orientation { + case .normal: break + case .leftUp: transform.translateX(by: device.height, yBy: 0) + case .bottomUp: transform.translateX(by: device.width, yBy: device.height) + case .rightUp: transform.translateX(by: 0, yBy: device.width) + } + transform.rotate(byDegrees: orientation.degrees) transform.concat() + let body = NSRect(x: 0, y: 0, width: device.width, height: device.height) + let r = device.cornerRadius + let dark = device.darkFront + // Hardware controls sit behind the aluminium edge. - let power = NSRect(x: 128.5, y: -1.1, width: 19, height: 2.3) - let up = NSRect(x: 168.4, y: 34.5, width: 2.3, height: 12) - let down = NSRect(x: 168.4, y: 53, width: 2.3, height: 12) - for (button, rect) in [(HardwareButton.power, power), (.volumeUp, up), (.volumeDown, down)] { - gradient(rect, radius: 0.8, colors: [c(0.55), c(0.9), c(0.65)], angle: 0) - if hovered == button || pressed == button { glow(rect.insetBy(dx: -0.5, dy: -0.5), radius: 1.2) } + for entry in device.buttons { + let shades = dark ? [c(0.18), c(0.42), c(0.22)] : [c(0.55), c(0.9), c(0.65)] + gradient(entry.drawn, radius: 0.8, colors: shades, angle: entry.drawn.width > entry.drawn.height ? 90 : 0) + if hovered == entry.button || pressed == entry.button { glow(entry.drawn.insetBy(dx: -0.5, dy: -0.5), radius: 1.2) } + } + if let mute = device.muteSwitch { + gradient(mute, radius: 0.5, colors: dark ? [c(0.18), c(0.4), c(0.22)] : [c(0.55), c(0.9), c(0.65)], angle: 0) } // A shadow follows the silhouette, rather than adding a rectangular window border. NSGraphicsContext.saveGraphicsState() let shadow = NSShadow(); shadow.shadowColor = c(0, 0.40); shadow.shadowBlurRadius = 8 shadow.shadowOffset = NSSize(width: 0, height: -2); shadow.set() - rounded(NSRect(x: 0, y: 0, width: 169.5, height: 240), 10, fill: c(0.75)) + rounded(body, r, fill: c(dark ? 0.2 : 0.75)) NSGraphicsContext.restoreGraphicsState() - // Silver chamfer, softly rounded white front glass, and the thin dark display gasket. - gradient(NSRect(x: 0, y: 0, width: 169.5, height: 240), radius: 10, - colors: [c(0.65), c(0.96), c(0.72)], angle: 0) - rounded(NSRect(x: 0.85, y: 0.85, width: 167.8, height: 238.3), 9.1, - fill: c(0.96), stroke: c(1, 0.9), lineWidth: 0.35) - gradient(NSRect(x: 1.25, y: 1.25, width: 167, height: 237.5), radius: 8.8, - colors: [c(0.94), c(0.992), c(0.96)], angle: 0) + // The metal edge, the softly rounded front glass, and the thin dark display gasket. + gradient(body, radius: r, colors: dark ? [c(0.12), c(0.34), c(0.16)] : [c(0.65), c(0.96), c(0.72)], angle: 0) + let glass = body.insetBy(dx: 0.85, dy: 0.85) + rounded(glass, r - 0.9, fill: c(dark ? 0.05 : 0.96), stroke: c(dark ? 0.32 : 1, 0.9), lineWidth: 0.35) + gradient(body.insetBy(dx: 1.25, dy: 1.25), radius: r - 1.2, + colors: dark ? [c(0.035), c(0.075), c(0.045)] : [c(0.94), c(0.992), c(0.96)], angle: 0) - // 9.7-inch display is 147.83 × 197.10 mm (4:3), centered horizontally. let display = displayRect - rounded(display.insetBy(dx: -0.35, dy: -0.35), 0.45, fill: c(0.14)) - rounded(display, 0.12, fill: NSColor(calibratedRed: 0.025, green: 0.029, blue: 0.035, alpha: 1)) + rounded(display.insetBy(dx: -0.35, dy: -0.35), device.displayRadius + 0.33, fill: c(dark ? 0.02 : 0.14)) + rounded(display, device.displayRadius, fill: NSColor(calibratedRed: 0.025, green: 0.029, blue: 0.035, alpha: 1)) // A restrained glass reflection; no fake desktop or fabricated device state. NSGraphicsContext.saveGraphicsState() NSBezierPath(rect: display).addClip() let reflection = NSBezierPath() reflection.move(to: NSPoint(x: display.minX, y: display.minY)) reflection.line(to: NSPoint(x: display.maxX, y: display.minY)) - reflection.line(to: NSPoint(x: display.minX, y: display.minY + 88)) + reflection.line(to: NSPoint(x: display.minX, y: display.minY + display.height * 0.45)) reflection.close() c(1, 0.012).setFill(); reflection.fill() NSGraphicsContext.restoreGraphicsState() - // Front camera and home button ring. - rounded(NSRect(x: 83.1, y: 9.0, width: 3.3, height: 3.3), 1.65, fill: c(0.71)) - rounded(NSRect(x: 83.55, y: 9.45, width: 2.4, height: 2.4), 1.2, + // Front camera, earpiece and the Home button ring. + let cam = device.camera + rounded(NSRect(x: cam.x - 1.65, y: cam.y - 1.65, width: 3.3, height: 3.3), 1.65, fill: c(dark ? 0.16 : 0.71)) + rounded(NSRect(x: cam.x - 1.2, y: cam.y - 1.2, width: 2.4, height: 2.4), 1.2, fill: NSColor(calibratedRed: 0.10, green: 0.14, blue: 0.18, alpha: 1)) - rounded(NSRect(x: 84.25, y: 9.95, width: 0.65, height: 0.65), 0.32, fill: c(0.49)) - let home = NSRect(x: 79.2, y: 223.15, width: 11.1, height: 11.1) - gradient(home, radius: 5.55, colors: [c(0.62), c(0.94), c(0.69)], angle: 45) - rounded(home.insetBy(dx: 0.65, dy: 0.65), 4.9, fill: c(0.97), stroke: c(1, 0.8), lineWidth: 0.15) - if hovered == .home || pressed == .home { glow(home.insetBy(dx: -0.6, dy: -0.6), radius: 6.2) } - if pressed == .home { rounded(home.insetBy(dx: 0.8, dy: 0.8), 4.7, fill: c(0.86, 0.35)) } + rounded(NSRect(x: cam.x - 0.5, y: cam.y - 0.7, width: 0.65, height: 0.65), 0.32, fill: c(0.49)) + if let earpiece = device.earpiece { + rounded(earpiece, earpiece.height / 2, fill: c(dark ? 0.13 : 0.62), stroke: c(dark ? 0.24 : 0.5, 0.8), lineWidth: 0.15) + } + let home = device.home + let ring = dark ? [c(0.28), c(0.55), c(0.32)] : [c(0.62), c(0.94), c(0.69)] + gradient(home, radius: home.width / 2, colors: ring, angle: 45) + rounded(home.insetBy(dx: 0.65, dy: 0.65), home.width / 2 - 0.65, fill: c(dark ? 0.06 : 0.97), + stroke: c(dark ? 0.3 : 1, 0.8), lineWidth: 0.15) + if hovered == .home || pressed == .home { glow(home.insetBy(dx: -0.6, dy: -0.6), radius: home.width / 2 + 0.6) } + if pressed == .home { rounded(home.insetBy(dx: 0.8, dy: 0.8), home.width / 2 - 0.8, fill: c(dark ? 0.35 : 0.86, 0.35)) } NSGraphicsContext.restoreGraphicsState() } @@ -607,6 +740,38 @@ final class AppDelegate: NSObject, NSApplicationDelegate { } appMenu.addItem(.separator()) } + let view = SimulatorView(frame: .zero) + if let directory = ProcessInfo.processInfo.environment["ORCHARD_VM_DIR"] { + // The device the VM's panel was started for. + let name = (try? String(contentsOfFile: directory + "/device", encoding: .utf8))? + .trimmingCharacters(in: .whitespacesAndNewlines) ?? "ipad6" + view.setDevice(DeviceProfile.named(name)) + if ProcessInfo.processInfo.environment["ORCHARD_VM_SCRIPT"] != nil { + for device in DeviceProfile.all { + let item = NSMenuItem(title: device.title, action: #selector(selectDevice(_:)), keyEquivalent: "") + item.target = self + item.representedObject = device.name + item.state = device.name == view.profile.name ? .on : .off + appMenu.addItem(item) + } + appMenu.addItem(.separator()) + } + // Upright to begin with, whatever the VM last felt. + sendControl("orientation normal") + } + // For the render check: draw another device, or the device turned. + if let name = ProcessInfo.processInfo.environment["ORCHARD_SIMULATOR_DEVICE"] { view.setDevice(DeviceProfile.named(name)) } + if let held = ProcessInfo.processInfo.environment["ORCHARD_SIMULATOR_ORIENTATION"].flatMap(Orientation.init(rawValue:)) { + view.setOrientation(held) + } + for (title, clockwise, key) in [("Rotate Left", false, NSLeftArrowFunctionKey), ("Rotate Right", true, NSRightArrowFunctionKey)] { + let item = NSMenuItem(title: title, action: #selector(rotate(_:)), keyEquivalent: String(Character(UnicodeScalar(key)!))) + item.keyEquivalentModifierMask = [.command, .control] + item.target = self + item.tag = clockwise ? 1 : 0 + appMenu.addItem(item) + } + appMenu.addItem(.separator()) let touch = NSMenuItem(title: "Touch Input", action: #selector(selectTouchInput(_:)), keyEquivalent: "") touch.target = self touch.tag = 1 @@ -627,17 +792,24 @@ final class AppDelegate: NSObject, NSApplicationDelegate { } appItem.submenu = appMenu app.mainMenu = menu - window = SimulatorWindow(contentRect: NSRect(origin: .zero, size: Device.canvas), styleMask: [.borderless], backing: .buffered, defer: false) + window = SimulatorWindow(contentRect: NSRect(origin: .zero, size: view.canvas), styleMask: [.borderless], backing: .buffered, defer: false) window.title = "Orchard Simulator" window.isOpaque = false window.backgroundColor = .clear window.hasShadow = false window.acceptsMouseMovedEvents = true window.isReleasedWhenClosed = false - window.contentView = SimulatorView(frame: NSRect(origin: .zero, size: Device.canvas)) + view.frame = NSRect(origin: .zero, size: view.canvas) + window.contentView = view window.center() - window.makeKeyAndOrderFront(nil) - app.activate(ignoringOtherApps: true) + // Started in the background (a rebuild while the person works + // elsewhere): shown, but not brought to the front. + if ProcessInfo.processInfo.environment["ORCHARD_SIMULATOR_BACKGROUND"] == "1" { + window.orderFront(nil) + } else { + window.makeKeyAndOrderFront(nil) + app.activate(ignoringOtherApps: true) + } if let path = ProcessInfo.processInfo.environment["ORCHARD_SIMULATOR_TOUCH_CHECK"] { DispatchQueue.main.asyncAfter(deadline: .now() + 3) { [weak self] in guard let view = self?.window.contentView as? SimulatorView else { app.terminate(nil); return } @@ -690,6 +862,77 @@ final class AppDelegate: NSObject, NSApplicationDelegate { alert.runModal() } } + // Turns the device in the window, frame and screen together, and tells the + // VM's accelerometer; the guest turns its picture as a tablet does. + @objc private func rotate(_ item: NSMenuItem) { + guard let view = window.contentView as? SimulatorView else { return } + let next = view.orientation.turned(clockwise: item.tag == 1) + refit(view) { view.setOrientation(next) } + sendControl("orientation \(next.rawValue)") + } + + // Another device means another panel: the VM restarts with it, on the + // same disk; the session starts again at the PIN screen. + @objc private func selectDevice(_ item: NSMenuItem) { + guard let view = window.contentView as? SimulatorView, let name = item.representedObject as? String, + name != view.profile.name else { return } + let device = DeviceProfile.named(name) + let confirm = NSAlert() + confirm.messageText = "Switch to \(device.title)?" + confirm.informativeText = "The VM restarts with the \(device.title)'s screen. Open apps close; the disk stays as it is." + confirm.addButton(withTitle: "Restart as \(device.title)") + confirm.addButton(withTitle: "Cancel") + guard confirm.runModal() == .alertFirstButtonReturn else { return } + window.alphaValue = 0.5 + runVM(["device", name]) { ok, message in + self.window.alphaValue = 1 + guard ok else { + let alert = NSAlert() + alert.messageText = "Could not switch the device" + alert.informativeText = message + alert.runModal() + return + } + self.refit(view) { view.setDevice(device) } + for entry in item.menu?.items ?? [] where entry.action == #selector(self.selectDevice(_:)) { + entry.state = entry === item ? .on : .off + } + } + } + + // Resizes the window to the view's new canvas at the same zoom, about the + // same centre. + private func refit(_ view: SimulatorView, _ change: () -> Void) { + let zoom = window.frame.width / view.canvas.width + let center = NSPoint(x: window.frame.midX, y: window.frame.midY) + change() + let size = NSSize(width: view.canvas.width * zoom, height: view.canvas.height * zoom) + window.setFrame(NSRect(x: center.x - size.width / 2, y: center.y - size.height / 2, width: size.width, height: size.height), display: true) + } + + // One line to the VM bridge (org.orchard.control), as the VM script sends it. + private func sendControl(_ line: String) { + guard let directory = ProcessInfo.processInfo.environment["ORCHARD_VM_DIR"] else { return } + DispatchQueue.global().async { + let fd = socket(AF_UNIX, SOCK_STREAM, 0) + guard fd >= 0 else { return } + defer { close(fd) } + var address = sockaddr_un() + address.sun_family = sa_family_t(AF_UNIX) + let path = directory + "/control.sock" + guard path.utf8.count < MemoryLayout.size(ofValue: address.sun_path) else { return } + withUnsafeMutableBytes(of: &address.sun_path) { buffer in + path.utf8CString.withUnsafeBytes { buffer.copyMemory(from: $0) } + } + let connected = withUnsafePointer(to: &address) { + $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { connect(fd, $0, socklen_t(MemoryLayout.size)) } + } + guard connected == 0 else { return } + let data = Array((line + "\n").utf8) + _ = data.withUnsafeBytes { Darwin.send(fd, $0.baseAddress, $0.count, 0) } + } + } + @objc private func selectTouchInput(_ item: NSMenuItem) { guard let view = window.contentView as? SimulatorView else { return } view.setTouchMode(item.tag == 1) diff --git a/simulator/vm b/simulator/vm index 5f2cdbf0..df0de740 100755 --- a/simulator/vm +++ b/simulator/vm @@ -1,7 +1,8 @@ #!/bin/sh set -eu HERE=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) -D=$(CDPATH= cd -- "$HERE/../out/simulator/vm" 2>/dev/null && pwd) || { echo 'VM is not prepared.' >&2; exit 1; } +# The window passes its VM's directory; otherwise the one beside this script. +D=$(CDPATH= cd -- "${ORCHARD_VM_DIR:-$HERE/../out/simulator/vm}" 2>/dev/null && pwd) || { echo 'VM is not prepared.' >&2; exit 1; } QEMU=/opt/glustore/bin/qemu-system-aarch64 running() { [ -f "$D/qemu.pid" ] || return 1 @@ -15,12 +16,17 @@ case "${1:-status}" in if running; then echo "VM already running (PID $pid)"; exit 0; fi [ -x "$QEMU" ] || { echo 'Install qemu using glu first.' >&2; exit 1; } [ -f "$D/root.raw" ] && [ -f "$D/vmlinuz" ] || { echo 'VM disk/kernel not prepared.' >&2; exit 1; } + # The simulated device fixes the virtual panel, as a real one does. + case "$(cat "$D/device" 2>/dev/null || echo ipad6)" in + iphone7) panel='xres=750,yres=1334' ;; + *) panel='xres=1536,yres=2048' ;; + esac umask 077 rm -f "$D/qmp.sock" "$D/agent.sock" "$D/viewer.sock" "$D/display.sock" "$D/control.sock" nohup "$QEMU" -machine virt,accel=hvf -cpu host -m 4096 -smp 4 \ -kernel "$D/vmlinuz" -append 'root=/dev/vda rootwait rw console=ttyAMA0 quiet fbcon=map:9 vt.global_cursor_default=0' \ -drive "file=$D/root.raw,format=raw,if=virtio" \ - -device virtio-gpu-pci,xres=1536,yres=2048 -device qemu-xhci -device usb-tablet -device virtio-multitouch-pci -device virtio-keyboard-pci \ + -device virtio-gpu-pci,$panel -device qemu-xhci -device usb-tablet -device virtio-multitouch-pci -device virtio-keyboard-pci \ -device virtio-serial-pci -chardev "socket,id=screen,path=$D/display.sock,server=on,wait=off" \ -device virtserialport,chardev=screen,name=org.orchard.display \ -chardev "socket,id=control,path=$D/control.sock,server=on,wait=off" \ @@ -83,5 +89,27 @@ PY printf '%s\n' "$2" > "$D/desktop" echo "Desktop: $2 (the session restarts)" ;; - *) echo "Usage: $0 {start|stop|kill|status|desktop gpu|software}" >&2; exit 2;; + orientation) + # Which edge is up as the window holds the device; the VM accelerometer + # reports the matching gravity. + case "${2:-}" in normal|left-up|right-up|bottom-up) ;; *) echo "Usage: $0 orientation {normal|left-up|right-up|bottom-up}" >&2; exit 2;; esac + running || { echo 'VM is not running' >&2; exit 1; } + python3 - "$D/control.sock" "$2" <<'PY' +import socket, sys +s = socket.socket(socket.AF_UNIX) +s.settimeout(4) +s.connect(sys.argv[1]) +s.sendall(b'orientation ' + sys.argv[2].encode() + b'\n') +s.close() +PY + ;; + device) + # The simulated device: its panel is fixed when the VM starts, so a + # running VM restarts with the new one (same disk, same install). + case "${2:-}" in ipad6|iphone7) ;; *) echo "Usage: $0 device {ipad6|iphone7}" >&2; exit 2;; esac + printf '%s\n' "$2" > "$D/device" + if running; then "$0" stop && "$0" start; fi + echo "Device: $2" + ;; + *) echo "Usage: $0 {start|stop|kill|status|desktop gpu|software|orientation EDGE|device ipad6|iphone7}" >&2; exit 2;; esac diff --git a/simulator/vm-support/Makefile b/simulator/vm-support/Makefile index f2b8905d..38bcf41a 100644 --- a/simulator/vm-support/Makefile +++ b/simulator/vm-support/Makefile @@ -1 +1 @@ -obj-m += orchard-vm-backlight.o +obj-m += orchard-vm-backlight.o orchard-vm-accel.o diff --git a/simulator/vm-support/install-linux.sh b/simulator/vm-support/install-linux.sh index f7543149..2b9af5ca 100755 --- a/simulator/vm-support/install-linux.sh +++ b/simulator/vm-support/install-linux.sh @@ -7,15 +7,17 @@ SUPPORT="$BASE/simulator/vm-support" MOUNT="$D/mount" KVER=$(cat "$D/kernel-version") [ "$(id -u)" = 0 ] || { echo 'Run as root in the Podman Linux VM' >&2; exit 1; } -[ -f "$D/root.raw" ] && [ -f "$D/orchard-vm-backlight.ko" ] || { echo 'VM disk or matching module missing' >&2; exit 1; } +[ -f "$D/root.raw" ] && [ -f "$D/orchard-vm-backlight.ko" ] && [ -f "$D/orchard-vm-accel.ko" ] || { echo 'VM disk or matching modules missing' >&2; exit 1; } mkdir -p "$MOUNT" cleanup() { if mountpoint -q "$MOUNT"; then umount "$MOUNT"; fi; } trap cleanup EXIT INT TERM mount -o loop "$D/root.raw" "$MOUNT" install -D -m 0644 "$D/orchard-vm-backlight.ko" "$MOUNT/usr/lib/modules/$KVER/extra/orchard-vm-backlight.ko" +install -D -m 0644 "$D/orchard-vm-accel.ko" "$MOUNT/usr/lib/modules/$KVER/extra/orchard-vm-accel.ko" depmod -b "$MOUNT" "$KVER" install -D -m 0755 "$SUPPORT/orchard-vm-bridge.py" "$MOUNT/usr/local/libexec/orchard-vm-bridge" printf '%s\n' orchard-vm-backlight > "$MOUNT/etc/modules-load.d/orchard-vm-backlight.conf" +printf '%s\n' industrialio orchard-vm-accel > "$MOUNT/etc/modules-load.d/orchard-vm-accel.conf" cat > "$MOUNT/etc/systemd/system/orchard-vm-bridge.service" <<'UNIT' [Unit] Description=Orchard simulator virtual hardware bridge @@ -34,4 +36,4 @@ sync umount "$MOUNT" rmdir "$MOUNT" trap - EXIT INT TERM -printf 'Installed VM-only backlight and bridge for %s\n' "$KVER" +printf 'Installed VM-only backlight, accelerometer and bridge for %s\n' "$KVER" diff --git a/simulator/vm-support/orchard-vm-accel.c b/simulator/vm-support/orchard-vm-accel.c new file mode 100644 index 00000000..5059616d --- /dev/null +++ b/simulator/vm-support/orchard-vm-accel.c @@ -0,0 +1,92 @@ +// VM-only substitute for a tablet's accelerometer, not part of the product image. +// The simulator window turns the device; the VM bridge sets the gravity it +// would feel (in mg) through this module's parameters. iio-sensor-proxy polls +// it like any IIO accelerometer, so rotation runs the tablet's own path. +#include +#include +#include + +static int gravity_x; +static int gravity_y = -1000; // upright: gravity towards the bottom edge +static int gravity_z; +module_param_named(x, gravity_x, int, 0644); +module_param_named(y, gravity_y, int, 0644); +module_param_named(z, gravity_z, int, 0644); + +static struct platform_device *device; +static struct iio_dev *accel; + +#define VM_ACCEL_CHANNEL(axis, index) { \ + .type = IIO_ACCEL, \ + .modified = 1, \ + .channel2 = IIO_MOD_##axis, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ + .scan_index = index, \ +} + +static const struct iio_chan_spec vm_accel_channels[] = { + VM_ACCEL_CHANNEL(X, 0), + VM_ACCEL_CHANNEL(Y, 1), + VM_ACCEL_CHANNEL(Z, 2), +}; + +static int vm_accel_read_raw(struct iio_dev *dev, struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_RAW: + switch (chan->channel2) { + case IIO_MOD_X: *val = READ_ONCE(gravity_x); break; + case IIO_MOD_Y: *val = READ_ONCE(gravity_y); break; + default: *val = READ_ONCE(gravity_z); break; + } + return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + // One mg in m/s². + *val = 0; + *val2 = 9807; + return IIO_VAL_INT_PLUS_MICRO; + } + return -EINVAL; +} + +static const struct iio_info vm_accel_info = { + .read_raw = vm_accel_read_raw, +}; + +static int __init vm_accel_init(void) +{ + int error; + + device = platform_device_register_simple("orchard-vm-accel", -1, NULL, 0); + if (IS_ERR(device)) return PTR_ERR(device); + accel = iio_device_alloc(&device->dev, 0); + if (!accel) { + platform_device_unregister(device); + return -ENOMEM; + } + accel->name = "orchard-vm-accel"; + accel->info = &vm_accel_info; + accel->channels = vm_accel_channels; + accel->num_channels = ARRAY_SIZE(vm_accel_channels); + accel->modes = INDIO_DIRECT_MODE; + error = iio_device_register(accel); + if (error) { + iio_device_free(accel); + platform_device_unregister(device); + return error; + } + return 0; +} + +static void __exit vm_accel_exit(void) +{ + iio_device_unregister(accel); + iio_device_free(accel); + platform_device_unregister(device); +} +module_init(vm_accel_init); +module_exit(vm_accel_exit); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Virtual accelerometer for the Orchard simulator"); diff --git a/simulator/vm-support/orchard-vm-bridge.py b/simulator/vm-support/orchard-vm-bridge.py index c5835a1d..42f6c4a6 100644 --- a/simulator/vm-support/orchard-vm-bridge.py +++ b/simulator/vm-support/orchard-vm-bridge.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -"""VM-only hardware bridge: report backlight state; accept shutdown and the desktop switch only.""" +"""VM-only hardware bridge: report backlight state; accept shutdown, the desktop switch and the device's orientation only.""" import errno import os import select @@ -9,6 +9,15 @@ BASE = '/dev/virtio-ports/' BRIGHTNESS = '/sys/class/backlight/20e200080.backlight/brightness' +ACCEL = '/sys/module/orchard_vm_accel/parameters/' +# The gravity the VM accelerometer reports (mg) when the window holds the +# device this way up; iio-sensor-proxy names its orientations the same way. +GRAVITY = { + b'normal': (0, -1000, 0), + b'bottom-up': (0, 1000, 0), + b'left-up': (1000, 0, 0), + b'right-up': (-1000, 0, 0), +} MAXIMUM = '/sys/class/backlight/20e200080.backlight/max_brightness' def open_port(name): @@ -56,6 +65,13 @@ def commands(): line, pending = pending.split(b'\n', 1) if line == b'shutdown': subprocess.run(['/usr/bin/systemctl', 'poweroff'], check=False) + elif line.startswith(b'orientation ') and line[12:] in GRAVITY: + for axis, value in zip('xyz', GRAVITY[line[12:]]): + try: + with open(ACCEL + axis, 'w') as f: + f.write(str(value)) + except OSError: + pass elif line in (b'desktop gpu', b'desktop software'): # The GPU desktop on software OpenGL, or the software desktop; # the session starts again with it (back to the PIN screen). From 4c8ca6b96eb4cb1bad0245c39132eb21dac1d964 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Thu, 19 Nov 2026 23:37:23 +0800 Subject: [PATCH 2/3] simulator: build the accelerometer module on existing disks; switch device only after a clean stop simulator/setup rebuilt the VM modules only when the backlight module was missing, so an existing disk never got orchard-vm-accel.ko and install-linux.sh then refused to run. simulator/vm device wrote the new device before stopping, so a stop that timed out still switched the panel on the next start. --- simulator/setup | 8 ++++---- simulator/vm | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/simulator/setup b/simulator/setup index 756c6132..b768e588 100755 --- a/simulator/setup +++ b/simulator/setup @@ -56,14 +56,14 @@ podman machine ssh "sudo $HERE/prepare-vm-linux.sh $DOWNLOAD" # button's blanking then dims the window as it darkens a tablet's panel. # Without it everything else still works; the window just stays lit. kver=$(cat "$VM/kernel-version") -if [ ! -f "$VM/orchard-vm-backlight.ko" ] || [ "$(cat "$VM/backlight-kernel" 2>/dev/null)" != "$kver" ]; then - rm -f "$VM/orchard-vm-backlight.ko" +if [ ! -f "$VM/orchard-vm-backlight.ko" ] || [ ! -f "$VM/orchard-vm-accel.ko" ] || [ "$(cat "$VM/backlight-kernel" 2>/dev/null)" != "$kver" ]; then + rm -f "$VM/orchard-vm-backlight.ko" "$VM/orchard-vm-accel.ko" if "$HERE/build-vm-backlight"; then printf '%s\n' "$kver" > "$VM/backlight-kernel"; fi fi -if [ -f "$VM/orchard-vm-backlight.ko" ]; then +if [ -f "$VM/orchard-vm-backlight.ko" ] && [ -f "$VM/orchard-vm-accel.ko" ]; then podman machine ssh "sudo $HERE/vm-support/install-linux.sh" else - echo 'No VM backlight (building it failed): Power will not dim the window.' >&2 + echo 'No VM modules (building them failed): Power will not dim the window and turning it will not rotate the desktop.' >&2 fi printf '%s\n' "$version" > "$VM/release" echo "Simulator disk ready: Omarchy A $version. Start it with simulator/run." diff --git a/simulator/vm b/simulator/vm index df0de740..ee058fb2 100755 --- a/simulator/vm +++ b/simulator/vm @@ -107,8 +107,9 @@ PY # The simulated device: its panel is fixed when the VM starts, so a # running VM restarts with the new one (same disk, same install). case "${2:-}" in ipad6|iphone7) ;; *) echo "Usage: $0 device {ipad6|iphone7}" >&2; exit 2;; esac - printf '%s\n' "$2" > "$D/device" - if running; then "$0" stop && "$0" start; fi + # Recorded only once the old VM is down, so a failed stop changes nothing. + if running; then "$0" stop; printf '%s\n' "$2" > "$D/device"; "$0" start + else printf '%s\n' "$2" > "$D/device"; fi echo "Device: $2" ;; *) echo "Usage: $0 {start|stop|kill|status|desktop gpu|software|orientation EDGE|device ipad6|iphone7}" >&2; exit 2;; From af32fcb63eb07e256a70ae57dab96e234b95a56b Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Fri, 20 Nov 2026 00:02:59 +0800 Subject: [PATCH 3/3] simulator: reconnect only when the VM goes away; follow a terminal device switch - The display bridge tells a guest that stops showing a surface (D-Bus Disable, connection open) apart from a closed connection: only the latter reconnects, so a Disable no longer starts a second connection. A closed connection's loop ends and frees what it held, so a VM restart leaves no thread behind. - When the guest's screen appears again, the window reads the VM's device file: a `simulator/vm device iphone7` from a terminal brings the iPhone's frame (and the Device menu's tick) with it. Assisted-by: LLM --- simulator/GuestDisplay.swift | 21 +++++++++++++++------ simulator/display/qemu-display.c | 26 ++++++++++++++++++++------ simulator/display/qemu-display.h | 5 ++++- simulator/main.swift | 27 +++++++++++++++++++++++++-- 4 files changed, 64 insertions(+), 15 deletions(-) diff --git a/simulator/GuestDisplay.swift b/simulator/GuestDisplay.swift index 2fe18dbc..e19f5634 100644 --- a/simulator/GuestDisplay.swift +++ b/simulator/GuestDisplay.swift @@ -39,12 +39,10 @@ final class GuestDisplay { Unmanaged.fromOpaque(context!).takeUnretainedValue().changed() }, disable: { context in - let display = Unmanaged.fromOpaque(context!).takeUnretainedValue() - display.lock.lock() - display.pixels = nil - display.lock.unlock() - DispatchQueue.main.async { display.onConnection?(false) } - DispatchQueue.main.asyncAfter(deadline: .now() + 1) { display.start() } + Unmanaged.fromOpaque(context!).takeUnretainedValue().lost(reconnect: false) + }, + closed: { context in + Unmanaged.fromOpaque(context!).takeUnretainedValue().lost(reconnect: true) }) if qd_start(self.qmpPath, callbacks, &error, Int32(error.count)) == 0 { DispatchQueue.main.async { self.connecting = false } @@ -55,6 +53,17 @@ final class GuestDisplay { } } + // No surface any more. A closed connection (the VM went away, a restart + // for another device, say) is followed by connecting to the next VM; a + // guest that only stopped showing one keeps its connection. + private func lost(reconnect: Bool) { + lock.lock() + pixels = nil + lock.unlock() + DispatchQueue.main.async { self.onConnection?(false) } + if reconnect { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.start() } } + } + // One redraw per main-loop turn, however many rectangles arrived. private func changed() { lock.lock() diff --git a/simulator/display/qemu-display.c b/simulator/display/qemu-display.c index bc119998..88cb6aef 100644 --- a/simulator/display/qemu-display.c +++ b/simulator/display/qemu-display.c @@ -148,10 +148,13 @@ static GVariant *get_property(GDBusConnection *connection, const char *sender, c static const GDBusInterfaceVTable LISTENER_VTABLE = {method_call, get_property, NULL}; +// The listener connection went away with QEMU: the viewer hears it first +// (so it lets go of the surface), then this connection's loop ends. static void closed(GDBusConnection *connection, gboolean vanished, GError *error, gpointer user) { display *d = user; - d->cb.disable(d->cb.context); + d->cb.closed(d->cb.context); + g_main_loop_quit(d->loop); } static GDBusConnection *connection_on(int fd, const char *guid, GDBusConnectionFlags flags, GError **error) @@ -199,7 +202,17 @@ static gpointer run_loop(gpointer user) job->done = TRUE; g_cond_signal(&job->ready); g_mutex_unlock(&job->lock); - if (connection) g_main_loop_run(d->loop); + if (!connection) return NULL; + g_main_loop_run(d->loop); + // Closed: nothing of this connection stays behind. + drop_surface(d); + g_object_unref(d->listener); + g_object_unref(d->qemu); + g_dbus_node_info_unref(d->info); + g_main_loop_unref(d->loop); + g_main_context_pop_thread_default(d->context); + g_main_context_unref(d->context); + g_free(d); return NULL; } @@ -298,12 +311,13 @@ int qd_start(const char *qmp_path, qd_callbacks callbacks, char *error, int erro g_mutex_lock(&job->lock); while (!job->done) g_cond_wait(&job->ready, &job->lock); g_mutex_unlock(&job->lock); + if (!d->listener) err = job->error; + g_mutex_clear(&job->lock); + g_cond_clear(&job->ready); + g_free(job); if (!reply) goto fail; g_variant_unref(reply); - if (!d->listener) { - err = job->error; - goto fail; - } + if (!d->listener) goto fail; return 0; fail: snprintf(error, error_size, "%s", err ? err->message : "D-Bus display failed"); diff --git a/simulator/display/qemu-display.h b/simulator/display/qemu-display.h index 37824e30..ac53b1ca 100644 --- a/simulator/display/qemu-display.h +++ b/simulator/display/qemu-display.h @@ -13,8 +13,11 @@ typedef struct { void (*scanout)(void *context, const void *pixels, uint32_t width, uint32_t height, uint32_t stride); // A rectangle of the current surface changed. void (*update)(void *context, int32_t x, int32_t y, int32_t width, int32_t height); - // The display went away (guest off, connection lost). + // The guest stopped showing a surface; the connection stays. void (*disable)(void *context); + // The connection is gone (QEMU quit or restarted). Its thread has ended; + // a viewer that wants the next VM calls qd_start again. + void (*closed)(void *context); } qd_callbacks; // Connects through the QMP socket at QMP_PATH (one QEMU started with diff --git a/simulator/main.swift b/simulator/main.swift index 13eba560..4797aa47 100644 --- a/simulator/main.swift +++ b/simulator/main.swift @@ -89,6 +89,9 @@ final class SimulatorView: NSView { private var hardware: VMHardware? private var brightness: CGFloat = 1 { didSet { screen.brightness = brightness } } private var guestConnected = false + // The guest's screen appeared (again): the VM may have restarted as + // another device. + var onConnect: (() -> Void)? private var guestPointerDown = false private var touchPosition = NSPoint.zero private(set) var touchMode = true @@ -172,6 +175,7 @@ final class SimulatorView: NSView { display.onFrame = { [weak self, weak display] in self?.screen.show(display?.image()) } display.onConnection = { [weak self] connected in guard let self else { return } + if connected && !self.guestConnected { self.onConnect?() } self.guestConnected = connected if !connected { self.releaseGuestInput() @@ -756,6 +760,16 @@ final class AppDelegate: NSObject, NSApplicationDelegate { } appMenu.addItem(.separator()) } + // A VM restarted as another device from a terminal + // (`simulator/vm device iphone7`): the window takes its frame. + view.onConnect = { [weak self, weak view] in + guard let self, let view else { return } + let name = (try? String(contentsOfFile: directory + "/device", encoding: .utf8))? + .trimmingCharacters(in: .whitespacesAndNewlines) ?? "ipad6" + guard name != view.profile.name else { return } + self.refit(view) { view.setDevice(DeviceProfile.named(name)) } + self.markDevice(name) + } // Upright to begin with, whatever the VM last felt. sendControl("orientation normal") } @@ -894,10 +908,19 @@ final class AppDelegate: NSObject, NSApplicationDelegate { return } self.refit(view) { view.setDevice(device) } - for entry in item.menu?.items ?? [] where entry.action == #selector(self.selectDevice(_:)) { - entry.state = entry === item ? .on : .off + self.markDevice(name) + } + } + + // The Device menu's tick on NAME. + private func markDevice(_ name: String) { + func walk(_ menu: NSMenu?) { + for entry in menu?.items ?? [] { + if entry.action == #selector(selectDevice(_:)) { entry.state = (entry.representedObject as? String) == name ? .on : .off } + walk(entry.submenu) } } + walk(NSApp.mainMenu) } // Resizes the window to the view's new canvas at the same zoom, about the