SDL_hidapi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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. /* Original hybrid wrapper for Linux by Valve Software. Their original notes:
  19. *
  20. * The libusb version doesn't support Bluetooth, but not all Linux
  21. * distributions allow access to /dev/hidraw*
  22. *
  23. * This merges the two, at a small performance cost, until distributions
  24. * have granted access to /dev/hidraw*
  25. */
  26. #include "../SDL_internal.h"
  27. #include "SDL_loadso.h"
  28. #ifdef SDL_JOYSTICK_HIDAPI
  29. /* Platform HIDAPI Implementation */
  30. #define hid_device_ PLATFORM_hid_device_
  31. #define hid_device PLATFORM_hid_device
  32. #define hid_device_info PLATFORM_hid_device_info
  33. #define hid_init PLATFORM_hid_init
  34. #define hid_exit PLATFORM_hid_exit
  35. #define hid_enumerate PLATFORM_hid_enumerate
  36. #define hid_free_enumeration PLATFORM_hid_free_enumeration
  37. #define hid_open PLATFORM_hid_open
  38. #define hid_open_path PLATFORM_hid_open_path
  39. #define hid_write PLATFORM_hid_write
  40. #define hid_read_timeout PLATFORM_hid_read_timeout
  41. #define hid_read PLATFORM_hid_read
  42. #define hid_set_nonblocking PLATFORM_hid_set_nonblocking
  43. #define hid_send_feature_report PLATFORM_hid_send_feature_report
  44. #define hid_get_feature_report PLATFORM_hid_get_feature_report
  45. #define hid_close PLATFORM_hid_close
  46. #define hid_get_manufacturer_string PLATFORM_hid_get_manufacturer_string
  47. #define hid_get_product_string PLATFORM_hid_get_product_string
  48. #define hid_get_serial_number_string PLATFORM_hid_get_serial_number_string
  49. #define hid_get_indexed_string PLATFORM_hid_get_indexed_string
  50. #define hid_error PLATFORM_hid_error
  51. #define new_hid_device PLATFORM_new_hid_device
  52. #define free_hid_device PLATFORM_free_hid_device
  53. #define input_report PLATFORM_input_report
  54. #define return_data PLATFORM_return_data
  55. #define make_path PLATFORM_make_path
  56. #define read_thread PLATFORM_read_thread
  57. #undef HIDAPI_H__
  58. #if __LINUX__
  59. #include "../../core/linux/SDL_udev.h"
  60. #if SDL_USE_LIBUDEV
  61. static const SDL_UDEV_Symbols *udev_ctx = NULL;
  62. #define udev_device_get_sysattr_value udev_ctx->udev_device_get_sysattr_value
  63. #define udev_new udev_ctx->udev_new
  64. #define udev_unref udev_ctx->udev_unref
  65. #define udev_device_new_from_devnum udev_ctx->udev_device_new_from_devnum
  66. #define udev_device_get_parent_with_subsystem_devtype udev_ctx->udev_device_get_parent_with_subsystem_devtype
  67. #define udev_device_unref udev_ctx->udev_device_unref
  68. #define udev_enumerate_new udev_ctx->udev_enumerate_new
  69. #define udev_enumerate_add_match_subsystem udev_ctx->udev_enumerate_add_match_subsystem
  70. #define udev_enumerate_scan_devices udev_ctx->udev_enumerate_scan_devices
  71. #define udev_enumerate_get_list_entry udev_ctx->udev_enumerate_get_list_entry
  72. #define udev_list_entry_get_name udev_ctx->udev_list_entry_get_name
  73. #define udev_device_new_from_syspath udev_ctx->udev_device_new_from_syspath
  74. #define udev_device_get_devnode udev_ctx->udev_device_get_devnode
  75. #define udev_list_entry_get_next udev_ctx->udev_list_entry_get_next
  76. #define udev_enumerate_unref udev_ctx->udev_enumerate_unref
  77. #include "linux/hid.c"
  78. #define HAVE_PLATFORM_BACKEND 1
  79. #endif /* SDL_USE_LIBUDEV */
  80. #elif __MACOSX__
  81. #include "mac/hid.c"
  82. #define HAVE_PLATFORM_BACKEND 1
  83. #define udev_ctx 1
  84. #elif __WINDOWS__
  85. #include "windows/hid.c"
  86. #define HAVE_PLATFORM_BACKEND 1
  87. #define udev_ctx 1
  88. #else
  89. #error Need a hid.c for this platform!
  90. #endif
  91. #undef hid_device_
  92. #undef hid_device
  93. #undef hid_device_info
  94. #undef hid_init
  95. #undef hid_exit
  96. #undef hid_enumerate
  97. #undef hid_free_enumeration
  98. #undef hid_open
  99. #undef hid_open_path
  100. #undef hid_write
  101. #undef hid_read_timeout
  102. #undef hid_read
  103. #undef hid_set_nonblocking
  104. #undef hid_send_feature_report
  105. #undef hid_get_feature_report
  106. #undef hid_close
  107. #undef hid_get_manufacturer_string
  108. #undef hid_get_product_string
  109. #undef hid_get_serial_number_string
  110. #undef hid_get_indexed_string
  111. #undef hid_error
  112. #undef new_hid_device
  113. #undef free_hid_device
  114. #undef input_report
  115. #undef return_data
  116. #undef make_path
  117. #undef read_thread
  118. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  119. #define HAVE_DRIVER_BACKEND 1
  120. #endif
  121. #ifdef HAVE_DRIVER_BACKEND
  122. /* DRIVER HIDAPI Implementation */
  123. #define hid_device_ DRIVER_hid_device_
  124. #define hid_device DRIVER_hid_device
  125. #define hid_device_info DRIVER_hid_device_info
  126. #define hid_init DRIVER_hid_init
  127. #define hid_exit DRIVER_hid_exit
  128. #define hid_enumerate DRIVER_hid_enumerate
  129. #define hid_free_enumeration DRIVER_hid_free_enumeration
  130. #define hid_open DRIVER_hid_open
  131. #define hid_open_path DRIVER_hid_open_path
  132. #define hid_write DRIVER_hid_write
  133. #define hid_read_timeout DRIVER_hid_read_timeout
  134. #define hid_read DRIVER_hid_read
  135. #define hid_set_nonblocking DRIVER_hid_set_nonblocking
  136. #define hid_send_feature_report DRIVER_hid_send_feature_report
  137. #define hid_get_feature_report DRIVER_hid_get_feature_report
  138. #define hid_close DRIVER_hid_close
  139. #define hid_get_manufacturer_string DRIVER_hid_get_manufacturer_string
  140. #define hid_get_product_string DRIVER_hid_get_product_string
  141. #define hid_get_serial_number_string DRIVER_hid_get_serial_number_string
  142. #define hid_get_indexed_string DRIVER_hid_get_indexed_string
  143. #define hid_error DRIVER_hid_error
  144. #ifdef SDL_JOYSTICK_HIDAPI_STEAMXBOX
  145. #undef HIDAPI_H__
  146. #include "steamxbox/hid.c"
  147. #else
  148. #error Need a driver hid.c for this platform!
  149. #endif
  150. #undef hid_device_
  151. #undef hid_device
  152. #undef hid_device_info
  153. #undef hid_init
  154. #undef hid_exit
  155. #undef hid_enumerate
  156. #undef hid_free_enumeration
  157. #undef hid_open
  158. #undef hid_open_path
  159. #undef hid_write
  160. #undef hid_read_timeout
  161. #undef hid_read
  162. #undef hid_set_nonblocking
  163. #undef hid_send_feature_report
  164. #undef hid_get_feature_report
  165. #undef hid_close
  166. #undef hid_get_manufacturer_string
  167. #undef hid_get_product_string
  168. #undef hid_get_serial_number_string
  169. #undef hid_get_indexed_string
  170. #undef hid_error
  171. #endif /* HAVE_DRIVER_BACKEND */
  172. #ifdef SDL_LIBUSB_DYNAMIC
  173. /* libusb HIDAPI Implementation */
  174. /* Include this now, for our dynamically-loaded libusb context */
  175. #include <libusb.h>
  176. static struct
  177. {
  178. void* libhandle;
  179. int (*init)(libusb_context **ctx);
  180. void (*exit)(libusb_context *ctx);
  181. ssize_t (*get_device_list)(libusb_context *ctx, libusb_device ***list);
  182. void (*free_device_list)(libusb_device **list, int unref_devices);
  183. int (*get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
  184. int (*get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);
  185. int (*get_config_descriptor)(
  186. libusb_device *dev,
  187. uint8_t config_index,
  188. struct libusb_config_descriptor **config
  189. );
  190. void (*free_config_descriptor)(struct libusb_config_descriptor *config);
  191. uint8_t (*get_bus_number)(libusb_device *dev);
  192. uint8_t (*get_device_address)(libusb_device *dev);
  193. int (*open)(libusb_device *dev, libusb_device_handle **dev_handle);
  194. void (*close)(libusb_device_handle *dev_handle);
  195. int (*claim_interface)(libusb_device_handle *dev_handle, int interface_number);
  196. int (*release_interface)(libusb_device_handle *dev_handle, int interface_number);
  197. int (*kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
  198. int (*detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  199. int (*attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  200. int (*set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
  201. struct libusb_transfer * (*alloc_transfer)(int iso_packets);
  202. int (*submit_transfer)(struct libusb_transfer *transfer);
  203. int (*cancel_transfer)(struct libusb_transfer *transfer);
  204. void (*free_transfer)(struct libusb_transfer *transfer);
  205. int (*control_transfer)(
  206. libusb_device_handle *dev_handle,
  207. uint8_t request_type,
  208. uint8_t bRequest,
  209. uint16_t wValue,
  210. uint16_t wIndex,
  211. unsigned char *data,
  212. uint16_t wLength,
  213. unsigned int timeout
  214. );
  215. int (*interrupt_transfer)(
  216. libusb_device_handle *dev_handle,
  217. unsigned char endpoint,
  218. unsigned char *data,
  219. int length,
  220. int *actual_length,
  221. unsigned int timeout
  222. );
  223. int (*handle_events)(libusb_context *ctx);
  224. int (*handle_events_completed)(libusb_context *ctx, int *completed);
  225. } libusb_ctx;
  226. #define libusb_init libusb_ctx.init
  227. #define libusb_exit libusb_ctx.exit
  228. #define libusb_get_device_list libusb_ctx.get_device_list
  229. #define libusb_free_device_list libusb_ctx.free_device_list
  230. #define libusb_get_device_descriptor libusb_ctx.get_device_descriptor
  231. #define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
  232. #define libusb_get_config_descriptor libusb_ctx.get_config_descriptor
  233. #define libusb_free_config_descriptor libusb_ctx.free_config_descriptor
  234. #define libusb_get_bus_number libusb_ctx.get_bus_number
  235. #define libusb_get_device_address libusb_ctx.get_device_address
  236. #define libusb_open libusb_ctx.open
  237. #define libusb_close libusb_ctx.close
  238. #define libusb_claim_interface libusb_ctx.claim_interface
  239. #define libusb_release_interface libusb_ctx.release_interface
  240. #define libusb_kernel_driver_active libusb_ctx.kernel_driver_active
  241. #define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver
  242. #define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver
  243. #define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting
  244. #define libusb_alloc_transfer libusb_ctx.alloc_transfer
  245. #define libusb_submit_transfer libusb_ctx.submit_transfer
  246. #define libusb_cancel_transfer libusb_ctx.cancel_transfer
  247. #define libusb_free_transfer libusb_ctx.free_transfer
  248. #define libusb_control_transfer libusb_ctx.control_transfer
  249. #define libusb_interrupt_transfer libusb_ctx.interrupt_transfer
  250. #define libusb_handle_events libusb_ctx.handle_events
  251. #define libusb_handle_events_completed libusb_ctx.handle_events_completed
  252. #define hid_device_ LIBUSB_hid_device_
  253. #define hid_device LIBUSB_hid_device
  254. #define hid_device_info LIBUSB_hid_device_info
  255. #define hid_init LIBUSB_hid_init
  256. #define hid_exit LIBUSB_hid_exit
  257. #define hid_enumerate LIBUSB_hid_enumerate
  258. #define hid_free_enumeration LIBUSB_hid_free_enumeration
  259. #define hid_open LIBUSB_hid_open
  260. #define hid_open_path LIBUSB_hid_open_path
  261. #define hid_write LIBUSB_hid_write
  262. #define hid_read_timeout LIBUSB_hid_read_timeout
  263. #define hid_read LIBUSB_hid_read
  264. #define hid_set_nonblocking LIBUSB_hid_set_nonblocking
  265. #define hid_send_feature_report LIBUSB_hid_send_feature_report
  266. #define hid_get_feature_report LIBUSB_hid_get_feature_report
  267. #define hid_close LIBUSB_hid_close
  268. #define hid_get_manufacturer_string LIBUSB_hid_get_manufacturer_string
  269. #define hid_get_product_string LIBUSB_hid_get_product_string
  270. #define hid_get_serial_number_string LIBUSB_hid_get_serial_number_string
  271. #define hid_get_indexed_string LIBUSB_hid_get_indexed_string
  272. #define hid_error LIBUSB_hid_error
  273. #define new_hid_device LIBUSB_new_hid_device
  274. #define free_hid_device LIBUSB_free_hid_device
  275. #define input_report LIBUSB_input_report
  276. #define return_data LIBUSB_return_data
  277. #define make_path LIBUSB_make_path
  278. #define read_thread LIBUSB_read_thread
  279. #ifndef __FreeBSD__
  280. /* this is awkwardly inlined, so we need to re-implement it here
  281. * so we can override the libusb_control_transfer call */
  282. static int
  283. SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
  284. uint8_t descriptor_index, uint16_t lang_id,
  285. unsigned char *data, int length)
  286. {
  287. return libusb_control_transfer(dev,
  288. LIBUSB_ENDPOINT_IN | 0x0, /* Endpoint 0 IN */
  289. LIBUSB_REQUEST_GET_DESCRIPTOR,
  290. (LIBUSB_DT_STRING << 8) | descriptor_index,
  291. lang_id,
  292. data,
  293. (uint16_t) length,
  294. 1000);
  295. }
  296. #define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
  297. #endif /* __FreeBSD__ */
  298. #undef HIDAPI_H__
  299. #include "libusb/hid.c"
  300. #undef hid_device_
  301. #undef hid_device
  302. #undef hid_device_info
  303. #undef hid_init
  304. #undef hid_exit
  305. #undef hid_enumerate
  306. #undef hid_free_enumeration
  307. #undef hid_open
  308. #undef hid_open_path
  309. #undef hid_write
  310. #undef hid_read_timeout
  311. #undef hid_read
  312. #undef hid_set_nonblocking
  313. #undef hid_send_feature_report
  314. #undef hid_get_feature_report
  315. #undef hid_close
  316. #undef hid_get_manufacturer_string
  317. #undef hid_get_product_string
  318. #undef hid_get_serial_number_string
  319. #undef hid_get_indexed_string
  320. #undef hid_error
  321. #undef new_hid_device
  322. #undef free_hid_device
  323. #undef input_report
  324. #undef return_data
  325. #undef make_path
  326. #undef read_thread
  327. #endif /* SDL_LIBUSB_DYNAMIC */
  328. /* Shared HIDAPI Implementation */
  329. #undef HIDAPI_H__
  330. #include "hidapi/hidapi.h"
  331. struct hidapi_backend {
  332. int (*hid_write)(hid_device* device, const unsigned char* data, size_t length);
  333. int (*hid_read_timeout)(hid_device* device, unsigned char* data, size_t length, int milliseconds);
  334. int (*hid_read)(hid_device* device, unsigned char* data, size_t length);
  335. int (*hid_set_nonblocking)(hid_device* device, int nonblock);
  336. int (*hid_send_feature_report)(hid_device* device, const unsigned char* data, size_t length);
  337. int (*hid_get_feature_report)(hid_device* device, unsigned char* data, size_t length);
  338. void (*hid_close)(hid_device* device);
  339. int (*hid_get_manufacturer_string)(hid_device* device, wchar_t* string, size_t maxlen);
  340. int (*hid_get_product_string)(hid_device* device, wchar_t* string, size_t maxlen);
  341. int (*hid_get_serial_number_string)(hid_device* device, wchar_t* string, size_t maxlen);
  342. int (*hid_get_indexed_string)(hid_device* device, int string_index, wchar_t* string, size_t maxlen);
  343. const wchar_t* (*hid_error)(hid_device* device);
  344. };
  345. #if HAVE_PLATFORM_BACKEND
  346. static const struct hidapi_backend PLATFORM_Backend = {
  347. (void*)PLATFORM_hid_write,
  348. (void*)PLATFORM_hid_read_timeout,
  349. (void*)PLATFORM_hid_read,
  350. (void*)PLATFORM_hid_set_nonblocking,
  351. (void*)PLATFORM_hid_send_feature_report,
  352. (void*)PLATFORM_hid_get_feature_report,
  353. (void*)PLATFORM_hid_close,
  354. (void*)PLATFORM_hid_get_manufacturer_string,
  355. (void*)PLATFORM_hid_get_product_string,
  356. (void*)PLATFORM_hid_get_serial_number_string,
  357. (void*)PLATFORM_hid_get_indexed_string,
  358. (void*)PLATFORM_hid_error
  359. };
  360. #endif /* HAVE_PLATFORM_BACKEND */
  361. #if HAVE_DRIVER_BACKEND
  362. static const struct hidapi_backend DRIVER_Backend = {
  363. (void*)DRIVER_hid_write,
  364. (void*)DRIVER_hid_read_timeout,
  365. (void*)DRIVER_hid_read,
  366. (void*)DRIVER_hid_set_nonblocking,
  367. (void*)DRIVER_hid_send_feature_report,
  368. (void*)DRIVER_hid_get_feature_report,
  369. (void*)DRIVER_hid_close,
  370. (void*)DRIVER_hid_get_manufacturer_string,
  371. (void*)DRIVER_hid_get_product_string,
  372. (void*)DRIVER_hid_get_serial_number_string,
  373. (void*)DRIVER_hid_get_indexed_string,
  374. (void*)DRIVER_hid_error
  375. };
  376. #endif /* HAVE_DRIVER_BACKEND */
  377. #ifdef SDL_LIBUSB_DYNAMIC
  378. static const struct hidapi_backend LIBUSB_Backend = {
  379. (void*)LIBUSB_hid_write,
  380. (void*)LIBUSB_hid_read_timeout,
  381. (void*)LIBUSB_hid_read,
  382. (void*)LIBUSB_hid_set_nonblocking,
  383. (void*)LIBUSB_hid_send_feature_report,
  384. (void*)LIBUSB_hid_get_feature_report,
  385. (void*)LIBUSB_hid_close,
  386. (void*)LIBUSB_hid_get_manufacturer_string,
  387. (void*)LIBUSB_hid_get_product_string,
  388. (void*)LIBUSB_hid_get_serial_number_string,
  389. (void*)LIBUSB_hid_get_indexed_string,
  390. (void*)LIBUSB_hid_error
  391. };
  392. #endif /* SDL_LIBUSB_DYNAMIC */
  393. typedef struct _HIDDeviceWrapper HIDDeviceWrapper;
  394. struct _HIDDeviceWrapper
  395. {
  396. hid_device *device; /* must be first field */
  397. const struct hidapi_backend *backend;
  398. };
  399. static HIDDeviceWrapper *
  400. CreateHIDDeviceWrapper(hid_device *device, const struct hidapi_backend *backend)
  401. {
  402. HIDDeviceWrapper *ret = (HIDDeviceWrapper *)SDL_malloc(sizeof(*ret));
  403. ret->device = device;
  404. ret->backend = backend;
  405. return ret;
  406. }
  407. static hid_device *
  408. WrapHIDDevice(HIDDeviceWrapper *wrapper)
  409. {
  410. return (hid_device *)wrapper;
  411. }
  412. static HIDDeviceWrapper *
  413. UnwrapHIDDevice(hid_device *device)
  414. {
  415. return (HIDDeviceWrapper *)device;
  416. }
  417. static void
  418. DeleteHIDDeviceWrapper(HIDDeviceWrapper *device)
  419. {
  420. SDL_free(device);
  421. }
  422. #define COPY_IF_EXISTS(var) \
  423. if (pSrc->var != NULL) { \
  424. pDst->var = SDL_strdup(pSrc->var); \
  425. } else { \
  426. pDst->var = NULL; \
  427. }
  428. #define WCOPY_IF_EXISTS(var) \
  429. if (pSrc->var != NULL) { \
  430. pDst->var = SDL_wcsdup(pSrc->var); \
  431. } else { \
  432. pDst->var = NULL; \
  433. }
  434. #ifdef SDL_LIBUSB_DYNAMIC
  435. static void
  436. LIBUSB_CopyHIDDeviceInfo(struct LIBUSB_hid_device_info *pSrc,
  437. struct hid_device_info *pDst)
  438. {
  439. COPY_IF_EXISTS(path)
  440. pDst->vendor_id = pSrc->vendor_id;
  441. pDst->product_id = pSrc->product_id;
  442. WCOPY_IF_EXISTS(serial_number)
  443. pDst->release_number = pSrc->release_number;
  444. WCOPY_IF_EXISTS(manufacturer_string)
  445. WCOPY_IF_EXISTS(product_string)
  446. pDst->usage_page = pSrc->usage_page;
  447. pDst->usage = pSrc->usage;
  448. pDst->interface_number = pSrc->interface_number;
  449. pDst->interface_class = pSrc->interface_class;
  450. pDst->interface_subclass = pSrc->interface_subclass;
  451. pDst->interface_protocol = pSrc->interface_protocol;
  452. pDst->next = NULL;
  453. }
  454. #endif /* SDL_LIBUSB_DYNAMIC */
  455. #if HAVE_DRIVER_BACKEND
  456. static void
  457. DRIVER_CopyHIDDeviceInfo(struct DRIVER_hid_device_info *pSrc,
  458. struct hid_device_info *pDst)
  459. {
  460. COPY_IF_EXISTS(path)
  461. pDst->vendor_id = pSrc->vendor_id;
  462. pDst->product_id = pSrc->product_id;
  463. WCOPY_IF_EXISTS(serial_number)
  464. pDst->release_number = pSrc->release_number;
  465. WCOPY_IF_EXISTS(manufacturer_string)
  466. WCOPY_IF_EXISTS(product_string)
  467. pDst->usage_page = pSrc->usage_page;
  468. pDst->usage = pSrc->usage;
  469. pDst->interface_number = pSrc->interface_number;
  470. pDst->interface_class = pSrc->interface_class;
  471. pDst->interface_subclass = pSrc->interface_subclass;
  472. pDst->interface_protocol = pSrc->interface_protocol;
  473. pDst->next = NULL;
  474. }
  475. #endif /* HAVE_DRIVER_BACKEND */
  476. #if HAVE_PLATFORM_BACKEND
  477. static void
  478. PLATFORM_CopyHIDDeviceInfo(struct PLATFORM_hid_device_info *pSrc,
  479. struct hid_device_info *pDst)
  480. {
  481. COPY_IF_EXISTS(path)
  482. pDst->vendor_id = pSrc->vendor_id;
  483. pDst->product_id = pSrc->product_id;
  484. WCOPY_IF_EXISTS(serial_number)
  485. pDst->release_number = pSrc->release_number;
  486. WCOPY_IF_EXISTS(manufacturer_string)
  487. WCOPY_IF_EXISTS(product_string)
  488. pDst->usage_page = pSrc->usage_page;
  489. pDst->usage = pSrc->usage;
  490. pDst->interface_number = pSrc->interface_number;
  491. pDst->interface_class = pSrc->interface_class;
  492. pDst->interface_subclass = pSrc->interface_subclass;
  493. pDst->interface_protocol = pSrc->interface_protocol;
  494. pDst->next = NULL;
  495. }
  496. #endif /* HAVE_PLATFORM_BACKEND */
  497. #undef COPY_IF_EXISTS
  498. #undef WCOPY_IF_EXISTS
  499. static SDL_bool SDL_hidapi_wasinit = SDL_FALSE;
  500. int HID_API_EXPORT HID_API_CALL hid_init(void)
  501. {
  502. int err;
  503. if (SDL_hidapi_wasinit == SDL_TRUE) {
  504. return 0;
  505. }
  506. #ifdef SDL_LIBUSB_DYNAMIC
  507. libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
  508. if (libusb_ctx.libhandle != NULL) {
  509. SDL_bool loaded = SDL_TRUE;
  510. #define LOAD_LIBUSB_SYMBOL(func) \
  511. if (!(libusb_ctx.func = SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func))) {loaded = SDL_FALSE;}
  512. LOAD_LIBUSB_SYMBOL(init)
  513. LOAD_LIBUSB_SYMBOL(exit)
  514. LOAD_LIBUSB_SYMBOL(get_device_list)
  515. LOAD_LIBUSB_SYMBOL(free_device_list)
  516. LOAD_LIBUSB_SYMBOL(get_device_descriptor)
  517. LOAD_LIBUSB_SYMBOL(get_active_config_descriptor)
  518. LOAD_LIBUSB_SYMBOL(get_config_descriptor)
  519. LOAD_LIBUSB_SYMBOL(free_config_descriptor)
  520. LOAD_LIBUSB_SYMBOL(get_bus_number)
  521. LOAD_LIBUSB_SYMBOL(get_device_address)
  522. LOAD_LIBUSB_SYMBOL(open)
  523. LOAD_LIBUSB_SYMBOL(close)
  524. LOAD_LIBUSB_SYMBOL(claim_interface)
  525. LOAD_LIBUSB_SYMBOL(release_interface)
  526. LOAD_LIBUSB_SYMBOL(kernel_driver_active)
  527. LOAD_LIBUSB_SYMBOL(detach_kernel_driver)
  528. LOAD_LIBUSB_SYMBOL(attach_kernel_driver)
  529. LOAD_LIBUSB_SYMBOL(set_interface_alt_setting)
  530. LOAD_LIBUSB_SYMBOL(alloc_transfer)
  531. LOAD_LIBUSB_SYMBOL(submit_transfer)
  532. LOAD_LIBUSB_SYMBOL(cancel_transfer)
  533. LOAD_LIBUSB_SYMBOL(free_transfer)
  534. LOAD_LIBUSB_SYMBOL(control_transfer)
  535. LOAD_LIBUSB_SYMBOL(interrupt_transfer)
  536. LOAD_LIBUSB_SYMBOL(handle_events)
  537. LOAD_LIBUSB_SYMBOL(handle_events_completed)
  538. #undef LOAD_LIBUSB_SYMBOL
  539. if (loaded == SDL_TRUE) {
  540. if ((err = LIBUSB_hid_init()) < 0) {
  541. SDL_UnloadObject(libusb_ctx.libhandle);
  542. libusb_ctx.libhandle = NULL;
  543. return err;
  544. }
  545. } else {
  546. SDL_UnloadObject(libusb_ctx.libhandle);
  547. libusb_ctx.libhandle = NULL;
  548. /* SDL_LogWarn(SDL_LOG_CATEGORY_INPUT, SDL_LIBUSB_DYNAMIC " found but could not load function."); */
  549. /* ignore error: continue without libusb */
  550. }
  551. }
  552. #endif /* SDL_LIBUSB_DYNAMIC */
  553. #if HAVE_PLATFORM_BACKEND
  554. #if __LINUX__
  555. udev_ctx = SDL_UDEV_GetUdevSyms();
  556. #endif /* __LINUX __ */
  557. if (udev_ctx && (err = PLATFORM_hid_init()) < 0) {
  558. #ifdef SDL_LIBUSB_DYNAMIC
  559. if (libusb_ctx.libhandle) {
  560. LIBUSB_hid_exit();
  561. SDL_UnloadObject(libusb_ctx.libhandle);
  562. libusb_ctx.libhandle = NULL;
  563. }
  564. #endif /* SDL_LIBUSB_DYNAMIC */
  565. return err;
  566. }
  567. #endif /* HAVE_PLATFORM_BACKEND */
  568. SDL_hidapi_wasinit = SDL_TRUE;
  569. return 0;
  570. }
  571. int HID_API_EXPORT HID_API_CALL hid_exit(void)
  572. {
  573. int err = 0;
  574. if (SDL_hidapi_wasinit == SDL_FALSE) {
  575. return 0;
  576. }
  577. SDL_hidapi_wasinit = SDL_FALSE;
  578. #if HAVE_PLATFORM_BACKEND
  579. if (udev_ctx) {
  580. err = PLATFORM_hid_exit();
  581. }
  582. #endif /* HAVE_PLATFORM_BACKEND */
  583. #ifdef SDL_LIBUSB_DYNAMIC
  584. if (libusb_ctx.libhandle) {
  585. err |= LIBUSB_hid_exit(); /* Ehhhhh */
  586. SDL_UnloadObject(libusb_ctx.libhandle);
  587. libusb_ctx.libhandle = NULL;
  588. }
  589. #endif /* SDL_LIBUSB_DYNAMIC */
  590. return err;
  591. }
  592. struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
  593. {
  594. #ifdef SDL_LIBUSB_DYNAMIC
  595. struct LIBUSB_hid_device_info *usb_devs = NULL;
  596. struct LIBUSB_hid_device_info *usb_dev;
  597. #endif
  598. #if HAVE_DRIVER_BACKEND
  599. struct DRIVER_hid_device_info* driver_devs = NULL;
  600. struct DRIVER_hid_device_info* driver_dev;
  601. #endif
  602. #if HAVE_PLATFORM_BACKEND
  603. struct PLATFORM_hid_device_info *raw_devs = NULL;
  604. struct PLATFORM_hid_device_info *raw_dev;
  605. #endif
  606. struct hid_device_info *devs = NULL, *last = NULL, *new_dev;
  607. if (hid_init() != 0) {
  608. return NULL;
  609. }
  610. #ifdef SDL_LIBUSB_DYNAMIC
  611. if (libusb_ctx.libhandle) {
  612. usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
  613. #ifdef DEBUG_HIDAPI
  614. SDL_Log("libusb devices found:");
  615. #endif
  616. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  617. new_dev = (struct hid_device_info*) SDL_malloc(sizeof(struct hid_device_info));
  618. if (!new_dev) {
  619. LIBUSB_hid_free_enumeration(usb_devs);
  620. hid_free_enumeration(devs);
  621. SDL_OutOfMemory();
  622. return NULL;
  623. }
  624. LIBUSB_CopyHIDDeviceInfo(usb_dev, new_dev);
  625. #ifdef DEBUG_HIDAPI
  626. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  627. usb_dev->manufacturer_string, usb_dev->product_string,
  628. usb_dev->vendor_id, usb_dev->product_id);
  629. #endif
  630. if (last != NULL) {
  631. last->next = new_dev;
  632. } else {
  633. devs = new_dev;
  634. }
  635. last = new_dev;
  636. }
  637. }
  638. #endif /* SDL_LIBUSB_DYNAMIC */
  639. #ifdef HAVE_DRIVER_BACKEND
  640. driver_devs = DRIVER_hid_enumerate(vendor_id, product_id);
  641. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  642. new_dev = (struct hid_device_info*) SDL_malloc(sizeof(struct hid_device_info));
  643. DRIVER_CopyHIDDeviceInfo(driver_dev, new_dev);
  644. if (last != NULL) {
  645. last->next = new_dev;
  646. } else {
  647. devs = new_dev;
  648. }
  649. last = new_dev;
  650. }
  651. #endif /* HAVE_DRIVER_BACKEND */
  652. #if HAVE_PLATFORM_BACKEND
  653. if (udev_ctx) {
  654. raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
  655. #ifdef DEBUG_HIDAPI
  656. SDL_Log("hidraw devices found:");
  657. #endif
  658. for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
  659. SDL_bool bFound = SDL_FALSE;
  660. #ifdef DEBUG_HIDAPI
  661. SDL_Log(" - %ls %ls 0x%.4hx 0x%.4hx",
  662. raw_dev->manufacturer_string, raw_dev->product_string,
  663. raw_dev->vendor_id, raw_dev->product_id);
  664. #endif
  665. #ifdef SDL_LIBUSB_DYNAMIC
  666. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  667. if (raw_dev->vendor_id == usb_dev->vendor_id &&
  668. raw_dev->product_id == usb_dev->product_id &&
  669. (raw_dev->interface_number < 0 || raw_dev->interface_number == usb_dev->interface_number)) {
  670. bFound = SDL_TRUE;
  671. break;
  672. }
  673. }
  674. #endif
  675. #ifdef HAVE_DRIVER_BACKEND
  676. for (driver_dev = driver_devs; driver_dev; driver_dev = driver_dev->next) {
  677. if (raw_dev->vendor_id == driver_dev->vendor_id &&
  678. raw_dev->product_id == driver_dev->product_id &&
  679. (raw_dev->interface_number < 0 || raw_dev->interface_number == driver_dev->interface_number)) {
  680. bFound = SDL_TRUE;
  681. break;
  682. }
  683. }
  684. #endif
  685. if (!bFound) {
  686. new_dev = (struct hid_device_info*) SDL_malloc(sizeof(struct hid_device_info));
  687. if (!new_dev) {
  688. #ifdef SDL_LIBUSB_DYNAMIC
  689. if (libusb_ctx.libhandle) {
  690. LIBUSB_hid_free_enumeration(usb_devs);
  691. }
  692. #endif
  693. PLATFORM_hid_free_enumeration(raw_devs);
  694. hid_free_enumeration(devs);
  695. SDL_OutOfMemory();
  696. return NULL;
  697. }
  698. PLATFORM_CopyHIDDeviceInfo(raw_dev, new_dev);
  699. new_dev->next = NULL;
  700. if (last != NULL) {
  701. last->next = new_dev;
  702. } else {
  703. devs = new_dev;
  704. }
  705. last = new_dev;
  706. }
  707. }
  708. PLATFORM_hid_free_enumeration(raw_devs);
  709. }
  710. #endif /* HAVE_PLATFORM_BACKEND */
  711. #ifdef SDL_LIBUSB_DYNAMIC
  712. if (libusb_ctx.libhandle) {
  713. LIBUSB_hid_free_enumeration(usb_devs);
  714. }
  715. #endif
  716. return devs;
  717. }
  718. void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
  719. {
  720. while (devs) {
  721. struct hid_device_info *next = devs->next;
  722. SDL_free(devs->path);
  723. SDL_free(devs->serial_number);
  724. SDL_free(devs->manufacturer_string);
  725. SDL_free(devs->product_string);
  726. SDL_free(devs);
  727. devs = next;
  728. }
  729. }
  730. HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
  731. {
  732. hid_device *pDevice = NULL;
  733. if (hid_init() != 0) {
  734. return NULL;
  735. }
  736. #if HAVE_PLATFORM_BACKEND
  737. if (udev_ctx &&
  738. (pDevice = (hid_device*) PLATFORM_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  739. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  740. return WrapHIDDevice(wrapper);
  741. }
  742. #endif /* HAVE_PLATFORM_BACKEND */
  743. #if HAVE_DRIVER_BACKEND
  744. if ((pDevice = (hid_device*) DRIVER_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  745. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  746. return WrapHIDDevice(wrapper);
  747. }
  748. #endif /* HAVE_DRIVER_BACKEND */
  749. #ifdef SDL_LIBUSB_DYNAMIC
  750. if (libusb_ctx.libhandle &&
  751. (pDevice = (hid_device*) LIBUSB_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  752. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  753. return WrapHIDDevice(wrapper);
  754. }
  755. #endif /* SDL_LIBUSB_DYNAMIC */
  756. return NULL;
  757. }
  758. HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path, int bExclusive /* = false */)
  759. {
  760. hid_device *pDevice = NULL;
  761. if (hid_init() != 0) {
  762. return NULL;
  763. }
  764. #if HAVE_PLATFORM_BACKEND
  765. if (udev_ctx &&
  766. (pDevice = (hid_device*) PLATFORM_hid_open_path(path, bExclusive)) != NULL) {
  767. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  768. return WrapHIDDevice(wrapper);
  769. }
  770. #endif /* HAVE_PLATFORM_BACKEND */
  771. #if HAVE_DRIVER_BACKEND
  772. if ((pDevice = (hid_device*) DRIVER_hid_open_path(path, bExclusive)) != NULL) {
  773. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &DRIVER_Backend);
  774. return WrapHIDDevice(wrapper);
  775. }
  776. #endif /* HAVE_DRIVER_BACKEND */
  777. #ifdef SDL_LIBUSB_DYNAMIC
  778. if (libusb_ctx.libhandle &&
  779. (pDevice = (hid_device*) LIBUSB_hid_open_path(path, bExclusive)) != NULL) {
  780. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  781. return WrapHIDDevice(wrapper);
  782. }
  783. #endif /* SDL_LIBUSB_DYNAMIC */
  784. return NULL;
  785. }
  786. int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
  787. {
  788. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  789. return wrapper->backend->hid_write(wrapper->device, data, length);
  790. }
  791. int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
  792. {
  793. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  794. return wrapper->backend->hid_read_timeout(wrapper->device, data, length, milliseconds);
  795. }
  796. int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
  797. {
  798. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  799. return wrapper->backend->hid_read(wrapper->device, data, length);
  800. }
  801. int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
  802. {
  803. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  804. return wrapper->backend->hid_set_nonblocking(wrapper->device, nonblock);
  805. }
  806. int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
  807. {
  808. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  809. return wrapper->backend->hid_send_feature_report(wrapper->device, data, length);
  810. }
  811. int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
  812. {
  813. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  814. return wrapper->backend->hid_get_feature_report(wrapper->device, data, length);
  815. }
  816. void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
  817. {
  818. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  819. wrapper->backend->hid_close(wrapper->device);
  820. DeleteHIDDeviceWrapper(wrapper);
  821. }
  822. int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
  823. {
  824. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  825. return wrapper->backend->hid_get_manufacturer_string(wrapper->device, string, maxlen);
  826. }
  827. int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
  828. {
  829. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  830. return wrapper->backend->hid_get_product_string(wrapper->device, string, maxlen);
  831. }
  832. int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
  833. {
  834. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  835. return wrapper->backend->hid_get_serial_number_string(wrapper->device, string, maxlen);
  836. }
  837. int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
  838. {
  839. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  840. return wrapper->backend->hid_get_indexed_string(wrapper->device, string_index, string, maxlen);
  841. }
  842. HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device)
  843. {
  844. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  845. return wrapper->backend->hid_error(wrapper->device);
  846. }
  847. #endif /* SDL_JOYSTICK_HIDAPI */
  848. /* vi: set sts=4 ts=4 sw=4 expandtab: */