SDL_hidapi_libusb.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. /* Define standard library functions in terms of SDL */
  19. /* #pragma push_macro/pop_macro works correctly only as of gcc >= 4.4.3
  20. clang-3.0 _seems_ to be OK. */
  21. #pragma push_macro("calloc")
  22. #pragma push_macro("malloc")
  23. #pragma push_macro("realloc")
  24. #pragma push_macro("free")
  25. #pragma push_macro("iconv_t")
  26. #pragma push_macro("iconv")
  27. #pragma push_macro("iconv_open")
  28. #pragma push_macro("iconv_close")
  29. #pragma push_macro("setlocale")
  30. #pragma push_macro("snprintf")
  31. #pragma push_macro("strcmp")
  32. #pragma push_macro("strdup")
  33. #pragma push_macro("strncpy")
  34. #pragma push_macro("tolower")
  35. #pragma push_macro("wcscmp")
  36. #pragma push_macro("wcsdup")
  37. #pragma push_macro("wcsncpy")
  38. #undef calloc
  39. #undef malloc
  40. #undef realloc
  41. #undef free
  42. #undef iconv_t
  43. #undef iconv
  44. #undef iconv_open
  45. #undef iconv_close
  46. #undef setlocale
  47. #undef snprintf
  48. #undef strcmp
  49. #undef strdup
  50. #undef strncpy
  51. #undef tolower
  52. #undef wcscmp
  53. #undef wcsdup
  54. #undef wcsncpy
  55. #define calloc SDL_calloc
  56. #define malloc SDL_malloc
  57. #define realloc SDL_realloc
  58. #define free SDL_free
  59. #define iconv_t SDL_iconv_t
  60. #ifndef ICONV_CONST
  61. #define ICONV_CONST
  62. #define UNDEF_ICONV_CONST
  63. #endif
  64. #define iconv(a,b,c,d,e) SDL_iconv(a, (const char **)b, c, d, e)
  65. #define iconv_open SDL_iconv_open
  66. #define iconv_close SDL_iconv_close
  67. #define setlocale(X, Y) NULL
  68. #define snprintf SDL_snprintf
  69. #define strcmp SDL_strcmp
  70. #define strdup SDL_strdup
  71. #define strncpy SDL_strlcpy
  72. #define tolower SDL_tolower
  73. #define wcscmp SDL_wcscmp
  74. #define wcsdup SDL_wcsdup
  75. #define wcsncpy SDL_wcslcpy
  76. /* this is awkwardly inlined, so we need to re-implement it here
  77. * so we can override the libusb_control_transfer call */
  78. static int SDL_libusb_get_string_descriptor(libusb_device_handle *dev,
  79. uint8_t descriptor_index, uint16_t lang_id,
  80. unsigned char *data, int length)
  81. {
  82. return libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN | 0x0, LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | descriptor_index, lang_id,
  83. data, (uint16_t)length, 1000); /* Endpoint 0 IN */
  84. }
  85. #define libusb_get_string_descriptor SDL_libusb_get_string_descriptor
  86. /* this is inlined, except on FreeBSD, so we need to re-implement it here */
  87. static void SDL_libusb_fill_interrupt_transfer(
  88. struct libusb_transfer *transfer, libusb_device_handle *dev_handle,
  89. unsigned char endpoint, unsigned char *buffer, int length,
  90. libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout)
  91. {
  92. transfer->dev_handle = dev_handle;
  93. transfer->endpoint = endpoint;
  94. transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT;
  95. transfer->timeout = timeout;
  96. transfer->buffer = buffer;
  97. transfer->length = length;
  98. transfer->user_data = user_data;
  99. transfer->callback = callback;
  100. }
  101. #define libusb_fill_interrupt_transfer SDL_libusb_fill_interrupt_transfer
  102. #define HIDAPI_THREAD_MODEL_INCLUDE "hidapi_thread_sdl.h"
  103. #ifndef LIBUSB_API_VERSION
  104. #ifdef LIBUSBX_API_VERSION
  105. #define LIBUSB_API_VERSION LIBUSBX_API_VERSION
  106. #else
  107. #define LIBUSB_API_VERSION 0x0
  108. #endif
  109. #endif
  110. /* we need libusb >= 1.0.16 because of libusb_get_port_numbers */
  111. /* we don't need libusb_wrap_sys_device: */
  112. #define HIDAPI_TARGET_LIBUSB_API_VERSION 0x01000102
  113. #undef HIDAPI_H__
  114. #include "libusb/hid.c"
  115. /* restore libc function macros */
  116. #ifdef UNDEF_ICONV_CONST
  117. #undef ICONV_CONST
  118. #undef UNDEF_ICONV_CONST
  119. #endif
  120. #pragma pop_macro("calloc")
  121. #pragma pop_macro("malloc")
  122. #pragma pop_macro("realloc")
  123. #pragma pop_macro("free")
  124. #pragma pop_macro("iconv_t")
  125. #pragma pop_macro("iconv")
  126. #pragma pop_macro("iconv_open")
  127. #pragma pop_macro("iconv_close")
  128. #pragma pop_macro("setlocale")
  129. #pragma pop_macro("snprintf")
  130. #pragma pop_macro("strcmp")
  131. #pragma pop_macro("strdup")
  132. #pragma pop_macro("strncpy")
  133. #pragma pop_macro("tolower")
  134. #pragma pop_macro("wcscmp")
  135. #pragma pop_macro("wcsdup")
  136. #pragma pop_macro("wcsncpy")