@@ -482,6 +482,9 @@ struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id,
482482
483483 /* Release Number */
484484 cur_dev -> release_number = get_int_property (dev , CFSTR (kIOHIDVersionNumberKey ));
485+
486+ /* store the current IOHIDDeviceRef */
487+ cur_dev -> dev_ref = dev ;
485488
486489 /* We can only retrieve the interface number for USB HID devices.
487490 * IOKit always seems to return 0 when querying a standard USB device
@@ -521,6 +524,7 @@ hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short pr
521524 /* This function is identical to the Linux version. Platform independent. */
522525 struct hid_device_info * devs , * cur_dev ;
523526 const char * path_to_open = NULL ;
527+ IOHIDDeviceRef dev_to_open = NULL ;
524528 hid_device * handle = NULL ;
525529
526530 devs = hid_enumerate (vendor_id , product_id );
@@ -531,10 +535,16 @@ hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short pr
531535 if (serial_number ) {
532536 if (wcscmp (serial_number , cur_dev -> serial_number ) == 0 ) {
533537 path_to_open = cur_dev -> path ;
538+ // when there is a serial number, use the reference instead of the path
539+ dev_to_open = cur_dev -> dev_ref ;
534540 break ;
535541 }
542+ else {
543+ // serial number doesn't match, keep iterating
544+ }
536545 }
537546 else {
547+ // there was no serial number argument, open by path
538548 path_to_open = cur_dev -> path ;
539549 break ;
540550 }
@@ -543,9 +553,12 @@ hid_device * HID_API_EXPORT hid_open(unsigned short vendor_id, unsigned short pr
543553 }
544554
545555 if (path_to_open ) {
546- /* Open the device */
556+ /* Open the device using the path */
547557 handle = hid_open_path (path_to_open );
548558 }
559+ else if (dev_to_open ) {
560+ handle = hid_open_ref (dev_to_open );
561+ }
549562
550563 hid_free_enumeration (devs );
551564
@@ -761,6 +774,76 @@ hid_device * HID_API_EXPORT hid_open_path(const char *path)
761774 return NULL ;
762775}
763776
777+ /* hid_open_ref()
778+ *
779+ * ref must be a valid IOHIDDeviceRef
780+ */
781+ hid_device * HID_API_EXPORT hid_open_ref (const unsigned long long dev_ref )
782+ {
783+ hid_device * dev = NULL ;
784+ //io_registry_entry_t entry = MACH_PORT_NULL;
785+ IOReturn ret = kIOReturnInvalid ;
786+
787+ /* Set up the HID Manager if it hasn't been done */
788+ if (hid_init () < 0 )
789+ goto return_error ;
790+
791+ dev = new_hid_device ();
792+
793+ io_object_t iokit_dev = hidapi_IOHIDDeviceGetService (dev_ref );
794+ /* Create an IOHIDDevice for the entry */
795+ dev -> device_handle = IOHIDDeviceCreate (kCFAllocatorDefault , iokit_dev );
796+ if (dev -> device_handle == NULL ) {
797+ /* Error creating the HID device */
798+ goto return_error ;
799+ }
800+
801+ /* Open the IOHIDDevice */
802+ ret = IOHIDDeviceOpen (dev -> device_handle , kIOHIDOptionsTypeSeizeDevice );
803+ if (ret == kIOReturnSuccess ) {
804+ char str [32 ];
805+
806+ /* Create the buffers for receiving data */
807+ dev -> max_input_report_len = (CFIndex ) get_max_report_length (dev -> device_handle );
808+ dev -> input_report_buf = (uint8_t * ) calloc (dev -> max_input_report_len , sizeof (uint8_t ));
809+
810+ /* Create the Run Loop Mode for this device.
811+ printing the reference seems to work. */
812+ sprintf (str , "HIDAPI_%p" , dev -> device_handle );
813+ dev -> run_loop_mode =
814+ CFStringCreateWithCString (NULL , str , kCFStringEncodingASCII );
815+
816+ /* Attach the device to a Run Loop */
817+ IOHIDDeviceRegisterInputReportCallback (
818+ dev -> device_handle , dev -> input_report_buf , dev -> max_input_report_len ,
819+ & hid_report_callback , dev );
820+ IOHIDDeviceRegisterRemovalCallback (dev -> device_handle , hid_device_removal_callback , dev );
821+
822+ /* Start the read thread */
823+ pthread_create (& dev -> thread , NULL , read_thread , dev );
824+
825+ /* Wait here for the read thread to be initialized. */
826+ pthread_barrier_wait (& dev -> barrier );
827+
828+ //IOObjectRelease(entry);
829+ return dev ;
830+ }
831+ else {
832+ goto return_error ;
833+ }
834+
835+ return_error :
836+ if (dev -> device_handle != NULL )
837+ CFRelease (dev -> device_handle );
838+
839+ //if (entry != MACH_PORT_NULL)
840+ // IOObjectRelease(entry);
841+
842+ free_hid_device (dev );
843+ return NULL ;
844+ }
845+
846+
764847static int set_report (hid_device * dev , IOHIDReportType type , const unsigned char * data , size_t length )
765848{
766849 const unsigned char * data_to_send = data ;
0 commit comments