Skip to content

Commit c5ee4d3

Browse files
authored
Fix WinAPI backend compilation as C++ source (#718)
Fix compilation with older WINVER.
1 parent b26f057 commit c5ee4d3

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

windows/hid.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n
460460
{
461461
wchar_t *device_id = NULL, *hardware_ids = NULL;
462462

463-
device_id = hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
463+
device_id = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
464464
if (!device_id)
465465
goto end;
466466

@@ -478,7 +478,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n
478478
}
479479

480480
/* Get the hardware ids from devnode */
481-
hardware_ids = hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_HardwareIds, DEVPROP_TYPE_STRING_LIST);
481+
hardware_ids = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_HardwareIds, DEVPROP_TYPE_STRING_LIST);
482482
if (!hardware_ids)
483483
goto end;
484484

@@ -509,7 +509,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n
509509

510510
/* Try to get USB device manufacturer string if not provided by HidD_GetManufacturerString. */
511511
if (wcslen(dev->manufacturer_string) == 0) {
512-
wchar_t* manufacturer_string = hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_Manufacturer, DEVPROP_TYPE_STRING);
512+
wchar_t* manufacturer_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_Manufacturer, DEVPROP_TYPE_STRING);
513513
if (manufacturer_string) {
514514
free(dev->manufacturer_string);
515515
dev->manufacturer_string = manufacturer_string;
@@ -529,7 +529,7 @@ static void hid_internal_get_usb_info(struct hid_device_info* dev, DEVINST dev_n
529529

530530
/* Get the device id of the USB device. */
531531
free(device_id);
532-
device_id = hid_internal_get_devnode_property(usb_dev_node, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
532+
device_id = (wchar_t *)hid_internal_get_devnode_property(usb_dev_node, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
533533
if (!device_id)
534534
goto end;
535535

@@ -568,7 +568,7 @@ static void hid_internal_get_ble_info(struct hid_device_info* dev, DEVINST dev_n
568568
{
569569
if (wcslen(dev->manufacturer_string) == 0) {
570570
/* Manufacturer Name String (UUID: 0x2A29) */
571-
wchar_t* manufacturer_string = hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_Manufacturer, DEVPROP_TYPE_STRING);
571+
wchar_t* manufacturer_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_Manufacturer, DEVPROP_TYPE_STRING);
572572
if (manufacturer_string) {
573573
free(dev->manufacturer_string);
574574
dev->manufacturer_string = manufacturer_string;
@@ -577,7 +577,7 @@ static void hid_internal_get_ble_info(struct hid_device_info* dev, DEVINST dev_n
577577

578578
if (wcslen(dev->serial_number) == 0) {
579579
/* Serial Number String (UUID: 0x2A25) */
580-
wchar_t* serial_number = hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_DeviceAddress, DEVPROP_TYPE_STRING);
580+
wchar_t* serial_number = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_DeviceAddress, DEVPROP_TYPE_STRING);
581581
if (serial_number) {
582582
free(dev->serial_number);
583583
dev->serial_number = serial_number;
@@ -586,13 +586,13 @@ static void hid_internal_get_ble_info(struct hid_device_info* dev, DEVINST dev_n
586586

587587
if (wcslen(dev->product_string) == 0) {
588588
/* Model Number String (UUID: 0x2A24) */
589-
wchar_t* product_string = hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_ModelNumber, DEVPROP_TYPE_STRING);
589+
wchar_t* product_string = (wchar_t *)hid_internal_get_devnode_property(dev_node, (const DEVPROPKEY*)&PKEY_DeviceInterface_Bluetooth_ModelNumber, DEVPROP_TYPE_STRING);
590590
if (!product_string) {
591591
DEVINST parent_dev_node = 0;
592592
/* Fallback: Get devnode grandparent to reach out Bluetooth LE device node */
593593
if (CM_Get_Parent(&parent_dev_node, dev_node, 0) == CR_SUCCESS) {
594594
/* Device Name (UUID: 0x2A00) */
595-
product_string = hid_internal_get_devnode_property(parent_dev_node, &DEVPKEY_NAME, DEVPROP_TYPE_STRING);
595+
product_string = (wchar_t *)hid_internal_get_devnode_property(parent_dev_node, &DEVPKEY_NAME, DEVPROP_TYPE_STRING);
596596
}
597597
}
598598

@@ -621,7 +621,7 @@ static hid_internal_detect_bus_type_result hid_internal_detect_bus_type(const wc
621621
hid_internal_detect_bus_type_result result = { 0 };
622622

623623
/* Get the device id from interface path */
624-
device_id = hid_internal_get_device_interface_property(interface_path, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
624+
device_id = (wchar_t *)hid_internal_get_device_interface_property(interface_path, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
625625
if (!device_id)
626626
goto end;
627627

@@ -636,7 +636,7 @@ static hid_internal_detect_bus_type_result hid_internal_detect_bus_type(const wc
636636
goto end;
637637

638638
/* Get the compatible ids from parent devnode */
639-
compatible_ids = hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_CompatibleIds, DEVPROP_TYPE_STRING_LIST);
639+
compatible_ids = (wchar_t *)hid_internal_get_devnode_property(dev_node, &DEVPKEY_Device_CompatibleIds, DEVPROP_TYPE_STRING_LIST);
640640
if (!compatible_ids)
641641
goto end;
642642

@@ -1515,7 +1515,7 @@ int HID_API_EXPORT_CALL hid_winapi_get_container_id(hid_device *dev, GUID *conta
15151515
}
15161516

15171517
/* Get the device id from interface path */
1518-
device_id = hid_internal_get_device_interface_property(interface_path, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
1518+
device_id = (wchar_t *)hid_internal_get_device_interface_property(interface_path, &DEVPKEY_Device_InstanceId, DEVPROP_TYPE_STRING);
15191519
if (!device_id) {
15201520
register_string_error(dev, L"Failed to get device interface property InstanceId");
15211521
goto end;

windows/hidapi_descriptor_reconstruct.c

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static struct rd_main_item_node * rd_append_main_item_node(int first_bit, int la
140140
list = &(*list)->next;
141141
}
142142

143-
new_list_node = malloc(sizeof(*new_list_node)); // Create new list entry
143+
new_list_node = (struct rd_main_item_node *)malloc(sizeof(*new_list_node)); // Create new list entry
144144
new_list_node->FirstBit = first_bit;
145145
new_list_node->LastBit = last_bit;
146146
new_list_node->TypeOfNode = type_of_node;
@@ -203,21 +203,21 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
203203

204204
// Allocate memory and initialize lookup table
205205
rd_bit_range ****coll_bit_range;
206-
coll_bit_range = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_bit_range));
206+
coll_bit_range = (rd_bit_range ****)malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_bit_range));
207207
for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
208-
coll_bit_range[collection_node_idx] = malloc(256 * sizeof(*coll_bit_range[0])); // 256 possible report IDs (incl. 0x00)
208+
coll_bit_range[collection_node_idx] = (rd_bit_range ***)malloc(256 * sizeof(*coll_bit_range[0])); // 256 possible report IDs (incl. 0x00)
209209
for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
210-
coll_bit_range[collection_node_idx][reportid_idx] = malloc(NUM_OF_HIDP_REPORT_TYPES * sizeof(*coll_bit_range[0][0]));
211-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
212-
coll_bit_range[collection_node_idx][reportid_idx][rt_idx] = malloc(sizeof(rd_bit_range));
210+
coll_bit_range[collection_node_idx][reportid_idx] = (rd_bit_range **)malloc(NUM_OF_HIDP_REPORT_TYPES * sizeof(*coll_bit_range[0][0]));
211+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
212+
coll_bit_range[collection_node_idx][reportid_idx][rt_idx] = (rd_bit_range *)malloc(sizeof(rd_bit_range));
213213
coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->FirstBit = -1;
214214
coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->LastBit = -1;
215215
}
216216
}
217217
}
218218

219219
// Fill the lookup table where caps exist
220-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
220+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
221221
for (USHORT caps_idx = pp_data->caps_info[rt_idx].FirstCap; caps_idx < pp_data->caps_info[rt_idx].LastCap; caps_idx++) {
222222
int first_bit, last_bit;
223223
first_bit = (pp_data->caps[caps_idx].BytePosition - 1) * 8
@@ -241,8 +241,8 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
241241
// coll_number_of_direct_childs[COLLECTION_INDEX]
242242
// *************************************************************************
243243
int max_coll_level = 0;
244-
int *coll_levels = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_levels[0]));
245-
int *coll_number_of_direct_childs = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_number_of_direct_childs[0]));
244+
int *coll_levels = (int *)malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_levels[0]));
245+
int *coll_number_of_direct_childs = (int *)malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_number_of_direct_childs[0]));
246246
for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
247247
coll_levels[collection_node_idx] = -1;
248248
coll_number_of_direct_childs[collection_node_idx] = 0;
@@ -286,7 +286,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
286286
USHORT child_idx = link_collection_nodes[collection_node_idx].FirstChild;
287287
while (child_idx) {
288288
for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
289-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
289+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
290290
// Merge bit range from childs
291291
if ((coll_bit_range[child_idx][reportid_idx][rt_idx]->FirstBit != -1) &&
292292
(coll_bit_range[collection_node_idx][reportid_idx][rt_idx]->FirstBit > coll_bit_range[child_idx][reportid_idx][rt_idx]->FirstBit)) {
@@ -307,11 +307,9 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
307307
// Determine child collection order of the whole hierarchy, based on previously determined bit ranges
308308
// and store it this index coll_child_order[COLLECTION_INDEX][DIRECT_CHILD_INDEX]
309309
// **************************************************************************************************
310-
USHORT **coll_child_order;
311-
coll_child_order = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_child_order));
310+
USHORT **coll_child_order = (USHORT **)malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_child_order));
312311
{
313-
BOOLEAN *coll_parsed_flag;
314-
coll_parsed_flag = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_parsed_flag[0]));
312+
BOOLEAN *coll_parsed_flag = (BOOLEAN *)malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_parsed_flag[0]));
315313
for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
316314
coll_parsed_flag[collection_node_idx] = FALSE;
317315
}
@@ -321,7 +319,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
321319
if ((coll_number_of_direct_childs[collection_node_idx] != 0) &&
322320
(coll_parsed_flag[link_collection_nodes[collection_node_idx].FirstChild] == FALSE)) {
323321
coll_parsed_flag[link_collection_nodes[collection_node_idx].FirstChild] = TRUE;
324-
coll_child_order[collection_node_idx] = malloc((coll_number_of_direct_childs[collection_node_idx]) * sizeof(*coll_child_order[0]));
322+
coll_child_order[collection_node_idx] = (USHORT *)malloc((coll_number_of_direct_childs[collection_node_idx]) * sizeof(*coll_child_order[0]));
325323

326324
{
327325
// Create list of child collection indices
@@ -339,7 +337,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
339337

340338
if (coll_number_of_direct_childs[collection_node_idx] > 1) {
341339
// Sort child collections indices by bit positions
342-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
340+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
343341
for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
344342
for (int child_idx = 1; child_idx < coll_number_of_direct_childs[collection_node_idx]; child_idx++) {
345343
// since the coll_bit_range array is not sorted, we need to reference the collection index in
@@ -380,10 +378,10 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
380378
// ***************************************************************************************
381379
struct rd_main_item_node *main_item_list = NULL; // List root
382380
// Lookup table to find the Collection items in the list by index
383-
struct rd_main_item_node **coll_begin_lookup = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_begin_lookup));
384-
struct rd_main_item_node **coll_end_lookup = malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_end_lookup));
381+
struct rd_main_item_node **coll_begin_lookup = (struct rd_main_item_node **)malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_begin_lookup));
382+
struct rd_main_item_node **coll_end_lookup = (struct rd_main_item_node **)malloc(pp_data->NumberLinkCollectionNodes * sizeof(*coll_end_lookup));
385383
{
386-
int *coll_last_written_child = malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_last_written_child[0]));
384+
int *coll_last_written_child = (int *)malloc(pp_data->NumberLinkCollectionNodes * sizeof(coll_last_written_child[0]));
387385
for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
388386
coll_last_written_child[collection_node_idx] = -1;
389387
}
@@ -467,7 +465,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
467465
// Inserted Input/Output/Feature main items into the main_item_list
468466
// in order of reconstructed bit positions
469467
// ****************************************************************
470-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
468+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
471469
// Add all value caps to node list
472470
struct rd_main_item_node *firstDelimiterNode = NULL;
473471
struct rd_main_item_node *delimiterCloseNode = NULL;
@@ -530,7 +528,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
530528
{
531529
int last_bit_position[NUM_OF_HIDP_REPORT_TYPES][256];
532530
struct rd_main_item_node *last_report_item_lookup[NUM_OF_HIDP_REPORT_TYPES][256];
533-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
531+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
534532
for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
535533
last_bit_position[rt_idx][reportid_idx] = -1;
536534
last_report_item_lookup[rt_idx][reportid_idx] = NULL;
@@ -570,7 +568,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
570568
}
571569

