Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Fix: macOS ImageIO SIGBUS crash fixes (#205)

Luka23cf4eadbc05
diff --git a/sources/vphone-cli/VPhoneKeyHelper.swift b/sources/vphone-cli/VPhoneKeyHelper.swift
index 8e1c0ca4..c5350468 100644
--- a/sources/vphone-cli/VPhoneKeyHelper.swift
+++ b/sources/vphone-cli/VPhoneKeyHelper.swift
@@ -26,17 +26,34 @@ class VPhoneKeyHelper {
private func requireConnection() -> Bool {
if control.isConnected { return true }
- let alert = NSAlert()
- alert.messageText = "vphoned Not Connected"
- alert.informativeText =
- "The guest agent is not connected. Key injection requires vphoned running inside the VM."
- alert.alertStyle = .warning
- alert.addButton(withTitle: "OK")
- if let window {
- alert.beginSheetModal(for: window)
- } else {
- alert.runModal()
- }
+
+ let panel = NSPanel(
+ contentRect: NSRect(x: 0, y: 0, width: 380, height: 110),
+ styleMask: [.titled, .closable],
+ backing: .buffered,
+ defer: false
+ )
+ panel.title = "vphoned Not Connected"
+ panel.center()
+
+ let msg = NSTextField(labelWithString: "The guest agent is not connected. Key injection requires vphoned running inside the VM.")
+ msg.frame = NSRect(x: 20, y: 50, width: 340, height: 44)
+ msg.lineBreakMode = .byWordWrapping
+ msg.maximumNumberOfLines = 3
+
+ let ok = NSButton(frame: NSRect(x: 280, y: 12, width: 80, height: 28))
+ ok.title = "OK"
+ ok.bezelStyle = .rounded
+ ok.keyEquivalent = "\r"
+ ok.target = NSApp
+ ok.action = #selector(NSApplication.stopModal(withCode:))
+
+ panel.contentView?.addSubview(msg)
+ panel.contentView?.addSubview(ok)
+
+ NSApp.runModal(for: panel)
+ panel.orderOut(nil)
+
return false
}
diff --git a/sources/vphone-cli/VPhoneMenuApps.swift b/sources/vphone-cli/VPhoneMenuApps.swift
index 60df342c..267d4dff 100644
--- a/sources/vphone-cli/VPhoneMenuApps.swift
+++ b/sources/vphone-cli/VPhoneMenuApps.swift
@@ -84,20 +84,45 @@ extension VPhoneMenuController {
}
@objc func openURL() {
- let alert = NSAlert()
- alert.messageText = "Open URL"
- alert.informativeText = "Enter URL to open on the guest:"
- alert.alertStyle = .informational
- alert.addButton(withTitle: "Open")
- alert.addButton(withTitle: "Cancel")
-
- let input = NSTextField(frame: NSRect(x: 0, y: 0, width: 400, height: 24))
- input.placeholderString = "https://example.com"
- alert.accessoryView = input
-
- guard alert.runModal() == .alertFirstButtonReturn else { return }
- let url = input.stringValue
- guard !url.isEmpty else { return }
+ let panel = NSPanel(
+ contentRect: NSRect(x: 0, y: 0, width: 420, height: 110),
+ styleMask: [.titled, .closable],
+ backing: .buffered,
+ defer: false
+ )
+ panel.title = "Open URL"
+ panel.center()
+
+ let lbl = NSTextField(labelWithString: "Enter URL to open on the guest:")
+ lbl.frame = NSRect(x: 20, y: 70, width: 380, height: 20)
+
+ let field = NSTextField(frame: NSRect(x: 20, y: 42, width: 380, height: 24))
+ field.placeholderString = "https://example.com"
+
+ let ok = NSButton(frame: NSRect(x: 310, y: 10, width: 90, height: 28))
+ ok.title = "Open"
+ ok.bezelStyle = .rounded
+ ok.keyEquivalent = "\r"
+ ok.target = self
+ ok.action = #selector(VPhoneMenuController.confirmModal)
+
+ let cancel = NSButton(frame: NSRect(x: 210, y: 10, width: 90, height: 28))
+ cancel.title = "Cancel"
+ cancel.bezelStyle = .rounded
+ cancel.keyEquivalent = "\u{1b}"
+ cancel.target = NSApp
+ cancel.action = #selector(NSApplication.abortModal)
+
+ panel.contentView?.addSubview(lbl)
+ panel.contentView?.addSubview(field)
+ panel.contentView?.addSubview(ok)
+ panel.contentView?.addSubview(cancel)
+
+ let response = NSApp.runModal(for: panel)
+ panel.orderOut(nil)
+
+ guard response == .OK, !field.stringValue.isEmpty else { return }
+ let url = field.stringValue
Task {
do {
@@ -108,4 +133,8 @@ extension VPhoneMenuController {
}
}
}
+
+ @objc func confirmModal() {
+ NSApp.stopModal(withCode: .OK)
+ }
}
diff --git a/sources/vphone-cli/VPhoneMenuConnect.swift b/sources/vphone-cli/VPhoneMenuConnect.swift
index 56a2bb35..78d22b64 100644
--- a/sources/vphone-cli/VPhoneMenuConnect.swift
+++ b/sources/vphone-cli/VPhoneMenuConnect.swift
@@ -155,20 +155,45 @@ extension VPhoneMenuController {
}
@objc func setClipboardText() {
- let alert = NSAlert()
- alert.messageText = "Set Clipboard Text"
- alert.informativeText = "Enter text to set on the guest clipboard:"
- alert.alertStyle = .informational
- alert.addButton(withTitle: "Set")
- alert.addButton(withTitle: "Cancel")
-
- let input = NSTextField(frame: NSRect(x: 0, y: 0, width: 300, height: 80))
- input.placeholderString = "Text to copy to clipboard"
- alert.accessoryView = input
-
- guard alert.runModal() == .alertFirstButtonReturn else { return }
- let text = input.stringValue
- guard !text.isEmpty else { return }
+ let panel = NSPanel(
+ contentRect: NSRect(x: 0, y: 0, width: 420, height: 150),
+ styleMask: [.titled, .closable],
+ backing: .buffered,
+ defer: false
+ )
+ panel.title = "Set Clipboard Text"
+ panel.center()
+
+ let lbl = NSTextField(labelWithString: "Enter text to set on the guest clipboard:")
+ lbl.frame = NSRect(x: 20, y: 110, width: 380, height: 20)
+
+ let field = NSTextField(frame: NSRect(x: 20, y: 50, width: 380, height: 50))
+ field.placeholderString = "Text to copy to clipboard"
+
+ let ok = NSButton(frame: NSRect(x: 310, y: 12, width: 90, height: 28))
+ ok.title = "Set"
+ ok.bezelStyle = .rounded
+ ok.keyEquivalent = "\r"
+ ok.target = self
+ ok.action = #selector(VPhoneMenuController.confirmModal)
+
+ let cancel = NSButton(frame: NSRect(x: 210, y: 12, width: 90, height: 28))
+ cancel.title = "Cancel"
+ cancel.bezelStyle = .rounded
+ cancel.keyEquivalent = "\u{1b}"
+ cancel.target = NSApp
+ cancel.action = #selector(NSApplication.abortModal)
+
+ panel.contentView?.addSubview(lbl)
+ panel.contentView?.addSubview(field)
+ panel.contentView?.addSubview(ok)
+ panel.contentView?.addSubview(cancel)
+
+ let response = NSApp.runModal(for: panel)
+ panel.orderOut(nil)
+
+ guard response == .OK, !field.stringValue.isEmpty else { return }
+ let text = field.stringValue
Task {
do {
@@ -183,32 +208,50 @@ extension VPhoneMenuController {
// MARK: - Settings
@objc func readSetting() {
- let alert = NSAlert()
- alert.messageText = "Read Setting"
- alert.informativeText = "Enter preference domain and key:"
- alert.alertStyle = .informational
- alert.addButton(withTitle: "Read")
- alert.addButton(withTitle: "Cancel")
-
- let stack = NSStackView(frame: NSRect(x: 0, y: 0, width: 350, height: 56))
- stack.orientation = .vertical
- stack.spacing = 8
-
- let domainField = NSTextField(frame: .zero)
+ let panel = NSPanel(
+ contentRect: NSRect(x: 0, y: 0, width: 420, height: 160),
+ styleMask: [.titled, .closable],
+ backing: .buffered,
+ defer: false
+ )
+ panel.title = "Read Setting"
+ panel.center()
+
+ let lbl1 = NSTextField(labelWithString: "Domain:")
+ lbl1.frame = NSRect(x: 20, y: 118, width: 380, height: 18)
+ let domainField = NSTextField(frame: NSRect(x: 20, y: 92, width: 380, height: 22))
domainField.placeholderString = "com.apple.springboard"
- domainField.translatesAutoresizingMaskIntoConstraints = false
- domainField.widthAnchor.constraint(equalToConstant: 350).isActive = true
- let keyField = NSTextField(frame: .zero)
+ let lbl2 = NSTextField(labelWithString: "Key (leave empty for all keys):")
+ lbl2.frame = NSRect(x: 20, y: 68, width: 380, height: 18)
+ let keyField = NSTextField(frame: NSRect(x: 20, y: 42, width: 380, height: 22))
keyField.placeholderString = "Key (leave empty for all keys)"
- keyField.translatesAutoresizingMaskIntoConstraints = false
- keyField.widthAnchor.constraint(equalToConstant: 350).isActive = true
-
- stack.addArrangedSubview(domainField)
- stack.addArrangedSubview(keyField)
- alert.accessoryView = stack
- guard alert.runModal() == .alertFirstButtonReturn else { return }
+ let ok = NSButton(frame: NSRect(x: 310, y: 10, width: 90, height: 28))
+ ok.title = "Read"
+ ok.bezelStyle = .rounded
+ ok.keyEquivalent = "\r"
+ ok.target = self
+ ok.action = #selector(VPhoneMenuController.confirmModal)
+
+ let cancel = NSButton(frame: NSRect(x: 210, y: 10, width: 90, height: 28))
+ cancel.title = "Cancel"
+ cancel.bezelStyle = .rounded
+ cancel.keyEquivalent = "\u{1b}"
+ cancel.target = NSApp
+ cancel.action = #selector(NSApplication.abortModal)
+
+ panel.contentView?.addSubview(lbl1)
+ panel.contentView?.addSubview(domainField)
+ panel.contentView?.addSubview(lbl2)
+ panel.contentView?.addSubview(keyField)
+ panel.contentView?.addSubview(ok)
+ panel.contentView?.addSubview(cancel)
+
+ let response = NSApp.runModal(for: panel)
+ panel.orderOut(nil)
+
+ guard response == .OK else { return }
let domain = domainField.stringValue
guard !domain.isEmpty else { return }
let key: String? = keyField.stringValue.isEmpty ? nil : keyField.stringValue
@@ -238,44 +281,64 @@ extension VPhoneMenuController {
}
@objc func writeSetting() {
- let alert = NSAlert()
- alert.messageText = "Write Setting"
- alert.informativeText = "Enter preference domain, key, type, and value:"
- alert.alertStyle = .informational
- alert.addButton(withTitle: "Write")
- alert.addButton(withTitle: "Cancel")
-
- let stack = NSStackView(frame: NSRect(x: 0, y: 0, width: 350, height: 116))
- stack.orientation = .vertical
- stack.spacing = 8
-
- let domainField = NSTextField(frame: .zero)
+ let panel = NSPanel(
+ contentRect: NSRect(x: 0, y: 0, width: 420, height: 240),
+ styleMask: [.titled, .closable],
+ backing: .buffered,
+ defer: false
+ )
+ panel.title = "Write Setting"
+ panel.center()
+
+ let lbl1 = NSTextField(labelWithString: "Domain:")
+ lbl1.frame = NSRect(x: 20, y: 198, width: 380, height: 18)
+ let domainField = NSTextField(frame: NSRect(x: 20, y: 172, width: 380, height: 22))
domainField.placeholderString = "com.apple.springboard"
- domainField.translatesAutoresizingMaskIntoConstraints = false
- domainField.widthAnchor.constraint(equalToConstant: 350).isActive = true
- let keyField = NSTextField(frame: .zero)
+ let lbl2 = NSTextField(labelWithString: "Key:")
+ lbl2.frame = NSRect(x: 20, y: 148, width: 380, height: 18)
+ let keyField = NSTextField(frame: NSRect(x: 20, y: 122, width: 380, height: 22))
keyField.placeholderString = "Key"
- keyField.translatesAutoresizingMaskIntoConstraints = false
- keyField.widthAnchor.constraint(equalToConstant: 350).isActive = true
- let typeField = NSTextField(frame: .zero)
+ let lbl3 = NSTextField(labelWithString: "Type:")
+ lbl3.frame = NSRect(x: 20, y: 98, width: 380, height: 18)
+ let typeField = NSTextField(frame: NSRect(x: 20, y: 72, width: 380, height: 22))
typeField.placeholderString = "Type: boolean | string | integer | float"
- typeField.translatesAutoresizingMaskIntoConstraints = false
- typeField.widthAnchor.constraint(equalToConstant: 350).isActive = true
- let valueField = NSTextField(frame: .zero)
+ let lbl4 = NSTextField(labelWithString: "Value:")
+ lbl4.frame = NSRect(x: 20, y: 48, width: 380, height: 18)
+ let valueField = NSTextField(frame: NSRect(x: 20, y: 42, width: 220, height: 22))
valueField.placeholderString = "Value"
- valueField.translatesAutoresizingMaskIntoConstraints = false
- valueField.widthAnchor.constraint(equalToConstant: 350).isActive = true
- stack.addArrangedSubview(domainField)
- stack.addArrangedSubview(keyField)
- stack.addArrangedSubview(typeField)
- stack.addArrangedSubview(valueField)
- alert.accessoryView = stack
-
- guard alert.runModal() == .alertFirstButtonReturn else { return }
+ let ok = NSButton(frame: NSRect(x: 310, y: 10, width: 90, height: 28))
+ ok.title = "Write"
+ ok.bezelStyle = .rounded
+ ok.keyEquivalent = "\r"
+ ok.target = self
+ ok.action = #selector(VPhoneMenuController.confirmModal)
+
+ let cancel = NSButton(frame: NSRect(x: 210, y: 10, width: 90, height: 28))
+ cancel.title = "Cancel"
+ cancel.bezelStyle = .rounded
+ cancel.keyEquivalent = "\u{1b}"
+ cancel.target = NSApp
+ cancel.action = #selector(NSApplication.abortModal)
+
+ panel.contentView?.addSubview(lbl1)
+ panel.contentView?.addSubview(domainField)
+ panel.contentView?.addSubview(lbl2)
+ panel.contentView?.addSubview(keyField)
+ panel.contentView?.addSubview(lbl3)
+ panel.contentView?.addSubview(typeField)
+ panel.contentView?.addSubview(lbl4)
+ panel.contentView?.addSubview(valueField)
+ panel.contentView?.addSubview(ok)
+ panel.contentView?.addSubview(cancel)
+
+ let response = NSApp.runModal(for: panel)
+ panel.orderOut(nil)
+
+ guard response == .OK else { return }
let domain = domainField.stringValue
let key = keyField.stringValue
let type = typeField.stringValue
@@ -312,14 +375,31 @@ extension VPhoneMenuController {
// MARK: - Alert
func showAlert(title: String, message: String, style: NSAlert.Style) {
- let alert = NSAlert()
- alert.messageText = title
- alert.informativeText = message
- alert.alertStyle = style
- if let window = NSApp.keyWindow {
- alert.beginSheetModal(for: window)
- } else {
- alert.runModal()
- }
+ let panel = NSPanel(
+ contentRect: NSRect(x: 0, y: 0, width: 380, height: 120),
+ styleMask: [.titled, .closable],
+ backing: .buffered,
+ defer: false
+ )
+ panel.title = title
+ panel.center()
+
+ let msg = NSTextField(labelWithString: message)
+ msg.frame = NSRect(x: 20, y: 50, width: 340, height: 50)
+ msg.lineBreakMode = .byWordWrapping
+ msg.maximumNumberOfLines = 3
+
+ let ok = NSButton(frame: NSRect(x: 280, y: 12, width: 80, height: 28))
+ ok.title = "OK"
+ ok.bezelStyle = .rounded
+ ok.keyEquivalent = "\r"
+ ok.target = NSApp
+ ok.action = #selector(NSApplication.stopModal(withCode:))
+
+ panel.contentView?.addSubview(msg)
+ panel.contentView?.addSubview(ok)
+
+ NSApp.runModal(for: panel)
+ panel.orderOut(nil)
}
}
diff --git a/sources/vphone-cli/VPhoneMenuRecord.swift b/sources/vphone-cli/VPhoneMenuRecord.swift
index 710006fb..100d6189 100644
--- a/sources/vphone-cli/VPhoneMenuRecord.swift
+++ b/sources/vphone-cli/VPhoneMenuRecord.swift
@@ -79,24 +79,59 @@ extension VPhoneMenuController {
}
private func showRecordingSavedAlert(url: URL) {
- let alert = NSAlert()
- alert.messageText = "Recording"
- alert.informativeText = "Saved to \(url.path)"
- alert.alertStyle = .informational
- alert.addButton(withTitle: "Reveal in Finder")
- alert.addButton(withTitle: "OK")
-
- if let window = NSApp.keyWindow {
- alert.beginSheetModal(for: window) { response in
- if response == .alertFirstButtonReturn {
- NSWorkspace.shared.activateFileViewerSelecting([url])
- }
- }
- } else {
- let response = alert.runModal()
- if response == .alertFirstButtonReturn {
- NSWorkspace.shared.activateFileViewerSelecting([url])
- }
+ let panel = NSPanel(
+ contentRect: NSRect(x: 0, y: 0, width: 420, height: 110),
+ styleMask: [.titled, .closable],
+ backing: .buffered,
+ defer: false
+ )
+ panel.title = "Recording"
+ panel.center()
+
+ let msg = NSTextField(labelWithString: "Saved to \(url.path)")
+ msg.frame = NSRect(x: 20, y: 60, width: 380, height: 30)
+ msg.lineBreakMode = .byTruncatingMiddle
+
+ let reveal = NSButton(frame: NSRect(x: 20, y: 12, width: 150, height: 28))
+ reveal.title = "Reveal in Finder"
+ reveal.bezelStyle = .rounded
+
+ let ok = NSButton(frame: NSRect(x: 310, y: 12, width: 90, height: 28))
+ ok.title = "OK"
+ ok.bezelStyle = .rounded
+ ok.keyEquivalent = "\r"
+ ok.target = self
+ ok.action = #selector(VPhoneMenuController.confirmModal)
+
+ panel.contentView?.addSubview(msg)
+ panel.contentView?.addSubview(reveal)
+ panel.contentView?.addSubview(ok)
+
+ var shouldReveal = false
+ reveal.target = nil
+ reveal.action = nil
+
+ let capturedURL = url
+ reveal.target = NSApp
+ reveal.action = #selector(NSApplication.stopModal(withCode:))
+
+ // Use a custom approach: reveal button stops modal with code 100
+ class RevealHelper: NSObject {
+ var action: () -> Void
+ init(_ action: @escaping () -> Void) { self.action = action }
+ @objc func clicked() { action() }
+ }
+ let helper = RevealHelper {
+ NSApp.stopModal(withCode: NSApplication.ModalResponse(rawValue: 100))
+ }
+ reveal.target = helper
+ reveal.action = #selector(RevealHelper.clicked)
+
+ let response = NSApp.runModal(for: panel)
+ panel.orderOut(nil)
+
+ if response.rawValue == 100 {
+ NSWorkspace.shared.activateFileViewerSelecting([capturedURL])
}
}
}
diff --git a/sources/vphone-cli/VPhoneScreenRecorder.swift b/sources/vphone-cli/VPhoneScreenRecorder.swift
index 7ed03ba8..cca2bc4f 100644
--- a/sources/vphone-cli/VPhoneScreenRecorder.swift
+++ b/sources/vphone-cli/VPhoneScreenRecorder.swift
@@ -1,4 +1,5 @@
import AppKit
+import ImageIO
import AVFoundation
import CoreVideo
import ObjectiveC.runtime
@@ -11,7 +12,7 @@ class VPhoneScreenRecorder {
private enum CaptureError: LocalizedError {
case captureFailed
case clipboardWriteFailed
- case pngEncodingFailed
+ case encodingFailed
var errorDescription: String? {
switch self {
@@ -19,7 +20,7 @@ class VPhoneScreenRecorder {
"Failed to capture a frame from the virtual machine."
case .clipboardWriteFailed:
"Failed to copy the screenshot to the pasteboard."
- case .pngEncodingFailed:
+ case .encodingFailed:
"Failed to encode the screenshot as PNG."
}
}
@@ -130,29 +131,37 @@ class VPhoneScreenRecorder {
func copyScreenshotToPasteboard(view: NSView) async throws {
let cgImage = try await captureStillImage(from: view)
- let image = NSImage(
- cgImage: cgImage,
- size: NSSize(width: cgImage.width, height: cgImage.height)
- )
- let pasteboard = NSPasteboard.general
- pasteboard.clearContents()
- guard pasteboard.writeObjects([image]) else {
+ let data = NSMutableData()
+ guard let dest = CGImageDestinationCreateWithData(data, "public.jpeg" as CFString, 1, nil) else {
throw CaptureError.clipboardWriteFailed
}
+ CGImageDestinationAddImage(dest, cgImage, nil)
+ guard CGImageDestinationFinalize(dest) else {
+ throw CaptureError.clipboardWriteFailed
+ }
+
+ let pasteboard = NSPasteboard.general
+ pasteboard.clearContents()
+ pasteboard.setData(data as Data, forType: .init("public.jpeg"))
print("[record] screenshot copied to clipboard")
}
func saveScreenshot(view: NSView) async throws -> URL {
let cgImage = try await captureStillImage(from: view)
- let bitmap = NSBitmapImageRep(cgImage: cgImage)
- guard let pngData = bitmap.representation(using: .png, properties: [:]) else {
- throw CaptureError.pngEncodingFailed
+ let url = screenshotOutputURL()
+
+ guard let dest = CGImageDestinationCreateWithURL(
+ url as CFURL, "public.jpeg" as CFString, 1, nil
+ ) else {
+ throw CaptureError.encodingFailed
+ }
+ CGImageDestinationAddImage(dest, cgImage, nil)
+ guard CGImageDestinationFinalize(dest) else {
+ throw CaptureError.encodingFailed
}
- let url = screenshotOutputURL()
- try pngData.write(to: url, options: .atomic)
print("[record] screenshot saved - \(url.path)")
return url
}
@@ -300,7 +309,7 @@ class VPhoneScreenRecorder {
}
private func screenshotOutputURL() -> URL {
- desktopDirectory().appendingPathComponent("vphone-screenshot-\(timestampString()).png")
+ desktopDirectory().appendingPathComponent("vphone-screenshot-\(timestampString()).jpg")
}
private func timestampString() -> String {