Skip to content

Commit

Permalink
[Mac] Set a valid interface number on hid_device_info for USB HID dev…
Browse files Browse the repository at this point in the history
…ices

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.
  • Loading branch information
dylanmckay committed Feb 15, 2018
1 parent a6a622f commit ef1a735
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions hidapi/hidapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
12 changes: 12 additions & 0 deletions mac/hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <IOKit/hid/IOHIDManager.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/USBSpec.h>
#include <CoreFoundation/CoreFoundation.h>
#include <wchar.h>
#include <locale.h>
Expand Down Expand Up @@ -450,6 +451,17 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
cur_dev->usage_page = get_int_property(dev, CFSTR(kIOHIDPrimaryUsagePageKey));
cur_dev->usage = get_int_property(dev, CFSTR(kIOHIDPrimaryUsageKey));

int device_class = get_int_property(dev, CFSTR(kUSBInterfaceClass));
bool is_hid = device_class == kUSBHIDClass;

// 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_hid) {
/* Get the interface number */
cur_dev->interface_number = get_int_property(dev, CFSTR(kUSBInterfaceNumber));
}

/* Fill out the record */
cur_dev->next = NULL;

Expand Down

0 comments on commit ef1a735

Please sign in to comment.