From 1f62ba9a957163595d48ba34bde1602a1562bc6c Mon Sep 17 00:00:00 2001 From: Stefano Cotta Ramusino Date: Sun, 28 Aug 2016 20:54:18 +0200 Subject: [PATCH 1/2] Set HIDD_ATTRIBUTES size with ULONG value according to definition --- hid_windows.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hid_windows.go b/hid_windows.go index 85b39dc..9c4c362 100644 --- a/hid_windows.go +++ b/hid_windows.go @@ -191,7 +191,7 @@ func ByPath(devicePath string) (*DeviceInfo, error) { } var attrs C.HIDD_ATTRIBUTES - attrs.Size = C.DWORD(unsafe.Sizeof(attrs)) + attrs.Size = C.ULONG(unsafe.Sizeof(attrs)) C.HidD_GetAttributes(dev.h(), &attrs) devInfo.VendorID = uint16(attrs.VendorID) From 91526b0a9f75526e5c74d69312dbc1960461e390 Mon Sep 17 00:00:00 2001 From: Justin Gallardo Date: Fri, 9 Mar 2018 15:05:52 -0800 Subject: [PATCH 2/2] Fix CF types for go 1.10 --- hid_darwin.go | 19 ++++++++++++------- hid_darwin_go110.go | 14 ++++++++++++++ hid_darwin_go19.go | 14 ++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 hid_darwin_go110.go create mode 100644 hid_darwin_go19.go diff --git a/hid_darwin.go b/hid_darwin.go index ccb5bff..17bed79 100644 --- a/hid_darwin.go +++ b/hid_darwin.go @@ -1,7 +1,7 @@ package hid /* -#cgo LDFLAGS: -L . -L/usr/local/lib -framework CoreFoundation -framework IOKit -fconstant-cfstrings +#cgo LDFLAGS: -L . -L/usr/local/lib -framework CoreFoundation -framework IOKit #include #include @@ -164,7 +164,7 @@ func cfstring(s string) C.CFStringRef { } func gostring(cfs C.CFStringRef) string { - if cfs == nil { + if cfs == nilCfStringRef { return "" } @@ -189,7 +189,7 @@ func gostring(cfs C.CFStringRef) string { func getIntProp(device C.IOHIDDeviceRef, key C.CFStringRef) int32 { var value int32 ref := C.IOHIDDeviceGetProperty(device, key) - if ref == nil { + if ref == nilCfTypeRef { return 0 } if C.CFGetTypeID(ref) != C.CFNumberGetTypeID() { @@ -213,12 +213,17 @@ func getPath(osDev C.IOHIDDeviceRef) string { } func iterateDevices(action func(device C.IOHIDDeviceRef) bool) cleanupDeviceManagerFn { - mgr := C.IOHIDManagerCreate(C.kCFAllocatorDefault, C.kIOHIDOptionsTypeNone) + var mgr C.IOHIDManagerRef + mgr = C.IOHIDManagerCreate(C.kCFAllocatorDefault, C.kIOHIDOptionsTypeNone) C.IOHIDManagerSetDeviceMatching(mgr, nil) C.IOHIDManagerOpen(mgr, C.kIOHIDOptionsTypeNone) - allDevicesSet := C.IOHIDManagerCopyDevices(mgr) - defer C.CFRelease(C.CFTypeRef(allDevicesSet)) + var allDevicesSet C.CFSetRef + allDevicesSet = C.IOHIDManagerCopyDevices(mgr) + if allDevicesSet == nilCfSetRef { + return func() {} + } + defer C.CFRelease((C.CFTypeRef)(allDevicesSet)) devCnt := C.CFSetGetCount(allDevicesSet) allDevices := make([]unsafe.Pointer, uint64(devCnt)) C.CFSetGetValues(allDevicesSet, &allDevices[0]) @@ -334,7 +339,7 @@ func (dev *osxDevice) close(disconnected bool) { delete(deviceCtx, dev.osDevice) deviceCtxMtx.Unlock() C.CFRelease(C.CFTypeRef(dev.osDevice)) - dev.osDevice = nil + dev.osDevice = nilIOHIDDeviceRef dev.closeDM() dev.disconnected = true } diff --git a/hid_darwin_go110.go b/hid_darwin_go110.go new file mode 100644 index 0000000..b86f52f --- /dev/null +++ b/hid_darwin_go110.go @@ -0,0 +1,14 @@ +// +build go1.10,darwin + +package hid + +/* +#include +#include +*/ +import "C" + +var nilCfStringRef C.CFStringRef= 0 +var nilCfTypeRef C.CFTypeRef = 0 +var nilCfSetRef C.CFSetRef = 0 +var nilIOHIDDeviceRef C.IOHIDDeviceRef = 0 diff --git a/hid_darwin_go19.go b/hid_darwin_go19.go new file mode 100644 index 0000000..123c1db --- /dev/null +++ b/hid_darwin_go19.go @@ -0,0 +1,14 @@ +// +build go1.9,!go1.10,darwin + +package hid + +/* +#include +#include + */ +import "C" + +var nilCfStringRef C.CFStringRef= nil +var nilCfTypeRef C.CFTypeRef = nil +var nilCfSetRef C.CFSetRef = nil +var nilIOHIDDeviceRef C.IOHIDDeviceRef = nil