SDL_hidapi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 Sam Lantinga <[email protected]>
  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. /**
  19. * \file SDL_hidapi.h
  20. *
  21. * Header file for SDL HIDAPI functions.
  22. *
  23. * This is an adaptation of the original HIDAPI interface by Alan Ott,
  24. * and includes source code licensed under the following BSD license:
  25. *
  26. Copyright (c) 2010, Alan Ott, Signal 11 Software
  27. All rights reserved.
  28. Redistribution and use in source and binary forms, with or without
  29. modification, are permitted provided that the following conditions are met:
  30. * Redistributions of source code must retain the above copyright notice,
  31. this list of conditions and the following disclaimer.
  32. * Redistributions in binary form must reproduce the above copyright
  33. notice, this list of conditions and the following disclaimer in the
  34. documentation and/or other materials provided with the distribution.
  35. * Neither the name of Signal 11 Software nor the names of its
  36. contributors may be used to endorse or promote products derived from
  37. this software without specific prior written permission.
  38. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  39. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  42. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  43. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  44. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  45. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  46. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  48. POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. * If you would like a version of SDL without this code, you can build SDL
  51. * with SDL_DISABLE_HIDAPI defined. You might want to do this for example on
  52. * iOS or tvOS to avoid a dependency on the CoreBluetooth framework.
  53. */
  54. #ifndef SDL_hidapi_h_
  55. #define SDL_hidapi_h_
  56. /**
  57. * \brief A handle representing an open HID device
  58. */
  59. struct SDL_hid_device_;
  60. typedef struct SDL_hid_device_ SDL_hid_device; /**< opaque hidapi structure */
  61. /** hidapi info structure */
  62. /**
  63. * \brief Information about a connected HID device
  64. */
  65. typedef struct SDL_hid_device_info
  66. {
  67. /** Platform-specific device path */
  68. char *path;
  69. /** Device Vendor ID */
  70. unsigned short vendor_id;
  71. /** Device Product ID */
  72. unsigned short product_id;
  73. /** Serial Number */
  74. wchar_t *serial_number;
  75. /** Device Release Number in binary-coded decimal,
  76. also known as Device Version Number */
  77. unsigned short release_number;
  78. /** Manufacturer String */
  79. wchar_t *manufacturer_string;
  80. /** Product string */
  81. wchar_t *product_string;
  82. /** Usage Page for this Device/Interface
  83. (Windows/Mac only). */
  84. unsigned short usage_page;
  85. /** Usage for this Device/Interface
  86. (Windows/Mac only).*/
  87. unsigned short usage;
  88. /** The USB interface which this logical device
  89. represents.
  90. * Valid on both Linux implementations in all cases.
  91. * Valid on the Windows implementation only if the device
  92. contains more than one interface. */
  93. int interface_number;
  94. /** Additional information about the USB interface.
  95. Valid on libusb and Android implementations. */
  96. int interface_class;
  97. int interface_subclass;
  98. int interface_protocol;
  99. /** Pointer to the next device */
  100. struct SDL_hid_device_info *next;
  101. } SDL_hid_device_info;
  102. /**
  103. * Initialize the HIDAPI library.
  104. *
  105. * This function initializes the HIDAPI library. Calling it is not
  106. * strictly necessary, as it will be called automatically by
  107. * SDL_hid_enumerate() and any of the SDL_hid_open_*() functions
  108. * if it is needed. This function should be called at the beginning
  109. * of execution however, if there is a chance of HIDAPI handles
  110. * being opened by different threads simultaneously.
  111. *
  112. * \returns 0 on success and -1 on error.
  113. *
  114. * \since This function is available since SDL 2.0.18.
  115. */
  116. extern DECLSPEC int SDLCALL SDL_hid_init(void);
  117. /**
  118. * Finalize the HIDAPI library.
  119. *
  120. * This function frees all of the static data associated with
  121. * HIDAPI. It should be called at the end of execution to avoid
  122. * memory leaks.
  123. *
  124. * \returns 0 on success and -1 on error.
  125. *
  126. * \since This function is available since SDL 2.0.18.
  127. */
  128. extern DECLSPEC int SDLCALL SDL_hid_exit(void);
  129. /**
  130. * Enumerate the HID Devices.
  131. *
  132. * This function returns a linked list of all the HID devices
  133. * attached to the system which match vendor_id and product_id.
  134. * If `vendor_id` is set to 0 then any vendor matches.
  135. * If `product_id` is set to 0 then any product matches.
  136. * If `vendor_id` and `product_id` are both set to 0, then
  137. * all HID devices will be returned.
  138. *
  139. * \param vendor_id The Vendor ID (VID) of the types of device to open.
  140. * \param product_id The Product ID (PID) of the types of device to open.
  141. *
  142. * \returns a pointer to a linked list of type SDL_hid_device_info,
  143. * containing information about the HID devices attached to the
  144. * system, or NULL in the case of failure. Free this linked list
  145. * by calling SDL_hid_free_enumeration().
  146. *
  147. * \since This function is available since SDL 2.0.18.
  148. */
  149. extern DECLSPEC SDL_hid_device_info * SDLCALL SDL_hid_enumerate(unsigned short vendor_id, unsigned short product_id);
  150. /**
  151. * Free an enumeration Linked List
  152. *
  153. * This function frees a linked list created by SDL_hid_enumerate().
  154. *
  155. * \param devs Pointer to a list of struct_device returned from
  156. * SDL_hid_enumerate().
  157. */
  158. extern DECLSPEC void SDLCALL SDL_hid_free_enumeration(SDL_hid_device_info *devs);
  159. /**
  160. * Open a HID device using a Vendor ID (VID), Product ID
  161. * (PID) and optionally a serial number.
  162. *
  163. * If `serial_number` is NULL, the first device with the
  164. * specified VID and PID is opened.
  165. *
  166. * \param vendor_id The Vendor ID (VID) of the device to open.
  167. * \param product_id The Product ID (PID) of the device to open.
  168. * \param serial_number The Serial Number of the device to open
  169. * (Optionally NULL).
  170. *
  171. * \returns a pointer to a SDL_hid_device object on success or NULL on failure.
  172. *
  173. * \since This function is available since SDL 2.0.18.
  174. */
  175. SDL_hid_device * SDL_hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number);
  176. /**
  177. * Open a HID device by its path name.
  178. *
  179. * The path name be determined by calling SDL_hid_enumerate(), or a
  180. * platform-specific path name can be used (eg: /dev/hidraw0 on
  181. * Linux).
  182. *
  183. * \param path The path name of the device to open
  184. *
  185. * \returns a pointer to a SDL_hid_device object on success or NULL on failure.
  186. *
  187. * \since This function is available since SDL 2.0.18.
  188. */
  189. SDL_hid_device * SDL_hid_open_path(const char *path, int bExclusive /* = false */);
  190. /**
  191. * Write an Output report to a HID device.
  192. *
  193. * The first byte of `data` must contain the Report ID. For
  194. * devices which only support a single report, this must be set
  195. * to 0x0. The remaining bytes contain the report data. Since
  196. * the Report ID is mandatory, calls to SDL_hid_write() will always
  197. * contain one more byte than the report contains. For example,
  198. * if a hid report is 16 bytes long, 17 bytes must be passed to
  199. * SDL_hid_write(), the Report ID (or 0x0, for devices with a
  200. * single report), followed by the report data (16 bytes). In
  201. * this example, the length passed in would be 17.
  202. *
  203. * SDL_hid_write() will send the data on the first OUT endpoint, if
  204. * one exists. If it does not, it will send the data through
  205. * the Control Endpoint (Endpoint 0).
  206. *
  207. * \param dev A device handle returned from SDL_hid_open().
  208. * \param data The data to send, including the report number as
  209. * the first byte.
  210. * \param length The length in bytes of the data to send.
  211. *
  212. * \returns the actual number of bytes written and -1 on error.
  213. *
  214. * \since This function is available since SDL 2.0.18.
  215. */
  216. extern DECLSPEC int SDLCALL SDL_hid_write(SDL_hid_device *dev, const unsigned char *data, size_t length);
  217. /**
  218. * Read an Input report from a HID device with timeout.
  219. *
  220. * Input reports are returned
  221. * to the host through the INTERRUPT IN endpoint. The first byte will
  222. * contain the Report number if the device uses numbered reports.
  223. *
  224. * \param dev A device handle returned from SDL_hid_open().
  225. * \param data A buffer to put the read data into.
  226. * \param length The number of bytes to read. For devices with
  227. * multiple reports, make sure to read an extra byte for
  228. * the report number.
  229. * \param milliseconds timeout in milliseconds or -1 for blocking wait.
  230. *
  231. * \returns the actual number of bytes read and -1 on error.
  232. If no packet was available to be read within the timeout period, this function returns 0.
  233. *
  234. * \since This function is available since SDL 2.0.18.
  235. */
  236. extern DECLSPEC int SDLCALL SDL_hid_read_timeout(SDL_hid_device *dev, unsigned char *data, size_t length, int milliseconds);
  237. /**
  238. * Read an Input report from a HID device.
  239. *
  240. * Input reports are returned
  241. * to the host through the INTERRUPT IN endpoint. The first byte will
  242. * contain the Report number if the device uses numbered reports.
  243. *
  244. * \param dev A device handle returned from SDL_hid_open().
  245. * \param data A buffer to put the read data into.
  246. * \param length The number of bytes to read. For devices with
  247. * multiple reports, make sure to read an extra byte for
  248. * the report number.
  249. *
  250. * \returns the actual number of bytes read and -1 on error.
  251. * If no packet was available to be read and the handle is in non-blocking mode, this function returns 0.
  252. *
  253. * \since This function is available since SDL 2.0.18.
  254. */
  255. extern DECLSPEC int SDLCALL SDL_hid_read(SDL_hid_device *dev, unsigned char *data, size_t length);
  256. /**
  257. * Set the device handle to be non-blocking.
  258. *
  259. * In non-blocking mode calls to SDL_hid_read() will return
  260. * immediately with a value of 0 if there is no data to be
  261. * read. In blocking mode, SDL_hid_read() will wait (block) until
  262. * there is data to read before returning.
  263. *
  264. * Nonblocking can be turned on and off at any time.
  265. *
  266. * \param dev A device handle returned from SDL_hid_open().
  267. * \param nonblock enable or not the nonblocking reads
  268. * - 1 to enable nonblocking
  269. * - 0 to disable nonblocking.
  270. *
  271. * \returns 0 on success and -1 on error.
  272. *
  273. * \since This function is available since SDL 2.0.18.
  274. */
  275. extern DECLSPEC int SDLCALL SDL_hid_set_nonblocking(SDL_hid_device *dev, int nonblock);
  276. /**
  277. * Send a Feature report to the device.
  278. *
  279. * Feature reports are sent over the Control endpoint as a
  280. * Set_Report transfer. The first byte of `data` must
  281. * contain the Report ID. For devices which only support a
  282. * single report, this must be set to 0x0. The remaining bytes
  283. * contain the report data. Since the Report ID is mandatory,
  284. * calls to SDL_hid_send_feature_report() will always contain one
  285. * more byte than the report contains. For example, if a hid
  286. * report is 16 bytes long, 17 bytes must be passed to
  287. * SDL_hid_send_feature_report(): the Report ID (or 0x0, for
  288. * devices which do not use numbered reports), followed by the
  289. * report data (16 bytes). In this example, the length passed
  290. * in would be 17.
  291. *
  292. * \param dev A device handle returned from SDL_hid_open().
  293. * \param data The data to send, including the report number as
  294. * the first byte.
  295. * \param length The length in bytes of the data to send, including
  296. * the report number.
  297. *
  298. * \returns the actual number of bytes written and -1 on error.
  299. *
  300. * \since This function is available since SDL 2.0.18.
  301. */
  302. extern DECLSPEC int SDLCALL SDL_hid_send_feature_report(SDL_hid_device *dev, const unsigned char *data, size_t length);
  303. /**
  304. * Get a feature report from a HID device.
  305. *
  306. * Set the first byte of `data` to the Report ID of the
  307. * report to be read. Make sure to allow space for this
  308. * extra byte in `data`. Upon return, the first byte will
  309. * still contain the Report ID, and the report data will
  310. * start in data[1].
  311. *
  312. * \param dev A device handle returned from SDL_hid_open().
  313. * \param data A buffer to put the read data into, including
  314. * the Report ID. Set the first byte of `data` to the
  315. * Report ID of the report to be read, or set it to zero
  316. * if your device does not use numbered reports.
  317. * \param length The number of bytes to read, including an
  318. * extra byte for the report ID. The buffer can be longer
  319. * than the actual report.
  320. *
  321. * \returns the number of bytes read plus one for the report ID
  322. * (which is still in the first byte), or -1 on error.
  323. *
  324. * \since This function is available since SDL 2.0.18.
  325. */
  326. extern DECLSPEC int SDLCALL SDL_hid_get_feature_report(SDL_hid_device *dev, unsigned char *data, size_t length);
  327. /**
  328. * Close a HID device.
  329. *
  330. * \param dev A device handle returned from SDL_hid_open().
  331. */
  332. extern DECLSPEC void SDLCALL SDL_hid_close(SDL_hid_device *dev);
  333. /**
  334. * Get The Manufacturer String from a HID device.
  335. *
  336. * \param dev A device handle returned from SDL_hid_open().
  337. * \param string A wide string buffer to put the data into.
  338. * \param maxlen The length of the buffer in multiples of wchar_t.
  339. *
  340. * \returns 0 on success and -1 on error.
  341. *
  342. * \since This function is available since SDL 2.0.18.
  343. */
  344. extern DECLSPEC int SDLCALL SDL_hid_get_manufacturer_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);
  345. /**
  346. * Get The Product String from a HID device.
  347. *
  348. * \param dev A device handle returned from SDL_hid_open().
  349. * \param string A wide string buffer to put the data into.
  350. * \param maxlen The length of the buffer in multiples of wchar_t.
  351. *
  352. * \returns 0 on success and -1 on error.
  353. *
  354. * \since This function is available since SDL 2.0.18.
  355. */
  356. extern DECLSPEC int SDLCALL SDL_hid_get_product_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);
  357. /**
  358. * Get The Serial Number String from a HID device.
  359. *
  360. * \param dev A device handle returned from SDL_hid_open().
  361. * \param string A wide string buffer to put the data into.
  362. * \param maxlen The length of the buffer in multiples of wchar_t.
  363. *
  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_serial_number_string(SDL_hid_device *dev, wchar_t *string, size_t maxlen);
  369. /**
  370. * Get a string from a HID device, based on its string index.
  371. *
  372. * \param dev A device handle returned from SDL_hid_open().
  373. * \param string_index The index of the string to get.
  374. * \param string A wide string buffer to put the data into.
  375. * \param maxlen The length of the buffer in multiples of wchar_t.
  376. *
  377. * \returns 0 on success and -1 on error.
  378. *
  379. * \since This function is available since SDL 2.0.18.
  380. */
  381. extern DECLSPEC int SDLCALL SDL_hid_get_indexed_string(SDL_hid_device *dev, int string_index, wchar_t *string, size_t maxlen);
  382. #endif /* SDL_hidapi_h_ */
  383. /* vi: set sts=4 ts=4 sw=4 expandtab: */