Skip to content

Commit 3ebc3a4

Browse files
committed
Update the SDL HIDAPI API to match upstream hidapi 0.14.0
1 parent cff9615 commit 3ebc3a4

File tree

6 files changed

+175
-25
lines changed

6 files changed

+175
-25
lines changed

include/SDL3/SDL_hidapi.h

Lines changed: 85 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ extern "C" {
7676
struct SDL_hid_device_;
7777
typedef struct SDL_hid_device_ SDL_hid_device; /**< opaque hidapi structure */
7878

79+
/**
80+
* \brief HID underlying bus types.
81+
*/
82+
typedef enum {
83+
/** Unknown bus type */
84+
SDL_HID_API_BUS_UNKNOWN = 0x00,
85+
86+
/** USB bus
87+
Specifications:
88+
https://usb.org/hid */
89+
SDL_HID_API_BUS_USB = 0x01,
90+
91+
/** Bluetooth or Bluetooth LE bus
92+
Specifications:
93+
https://www.bluetooth.com/specifications/specs/human-interface-device-profile-1-1-1/
94+
https://www.bluetooth.com/specifications/specs/hid-service-1-0/
95+
https://www.bluetooth.com/specifications/specs/hid-over-gatt-profile-1-0/ */
96+
SDL_HID_API_BUS_BLUETOOTH = 0x02,
97+
98+
/** I2C bus
99+
Specifications:
100+
https://docs.microsoft.com/previous-versions/windows/hardware/design/dn642101(v=vs.85) */
101+
SDL_HID_API_BUS_I2C = 0x03,
102+
103+
/** SPI bus
104+
Specifications:
105+
https://www.microsoft.com/download/details.aspx?id=103325 */
106+
SDL_HID_API_BUS_SPI = 0x04,
107+
108+
} SDL_hid_bus_type;
109+
79110
/** hidapi info structure */
80111
/**
81112
* \brief Information about a connected HID device
@@ -98,17 +129,17 @@ typedef struct SDL_hid_device_info
98129
/** Product string */
99130
wchar_t *product_string;
100131
/** Usage Page for this Device/Interface
101-
(Windows/Mac only). */
132+
(Windows/Mac/hidraw only) */
102133
unsigned short usage_page;
103134
/** Usage for this Device/Interface
104-
(Windows/Mac only).*/
135+
(Windows/Mac/hidraw only) */
105136
unsigned short usage;
106137
/** The USB interface which this logical device
107138
represents.
108139
109-
* Valid on both Linux implementations in all cases.
110-
* Valid on the Windows implementation only if the device
111-
contains more than one interface. */
140+
Valid only if the device is a USB HID device.
141+
Set to -1 in all other cases.
142+
*/
112143
int interface_number;
113144

114145
/** Additional information about the USB interface.
@@ -117,8 +148,12 @@ typedef struct SDL_hid_device_info
117148
int interface_subclass;
118149
int interface_protocol;
119150

151+
/** Underlying bus type */
152+
SDL_hid_bus_type bus_type;
153+
120154
/** Pointer to the next device */
121155
struct SDL_hid_device_info *next;
156+
122157
} SDL_hid_device_info;
123158

124159

@@ -187,8 +222,8 @@ extern DECLSPEC Uint32 SDLCALL SDL_hid_device_change_count(void);
187222
* matches. If `vendor_id` and `product_id` are both set to 0, then all HID
188223
* devices will be returned.
189224
*
190-
* \param vendor_id The Vendor ID (VID) of the types of device to open.
191-
* \param product_id The Product ID (PID) of the types of device to open.
225+
* \param vendor_id The Vendor ID (VID) of the types of device to open, or 0 to match any vendor.
226+
* \param product_id The Product ID (PID) of the types of device to open, or 0 to match any product.
192227
* \returns a pointer to a linked list of type SDL_hid_device_info, containing
193228
* information about the HID devices attached to the system, or NULL
194229
* in the case of failure. Free this linked list by calling
@@ -237,13 +272,12 @@ extern DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open(unsigned short vendor_id,
237272
* platform-specific path name can be used (eg: /dev/hidraw0 on Linux).
238273
*
239274
* \param path The path name of the device to open
240-
* \param bExclusive Open device in exclusive mode (Windows only)
241275
* \returns a pointer to a SDL_hid_device object on success or NULL on
242276
* failure.
243277
*
244278
* \since This function is available since SDL 3.0.0.
245279
*/
246-
extern DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path, int bExclusive /* = false */);
280+
extern DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path);
247281

248282
/**
249283
* Write an Output report to a HID device.
@@ -377,6 +411,28 @@ extern DECLSPEC int SDLCALL SDL_hid_send_feature_report(SDL_hid_device *dev, con
377411
*/
378412
extern DECLSPEC int SDLCALL SDL_hid_get_feature_report(SDL_hid_device *dev, unsigned char *data, size_t length);
379413

