SDL_hidapi_gamecube.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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_c.h"
  21. #include "../../misc/SDL_libusb.h"
  22. #include "../SDL_sysjoystick.h"
  23. #include "SDL_hidapijoystick_c.h"
  24. #include "SDL_hidapi_rumble.h"
  25. #include "../../hidapi/SDL_hidapi_c.h"
  26. #ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
  27. // Define this if you want to log all packets from the controller
  28. #if 0
  29. #define DEBUG_GAMECUBE_PROTOCOL
  30. #endif
  31. #define MAX_CONTROLLERS 4
  32. typedef struct
  33. {
  34. bool pc_mode;
  35. SDL_JoystickID joysticks[MAX_CONTROLLERS];
  36. Uint8 wireless[MAX_CONTROLLERS];
  37. Uint8 min_axis[MAX_CONTROLLERS * SDL_GAMEPAD_AXIS_COUNT];
  38. Uint8 max_axis[MAX_CONTROLLERS * SDL_GAMEPAD_AXIS_COUNT];
  39. Uint8 rumbleAllowed[MAX_CONTROLLERS];
  40. Uint8 rumble[1 + MAX_CONTROLLERS];
  41. // Without this variable, hid_write starts to lag a TON
  42. bool rumbleUpdate;
  43. bool useRumbleBrake;
  44. } SDL_DriverGameCube_Context;
  45. static void HIDAPI_DriverGameCube_RegisterHints(SDL_HintCallback callback, void *userdata)
  46. {
  47. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, callback, userdata);
  48. }
  49. static void HIDAPI_DriverGameCube_UnregisterHints(SDL_HintCallback callback, void *userdata)
  50. {
  51. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE, callback, userdata);
  52. }
  53. static bool HIDAPI_DriverGameCube_IsEnabled(void)
  54. {
  55. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,
  56. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI,
  57. SDL_HIDAPI_DEFAULT));
  58. }
  59. static bool HIDAPI_DriverGameCube_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  60. {
  61. if (vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) {
  62. // Nintendo Co., Ltd. Wii U GameCube Controller Adapter
  63. return true;
  64. }
  65. if (vendor_id == USB_VENDOR_DRAGONRISE &&
  66. (product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER1 ||
  67. product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER2 ||
  68. product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER3)) {
  69. // EVORETRO GameCube Controller Adapter
  70. return true;
  71. }
  72. return false;
  73. }
  74. static void ResetAxisRange(SDL_DriverGameCube_Context *ctx, int joystick_index)
  75. {
  76. SDL_memset(&ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT], 128 - 88, SDL_GAMEPAD_AXIS_COUNT);
  77. SDL_memset(&ctx->max_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT], 128 + 88, SDL_GAMEPAD_AXIS_COUNT);
  78. // Trigger axes may have a higher resting value
  79. ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT + SDL_GAMEPAD_AXIS_LEFT_TRIGGER] = 40;
  80. ctx->min_axis[joystick_index * SDL_GAMEPAD_AXIS_COUNT + SDL_GAMEPAD_AXIS_RIGHT_TRIGGER] = 40;
  81. }
  82. static void SDLCALL SDL_JoystickGameCubeRumbleBrakeHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  83. {
  84. if (hint) {
  85. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)userdata;
  86. ctx->useRumbleBrake = SDL_GetStringBoolean(hint, false);
  87. }
  88. }
  89. static bool HIDAPI_DriverGameCube_EnableAdapter(SDL_HIDAPI_Device *device)
  90. {
  91. #ifdef HAVE_LIBUSB
  92. // Need to close the device while sending USB commands to it
  93. SDL_hid_close(device->dev);
  94. // This is needed to enable input for Nyko and EVORETRO GameCube adapters
  95. SDL_LibUSBContext *libusb_ctx;
  96. if (SDL_InitLibUSB(&libusb_ctx)) {
  97. libusb_context *context = NULL;
  98. libusb_device **devs = NULL;
  99. libusb_device_handle *handle = NULL;
  100. struct libusb_device_descriptor desc;
  101. ssize_t i, num_devs;
  102. bool kernel_detached = false;
  103. if (libusb_ctx->init(&context) == 0) {
  104. num_devs = libusb_ctx->get_device_list(context, &devs);
  105. for (i = 0; i < num_devs; ++i) {
  106. if (libusb_ctx->get_device_descriptor(devs[i], &desc) != 0) {
  107. continue;
  108. }
  109. if (desc.idVendor != USB_VENDOR_NINTENDO ||
  110. desc.idProduct != USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) {
  111. continue;
  112. }
  113. if (libusb_ctx->open(devs[i], &handle) != 0) {
  114. continue;
  115. }
  116. if (libusb_ctx->kernel_driver_active(handle, 0)) {
  117. if (libusb_ctx->detach_kernel_driver(handle, 0) == 0) {
  118. kernel_detached = true;
  119. }
  120. }
  121. if (libusb_ctx->claim_interface(handle, 0) == 0) {
  122. libusb_ctx->control_transfer(handle, 0x21, 11, 0x0001, 0, NULL, 0, 1000);
  123. libusb_ctx->release_interface(handle, 0);
  124. }
  125. if (kernel_detached) {
  126. libusb_ctx->attach_kernel_driver(handle, 0);
  127. }
  128. libusb_ctx->close(handle);
  129. }
  130. libusb_ctx->free_device_list(devs, 1);
  131. libusb_ctx->exit(context);
  132. }
  133. SDL_QuitLibUSB();
  134. }
  135. // Reopen the device now that we're done
  136. device->dev = SDL_hid_open_path(device->path);
  137. if (!device->dev) {
  138. return false;
  139. }
  140. #endif // HAVE_LIBUSB
  141. Uint8 initMagic = 0x13;
  142. if (SDL_hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {
  143. SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
  144. "HIDAPI_DriverGameCube_InitDevice(): Couldn't initialize WUP-028");
  145. return false;
  146. }
  147. // Wait for the adapter to initialize
  148. SDL_Delay(10);
  149. return true;
  150. }
  151. static bool HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
  152. {
  153. SDL_DriverGameCube_Context *ctx;
  154. Uint8 packet[37];
  155. Uint8 *curSlot;
  156. Uint8 i;
  157. int size;
  158. Uint8 rumbleMagic = 0x11;
  159. ctx = (SDL_DriverGameCube_Context *)SDL_calloc(1, sizeof(*ctx));
  160. if (!ctx) {
  161. return false;
  162. }
  163. device->context = ctx;
  164. ctx->rumble[0] = rumbleMagic;
  165. if (device->vendor_id != USB_VENDOR_NINTENDO) {
  166. ctx->pc_mode = true;
  167. }
  168. if (ctx->pc_mode) {
  169. #ifdef SDL_PLATFORM_WIN32
  170. // We get a separate device for each slot
  171. ResetAxisRange(ctx, 0);
  172. HIDAPI_JoystickConnected(device, &ctx->joysticks[0]);
  173. #else
  174. for (i = 0; i < MAX_CONTROLLERS; ++i) {
  175. ResetAxisRange(ctx, i);
  176. HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
  177. }
  178. #endif
  179. } else {
  180. if (!HIDAPI_DriverGameCube_EnableAdapter(device)) {
  181. return false;
  182. }
  183. // Add all the applicable joysticks
  184. while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
  185. #ifdef DEBUG_GAMECUBE_PROTOCOL
  186. HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
  187. #endif
  188. if (size < 37 || packet[0] != 0x21) {
  189. continue; // Nothing to do yet...?
  190. }
  191. // Go through all 4 slots
  192. curSlot = packet + 1;
  193. for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
  194. ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
  195. // Only allow rumble if the adapter's second USB cable is connected
  196. ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) && !ctx->wireless[i];
  197. if (curSlot[0] & 0x30) { // 0x10 - Wired, 0x20 - Wireless
  198. if (ctx->joysticks[i] == 0) {
  199. ResetAxisRange(ctx, i);
  200. HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
  201. }
  202. } else {
  203. if (ctx->joysticks[i] != 0) {
  204. HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
  205. ctx->joysticks[i] = 0;
  206. }
  207. continue;
  208. }
  209. }
  210. }
  211. }
  212. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE_RUMBLE_BRAKE,
  213. SDL_JoystickGameCubeRumbleBrakeHintChanged, ctx);
  214. HIDAPI_SetDeviceName(device, "Nintendo GameCube Controller");
  215. return true;
  216. }
  217. static int HIDAPI_DriverGameCube_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  218. {
  219. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  220. Uint8 i;
  221. for (i = 0; i < 4; ++i) {
  222. if (instance_id == ctx->joysticks[i]) {
  223. return i;
  224. }
  225. }
  226. return -1;
  227. }
  228. static void HIDAPI_DriverGameCube_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  229. {
  230. }
  231. static void HIDAPI_DriverGameCube_HandleJoystickPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 slot, const Uint8 *packet, bool invert_c_stick)
  232. {
  233. SDL_Joystick *joystick;
  234. Uint8 i;
  235. Uint8 v;
  236. Sint16 axis_value;
  237. Uint64 timestamp = SDL_GetTicksNS();
  238. #ifdef SDL_PLATFORM_WIN32
  239. // We get a separate device for each slot
  240. i = 0;
  241. #else
  242. i = slot;
  243. if (i >= MAX_CONTROLLERS) {
  244. // Invalid packet?
  245. return;
  246. }
  247. #endif
  248. joystick = SDL_GetJoystickFromID(ctx->joysticks[i]);
  249. if (!joystick) {
  250. // Hasn't been opened yet, skip
  251. return;
  252. }
  253. #define READ_BUTTON(off, flag, button) \
  254. SDL_SendJoystickButton( \
  255. timestamp, \
  256. joystick, \
  257. button, \
  258. ((packet[off] & flag) != 0));
  259. READ_BUTTON(0, 0x02, 0) // A
  260. READ_BUTTON(0, 0x04, 1) // B
  261. READ_BUTTON(0, 0x08, 3) // Y
  262. READ_BUTTON(0, 0x01, 2) // X
  263. READ_BUTTON(1, 0x80, 4) // DPAD_LEFT
  264. READ_BUTTON(1, 0x20, 5) // DPAD_RIGHT
  265. READ_BUTTON(1, 0x40, 6) // DPAD_DOWN
  266. READ_BUTTON(1, 0x10, 7) // DPAD_UP
  267. READ_BUTTON(1, 0x02, 8) // START
  268. READ_BUTTON(0, 0x80, 9) // RIGHTSHOULDER
  269. /* These two buttons are for the bottoms of the analog triggers.
  270. * More than likely, you're going to want to read the axes instead!
  271. * -flibit
  272. */
  273. READ_BUTTON(0, 0x20, 10) // TRIGGERRIGHT
  274. READ_BUTTON(0, 0x10, 11) // TRIGGERLEFT
  275. #undef READ_BUTTON
  276. #define READ_AXIS(off, axis, invert) \
  277. v = (invert) ? (0xff - packet[off]) : packet[off]; \
  278. if (v < ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \
  279. ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = v; \
  280. if (v > ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \
  281. ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = v; \
  282. axis_value = (Sint16)HIDAPI_RemapVal(v, ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \
  283. SDL_SendJoystickAxis( \
  284. timestamp, \
  285. joystick, \
  286. axis, axis_value);
  287. READ_AXIS(2, SDL_GAMEPAD_AXIS_LEFTX, 0)
  288. READ_AXIS(3, SDL_GAMEPAD_AXIS_LEFTY, 1)
  289. READ_AXIS(5, SDL_GAMEPAD_AXIS_RIGHTX, invert_c_stick ? 1 : 0)
  290. READ_AXIS(4, SDL_GAMEPAD_AXIS_RIGHTY, invert_c_stick ? 0 : 1)
  291. READ_AXIS(6, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0)
  292. READ_AXIS(7, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0)
  293. #undef READ_AXIS
  294. }
  295. static void HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)
  296. {
  297. SDL_Joystick *joystick;
  298. Uint8 *curSlot;
  299. Uint8 i;
  300. Sint16 axis_value;
  301. Uint64 timestamp = SDL_GetTicksNS();
  302. if (size < 37 || packet[0] != 0x21) {
  303. return; // Nothing to do right now...?
  304. }
  305. // Go through all 4 slots
  306. curSlot = packet + 1;
  307. for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
  308. ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
  309. // Only allow rumble if the adapter's second USB cable is connected
  310. ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) && !ctx->wireless[i];
  311. if (curSlot[0] & 0x30) { // 0x10 - Wired, 0x20 - Wireless
  312. if (ctx->joysticks[i] == 0) {
  313. ResetAxisRange(ctx, i);
  314. HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
  315. }
  316. joystick = SDL_GetJoystickFromID(ctx->joysticks[i]);
  317. // Hasn't been opened yet, skip
  318. if (!joystick) {
  319. continue;
  320. }
  321. } else {
  322. if (ctx->joysticks[i] != 0) {
  323. HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
  324. ctx->joysticks[i] = 0;
  325. }
  326. continue;
  327. }
  328. #define READ_BUTTON(off, flag, button) \
  329. SDL_SendJoystickButton( \
  330. timestamp, \
  331. joystick, \
  332. button, \
  333. ((curSlot[off] & flag) != 0));
  334. READ_BUTTON(1, 0x01, 0) // A
  335. READ_BUTTON(1, 0x02, 1) // B
  336. READ_BUTTON(1, 0x04, 2) // X
  337. READ_BUTTON(1, 0x08, 3) // Y
  338. READ_BUTTON(1, 0x10, 4) // DPAD_LEFT
  339. READ_BUTTON(1, 0x20, 5) // DPAD_RIGHT
  340. READ_BUTTON(1, 0x40, 6) // DPAD_DOWN
  341. READ_BUTTON(1, 0x80, 7) // DPAD_UP
  342. READ_BUTTON(2, 0x01, 8) // START
  343. READ_BUTTON(2, 0x02, 9) // RIGHTSHOULDER
  344. /* These two buttons are for the bottoms of the analog triggers.
  345. * More than likely, you're going to want to read the axes instead!
  346. * -flibit
  347. */
  348. READ_BUTTON(2, 0x04, 10) // TRIGGERRIGHT
  349. READ_BUTTON(2, 0x08, 11) // TRIGGERLEFT
  350. #undef READ_BUTTON
  351. #define READ_AXIS(off, axis) \
  352. if (curSlot[off] < ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \
  353. ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = curSlot[off]; \
  354. if (curSlot[off] > ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis]) \
  355. ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis] = curSlot[off]; \
  356. axis_value = (Sint16)HIDAPI_RemapVal(curSlot[off], ctx->min_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], ctx->max_axis[i * SDL_GAMEPAD_AXIS_COUNT + axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \
  357. SDL_SendJoystickAxis( \
  358. timestamp, \
  359. joystick, \
  360. axis, axis_value);
  361. READ_AXIS(3, SDL_GAMEPAD_AXIS_LEFTX)
  362. READ_AXIS(4, SDL_GAMEPAD_AXIS_LEFTY)
  363. READ_AXIS(5, SDL_GAMEPAD_AXIS_RIGHTX)
  364. READ_AXIS(6, SDL_GAMEPAD_AXIS_RIGHTY)
  365. READ_AXIS(7, SDL_GAMEPAD_AXIS_LEFT_TRIGGER)
  366. READ_AXIS(8, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER)
  367. #undef READ_AXIS
  368. }
  369. }
  370. static bool HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)
  371. {
  372. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  373. Uint8 packet[USB_PACKET_LENGTH];
  374. int size;
  375. // Read input packet
  376. while ((size = SDL_hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
  377. #ifdef DEBUG_GAMECUBE_PROTOCOL
  378. HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
  379. #endif
  380. if (ctx->pc_mode) {
  381. if (size == 10) {
  382. // This is the older firmware
  383. // The first byte is the index of the connected controller
  384. // The C stick has an inverted value range compared to the left stick
  385. HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, packet[0] - 1, &packet[1], true);
  386. } else if (size == 9) {
  387. // This is the newer firmware (version 0x7)
  388. // The C stick has the same value range compared to the left stick
  389. HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, 0, packet, false);
  390. } else {
  391. // How do we handle this packet?
  392. }
  393. } else {
  394. HIDAPI_DriverGameCube_HandleNintendoPacket(device, ctx, packet, size);
  395. }
  396. }
  397. // Write rumble packet
  398. if (ctx->rumbleUpdate) {
  399. SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));
  400. ctx->rumbleUpdate = false;
  401. }
  402. // If we got here, nothing bad happened!
  403. return true;
  404. }
  405. static bool HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  406. {
  407. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  408. Uint8 i;
  409. SDL_AssertJoysticksLocked();
  410. for (i = 0; i < MAX_CONTROLLERS; i += 1) {
  411. if (joystick->instance_id == ctx->joysticks[i]) {
  412. joystick->nbuttons = 12;
  413. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  414. if (ctx->wireless[i]) {
  415. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
  416. } else {
  417. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
  418. }
  419. return true;
  420. }
  421. }
  422. return false; // Should never get here!
  423. }
  424. static bool HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  425. {
  426. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  427. Uint8 i, val;
  428. SDL_AssertJoysticksLocked();
  429. if (ctx->pc_mode) {
  430. return SDL_Unsupported();
  431. }
  432. for (i = 0; i < MAX_CONTROLLERS; i += 1) {
  433. if (joystick->instance_id == ctx->joysticks[i]) {
  434. if (ctx->wireless[i]) {
  435. return SDL_SetError("Nintendo GameCube WaveBird controllers do not support rumble");
  436. }
  437. if (!ctx->rumbleAllowed[i]) {
  438. return SDL_SetError("Second USB cable for WUP-028 not connected");
  439. }
  440. if (ctx->useRumbleBrake) {
  441. if (low_frequency_rumble == 0 && high_frequency_rumble > 0) {
  442. val = 0; // if only low is 0 we want to do a regular stop
  443. } else if (low_frequency_rumble == 0 && high_frequency_rumble == 0) {
  444. val = 2; // if both frequencies are 0 we want to do a hard stop
  445. } else {
  446. val = 1; // normal rumble
  447. }
  448. } else {
  449. val = (low_frequency_rumble > 0 || high_frequency_rumble > 0);
  450. }
  451. if (val != ctx->rumble[i + 1]) {
  452. ctx->rumble[i + 1] = val;
  453. ctx->rumbleUpdate = true;
  454. }
  455. return true;
  456. }
  457. }
  458. // Should never get here!
  459. return SDL_SetError("Couldn't find joystick");
  460. }
  461. static bool HIDAPI_DriverGameCube_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  462. {
  463. return SDL_Unsupported();
  464. }
  465. static Uint32 HIDAPI_DriverGameCube_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  466. {
  467. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  468. Uint32 result = 0;
  469. SDL_AssertJoysticksLocked();
  470. if (!ctx->pc_mode) {
  471. Uint8 i;
  472. for (i = 0; i < MAX_CONTROLLERS; i += 1) {
  473. if (joystick->instance_id == ctx->joysticks[i]) {
  474. if (!ctx->wireless[i] && ctx->rumbleAllowed[i]) {
  475. result |= SDL_JOYSTICK_CAP_RUMBLE;
  476. break;
  477. }
  478. }
  479. }
  480. }
  481. return result;
  482. }
  483. static bool HIDAPI_DriverGameCube_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  484. {
  485. return SDL_Unsupported();
  486. }
  487. static bool HIDAPI_DriverGameCube_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  488. {
  489. return SDL_Unsupported();
  490. }
  491. static bool HIDAPI_DriverGameCube_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  492. {
  493. return SDL_Unsupported();
  494. }
  495. static void HIDAPI_DriverGameCube_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  496. {
  497. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  498. // Stop rumble activity
  499. if (ctx->rumbleUpdate) {
  500. SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));
  501. ctx->rumbleUpdate = false;
  502. }
  503. }
  504. static void HIDAPI_DriverGameCube_FreeDevice(SDL_HIDAPI_Device *device)
  505. {
  506. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  507. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE_RUMBLE_BRAKE,
  508. SDL_JoystickGameCubeRumbleBrakeHintChanged, ctx);
  509. }
  510. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube = {
  511. SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,
  512. true,
  513. HIDAPI_DriverGameCube_RegisterHints,
  514. HIDAPI_DriverGameCube_UnregisterHints,
  515. HIDAPI_DriverGameCube_IsEnabled,
  516. HIDAPI_DriverGameCube_IsSupportedDevice,
  517. HIDAPI_DriverGameCube_InitDevice,
  518. HIDAPI_DriverGameCube_GetDevicePlayerIndex,
  519. HIDAPI_DriverGameCube_SetDevicePlayerIndex,
  520. HIDAPI_DriverGameCube_UpdateDevice,
  521. HIDAPI_DriverGameCube_OpenJoystick,
  522. HIDAPI_DriverGameCube_RumbleJoystick,
  523. HIDAPI_DriverGameCube_RumbleJoystickTriggers,
  524. HIDAPI_DriverGameCube_GetJoystickCapabilities,
  525. HIDAPI_DriverGameCube_SetJoystickLED,
  526. HIDAPI_DriverGameCube_SendJoystickEffect,
  527. HIDAPI_DriverGameCube_SetJoystickSensorsEnabled,
  528. HIDAPI_DriverGameCube_CloseJoystick,
  529. HIDAPI_DriverGameCube_FreeDevice,
  530. };
  531. #endif // SDL_JOYSTICK_HIDAPI_GAMECUBE
  532. #endif // SDL_JOYSTICK_HIDAPI