SDL_hidapi.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #if __LINUX__
  58. #include "../../core/linux/SDL_udev.h"
  59. #if SDL_USE_LIBUDEV
  60. static const SDL_UDEV_Symbols *udev_ctx = NULL;
  61. #define udev_device_get_sysattr_value udev_ctx->udev_device_get_sysattr_value
  62. #define udev_new udev_ctx->udev_new
  63. #define udev_unref udev_ctx->udev_unref
  64. #define udev_device_new_from_devnum udev_ctx->udev_device_new_from_devnum
  65. #define udev_device_get_parent_with_subsystem_devtype udev_ctx->udev_device_get_parent_with_subsystem_devtype
  66. #define udev_device_unref udev_ctx->udev_device_unref
  67. #define udev_enumerate_new udev_ctx->udev_enumerate_new
  68. #define udev_enumerate_add_match_subsystem udev_ctx->udev_enumerate_add_match_subsystem
  69. #define udev_enumerate_scan_devices udev_ctx->udev_enumerate_scan_devices
  70. #define udev_enumerate_get_list_entry udev_ctx->udev_enumerate_get_list_entry
  71. #define udev_list_entry_get_name udev_ctx->udev_list_entry_get_name
  72. #define udev_device_new_from_syspath udev_ctx->udev_device_new_from_syspath
  73. #define udev_device_get_devnode udev_ctx->udev_device_get_devnode
  74. #define udev_list_entry_get_next udev_ctx->udev_list_entry_get_next
  75. #define udev_enumerate_unref udev_ctx->udev_enumerate_unref
  76. #include "linux/hid.c"
  77. #define HAVE_PLATFORM_BACKEND 1
  78. #endif /* SDL_USE_LIBUDEV */
  79. #elif __MACOSX__
  80. #include "mac/hid.c"
  81. #define HAVE_PLATFORM_BACKEND 1
  82. #define udev_ctx 1
  83. #elif __WINDOWS__
  84. #include "windows/hid.c"
  85. #define HAVE_PLATFORM_BACKEND 1
  86. #define udev_ctx 1
  87. #else
  88. #error Need a hid.c for this platform!
  89. #endif
  90. #undef hid_device_
  91. #undef hid_device
  92. #undef hid_device_info
  93. #undef hid_init
  94. #undef hid_exit
  95. #undef hid_enumerate
  96. #undef hid_free_enumeration
  97. #undef hid_open
  98. #undef hid_open_path
  99. #undef hid_write
  100. #undef hid_read_timeout
  101. #undef hid_read
  102. #undef hid_set_nonblocking
  103. #undef hid_send_feature_report
  104. #undef hid_get_feature_report
  105. #undef hid_close
  106. #undef hid_get_manufacturer_string
  107. #undef hid_get_product_string
  108. #undef hid_get_serial_number_string
  109. #undef hid_get_indexed_string
  110. #undef hid_error
  111. #undef new_hid_device
  112. #undef free_hid_device
  113. #undef input_report
  114. #undef return_data
  115. #undef make_path
  116. #undef read_thread
  117. #ifndef SDL_LIBUSB_DYNAMIC
  118. #if __WINDOWS__
  119. #define SDL_LIBUSB_DYNAMIC "libusb-1.0.dll"
  120. #endif /* __WINDOWS__ */
  121. #endif /* SDL_LIBUSB_DYNAMIC */
  122. #ifdef SDL_LIBUSB_DYNAMIC
  123. /* libusb HIDAPI Implementation */
  124. /* Include this now, for our dynamically-loaded libusb context */
  125. #include <libusb.h>
  126. static struct
  127. {
  128. void* libhandle;
  129. int (*init)(libusb_context **ctx);
  130. void (*exit)(libusb_context *ctx);
  131. ssize_t (*get_device_list)(libusb_context *ctx, libusb_device ***list);
  132. void (*free_device_list)(libusb_device **list, int unref_devices);
  133. int (*get_device_descriptor)(libusb_device *dev, struct libusb_device_descriptor *desc);
  134. int (*get_active_config_descriptor)(libusb_device *dev, struct libusb_config_descriptor **config);
  135. int (*get_config_descriptor)(
  136. libusb_device *dev,
  137. uint8_t config_index,
  138. struct libusb_config_descriptor **config
  139. );
  140. void (*free_config_descriptor)(struct libusb_config_descriptor *config);
  141. uint8_t (*get_bus_number)(libusb_device *dev);
  142. uint8_t (*get_device_address)(libusb_device *dev);
  143. int (*open)(libusb_device *dev, libusb_device_handle **dev_handle);
  144. void (*close)(libusb_device_handle *dev_handle);
  145. int (*claim_interface)(libusb_device_handle *dev_handle, int interface_number);
  146. int (*release_interface)(libusb_device_handle *dev_handle, int interface_number);
  147. int (*kernel_driver_active)(libusb_device_handle *dev_handle, int interface_number);
  148. int (*detach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  149. int (*attach_kernel_driver)(libusb_device_handle *dev_handle, int interface_number);
  150. int (*set_interface_alt_setting)(libusb_device_handle *dev, int interface_number, int alternate_setting);
  151. struct libusb_transfer * (*alloc_transfer)(int iso_packets);
  152. int (*submit_transfer)(struct libusb_transfer *transfer);
  153. int (*cancel_transfer)(struct libusb_transfer *transfer);
  154. void (*free_transfer)(struct libusb_transfer *transfer);
  155. int (*control_transfer)(
  156. libusb_device_handle *dev_handle,
  157. uint8_t request_type,
  158. uint8_t bRequest,
  159. uint16_t wValue,
  160. uint16_t wIndex,
  161. unsigned char *data,
  162. uint16_t wLength,
  163. unsigned int timeout
  164. );
  165. int (*interrupt_transfer)(
  166. libusb_device_handle *dev_handle,
  167. unsigned char endpoint,
  168. unsigned char *data,
  169. int length,
  170. int *actual_length,
  171. unsigned int timeout
  172. );
  173. int (*handle_events)(libusb_context *ctx);
  174. int (*handle_events_completed)(libusb_context *ctx, int *completed);
  175. } libusb_ctx;
  176. #define libusb_init libusb_ctx.init
  177. #define libusb_exit libusb_ctx.exit
  178. #define libusb_get_device_list libusb_ctx.get_device_list
  179. #define libusb_free_device_list libusb_ctx.free_device_list
  180. #define libusb_get_device_descriptor libusb_ctx.get_device_descriptor
  181. #define libusb_get_active_config_descriptor libusb_ctx.get_active_config_descriptor
  182. #define libusb_get_config_descriptor libusb_ctx.get_config_descriptor
  183. #define libusb_free_config_descriptor libusb_ctx.free_config_descriptor
  184. #define libusb_get_bus_number libusb_ctx.get_bus_number
  185. #define libusb_get_device_address libusb_ctx.get_device_address
  186. #define libusb_open libusb_ctx.open
  187. #define libusb_close libusb_ctx.close
  188. #define libusb_claim_interface libusb_ctx.claim_interface
  189. #define libusb_release_interface libusb_ctx.release_interface
  190. #define libusb_kernel_driver_active libusb_ctx.kernel_driver_active
  191. #define libusb_detach_kernel_driver libusb_ctx.detach_kernel_driver
  192. #define libusb_attach_kernel_driver libusb_ctx.attach_kernel_driver
  193. #define libusb_set_interface_alt_setting libusb_ctx.set_interface_alt_setting
  194. #define libusb_alloc_transfer libusb_ctx.alloc_transfer
  195. #define libusb_submit_transfer libusb_ctx.submit_transfer
  196. #define libusb_cancel_transfer libusb_ctx.cancel_transfer
  197. #define libusb_free_transfer libusb_ctx.free_transfer
  198. #define libusb_control_transfer libusb_ctx.control_transfer
  199. #define libusb_interrupt_transfer libusb_ctx.interrupt_transfer
  200. #define libusb_handle_events libusb_ctx.handle_events
  201. #define libusb_handle_events_completed libusb_ctx.handle_events_completed
  202. #define hid_device_ LIBUSB_hid_device_
  203. #define hid_device LIBUSB_hid_device
  204. #define hid_device_info LIBUSB_hid_device_info
  205. #define hid_init LIBUSB_hid_init
  206. #define hid_exit LIBUSB_hid_exit
  207. #define hid_enumerate LIBUSB_hid_enumerate
  208. #define hid_free_enumeration LIBUSB_hid_free_enumeration
  209. #define hid_open LIBUSB_hid_open
  210. #define hid_open_path LIBUSB_hid_open_path
  211. #define hid_write LIBUSB_hid_write
  212. #define hid_read_timeout LIBUSB_hid_read_timeout
  213. #define hid_read LIBUSB_hid_read
  214. #define hid_set_nonblocking LIBUSB_hid_set_nonblocking
  215. #define hid_send_feature_report LIBUSB_hid_send_feature_report
  216. #define hid_get_feature_report LIBUSB_hid_get_feature_report
  217. #define hid_close LIBUSB_hid_close
  218. #define hid_get_manufacturer_string LIBUSB_hid_get_manufacturer_string
  219. #define hid_get_product_string LIBUSB_hid_get_product_string
  220. #define hid_get_serial_number_string LIBUSB_hid_get_serial_number_string
  221. #define hid_get_indexed_string LIBUSB_hid_get_indexed_string
  222. #define hid_error LIBUSB_hid_error
  223. #define new_hid_device LIBUSB_new_hid_device
  224. #define free_hid_device LIBUSB_free_hid_device
  225. #define input_report LIBUSB_input_report
  226. #define return_data LIBUSB_return_data
  227. #define make_path LIBUSB_make_path
  228. #define read_thread LIBUSB_read_thread
  229. #ifndef __FreeBSD__
  230. /* this is awkwardly inlined, so we need to re-implement it here
  231. * so we can override the libusb_control_transfer call */
  232. static int
  233. SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
  234. uint8_t descriptor_index, uint16_t lang_id,
  235. unsigned char *data, int length)
  236. {
  237. return libusb_control_transfer(dev,
  238. LIBUSB_ENDPOINT_IN | 0x0, /* Endpoint 0 IN */
  239. LIBUSB_REQUEST_GET_DESCRIPTOR,
  240. (LIBUSB_DT_STRING << 8) | descriptor_index,
  241. lang_id,
  242. data,
  243. (uint16_t) length,
  244. 1000);
  245. }
  246. #define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
  247. #endif /* __FreeBSD__ */
  248. #undef HIDAPI_H__
  249. #include "libusb/hid.c"
  250. #undef hid_device_
  251. #undef hid_device
  252. #undef hid_device_info
  253. #undef hid_init
  254. #undef hid_exit
  255. #undef hid_enumerate
  256. #undef hid_free_enumeration
  257. #undef hid_open
  258. #undef hid_open_path
  259. #undef hid_write
  260. #undef hid_read_timeout
  261. #undef hid_read
  262. #undef hid_set_nonblocking
  263. #undef hid_send_feature_report
  264. #undef hid_get_feature_report
  265. #undef hid_close
  266. #undef hid_get_manufacturer_string
  267. #undef hid_get_product_string
  268. #undef hid_get_serial_number_string
  269. #undef hid_get_indexed_string
  270. #undef hid_error
  271. #undef new_hid_device
  272. #undef free_hid_device
  273. #undef input_report
  274. #undef return_data
  275. #undef make_path
  276. #undef read_thread
  277. #endif /* SDL_LIBUSB_DYNAMIC */
  278. /* Shared HIDAPI Implementation */
  279. #undef HIDAPI_H__
  280. #include "hidapi.h"
  281. struct hidapi_backend {
  282. #define F(x) typeof(x) *x
  283. F(hid_write);
  284. F(hid_read_timeout);
  285. F(hid_read);
  286. F(hid_set_nonblocking);
  287. F(hid_send_feature_report);
  288. F(hid_get_feature_report);
  289. F(hid_close);
  290. F(hid_get_manufacturer_string);
  291. F(hid_get_product_string);
  292. F(hid_get_serial_number_string);
  293. F(hid_get_indexed_string);
  294. F(hid_error);
  295. #undef F
  296. };
  297. #if HAVE_PLATFORM_BACKEND
  298. static const struct hidapi_backend PLATFORM_Backend = {
  299. (void*)PLATFORM_hid_write,
  300. (void*)PLATFORM_hid_read_timeout,
  301. (void*)PLATFORM_hid_read,
  302. (void*)PLATFORM_hid_set_nonblocking,
  303. (void*)PLATFORM_hid_send_feature_report,
  304. (void*)PLATFORM_hid_get_feature_report,
  305. (void*)PLATFORM_hid_close,
  306. (void*)PLATFORM_hid_get_manufacturer_string,
  307. (void*)PLATFORM_hid_get_product_string,
  308. (void*)PLATFORM_hid_get_serial_number_string,
  309. (void*)PLATFORM_hid_get_indexed_string,
  310. (void*)PLATFORM_hid_error
  311. };
  312. #endif /* HAVE_PLATFORM_BACKEND */
  313. #ifdef SDL_LIBUSB_DYNAMIC
  314. static const struct hidapi_backend LIBUSB_Backend = {
  315. (void*)LIBUSB_hid_write,
  316. (void*)LIBUSB_hid_read_timeout,
  317. (void*)LIBUSB_hid_read,
  318. (void*)LIBUSB_hid_set_nonblocking,
  319. (void*)LIBUSB_hid_send_feature_report,
  320. (void*)LIBUSB_hid_get_feature_report,
  321. (void*)LIBUSB_hid_close,
  322. (void*)LIBUSB_hid_get_manufacturer_string,
  323. (void*)LIBUSB_hid_get_product_string,
  324. (void*)LIBUSB_hid_get_serial_number_string,
  325. (void*)LIBUSB_hid_get_indexed_string,
  326. (void*)LIBUSB_hid_error
  327. };
  328. #endif /* SDL_LIBUSB_DYNAMIC */
  329. typedef struct _HIDDeviceWrapper HIDDeviceWrapper;
  330. struct _HIDDeviceWrapper
  331. {
  332. hid_device *device; /* must be first field */
  333. const struct hidapi_backend *backend;
  334. };
  335. static HIDDeviceWrapper *
  336. CreateHIDDeviceWrapper(hid_device *device, const struct hidapi_backend *backend)
  337. {
  338. HIDDeviceWrapper *ret = SDL_malloc(sizeof(*ret));
  339. ret->device = device;
  340. ret->backend = backend;
  341. return ret;
  342. }
  343. static hid_device *
  344. WrapHIDDevice(HIDDeviceWrapper *wrapper)
  345. {
  346. return (hid_device *)wrapper;
  347. }
  348. static HIDDeviceWrapper *
  349. UnwrapHIDDevice(hid_device *device)
  350. {
  351. return (HIDDeviceWrapper *)device;
  352. }
  353. static void
  354. DeleteHIDDeviceWrapper(HIDDeviceWrapper *device)
  355. {
  356. SDL_free(device);
  357. }
  358. #define COPY_IF_EXISTS(var) \
  359. if (pSrc->var != NULL) { \
  360. pDst->var = SDL_strdup(pSrc->var); \
  361. } else { \
  362. pDst->var = NULL; \
  363. }
  364. #define WCOPY_IF_EXISTS(var) \
  365. if (pSrc->var != NULL) { \
  366. pDst->var = SDL_wcsdup(pSrc->var); \
  367. } else { \
  368. pDst->var = NULL; \
  369. }
  370. #ifdef SDL_LIBUSB_DYNAMIC
  371. static void
  372. LIBUSB_CopyHIDDeviceInfo(struct LIBUSB_hid_device_info *pSrc,
  373. struct hid_device_info *pDst)
  374. {
  375. COPY_IF_EXISTS(path)
  376. pDst->vendor_id = pSrc->vendor_id;
  377. pDst->product_id = pSrc->product_id;
  378. WCOPY_IF_EXISTS(serial_number)
  379. pDst->release_number = pSrc->release_number;
  380. WCOPY_IF_EXISTS(manufacturer_string)
  381. WCOPY_IF_EXISTS(product_string)
  382. pDst->usage_page = pSrc->usage_page;
  383. pDst->usage = pSrc->usage;
  384. pDst->interface_number = pSrc->interface_number;
  385. pDst->interface_class = pSrc->interface_class;
  386. pDst->interface_subclass = pSrc->interface_subclass;
  387. pDst->interface_protocol = pSrc->interface_protocol;
  388. pDst->next = NULL;
  389. }
  390. #endif /* SDL_LIBUSB_DYNAMIC */
  391. #if HAVE_PLATFORM_BACKEND
  392. static void
  393. PLATFORM_CopyHIDDeviceInfo(struct PLATFORM_hid_device_info *pSrc,
  394. struct hid_device_info *pDst)
  395. {
  396. COPY_IF_EXISTS(path)
  397. pDst->vendor_id = pSrc->vendor_id;
  398. pDst->product_id = pSrc->product_id;
  399. WCOPY_IF_EXISTS(serial_number)
  400. pDst->release_number = pSrc->release_number;
  401. WCOPY_IF_EXISTS(manufacturer_string)
  402. WCOPY_IF_EXISTS(product_string)
  403. pDst->usage_page = pSrc->usage_page;
  404. pDst->usage = pSrc->usage;
  405. pDst->interface_number = pSrc->interface_number;
  406. pDst->interface_class = pSrc->interface_class;
  407. pDst->interface_subclass = pSrc->interface_subclass;
  408. pDst->interface_protocol = pSrc->interface_protocol;
  409. pDst->next = NULL;
  410. }
  411. #endif /* HAVE_PLATFORM_BACKEND */
  412. #undef COPY_IF_EXISTS
  413. #undef WCOPY_IF_EXISTS
  414. static SDL_bool SDL_hidapi_wasinit = SDL_FALSE;
  415. int HID_API_EXPORT HID_API_CALL hid_init(void)
  416. {
  417. int err;
  418. if (SDL_hidapi_wasinit == SDL_TRUE) {
  419. return 0;
  420. }
  421. #ifdef SDL_LIBUSB_DYNAMIC
  422. libusb_ctx.libhandle = SDL_LoadObject(SDL_LIBUSB_DYNAMIC);
  423. if (libusb_ctx.libhandle != NULL) {
  424. #define LOAD_LIBUSB_SYMBOL(func) \
  425. libusb_ctx.func = SDL_LoadFunction(libusb_ctx.libhandle, "libusb_" #func);
  426. LOAD_LIBUSB_SYMBOL(init)
  427. LOAD_LIBUSB_SYMBOL(exit)
  428. LOAD_LIBUSB_SYMBOL(get_device_list)
  429. LOAD_LIBUSB_SYMBOL(free_device_list)
  430. LOAD_LIBUSB_SYMBOL(get_device_descriptor)
  431. LOAD_LIBUSB_SYMBOL(get_active_config_descriptor)
  432. LOAD_LIBUSB_SYMBOL(get_config_descriptor)
  433. LOAD_LIBUSB_SYMBOL(free_config_descriptor)
  434. LOAD_LIBUSB_SYMBOL(get_bus_number)
  435. LOAD_LIBUSB_SYMBOL(get_device_address)
  436. LOAD_LIBUSB_SYMBOL(open)
  437. LOAD_LIBUSB_SYMBOL(close)
  438. LOAD_LIBUSB_SYMBOL(claim_interface)
  439. LOAD_LIBUSB_SYMBOL(release_interface)
  440. LOAD_LIBUSB_SYMBOL(kernel_driver_active)
  441. LOAD_LIBUSB_SYMBOL(detach_kernel_driver)
  442. LOAD_LIBUSB_SYMBOL(attach_kernel_driver)
  443. LOAD_LIBUSB_SYMBOL(set_interface_alt_setting)
  444. LOAD_LIBUSB_SYMBOL(alloc_transfer)
  445. LOAD_LIBUSB_SYMBOL(submit_transfer)
  446. LOAD_LIBUSB_SYMBOL(cancel_transfer)
  447. LOAD_LIBUSB_SYMBOL(free_transfer)
  448. LOAD_LIBUSB_SYMBOL(control_transfer)
  449. LOAD_LIBUSB_SYMBOL(interrupt_transfer)
  450. LOAD_LIBUSB_SYMBOL(handle_events)
  451. LOAD_LIBUSB_SYMBOL(handle_events_completed)
  452. #undef LOAD_LIBUSB_SYMBOL
  453. if ((err = LIBUSB_hid_init()) < 0) {
  454. SDL_UnloadObject(libusb_ctx.libhandle);
  455. return err;
  456. }
  457. }
  458. #endif /* SDL_LIBUSB_DYNAMIC */
  459. #if HAVE_PLATFORM_BACKEND
  460. #if __LINUX__
  461. udev_ctx = SDL_UDEV_GetUdevSyms();
  462. #endif /* __LINUX __ */
  463. if (udev_ctx && (err = PLATFORM_hid_init()) < 0) {
  464. #ifdef SDL_LIBUSB_DYNAMIC
  465. if (libusb_ctx.libhandle) {
  466. SDL_UnloadObject(libusb_ctx.libhandle);
  467. }
  468. #endif /* SDL_LIBUSB_DYNAMIC */
  469. return err;
  470. }
  471. #endif /* HAVE_PLATFORM_BACKEND */
  472. return 0;
  473. }
  474. int HID_API_EXPORT HID_API_CALL hid_exit(void)
  475. {
  476. int err = 0;
  477. if (SDL_hidapi_wasinit == SDL_FALSE) {
  478. return 0;
  479. }
  480. #if HAVE_PLATFORM_BACKEND
  481. if (udev_ctx) {
  482. err = PLATFORM_hid_exit();
  483. }
  484. #endif /* HAVE_PLATFORM_BACKEND */
  485. #ifdef SDL_LIBUSB_DYNAMIC
  486. if (libusb_ctx.libhandle) {
  487. err |= LIBUSB_hid_exit(); /* Ehhhhh */
  488. SDL_UnloadObject(libusb_ctx.libhandle);
  489. }
  490. #endif /* SDL_LIBUSB_DYNAMIC */
  491. return err;
  492. }
  493. struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id)
  494. {
  495. #if HAVE_PLATFORM_BACKEND
  496. struct PLATFORM_hid_device_info *raw_devs = NULL;
  497. struct PLATFORM_hid_device_info *raw_dev;
  498. #endif /* HAVE_PLATFORM_BACKEND */
  499. struct hid_device_info *devs = NULL, *last = NULL, *new_dev;
  500. SDL_bool bFound;
  501. if (SDL_hidapi_wasinit == SDL_FALSE) {
  502. hid_init();
  503. }
  504. #if HAVE_PLATFORM_BACKEND
  505. if (udev_ctx) {
  506. raw_devs = PLATFORM_hid_enumerate(vendor_id, product_id);
  507. }
  508. #endif /* HAVE_PLATFORM_BACKEND */
  509. #ifdef SDL_LIBUSB_DYNAMIC
  510. if (libusb_ctx.libhandle) {
  511. struct LIBUSB_hid_device_info *usb_devs = LIBUSB_hid_enumerate(vendor_id, product_id);
  512. struct LIBUSB_hid_device_info *usb_dev;
  513. for (usb_dev = usb_devs; usb_dev; usb_dev = usb_dev->next) {
  514. bFound = SDL_FALSE;
  515. #if HAVE_PLATFORM_BACKEND
  516. for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
  517. if (raw_dev->vendor_id == 0x057e && raw_dev->product_id == 0x0337) {
  518. /* The GameCube adapter is handled by the USB HIDAPI driver */
  519. continue;
  520. }
  521. if (usb_dev->vendor_id == raw_dev->vendor_id &&
  522. usb_dev->product_id == raw_dev->product_id &&
  523. (raw_dev->interface_number < 0 || usb_dev->interface_number == raw_dev->interface_number)) {
  524. bFound = SDL_TRUE;
  525. break;
  526. }
  527. }
  528. #endif
  529. if (!bFound) {
  530. new_dev = (struct hid_device_info*) SDL_malloc(sizeof(struct hid_device_info));
  531. LIBUSB_CopyHIDDeviceInfo(usb_dev, new_dev);
  532. if (last != NULL) {
  533. last->next = new_dev;
  534. } else {
  535. devs = new_dev;
  536. }
  537. last = new_dev;
  538. }
  539. }
  540. LIBUSB_hid_free_enumeration(usb_devs);
  541. }
  542. #endif /* SDL_LIBUSB_DYNAMIC */
  543. #if HAVE_PLATFORM_BACKEND
  544. if (udev_ctx) {
  545. for (raw_dev = raw_devs; raw_dev; raw_dev = raw_dev->next) {
  546. bFound = SDL_FALSE;
  547. for (new_dev = devs; new_dev; new_dev = new_dev->next) {
  548. if (raw_dev->vendor_id == new_dev->vendor_id &&
  549. raw_dev->product_id == new_dev->product_id &&
  550. raw_dev->interface_number == new_dev->interface_number) {
  551. bFound = SDL_TRUE;
  552. break;
  553. }
  554. }
  555. if (!bFound) {
  556. new_dev = (struct hid_device_info*) SDL_malloc(sizeof(struct hid_device_info));
  557. PLATFORM_CopyHIDDeviceInfo(raw_dev, new_dev);
  558. new_dev->next = NULL;
  559. if (last != NULL) {
  560. last->next = new_dev;
  561. } else {
  562. devs = new_dev;
  563. }
  564. last = new_dev;
  565. }
  566. }
  567. PLATFORM_hid_free_enumeration(raw_devs);
  568. }
  569. #endif /* HAVE_PLATFORM_BACKEND */
  570. return devs;
  571. }
  572. void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs)
  573. {
  574. while (devs) {
  575. struct hid_device_info *next = devs->next;
  576. SDL_free(devs->path);
  577. SDL_free(devs->serial_number);
  578. SDL_free(devs->manufacturer_string);
  579. SDL_free(devs->product_string);
  580. SDL_free(devs);
  581. devs = next;
  582. }
  583. }
  584. HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
  585. {
  586. hid_device *pDevice = NULL;
  587. if (SDL_hidapi_wasinit == SDL_FALSE) {
  588. hid_init();
  589. }
  590. #if HAVE_PLATFORM_BACKEND
  591. if (udev_ctx &&
  592. (pDevice = (hid_device*) PLATFORM_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  593. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  594. return WrapHIDDevice(wrapper);
  595. }
  596. #endif /* HAVE_PLATFORM_BACKEND */
  597. #ifdef SDL_LIBUSB_DYNAMIC
  598. if (libusb_ctx.libhandle &&
  599. (pDevice = (hid_device*) LIBUSB_hid_open(vendor_id, product_id, serial_number)) != NULL) {
  600. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  601. return WrapHIDDevice(wrapper);
  602. }
  603. #endif /* SDL_LIBUSB_DYNAMIC */
  604. return NULL;
  605. }
  606. HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path, int bExclusive /* = false */)
  607. {
  608. hid_device *pDevice = NULL;
  609. if (SDL_hidapi_wasinit == SDL_FALSE) {
  610. hid_init();
  611. }
  612. #if HAVE_PLATFORM_BACKEND
  613. if (udev_ctx &&
  614. (pDevice = (hid_device*) PLATFORM_hid_open_path(path, bExclusive)) != NULL) {
  615. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &PLATFORM_Backend);
  616. return WrapHIDDevice(wrapper);
  617. }
  618. #endif /* HAVE_PLATFORM_BACKEND */
  619. #ifdef SDL_LIBUSB_DYNAMIC
  620. if (libusb_ctx.libhandle &&
  621. (pDevice = (hid_device*) LIBUSB_hid_open_path(path, bExclusive)) != NULL) {
  622. HIDDeviceWrapper *wrapper = CreateHIDDeviceWrapper(pDevice, &LIBUSB_Backend);
  623. return WrapHIDDevice(wrapper);
  624. }
  625. #endif /* SDL_LIBUSB_DYNAMIC */
  626. return NULL;
  627. }
  628. int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length)
  629. {
  630. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  631. return wrapper->backend->hid_write(wrapper->device, data, length);
  632. }
  633. int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds)
  634. {
  635. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  636. return wrapper->backend->hid_read_timeout(wrapper->device, data, length, milliseconds);
  637. }
  638. int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length)
  639. {
  640. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  641. return wrapper->backend->hid_read(wrapper->device, data, length);
  642. }
  643. int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock)
  644. {
  645. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  646. return wrapper->backend->hid_set_nonblocking(wrapper->device, nonblock);
  647. }
  648. int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length)
  649. {
  650. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  651. return wrapper->backend->hid_send_feature_report(wrapper->device, data, length);
  652. }
  653. int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length)
  654. {
  655. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  656. return wrapper->backend->hid_get_feature_report(wrapper->device, data, length);
  657. }
  658. void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device)
  659. {
  660. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  661. wrapper->backend->hid_close(wrapper->device);
  662. DeleteHIDDeviceWrapper(wrapper);
  663. }
  664. int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen)
  665. {
  666. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  667. return wrapper->backend->hid_get_manufacturer_string(wrapper->device, string, maxlen);
  668. }
  669. int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen)
  670. {
  671. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  672. return wrapper->backend->hid_get_product_string(wrapper->device, string, maxlen);
  673. }
  674. int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen)
  675. {
  676. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  677. return wrapper->backend->hid_get_serial_number_string(wrapper->device, string, maxlen);
  678. }
  679. int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen)
  680. {
  681. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  682. return wrapper->backend->hid_get_indexed_string(wrapper->device, string_index, string, maxlen);
  683. }
  684. HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device)
  685. {
  686. HIDDeviceWrapper *wrapper = UnwrapHIDDevice(device);
  687. return wrapper->backend->hid_error(wrapper->device);
  688. }
  689. #endif /* SDL_JOYSTICK_HIDAPI */