SDL_hidapi_gamecube.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 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. #include "../../SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "SDL_hints.h"
  21. #include "SDL_log.h"
  22. #include "SDL_events.h"
  23. #include "SDL_timer.h"
  24. #include "SDL_haptic.h"
  25. #include "SDL_joystick.h"
  26. #include "SDL_gamecontroller.h"
  27. #include "../SDL_sysjoystick.h"
  28. #include "SDL_hidapijoystick_c.h"
  29. #ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
  30. typedef struct {
  31. SDL_JoystickID joysticks[4];
  32. Uint8 wireless[4];
  33. Uint8 rumbleAllowed[4];
  34. Uint8 rumble[5];
  35. Uint32 rumbleExpiration[4];
  36. /* Without this variable, hid_write starts to lag a TON */
  37. Uint8 rumbleUpdate;
  38. } SDL_DriverGameCube_Context;
  39. static SDL_bool
  40. HIDAPI_DriverGameCube_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number)
  41. {
  42. return SDL_IsJoystickGameCube(vendor_id, product_id);
  43. }
  44. static const char *
  45. HIDAPI_DriverGameCube_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  46. {
  47. /* Give a user friendly name for this controller */
  48. if (SDL_IsJoystickGameCube(vendor_id, product_id)) {
  49. return "Nintendo GameCube Controller";
  50. }
  51. return NULL;
  52. }
  53. static SDL_bool
  54. HIDAPI_DriverGameCube_InitDriver(SDL_HIDAPI_DriverData *context, Uint16 vendor_id, Uint16 product_id, int *num_joysticks)
  55. {
  56. SDL_DriverGameCube_Context *ctx;
  57. Uint8 packet[37];
  58. Uint8 *curSlot;
  59. Uint8 i;
  60. int size;
  61. Uint8 initMagic = 0x13;
  62. Uint8 rumbleMagic = 0x11;
  63. ctx = (SDL_DriverGameCube_Context *)SDL_calloc(1, sizeof(*ctx));
  64. if (!ctx) {
  65. SDL_OutOfMemory();
  66. return SDL_FALSE;
  67. }
  68. ctx->joysticks[0] = -1;
  69. ctx->joysticks[1] = -1;
  70. ctx->joysticks[2] = -1;
  71. ctx->joysticks[3] = -1;
  72. ctx->rumble[0] = rumbleMagic;
  73. context->context = ctx;
  74. /* This is all that's needed to initialize the device. Really! */
  75. if (hid_write(context->device, &initMagic, sizeof(initMagic)) <= 0) {
  76. SDL_SetError("Couldn't initialize WUP-028");
  77. SDL_free(ctx);
  78. return SDL_FALSE;
  79. }
  80. /* Add all the applicable joysticks */
  81. while ((size = hid_read_timeout(context->device, packet, sizeof(packet), 0)) > 0) {
  82. if (size < 37 || packet[0] != 0x21) {
  83. continue; /* Nothing to do yet...? */
  84. }
  85. /* Go through all 4 slots */
  86. curSlot = packet + 1;
  87. for (i = 0; i < 4; i += 1, curSlot += 9) {
  88. ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
  89. /* Only allow rumble if the adapter's second USB cable is connected */
  90. ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
  91. if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
  92. if (ctx->joysticks[i] == -1) {
  93. ctx->joysticks[i] = SDL_GetNextJoystickInstanceID();
  94. *num_joysticks += 1;
  95. SDL_PrivateJoystickAdded(ctx->joysticks[i]);
  96. }
  97. } else {
  98. if (ctx->joysticks[i] != -1) {
  99. SDL_PrivateJoystickRemoved(ctx->joysticks[i]);
  100. *num_joysticks -= 1;
  101. ctx->joysticks[i] = -1;
  102. }
  103. continue;
  104. }
  105. }
  106. }
  107. return SDL_TRUE;
  108. }
  109. static void
  110. HIDAPI_DriverGameCube_QuitDriver(SDL_HIDAPI_DriverData *context, SDL_bool send_event, int *num_joysticks)
  111. {
  112. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
  113. Uint8 i;
  114. /* Stop all rumble activity */
  115. for (i = 1; i < 5; i += 1) {
  116. ctx->rumble[i] = 0;
  117. }
  118. hid_write(context->device, ctx->rumble, sizeof(ctx->rumble));
  119. /* Remove all joysticks! */
  120. for (i = 0; i < 4; i += 1) {
  121. if (ctx->joysticks[i] != -1) {
  122. *num_joysticks -= 1;
  123. if (send_event) {
  124. SDL_PrivateJoystickRemoved(ctx->joysticks[i]);
  125. }
  126. }
  127. }
  128. SDL_free(context->context);
  129. }
  130. static SDL_bool
  131. HIDAPI_DriverGameCube_UpdateDriver(SDL_HIDAPI_DriverData *context, int *num_joysticks)
  132. {
  133. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
  134. SDL_Joystick *joystick;
  135. Uint8 packet[37];
  136. Uint8 *curSlot;
  137. Uint32 now;
  138. Uint8 i;
  139. int size;
  140. /* Read input packet */
  141. while ((size = hid_read_timeout(context->device, packet, sizeof(packet), 0)) > 0) {
  142. if (size < 37 || packet[0] != 0x21) {
  143. continue; /* Nothing to do right now...? */
  144. }
  145. /* Go through all 4 slots */
  146. curSlot = packet + 1;
  147. for (i = 0; i < 4; i += 1, curSlot += 9) {
  148. ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
  149. /* Only allow rumble if the adapter's second USB cable is connected */
  150. ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
  151. if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
  152. if (ctx->joysticks[i] == -1) {
  153. ctx->joysticks[i] = SDL_GetNextJoystickInstanceID();
  154. *num_joysticks += 1;
  155. SDL_PrivateJoystickAdded(ctx->joysticks[i]);
  156. }
  157. joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
  158. /* Hasn't been opened yet, skip */
  159. if (joystick == NULL) {
  160. continue;
  161. }
  162. } else {
  163. if (ctx->joysticks[i] != -1) {
  164. SDL_PrivateJoystickRemoved(ctx->joysticks[i]);
  165. *num_joysticks -= 1;
  166. ctx->joysticks[i] = -1;
  167. }
  168. continue;
  169. }
  170. #define READ_BUTTON(off, flag, button) \
  171. SDL_PrivateJoystickButton( \
  172. joystick, \
  173. button, \
  174. (curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
  175. );
  176. READ_BUTTON(1, 0x01, 0) /* A */
  177. READ_BUTTON(1, 0x02, 1) /* B */
  178. READ_BUTTON(1, 0x04, 2) /* X */
  179. READ_BUTTON(1, 0x08, 3) /* Y */
  180. READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */
  181. READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */
  182. READ_BUTTON(1, 0x40, 6) /* DPAD_DOWN */
  183. READ_BUTTON(1, 0x80, 7) /* DPAD_UP */
  184. READ_BUTTON(2, 0x01, 8) /* START */
  185. READ_BUTTON(2, 0x02, 9) /* RIGHTSHOULDER */
  186. /* These two buttons are for the bottoms of the analog triggers.
  187. * More than likely, you're going to want to read the axes instead!
  188. * -flibit
  189. */
  190. READ_BUTTON(2, 0x04, 10) /* TRIGGERRIGHT */
  191. READ_BUTTON(2, 0x08, 11) /* TRIGGERLEFT */
  192. #undef READ_BUTTON
  193. /* Axis math taken from SDL_xinputjoystick.c */
  194. #define READ_AXIS(off, axis) \
  195. SDL_PrivateJoystickAxis( \
  196. joystick, \
  197. axis, \
  198. (Sint16)(((int)curSlot[off] * 257) - 32768) \
  199. );
  200. READ_AXIS(3, 0) /* LEFTX */
  201. READ_AXIS(4, 1) /* LEFTY */
  202. READ_AXIS(5, 2) /* RIGHTX */
  203. READ_AXIS(6, 3) /* RIGHTY */
  204. READ_AXIS(7, 4) /* TRIGGERLEFT */
  205. READ_AXIS(8, 5) /* TRIGGERRIGHT */
  206. #undef READ_AXIS
  207. }
  208. }
  209. /* Write rumble packet */
  210. now = SDL_GetTicks();
  211. for (i = 0; i < 4; i += 1) {
  212. if (ctx->rumbleExpiration[i] > 0) {
  213. if (SDL_TICKS_PASSED(now, ctx->rumbleExpiration[i]) || !ctx->rumbleAllowed[i]) {
  214. ctx->rumble[i + 1] = 0;
  215. ctx->rumbleExpiration[i] = 0;
  216. ctx->rumbleUpdate = 1;
  217. }
  218. }
  219. }
  220. if (ctx->rumbleUpdate) {
  221. hid_write(context->device, ctx->rumble, sizeof(ctx->rumble));
  222. ctx->rumbleUpdate = 0;
  223. }
  224. /* If we got here, nothing bad happened! */
  225. return SDL_TRUE;
  226. }
  227. static int
  228. HIDAPI_DriverGameCube_NumJoysticks(SDL_HIDAPI_DriverData *context)
  229. {
  230. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
  231. int i, joysticks = 0;
  232. for (i = 0; i < 4; i += 1) {
  233. if (ctx->joysticks[i] != -1) {
  234. joysticks += 1;
  235. }
  236. }
  237. return joysticks;
  238. }
  239. static int
  240. HIDAPI_DriverGameCube_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index)
  241. {
  242. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
  243. Uint8 i;
  244. for (i = 0; i < 4; i += 1) {
  245. if (ctx->joysticks[i] != -1) {
  246. if (index == 0) {
  247. return i;
  248. }
  249. index -= 1;
  250. }
  251. }
  252. return -1; /* Should never get here! */
  253. }
  254. static SDL_JoystickID
  255. HIDAPI_DriverGameCube_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
  256. {
  257. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
  258. Uint8 i;
  259. for (i = 0; i < 4; i += 1) {
  260. if (ctx->joysticks[i] != -1) {
  261. if (index == 0) {
  262. return ctx->joysticks[i];
  263. }
  264. index -= 1;
  265. }
  266. }
  267. return -1; /* Should never get here! */
  268. }
  269. static SDL_bool
  270. HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick)
  271. {
  272. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
  273. SDL_JoystickID instance = SDL_JoystickInstanceID(joystick);
  274. Uint8 i;
  275. for (i = 0; i < 4; i += 1) {
  276. if (instance == ctx->joysticks[i]) {
  277. joystick->nbuttons = 12;
  278. joystick->naxes = 6;
  279. joystick->epowerlevel = ctx->wireless[i] ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
  280. joystick->player_index = i;
  281. return SDL_TRUE;
  282. }
  283. }
  284. return SDL_FALSE; /* Should never get here! */
  285. }
  286. static int
  287. HIDAPI_DriverGameCube_Rumble(SDL_HIDAPI_DriverData *context, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  288. {
  289. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
  290. SDL_JoystickID instance = SDL_JoystickInstanceID(joystick);
  291. Uint8 i, val;
  292. for (i = 0; i < 4; i += 1) {
  293. if (instance == ctx->joysticks[i]) {
  294. if (ctx->wireless[i]) {
  295. return SDL_SetError("Ninteno GameCube WaveBird controllers do not support rumble");
  296. }
  297. if (!ctx->rumbleAllowed[i]) {
  298. return SDL_SetError("Second USB cable for WUP-028 not connected");
  299. }
  300. val = (low_frequency_rumble > 0 || high_frequency_rumble > 0);
  301. if (val != ctx->rumble[i + 1]) {
  302. ctx->rumble[i + 1] = val;
  303. ctx->rumbleUpdate = 1;
  304. }
  305. if (val && duration_ms < SDL_HAPTIC_INFINITY) {
  306. ctx->rumbleExpiration[i] = SDL_GetTicks() + duration_ms;
  307. } else {
  308. ctx->rumbleExpiration[i] = 0;
  309. }
  310. return 0;
  311. }
  312. }
  313. return -1; /* Should never get here! */
  314. }
  315. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube =
  316. {
  317. SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,
  318. SDL_TRUE,
  319. HIDAPI_DriverGameCube_IsSupportedDevice,
  320. HIDAPI_DriverGameCube_GetDeviceName,
  321. HIDAPI_DriverGameCube_InitDriver,
  322. HIDAPI_DriverGameCube_QuitDriver,
  323. HIDAPI_DriverGameCube_UpdateDriver,
  324. HIDAPI_DriverGameCube_NumJoysticks,
  325. HIDAPI_DriverGameCube_PlayerIndexForIndex,
  326. HIDAPI_DriverGameCube_InstanceIDForIndex,
  327. HIDAPI_DriverGameCube_OpenJoystick,
  328. HIDAPI_DriverGameCube_Rumble
  329. };
  330. #endif /* SDL_JOYSTICK_HIDAPI_GAMECUBE */
  331. #endif /* SDL_JOYSTICK_HIDAPI */