1
0

SDL_hidapi_gamecube.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. #include "../../SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "SDL_hints.h"
  21. #include "SDL_events.h"
  22. #include "SDL_timer.h"
  23. #include "SDL_haptic.h"
  24. #include "SDL_joystick.h"
  25. #include "SDL_gamecontroller.h"
  26. #include "../../SDL_hints_c.h"
  27. #include "../SDL_sysjoystick.h"
  28. #include "SDL_hidapijoystick_c.h"
  29. #include "SDL_hidapi_rumble.h"
  30. #include "../../hidapi/SDL_hidapi.h"
  31. #ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
  32. /* Define this if you want to log all packets from the controller */
  33. /*#define DEBUG_GAMECUBE_PROTOCOL*/
  34. #define MAX_CONTROLLERS 4
  35. typedef struct {
  36. SDL_bool pc_mode;
  37. SDL_JoystickID joysticks[MAX_CONTROLLERS];
  38. Uint8 wireless[MAX_CONTROLLERS];
  39. Uint8 min_axis[MAX_CONTROLLERS*SDL_CONTROLLER_AXIS_MAX];
  40. Uint8 max_axis[MAX_CONTROLLERS*SDL_CONTROLLER_AXIS_MAX];
  41. Uint8 rumbleAllowed[MAX_CONTROLLERS];
  42. Uint8 rumble[1+MAX_CONTROLLERS];
  43. /* Without this variable, hid_write starts to lag a TON */
  44. SDL_bool rumbleUpdate;
  45. SDL_bool m_bUseButtonLabels;
  46. } SDL_DriverGameCube_Context;
  47. static SDL_bool
  48. HIDAPI_DriverGameCube_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)
  49. {
  50. if (vendor_id == USB_VENDOR_NINTENDO && product_id == USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER) {
  51. /* Nintendo Co., Ltd. Wii U GameCube Controller Adapter */
  52. return SDL_TRUE;
  53. }
  54. if (vendor_id == USB_VENDOR_SHENZHEN && product_id == USB_PRODUCT_EVORETRO_GAMECUBE_ADAPTER) {
  55. /* EVORETRO GameCube Controller Adapter */
  56. return SDL_TRUE;
  57. }
  58. return SDL_FALSE;
  59. }
  60. static const char *
  61. HIDAPI_DriverGameCube_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  62. {
  63. return "Nintendo GameCube Controller";
  64. }
  65. static void
  66. ResetAxisRange(SDL_DriverGameCube_Context *ctx, int joystick_index)
  67. {
  68. SDL_memset(&ctx->min_axis[joystick_index*SDL_CONTROLLER_AXIS_MAX], 128-88, SDL_CONTROLLER_AXIS_MAX);
  69. SDL_memset(&ctx->max_axis[joystick_index*SDL_CONTROLLER_AXIS_MAX], 128+88, SDL_CONTROLLER_AXIS_MAX);
  70. /* Trigger axes may have a higher resting value */
  71. ctx->min_axis[joystick_index*SDL_CONTROLLER_AXIS_MAX+SDL_CONTROLLER_AXIS_TRIGGERLEFT] = 40;
  72. ctx->min_axis[joystick_index*SDL_CONTROLLER_AXIS_MAX+SDL_CONTROLLER_AXIS_TRIGGERRIGHT] = 40;
  73. }
  74. static float fsel(float fComparand, float fValGE, float fLT)
  75. {
  76. return fComparand >= 0 ? fValGE : fLT;
  77. }
  78. static float RemapVal(float val, float A, float B, float C, float D)
  79. {
  80. if (A == B) {
  81. return fsel(val - B, D, C);
  82. }
  83. if (val < A) {
  84. val = A;
  85. }
  86. if (val > B) {
  87. val = B;
  88. }
  89. return C + (D - C) * (val - A) / (B - A);
  90. }
  91. static void SDLCALL SDL_GameControllerButtonReportingHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  92. {
  93. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)userdata;
  94. ctx->m_bUseButtonLabels = SDL_GetStringBoolean(hint, SDL_TRUE);
  95. }
  96. static Uint8 RemapButton(SDL_DriverGameCube_Context *ctx, Uint8 button)
  97. {
  98. if (!ctx->m_bUseButtonLabels) {
  99. /* Use button positions */
  100. switch (button) {
  101. case SDL_CONTROLLER_BUTTON_B:
  102. return SDL_CONTROLLER_BUTTON_X;
  103. case SDL_CONTROLLER_BUTTON_X:
  104. return SDL_CONTROLLER_BUTTON_B;
  105. default:
  106. break;
  107. }
  108. }
  109. return button;
  110. }
  111. static SDL_bool
  112. HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
  113. {
  114. SDL_DriverGameCube_Context *ctx;
  115. Uint8 packet[37];
  116. Uint8 *curSlot;
  117. Uint8 i;
  118. int size;
  119. Uint8 initMagic = 0x13;
  120. Uint8 rumbleMagic = 0x11;
  121. #ifdef HAVE_ENABLE_GAMECUBE_ADAPTORS
  122. SDL_EnableGameCubeAdaptors();
  123. #endif
  124. ctx = (SDL_DriverGameCube_Context *)SDL_calloc(1, sizeof(*ctx));
  125. if (!ctx) {
  126. SDL_OutOfMemory();
  127. return SDL_FALSE;
  128. }
  129. device->dev = hid_open_path(device->path, 0);
  130. if (!device->dev) {
  131. SDL_free(ctx);
  132. SDL_SetError("Couldn't open %s", device->path);
  133. return SDL_FALSE;
  134. }
  135. device->context = ctx;
  136. ctx->joysticks[0] = -1;
  137. ctx->joysticks[1] = -1;
  138. ctx->joysticks[2] = -1;
  139. ctx->joysticks[3] = -1;
  140. ctx->rumble[0] = rumbleMagic;
  141. if (device->vendor_id != USB_VENDOR_NINTENDO) {
  142. ctx->pc_mode = SDL_TRUE;
  143. }
  144. if (ctx->pc_mode) {
  145. for (i = 0; i < MAX_CONTROLLERS; ++i) {
  146. ResetAxisRange(ctx, i);
  147. HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
  148. }
  149. } else {
  150. /* This is all that's needed to initialize the device. Really! */
  151. if (hid_write(device->dev, &initMagic, sizeof(initMagic)) != sizeof(initMagic)) {
  152. SDL_SetError("Couldn't initialize WUP-028");
  153. goto error;
  154. }
  155. /* Wait for the adapter to initialize */
  156. SDL_Delay(10);
  157. /* Add all the applicable joysticks */
  158. while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
  159. #ifdef DEBUG_GAMECUBE_PROTOCOL
  160. HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
  161. #endif
  162. if (size < 37 || packet[0] != 0x21) {
  163. continue; /* Nothing to do yet...? */
  164. }
  165. /* Go through all 4 slots */
  166. curSlot = packet + 1;
  167. for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
  168. ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
  169. /* Only allow rumble if the adapter's second USB cable is connected */
  170. ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
  171. if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
  172. if (ctx->joysticks[i] == -1) {
  173. ResetAxisRange(ctx, i);
  174. HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
  175. }
  176. } else {
  177. if (ctx->joysticks[i] != -1) {
  178. HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
  179. ctx->joysticks[i] = -1;
  180. }
  181. continue;
  182. }
  183. }
  184. }
  185. }
  186. SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
  187. SDL_GameControllerButtonReportingHintChanged, ctx);
  188. return SDL_TRUE;
  189. error:
  190. SDL_LockMutex(device->dev_lock);
  191. {
  192. if (device->dev) {
  193. hid_close(device->dev);
  194. device->dev = NULL;
  195. }
  196. if (device->context) {
  197. SDL_free(device->context);
  198. device->context = NULL;
  199. }
  200. }
  201. SDL_UnlockMutex(device->dev_lock);
  202. return SDL_FALSE;
  203. }
  204. static int
  205. HIDAPI_DriverGameCube_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  206. {
  207. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  208. Uint8 i;
  209. for (i = 0; i < 4; ++i) {
  210. if (instance_id == ctx->joysticks[i]) {
  211. return i;
  212. }
  213. }
  214. return -1;
  215. }
  216. static void
  217. HIDAPI_DriverGameCube_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  218. {
  219. }
  220. static void
  221. HIDAPI_DriverGameCube_HandleJoystickPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)
  222. {
  223. SDL_Joystick *joystick;
  224. Uint8 i, v;
  225. Sint16 axis_value;
  226. if (size != 10) {
  227. return; /* How do we handle this packet? */
  228. }
  229. i = packet[0] - 1;
  230. if (i >= MAX_CONTROLLERS) {
  231. return; /* How do we handle this packet? */
  232. }
  233. joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
  234. #define READ_BUTTON(off, flag, button) \
  235. SDL_PrivateJoystickButton( \
  236. joystick, \
  237. RemapButton(ctx, button), \
  238. (packet[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
  239. );
  240. READ_BUTTON(1, 0x02, 0) /* A */
  241. READ_BUTTON(1, 0x04, 1) /* B */
  242. READ_BUTTON(1, 0x01, 2) /* X */
  243. READ_BUTTON(1, 0x08, 3) /* Y */
  244. READ_BUTTON(2, 0x80, 4) /* DPAD_LEFT */
  245. READ_BUTTON(2, 0x20, 5) /* DPAD_RIGHT */
  246. READ_BUTTON(2, 0x40, 6) /* DPAD_DOWN */
  247. READ_BUTTON(2, 0x10, 7) /* DPAD_UP */
  248. READ_BUTTON(2, 0x02, 8) /* START */
  249. READ_BUTTON(1, 0x80, 9) /* RIGHTSHOULDER */
  250. /* These two buttons are for the bottoms of the analog triggers.
  251. * More than likely, you're going to want to read the axes instead!
  252. * -flibit
  253. */
  254. READ_BUTTON(1, 0x20, 10) /* TRIGGERRIGHT */
  255. READ_BUTTON(1, 0x10, 11) /* TRIGGERLEFT */
  256. #undef READ_BUTTON
  257. #define READ_AXIS(off, axis, invert) \
  258. v = invert ? (0xff - packet[off]) : packet[off]; \
  259. if (v < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = v; \
  260. if (v > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = v; \
  261. axis_value = (Sint16)RemapVal(v, ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16); \
  262. SDL_PrivateJoystickAxis( \
  263. joystick, \
  264. axis, axis_value \
  265. );
  266. READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX, 0)
  267. READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY, 0)
  268. READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTX, 1)
  269. READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTY, 1)
  270. READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT, 0)
  271. READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 0)
  272. #undef READ_AXIS
  273. }
  274. static void
  275. HIDAPI_DriverGameCube_HandleNintendoPacket(SDL_HIDAPI_Device *device, SDL_DriverGameCube_Context *ctx, Uint8 *packet, int size)
  276. {
  277. SDL_Joystick *joystick;
  278. Uint8 *curSlot;
  279. Uint8 i;
  280. Sint16 axis_value;
  281. if (size < 37 || packet[0] != 0x21) {
  282. return; /* Nothing to do right now...? */
  283. }
  284. /* Go through all 4 slots */
  285. curSlot = packet + 1;
  286. for (i = 0; i < MAX_CONTROLLERS; i += 1, curSlot += 9) {
  287. ctx->wireless[i] = (curSlot[0] & 0x20) != 0;
  288. /* Only allow rumble if the adapter's second USB cable is connected */
  289. ctx->rumbleAllowed[i] = (curSlot[0] & 0x04) != 0 && !ctx->wireless[i];
  290. if (curSlot[0] & 0x30) { /* 0x10 - Wired, 0x20 - Wireless */
  291. if (ctx->joysticks[i] == -1) {
  292. ResetAxisRange(ctx, i);
  293. HIDAPI_JoystickConnected(device, &ctx->joysticks[i]);
  294. }
  295. joystick = SDL_JoystickFromInstanceID(ctx->joysticks[i]);
  296. /* Hasn't been opened yet, skip */
  297. if (joystick == NULL) {
  298. continue;
  299. }
  300. } else {
  301. if (ctx->joysticks[i] != -1) {
  302. HIDAPI_JoystickDisconnected(device, ctx->joysticks[i]);
  303. ctx->joysticks[i] = -1;
  304. }
  305. continue;
  306. }
  307. #define READ_BUTTON(off, flag, button) \
  308. SDL_PrivateJoystickButton( \
  309. joystick, \
  310. RemapButton(ctx, button), \
  311. (curSlot[off] & flag) ? SDL_PRESSED : SDL_RELEASED \
  312. );
  313. READ_BUTTON(1, 0x01, 0) /* A */
  314. READ_BUTTON(1, 0x04, 1) /* B */
  315. READ_BUTTON(1, 0x02, 2) /* X */
  316. READ_BUTTON(1, 0x08, 3) /* Y */
  317. READ_BUTTON(1, 0x10, 4) /* DPAD_LEFT */
  318. READ_BUTTON(1, 0x20, 5) /* DPAD_RIGHT */
  319. READ_BUTTON(1, 0x40, 6) /* DPAD_DOWN */
  320. READ_BUTTON(1, 0x80, 7) /* DPAD_UP */
  321. READ_BUTTON(2, 0x01, 8) /* START */
  322. READ_BUTTON(2, 0x02, 9) /* RIGHTSHOULDER */
  323. /* These two buttons are for the bottoms of the analog triggers.
  324. * More than likely, you're going to want to read the axes instead!
  325. * -flibit
  326. */
  327. READ_BUTTON(2, 0x04, 10) /* TRIGGERRIGHT */
  328. READ_BUTTON(2, 0x08, 11) /* TRIGGERLEFT */
  329. #undef READ_BUTTON
  330. #define READ_AXIS(off, axis) \
  331. if (curSlot[off] < ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
  332. if (curSlot[off] > ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis]) ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis] = curSlot[off]; \
  333. axis_value = (Sint16)(RemapVal(curSlot[off], ctx->min_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], ctx->max_axis[i*SDL_CONTROLLER_AXIS_MAX+axis], SDL_MIN_SINT16, SDL_MAX_SINT16)); \
  334. SDL_PrivateJoystickAxis( \
  335. joystick, \
  336. axis, axis_value \
  337. );
  338. READ_AXIS(3, SDL_CONTROLLER_AXIS_LEFTX)
  339. READ_AXIS(4, SDL_CONTROLLER_AXIS_LEFTY)
  340. READ_AXIS(5, SDL_CONTROLLER_AXIS_RIGHTX)
  341. READ_AXIS(6, SDL_CONTROLLER_AXIS_RIGHTY)
  342. READ_AXIS(7, SDL_CONTROLLER_AXIS_TRIGGERLEFT)
  343. READ_AXIS(8, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)
  344. #undef READ_AXIS
  345. }
  346. }
  347. static SDL_bool
  348. HIDAPI_DriverGameCube_UpdateDevice(SDL_HIDAPI_Device *device)
  349. {
  350. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  351. Uint8 packet[USB_PACKET_LENGTH];
  352. int size;
  353. /* Read input packet */
  354. while ((size = hid_read_timeout(device->dev, packet, sizeof(packet), 0)) > 0) {
  355. #ifdef DEBUG_GAMECUBE_PROTOCOL
  356. //HIDAPI_DumpPacket("Nintendo GameCube packet: size = %d", packet, size);
  357. #endif
  358. if (ctx->pc_mode) {
  359. HIDAPI_DriverGameCube_HandleJoystickPacket(device, ctx, packet, size);
  360. } else {
  361. HIDAPI_DriverGameCube_HandleNintendoPacket(device, ctx, packet, size);
  362. }
  363. }
  364. /* Write rumble packet */
  365. if (ctx->rumbleUpdate) {
  366. SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));
  367. ctx->rumbleUpdate = SDL_FALSE;
  368. }
  369. /* If we got here, nothing bad happened! */
  370. return SDL_TRUE;
  371. }
  372. static SDL_bool
  373. HIDAPI_DriverGameCube_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  374. {
  375. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  376. Uint8 i;
  377. for (i = 0; i < MAX_CONTROLLERS; i += 1) {
  378. if (joystick->instance_id == ctx->joysticks[i]) {
  379. joystick->nbuttons = 12;
  380. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  381. joystick->epowerlevel = ctx->wireless[i] ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
  382. return SDL_TRUE;
  383. }
  384. }
  385. return SDL_FALSE; /* Should never get here! */
  386. }
  387. static int
  388. HIDAPI_DriverGameCube_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  389. {
  390. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  391. Uint8 i, val;
  392. if (ctx->pc_mode) {
  393. return SDL_Unsupported();
  394. }
  395. for (i = 0; i < MAX_CONTROLLERS; i += 1) {
  396. if (joystick->instance_id == ctx->joysticks[i]) {
  397. if (ctx->wireless[i]) {
  398. return SDL_SetError("Ninteno GameCube WaveBird controllers do not support rumble");
  399. }
  400. if (!ctx->rumbleAllowed[i]) {
  401. return SDL_SetError("Second USB cable for WUP-028 not connected");
  402. }
  403. val = (low_frequency_rumble > 0 || high_frequency_rumble > 0);
  404. if (val != ctx->rumble[i + 1]) {
  405. ctx->rumble[i + 1] = val;
  406. ctx->rumbleUpdate = SDL_TRUE;
  407. }
  408. return 0;
  409. }
  410. }
  411. /* Should never get here! */
  412. SDL_SetError("Couldn't find joystick");
  413. return -1;
  414. }
  415. static int
  416. HIDAPI_DriverGameCube_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  417. {
  418. return SDL_Unsupported();
  419. }
  420. static SDL_bool
  421. HIDAPI_DriverGameCube_HasJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  422. {
  423. return SDL_FALSE;
  424. }
  425. static int
  426. HIDAPI_DriverGameCube_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  427. {
  428. return SDL_Unsupported();
  429. }
  430. static int
  431. HIDAPI_DriverGameCube_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
  432. {
  433. return SDL_Unsupported();
  434. }
  435. static void
  436. HIDAPI_DriverGameCube_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  437. {
  438. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  439. /* Stop rumble activity */
  440. if (ctx->rumbleUpdate) {
  441. SDL_HIDAPI_SendRumble(device, ctx->rumble, sizeof(ctx->rumble));
  442. ctx->rumbleUpdate = SDL_FALSE;
  443. }
  444. }
  445. static void
  446. HIDAPI_DriverGameCube_FreeDevice(SDL_HIDAPI_Device *device)
  447. {
  448. SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
  449. SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
  450. SDL_GameControllerButtonReportingHintChanged, ctx);
  451. SDL_LockMutex(device->dev_lock);
  452. {
  453. hid_close(device->dev);
  454. device->dev = NULL;
  455. SDL_free(device->context);
  456. device->context = NULL;
  457. }
  458. SDL_UnlockMutex(device->dev_lock);
  459. }
  460. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube =
  461. {
  462. SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE,
  463. SDL_TRUE,
  464. HIDAPI_DriverGameCube_IsSupportedDevice,
  465. HIDAPI_DriverGameCube_GetDeviceName,
  466. HIDAPI_DriverGameCube_InitDevice,
  467. HIDAPI_DriverGameCube_GetDevicePlayerIndex,
  468. HIDAPI_DriverGameCube_SetDevicePlayerIndex,
  469. HIDAPI_DriverGameCube_UpdateDevice,
  470. HIDAPI_DriverGameCube_OpenJoystick,
  471. HIDAPI_DriverGameCube_RumbleJoystick,
  472. HIDAPI_DriverGameCube_RumbleJoystickTriggers,
  473. HIDAPI_DriverGameCube_HasJoystickLED,
  474. HIDAPI_DriverGameCube_SetJoystickLED,
  475. HIDAPI_DriverGameCube_SetJoystickSensorsEnabled,
  476. HIDAPI_DriverGameCube_CloseJoystick,
  477. HIDAPI_DriverGameCube_FreeDevice,
  478. };
  479. #endif /* SDL_JOYSTICK_HIDAPI_GAMECUBE */
  480. #endif /* SDL_JOYSTICK_HIDAPI */
  481. /* vi: set ts=4 sw=4 expandtab: */