From c283c71daba0a5ce2695c674beedbb4dcfb8ac02 Mon Sep 17 00:00:00 2001 From: Dylan McKay Date: Thu, 15 Feb 2018 19:10:53 +1300 Subject: [PATCH 1/2] [Mac] Set a valid interface number on hid_device_info for USB HID devices Previously the interface would never be set on Mac. This presents a big pain because retrieving interface numbers can be the only way to distinguish between the interfaces returned by HIDAPI. This change makes it possible to retrieve interface number from an hid_device_info on Mac for USB HID devices only. It is unclear if the Mac OS IOKit library returns valid interface numbers for non-HID USB devices. Because of this, I have opted to simply skip that case - leave it initialised to `-1`. In the future, we can easily relax this restriction if it turns out IOKit correctly returns interface number with non-HID USB devices. For now, this PR brings 90% of the value at 5% of the risk. --- hidapi/hidapi.h | 10 +++++++--- mac/hid.c | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/hidapi/hidapi.h b/hidapi/hidapi.h index e5bc2dc4..6f15309c 100644 --- a/hidapi/hidapi.h +++ b/hidapi/hidapi.h @@ -69,9 +69,13 @@ extern "C" { (Windows/Mac only).*/ unsigned short usage; /** The USB interface which this logical device - represents. Valid on both Linux implementations - in all cases, and valid on the Windows implementation - only if the device contains more than one interface. */ + represents. + + * Valid on both Linux implementations in all cases + * Valid on the Windows implementation only if the device + contains more than one interface + * Valid on the Mac implementation if and only if the device + * is a USB HID device. */ int interface_number; /** Pointer to the next device */ diff --git a/mac/hid.c b/mac/hid.c index e0756a15..dd3f4529 100644 --- a/mac/hid.c +++ b/mac/hid.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -432,6 +433,7 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, if ((vendor_id == 0x0 || vendor_id == dev_vid) && (product_id == 0x0 || product_id == dev_pid)) { struct hid_device_info *tmp; + bool is_usb_hid; /* Is this an actual HID usb device */ io_object_t iokit_dev; kern_return_t res; io_string_t path; @@ -446,6 +448,8 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, } cur_dev = tmp; + is_usb_hid = get_int_property(dev, CFSTR(kUSBInterfaceClass)) == kUSBHIDClass; + /* Get the Usage Page and Usage for this device. */ cur_dev->usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey)); cur_dev->usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey)); @@ -479,7 +483,16 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, cur_dev->release_number = get_int_property(dev, CFSTR(kIOHIDVersionNumberKey)); /* Interface Number (Unsupported on Mac)*/ - cur_dev->interface_number = -1; + + /* We can only retrieve the interface number for USB HID devices. + * IOKit always seems to return 0 when querying a standard USB device + * for its interface. */ + if (is_usb_hid) { + /* Get the interface number */ + cur_dev->interface_number = get_int_property(dev, CFSTR(kUSBInterfaceNumber)); + } else { + cur_dev->interface_number = -1; + } } } From 2357fb5519534d90788ab02101f963cdb421c111 Mon Sep 17 00:00:00 2001 From: Dylan McKay Date: Thu, 15 Feb 2018 19:10:53 +1300 Subject: [PATCH 2/2] [Mac] Set a valid interface number on hid_device_info for USB HID devices Previously the interface would never be set on Mac. This presents a big pain because retrieving interface numbers can be the only way to distinguish between the interfaces returned by HIDAPI. This change makes it possible to retrieve interface number from an hid_device_info on Mac for USB HID devices only. It is unclear if the Mac OS IOKit library returns valid interface numbers for non-HID USB devices. Because of this, I have opted to simply skip that case - leave it initialised to `-1`. In the future, we can easily relax this restriction if it turns out IOKit correctly returns interface number with non-HID USB devices. For now, this PR brings 90% of the value at 5% of the risk. --- a | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 a diff --git a/a b/a new file mode 100644 index 00000000..e69de29b