sdlhidapi.inc 18 KB

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