Skip to content

Commit b923aed

Browse files
committed
Windows: hid_get_feature/input_report return size fix (#334)
- It appears when the numbered reports aren't used, WinAPI returns size of the report excluding the data[0] which contains 0, as an indication that numbered reports aren't used; Explicity count that byte in the result. Fixes: #328
1 parent 4d78749 commit b923aed

File tree

1 file changed

+19
-54
lines changed

1 file changed

+19
-54
lines changed

windows/hid.c

Lines changed: 19 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,33 +1039,24 @@ int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *dev, const u
10391039
return (int) length;
10401040
}
10411041

1042-
1043-
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
1042+
static int hid_get_report(hid_device *dev, DWORD report_type, unsigned char *data, size_t length)
10441043
{
10451044
BOOL res;
1046-
#if 0
1047-
res = HidD_GetFeature(dev->device_handle, data, length);
1048-
if (!res) {
1049-
register_error(dev, "HidD_GetFeature");
1050-
return -1;
1051-
}
1052-
return 0; /* HidD_GetFeature() doesn't give us an actual length, unfortunately */
1053-
#else
1054-
DWORD bytes_returned;
1045+
DWORD bytes_returned = 0;
10551046

10561047
OVERLAPPED ol;
10571048
memset(&ol, 0, sizeof(ol));
10581049

10591050
res = DeviceIoControl(dev->device_handle,
1060-
IOCTL_HID_GET_FEATURE,
1051+
report_type,
10611052
data, (DWORD) length,
10621053
data, (DWORD) length,
10631054
&bytes_returned, &ol);
10641055

10651056
if (!res) {
10661057
if (GetLastError() != ERROR_IO_PENDING) {
10671058
/* DeviceIoControl() failed. Return error. */
1068-
register_error(dev, "Send Feature Report DeviceIoControl");
1059+
register_error(dev, "Get Input/Feature Report DeviceIoControl");
10691060
return -1;
10701061
}
10711062
}
@@ -1075,56 +1066,30 @@ int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned
10751066
res = GetOverlappedResult(dev->device_handle, &ol, &bytes_returned, TRUE/*wait*/);
10761067
if (!res) {
10771068
/* The operation failed. */
1078-
register_error(dev, "Send Feature Report GetOverLappedResult");
1069+
register_error(dev, "Get Input/Feature Report GetOverLappedResult");
10791070
return -1;
10801071
}
10811072

1073+
/* When numbered reports aren't used,
1074+
bytes_returned seem to include only what is actually received from the device
1075+
(not including the first byte with 0, as an indication "no numbered reports"). */
1076+
if (data[0] == 0x0) {
1077+
bytes_returned++;
1078+
}
1079+
10821080
return bytes_returned;
1083-
#endif
10841081
}
10851082

1083+
int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
1084+
{
1085+
/* We could use HidD_GetFeature() instead, but it doesn't give us an actual length, unfortunately */
1086+
return hid_get_report(dev, IOCTL_HID_GET_FEATURE, data, length);
1087+
}
10861088

10871089
int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned char *data, size_t length)
10881090
{
1089-
BOOL res;
1090-
#if 0
1091-
res = HidD_GetInputReport(dev->device_handle, data, length);
1092-
if (!res) {
1093-
register_error(dev, "HidD_GetInputReport");
1094-
return -1;
1095-
}
1096-
return length;
1097-
#else
1098-
DWORD bytes_returned;
1099-
1100-
OVERLAPPED ol;
1101-
memset(&ol, 0, sizeof(ol));
1102-
1103-
res = DeviceIoControl(dev->device_handle,
1104-
IOCTL_HID_GET_INPUT_REPORT,
1105-
data, (DWORD) length,
1106-
data, (DWORD) length,
1107-
&bytes_returned, &ol);
1108-
1109-
if (!res) {
1110-
if (GetLastError() != ERROR_IO_PENDING) {
1111-
/* DeviceIoControl() failed. Return error. */
1112-
register_error(dev, "Send Input Report DeviceIoControl");
1113-
return -1;
1114-
}
1115-
}
1116-
1117-
/* Wait here until the write is done. This makes
1118-
hid_get_feature_report() synchronous. */
1119-
res = GetOverlappedResult(dev->device_handle, &ol, &bytes_returned, TRUE/*wait*/);
1120-
if (!res) {
1121-
/* The operation failed. */
1122-
register_error(dev, "Send Input Report GetOverLappedResult");
1123-
return -1;
1124-
}
1125-
1126-
return bytes_returned;
1127-
#endif
1091+
/* We could use HidD_GetInputReport() instead, but it doesn't give us an actual length, unfortunately */
1092+
return hid_get_report(dev, IOCTL_HID_GET_INPUT_REPORT, data, length);
11281093
}
11291094

11301095
void HID_API_EXPORT HID_API_CALL hid_close(hid_device *dev)

0 commit comments

Comments
 (0)