Skip to content

Commit ef1a735

Browse files
committed
[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. Mac OS' IOKit library always returns interface number 0 UNLESS the device is an HID device. If we know we have an HID device, we can safely forward the interface number onto HIDAPI users. This change makes it possible to retrieve interface number from an hid_device_info on Mac for USB HID devices only.
1 parent a6a622f commit ef1a735

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

hidapi/hidapi.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ extern "C" {
6969
(Windows/Mac only).*/
7070
unsigned short usage;
7171
/** The USB interface which this logical device
72-
represents. Valid on both Linux implementations
73-
in all cases, and valid on the Windows implementation
74-
only if the device contains more than one interface. */
72+
represents.
73+
74+
* Valid on both Linux implementations in all cases
75+
* Valid on the Windows implementation only if the device
76+
contains more than one interface
77+
* Valid on the Mac implementation if and only if the device
78+
* is a USB HID device. */
7579
int interface_number;
7680

7781
/** Pointer to the next device */

mac/hid.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <IOKit/hid/IOHIDManager.h>
2626
#include <IOKit/hid/IOHIDKeys.h>
2727
#include <IOKit/IOKitLib.h>
28+
#include <IOKit/usb/USBSpec.h>
2829
#include <CoreFoundation/CoreFoundation.h>
2930
#include <wchar.h>
3031
#include <locale.h>
@@ -450,6 +451,17 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
450451
cur_dev->usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey));
451452
cur_dev->usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey));
452453

454+
int device_class = get_int_property(dev, CFSTR(kUSBInterfaceClass));
455+
bool is_hid = device_class == kUSBHIDClass;
456+
457+
// We can only retrieve the interface number for USB HID devices.
458+
// IOKit always seems to return 0 when querying a standard USB device
459+
// for its interface.
460+
if (is_hid) {
461+
/* Get the interface number */
462+
cur_dev->interface_number = get_int_property(dev, CFSTR(kUSBInterfaceNumber));
463+
}
464+
453465
/* Fill out the record */
454466
cur_dev->next = NULL;
455467

0 commit comments

Comments
 (0)