1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
4 | |
5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. |
8 | |
9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: |
12 | |
13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ |
21 | |
22 | /* WIKI CATEGORY: HIDAPI */ |
23 | |
24 | /** |
25 | * # CategoryHIDAPI |
26 | * |
27 | * Header file for SDL HIDAPI functions. |
28 | * |
29 | * This is an adaptation of the original HIDAPI interface by Alan Ott, and |
30 | * includes source code licensed under the following license: |
31 | * |
32 | * ``` |
33 | * HIDAPI - Multi-Platform library for |
34 | * communication with HID devices. |
35 | * |
36 | * Copyright 2009, Alan Ott, Signal 11 Software. |
37 | * All Rights Reserved. |
38 | * |
39 | * This software may be used by anyone for any reason so |
40 | * long as the copyright notice in the source files |
41 | * remains intact. |
42 | * ``` |
43 | * |
44 | * (Note that this license is the same as item three of SDL's zlib license, so |
45 | * it adds no new requirements on the user.) |
46 | * |
47 | * If you would like a version of SDL without this code, you can build SDL |
48 | * with SDL_HIDAPI_DISABLED defined to 1. You might want to do this for |
49 | * example on iOS or tvOS to avoid a dependency on the CoreBluetooth |
50 | * framework. |
51 | */ |
52 | |
53 | #ifndef SDL_hidapi_h_ |
54 | #define SDL_hidapi_h_ |
55 | |
56 | #include <SDL3/SDL_stdinc.h> |
57 | #include <SDL3/SDL_error.h> |
58 | |
59 | #include <SDL3/SDL_begin_code.h> |
60 | /* Set up for C function definitions, even when using C++ */ |
61 | #ifdef __cplusplus |
62 | extern "C" { |
63 | #endif |
64 | |
65 | /** |
66 | * An opaque handle representing an open HID device. |
67 | * |
68 | * \since This struct is available since SDL 3.2.0. |
69 | */ |
70 | typedef struct SDL_hid_device SDL_hid_device; |
71 | |
72 | /** |
73 | * HID underlying bus types. |
74 | * |
75 | * \since This enum is available since SDL 3.2.0. |
76 | */ |
77 | typedef enum SDL_hid_bus_type { |
78 | /** Unknown bus type */ |
79 | SDL_HID_API_BUS_UNKNOWN = 0x00, |
80 | |
81 | /** USB bus |
82 | Specifications: |
83 | https://usb.org/hid */ |
84 | SDL_HID_API_BUS_USB = 0x01, |
85 | |
86 | /** Bluetooth or Bluetooth LE bus |
87 | Specifications: |
88 | https://www.bluetooth.com/specifications/specs/human-interface-device-profile-1-1-1/ |
89 | https://www.bluetooth.com/specifications/specs/hid-service-1-0/ |
90 | https://www.bluetooth.com/specifications/specs/hid-over-gatt-profile-1-0/ */ |
91 | SDL_HID_API_BUS_BLUETOOTH = 0x02, |
92 | |
93 | /** I2C bus |
94 | Specifications: |
95 | https://docs.microsoft.com/previous-versions/windows/hardware/design/dn642101(v=vs.85) */ |
96 | SDL_HID_API_BUS_I2C = 0x03, |
97 | |
98 | /** SPI bus |
99 | Specifications: |
100 | https://www.microsoft.com/download/details.aspx?id=103325 */ |
101 | SDL_HID_API_BUS_SPI = 0x04 |
102 | |
103 | } SDL_hid_bus_type; |
104 | |
105 | /** hidapi info structure */ |
106 | |
107 | /** |
108 | * Information about a connected HID device |
109 | * |
110 | * \since This struct is available since SDL 3.2.0. |
111 | */ |
112 | typedef struct SDL_hid_device_info |
113 | { |
114 | /** Platform-specific device path */ |
115 | char *path; |
116 | /** Device Vendor ID */ |
117 | unsigned short vendor_id; |
118 | /** Device Product ID */ |
119 | unsigned short product_id; |
120 | /** Serial Number */ |
121 | wchar_t *serial_number; |
122 | /** Device Release Number in binary-coded decimal, |
123 | also known as Device Version Number */ |
124 | unsigned short release_number; |
125 | /** Manufacturer String */ |
126 | wchar_t *manufacturer_string; |
127 | /** Product string */ |
128 | wchar_t *product_string; |
129 | /** Usage Page for this Device/Interface |
130 | (Windows/Mac/hidraw only) */ |
131 | unsigned short usage_page; |
132 | /** Usage for this Device/Interface |
133 | (Windows/Mac/hidraw only) */ |
134 | unsigned short usage; |
135 | /** The USB interface which this logical device |
136 | represents. |
137 | |
138 | Valid only if the device is a USB HID device. |
139 | Set to -1 in all other cases. |
140 | */ |
141 | int interface_number; |
142 | |
143 | /** Additional information about the USB interface. |
144 | Valid on libusb and Android implementations. */ |
145 | int interface_class; |
146 | int interface_subclass; |
147 | int interface_protocol; |
148 | |
149 | /** Underlying bus type */ |
150 | SDL_hid_bus_type bus_type; |
151 | |
152 | /** Pointer to the next device */ |
153 | struct SDL_hid_device_info *next; |
154 | |
155 | } SDL_hid_device_info; |
156 | |
157 | |
158 | /** |
159 | * Initialize the HIDAPI library. |
160 | * |
161 | * This function initializes the HIDAPI library. Calling it is not strictly |
162 | * necessary, as it will be called automatically by SDL_hid_enumerate() and |
163 | * any of the SDL_hid_open_*() functions if it is needed. This function should |
164 | * be called at the beginning of execution however, if there is a chance of |
165 | * HIDAPI handles being opened by different threads simultaneously. |
166 | * |
167 | * Each call to this function should have a matching call to SDL_hid_exit() |
168 | * |
169 | * \returns 0 on success or a negative error code on failure; call |
170 | * SDL_GetError() for more information. |
171 | * |
172 | * \since This function is available since SDL 3.2.0. |
173 | * |
174 | * \sa SDL_hid_exit |
175 | */ |
176 | extern SDL_DECLSPEC int SDLCALL SDL_hid_init(void); |
177 | |
178 | /** |
179 | * Finalize the HIDAPI library. |
180 | * |
181 | * This function frees all of the static data associated with HIDAPI. It |
182 | * should be called at the end of execution to avoid memory leaks. |
183 | * |
184 | * \returns 0 on success or a negative error code on failure; call |
185 | * SDL_GetError() for more information. |
186 | * |
187 | * \since This function is available since SDL 3.2.0. |
188 | * |
189 | * \sa SDL_hid_init |
190 | */ |
191 | extern SDL_DECLSPEC int SDLCALL SDL_hid_exit(void); |
192 | |
193 | /** |
194 | * Check to see if devices may have been added or removed. |
195 | * |
196 | * Enumerating the HID devices is an expensive operation, so you can call this |
197 | * to see if there have been any system device changes since the last call to |
198 | * this function. A change in the counter returned doesn't necessarily mean |
199 | * that anything has changed, but you can call SDL_hid_enumerate() to get an |
200 | * updated device list. |
201 | * |
202 | * Calling this function for the first time may cause a thread or other system |
203 | * resource to be allocated to track device change notifications. |
204 | * |
205 | * \returns a change counter that is incremented with each potential device |
206 | * change, or 0 if device change detection isn't available. |
207 | * |
208 | * \since This function is available since SDL 3.2.0. |
209 | * |
210 | * \sa SDL_hid_enumerate |
211 | */ |
212 | extern SDL_DECLSPEC Uint32 SDLCALL SDL_hid_device_change_count(void); |
213 | |
214 | /** |
215 | * Enumerate the HID Devices. |
216 | * |
217 | * This function returns a linked list of all the HID devices attached to the |
218 | * system which match vendor_id and product_id. If `vendor_id` is set to 0 |
219 | * then any vendor matches. If `product_id` is set to 0 then any product |
220 | * matches. If `vendor_id` and `product_id` are both set to 0, then all HID |
221 | * devices will be returned. |
222 | * |
223 | * By default SDL will only enumerate controllers, to reduce risk of hanging |
224 | * or crashing on bad drivers, but SDL_HINT_HIDAPI_ENUMERATE_ONLY_CONTROLLERS |
225 | * can be set to "0" to enumerate all HID devices. |
226 | * |
227 | * \param vendor_id the Vendor ID (VID) of the types of device to open, or 0 |
228 | * to match any vendor. |
229 | * \param product_id the Product ID (PID) of the types of device to open, or 0 |
230 | * to match any product. |
231 | * \returns a pointer to a linked list of type SDL_hid_device_info, containing |
232 | * information about the HID devices attached to the system, or NULL |
233 | * in the case of failure. Free this linked list by calling |
234 | * SDL_hid_free_enumeration(). |
235 | * |
236 | * \since This function is available since SDL 3.2.0. |
237 | * |
238 | * \sa SDL_hid_device_change_count |
239 | */ |
240 | extern SDL_DECLSPEC SDL_hid_device_info * SDLCALL SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id); |
241 | |
242 | /** |
243 | * Free an enumeration linked list. |
244 | * |
245 | * This function frees a linked list created by SDL_hid_enumerate(). |
246 | * |
247 | * \param devs pointer to a list of struct_device returned from |
248 | * SDL_hid_enumerate(). |
249 | * |
250 | * \since This function is available since SDL 3.2.0. |
251 | */ |
252 | extern SDL_DECLSPEC void SDLCALL SDL_hid_free_enumeration(SDL_hid_device_info *devs); |
253 | |
254 | /** |
255 | * Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally |
256 | * a serial number. |
257 | * |
258 | * If `serial_number` is NULL, the first device with the specified VID and PID |
259 | * is opened. |
260 | * |
261 | * \param vendor_id the Vendor ID (VID) of the device to open. |
262 | * \param product_id the Product ID (PID) of the device to open. |
263 | * \param serial_number the Serial Number of the device to open (Optionally |
264 | * NULL). |
265 | * \returns a pointer to a SDL_hid_device object on success or NULL on |
266 | * failure; call SDL_GetError() for more information. |
267 | * |
268 | * \since This function is available since SDL 3.2.0. |
269 | */ |
270 | extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number); |
271 | |
272 | /** |
273 | * Open a HID device by its path name. |
274 | * |
275 | * The path name be determined by calling SDL_hid_enumerate(), or a |
276 | * platform-specific path name can be used (eg: /dev/hidraw0 on Linux). |
277 | * |
278 | * \param path the path name of the device to open. |
279 | * \returns a pointer to a SDL_hid_device object on success or NULL on |
280 | * failure; call SDL_GetError() for more information. |
281 | * |
282 | * \since This function is available since SDL 3.2.0. |
283 | */ |
284 | extern SDL_DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path); |
285 | |
286 | /** |
287 | * Write an Output report to a HID device. |
288 | * |
289 | * The first byte of `data` must contain the Report ID. For devices which only |
290 | * support a single report, this must be set to 0x0. The remaining bytes |
291 | * contain the report data. Since the Report ID is mandatory, calls to |
292 | * SDL_hid_write() will always contain one more byte than the report contains. |
293 | * For example, if a hid report is 16 bytes long, 17 bytes must be passed to |
294 | * SDL_hid_write(), the Report ID (or 0x0, for devices with a single report), |
295 | * followed by the report data (16 bytes). In this example, the length passed |
296 | * in would be 17. |
297 | * |
298 | * SDL_hid_write() will send the data on the first OUT endpoint, if one |
299 | * exists. If it does not, it will send the data through the Control Endpoint |
300 | * (Endpoint 0). |
301 | * |
302 | * \param dev a device handle returned from SDL_hid_open(). |
303 | * \param data the data to send, including the report number as the first |
304 | * byte. |
305 | * \param length the length in bytes of the data to send. |
306 | * \returns the actual number of bytes written and -1 on on failure; call |
307 | * SDL_GetError() for more information. |
308 | * |
309 | * \since This function is available since SDL 3.2.0. |
310 | */ |
311 | extern SDL_DECLSPEC int SDLCALL SDL_hid_write(SDL_hid_device *dev, const unsigned char *data, size_t length); |
312 | |
313 | /** |
314 | * Read an Input report from a HID device with timeout. |
315 | * |
316 | * Input reports are returned to the host through the INTERRUPT IN endpoint. |
317 | * The first byte will contain the Report number if the device uses numbered |
318 | * reports. |
319 | * |
320 | * \param dev a device handle returned from SDL_hid_open(). |
321 | * \param data a buffer to put the read data into. |
322 | * \param length the number of bytes to read. For devices with multiple |
323 | * reports, make sure to read an extra byte for the report |
324 | * number. |
325 | * \param milliseconds timeout in milliseconds or -1 for blocking wait. |
326 | * \returns the actual number of bytes read and -1 on on failure; call |
327 | * SDL_GetError() for more information. If no packet was available to |
328 | * be read within the timeout period, this function returns 0. |
329 | * |
330 | * \since This function is available since SDL 3.2.0. |
331 | */ |
332 | extern SDL_DECLSPEC int SDLCALL SDL_hid_read_timeout(SDL_hid_device *dev, unsigned char *data, size_t length, int milliseconds); |
333 | |
334 | /** |
335 | * Read an Input report from a HID device. |
336 | * |
337 | * Input reports are returned to the host through the INTERRUPT IN endpoint. |
338 | * The first byte will contain the Report number if the device uses numbered |
339 | * reports. |
340 | * |
341 | * \param dev a device handle returned from SDL_hid_open(). |
342 | * \param data a buffer to put the read data into. |
343 | * \param length the number of bytes to read. For devices with multiple |
344 | * reports, make sure to read an extra byte for the report |
345 | * number. |
346 | * \returns the actual number of bytes read and -1 on failure; call |
347 | * SDL_GetError() for more information. If no packet was available to |
348 | * be read and the handle is in non-blocking mode, this function |
349 | * returns 0. |
350 | * |
351 | * \since This function is available since SDL 3.2.0. |
352 | */ |
353 | extern SDL_DECLSPEC int SDLCALL SDL_hid_read(SDL_hid_device *dev, unsigned char *data, size_t length); |
354 | |
355 | /** |
356 | * Set the device handle to be non-blocking. |
357 | * |
358 | * In non-blocking mode calls to SDL_hid_read() will return immediately with a |
359 | * value of 0 if there is no data to be read. In blocking mode, SDL_hid_read() |
360 | * will wait (block) until there is data to read before returning. |
361 | * |
362 | * Nonblocking can be turned on and off at any time. |
363 | * |
364 | * \param dev a device handle returned from SDL_hid_open(). |
365 | * \param nonblock enable or not the nonblocking reads - 1 to enable |
366 | * nonblocking - 0 to disable nonblocking. |
367 | * \returns 0 on success or a negative error code on failure; call |
368 | * SDL_GetError() for more information. |
369 | * |
370 | * \since This function is available since SDL 3.2.0. |
371 | */ |
372 | extern SDL_DECLSPEC int SDLCALL SDL_hid_set_nonblocking(SDL_hid_device *dev, int nonblock); |
373 | |
374 | /** |
375 | * Send a Feature report to the device. |
376 | * |
377 | * Feature reports are sent over the Control endpoint as a Set_Report |
378 | * transfer. The first byte of `data` must contain the Report ID. For devices |
379 | * which only support a single report, this must be set to 0x0. The remaining |
380 | * bytes contain the report data. Since the Report ID is mandatory, calls to |
381 | * SDL_hid_send_feature_report() will always contain one more byte than the |
382 | * report contains. For example, if a hid report is 16 bytes long, 17 bytes |
383 | * must be passed to SDL_hid_send_feature_report(): the Report ID (or 0x0, for |
384 | * devices which do not use numbered reports), followed by the report data (16 |
385 | * bytes). In this example, the length passed in would be 17. |
386 | * |
387 | * \param dev a device handle returned from SDL_hid_open(). |
388 | * \param data the data to send, including the report number as the first |
389 | * byte. |
390 | * \param length the length in bytes of the data to send, including the report |
391 | * number. |
392 | * \returns the actual number of bytes written and -1 on failure; call |
393 | * SDL_GetError() for more information. |
394 | * |
395 | * \since This function is available since SDL 3.2.0. |
396 | */ |
397 | extern SDL_DECLSPEC int SDLCALL SDL_hid_send_feature_report(SDL_hid_device *dev, const unsigned char *data, size_t length); |
398 | |
399 | /** |
400 | * Get a feature report from a HID device. |
401 | * |
402 | * Set the first byte of `data` to the Report ID of the report to be read. |
403 | * Make sure to allow space for this extra byte in `data`. Upon return, the |
404 | * first byte will still contain the Report ID, and the report data will start |
405 | * in data[1]. |
406 | * |
407 | * \param dev a device handle returned from SDL_hid_open(). |
408 | * \param data a buffer to put the read data into, including the Report ID. |
409 | * Set the first byte of `data` to the Report ID of the report to |
410 | * be read, or set it to zero if your device does not use numbered |
411 | * reports. |
412 | * \param length the number of bytes to read, including an extra byte for the |
413 | * report ID. The buffer can be longer than the actual report. |
414 | * \returns the number of bytes read plus one for the report ID (which is |
415 | * still in the first byte), or -1 on on failure; call SDL_GetError() |
416 | * for more information. |
417 | * |
418 | * \since This function is available since SDL 3.2.0. |
419 | */ |
420 | extern SDL_DECLSPEC int SDLCALL SDL_hid_get_feature_report(SDL_hid_device *dev, unsigned char *data, size_t length); |
421 | |
422 | /** |
423 | * Get an input report from a HID device. |
424 | * |
425 | * Set the first byte of `data` to the Report ID of the report to be read. |
426 | * Make sure to allow space for this extra byte in `data`. Upon return, the |
427 | * first byte will still contain the Report ID, and the report data will start |
428 | * in data[1]. |
429 | * |
430 | * \param dev a device handle returned from SDL_hid_open(). |
431 | * \param data a buffer to put the read data into, including the Report ID. |
432 | * Set the first byte of `data` to the Report ID of the report to |
433 | * be read, or set it to zero if your device does not use numbered |
434 | * reports. |
435 | * \param length the number of bytes to read, including an extra byte for the |
436 | * report ID. The buffer can be longer than the actual report. |
437 | * \returns the number of bytes read plus one for the report ID (which is |
438 | * still in the first byte), or -1 on on failure; call SDL_GetError() |
439 | * for more information. |
440 | * |
441 | * \since This function is available since SDL 3.2.0. |
442 | */ |
443 | extern SDL_DECLSPEC int SDLCALL SDL_hid_get_input_report(SDL_hid_device *dev, unsigned char *data, size_t length); |
444 | |
445 | /** |
446 | * Close a HID device. |
447 | * |
448 | * \param dev a device handle returned from SDL_hid_open(). |
449 | * \returns 0 on success or a negative error code on failure; call |
450 | * SDL_GetError() for more information. |
451 | * |
452 | * \since This function is available since SDL 3.2.0. |
453 | */ |
454 | extern SDL_DECLSPEC int SDLCALL SDL_hid_close(SDL_hid_device *dev); |
455 | |
456 | /** |
457 | * Get The Manufacturer String from a HID device. |
458 | * |
459 | * \param dev a device handle returned from SDL_hid_open(). |
460 | * \param string a wide string buffer to put the data into. |
461 | * \param maxlen the length of the buffer in multiples of wchar_t. |
462 | * \returns 0 on success or a negative error code on failure; call |
463 | * SDL_GetError() for more information. |
464 | * |
465 | * \since This function is available since SDL 3.2.0. |
466 | */ |
467 | extern SDL_DECLSPEC int SDLCALL SDL_hid_get_manufacturer_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen); |
468 | |
469 | /** |
470 | * Get The Product String from a HID device. |
471 | * |
472 | * \param dev a device handle returned from SDL_hid_open(). |
473 | * \param string a wide string buffer to put the data into. |
474 | * \param maxlen the length of the buffer in multiples of wchar_t. |
475 | * \returns 0 on success or a negative error code on failure; call |
476 | * SDL_GetError() for more information. |
477 | * |
478 | * \since This function is available since SDL 3.2.0. |
479 | */ |
480 | extern SDL_DECLSPEC int SDLCALL SDL_hid_get_product_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen); |
481 | |
482 | /** |
483 | * Get The Serial Number String from a HID device. |
484 | * |
485 | * \param dev a device handle returned from SDL_hid_open(). |
486 | * \param string a wide string buffer to put the data into. |
487 | * \param maxlen the length of the buffer in multiples of wchar_t. |
488 | * \returns 0 on success or a negative error code on failure; call |
489 | * SDL_GetError() for more information. |
490 | * |
491 | * \since This function is available since SDL 3.2.0. |
492 | */ |
493 | extern SDL_DECLSPEC int SDLCALL SDL_hid_get_serial_number_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen); |
494 | |
495 | /** |
496 | * Get a string from a HID device, based on its string index. |
497 | * |
498 | * \param dev a device handle returned from SDL_hid_open(). |
499 | * \param string_index the index of the string to get. |
500 | * \param string a wide string buffer to put the data into. |
501 | * \param maxlen the length of the buffer in multiples of wchar_t. |
502 | * \returns 0 on success or a negative error code on failure; call |
503 | * SDL_GetError() for more information. |
504 | * |
505 | * \since This function is available since SDL 3.2.0. |
506 | */ |
507 | extern SDL_DECLSPEC int SDLCALL SDL_hid_get_indexed_string(SDL_hid_device *dev, int string_index, wchar_t *string, size_t maxlen); |
508 | |
509 | /** |
510 | * Get the device info from a HID device. |
511 | * |
512 | * \param dev a device handle returned from SDL_hid_open(). |
513 | * \returns a pointer to the SDL_hid_device_info for this hid_device or NULL |
514 | * on failure; call SDL_GetError() for more information. This struct |
515 | * is valid until the device is closed with SDL_hid_close(). |
516 | * |
517 | * \since This function is available since SDL 3.2.0. |
518 | */ |
519 | extern SDL_DECLSPEC SDL_hid_device_info * SDLCALL SDL_hid_get_device_info(SDL_hid_device *dev); |
520 | |
521 | /** |
522 | * Get a report descriptor from a HID device. |
523 | * |
524 | * User has to provide a preallocated buffer where descriptor will be copied |
525 | * to. The recommended size for a preallocated buffer is 4096 bytes. |
526 | * |
527 | * \param dev a device handle returned from SDL_hid_open(). |
528 | * \param buf the buffer to copy descriptor into. |
529 | * \param buf_size the size of the buffer in bytes. |
530 | * \returns the number of bytes actually copied or -1 on failure; call |
531 | * SDL_GetError() for more information. |
532 | * |
533 | * \since This function is available since SDL 3.2.0. |
534 | */ |
535 | extern SDL_DECLSPEC int SDLCALL SDL_hid_get_report_descriptor(SDL_hid_device *dev, unsigned char *buf, size_t buf_size); |
536 | |
537 | /** |
538 | * Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers. |
539 | * |
540 | * \param active true to start the scan, false to stop the scan. |
541 | * |
542 | * \since This function is available since SDL 3.2.0. |
543 | */ |
544 | extern SDL_DECLSPEC void SDLCALL SDL_hid_ble_scan(bool active); |
545 | |
546 | /* Ends C function definitions when using C++ */ |
547 | #ifdef __cplusplus |
548 | } |
549 | #endif |
550 | #include <SDL3/SDL_close_code.h> |
551 | |
552 | #endif /* SDL_hidapi_h_ */ |
553 | |