SDL: Fixed compiling hidapi in SDL build environment

From 651d9c4a6e099d9e4714da273b400f6272c92906 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Wed, 24 May 2023 10:03:52 -0700
Subject: [PATCH] Fixed compiling hidapi in SDL build environment

---
 src/hidapi/hidapi/hidapi.h                    |  4 +++
 src/hidapi/libusb/hid.c                       | 27 +++++++-------
 src/hidapi/windows/hid.c                      | 23 ++++++------
 src/hidapi/windows/hidapi_cfgmgr32.h          | 35 ++++++++++++-------
 .../windows/hidapi_descriptor_reconstruct.c   |  8 +++--
 .../windows/hidapi_descriptor_reconstruct.h   |  4 +--
 src/hidapi/windows/hidapi_hidclass.h          |  2 ++
 src/hidapi/windows/hidapi_hidsdi.h            |  2 --
 8 files changed, 64 insertions(+), 41 deletions(-)

diff --git a/src/hidapi/hidapi/hidapi.h b/src/hidapi/hidapi/hidapi.h
index 744ceb0b01ee..3397d18f9495 100644
--- a/src/hidapi/hidapi/hidapi.h
+++ b/src/hidapi/hidapi/hidapi.h
@@ -105,6 +105,8 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
+#ifndef DEFINED_HID_TYPES
+#define DEFINED_HID_TYPES
 		/** A structure to hold the version numbers. */
 		struct hid_api_version {
 			int major; /**< major version number */
@@ -186,6 +188,8 @@ extern "C" {
 			hid_bus_type bus_type;
 		};
 
+#endif /* DEFINED_HID_TYPES */
+
 
 		/** @brief Initialize the HIDAPI library.
 
diff --git a/src/hidapi/libusb/hid.c b/src/hidapi/libusb/hid.c
index 188e536d537d..7c590c444ba4 100644
--- a/src/hidapi/libusb/hid.c
+++ b/src/hidapi/libusb/hid.c
@@ -566,7 +566,7 @@ int HID_API_EXPORT hid_exit(void)
 
 static int hid_get_report_descriptor_libusb(libusb_device_handle *handle, int interface_num, uint16_t expected_report_descriptor_size, unsigned char *buf, size_t buf_size)
 {
-	unsigned char tmp[HID_API_MAX_REPORT_DESCRIPTOR_SIZE];
+	unsigned char *tmp = (unsigned char *)malloc(HID_API_MAX_REPORT_DESCRIPTOR_SIZE);
 
 	if (expected_report_descriptor_size > HID_API_MAX_REPORT_DESCRIPTOR_SIZE)
 		expected_report_descriptor_size = HID_API_MAX_REPORT_DESCRIPTOR_SIZE;
@@ -575,15 +575,16 @@ static int hid_get_report_descriptor_libusb(libusb_device_handle *handle, int in
 	   See USB HID Specificatin, sectin 7.1.1
 	*/
 	int res = libusb_control_transfer(handle, LIBUSB_ENDPOINT_IN|LIBUSB_RECIPIENT_INTERFACE, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_REPORT << 8), interface_num, tmp, expected_report_descriptor_size, 5000);
-	if (res < 0) {
+	if (res >= 0) {
+		if (res > (int)buf_size)
+			res = (int)buf_size;
+
+		memcpy(buf, tmp, (size_t)res);
+	} else {
 		LOG("libusb_control_transfer() for getting the HID Report descriptor failed with %d: %s\n", res, libusb_error_name(res));
-		return -1;
 	}
+	free(tmp);
 
-	if (res > (int)buf_size)
-		res = (int)buf_size;
-
-	memcpy(buf, tmp, (size_t)res);
 	return res;
 }
 
