SDL_hidapi.c 36 KB

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