CocoaSpice: update USB manager to support async

This commit is contained in:
osy 2023-09-04 19:39:33 -07:00
parent 8e820293bd
commit d1e547eec1
3 changed files with 47 additions and 46 deletions

View File

@ -293,17 +293,24 @@ extension VMSessionState: CSUSBManagerDelegate {
}
}
private func withUsbManagerSerialized<T>(_ task: @escaping () async -> T, onComplete: @escaping @MainActor (T) -> Void) {
private func withUsbManagerSerialized<T>(_ task: @escaping () async throws -> T, onSuccess: @escaping @MainActor (T) -> Void = { _ in }, onError: @escaping @MainActor (Error) -> Void = { _ in }) {
usbManagerQueue.async {
let event = DispatchSemaphore(value: 0)
Task.detached { [self] in
await MainActor.run {
isUsbBusy = true
}
let result = await task()
await MainActor.run {
isUsbBusy = false
onComplete(result)
do {
let result = try await task()
await MainActor.run {
isUsbBusy = false
onSuccess(result)
}
} catch {
await MainActor.run {
isUsbBusy = false
onError(error)
}
}
event.signal()
}
@ -323,7 +330,7 @@ extension VMSessionState: CSUSBManagerDelegate {
logger.debug("found device: \(name ?? "(unknown)")")
}
return devices
} onComplete: { devices in
} onSuccess: { devices in
self.allUsbDevices = devices
}
}
@ -338,13 +345,11 @@ extension VMSessionState: CSUSBManagerDelegate {
return
}
withUsbManagerSerialized {
await usbManager.connectUsbDevice(usbDevice)
} onComplete: { success, message in
if success {
self.connectedUsbDevices.append(usbDevice)
} else {
self.nonfatalError = message
}
try await usbManager.connectUsbDevice(usbDevice)
} onSuccess: {
self.connectedUsbDevices.append(usbDevice)
} onError: { error in
self.nonfatalError = error.localizedDescription
}
}
@ -358,12 +363,10 @@ extension VMSessionState: CSUSBManagerDelegate {
return
}
withUsbManagerSerialized {
await usbManager.disconnectUsbDevice(usbDevice)
} onComplete: { success, message in
if !success {
self.nonfatalError = message
}
self.connectedUsbDevices.removeAll(where: { $0 == usbDevice })
try await usbManager.disconnectUsbDevice(usbDevice)
} onError: { error in
self.nonfatalError = error.localizedDescription
}
}

View File

@ -352,15 +352,15 @@ extension VMDisplayQemuWindowController: CSUSBManagerDelegate {
guard response == .alertFirstButtonReturn else {
return
}
DispatchQueue.global(qos: .utility).async {
usbManager.connectUsbDevice(usbDevice) { (result, message) in
DispatchQueue.main.async {
if let msg = message {
self.showErrorAlert(msg)
}
if result {
self.connectedUsbDevices.append(usbDevice)
}
Task.detached {
do {
try await usbManager.connectUsbDevice(usbDevice)
await MainActor.run {
self.connectedUsbDevices.append(usbDevice)
}
} catch {
await MainActor.run {
self.showErrorAlert(error.localizedDescription)
}
}
}
@ -434,15 +434,15 @@ extension VMDisplayQemuWindowController {
return
}
let device = allUsbDevices[menu.tag]
DispatchQueue.global(qos: .utility).async {
usbManager.connectUsbDevice(device) { (result, message) in
DispatchQueue.main.async {
if let msg = message {
self.showErrorAlert(msg)
}
if result {
self.connectedUsbDevices.append(device)
}
Task.detached {
do {
try await usbManager.connectUsbDevice(device)
await MainActor.run {
self.connectedUsbDevices.append(device)
}
} catch {
await MainActor.run {
self.showErrorAlert(error.localizedDescription)
}
}
}
@ -458,15 +458,13 @@ extension VMDisplayQemuWindowController {
return
}
let device = allUsbDevices[menu.tag]
DispatchQueue.global(qos: .utility).async {
usbManager.disconnectUsbDevice(device) { (result, message) in
DispatchQueue.main.async {
if let msg = message {
self.showErrorAlert(msg)
}
if result {
self.connectedUsbDevices.removeAll(where: { $0 == device })
}
connectedUsbDevices.removeAll(where: { $0 == device })
Task.detached {
do {
try await usbManager.disconnectUsbDevice(device)
} catch {
await MainActor.run {
self.showErrorAlert(error.localizedDescription)
}
}
}

View File

@ -15,7 +15,7 @@
"location" : "https://github.com/utmapp/CocoaSpice.git",
"state" : {
"branch" : "visionos",
"revision" : "5405e085b9bc45ab13a862ddea350fdf660db86d"
"revision" : "4529c9686259e8d1e94d6253ad2e3a563fd1498d"
}
},
{