@@ -592,10 +593,10 @@ static int hid_get_report_descriptor_libusb(libusb_device_handle *handle, int in
  */
 static void fill_device_info_usage(struct hid_device_info *cur_dev, libusb_device_handle *handle, int interface_num, uint16_t expected_report_descriptor_size)
 {
-	unsigned char hid_report_descriptor[HID_API_MAX_REPORT_DESCRIPTOR_SIZE];
+	unsigned char *hid_report_descriptor = malloc(HID_API_MAX_REPORT_DESCRIPTOR_SIZE);
 	unsigned short page = 0, usage = 0;
 
-	int res = hid_get_report_descriptor_libusb(handle, interface_num, expected_report_descriptor_size, hid_report_descriptor, sizeof(hid_report_descriptor));
+	int res = hid_get_report_descriptor_libusb(handle, interface_num, expected_report_descriptor_size, hid_report_descriptor, HID_API_MAX_REPORT_DESCRIPTOR_SIZE);
 	if (res >= 0) {
 		/* Parse the usage and usage page
 		   out of the report descriptor. */
@@ -1298,7 +1299,7 @@ int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t
 			0x09/*HID Set_Report*/,
 			(2/*HID output*/ << 8) | report_number,
 			dev->interface,
-			(unsigned char *)data, length,
+			(unsigned char *)data, (uint16_t)length,
 			1000/*timeout millis*/);
 
 		if (res < 0)
@@ -1469,7 +1470,7 @@ int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char
 		0x09/*HID set_report*/,
 		(3/*HID feature*/ << 8) | report_number,
 		dev->interface,
-		(unsigned char *)data, length,
+		(unsigned char *)data, (uint16_t)length,
 		1000/*timeout millis*/);
 
 	if (res < 0)
@@ -1500,7 +1501,7 @@ int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data,
 		0x01/*HID get_report*/,
 		(3/*HID feature*/ << 8) | report_number,
 		dev->interface,
-		(unsigned char *)data, length,
+		(unsigned char *)data, (uint16_t)length,
 		1000/*timeout millis*/);
 
 	if (res < 0)
@@ -1530,7 +1531,7 @@ int HID_API_EXPORT HID_API_CALL hid_get_input_report(hid_device *dev, unsigned c
 		0x01/*HID get_report*/,
 		(1/*HID Input*/ << 8) | report_number,
 		dev->interface,
-		(unsigned char *)data, length,
+		(unsigned char *)data, (uint16_t)length,
 		1000/*timeout millis*/);
 
 	if (res < 0)
diff --git a/src/hidapi/windows/hid.c b/src/hidapi/windows/hid.c
index 699969146752..98d5df4cd3e3 100644
--- a/src/hidapi/windows/hid.c
+++ b/src/hidapi/windows/hid.c
@@ -38,11 +38,12 @@ extern "C" {
 typedef LONG NTSTATUS;
 #endif
 
-#ifdef __MINGW32__
-#include <ntdef.h>
-#include <winbase.h>
+#ifndef WC_ERR_INVALID_CHARS
 #define WC_ERR_INVALID_CHARS 0x00000080
 #endif
+#ifndef _WIN32_WINNT_WIN8
+#define _WIN32_WINNT_WIN8 0x0602
+#endif
 
 #ifdef __CYGWIN__
 #include <ntdef.h>
@@ -116,6 +117,11 @@ static void free_library_handles()
 	cfgmgr32_lib_handle = NULL;
 }
 
+#if defined(__GNUC__)
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wcast-function-type"
+#endif
+
 static int lookup_functions()
 {
 	hid_lib_handle = LoadLibraryW(L"hid.dll");
@@ -128,10 +134,6 @@ static int lookup_functions()
 		goto err;
 	}
 
-#if defined(__GNUC__)
-# pragma GCC diagnostic push
-# pragma GCC diagnostic ignored "-Wcast-function-type"
-#endif
 #define RESOLVE(lib_handle, x) x = (x##_)GetProcAddress(lib_handle, #x); if (!x) goto err;
 
 	RESOLVE(hid_lib_handle, HidD_GetHidGuid);
@@ -156,9 +158,6 @@ static int lookup_functions()
 	RESOLVE(cfgmgr32_lib_handle, CM_Get_Device_Interface_ListW);
 
 #undef RESOLVE
-#if defined(__GNUC__)
-# pragma GCC diagnostic pop
-#endif
 
 	return 0;
 
