From 3672424b0b7add25be1c8793bbe5af5ce8dd412d Mon Sep 17 00:00:00 2001 From: Raphael Assenat Date: Sat, 24 Oct 2015 15:09:52 -0400 Subject: [PATCH] Update hid.c for windows --- tool/hid.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tool/hid.c b/tool/hid.c index d1d6d75..08ad6cd 100644 --- a/tool/hid.c +++ b/tool/hid.c @@ -106,6 +106,7 @@ extern "C" { typedef BOOLEAN (__stdcall *HidD_GetPreparsedData_)(HANDLE handle, PHIDP_PREPARSED_DATA *preparsed_data); typedef BOOLEAN (__stdcall *HidD_FreePreparsedData_)(PHIDP_PREPARSED_DATA preparsed_data); typedef NTSTATUS (__stdcall *HidP_GetCaps_)(PHIDP_PREPARSED_DATA preparsed_data, HIDP_CAPS *caps); + typedef BOOLEAN (__stdcall *HidD_SetNumInputBuffers_)(HANDLE handle, ULONG number_buffers); static HidD_GetAttributes_ HidD_GetAttributes; static HidD_GetSerialNumberString_ HidD_GetSerialNumberString; @@ -117,6 +118,7 @@ extern "C" { static HidD_GetPreparsedData_ HidD_GetPreparsedData; static HidD_FreePreparsedData_ HidD_FreePreparsedData; static HidP_GetCaps_ HidP_GetCaps; + static HidD_SetNumInputBuffers_ HidD_SetNumInputBuffers; static HMODULE lib_handle = NULL; static BOOLEAN initialized = FALSE; @@ -206,6 +208,7 @@ static int lookup_functions() RESOLVE(HidD_GetPreparsedData); RESOLVE(HidD_FreePreparsedData); RESOLVE(HidP_GetCaps); + RESOLVE(HidD_SetNumInputBuffers); #undef RESOLVE } else @@ -567,6 +570,13 @@ HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path) goto err; } + /* Set the Input Report buffer size to 64 reports. */ + res = HidD_SetNumInputBuffers(dev->device_handle, 64); + if (!res) { + register_error(dev, "HidD_SetNumInputBuffers"); + goto err; + } + /* Get the Input Report length for the device. */ res = HidD_GetPreparsedData(dev->device_handle, &pp_data); if (!res) { @@ -652,6 +662,7 @@ end_of_function: int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds) { DWORD bytes_read = 0; + size_t copy_len = 0; BOOL res; /* Copy the handle for convenience. */ @@ -699,14 +710,13 @@ int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char number (0x0) on the beginning of the report anyway. To make this work like the other platforms, and to make it work more like the HID spec, we'll skip over this byte. */ - size_t copy_len; bytes_read--; copy_len = length > bytes_read ? bytes_read : length; memcpy(data, dev->read_buf+1, copy_len); } else { /* Copy the whole buffer, report number and all. */ - size_t copy_len = length > bytes_read ? bytes_read : length; + copy_len = length > bytes_read ? bytes_read : length; memcpy(data, dev->read_buf, copy_len); } } @@ -717,7 +727,7 @@ end_of_function: return -1; } - return bytes_read; + return copy_len; } int HID_API_EXPORT HID_API_CALL hid_read(hid_device *dev, unsigned char *data, size_t length) @@ -781,6 +791,12 @@ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned register_error(dev, "Send Feature Report GetOverLappedResult"); return -1; } + + /* bytes_returned does not include the first byte which contains the + report ID. The data buffer actually contains one more byte than + bytes_returned. */ + bytes_returned++; + return bytes_returned; #endif }