572570
// Add 8 bit padding at each report end
573-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
571+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
574572
for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
575573
if (last_bit_position[rt_idx][reportid_idx] != -1) {
576574
int padding = 8 - ((last_bit_position[rt_idx][reportid_idx] + 1) % 8);
@@ -588,7 +586,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
588586
}
589587

590588
// Add full byte padding at the end of the report descriptor (only reconstructable, for devices without Report IDs)
591-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
589+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
592590
if (!devicehasReportIDs && pp_data->caps_info[rt_idx].NumberOfCaps > 0 && pp_data->caps_info[rt_idx].ReportByteLength > 0) {
593591
// ReportID 0 means this device uses not Report IDs
594592
// => Maximum one report per type possible, so we can take the size from the buffer size for the report type
@@ -1010,7 +1008,7 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
10101008
// Free multidimensionable array: coll_child_order[COLLECTION_INDEX][DIRECT_CHILD_INDEX]
10111009
for (USHORT collection_node_idx = 0; collection_node_idx < pp_data->NumberLinkCollectionNodes; collection_node_idx++) {
10121010
for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
1013-
for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
1011+
for (int rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
10141012
free(coll_bit_range[collection_node_idx][reportid_idx][rt_idx]);
10151013
}
10161014
free(coll_bit_range[collection_node_idx][reportid_idx]);

windows/hidapi_hidclass.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,24 @@
2929
/* This part of the header mimics hidclass.h,
3030
but only what is used by HIDAPI */
3131

32+
#ifndef FILE_DEVICE_KEYBOARD
33+
#define FILE_DEVICE_KEYBOARD 0x0000000b
34+
#endif
35+
36+
#ifndef METHOD_OUT_DIRECT
37+
#define METHOD_OUT_DIRECT 2
38+
#endif
39+
40+
#ifndef FILE_ANY_ACCESS
41+
#define FILE_ANY_ACCESS 0
42+
#endif
43+
44+
#ifndef CTL_CODE
45+
#define CTL_CODE( DeviceType, Function, Method, Access ) ( \
46+
((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
47+
)
48+
#endif
49+
3250
#define HID_OUT_CTL_CODE(id) CTL_CODE(FILE_DEVICE_KEYBOARD, (id), METHOD_OUT_DIRECT, FILE_ANY_ACCESS)
3351
#define IOCTL_HID_GET_FEATURE HID_OUT_CTL_CODE(100)
3452
#define IOCTL_HID_GET_INPUT_REPORT HID_OUT_CTL_CODE(104)

0 commit comments

Comments
 (0)