SDL_hidapi_combined.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. /* This driver supports the Nintendo Switch Joy-Cons pair controllers */
  19. #include "../../SDL_internal.h"
  20. #ifdef SDL_JOYSTICK_HIDAPI
  21. #include "SDL_hints.h"
  22. #include "SDL_joystick.h"
  23. #include "SDL_gamecontroller.h"
  24. #include "SDL_hidapijoystick_c.h"
  25. #include "../SDL_sysjoystick.h"
  26. static SDL_bool
  27. HIDAPI_DriverCombined_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  28. {
  29. /* This is always explicitly created for combined devices */
  30. return SDL_FALSE;
  31. }
  32. static const char *
  33. HIDAPI_DriverCombined_GetDeviceName(const char *name, Uint16 vendor_id, Uint16 product_id)
  34. {
  35. return NULL;
  36. }
  37. static SDL_bool
  38. HIDAPI_DriverCombined_InitDevice(SDL_HIDAPI_Device *device)
  39. {
  40. return HIDAPI_JoystickConnected(device, NULL);
  41. }
  42. static int
  43. HIDAPI_DriverCombined_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  44. {
  45. return -1;
  46. }
  47. static void
  48. HIDAPI_DriverCombined_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  49. {
  50. }
  51. static SDL_bool
  52. HIDAPI_DriverCombined_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  53. {
  54. int i;
  55. char *serial = NULL, *new_serial;
  56. size_t serial_length = 0, new_length;
  57. for (i = 0; i < device->num_children; ++i) {
  58. SDL_HIDAPI_Device *child = device->children[i];
  59. if (!child->driver->OpenJoystick(child, joystick)) {
  60. while (i-- > 0) {
  61. child = device->children[i];
  62. child->driver->CloseJoystick(child, joystick);
  63. }
  64. if (serial) {
  65. SDL_free(serial);
  66. }
  67. return SDL_FALSE;
  68. }
  69. /* Extend the serial number with the child serial number */
  70. if (joystick->serial) {
  71. new_length = serial_length + 1 + SDL_strlen(joystick->serial);
  72. new_serial = (char *)SDL_realloc(serial, new_length);
  73. if (new_serial) {
  74. if (serial) {
  75. SDL_strlcat(new_serial, ",", new_length);
  76. SDL_strlcat(new_serial, joystick->serial, new_length);
  77. } else {
  78. SDL_strlcpy(new_serial, joystick->serial, new_length);
  79. }
  80. serial = new_serial;
  81. serial_length = new_length;
  82. }
  83. SDL_free(joystick->serial);
  84. joystick->serial = NULL;
  85. }
  86. }
  87. /* Update the joystick with the combined serial numbers */
  88. if (joystick->serial) {
  89. SDL_free(joystick->serial);
  90. }
  91. joystick->serial = serial;
  92. return SDL_TRUE;
  93. }
  94. static int
  95. HIDAPI_DriverCombined_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  96. {
  97. int i;
  98. int result = -1;
  99. for (i = 0; i < device->num_children; ++i) {
  100. SDL_HIDAPI_Device *child = device->children[i];
  101. if (child->driver->RumbleJoystick(child, joystick, low_frequency_rumble, high_frequency_rumble) == 0) {
  102. result = 0;
  103. }
  104. }
  105. return result;
  106. }
  107. static int
  108. HIDAPI_DriverCombined_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  109. {
  110. int i;
  111. int result = -1;
  112. for (i = 0; i < device->num_children; ++i) {
  113. SDL_HIDAPI_Device *child = device->children[i];
  114. if (child->driver->RumbleJoystickTriggers(child, joystick, left_rumble, right_rumble) == 0) {
  115. result = 0;
  116. }
  117. }
  118. return result;
  119. }
  120. static Uint32
  121. HIDAPI_DriverCombined_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  122. {
  123. int i;
  124. Uint32 caps = 0;
  125. for (i = 0; i < device->num_children; ++i) {
  126. SDL_HIDAPI_Device *child = device->children[i];
  127. caps |= child->driver->GetJoystickCapabilities(child, joystick);
  128. }
  129. return caps;
  130. }
  131. static int
  132. HIDAPI_DriverCombined_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  133. {
  134. int i;
  135. int result = -1;
  136. for (i = 0; i < device->num_children; ++i) {
  137. SDL_HIDAPI_Device *child = device->children[i];
  138. if (child->driver->SetJoystickLED(child, joystick, red, green, blue) == 0) {
  139. result = 0;
  140. }
  141. }
  142. return result;
  143. }
  144. static int
  145. HIDAPI_DriverCombined_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  146. {
  147. return SDL_Unsupported();
  148. }
  149. static int
  150. HIDAPI_DriverCombined_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
  151. {
  152. int i;
  153. int result = -1;
  154. for (i = 0; i < device->num_children; ++i) {
  155. SDL_HIDAPI_Device *child = device->children[i];
  156. if (child->driver->SetJoystickSensorsEnabled(child, joystick, enabled) == 0) {
  157. result = 0;
  158. }
  159. }
  160. return result;
  161. }
  162. static SDL_bool
  163. HIDAPI_DriverCombined_UpdateDevice(SDL_HIDAPI_Device *device)
  164. {
  165. int i;
  166. int result = SDL_TRUE;
  167. for (i = 0; i < device->num_children; ++i) {
  168. SDL_HIDAPI_Device *child = device->children[i];
  169. if (!child->driver->UpdateDevice(child)) {
  170. result = SDL_FALSE;
  171. }
  172. }
  173. return result;
  174. }
  175. static void
  176. HIDAPI_DriverCombined_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  177. {
  178. int i;
  179. for (i = 0; i < device->num_children; ++i) {
  180. SDL_HIDAPI_Device *child = device->children[i];
  181. child->driver->CloseJoystick(child, joystick);
  182. }
  183. }
  184. static void
  185. HIDAPI_DriverCombined_FreeDevice(SDL_HIDAPI_Device *device)
  186. {
  187. }
  188. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverCombined =
  189. {
  190. SDL_HINT_JOYSTICK_HIDAPI,
  191. SDL_TRUE,
  192. SDL_TRUE,
  193. HIDAPI_DriverCombined_IsSupportedDevice,
  194. HIDAPI_DriverCombined_GetDeviceName,
  195. HIDAPI_DriverCombined_InitDevice,
  196. HIDAPI_DriverCombined_GetDevicePlayerIndex,
  197. HIDAPI_DriverCombined_SetDevicePlayerIndex,
  198. HIDAPI_DriverCombined_UpdateDevice,
  199. HIDAPI_DriverCombined_OpenJoystick,
  200. HIDAPI_DriverCombined_RumbleJoystick,
  201. HIDAPI_DriverCombined_RumbleJoystickTriggers,
  202. HIDAPI_DriverCombined_GetJoystickCapabilities,
  203. HIDAPI_DriverCombined_SetJoystickLED,
  204. HIDAPI_DriverCombined_SendJoystickEffect,
  205. HIDAPI_DriverCombined_SetJoystickSensorsEnabled,
  206. HIDAPI_DriverCombined_CloseJoystick,
  207. HIDAPI_DriverCombined_FreeDevice,
  208. };
  209. #endif /* SDL_JOYSTICK_HIDAPI */
  210. /* vi: set ts=4 sw=4 expandtab: */