SDL_hidapi.h 21 KB

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