@@ -167,6 +166,10 @@ static int lookup_functions()
 	return -1;
 }
 
+#if defined(__GNUC__)
+# pragma GCC diagnostic pop
+#endif
+
 #endif /* HIDAPI_USE_DDK */
 
 struct hid_device_ {
diff --git a/src/hidapi/windows/hidapi_cfgmgr32.h b/src/hidapi/windows/hidapi_cfgmgr32.h
index 638512a8b4d5..09bf4d768ba0 100644
--- a/src/hidapi/windows/hidapi_cfgmgr32.h
+++ b/src/hidapi/windows/hidapi_cfgmgr32.h
@@ -32,9 +32,20 @@
 /* This part of the header mimics cfgmgr32.h,
     but only what is used by HIDAPI */
 
-#include <initguid.h>
+//#include <initguid.h>
 #include <devpropdef.h>
-#include <propkeydef.h>
+//#include <propkeydef.h>
+
+#ifndef PROPERTYKEY_DEFINED
+#define PROPERTYKEY_DEFINED
+
+typedef struct
+{
+    GUID fmtid;
+    DWORD pid;
+} PROPERTYKEY;
+
+#endif /* PROPERTYKEY_DEFINED */
 
 typedef DWORD RETURN_TYPE;
 typedef RETURN_TYPE CONFIGRET;
@@ -55,20 +66,20 @@ typedef CONFIGRET(__stdcall* CM_Get_Parent_)(PDEVINST pdnDevInst, DEVINST dnDevI
 typedef CONFIGRET(__stdcall* CM_Get_DevNode_PropertyW_)(DEVINST dnDevInst, CONST DEVPROPKEY* PropertyKey, DEVPROPTYPE* PropertyType, PBYTE PropertyBuffer, PULONG PropertyBufferSize, ULONG ulFlags);
 typedef CONFIGRET(__stdcall* CM_Get_Device_Interface_PropertyW_)(LPCWSTR pszDeviceInterface, CONST DEVPROPKEY* PropertyKey, DEVPROPTYPE* PropertyType, PBYTE PropertyBuffer, PULONG PropertyBufferSize, ULONG ulFlags);
 typedef CONFIGRET(__stdcall* CM_Get_Device_Interface_List_SizeW_)(PULONG pulLen, LPGUID InterfaceClassGuid, DEVINSTID_W pDeviceID, ULONG ulFlags);
-typedef CONFIGRET(__stdcall* CM_Get_Device_Interface_ListW_)(LPGUID InterfaceClassGuid, DEVINSTID_W pDeviceID, PZZWSTR Buffer, ULONG BufferLen, ULONG ulFlags);
+typedef CONFIGRET(__stdcall* CM_Get_Device_Interface_ListW_)(LPGUID InterfaceClassGuid, DEVINSTID_W pDeviceID, WCHAR* /*PZZWSTR*/ Buffer, ULONG BufferLen, ULONG ulFlags);
 
 // from devpkey.h
-DEFINE_DEVPROPKEY(DEVPKEY_NAME, 0xb725f130, 0x47ef, 0x101a, 0xa5, 0xf1, 0x02, 0x60, 0x8c, 0x9e, 0xeb, 0xac, 10); // DEVPROP_TYPE_STRING
-DEFINE_DEVPROPKEY(DEVPKEY_Device_Manufacturer, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 13); // DEVPROP_TYPE_STRING
-DEFINE_DEVPROPKEY(DEVPKEY_Device_InstanceId, 0x78c34fc8, 0x104a, 0x4aca, 0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57, 256); // DEVPROP_TYPE_STRING
-DEFINE_DEVPROPKEY(DEVPKEY_Device_HardwareIds, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 3); // DEVPROP_TYPE_STRING_LIST
-DEFINE_DEVPROPKEY(DEVPKEY_Device_CompatibleIds, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 4); // DEVPROP_TYPE_STRING_LIST
-DEFINE_DEVPROPKEY(DEVPKEY_Device_ContainerId, 0x8c7ed206, 0x3f8a, 0x4827, 0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c, 2); // DEVPROP_TYPE_GUID
+static DEVPROPKEY DEVPKEY_NAME = { { 0xb725f130, 0x47ef, 0x101a, {0xa5, 0xf1, 0x02, 0x60, 0x8c, 0x9e, 0xeb, 0xac} }, 10 }; // DEVPROP_TYPE_STRING
+static DEVPROPKEY DEVPKEY_Device_Manufacturer = { { 0xa45c254e, 0xdf1c, 0x4efd, {0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0} }, 13 }; // DEVPROP_TYPE_STRING
+static DEVPROPKEY DEVPKEY_Device_InstanceId = { { 0x78c34fc8, 0x104a, 0x4aca, {0x9e, 0xa4, 0x52, 0x4d, 0x52, 0x99, 0x6e, 0x57} }, 256 }; // DEVPROP_TYPE_STRING
+static DEVPROPKEY DEVPKEY_Device_HardwareIds = { { 0xa45c254e, 0xdf1c, 0x4efd, {0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0} }, 3 }; // DEVPROP_TYPE_STRING_LIST
+static DEVPROPKEY DEVPKEY_Device_CompatibleIds = { { 0xa45c254e, 0xdf1c, 0x4efd, {0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0} }, 4 }; // DEVPROP_TYPE_STRING_LIST
+static DEVPROPKEY DEVPKEY_Device_ContainerId = { { 0x8c7ed206, 0x3f8a, 0x4827, {0xb3, 0xab, 0xae, 0x9e, 0x1f, 0xae, 0xfc, 0x6c} }, 2 }; // DEVPROP_TYPE_GUID
 
 // from propkey.h
-DEFINE_PROPERTYKEY(PKEY_DeviceInterface_Bluetooth_DeviceAddress, 0x2BD67D8B, 0x8BEB, 0x48D5, 0x87, 0xE0, 0x6C, 0xDA, 0x34, 0x28, 0x04, 0x0A, 1); // DEVPROP_TYPE_STRING
-DEFINE_PROPERTYKEY(PKEY_DeviceInterface_Bluetooth_Manufacturer, 0x2BD67D8B, 0x8BEB, 0x48D5, 0x87, 0xE0, 0x6C, 0xDA, 0x34, 0x28, 0x04, 0x0A, 4); // DEVPROP_TYPE_STRING
-DEFINE_PROPERTYKEY(PKEY_DeviceInterface_Bluetooth_ModelNumber, 0x2BD67D8B, 0x8BEB, 0x48D5, 0x87, 0xE0, 0x6C, 0xDA, 0x34, 0x28, 0x04, 0x0A, 5); // DEVPROP_TYPE_STRING
+static PROPERTYKEY PKEY_DeviceInterface_Bluetooth_DeviceAddress = { { 0x2bd67d8b, 0x8beb, 0x48d5, {0x87, 0xe0, 0x6c, 0xda, 0x34, 0x28, 0x04, 0x0a} }, 1 }; // DEVPROP_TYPE_STRING
+static PROPERTYKEY PKEY_DeviceInterface_Bluetooth_Manufacturer = { { 0x2bd67d8b, 0x8beb, 0x48d5, {0x87, 0xe0, 0x6c, 0xda, 0x34, 0x28, 0x04, 0x0a} }, 4 }; // DEVPROP_TYPE_STRING
+static PROPERTYKEY PKEY_DeviceInterface_Bluetooth_ModelNumber = { { 0x2BD67D8B, 0x8BEB, 0x48D5, {0x87, 0xE0, 0x6C, 0xDA, 0x34, 0x28, 0x04, 0x0A} }, 5 }; // DEVPROP_TYPE_STRING
 
 #endif
 
diff --git a/src/hidapi/windows/hidapi_descriptor_reconstruct.c b/src/hidapi/windows/hidapi_descriptor_reconstruct.c
index a4efb2597507..b5d2d52a8036 100644
--- a/src/hidapi/windows/hidapi_descriptor_reconstruct.c
+++ b/src/hidapi/windows/hidapi_descriptor_reconstruct.c
@@ -528,9 +528,11 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
 	//  report descriptors seem to have it, as assumed here.
 	// ***********************************************************
 	{
-		int last_bit_position[NUM_OF_HIDP_REPORT_TYPES][256];
-		struct rd_main_item_node *last_report_item_lookup[NUM_OF_HIDP_REPORT_TYPES][256];
+		int *last_bit_position[NUM_OF_HIDP_REPORT_TYPES];
+		struct rd_main_item_node **last_report_item_lookup[NUM_OF_HIDP_REPORT_TYPES];
 		for (HIDP_REPORT_TYPE rt_idx = 0; rt_idx < NUM_OF_HIDP_REPORT_TYPES; rt_idx++) {
+			last_bit_position[rt_idx] = malloc(256 * sizeof(*last_bit_position[rt_idx]));
+			last_report_item_lookup[rt_idx] = malloc(256 * sizeof(*last_report_item_lookup[rt_idx]));
 			for (int reportid_idx = 0; reportid_idx < 256; reportid_idx++) {
 				last_bit_position[rt_idx][reportid_idx] = -1;
 				last_report_item_lookup[rt_idx][reportid_idx] = NULL;
@@ -569,6 +571,8 @@ int hid_winapi_descriptor_reconstruct_pp_data(void *preparsed_data, unsigned cha
 					}
 				}
 			}
+			free(last_bit_position[rt_idx]);
+			free(last_report_item_lookup[rt_idx]);
 		}
 	}
 
diff --git a/src/hidapi/windows/hidapi_descriptor_reconstruct.h b/src/hidapi/windows/hidapi_descriptor_reconstruct.h
index 2ec8b208149c..448cb6be793a 100644
--- a/src/hidapi/windows/hidapi_descriptor_reconstruct.h
+++ b/src/hidapi/windows/hidapi_descriptor_reconstruct.h
@@ -27,7 +27,7 @@
 
 #include "hidapi_winapi.h"
 
-#if _MSC_VER
+#ifdef _MSC_VER
 #pragma warning(push)
 #pragma warning(disable: 4200)
 #pragma warning(disable: 4201)
@@ -106,7 +106,7 @@ struct rd_main_item_node {
 
 typedef struct hid_pp_caps_info_ {
 	USHORT FirstCap;
-	USHORT NumberOfCaps; // Includes empty caps after LastCap 
+	USHORT NumberOfCaps; // Includes empty caps after LastCap
 	USHORT LastCap;
 	USHORT ReportByteLength;
 } hid_pp_caps_info, *phid_pp_caps_info;
diff --git a/src/hidapi/windows/hidapi_hidclass.h b/src/hidapi/windows/hidapi_hidclass.h
index 13bd6f22bd54..8acc52e0adcc 100644
--- a/src/hidapi/windows/hidapi_hidclass.h
+++ b/src/hidapi/windows/hidapi_hidclass.h
@@ -26,6 +26,8 @@
 
 #else
 
+#include <winioctl.h>
+
 /* This part of the header mimics hidclass.h,
     but only what is used by HIDAPI */
 
diff --git a/src/hidapi/windows/hidapi_hidsdi.h b/src/hidapi/windows/hidapi_hidsdi.h
index 453f89973d6f..4da6b91d8f39 100644
--- a/src/hidapi/windows/hidapi_hidsdi.h
+++ b/src/hidapi/windows/hidapi_hidsdi.h
@@ -40,8 +40,6 @@ typedef struct _HIDD_ATTRIBUTES{
 	USHORT VersionNumber;
 } HIDD_ATTRIBUTES, *PHIDD_ATTRIBUTES;
 
-typedef struct _HIDP_PREPARSED_DATA * PHIDP_PREPARSED_DATA;
-
 typedef void (__stdcall *HidD_GetHidGuid_)(LPGUID hid_guid);
 typedef BOOLEAN (__stdcall *HidD_GetAttributes_)(HANDLE device, PHIDD_ATTRIBUTES attrib);
 typedef BOOLEAN (__stdcall *HidD_GetSerialNumberString_)(HANDLE device, PVOID buffer, ULONG buffer_len);