From c0c4100b9f3d6f72cde82496f43a941e215491a3 Mon Sep 17 00:00:00 2001 From: Giova Date: Wed, 11 Nov 2026 00:09:03 -0800 Subject: [PATCH] usb(macOS): connect a saved USB device when it is plugged in again A USB device set to "Auto connect on start" was only connected when the virtual machine started. When it was unplugged and plugged in again while the virtual machine ran, or restarted itself as a device does after a firmware update, UTM asked whether to connect it, or did nothing when the prompt is turned off or the window is not in front. Connect such a device when it appears, without asking. A device that another virtual machine has, or that this one has no room for, is left alone without an alert. The idea and a first version are from giovariot in #7928. Assisted-by: OpenCode:deepseek-v4.1-flash Assisted-by: Claude:claude-fable-5-2 --- .../VMDisplayQemuDisplayController.swift | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/Platform/macOS/Display/VMDisplayQemuDisplayController.swift b/Platform/macOS/Display/VMDisplayQemuDisplayController.swift index 1cf2b412ea..1030bb7840 100644 --- a/Platform/macOS/Display/VMDisplayQemuDisplayController.swift +++ b/Platform/macOS/Display/VMDisplayQemuDisplayController.swift @@ -328,14 +328,34 @@ extension VMDisplayQemuWindowController: CSUSBManagerDelegate { func spiceUsbManager(_ usbManager: CSUSBManager, deviceAttached device: CSUSBDevice) { logger.debug("USB device attached: \(device)") - if !isNoUsbPrompt { - Task { @MainActor in - if self.window!.isKeyWindow && self.vm.state == .started { + Task.detached { + // looking the device up reads its descriptors, which is kept off the main thread + let isAutoConnect = self.isAutoConnect(device) + await MainActor.run { + guard self.vm.state == .started else { + return + } + if isAutoConnect { + // also brings back a device that restarted itself, as one does after a firmware update + if usbManager.canRedirectUsbDevice(device, errorMessage: nil) && !usbManager.isUsbDeviceConnected(device) { + self.autoConnect(device, with: usbManager) + } + } else if !self.isNoUsbPrompt && self.window!.isKeyWindow { self.showConnectPrompt(for: device) } } } } + + /// Connect a device that is set to connect by itself + private func autoConnect(_ device: CSUSBDevice, with usbManager: CSUSBManager) { + withErrorAlert { + try await usbManager.connectUsbDeviceAfterUnmounting(device) + await MainActor.run { + self.primaryDisplayController.connectedUsbDevices.append(device) + } + } + } func spiceUsbManager(_ usbManager: CSUSBManager, deviceRemoved device: CSUSBDevice) { logger.debug("USB device removed: \(device)") @@ -516,12 +536,7 @@ extension VMDisplayQemuWindowController { } let filtered = devices.filter({ self.isAutoConnect($0) }) for device in filtered { - self.withErrorAlert { - try await usbManager.connectUsbDeviceAfterUnmounting(device) - await MainActor.run { - self.connectedUsbDevices.append(device) - } - } + self.autoConnect(device, with: usbManager) } } }