|
| 1 | +package vz |
| 2 | + |
| 3 | +/* |
| 4 | +#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc |
| 5 | +#cgo darwin LDFLAGS: -lobjc -framework Foundation -framework Virtualization |
| 6 | +# include "virtualization_15.h" |
| 7 | +*/ |
| 8 | +import "C" |
| 9 | +import ( |
| 10 | + "runtime/cgo" |
| 11 | + "unsafe" |
| 12 | + |
| 13 | + "github.com/Code-Hex/vz/v3/internal/objc" |
| 14 | +) |
| 15 | + |
| 16 | +// NewUSBMassStorageDevice initialize the runtime USB Mass Storage device object. |
| 17 | +// |
| 18 | +// This is only supported on macOS 15 and newer, error will |
| 19 | +// be returned on older versions. |
| 20 | +func NewUSBMassStorageDevice(config *USBMassStorageDeviceConfiguration) (USBDevice, error) { |
| 21 | + if err := macOSAvailable(15); err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + ptr := C.newVZUSBMassStorageDeviceWithConfiguration(objc.Ptr(config)) |
| 25 | + return newUSBDevice(ptr), nil |
| 26 | +} |
| 27 | + |
| 28 | +// USBControllerConfiguration for a usb controller configuration. |
| 29 | +type USBControllerConfiguration interface { |
| 30 | + objc.NSObject |
| 31 | + |
| 32 | + usbControllerConfiguration() |
| 33 | +} |
| 34 | + |
| 35 | +type baseUSBControllerConfiguration struct{} |
| 36 | + |
| 37 | +func (*baseUSBControllerConfiguration) usbControllerConfiguration() {} |
| 38 | + |
| 39 | +// XHCIControllerConfiguration is a configuration of the USB XHCI controller. |
| 40 | +// |
| 41 | +// This configuration creates a USB XHCI controller device for the guest. |
| 42 | +// see: https://developer.apple.com/documentation/virtualization/vzxhcicontrollerconfiguration?language=objc |
| 43 | +type XHCIControllerConfiguration struct { |
| 44 | + *pointer |
| 45 | + |
| 46 | + *baseUSBControllerConfiguration |
| 47 | +} |
| 48 | + |
| 49 | +var _ USBControllerConfiguration = (*XHCIControllerConfiguration)(nil) |
| 50 | + |
| 51 | +// NewXHCIControllerConfiguration creates a new XHCIControllerConfiguration. |
| 52 | +// |
| 53 | +// This is only supported on macOS 15 and newer, error will |
| 54 | +// be returned on older versions. |
| 55 | +func NewXHCIControllerConfiguration() (*XHCIControllerConfiguration, error) { |
| 56 | + if err := macOSAvailable(15); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + config := &XHCIControllerConfiguration{ |
| 61 | + pointer: objc.NewPointer(C.newVZXHCIControllerConfiguration()), |
| 62 | + } |
| 63 | + |
| 64 | + objc.SetFinalizer(config, func(self *XHCIControllerConfiguration) { |
| 65 | + objc.Release(self) |
| 66 | + }) |
| 67 | + return config, nil |
| 68 | +} |
| 69 | + |
| 70 | +// USBController is representing a USB controller in a virtual machine. |
| 71 | +type USBController struct { |
| 72 | + dispatchQueue unsafe.Pointer |
| 73 | + *pointer |
| 74 | +} |
| 75 | + |
| 76 | +func newUSBController(ptr, dispatchQueue unsafe.Pointer) *USBController { |
| 77 | + return &USBController{ |
| 78 | + dispatchQueue: dispatchQueue, |
| 79 | + pointer: objc.NewPointer(ptr), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +//export usbAttachDetachCompletionHandler |
| 84 | +func usbAttachDetachCompletionHandler(cgoHandleUintptr C.uintptr_t, errPtr unsafe.Pointer) { |
| 85 | + cgoHandle := cgo.Handle(cgoHandleUintptr) |
| 86 | + |
| 87 | + handler := cgoHandle.Value().(func(error)) |
| 88 | + |
| 89 | + if err := newNSError(errPtr); err != nil { |
| 90 | + handler(err) |
| 91 | + } else { |
| 92 | + handler(nil) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Attach attaches a USB device. |
| 97 | +// |
| 98 | +// This is only supported on macOS 15 and newer, error will |
| 99 | +// be returned on older versions. |
| 100 | +// |
| 101 | +// If the device is successfully attached to the controller, it will appear in the usbDevices property, |
| 102 | +// its usbController property will be set to point to the USB controller that it is attached to |
| 103 | +// and completion handler will return nil. |
| 104 | +// If the device was previously attached to this or another USB controller, attach function will fail |
| 105 | +// with the `vz.ErrorDeviceAlreadyAttached`. If the device cannot be initialized correctly, attach |
| 106 | +// function will fail with `vz.ErrorDeviceInitializationFailure`. |
| 107 | +func (u *USBController) Attach(device USBDevice) error { |
| 108 | + if err := macOSAvailable(15); err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + h, errCh := makeHandler() |
| 112 | + handle := cgo.NewHandle(h) |
| 113 | + defer handle.Delete() |
| 114 | + C.attachDeviceVZUSBController( |
| 115 | + objc.Ptr(u), |
| 116 | + objc.Ptr(device), |
| 117 | + u.dispatchQueue, |
| 118 | + C.uintptr_t(handle), |
| 119 | + ) |
| 120 | + return <-errCh |
| 121 | +} |
| 122 | + |
| 123 | +// Detach detaches a USB device. |
| 124 | +// |
| 125 | +// This is only supported on macOS 15 and newer, error will |
| 126 | +// be returned on older versions. |
| 127 | +// |
| 128 | +// If the device is successfully detached from the controller, it will disappear from the usbDevices property, |
| 129 | +// its usbController property will be set to nil and completion handler will return nil. |
| 130 | +// If the device wasn't attached to the controller at the time of calling detach method, it will fail |
| 131 | +// with the `vz.ErrorDeviceNotFound` error. |
| 132 | +func (u *USBController) Detach(device USBDevice) error { |
| 133 | + if err := macOSAvailable(15); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + h, errCh := makeHandler() |
| 137 | + handle := cgo.NewHandle(h) |
| 138 | + defer handle.Delete() |
| 139 | + C.detachDeviceVZUSBController( |
| 140 | + objc.Ptr(u), |
| 141 | + objc.Ptr(device), |
| 142 | + u.dispatchQueue, |
| 143 | + C.uintptr_t(handle), |
| 144 | + ) |
| 145 | + return <-errCh |
| 146 | +} |
| 147 | + |
| 148 | +// USBDevices return a list of USB devices attached to controller. |
| 149 | +// |
| 150 | +// This is only supported on macOS 15 and newer, nil will |
| 151 | +// be returned on older versions. |
| 152 | +func (u *USBController) USBDevices() []USBDevice { |
| 153 | + if err := macOSAvailable(15); err != nil { |
| 154 | + return nil |
| 155 | + } |
| 156 | + nsArray := objc.NewNSArray( |
| 157 | + C.usbDevicesVZUSBController(objc.Ptr(u)), |
| 158 | + ) |
| 159 | + ptrs := nsArray.ToPointerSlice() |
| 160 | + usbDevices := make([]USBDevice, len(ptrs)) |
| 161 | + for i, ptr := range ptrs { |
| 162 | + usbDevices[i] = newUSBDevice(ptr) |
| 163 | + } |
| 164 | + return usbDevices |
| 165 | +} |
| 166 | + |
| 167 | +// USBDevice is an interface that represents a USB device in a VM. |
| 168 | +type USBDevice interface { |
| 169 | + objc.NSObject |
| 170 | + |
| 171 | + UUID() string |
| 172 | + |
| 173 | + usbDevice() |
| 174 | +} |
| 175 | + |
| 176 | +func newUSBDevice(ptr unsafe.Pointer) *usbDevice { |
| 177 | + return &usbDevice{ |
| 178 | + pointer: objc.NewPointer(ptr), |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +type usbDevice struct { |
| 183 | + *pointer |
| 184 | +} |
| 185 | + |
| 186 | +func (*usbDevice) usbDevice() {} |
| 187 | + |
| 188 | +var _ USBDevice = (*usbDevice)(nil) |
| 189 | + |
| 190 | +// UUID returns the device UUID. |
| 191 | +func (u *usbDevice) UUID() string { |
| 192 | + cs := (*char)(C.getUUIDUSBDevice(objc.Ptr(u))) |
| 193 | + return cs.String() |
| 194 | +} |
0 commit comments