414+
/**
415+
* Get an input report from a HID device.
416+
*
417+
* Set the first byte of `data` to the Report ID of the report to be read.
418+
* Make sure to allow space for this extra byte in `data`. Upon return, the
419+
* first byte will still contain the Report ID, and the report data will start
420+
* in data[1].
421+
*
422+
* \param dev A device handle returned from SDL_hid_open().
423+
* \param data A buffer to put the read data into, including the Report ID.
424+
* Set the first byte of `data` to the Report ID of the report to
425+
* be read, or set it to zero if your device does not use numbered
426+
* reports.
427+
* \param length The number of bytes to read, including an extra byte for the
428+
* report ID. The buffer can be longer than the actual report.
429+
* \returns the number of bytes read plus one for the report ID (which is
430+
* still in the first byte), or -1 on error.
431+
*
432+
* \since This function is available since SDL 3.0.0.
433+
*/
434+
extern DECLSPEC int SDLCALL SDL_hid_get_input_report(SDL_hid_device *dev, unsigned char *data, size_t length);
435+
380436
/**
381437
* Close a HID device.
382438
*
@@ -441,6 +497,26 @@ extern DECLSPEC int SDLCALL SDL_hid_get_serial_number_string(SDL_hid_device *dev
441497
*/
442498
extern DECLSPEC int SDLCALL SDL_hid_get_indexed_string(SDL_hid_device *dev, int string_index, wchar_t *string, size_t maxlen);
443499

500+
/**
501+
* Get the device info from a HID device.
502+
*
503+
* \param dev A device handle returned from SDL_hid_open().
504+
* \returns a pointer to the SDL_hid_device_info for this hid_device, or NULL in the case of failure; call SDL_GetError() for more information. This struct is valid until the device is closed with SDL_hid_close().
505+
*/
506+
extern DECLSPEC SDL_hid_device_info * SDLCALL SDL_hid_get_device_info(SDL_hid_device *dev);
507+
508+
/**
509+
* Get a report descriptor from a HID device.
510+
*
511+
* User has to provide a preallocated buffer where descriptor will be copied to. The recommended size for a preallocated buffer is 4096 bytes.
512+
*
513+
* \param dev A device handle returned from SDL_hid_open().
514+
* \param buf The buffer to copy descriptor into.
515+
* \param buf_size The size of the buffer in bytes.
516+
* \returns the number of bytes actually copied, or -1 on error; call SDL_GetError() for more information.
517+
*/
518+
extern DECLSPEC int SDLCALL SDL_hid_get_report_descriptor(SDL_hid_device *dev, unsigned char *buf, size_t buf_size);
519+
444520
/**
445521
* Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers
446522
*

src/dynapi/SDL_dynapi.sym

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,9 @@ SDL3_0.0.0 {
861861
SDL_wcstol;
862862
SDL_swprintf;
863863
SDL_vswprintf;
864+
SDL_hid_get_input_report;
865+
SDL_hid_get_device_info;
866+
SDL_hid_get_report_descriptor;
864867
# extra symbols go here (don't modify this line)
865868
local: *;
866869
};

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,3 +887,6 @@
887887
#define SDL_wcstol SDL_wcstol_REAL
888888
#define SDL_swprintf SDL_swprintf_REAL
889889
#define SDL_vswprintf SDL_vswprintf_REAL
890+
#define SDL_hid_get_input_report SDL_hid_get_input_report_REAL
891+
#define SDL_hid_get_device_info SDL_hid_get_device_info_REAL
892+
#define SDL_hid_get_report_descriptor SDL_hid_get_report_descriptor_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ SDL_DYNAPI_PROC(int,SDL_hid_get_product_string,(SDL_hid_device *a, wchar_t *b, s
797797
SDL_DYNAPI_PROC(int,SDL_hid_get_serial_number_string,(SDL_hid_device *a, wchar_t *b, size_t c),(a,b,c),return)
798798
SDL_DYNAPI_PROC(int,SDL_hid_init,(void),(),return)
799799
SDL_DYNAPI_PROC(SDL_hid_device*,SDL_hid_open,(unsigned short a, unsigned short b, const wchar_t *c),(a,b,c),return)
800-
SDL_DYNAPI_PROC(SDL_hid_device*,SDL_hid_open_path,(const char *a, int b),(a,b),return)
800+
SDL_DYNAPI_PROC(SDL_hid_device*,SDL_hid_open_path,(const char *a),(a),return)
801801
SDL_DYNAPI_PROC(int,SDL_hid_read,(SDL_hid_device *a, unsigned char *b, size_t c),(a,b,c),return)
802802
SDL_DYNAPI_PROC(int,SDL_hid_read_timeout,(SDL_hid_device *a, unsigned char *b, size_t c, int d),(a,b,c,d),return)
803803
SDL_DYNAPI_PROC(int,SDL_hid_send_feature_report,(SDL_hid_device *a, const unsigned char *b, size_t c),(a,b,c),return)
@@ -932,3 +932,6 @@ SDL_DYNAPI_PROC(float,SDL_GetWindowDisplayScale,(SDL_Window *a),(a),return)
932932
SDL_DYNAPI_PROC(float,SDL_GetWindowPixelDensity,(SDL_Window *a),(a),return)
933933
SDL_DYNAPI_PROC(long,SDL_wcstol,(const wchar_t *a, wchar_t **b, int c),(a,b,c),return)
934934
SDL_DYNAPI_PROC(int,SDL_vswprintf,(wchar_t *a, size_t b, const wchar_t *c, va_list d),(a,b,c,d),return)
935+
SDL_DYNAPI_PROC(int,SDL_hid_get_input_report,(SDL_hid_device *a, unsigned char *b, size_t c),(a,b,c),return)
936+
SDL_DYNAPI_PROC(SDL_hid_device_info*,SDL_hid_get_device_info,(SDL_hid_device *a),(a),return)
937+
SDL_DYNAPI_PROC(int,SDL_hid_get_report_descriptor,(SDL_hid_device *a, unsigned char *b, size_t c),(a,b,c),return)

0 commit comments

Comments
 (0)