SDL_hidapi_8bitdo.c 26 KB

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