SDL_hidapi_8bitdo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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_sysjoystick.h"
  21. #include "SDL_hidapijoystick_c.h"
  22. #include "SDL_hidapi_rumble.h"
  23. #ifdef SDL_JOYSTICK_HIDAPI_8BITDO
  24. // Define this if you want to log all packets from the controller
  25. #if 0
  26. #define DEBUG_8BITDO_PROTOCOL
  27. #endif
  28. enum
  29. {
  30. SDL_GAMEPAD_BUTTON_8BITDO_L4 = 11,
  31. SDL_GAMEPAD_BUTTON_8BITDO_R4,
  32. SDL_GAMEPAD_BUTTON_8BITDO_PL,
  33. SDL_GAMEPAD_BUTTON_8BITDO_PR,
  34. SDL_GAMEPAD_NUM_8BITDO_BUTTONS,
  35. };
  36. #define SDL_8BITDO_FEATURE_REPORTID_ENABLE_SDL_REPORTID 0x06
  37. #define SDL_8BITDO_REPORTID_SDL_REPORTID 0x04
  38. #define SDL_8BITDO_REPORTID_NOT_SUPPORTED_SDL_REPORTID 0x03
  39. #define SDL_8BITDO_BT_REPORTID_SDL_REPORTID 0x01
  40. #define ABITDO_ACCEL_SCALE 4096.f
  41. #define SENSOR_INTERVAL_NS 8000000ULL
  42. typedef struct
  43. {
  44. bool sensors_supported;
  45. bool sensors_enabled;
  46. bool touchpad_01_supported;
  47. bool touchpad_02_supported;
  48. bool rumble_supported;
  49. bool rumble_type;
  50. bool rgb_supported;
  51. bool player_led_supported;
  52. bool powerstate_supported;
  53. Uint8 serial[6];
  54. Uint16 version;
  55. Uint16 version_beta;
  56. float accelScale;
  57. float gyroScale;
  58. Uint8 last_state[USB_PACKET_LENGTH];
  59. Uint64 sensor_timestamp; // Microseconds. Simulate onboard clock. Advance by known rate: SENSOR_INTERVAL_NS == 8ms = 125 Hz
  60. } SDL_Driver8BitDo_Context;
  61. #pragma pack(push,1)
  62. typedef struct
  63. {
  64. bool sensors_supported;
  65. bool touchpad_01_supported;
  66. bool touchpad_02_supported;
  67. bool rumble_supported;
  68. bool rumble_type;
  69. bool rgb_supported;
  70. Uint8 device_type;
  71. Uint8 serial[6];
  72. Uint16 version;
  73. Uint16 version_beta;
  74. Uint16 pid;
  75. } ABITDO_DEVICE_INFO;
  76. typedef struct
  77. {
  78. // Accelerometer values
  79. short sAccelX;
  80. short sAccelY;
  81. short sAccelZ;
  82. // Gyroscope values
  83. short sGyroX;
  84. short sGyroY;
  85. short sGyroZ;
  86. } ABITDO_SENSORS;
  87. #pragma pack(pop)
  88. static void HIDAPI_Driver8BitDo_RegisterHints(SDL_HintCallback callback, void *userdata)
  89. {
  90. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_8BITDO, callback, userdata);
  91. }
  92. static void HIDAPI_Driver8BitDo_UnregisterHints(SDL_HintCallback callback, void *userdata)
  93. {
  94. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_8BITDO, callback, userdata);
  95. }
  96. static bool HIDAPI_Driver8BitDo_IsEnabled(void)
  97. {
  98. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_8BITDO, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  99. }
  100. static int ReadFeatureReport(SDL_hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
  101. {
  102. SDL_memset(report, 0, length);
  103. report[0] = report_id;
  104. return SDL_hid_get_feature_report(dev, report, length);
  105. }
  106. static bool HIDAPI_Driver8BitDo_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)
  107. {
  108. if (vendor_id == USB_VENDOR_8BITDO) {
  109. switch (product_id) {
  110. case USB_PRODUCT_8BITDO_SF30_PRO:
  111. case USB_PRODUCT_8BITDO_SF30_PRO_BT:
  112. case USB_PRODUCT_8BITDO_SN30_PRO:
  113. case USB_PRODUCT_8BITDO_SN30_PRO_BT:
  114. case USB_PRODUCT_8BITDO_PRO_2:
  115. case USB_PRODUCT_8BITDO_PRO_2_BT:
  116. case USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS:
  117. return true;
  118. default:
  119. break;
  120. }
  121. }
  122. return false;
  123. }
  124. static bool HIDAPI_Driver8BitDo_InitDevice(SDL_HIDAPI_Device *device)
  125. {
  126. SDL_Driver8BitDo_Context *ctx = (SDL_Driver8BitDo_Context *)SDL_calloc(1, sizeof(*ctx));
  127. if (!ctx) {
  128. return false;
  129. }
  130. device->context = ctx;
  131. if (device->product_id == USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS) {
  132. // The Ultimate 2 Wireless v1.02 firmware has 12 byte reports, v1.03 firmware has 34 byte reports
  133. const int ULTIMATE2_WIRELESS_V103_REPORT_SIZE = 34;
  134. const int MAX_ATTEMPTS = 3;
  135. for (int attempt = 0; attempt < MAX_ATTEMPTS; ++attempt) {
  136. Uint8 data[USB_PACKET_LENGTH];
  137. int size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 80);
  138. if (size == 0) {
  139. // Try again
  140. continue;
  141. }
  142. if (size >= ULTIMATE2_WIRELESS_V103_REPORT_SIZE) {
  143. ctx->sensors_supported = true;
  144. ctx->rumble_supported = true;
  145. ctx->powerstate_supported = true;
  146. }
  147. break;
  148. }
  149. } else {
  150. Uint8 data[USB_PACKET_LENGTH];
  151. int size = ReadFeatureReport(device->dev, SDL_8BITDO_FEATURE_REPORTID_ENABLE_SDL_REPORTID, data, sizeof(data));
  152. if (size > 0) {
  153. ctx->sensors_supported = true;
  154. ctx->rumble_supported = true;
  155. ctx->powerstate_supported = true;
  156. }
  157. }
  158. if (device->product_id == USB_PRODUCT_8BITDO_SF30_PRO || device->product_id == USB_PRODUCT_8BITDO_SF30_PRO_BT) {
  159. HIDAPI_SetDeviceName(device, "8BitDo SF30 Pro");
  160. } else if (device->product_id == USB_PRODUCT_8BITDO_SN30_PRO || device->product_id == USB_PRODUCT_8BITDO_SN30_PRO_BT) {
  161. HIDAPI_SetDeviceName(device, "8BitDo SN30 Pro");
  162. } else if (device->product_id == USB_PRODUCT_8BITDO_PRO_2 || device->product_id == USB_PRODUCT_8BITDO_PRO_2_BT) {
  163. HIDAPI_SetDeviceName(device, "8BitDo Pro 2");
  164. }
  165. return HIDAPI_JoystickConnected(device, NULL);
  166. }
  167. static int HIDAPI_Driver8BitDo_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  168. {
  169. return -1;
  170. }
  171. static void HIDAPI_Driver8BitDo_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  172. {
  173. }
  174. #ifndef DEG2RAD
  175. #define DEG2RAD(x) ((float)(x) * (float)(SDL_PI_F / 180.f))
  176. #endif
  177. static bool HIDAPI_Driver8BitDo_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  178. {
  179. SDL_Driver8BitDo_Context *ctx = (SDL_Driver8BitDo_Context *)device->context;
  180. SDL_AssertJoysticksLocked();
  181. SDL_zeroa(ctx->last_state);
  182. // Initialize the joystick capabilities
  183. if (device->product_id == USB_PRODUCT_8BITDO_PRO_2 ||
  184. device->product_id == USB_PRODUCT_8BITDO_PRO_2_BT ||
  185. device->product_id == USB_PRODUCT_8BITDO_ULTIMATE2_WIRELESS) {
  186. // This controller has additional buttons
  187. joystick->nbuttons = SDL_GAMEPAD_NUM_8BITDO_BUTTONS;
  188. } else {
  189. joystick->nbuttons = 11;
  190. }
  191. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  192. joystick->nhats = 1;
  193. if (ctx->sensors_supported) {
  194. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 125.0f);
  195. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 125.0f);
  196. ctx->accelScale = SDL_STANDARD_GRAVITY / ABITDO_ACCEL_SCALE;
  197. ctx->gyroScale = DEG2RAD(2048) / INT16_MAX; // Hardware senses +/- 2048 Degrees per second mapped to +/- INT16_MAX
  198. }
  199. return true;
  200. }
  201. static bool HIDAPI_Driver8BitDo_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  202. {
  203. SDL_Driver8BitDo_Context *ctx = (SDL_Driver8BitDo_Context *)device->context;
  204. if (ctx->rumble_supported) {
  205. Uint8 rumble_packet[5] = { 0x05, 0x00, 0x00, 0x00, 0x00 };
  206. rumble_packet[1] = low_frequency_rumble >> 8;
  207. rumble_packet[2] = high_frequency_rumble >> 8;
  208. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  209. return SDL_SetError("Couldn't send rumble packet");
  210. }
  211. return true;
  212. } else {
  213. return SDL_Unsupported();
  214. }
  215. }
  216. static bool HIDAPI_Driver8BitDo_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  217. {
  218. return SDL_Unsupported();
  219. }
  220. static Uint32 HIDAPI_Driver8BitDo_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  221. {
  222. SDL_Driver8BitDo_Context *ctx = (SDL_Driver8BitDo_Context *)device->context;
  223. Uint32 caps = 0;
  224. if (ctx->rumble_supported) {
  225. caps |= SDL_JOYSTICK_CAP_RUMBLE;
  226. }
  227. if (ctx->rgb_supported) {
  228. caps |= SDL_JOYSTICK_CAP_RGB_LED;
  229. }
  230. return caps;
  231. }
  232. static bool HIDAPI_Driver8BitDo_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  233. {
  234. return SDL_Unsupported();
  235. }
  236. static bool HIDAPI_Driver8BitDo_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  237. {
  238. return SDL_Unsupported();
  239. }
  240. static bool HIDAPI_Driver8BitDo_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  241. {
  242. SDL_Driver8BitDo_Context *ctx = (SDL_Driver8BitDo_Context *)device->context;
  243. if (ctx->sensors_supported) {
  244. ctx->sensors_enabled = enabled;
  245. return true;
  246. }
  247. return SDL_Unsupported();
  248. }
  249. static void HIDAPI_Driver8BitDo_HandleOldStatePacket(SDL_Joystick *joystick, SDL_Driver8BitDo_Context *ctx, Uint8 *data, int size)
  250. {
  251. Sint16 axis;
  252. Uint64 timestamp = SDL_GetTicksNS();
  253. if (ctx->last_state[2] != data[2]) {
  254. Uint8 hat;
  255. switch (data[2]) {
  256. case 0:
  257. hat = SDL_HAT_UP;
  258. break;
  259. case 1:
  260. hat = SDL_HAT_RIGHTUP;
  261. break;
  262. case 2:
  263. hat = SDL_HAT_RIGHT;
  264. break;
  265. case 3:
  266. hat = SDL_HAT_RIGHTDOWN;
  267. break;
  268. case 4:
  269. hat = SDL_HAT_DOWN;
  270. break;
  271. case 5:
  272. hat = SDL_HAT_LEFTDOWN;
  273. break;
  274. case 6:
  275. hat = SDL_HAT_LEFT;
  276. break;
  277. case 7:
  278. hat = SDL_HAT_LEFTUP;
  279. break;
  280. default:
  281. hat = SDL_HAT_CENTERED;
  282. break;
  283. }
  284. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  285. }
  286. if (ctx->last_state[0] != data[0]) {
  287. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[0] & 0x01) != 0));
  288. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[0] & 0x02) != 0));
  289. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[0] & 0x08) != 0));
  290. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[0] & 0x10) != 0));
  291. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[0] & 0x40) != 0));
  292. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[0] & 0x80) != 0));
  293. }
  294. if (ctx->last_state[1] != data[1]) {
  295. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[1] & 0x10) != 0));
  296. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[1] & 0x04) != 0));
  297. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[1] & 0x08) != 0));
  298. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[1] & 0x20) != 0));
  299. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[1] & 0x40) != 0));
  300. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, (data[1] & 0x01) ? SDL_MAX_SINT16 : SDL_MIN_SINT16);
  301. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, (data[1] & 0x02) ? SDL_MAX_SINT16 : SDL_MIN_SINT16);
  302. }
  303. #define READ_STICK_AXIS(offset) \
  304. (data[offset] == 0x7f ? 0 : (Sint16)HIDAPI_RemapVal((float)((int)data[offset] - 0x7f), -0x7f, 0xff - 0x7f, SDL_MIN_SINT16, SDL_MAX_SINT16))
  305. {
  306. axis = READ_STICK_AXIS(3);
  307. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  308. axis = READ_STICK_AXIS(4);
  309. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  310. axis = READ_STICK_AXIS(5);
  311. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  312. axis = READ_STICK_AXIS(6);
  313. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  314. }
  315. #undef READ_STICK_AXIS
  316. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  317. }
  318. static void HIDAPI_Driver8BitDo_HandleStatePacket(SDL_Joystick *joystick, SDL_Driver8BitDo_Context *ctx, Uint8 *data, int size)
  319. {
  320. Sint16 axis;
  321. Uint64 timestamp = SDL_GetTicksNS();
  322. switch (data[0]) {
  323. case SDL_8BITDO_REPORTID_NOT_SUPPORTED_SDL_REPORTID: // Firmware without enhanced mode
  324. case SDL_8BITDO_REPORTID_SDL_REPORTID: // Enhanced mode USB report
  325. case SDL_8BITDO_BT_REPORTID_SDL_REPORTID: // Enhanced mode Bluetooth report
  326. break;
  327. default:
  328. // We don't know how to handle this report
  329. return;
  330. }
  331. if (ctx->last_state[1] != data[1]) {
  332. Uint8 hat;
  333. switch (data[1]) {
  334. case 0:
  335. hat = SDL_HAT_UP;
  336. break;
  337. case 1:
  338. hat = SDL_HAT_RIGHTUP;
  339. break;
  340. case 2:
  341. hat = SDL_HAT_RIGHT;
  342. break;
  343. case 3:
  344. hat = SDL_HAT_RIGHTDOWN;
  345. break;
  346. case 4:
  347. hat = SDL_HAT_DOWN;
  348. break;
  349. case 5:
  350. hat = SDL_HAT_LEFTDOWN;
  351. break;
  352. case 6:
  353. hat = SDL_HAT_LEFT;
  354. break;
  355. case 7:
  356. hat = SDL_HAT_LEFTUP;
  357. break;
  358. default:
  359. hat = SDL_HAT_CENTERED;
  360. break;
  361. }
  362. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  363. }
  364. if (ctx->last_state[8] != data[8]) {
  365. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[8] & 0x01) != 0));
  366. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[8] & 0x02) != 0));
  367. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[8] & 0x08) != 0));
  368. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[8] & 0x10) != 0));
  369. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[8] & 0x40) != 0));
  370. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[8] & 0x80) != 0));
  371. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_8BITDO_PL, ((data[8] & 0x20) != 0));
  372. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_8BITDO_PR, ((data[8] & 0x04) != 0));
  373. }
  374. if (ctx->last_state[9] != data[9]) {
  375. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[9] & 0x10) != 0));
  376. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[9] & 0x04) != 0));
  377. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[9] & 0x08) != 0));
  378. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[9] & 0x20) != 0));
  379. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[9] & 0x40) != 0));
  380. }
  381. if (size > 10 && ctx->last_state[10] != data[10]) {
  382. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_8BITDO_L4, ((data[10] & 0x01) != 0));
  383. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_8BITDO_R4, ((data[10] & 0x02) != 0));
  384. }
  385. #define READ_STICK_AXIS(offset) \
  386. (data[offset] == 0x7f ? 0 : (Sint16)HIDAPI_RemapVal((float)((int)data[offset] - 0x7f), -0x7f, 0xff - 0x7f, SDL_MIN_SINT16, SDL_MAX_SINT16))
  387. {
  388. axis = READ_STICK_AXIS(2);
  389. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  390. axis = READ_STICK_AXIS(3);
  391. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  392. axis = READ_STICK_AXIS(4);
  393. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  394. axis = READ_STICK_AXIS(5);
  395. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  396. }
  397. #undef READ_STICK_AXIS
  398. #define READ_TRIGGER_AXIS(offset) \
  399. (Sint16)(((int)data[offset] * 257) - 32768)
  400. {
  401. axis = READ_TRIGGER_AXIS(7);
  402. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  403. axis = READ_TRIGGER_AXIS(6);
  404. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  405. }
  406. #undef READ_TRIGGER_AXIS
  407. if (ctx->powerstate_supported) {
  408. SDL_PowerState state;
  409. int percent;
  410. Uint8 status = data[14] >> 7;
  411. Uint8 level = (data[14] & 0x7f);
  412. if (level == 100) {
  413. status = 2;
  414. }
  415. switch (status) {
  416. case 0:
  417. state = SDL_POWERSTATE_ON_BATTERY;
  418. percent = level;
  419. break;
  420. case 1:
  421. state = SDL_POWERSTATE_CHARGING;
  422. percent = level;
  423. break;
  424. case 2:
  425. state = SDL_POWERSTATE_CHARGED;
  426. percent = 100;
  427. break;
  428. default:
  429. state = SDL_POWERSTATE_UNKNOWN;
  430. percent = 0;
  431. break;
  432. }
  433. SDL_SendJoystickPowerInfo(joystick, state, percent);
  434. }
  435. if (ctx->sensors_enabled) {
  436. Uint64 sensor_timestamp;
  437. float values[3];
  438. ABITDO_SENSORS *sensors = (ABITDO_SENSORS *)&data[15];
  439. // Note: we cannot use the time stamp of the receiving computer due to packet delay creating "spiky" timings.
  440. // The imu time stamp is intended to be the sample time of the on-board hardware.
  441. // In the absence of time stamp data from the data[], we can simulate that by
  442. // advancing a time stamp by the observed/known imu clock rate. This is 8ms = 125 Hz
  443. sensor_timestamp = ctx->sensor_timestamp;
  444. ctx->sensor_timestamp += SENSOR_INTERVAL_NS;
  445. // This device's IMU values are reported differently from SDL
  446. // Thus we perform a rotation of the coordinate system to match the SDL standard.
  447. // By observation of this device:
  448. // Hardware x is reporting roll (rotation about the power jack's axis)
  449. // Hardware y is reporting pitch (rotation about the horizontal axis)
  450. // Hardware z is reporting yaw (rotation about the joysticks' center axis)
  451. values[0] = -sensors->sGyroY * ctx->gyroScale; // Rotation around pitch axis
  452. values[1] = sensors->sGyroZ * ctx->gyroScale; // Rotation around yaw axis
  453. values[2] = -sensors->sGyroX * ctx->gyroScale; // Rotation around roll axis
  454. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, values, 3);
  455. // By observation of this device:
  456. // Accelerometer X is positive when front of the controller points toward the sky.
  457. // Accelerometer y is positive when left side of the controller points toward the sky.
  458. // Accelerometer Z is positive when sticks point toward the sky.
  459. values[0] = -sensors->sAccelY * ctx->accelScale; // Acceleration along pitch axis
  460. values[1] = sensors->sAccelZ * ctx->accelScale; // Acceleration along yaw axis
  461. values[2] = -sensors->sAccelX * ctx->accelScale; // Acceleration along roll axis
  462. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, values, 3);
  463. }
  464. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  465. }
  466. static bool HIDAPI_Driver8BitDo_UpdateDevice(SDL_HIDAPI_Device *device)
  467. {
  468. SDL_Driver8BitDo_Context *ctx = (SDL_Driver8BitDo_Context *)device->context;
  469. SDL_Joystick *joystick = NULL;
  470. Uint8 data[USB_PACKET_LENGTH];
  471. int size = 0;
  472. if (device->num_joysticks > 0) {
  473. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  474. } else {
  475. return false;
  476. }
  477. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  478. #ifdef DEBUG_8BITDO_PROTOCOL
  479. HIDAPI_DumpPacket("8BitDo packet: size = %d", data, size);
  480. #endif
  481. if (!joystick) {
  482. continue;
  483. }
  484. if (size == 9) {
  485. // Old firmware USB report for the SF30 Pro and SN30 Pro controllers
  486. HIDAPI_Driver8BitDo_HandleOldStatePacket(joystick, ctx, data, size);
  487. } else {
  488. HIDAPI_Driver8BitDo_HandleStatePacket(joystick, ctx, data, size);
  489. }
  490. }
  491. if (size < 0) {
  492. // Read error, device is disconnected
  493. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  494. }
  495. return (size >= 0);
  496. }
  497. static void HIDAPI_Driver8BitDo_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  498. {
  499. }
  500. static void HIDAPI_Driver8BitDo_FreeDevice(SDL_HIDAPI_Device *device)
  501. {
  502. }
  503. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_Driver8BitDo = {
  504. SDL_HINT_JOYSTICK_HIDAPI_8BITDO,
  505. true,
  506. HIDAPI_Driver8BitDo_RegisterHints,
  507. HIDAPI_Driver8BitDo_UnregisterHints,
  508. HIDAPI_Driver8BitDo_IsEnabled,
  509. HIDAPI_Driver8BitDo_IsSupportedDevice,
  510. HIDAPI_Driver8BitDo_InitDevice,
  511. HIDAPI_Driver8BitDo_GetDevicePlayerIndex,
  512. HIDAPI_Driver8BitDo_SetDevicePlayerIndex,
  513. HIDAPI_Driver8BitDo_UpdateDevice,
  514. HIDAPI_Driver8BitDo_OpenJoystick,
  515. HIDAPI_Driver8BitDo_RumbleJoystick,
  516. HIDAPI_Driver8BitDo_RumbleJoystickTriggers,
  517. HIDAPI_Driver8BitDo_GetJoystickCapabilities,
  518. HIDAPI_Driver8BitDo_SetJoystickLED,
  519. HIDAPI_Driver8BitDo_SendJoystickEffect,
  520. HIDAPI_Driver8BitDo_SetJoystickSensorsEnabled,
  521. HIDAPI_Driver8BitDo_CloseJoystick,
  522. HIDAPI_Driver8BitDo_FreeDevice,
  523. };
  524. #endif // SDL_JOYSTICK_HIDAPI_8BITDO
  525. #endif // SDL_JOYSTICK_HIDAPI