@@ -222,7 +222,7 @@ static void free_hid_device(hid_device *dev)
222
222
free (dev -> write_buf );
223
223
free (dev -> feature_buf );
224
224
free (dev -> read_buf );
225
- free (dev -> device_info );
225
+ hid_free_enumeration (dev -> device_info );
226
226
free (dev );
227
227
}
228
228
@@ -781,72 +781,58 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsi
781
781
782
782
HID_API_EXPORT hid_device * HID_API_CALL hid_open_path (const char * path )
783
783
{
784
- hid_device * dev ;
785
- HIDP_CAPS caps ;
784
+ hid_device * dev = NULL ;
785
+ HANDLE device_handle = INVALID_HANDLE_VALUE ;
786
786
PHIDP_PREPARSED_DATA pp_data = NULL ;
787
- BOOLEAN res ;
788
- NTSTATUS nt_res ;
789
-
790
- if (hid_init () < 0 ) {
791
- return NULL ;
792
- }
787
+ HIDP_CAPS caps ;
793
788
794
- dev = new_hid_device ();
789
+ if (hid_init () < 0 )
790
+ goto end_of_function ;
795
791
796
792
/* Open a handle to the device */
797
- dev -> device_handle = open_device (path , TRUE);
793
+ device_handle = open_device (path , TRUE);
798
794
799
795
/* Check validity of write_handle. */
800
- if (dev -> device_handle == INVALID_HANDLE_VALUE ) {
796
+ if (device_handle == INVALID_HANDLE_VALUE ) {
801
797
/* System devices, such as keyboards and mice, cannot be opened in
802
798
read-write mode, because the system takes exclusive control over
803
799
them. This is to prevent keyloggers. However, feature reports
804
800
can still be sent and received. Retry opening the device, but
805
801
without read/write access. */
806
- dev -> device_handle = open_device (path , FALSE);
802
+ device_handle = open_device (path , FALSE);
807
803
808
804
/* Check the validity of the limited device_handle. */
809
- if (dev -> device_handle == INVALID_HANDLE_VALUE ) {
810
- /* Unable to open the device, even without read-write mode. */
811
- register_error (dev , "CreateFile" );
812
- goto err ;
813
- }
805
+ if (device_handle == INVALID_HANDLE_VALUE )
806
+ goto end_of_function ;
814
807
}
815
808
816
809
/* Set the Input Report buffer size to 64 reports. */
817
- res = HidD_SetNumInputBuffers (dev -> device_handle , 64 );
818
- if (!res ) {
819
- register_error (dev , "HidD_SetNumInputBuffers" );
820
- goto err ;
821
- }
810
+ if (!HidD_SetNumInputBuffers (device_handle , 64 ))
811
+ goto end_of_function ;
822
812
823
813
/* Get the Input Report length for the device. */
824
- res = HidD_GetPreparsedData (dev -> device_handle , & pp_data );
825
- if (!res ) {
826
- register_error (dev , "HidD_GetPreparsedData" );
827
- goto err ;
828
- }
829
- nt_res = HidP_GetCaps (pp_data , & caps );
830
- if (nt_res != HIDP_STATUS_SUCCESS ) {
831
- register_error (dev , "HidP_GetCaps" );
832
- goto err_pp_data ;
833
- }
814
+ if (!HidD_GetPreparsedData (device_handle , & pp_data ))
815
+ goto end_of_function ;
816
+
817
+ if (HidP_GetCaps (pp_data , & caps ) != HIDP_STATUS_SUCCESS )
818
+ goto end_of_function ;
819
+
820
+ dev = new_hid_device ();
821
+
822
+ dev -> device_handle = device_handle ;
823
+ device_handle = INVALID_HANDLE_VALUE ;
824
+
834
825
dev -> output_report_length = caps .OutputReportByteLength ;
835
826
dev -> input_report_length = caps .InputReportByteLength ;
836
827
dev -> feature_report_length = caps .FeatureReportByteLength ;
837
- HidD_FreePreparsedData (pp_data );
838
-
839
828
dev -> read_buf = (char * ) malloc (dev -> input_report_length );
840
-
841
829
dev -> device_info = hid_get_device_info (path , dev -> device_handle );
842
830
843
- return dev ;
831
+ end_of_function :
832
+ CloseHandle (device_handle );
833
+ HidD_FreePreparsedData (pp_data );
844
834
845
- err_pp_data :
846
- HidD_FreePreparsedData (pp_data );
847
- err :
848
- free_hid_device (dev );
849
- return NULL ;
835
+ return dev ;
850
836
}
851
837
852
838
int HID_API_EXPORT HID_API_CALL hid_write (hid_device * dev , const unsigned char * data , size_t length )
0 commit comments