|
|
@@ -2,7 +2,7 @@
|
|
|
// Copyright (c) 2017 Péter Szilágyi. All rights reserved.
|
|
|
//
|
|
|
// This file is released under the 3-clause BSD license. Note however that Linux
|
|
|
-// support depends on libusb, released under GNU GPL 2.1 or later.
|
|
|
+// support depends on libusb, released under LGNU GPL 2.1 or later.
|
|
|
|
|
|
// +build linux,cgo darwin,!ios,cgo windows,cgo
|
|
|
|
|
|
@@ -48,6 +48,15 @@ import (
|
|
|
"unsafe"
|
|
|
)
|
|
|
|
|
|
+// enumerateLock is a mutex serializing access to USB device enumeration needed
|
|
|
+// by the macOS USB HID system calls, which require 2 consecutive method calls
|
|
|
+// for enumeration, causing crashes if called concurrently.
|
|
|
+//
|
|
|
+// For more details, see:
|
|
|
+// https://developer.apple.com/documentation/iokit/1438371-iohidmanagersetdevicematching
|
|
|
+// > "subsequent calls will cause the hid manager to release previously enumerated devices"
|
|
|
+var enumerateLock sync.Mutex
|
|
|
+
|
|
|
func init() {
|
|
|
// Initialize the HIDAPI library
|
|
|
C.hid_init()
|
|
|
@@ -66,6 +75,9 @@ func Supported() bool {
|
|
|
// - If the product id is set to 0 then any product matches.
|
|
|
// - If the vendor and product id are both 0, all HID devices are returned.
|
|
|
func Enumerate(vendorID uint16, productID uint16) []DeviceInfo {
|
|
|
+ enumerateLock.Lock()
|
|
|
+ defer enumerateLock.Unlock()
|
|
|
+
|
|
|
// Gather all device infos and ensure they are freed before returning
|
|
|
head := C.hid_enumerate(C.ushort(vendorID), C.ushort(productID))
|
|
|
if head == nil {
|