SDL_hidapi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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 "SDL_stdinc.h"
  50. #include "begin_code.h"
  51. /* Set up for C function definitions, even when using C++ */
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. /**
  56. * A handle representing an open HID device
  57. */
  58. struct SDL_hid_device_;
  59. typedef struct SDL_hid_device_ SDL_hid_device; /**< opaque hidapi structure */
  60. /** hidapi info structure */
  61. /**
  62. * Information about a connected HID device
  63. */
  64. typedef struct SDL_hid_device_info
  65. {
  66. /** Platform-specific device path */
  67. char *path;
  68. /** Device Vendor ID */
  69. unsigned short vendor_id;
  70. /** Device Product ID */
  71. unsigned short product_id;
  72. /** Serial Number */
  73. wchar_t *serial_number;
  74. /** Device Release Number in binary-coded decimal,
  75. also known as Device Version Number */
  76. unsigned short release_number;
  77. /** Manufacturer String */
  78. wchar_t *manufacturer_string;
  79. /** Product string */
  80. wchar_t *product_string;
  81. /** Usage Page for this Device/Interface
  82. (Windows/Mac only). */
  83. unsigned short usage_page;
  84. /** Usage for this Device/Interface
  85. (Windows/Mac only).*/
  86. unsigned short usage;
  87. /** The USB interface which this logical device
  88. represents.
  89. * Valid on both Linux implementations in all cases.
  90. * Valid on the Windows implementation only if the device
  91. contains more than one interface. */
  92. int interface_number;
  93. /** Additional information about the USB interface.
  94. Valid on libusb and Android implementations. */
  95. int interface_class;
  96. int interface_subclass;
  97. int interface_protocol;
  98. /** Pointer to the next device */
  99. struct SDL_hid_device_info *next;
  100. } SDL_hid_device_info;
  101. /**
  102. * Initialize the HIDAPI library.
  103. *
  104. * This function initializes the HIDAPI library. Calling it is not strictly
  105. * necessary, as it will be called automatically by SDL_hid_enumerate() and
  106. * any of the SDL_hid_open_*() functions if it is needed. This function should
  107. * be called at the beginning of execution however, if there is a chance of
  108. * HIDAPI handles being opened by different threads simultaneously.
  109. *
  110. * Each call to this function should have a matching call to SDL_hid_exit()
  111. *
  112. * \returns 0 on success and -1 on error.
  113. *
  114. * \since This function is available since SDL 2.0.18.
  115. *
  116. * \sa SDL_hid_exit
  117. */
  118. extern DECLSPEC int SDLCALL SDL_hid_init(void);
  119. /**
  120. * Finalize the HIDAPI library.
  121. *
  122. * This function frees all of the static data associated with HIDAPI. It
  123. * should be called at the end of execution to avoid memory leaks.
  124. *
  125. * \returns 0 on success and -1 on error.
  126. *
  127. * \since This function is available since SDL 2.0.18.
  128. *
  129. * \sa SDL_hid_init
  130. */
  131. extern DECLSPEC int SDLCALL SDL_hid_exit(void);
  132. /**
  133. * Check to see if devices may have been added or removed.
  134. *
  135. * Enumerating the HID devices is an expensive operation, so you can call this
  136. * to see if there have been any system device changes since the last call to
  137. * this function. A change in the counter returned doesn't necessarily mean
  138. * that anything has changed, but you can call SDL_hid_enumerate() to get an
  139. * updated device list.
  140. *
  141. * Calling this function for the first time may cause a thread or other system
  142. * resource to be allocated to track device change notifications.
  143. *
  144. * \returns a change counter that is incremented with each potential device
  145. * change, or 0 if device change detection isn't available.
  146. *
  147. * \since This function is available since SDL 2.0.18.
  148. *
  149. * \sa SDL_hid_enumerate
  150. */
  151. extern DECLSPEC Uint32 SDLCALL SDL_hid_device_change_count(void);
  152. /**
  153. * Enumerate the HID Devices.
  154. *
  155. * This function returns a linked list of all the HID devices attached to the
  156. * system which match vendor_id and product_id. If `vendor_id` is set to 0
  157. * then any vendor matches. If `product_id` is set to 0 then any product
  158. * matches. If `vendor_id` and `product_id` are both set to 0, then all HID
  159. * devices will be returned.
  160. *
  161. * \param vendor_id The Vendor ID (VID) of the types of device to open.
  162. * \param product_id The Product ID (PID) of the types of device to open.
  163. * \returns a pointer to a linked list of type SDL_hid_device_info, containing
  164. * information about the HID devices attached to the system, or NULL
  165. * in the case of failure. Free this linked list by calling
  166. * SDL_hid_free_enumeration().
  167. *
  168. * \since This function is available since SDL 2.0.18.
  169. *
  170. * \sa SDL_hid_device_change_count
  171. */
  172. extern DECLSPEC SDL_hid_device_info * SDLCALL SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id);
  173. /**
  174. * Free an enumeration Linked List
  175. *
  176. * This function frees a linked list created by SDL_hid_enumerate().
  177. *
  178. * \param devs Pointer to a list of struct_device returned from
  179. * SDL_hid_enumerate().
  180. *
  181. * \since This function is available since SDL 2.0.18.
  182. */
  183. extern DECLSPEC void SDLCALL SDL_hid_free_enumeration(SDL_hid_device_info *devs);
  184. /**
  185. * Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally
  186. * a serial number.
  187. *
  188. * If `serial_number` is NULL, the first device with the specified VID and PID
  189. * is opened.
  190. *
  191. * \param vendor_id The Vendor ID (VID) of the device to open.
  192. * \param product_id The Product ID (PID) of the device to open.
  193. * \param serial_number The Serial Number of the device to open (Optionally
  194. * NULL).
  195. * \returns a pointer to a SDL_hid_device object on success or NULL on
  196. * failure.
  197. *
  198. * \since This function is available since SDL 2.0.18.
  199. */
  200. extern DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
  201. /**
  202. * Open a HID device by its path name.
  203. *
  204. * The path name be determined by calling SDL_hid_enumerate(), or a
  205. * platform-specific path name can be used (eg: /dev/hidraw0 on Linux).
  206. *
  207. * \param path The path name of the device to open.
  208. * \returns a pointer to a SDL_hid_device object on success or NULL on
  209. * failure.
  210. *
  211. * \since This function is available since SDL 2.0.18.
  212. */
  213. extern DECLSPEC SDL_hid_device * SDLCALL SDL_hid_open_path(const char *path, int bExclusive);
  214. /**
  215. * Write an Output report to a HID device.
  216. *
  217. * The first byte of `data` must contain the Report ID. For devices which only
  218. * support a single report, this must be set to 0x0. The remaining bytes
  219. * contain the report data. Since the Report ID is mandatory, calls to
  220. * SDL_hid_write() will always contain one more byte than the report contains.
  221. * For example, if a hid report is 16 bytes long, 17 bytes must be passed to
  222. * SDL_hid_write(), the Report ID (or 0x0, for devices with a single report),
  223. * followed by the report data (16 bytes). In this example, the length passed
  224. * in would be 17.
  225. *
  226. * SDL_hid_write() will send the data on the first OUT endpoint, if one
  227. * exists. If it does not, it will send the data through the Control Endpoint
  228. * (Endpoint 0).
  229. *
  230. * \param dev A device handle returned from SDL_hid_open().
  231. * \param data The data to send, including the report number as the first
  232. * byte.
  233. * \param length The length in bytes of the data to send.
  234. * \returns the actual number of bytes written and -1 on error.
  235. *
  236. * \since This function is available since SDL 2.0.18.
  237. */
  238. extern DECLSPEC int SDLCALL SDL_hid_write(SDL_hid_device *dev, const unsigned char *data, size_t length);
  239. /**
  240. * Read an Input report from a HID device with timeout.
  241. *
  242. * Input reports are returned to the host through the INTERRUPT IN endpoint.
  243. * The first byte will contain the Report number if the device uses numbered
  244. * reports.
  245. *
  246. * \param dev A device handle returned from SDL_hid_open().
  247. * \param data A buffer to put the read data into.
  248. * \param length The number of bytes to read. For devices with multiple
  249. * reports, make sure to read an extra byte for the report
  250. * number.
  251. * \param milliseconds timeout in milliseconds or -1 for blocking wait.
  252. * \returns the actual number of bytes read and -1 on error. If no packet was
  253. * available to be read within the timeout period, this function
  254. * returns 0.
  255. *
  256. * \since This function is available since SDL 2.0.18.
  257. */
  258. extern DECLSPEC int SDLCALL SDL_hid_read_timeout(SDL_hid_device *dev, unsigned char *data, size_t length, int milliseconds);
  259. /**
  260. * Read an Input report from a HID device.
  261. *
  262. * Input reports are returned to the host through the INTERRUPT IN endpoint.
  263. * The first byte will contain the Report number if the device uses numbered
  264. * reports.
  265. *
  266. * \param dev A device handle returned from SDL_hid_open().
  267. * \param data A buffer to put the read data into.
  268. * \param length The number of bytes to read. For devices with multiple
  269. * reports, make sure to read an extra byte for the report
  270. * number.
  271. * \returns the actual number of bytes read and -1 on error. If no packet was
  272. * available to be read and the handle is in non-blocking mode, this
  273. * function returns 0.
  274. *
  275. * \since This function is available since SDL 2.0.18.
  276. */
  277. extern DECLSPEC int SDLCALL SDL_hid_read(SDL_hid_device *dev, unsigned char *data, size_t length);
  278. /**
  279. * Set the device handle to be non-blocking.
  280. *
  281. * In non-blocking mode calls to SDL_hid_read() will return immediately with a
  282. * value of 0 if there is no data to be read. In blocking mode, SDL_hid_read()
  283. * will wait (block) until there is data to read before returning.
  284. *
  285. * Nonblocking can be turned on and off at any time.
  286. *
  287. * \param dev A device handle returned from SDL_hid_open().
  288. * \param nonblock enable or not the nonblocking reads - 1 to enable
  289. * nonblocking - 0 to disable nonblocking.
  290. * \returns 0 on success and -1 on error.
  291. *
  292. * \since This function is available since SDL 2.0.18.
  293. */
  294. extern DECLSPEC int SDLCALL SDL_hid_set_nonblocking(SDL_hid_device *dev, int nonblock);
  295. /**
  296. * Send a Feature report to the device.
  297. *
  298. * Feature reports are sent over the Control endpoint as a Set_Report
  299. * transfer. The first byte of `data` must contain the Report ID. For devices
  300. * which only support a single report, this must be set to 0x0. The remaining
  301. * bytes contain the report data. Since the Report ID is mandatory, calls to
  302. * SDL_hid_send_feature_report() will always contain one more byte than the
  303. * report contains. For example, if a hid report is 16 bytes long, 17 bytes
  304. * must be passed to SDL_hid_send_feature_report(): the Report ID (or 0x0, for
  305. * devices which do not use numbered reports), followed by the report data (16
  306. * bytes). In this example, the length passed in would be 17.
  307. *
  308. * \param dev A device handle returned from SDL_hid_open().
  309. * \param data The data to send, including the report number as the first
  310. * byte.
  311. * \param length The length in bytes of the data to send, including the report
  312. * number.
  313. * \returns the actual number of bytes written and -1 on error.
  314. *
  315. * \since This function is available since SDL 2.0.18.
  316. */
  317. extern DECLSPEC int SDLCALL SDL_hid_send_feature_report(SDL_hid_device *dev, const unsigned char *data, size_t length);
  318. /**
  319. * Get a feature report from a HID device.
  320. *
  321. * Set the first byte of `data` to the Report ID of the report to be read.
  322. * Make sure to allow space for this extra byte in `data`. Upon return, the
  323. * first byte will still contain the Report ID, and the report data will start
  324. * in data[1].
  325. *
  326. * \param dev A device handle returned from SDL_hid_open().
  327. * \param data A buffer to put the read data into, including the Report ID.
  328. * Set the first byte of `data` to the Report ID of the report to
  329. * be read, or set it to zero if your device does not use numbered
  330. * reports.
  331. * \param length The number of bytes to read, including an extra byte for the
  332. * report ID. The buffer can be longer than the actual report.
  333. * \returns the number of bytes read plus one for the report ID (which is
  334. * still in the first byte), or -1 on error.
  335. *
  336. * \since This function is available since SDL 2.0.18.
  337. */
  338. extern DECLSPEC int SDLCALL SDL_hid_get_feature_report(SDL_hid_device *dev, unsigned char *data, size_t length);
  339. /**
  340. * Close a HID device.
  341. *
  342. * \param dev A device handle returned from SDL_hid_open().
  343. *
  344. * \since This function is available since SDL 2.0.18.
  345. */
  346. extern DECLSPEC void SDLCALL SDL_hid_close(SDL_hid_device *dev);
  347. /**
  348. * Get The Manufacturer String from a HID device.
  349. *
  350. * \param dev A device handle returned from SDL_hid_open().
  351. * \param string A wide string buffer to put the data into.
  352. * \param maxlen The length of the buffer in multiples of wchar_t.
  353. * \returns 0 on success and -1 on error.
  354. *
  355. * \since This function is available since SDL 2.0.18.
  356. */
  357. extern DECLSPEC int SDLCALL SDL_hid_get_manufacturer_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);
  358. /**
  359. * Get The Product String from a HID device.
  360. *
  361. * \param dev A device handle returned from SDL_hid_open().
  362. * \param string A wide string buffer to put the data into.
  363. * \param maxlen The length of the buffer in multiples of wchar_t.
  364. * \returns 0 on success and -1 on error.
  365. *
  366. * \since This function is available since SDL 2.0.18.
  367. */
  368. extern DECLSPEC int SDLCALL SDL_hid_get_product_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);
  369. /**
  370. * Get The Serial Number String from a HID device.
  371. *
  372. * \param dev A device handle returned from SDL_hid_open().
  373. * \param string A wide string buffer to put the data into.
  374. * \param maxlen The length of the buffer in multiples of wchar_t.
  375. * \returns 0 on success and -1 on error.
  376. *
  377. * \since This function is available since SDL 2.0.18.
  378. */
  379. extern DECLSPEC int SDLCALL SDL_hid_get_serial_number_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);
  380. /**
  381. * Get a string from a HID device, based on its string index.
  382. *
  383. * \param dev A device handle returned from SDL_hid_open().
  384. * \param string_index The index of the string to get.
  385. * \param string A wide string buffer to put the data into.
  386. * \param maxlen The length of the buffer in multiples of wchar_t.
  387. * \returns 0 on success and -1 on error.
  388. *
  389. * \since This function is available since SDL 2.0.18.
  390. */
  391. extern DECLSPEC int SDLCALL SDL_hid_get_indexed_string(SDL_hid_device *dev, int string_index, wchar_t *string, size_t maxlen);
  392. /**
  393. * Start or stop a BLE scan on iOS and tvOS to pair Steam Controllers
  394. *
  395. * \param active SDL_TRUE to start the scan, SDL_FALSE to stop the scan.
  396. *
  397. * \since This function is available since SDL 2.0.18.
  398. */
  399. extern DECLSPEC void SDLCALL SDL_hid_ble_scan(SDL_bool active);
  400. /* Ends C function definitions when using C++ */
  401. #ifdef __cplusplus
  402. }
  403. #endif
  404. #include "close_code.h"
  405. #endif /* SDL_hidapi_h_ */
  406. /* vi: set sts=4 ts=4 sw=4 expandtab: */