SDL_hidapi_xboxone.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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 "../SDL_sysjoystick.h"
  22. #include "SDL_hidapijoystick_c.h"
  23. #include "SDL_hidapi_rumble.h"
  24. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  25. // Define this if you want verbose logging of the init sequence
  26. // #define DEBUG_JOYSTICK
  27. // Define this if you want to log all packets from the controller
  28. // #define DEBUG_XBOX_PROTOCOL
  29. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_WINGDK)
  30. #define XBOX_ONE_DRIVER_ACTIVE 1
  31. #else
  32. #define XBOX_ONE_DRIVER_ACTIVE 0
  33. #endif
  34. #define CONTROLLER_IDENTIFY_TIMEOUT_MS 100
  35. #define CONTROLLER_PREPARE_INPUT_TIMEOUT_MS 50
  36. // Deadzone thresholds
  37. #define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE 7849
  38. #define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689
  39. #define XINPUT_GAMEPAD_TRIGGER_THRESHOLD -25058 // Uint8 30 scaled to Sint16 full range
  40. enum
  41. {
  42. SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON = 11
  43. };
  44. // Power on
  45. static const Uint8 xbox_init_power_on[] = {
  46. 0x05, 0x20, 0x00, 0x01, 0x00
  47. };
  48. // Enable LED
  49. static const Uint8 xbox_init_enable_led[] = {
  50. 0x0A, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
  51. };
  52. // This controller passed security check
  53. static const Uint8 xbox_init_security_passed[] = {
  54. 0x06, 0x20, 0x00, 0x02, 0x01, 0x00
  55. };
  56. // Some PowerA controllers need to actually start the rumble motors
  57. static const Uint8 xbox_init_powera_rumble[] = {
  58. 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
  59. 0x1D, 0x1D, 0xFF, 0x00, 0x00
  60. };
  61. // Setup rumble (not needed for Microsoft controllers, but it doesn't hurt)
  62. static const Uint8 xbox_init_rumble[] = {
  63. 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
  64. 0x00, 0x00, 0xFF, 0x00, 0xEB
  65. };
  66. /*
  67. * This specifies the selection of init packets that a gamepad
  68. * will be sent on init *and* the order in which they will be
  69. * sent. The correct sequence number will be added when the
  70. * packet is going to be sent.
  71. */
  72. typedef struct
  73. {
  74. Uint16 vendor_id;
  75. Uint16 product_id;
  76. const Uint8 *data;
  77. int size;
  78. } SDL_DriverXboxOne_InitPacket;
  79. static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
  80. { 0x0000, 0x0000, xbox_init_power_on, sizeof(xbox_init_power_on) },
  81. { 0x0000, 0x0000, xbox_init_enable_led, sizeof(xbox_init_enable_led) },
  82. { 0x0000, 0x0000, xbox_init_security_passed, sizeof(xbox_init_security_passed) },
  83. { 0x24c6, 0x541a, xbox_init_powera_rumble, sizeof(xbox_init_powera_rumble) },
  84. { 0x24c6, 0x542a, xbox_init_powera_rumble, sizeof(xbox_init_powera_rumble) },
  85. { 0x24c6, 0x543a, xbox_init_powera_rumble, sizeof(xbox_init_powera_rumble) },
  86. { 0x0000, 0x0000, xbox_init_rumble, sizeof(xbox_init_rumble) },
  87. };
  88. typedef enum
  89. {
  90. XBOX_ONE_INIT_STATE_ANNOUNCED,
  91. XBOX_ONE_INIT_STATE_IDENTIFYING,
  92. XBOX_ONE_INIT_STATE_STARTUP,
  93. XBOX_ONE_INIT_STATE_PREPARE_INPUT,
  94. XBOX_ONE_INIT_STATE_COMPLETE,
  95. } SDL_XboxOneInitState;
  96. typedef enum
  97. {
  98. XBOX_ONE_RUMBLE_STATE_IDLE,
  99. XBOX_ONE_RUMBLE_STATE_QUEUED,
  100. XBOX_ONE_RUMBLE_STATE_BUSY
  101. } SDL_XboxOneRumbleState;
  102. typedef struct
  103. {
  104. SDL_HIDAPI_Device *device;
  105. Uint16 vendor_id;
  106. Uint16 product_id;
  107. SDL_XboxOneInitState init_state;
  108. Uint64 start_time;
  109. Uint8 sequence;
  110. Uint64 send_time;
  111. bool has_guide_packet;
  112. bool has_color_led;
  113. bool has_paddles;
  114. bool has_unmapped_state;
  115. bool has_trigger_rumble;
  116. bool has_share_button;
  117. Uint8 last_paddle_state;
  118. Uint8 low_frequency_rumble;
  119. Uint8 high_frequency_rumble;
  120. Uint8 left_trigger_rumble;
  121. Uint8 right_trigger_rumble;
  122. SDL_XboxOneRumbleState rumble_state;
  123. Uint64 rumble_time;
  124. bool rumble_pending;
  125. Uint8 last_state[USB_PACKET_LENGTH];
  126. Uint8 *chunk_buffer;
  127. Uint32 chunk_length;
  128. } SDL_DriverXboxOne_Context;
  129. static bool ControllerHasColorLED(Uint16 vendor_id, Uint16 product_id)
  130. {
  131. return vendor_id == USB_VENDOR_MICROSOFT && product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2;
  132. }
  133. static bool ControllerHasPaddles(Uint16 vendor_id, Uint16 product_id)
  134. {
  135. return SDL_IsJoystickXboxOneElite(vendor_id, product_id);
  136. }
  137. static bool ControllerHasTriggerRumble(Uint16 vendor_id, Uint16 product_id)
  138. {
  139. // All the Microsoft Xbox One controllers have trigger rumble
  140. if (vendor_id == USB_VENDOR_MICROSOFT) {
  141. return true;
  142. }
  143. /* It turns out other controllers a mixed bag as to whether they support
  144. trigger rumble or not, and when they do it's often a buzz rather than
  145. the vibration of the Microsoft trigger rumble, so for now just pretend
  146. that it is not available.
  147. */
  148. return false;
  149. }
  150. static bool ControllerHasShareButton(Uint16 vendor_id, Uint16 product_id)
  151. {
  152. return SDL_IsJoystickXboxSeriesX(vendor_id, product_id);
  153. }
  154. static int GetHomeLEDBrightness(const char *hint)
  155. {
  156. const int MAX_VALUE = 50;
  157. int value = 20;
  158. if (hint && *hint) {
  159. if (SDL_strchr(hint, '.') != NULL) {
  160. value = (int)(MAX_VALUE * SDL_atof(hint));
  161. } else if (!SDL_GetStringBoolean(hint, true)) {
  162. value = 0;
  163. }
  164. }
  165. return value;
  166. }
  167. static void SetHomeLED(SDL_DriverXboxOne_Context *ctx, int value)
  168. {
  169. Uint8 led_packet[] = { 0x0A, 0x20, 0x00, 0x03, 0x00, 0x00, 0x00 };
  170. if (value > 0) {
  171. led_packet[5] = 0x01;
  172. led_packet[6] = (Uint8)value;
  173. }
  174. SDL_HIDAPI_SendRumble(ctx->device, led_packet, sizeof(led_packet));
  175. }
  176. static void SDLCALL SDL_HomeLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  177. {
  178. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)userdata;
  179. if (hint && *hint) {
  180. SetHomeLED(ctx, GetHomeLEDBrightness(hint));
  181. }
  182. }
  183. static void SetInitState(SDL_DriverXboxOne_Context *ctx, SDL_XboxOneInitState state)
  184. {
  185. #ifdef DEBUG_JOYSTICK
  186. SDL_Log("Setting init state %d", state);
  187. #endif
  188. ctx->init_state = state;
  189. }
  190. static Uint8 GetNextPacketSequence(SDL_DriverXboxOne_Context *ctx)
  191. {
  192. ++ctx->sequence;
  193. if (!ctx->sequence) {
  194. ctx->sequence = 1;
  195. }
  196. return ctx->sequence;
  197. }
  198. static bool SendProtocolPacket(SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
  199. {
  200. #ifdef DEBUG_XBOX_PROTOCOL
  201. HIDAPI_DumpPacket("Xbox One sending packet: size = %d", data, size);
  202. #endif
  203. ctx->send_time = SDL_GetTicks();
  204. if (!SDL_HIDAPI_LockRumble()) {
  205. return false;
  206. }
  207. if (SDL_HIDAPI_SendRumbleAndUnlock(ctx->device, data, size) != size) {
  208. return false;
  209. }
  210. return true;
  211. }
  212. #if 0
  213. static bool SendSerialRequest(SDL_DriverXboxOne_Context *ctx)
  214. {
  215. Uint8 packet[] = { 0x1E, 0x20, 0x00, 0x01, 0x04 };
  216. packet[2] = GetNextPacketSequence(ctx);
  217. /* Request the serial number
  218. * Sending this should be done only after startup is complete.
  219. * It will cancel the announce packet if sent before that, and will be
  220. * ignored if sent during the startup sequence.
  221. */
  222. if (!SendProtocolPacket(ctx, packet, sizeof(packet))) {
  223. SDL_SetError("Couldn't send serial request packet");
  224. return false;
  225. }
  226. return true;
  227. }
  228. #endif
  229. static bool ControllerSendsAnnouncement(Uint16 vendor_id, Uint16 product_id)
  230. {
  231. if (vendor_id == USB_VENDOR_PDP && product_id == 0x0246) {
  232. // The PDP Rock Candy (PID 0x0246) doesn't send the announce packet on Linux for some reason
  233. return false;
  234. }
  235. return true;
  236. }
  237. static bool SendIdentificationRequest(SDL_DriverXboxOne_Context *ctx)
  238. {
  239. // Request identification, sent in response to announce packet
  240. Uint8 packet[] = {
  241. 0x04, 0x20, 0x00, 0x00
  242. };
  243. packet[2] = GetNextPacketSequence(ctx);
  244. if (!SendProtocolPacket(ctx, packet, sizeof(packet))) {
  245. SDL_SetError("Couldn't send identification request packet");
  246. return false;
  247. }
  248. return true;
  249. }
  250. static bool SendControllerStartup(SDL_DriverXboxOne_Context *ctx)
  251. {
  252. Uint16 vendor_id = ctx->vendor_id;
  253. Uint16 product_id = ctx->product_id;
  254. Uint8 init_packet[USB_PACKET_LENGTH];
  255. size_t i;
  256. for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
  257. const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[i];
  258. if (packet->vendor_id && (vendor_id != packet->vendor_id)) {
  259. continue;
  260. }
  261. if (packet->product_id && (product_id != packet->product_id)) {
  262. continue;
  263. }
  264. SDL_memcpy(init_packet, packet->data, packet->size);
  265. init_packet[2] = GetNextPacketSequence(ctx);
  266. if (init_packet[0] == 0x0A) {
  267. // Get the initial brightness value
  268. int brightness = GetHomeLEDBrightness(SDL_GetHint(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED));
  269. init_packet[5] = (brightness > 0) ? 0x01 : 0x00;
  270. init_packet[6] = (Uint8)brightness;
  271. }
  272. if (!SendProtocolPacket(ctx, init_packet, packet->size)) {
  273. SDL_SetError("Couldn't send initialization packet");
  274. return false;
  275. }
  276. // Wait to process the rumble packet
  277. if (packet->data == xbox_init_powera_rumble) {
  278. SDL_Delay(10);
  279. }
  280. }
  281. return true;
  282. }
  283. static void HIDAPI_DriverXboxOne_RegisterHints(SDL_HintCallback callback, void *userdata)
  284. {
  285. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX, callback, userdata);
  286. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE, callback, userdata);
  287. }
  288. static void HIDAPI_DriverXboxOne_UnregisterHints(SDL_HintCallback callback, void *userdata)
  289. {
  290. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX, callback, userdata);
  291. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE, callback, userdata);
  292. }
  293. static bool HIDAPI_DriverXboxOne_IsEnabled(void)
  294. {
  295. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE,
  296. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT)));
  297. }
  298. static bool HIDAPI_DriverXboxOne_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)
  299. {
  300. #if defined(SDL_PLATFORM_MACOS) && defined(SDL_JOYSTICK_MFI)
  301. if (!SDL_IsJoystickBluetoothXboxOne(vendor_id, product_id)) {
  302. // On macOS we get a shortened version of the real report and
  303. // you can't write output reports for wired controllers, so
  304. // we'll just use the GCController support instead.
  305. return false;
  306. }
  307. #endif
  308. return (type == SDL_GAMEPAD_TYPE_XBOXONE);
  309. }
  310. static bool HIDAPI_DriverXboxOne_InitDevice(SDL_HIDAPI_Device *device)
  311. {
  312. SDL_DriverXboxOne_Context *ctx;
  313. ctx = (SDL_DriverXboxOne_Context *)SDL_calloc(1, sizeof(*ctx));
  314. if (!ctx) {
  315. return false;
  316. }
  317. ctx->device = device;
  318. device->context = ctx;
  319. ctx->vendor_id = device->vendor_id;
  320. ctx->product_id = device->product_id;
  321. ctx->start_time = SDL_GetTicks();
  322. ctx->sequence = 0;
  323. ctx->has_color_led = ControllerHasColorLED(ctx->vendor_id, ctx->product_id);
  324. ctx->has_paddles = ControllerHasPaddles(ctx->vendor_id, ctx->product_id);
  325. ctx->has_trigger_rumble = ControllerHasTriggerRumble(ctx->vendor_id, ctx->product_id);
  326. ctx->has_share_button = ControllerHasShareButton(ctx->vendor_id, ctx->product_id);
  327. // Assume that the controller is correctly initialized when we start
  328. if (!ControllerSendsAnnouncement(device->vendor_id, device->product_id)) {
  329. // Jump into the startup sequence for this controller
  330. ctx->init_state = XBOX_ONE_INIT_STATE_STARTUP;
  331. } else {
  332. ctx->init_state = XBOX_ONE_INIT_STATE_COMPLETE;
  333. }
  334. #ifdef DEBUG_JOYSTICK
  335. SDL_Log("Controller version: %d (0x%.4x)", device->version, device->version);
  336. #endif
  337. device->type = SDL_GAMEPAD_TYPE_XBOXONE;
  338. return HIDAPI_JoystickConnected(device, NULL);
  339. }
  340. static int HIDAPI_DriverXboxOne_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  341. {
  342. return -1;
  343. }
  344. static void HIDAPI_DriverXboxOne_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  345. {
  346. }
  347. static bool HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  348. {
  349. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  350. SDL_AssertJoysticksLocked();
  351. ctx->low_frequency_rumble = 0;
  352. ctx->high_frequency_rumble = 0;
  353. ctx->left_trigger_rumble = 0;
  354. ctx->right_trigger_rumble = 0;
  355. ctx->rumble_state = XBOX_ONE_RUMBLE_STATE_IDLE;
  356. ctx->rumble_time = 0;
  357. ctx->rumble_pending = false;
  358. SDL_zeroa(ctx->last_state);
  359. // Initialize the joystick capabilities
  360. joystick->nbuttons = 11;
  361. if (ctx->has_share_button) {
  362. joystick->nbuttons += 1;
  363. }
  364. if (ctx->has_paddles) {
  365. joystick->nbuttons += 4;
  366. }
  367. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  368. joystick->nhats = 1;
  369. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED,
  370. SDL_HomeLEDHintChanged, ctx);
  371. return true;
  372. }
  373. static void HIDAPI_DriverXboxOne_RumbleSent(void *userdata)
  374. {
  375. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)userdata;
  376. ctx->rumble_time = SDL_GetTicks();
  377. }
  378. static bool HIDAPI_DriverXboxOne_UpdateRumble(SDL_DriverXboxOne_Context *ctx)
  379. {
  380. if (ctx->rumble_state == XBOX_ONE_RUMBLE_STATE_QUEUED) {
  381. if (ctx->rumble_time) {
  382. ctx->rumble_state = XBOX_ONE_RUMBLE_STATE_BUSY;
  383. }
  384. }
  385. if (ctx->rumble_state == XBOX_ONE_RUMBLE_STATE_BUSY) {
  386. const int RUMBLE_BUSY_TIME_MS = ctx->device->is_bluetooth ? 50 : 10;
  387. if (SDL_GetTicks() >= (ctx->rumble_time + RUMBLE_BUSY_TIME_MS)) {
  388. ctx->rumble_time = 0;
  389. ctx->rumble_state = XBOX_ONE_RUMBLE_STATE_IDLE;
  390. }
  391. }
  392. if (!ctx->rumble_pending) {
  393. return true;
  394. }
  395. if (ctx->rumble_state != XBOX_ONE_RUMBLE_STATE_IDLE) {
  396. return true;
  397. }
  398. // We're no longer pending, even if we fail to send the rumble below
  399. ctx->rumble_pending = false;
  400. if (!SDL_HIDAPI_LockRumble()) {
  401. return false;
  402. }
  403. if (ctx->device->is_bluetooth) {
  404. Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xEB };
  405. rumble_packet[2] = ctx->left_trigger_rumble;
  406. rumble_packet[3] = ctx->right_trigger_rumble;
  407. rumble_packet[4] = ctx->low_frequency_rumble;
  408. rumble_packet[5] = ctx->high_frequency_rumble;
  409. if (SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(ctx->device, rumble_packet, sizeof(rumble_packet), HIDAPI_DriverXboxOne_RumbleSent, ctx) != sizeof(rumble_packet)) {
  410. return SDL_SetError("Couldn't send rumble packet");
  411. }
  412. } else {
  413. Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xEB };
  414. rumble_packet[6] = ctx->left_trigger_rumble;
  415. rumble_packet[7] = ctx->right_trigger_rumble;
  416. rumble_packet[8] = ctx->low_frequency_rumble;
  417. rumble_packet[9] = ctx->high_frequency_rumble;
  418. if (SDL_HIDAPI_SendRumbleWithCallbackAndUnlock(ctx->device, rumble_packet, sizeof(rumble_packet), HIDAPI_DriverXboxOne_RumbleSent, ctx) != sizeof(rumble_packet)) {
  419. return SDL_SetError("Couldn't send rumble packet");
  420. }
  421. }
  422. ctx->rumble_state = XBOX_ONE_RUMBLE_STATE_QUEUED;
  423. return true;
  424. }
  425. static bool HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  426. {
  427. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  428. // Magnitude is 1..100 so scale the 16-bit input here
  429. ctx->low_frequency_rumble = (Uint8)(low_frequency_rumble / 655);
  430. ctx->high_frequency_rumble = (Uint8)(high_frequency_rumble / 655);
  431. ctx->rumble_pending = true;
  432. return HIDAPI_DriverXboxOne_UpdateRumble(ctx);
  433. }
  434. static bool HIDAPI_DriverXboxOne_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  435. {
  436. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  437. if (!ctx->has_trigger_rumble) {
  438. return SDL_Unsupported();
  439. }
  440. // Magnitude is 1..100 so scale the 16-bit input here
  441. ctx->left_trigger_rumble = (Uint8)(left_rumble / 655);
  442. ctx->right_trigger_rumble = (Uint8)(right_rumble / 655);
  443. ctx->rumble_pending = true;
  444. return HIDAPI_DriverXboxOne_UpdateRumble(ctx);
  445. }
  446. static Uint32 HIDAPI_DriverXboxOne_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  447. {
  448. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  449. Uint32 result = 0;
  450. result |= SDL_JOYSTICK_CAP_RUMBLE;
  451. if (ctx->has_trigger_rumble) {
  452. result |= SDL_JOYSTICK_CAP_TRIGGER_RUMBLE;
  453. }
  454. if (ctx->has_color_led) {
  455. result |= SDL_JOYSTICK_CAP_RGB_LED;
  456. }
  457. return result;
  458. }
  459. static bool HIDAPI_DriverXboxOne_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  460. {
  461. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  462. if (ctx->has_color_led) {
  463. Uint8 led_packet[] = { 0x0E, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 };
  464. led_packet[5] = 0x00; // Whiteness? Sets white intensity when RGB is 0, seems additive
  465. led_packet[6] = red;
  466. led_packet[7] = green;
  467. led_packet[8] = blue;
  468. if (SDL_HIDAPI_SendRumble(device, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
  469. return SDL_SetError("Couldn't send LED packet");
  470. }
  471. return true;
  472. } else {
  473. return SDL_Unsupported();
  474. }
  475. }
  476. static bool HIDAPI_DriverXboxOne_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  477. {
  478. return SDL_Unsupported();
  479. }
  480. static bool HIDAPI_DriverXboxOne_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  481. {
  482. return SDL_Unsupported();
  483. }
  484. /*
  485. * The Xbox One Elite controller with 5.13+ firmware sends the unmapped state in a separate packet.
  486. * We can use this to send the paddle state when they aren't mapped
  487. */
  488. static void HIDAPI_DriverXboxOne_HandleUnmappedStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  489. {
  490. Uint8 profile;
  491. int paddle_index;
  492. int button1_bit;
  493. int button2_bit;
  494. int button3_bit;
  495. int button4_bit;
  496. bool paddles_mapped;
  497. Uint64 timestamp = SDL_GetTicksNS();
  498. if (size == 17) {
  499. // XBox One Elite Series 2
  500. paddle_index = 14;
  501. button1_bit = 0x01;
  502. button2_bit = 0x02;
  503. button3_bit = 0x04;
  504. button4_bit = 0x08;
  505. profile = data[15];
  506. if (profile == 0) {
  507. paddles_mapped = false;
  508. } else if (SDL_memcmp(&data[0], &ctx->last_state[0], 14) == 0) {
  509. // We're using a profile, but paddles aren't mapped
  510. paddles_mapped = false;
  511. } else {
  512. // Something is mapped, we can't use the paddles
  513. paddles_mapped = true;
  514. }
  515. } else {
  516. // Unknown format
  517. return;
  518. }
  519. #ifdef DEBUG_XBOX_PROTOCOL
  520. SDL_Log(">>> Paddles: %d,%d,%d,%d mapped = %s",
  521. (data[paddle_index] & button1_bit) ? 1 : 0,
  522. (data[paddle_index] & button2_bit) ? 1 : 0,
  523. (data[paddle_index] & button3_bit) ? 1 : 0,
  524. (data[paddle_index] & button4_bit) ? 1 : 0,
  525. paddles_mapped ? "TRUE" : "FALSE");
  526. #endif
  527. if (paddles_mapped) {
  528. // Respect that the paddles are being used for other controls and don't pass them on to the app
  529. data[paddle_index] = 0;
  530. }
  531. if (ctx->last_paddle_state != data[paddle_index]) {
  532. Uint8 nButton = (Uint8)(SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON + ctx->has_share_button); // Next available button
  533. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button1_bit) != 0));
  534. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button2_bit) != 0));
  535. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button3_bit) != 0));
  536. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button4_bit) != 0));
  537. ctx->last_paddle_state = data[paddle_index];
  538. }
  539. ctx->has_unmapped_state = true;
  540. }
  541. static void HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  542. {
  543. Sint16 axis;
  544. Uint64 timestamp = SDL_GetTicksNS();
  545. // Enable paddles on the Xbox Elite controller when connected over USB
  546. if (ctx->has_paddles && !ctx->has_unmapped_state && size == 46) {
  547. Uint8 packet[] = { 0x4d, 0x00, 0x00, 0x02, 0x07, 0x00 };
  548. #ifdef DEBUG_JOYSTICK
  549. SDL_Log("Enabling paddles on XBox Elite 2");
  550. #endif
  551. SDL_HIDAPI_SendRumble(ctx->device, packet, sizeof(packet));
  552. }
  553. if (ctx->last_state[0] != data[0]) {
  554. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[0] & 0x04) != 0));
  555. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[0] & 0x08) != 0));
  556. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[0] & 0x10) != 0));
  557. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[0] & 0x20) != 0));
  558. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[0] & 0x40) != 0));
  559. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[0] & 0x80) != 0));
  560. }
  561. if (ctx->last_state[1] != data[1]) {
  562. Uint8 hat = 0;
  563. if (data[1] & 0x01) {
  564. hat |= SDL_HAT_UP;
  565. }
  566. if (data[1] & 0x02) {
  567. hat |= SDL_HAT_DOWN;
  568. }
  569. if (data[1] & 0x04) {
  570. hat |= SDL_HAT_LEFT;
  571. }
  572. if (data[1] & 0x08) {
  573. hat |= SDL_HAT_RIGHT;
  574. }
  575. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  576. if (ctx->vendor_id == USB_VENDOR_RAZER && ctx->product_id == USB_PRODUCT_RAZER_ATROX) {
  577. // The Razer Atrox has the right and left shoulder bits reversed
  578. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[1] & 0x20) != 0));
  579. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[1] & 0x10) != 0));
  580. } else {
  581. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[1] & 0x10) != 0));
  582. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[1] & 0x20) != 0));
  583. }
  584. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[1] & 0x40) != 0));
  585. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[1] & 0x80) != 0));
  586. }
  587. if (ctx->has_share_button) {
  588. /* Xbox Series X firmware version 5.0, report is 32 bytes, share button is in byte 14
  589. * Xbox Series X firmware version 5.1, report is 40 bytes, share button is in byte 14
  590. * Xbox Series X firmware version 5.5, report is 44 bytes, share button is in byte 18
  591. * Victrix Gambit Tournament Controller, report is 46 bytes, share button is in byte 28
  592. * ThrustMaster eSwap PRO Controller Xbox, report is 60 bytes, share button is in byte 42
  593. */
  594. if (size < 44) {
  595. if (ctx->last_state[14] != data[14]) {
  596. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON, ((data[14] & 0x01) != 0));
  597. }
  598. } else if (size == 44) {
  599. if (ctx->last_state[18] != data[18]) {
  600. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON, ((data[18] & 0x01) != 0));
  601. }
  602. } else if (size == 46) {
  603. if (ctx->last_state[28] != data[28]) {
  604. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON, ((data[28] & 0x01) != 0));
  605. }
  606. } else if (size == 60) {
  607. if (ctx->last_state[42] != data[42]) {
  608. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON, ((data[42] & 0x01) != 0));
  609. }
  610. }
  611. }
  612. /* Xbox One S report is 14 bytes
  613. Xbox One Elite Series 1 report is 29 bytes, paddles in data[28], mode in data[28] & 0x10, both modes have mapped paddles by default
  614. Paddle bits:
  615. P3: 0x01 (A) P1: 0x02 (B)
  616. P4: 0x04 (X) P2: 0x08 (Y)
  617. Xbox One Elite Series 2 4.x firmware report is 34 bytes, paddles in data[14], mode in data[15], mode 0 has no mapped paddles by default
  618. Paddle bits:
  619. P3: 0x04 (A) P1: 0x01 (B)
  620. P4: 0x08 (X) P2: 0x02 (Y)
  621. Xbox One Elite Series 2 5.x firmware report is 46 bytes, paddles in data[18], mode in data[19], mode 0 has no mapped paddles by default
  622. Paddle bits:
  623. P3: 0x04 (A) P1: 0x01 (B)
  624. P4: 0x08 (X) P2: 0x02 (Y)
  625. Xbox One Elite Series 2 5.17+ firmware report is 47 bytes, paddles in data[14], mode in data[20], mode 0 has no mapped paddles by default
  626. Paddle bits:
  627. P3: 0x04 (A) P1: 0x01 (B)
  628. P4: 0x08 (X) P2: 0x02 (Y)
  629. */
  630. if (ctx->has_paddles && !ctx->has_unmapped_state && (size == 29 || size == 34 || size == 46 || size == 47)) {
  631. int paddle_index;
  632. int button1_bit;
  633. int button2_bit;
  634. int button3_bit;
  635. int button4_bit;
  636. bool paddles_mapped;
  637. if (size == 29) {
  638. // XBox One Elite Series 1
  639. paddle_index = 28;
  640. button1_bit = 0x02;
  641. button2_bit = 0x08;
  642. button3_bit = 0x01;
  643. button4_bit = 0x04;
  644. // The mapped controller state is at offset 0, the raw state is at offset 14, compare them to see if the paddles are mapped
  645. paddles_mapped = (SDL_memcmp(&data[0], &data[14], 2) != 0);
  646. } else if (size == 34) {
  647. // XBox One Elite Series 2
  648. paddle_index = 14;
  649. button1_bit = 0x01;
  650. button2_bit = 0x02;
  651. button3_bit = 0x04;
  652. button4_bit = 0x08;
  653. paddles_mapped = (data[15] != 0);
  654. } else if (size == 46) {
  655. // XBox One Elite Series 2
  656. paddle_index = 18;
  657. button1_bit = 0x01;
  658. button2_bit = 0x02;
  659. button3_bit = 0x04;
  660. button4_bit = 0x08;
  661. paddles_mapped = (data[19] != 0);
  662. } else /* if (size == 47) */ {
  663. // XBox One Elite Series 2
  664. paddle_index = 14;
  665. button1_bit = 0x01;
  666. button2_bit = 0x02;
  667. button3_bit = 0x04;
  668. button4_bit = 0x08;
  669. paddles_mapped = (data[20] != 0);
  670. }
  671. #ifdef DEBUG_XBOX_PROTOCOL
  672. SDL_Log(">>> Paddles: %d,%d,%d,%d mapped = %s",
  673. (data[paddle_index] & button1_bit) ? 1 : 0,
  674. (data[paddle_index] & button2_bit) ? 1 : 0,
  675. (data[paddle_index] & button3_bit) ? 1 : 0,
  676. (data[paddle_index] & button4_bit) ? 1 : 0,
  677. paddles_mapped ? "TRUE" : "FALSE");
  678. #endif
  679. if (paddles_mapped) {
  680. // Respect that the paddles are being used for other controls and don't pass them on to the app
  681. data[paddle_index] = 0;
  682. }
  683. if (ctx->last_paddle_state != data[paddle_index]) {
  684. Uint8 nButton = (Uint8)(SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON + ctx->has_share_button); // Next available button
  685. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button1_bit) != 0));
  686. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button2_bit) != 0));
  687. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button3_bit) != 0));
  688. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button4_bit) != 0));
  689. ctx->last_paddle_state = data[paddle_index];
  690. }
  691. }
  692. axis = ((int)SDL_Swap16LE(*(Sint16 *)(&data[2])) * 64) - 32768;
  693. if (axis == 32704) {
  694. axis = 32767;
  695. }
  696. if (axis == -32768 && size == 26 && (data[18] & 0x80)) {
  697. axis = 32767;
  698. }
  699. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  700. axis = ((int)SDL_Swap16LE(*(Sint16 *)(&data[4])) * 64) - 32768;
  701. if (axis == -32768 && size == 26 && (data[18] & 0x40)) {
  702. axis = 32767;
  703. }
  704. if (axis == 32704) {
  705. axis = 32767;
  706. }
  707. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  708. axis = SDL_Swap16LE(*(Sint16 *)(&data[6]));
  709. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  710. axis = SDL_Swap16LE(*(Sint16 *)(&data[8]));
  711. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, ~axis);
  712. axis = SDL_Swap16LE(*(Sint16 *)(&data[10]));
  713. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  714. axis = SDL_Swap16LE(*(Sint16 *)(&data[12]));
  715. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, ~axis);
  716. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  717. // We don't have the unmapped state for this packet
  718. ctx->has_unmapped_state = false;
  719. }
  720. static void HIDAPI_DriverXboxOne_HandleStatusPacket(SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
  721. {
  722. if (ctx->init_state < XBOX_ONE_INIT_STATE_COMPLETE) {
  723. SetInitState(ctx, XBOX_ONE_INIT_STATE_COMPLETE);
  724. }
  725. }
  726. static void HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
  727. {
  728. Uint64 timestamp = SDL_GetTicksNS();
  729. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[0] & 0x01) != 0));
  730. }
  731. /*
  732. * Xbox One S with firmware 3.1.1221 uses a 16 byte packet and the GUIDE button in a separate packet
  733. */
  734. static void HIDAPI_DriverXboxOneBluetooth_HandleButtons16(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
  735. {
  736. if (ctx->last_state[14] != data[14]) {
  737. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[14] & 0x01) != 0));
  738. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[14] & 0x02) != 0));
  739. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[14] & 0x04) != 0));
  740. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[14] & 0x08) != 0));
  741. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[14] & 0x10) != 0));
  742. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[14] & 0x20) != 0));
  743. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[14] & 0x40) != 0));
  744. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[14] & 0x80) != 0));
  745. }
  746. if (ctx->last_state[15] != data[15]) {
  747. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[15] & 0x01) != 0));
  748. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[15] & 0x02) != 0));
  749. }
  750. }
  751. /*
  752. * Xbox One S with firmware 4.8.1923 uses a 17 byte packet with BACK button in byte 16 and the GUIDE button in a separate packet (on Windows), or in byte 15 (on Linux)
  753. * Xbox One S with firmware 5.x uses a 17 byte packet with BACK and GUIDE buttons in byte 15
  754. * Xbox One Elite Series 2 with firmware 4.7.1872 uses a 55 byte packet with BACK button in byte 16, paddles starting at byte 33, and the GUIDE button in a separate packet
  755. * Xbox One Elite Series 2 with firmware 4.8.1908 uses a 33 byte packet with BACK button in byte 16, paddles starting at byte 17, and the GUIDE button in a separate packet
  756. * Xbox One Elite Series 2 with firmware 5.11.3112 uses a 19 byte packet with BACK and GUIDE buttons in byte 15
  757. * Xbox Series X with firmware 5.5.2641 uses a 17 byte packet with BACK and GUIDE buttons in byte 15, and SHARE button in byte 17
  758. */
  759. static void HIDAPI_DriverXboxOneBluetooth_HandleButtons(Uint64 timestamp, SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  760. {
  761. if (ctx->last_state[14] != data[14]) {
  762. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[14] & 0x01) != 0));
  763. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[14] & 0x02) != 0));
  764. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[14] & 0x08) != 0));
  765. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[14] & 0x10) != 0));
  766. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[14] & 0x40) != 0));
  767. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[14] & 0x80) != 0));
  768. }
  769. if (ctx->last_state[15] != data[15]) {
  770. if (!ctx->has_guide_packet) {
  771. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[15] & 0x10) != 0));
  772. }
  773. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[15] & 0x08) != 0));
  774. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[15] & 0x20) != 0));
  775. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[15] & 0x40) != 0));
  776. }
  777. if (ctx->has_share_button) {
  778. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[15] & 0x04) != 0));
  779. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON, ((data[16] & 0x01) != 0));
  780. } else {
  781. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[15] & 0x04) || ((data[16] & 0x01)) != 0));
  782. }
  783. /*
  784. Paddle bits:
  785. P3: 0x04 (A) P1: 0x01 (B)
  786. P4: 0x08 (X) P2: 0x02 (Y)
  787. */
  788. if (ctx->has_paddles && (size == 20 || size == 39 || size == 55)) {
  789. int paddle_index;
  790. int button1_bit;
  791. int button2_bit;
  792. int button3_bit;
  793. int button4_bit;
  794. bool paddles_mapped;
  795. if (size == 55) {
  796. // Initial firmware for the Xbox Elite Series 2 controller
  797. paddle_index = 33;
  798. button1_bit = 0x01;
  799. button2_bit = 0x02;
  800. button3_bit = 0x04;
  801. button4_bit = 0x08;
  802. paddles_mapped = (data[35] != 0);
  803. } else if (size == 39) {
  804. // Updated firmware for the Xbox Elite Series 2 controller
  805. paddle_index = 17;
  806. button1_bit = 0x01;
  807. button2_bit = 0x02;
  808. button3_bit = 0x04;
  809. button4_bit = 0x08;
  810. paddles_mapped = (data[19] != 0);
  811. } else /* if (size == 20) */ {
  812. // Updated firmware for the Xbox Elite Series 2 controller (5.13+)
  813. paddle_index = 19;
  814. button1_bit = 0x01;
  815. button2_bit = 0x02;
  816. button3_bit = 0x04;
  817. button4_bit = 0x08;
  818. paddles_mapped = (data[17] != 0);
  819. }
  820. #ifdef DEBUG_XBOX_PROTOCOL
  821. SDL_Log(">>> Paddles: %d,%d,%d,%d mapped = %s",
  822. (data[paddle_index] & button1_bit) ? 1 : 0,
  823. (data[paddle_index] & button2_bit) ? 1 : 0,
  824. (data[paddle_index] & button3_bit) ? 1 : 0,
  825. (data[paddle_index] & button4_bit) ? 1 : 0,
  826. paddles_mapped ? "TRUE" : "FALSE");
  827. #endif
  828. if (paddles_mapped) {
  829. // Respect that the paddles are being used for other controls and don't pass them on to the app
  830. data[paddle_index] = 0;
  831. }
  832. if (ctx->last_paddle_state != data[paddle_index]) {
  833. Uint8 nButton = SDL_GAMEPAD_BUTTON_XBOX_SHARE_BUTTON; // Next available button
  834. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button1_bit) != 0));
  835. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button2_bit) != 0));
  836. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button3_bit) != 0));
  837. SDL_SendJoystickButton(timestamp, joystick, nButton++, ((data[paddle_index] & button4_bit) != 0));
  838. ctx->last_paddle_state = data[paddle_index];
  839. }
  840. }
  841. }
  842. static void HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  843. {
  844. Sint16 axis;
  845. Uint64 timestamp = SDL_GetTicksNS();
  846. if (size == 16) {
  847. // Original Xbox One S, with separate report for guide button
  848. HIDAPI_DriverXboxOneBluetooth_HandleButtons16(timestamp, joystick, ctx, data, size);
  849. } else if (size > 16) {
  850. HIDAPI_DriverXboxOneBluetooth_HandleButtons(timestamp, joystick, ctx, data, size);
  851. } else {
  852. #ifdef DEBUG_XBOX_PROTOCOL
  853. SDL_Log("Unknown Bluetooth state packet format");
  854. #endif
  855. return;
  856. }
  857. if (ctx->last_state[13] != data[13]) {
  858. Uint8 hat;
  859. switch (data[13]) {
  860. case 1:
  861. hat = SDL_HAT_UP;
  862. break;
  863. case 2:
  864. hat = SDL_HAT_RIGHTUP;
  865. break;
  866. case 3:
  867. hat = SDL_HAT_RIGHT;
  868. break;
  869. case 4:
  870. hat = SDL_HAT_RIGHTDOWN;
  871. break;
  872. case 5:
  873. hat = SDL_HAT_DOWN;
  874. break;
  875. case 6:
  876. hat = SDL_HAT_LEFTDOWN;
  877. break;
  878. case 7:
  879. hat = SDL_HAT_LEFT;
  880. break;
  881. case 8:
  882. hat = SDL_HAT_LEFTUP;
  883. break;
  884. default:
  885. hat = SDL_HAT_CENTERED;
  886. break;
  887. }
  888. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  889. }
  890. axis = ((int)SDL_Swap16LE(*(Sint16 *)(&data[9])) * 64) - 32768;
  891. if (axis == 32704) {
  892. axis = 32767;
  893. }
  894. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  895. axis = ((int)SDL_Swap16LE(*(Sint16 *)(&data[11])) * 64) - 32768;
  896. if (axis == 32704) {
  897. axis = 32767;
  898. }
  899. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  900. axis = (int)SDL_Swap16LE(*(Uint16 *)(&data[1])) - 0x8000;
  901. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  902. axis = (int)SDL_Swap16LE(*(Uint16 *)(&data[3])) - 0x8000;
  903. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  904. axis = (int)SDL_Swap16LE(*(Uint16 *)(&data[5])) - 0x8000;
  905. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  906. axis = (int)SDL_Swap16LE(*(Uint16 *)(&data[7])) - 0x8000;
  907. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  908. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  909. }
  910. static void HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
  911. {
  912. Uint64 timestamp = SDL_GetTicksNS();
  913. ctx->has_guide_packet = true;
  914. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[1] & 0x01) != 0));
  915. }
  916. static void HIDAPI_DriverXboxOneBluetooth_HandleBatteryPacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
  917. {
  918. Uint8 flags = data[1];
  919. bool on_usb = (((flags & 0x0C) >> 2) == 0);
  920. SDL_PowerState state;
  921. int percent = 0;
  922. // Mapped percentage value from:
  923. // https://learn.microsoft.com/en-us/gaming/gdk/_content/gc/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getbatterystate
  924. switch (flags & 0x03) {
  925. case 0:
  926. percent = 10;
  927. break;
  928. case 1:
  929. percent = 40;
  930. break;
  931. case 2:
  932. percent = 70;
  933. break;
  934. case 3:
  935. percent = 100;
  936. break;
  937. }
  938. if (on_usb) {
  939. state = SDL_POWERSTATE_CHARGING;
  940. } else {
  941. state = SDL_POWERSTATE_ON_BATTERY;
  942. }
  943. SDL_SendJoystickPowerInfo(joystick, state, percent);
  944. }
  945. static void HIDAPI_DriverXboxOne_HandleSerialIDPacket(SDL_DriverXboxOne_Context *ctx, const Uint8 *data, int size)
  946. {
  947. char serial[29];
  948. int i;
  949. for (i = 0; i < 14; ++i) {
  950. SDL_uitoa(data[2 + i], &serial[i * 2], 16);
  951. }
  952. serial[i * 2] = '\0';
  953. #ifdef DEBUG_JOYSTICK
  954. SDL_Log("Setting serial number to %s", serial);
  955. #endif
  956. HIDAPI_SetDeviceSerial(ctx->device, serial);
  957. }
  958. static bool HIDAPI_DriverXboxOne_UpdateInitState(SDL_DriverXboxOne_Context *ctx)
  959. {
  960. SDL_XboxOneInitState prev_state;
  961. do {
  962. prev_state = ctx->init_state;
  963. switch (ctx->init_state) {
  964. case XBOX_ONE_INIT_STATE_ANNOUNCED:
  965. if (XBOX_ONE_DRIVER_ACTIVE) {
  966. // The driver is taking care of identification
  967. SetInitState(ctx, XBOX_ONE_INIT_STATE_COMPLETE);
  968. } else {
  969. SendIdentificationRequest(ctx);
  970. SetInitState(ctx, XBOX_ONE_INIT_STATE_IDENTIFYING);
  971. }
  972. break;
  973. case XBOX_ONE_INIT_STATE_IDENTIFYING:
  974. if (SDL_GetTicks() >= (ctx->send_time + CONTROLLER_IDENTIFY_TIMEOUT_MS)) {
  975. // We haven't heard anything, let's move on
  976. #ifdef DEBUG_JOYSTICK
  977. SDL_Log("Identification request timed out after %llu ms", (SDL_GetTicks() - ctx->send_time));
  978. #endif
  979. SetInitState(ctx, XBOX_ONE_INIT_STATE_STARTUP);
  980. }
  981. break;
  982. case XBOX_ONE_INIT_STATE_STARTUP:
  983. if (XBOX_ONE_DRIVER_ACTIVE) {
  984. // The driver is taking care of startup
  985. SetInitState(ctx, XBOX_ONE_INIT_STATE_COMPLETE);
  986. } else {
  987. SendControllerStartup(ctx);
  988. SetInitState(ctx, XBOX_ONE_INIT_STATE_PREPARE_INPUT);
  989. }
  990. break;
  991. case XBOX_ONE_INIT_STATE_PREPARE_INPUT:
  992. if (SDL_GetTicks() >= (ctx->send_time + CONTROLLER_PREPARE_INPUT_TIMEOUT_MS)) {
  993. #ifdef DEBUG_JOYSTICK
  994. SDL_Log("Prepare input complete after %llu ms", (SDL_GetTicks() - ctx->send_time));
  995. #endif
  996. SetInitState(ctx, XBOX_ONE_INIT_STATE_COMPLETE);
  997. }
  998. break;
  999. case XBOX_ONE_INIT_STATE_COMPLETE:
  1000. break;
  1001. }
  1002. } while (ctx->init_state != prev_state);
  1003. return true;
  1004. }
  1005. /* GIP protocol handling adapted under the Zlib license with permission from @medusalix:
  1006. * https://github.com/medusalix/xone/blob/master/bus/protocol.h
  1007. * https://github.com/medusalix/xone/blob/master/bus/protocol.c
  1008. */
  1009. #define GIP_HEADER_MIN_LENGTH 3
  1010. // Internal commands
  1011. #define GIP_CMD_ACKNOWLEDGE 0x01
  1012. #define GIP_CMD_ANNOUNCE 0x02
  1013. #define GIP_CMD_STATUS 0x03
  1014. #define GIP_CMD_IDENTIFY 0x04
  1015. #define GIP_CMD_POWER 0x05
  1016. #define GIP_CMD_AUTHENTICATE 0x06
  1017. #define GIP_CMD_VIRTUAL_KEY 0x07
  1018. #define GIP_CMD_AUDIO_CONTROL 0x08
  1019. #define GIP_CMD_LED 0x0A
  1020. #define GIP_CMD_HID_REPORT 0x0B
  1021. #define GIP_CMD_FIRMWARE 0x0C
  1022. #define GIP_CMD_SERIAL_NUMBER 0x1E
  1023. #define GIP_CMD_AUDIO_SAMPLES 0x60
  1024. // External commands
  1025. #define GIP_CMD_RUMBLE 0x09
  1026. #define GIP_CMD_UNMAPPED_STATE 0x0C
  1027. #define GIP_CMD_INPUT 0x20
  1028. // Header option flags
  1029. #define GIP_OPT_ACKNOWLEDGE 0x10
  1030. #define GIP_OPT_INTERNAL 0x20
  1031. #define GIP_OPT_CHUNK_START 0x40
  1032. #define GIP_OPT_CHUNK 0x80
  1033. #pragma pack(push, 1)
  1034. struct gip_header {
  1035. Uint8 command;
  1036. Uint8 options;
  1037. Uint8 sequence;
  1038. Uint32 packet_length;
  1039. Uint32 chunk_offset;
  1040. };
  1041. struct gip_pkt_acknowledge {
  1042. Uint8 unknown;
  1043. Uint8 command;
  1044. Uint8 options;
  1045. Uint16 length;
  1046. Uint8 padding[2];
  1047. Uint16 remaining;
  1048. };
  1049. #pragma pack(pop)
  1050. static int EncodeVariableInt(Uint8 *buf, Uint32 val)
  1051. {
  1052. int i;
  1053. for (i = 0; i < sizeof(val); i++) {
  1054. buf[i] = (Uint8)val;
  1055. if (val > 0x7F) {
  1056. buf[i] |= 0x80;
  1057. }
  1058. val >>= 7;
  1059. if (!val) {
  1060. break;
  1061. }
  1062. }
  1063. return i + 1;
  1064. }
  1065. static int DecodeVariableInt(const Uint8 *data, int len, void *out)
  1066. {
  1067. int i;
  1068. Uint32 val = 0;
  1069. for (i = 0; i < sizeof(val) && i < len; i++) {
  1070. val |= (data[i] & 0x7F) << (i * 7);
  1071. if (!(data[i] & 0x80)) {
  1072. break;
  1073. }
  1074. }
  1075. SDL_memcpy(out, &val, sizeof(val));
  1076. return i + 1;
  1077. }
  1078. static int HIDAPI_GIP_GetActualHeaderLength(struct gip_header *hdr)
  1079. {
  1080. Uint32 pkt_len = hdr->packet_length;
  1081. Uint32 chunk_offset = hdr->chunk_offset;
  1082. int len = GIP_HEADER_MIN_LENGTH;
  1083. do {
  1084. len++;
  1085. pkt_len >>= 7;
  1086. } while (pkt_len);
  1087. if (hdr->options & GIP_OPT_CHUNK) {
  1088. while (chunk_offset) {
  1089. len++;
  1090. chunk_offset >>= 7;
  1091. }
  1092. }
  1093. return len;
  1094. }
  1095. static int HIDAPI_GIP_GetHeaderLength(struct gip_header *hdr)
  1096. {
  1097. int len = HIDAPI_GIP_GetActualHeaderLength(hdr);
  1098. // Header length must be even
  1099. return len + (len % 2);
  1100. }
  1101. static void HIDAPI_GIP_EncodeHeader(struct gip_header *hdr, Uint8 *buf)
  1102. {
  1103. int hdr_len = 0;
  1104. buf[hdr_len++] = hdr->command;
  1105. buf[hdr_len++] = hdr->options;
  1106. buf[hdr_len++] = hdr->sequence;
  1107. hdr_len += EncodeVariableInt(buf + hdr_len, hdr->packet_length);
  1108. // Header length must be even
  1109. if (HIDAPI_GIP_GetActualHeaderLength(hdr) % 2) {
  1110. buf[hdr_len - 1] |= 0x80;
  1111. buf[hdr_len++] = 0;
  1112. }
  1113. if (hdr->options & GIP_OPT_CHUNK) {
  1114. EncodeVariableInt(buf + hdr_len, hdr->chunk_offset);
  1115. }
  1116. }
  1117. static int HIDAPI_GIP_DecodeHeader(struct gip_header *hdr, const Uint8 *data, int len)
  1118. {
  1119. int hdr_len = 0;
  1120. hdr->command = data[hdr_len++];
  1121. hdr->options = data[hdr_len++];
  1122. hdr->sequence = data[hdr_len++];
  1123. hdr->packet_length = 0;
  1124. hdr->chunk_offset = 0;
  1125. hdr_len += DecodeVariableInt(data + hdr_len, len - hdr_len, &hdr->packet_length);
  1126. if (hdr->options & GIP_OPT_CHUNK) {
  1127. hdr_len += DecodeVariableInt(data + hdr_len, len - hdr_len, &hdr->chunk_offset);
  1128. }
  1129. return hdr_len;
  1130. }
  1131. static bool HIDAPI_GIP_SendPacket(SDL_DriverXboxOne_Context *ctx, struct gip_header *hdr, const void *data)
  1132. {
  1133. Uint8 packet[USB_PACKET_LENGTH];
  1134. int hdr_len, size;
  1135. hdr_len = HIDAPI_GIP_GetHeaderLength(hdr);
  1136. size = (hdr_len + hdr->packet_length);
  1137. if (size > sizeof(packet)) {
  1138. SDL_SetError("Couldn't send GIP packet, size (%d) too large", size);
  1139. return false;
  1140. }
  1141. if (!hdr->sequence) {
  1142. hdr->sequence = GetNextPacketSequence(ctx);
  1143. }
  1144. HIDAPI_GIP_EncodeHeader(hdr, packet);
  1145. if (data) {
  1146. SDL_memcpy(&packet[hdr_len], data, hdr->packet_length);
  1147. }
  1148. if (!SendProtocolPacket(ctx, packet, size)) {
  1149. SDL_SetError("Couldn't send protocol packet");
  1150. return false;
  1151. }
  1152. return true;
  1153. }
  1154. static bool HIDAPI_GIP_AcknowledgePacket(SDL_DriverXboxOne_Context *ctx, struct gip_header *ack)
  1155. {
  1156. if (XBOX_ONE_DRIVER_ACTIVE) {
  1157. // The driver is taking care of acks
  1158. return true;
  1159. } else {
  1160. struct gip_header hdr;
  1161. struct gip_pkt_acknowledge pkt;
  1162. SDL_zero(hdr);
  1163. hdr.command = GIP_CMD_ACKNOWLEDGE;
  1164. hdr.options = GIP_OPT_INTERNAL;
  1165. hdr.sequence = ack->sequence;
  1166. hdr.packet_length = sizeof(pkt);
  1167. SDL_zero(pkt);
  1168. pkt.command = ack->command;
  1169. pkt.options = GIP_OPT_INTERNAL;
  1170. pkt.length = SDL_Swap16LE((Uint16)(ack->chunk_offset + ack->packet_length));
  1171. if ((ack->options & GIP_OPT_CHUNK) && ctx->chunk_buffer) {
  1172. pkt.remaining = SDL_Swap16LE((Uint16)(ctx->chunk_length - pkt.length));
  1173. }
  1174. return HIDAPI_GIP_SendPacket(ctx, &hdr, &pkt);
  1175. }
  1176. }
  1177. static bool HIDAPI_GIP_DispatchPacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, struct gip_header *hdr, Uint8 *data, Uint32 size)
  1178. {
  1179. if ((hdr->options & 0x0F) != 0) {
  1180. // This is a packet for a device plugged into the controller, skip it
  1181. return true;
  1182. }
  1183. if (hdr->options & GIP_OPT_INTERNAL) {
  1184. switch (hdr->command) {
  1185. case GIP_CMD_ACKNOWLEDGE:
  1186. // Ignore this packet
  1187. break;
  1188. case GIP_CMD_ANNOUNCE:
  1189. // Controller is connected and waiting for initialization
  1190. /* The data bytes are:
  1191. 0x02 0x20 NN 0x1c, where NN is the packet sequence
  1192. then 6 bytes of wireless MAC address
  1193. then 2 bytes padding
  1194. then 16-bit VID
  1195. then 16-bit PID
  1196. then 16-bit firmware version quartet AA.BB.CC.DD
  1197. e.g. 0x05 0x00 0x05 0x00 0x51 0x0a 0x00 0x00
  1198. is firmware version 5.5.2641.0, and product version 0x0505 = 1285
  1199. then 8 bytes of unknown data
  1200. */
  1201. #ifdef DEBUG_JOYSTICK
  1202. SDL_Log("Controller announce after %llu ms", (SDL_GetTicks() - ctx->start_time));
  1203. #endif
  1204. SetInitState(ctx, XBOX_ONE_INIT_STATE_ANNOUNCED);
  1205. break;
  1206. case GIP_CMD_STATUS:
  1207. // Controller status update
  1208. HIDAPI_DriverXboxOne_HandleStatusPacket(ctx, data, size);
  1209. break;
  1210. case GIP_CMD_IDENTIFY:
  1211. #ifdef DEBUG_JOYSTICK
  1212. SDL_Log("Identification request completed after %llu ms", (SDL_GetTicks() - ctx->send_time));
  1213. #endif
  1214. #ifdef DEBUG_XBOX_PROTOCOL
  1215. HIDAPI_DumpPacket("Xbox One identification data: size = %d", data, size);
  1216. #endif
  1217. SetInitState(ctx, XBOX_ONE_INIT_STATE_STARTUP);
  1218. break;
  1219. case GIP_CMD_POWER:
  1220. // Ignore this packet
  1221. break;
  1222. case GIP_CMD_AUTHENTICATE:
  1223. // Ignore this packet
  1224. break;
  1225. case GIP_CMD_VIRTUAL_KEY:
  1226. if (!joystick) {
  1227. break;
  1228. }
  1229. HIDAPI_DriverXboxOne_HandleModePacket(joystick, ctx, data, size);
  1230. break;
  1231. case GIP_CMD_SERIAL_NUMBER:
  1232. /* If the packet starts with this:
  1233. 0x1E 0x30 0x00 0x10 0x04 0x00
  1234. then the next 14 bytes are the controller serial number
  1235. e.g. 0x30 0x39 0x37 0x31 0x32 0x33 0x33 0x32 0x33 0x35 0x34 0x30 0x33 0x36
  1236. is serial number "3039373132333332333534303336"
  1237. The controller sends that in response to this request:
  1238. 0x1E 0x20 0x00 0x01 0x04
  1239. */
  1240. HIDAPI_DriverXboxOne_HandleSerialIDPacket(ctx, data, size);
  1241. break;
  1242. default:
  1243. #ifdef DEBUG_JOYSTICK
  1244. SDL_Log("Unknown Xbox One packet: 0x%.2x", hdr->command);
  1245. #endif
  1246. break;
  1247. }
  1248. } else {
  1249. switch (hdr->command) {
  1250. case GIP_CMD_INPUT:
  1251. if (ctx->init_state < XBOX_ONE_INIT_STATE_COMPLETE) {
  1252. SetInitState(ctx, XBOX_ONE_INIT_STATE_COMPLETE);
  1253. // Ignore the first input, it may be spurious
  1254. #ifdef DEBUG_JOYSTICK
  1255. SDL_Log("Controller ignoring spurious input");
  1256. #endif
  1257. break;
  1258. }
  1259. if (!joystick) {
  1260. break;
  1261. }
  1262. HIDAPI_DriverXboxOne_HandleStatePacket(joystick, ctx, data, size);
  1263. break;
  1264. case GIP_CMD_UNMAPPED_STATE:
  1265. if (!joystick) {
  1266. break;
  1267. }
  1268. HIDAPI_DriverXboxOne_HandleUnmappedStatePacket(joystick, ctx, data, size);
  1269. break;
  1270. default:
  1271. #ifdef DEBUG_JOYSTICK
  1272. SDL_Log("Unknown Xbox One packet: 0x%.2x", hdr->command);
  1273. #endif
  1274. break;
  1275. }
  1276. }
  1277. return true;
  1278. }
  1279. static void HIDAPI_GIP_DestroyChunkBuffer(SDL_DriverXboxOne_Context *ctx)
  1280. {
  1281. if (ctx->chunk_buffer) {
  1282. SDL_free(ctx->chunk_buffer);
  1283. ctx->chunk_buffer = NULL;
  1284. ctx->chunk_length = 0;
  1285. }
  1286. }
  1287. static bool HIDAPI_GIP_CreateChunkBuffer(SDL_DriverXboxOne_Context *ctx, Uint32 size)
  1288. {
  1289. HIDAPI_GIP_DestroyChunkBuffer(ctx);
  1290. ctx->chunk_buffer = (Uint8 *)SDL_malloc(size);
  1291. if (ctx->chunk_buffer) {
  1292. ctx->chunk_length = size;
  1293. return true;
  1294. } else {
  1295. return false;
  1296. }
  1297. }
  1298. static bool HIDAPI_GIP_ProcessPacketChunked(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, struct gip_header *hdr, Uint8 *data)
  1299. {
  1300. bool result;
  1301. if (!ctx->chunk_buffer) {
  1302. return false;
  1303. }
  1304. if ((hdr->chunk_offset + hdr->packet_length) > ctx->chunk_length) {
  1305. return false;
  1306. }
  1307. if (hdr->packet_length) {
  1308. SDL_memcpy(ctx->chunk_buffer + hdr->chunk_offset, data, hdr->packet_length);
  1309. return true;
  1310. }
  1311. result = HIDAPI_GIP_DispatchPacket(joystick, ctx, hdr, ctx->chunk_buffer, ctx->chunk_length);
  1312. HIDAPI_GIP_DestroyChunkBuffer(ctx);
  1313. return result;
  1314. }
  1315. static bool HIDAPI_GIP_ProcessPacket(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, struct gip_header *hdr, Uint8 *data)
  1316. {
  1317. if (hdr->options & GIP_OPT_CHUNK_START) {
  1318. if (!HIDAPI_GIP_CreateChunkBuffer(ctx, hdr->chunk_offset)) {
  1319. return false;
  1320. }
  1321. ctx->chunk_length = hdr->chunk_offset;
  1322. hdr->chunk_offset = 0;
  1323. }
  1324. if (hdr->options & GIP_OPT_ACKNOWLEDGE) {
  1325. if (!HIDAPI_GIP_AcknowledgePacket(ctx, hdr)) {
  1326. return false;
  1327. }
  1328. }
  1329. if (hdr->options & GIP_OPT_CHUNK) {
  1330. return HIDAPI_GIP_ProcessPacketChunked(joystick, ctx, hdr, data);
  1331. } else {
  1332. return HIDAPI_GIP_DispatchPacket(joystick, ctx, hdr, data, hdr->packet_length);
  1333. }
  1334. }
  1335. static bool HIDAPI_GIP_ProcessData(SDL_Joystick *joystick, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  1336. {
  1337. struct gip_header hdr;
  1338. int hdr_len;
  1339. while (size > GIP_HEADER_MIN_LENGTH) {
  1340. hdr_len = HIDAPI_GIP_DecodeHeader(&hdr, data, size);
  1341. if ((hdr_len + hdr.packet_length) > (Uint32)size) {
  1342. // On macOS we get a shortened version of the real report
  1343. hdr.packet_length = (Uint32)(size - hdr_len);
  1344. }
  1345. if (!HIDAPI_GIP_ProcessPacket(joystick, ctx, &hdr, data + hdr_len)) {
  1346. return false;
  1347. }
  1348. data += hdr_len + hdr.packet_length;
  1349. size -= hdr_len + hdr.packet_length;
  1350. }
  1351. return true;
  1352. }
  1353. static bool HIDAPI_DriverXboxOne_UpdateDevice(SDL_HIDAPI_Device *device)
  1354. {
  1355. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  1356. SDL_Joystick *joystick = NULL;
  1357. Uint8 data[USB_PACKET_LENGTH];
  1358. int size;
  1359. if (device->num_joysticks > 0) {
  1360. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  1361. } else {
  1362. return false;
  1363. }
  1364. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  1365. #ifdef DEBUG_XBOX_PROTOCOL
  1366. HIDAPI_DumpPacket("Xbox One packet: size = %d", data, size);
  1367. #endif
  1368. if (device->is_bluetooth) {
  1369. switch (data[0]) {
  1370. case 0x01:
  1371. if (!joystick) {
  1372. break;
  1373. }
  1374. if (size >= 16) {
  1375. HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(joystick, ctx, data, size);
  1376. } else {
  1377. #ifdef DEBUG_JOYSTICK
  1378. SDL_Log("Unknown Xbox One Bluetooth packet size: %d", size);
  1379. #endif
  1380. }
  1381. break;
  1382. case 0x02:
  1383. if (!joystick) {
  1384. break;
  1385. }
  1386. HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(joystick, ctx, data, size);
  1387. break;
  1388. case 0x04:
  1389. if (!joystick) {
  1390. break;
  1391. }
  1392. HIDAPI_DriverXboxOneBluetooth_HandleBatteryPacket(joystick, ctx, data, size);
  1393. break;
  1394. default:
  1395. #ifdef DEBUG_JOYSTICK
  1396. SDL_Log("Unknown Xbox One packet: 0x%.2x", data[0]);
  1397. #endif
  1398. break;
  1399. }
  1400. } else {
  1401. HIDAPI_GIP_ProcessData(joystick, ctx, data, size);
  1402. }
  1403. }
  1404. HIDAPI_DriverXboxOne_UpdateInitState(ctx);
  1405. HIDAPI_DriverXboxOne_UpdateRumble(ctx);
  1406. if (size < 0) {
  1407. // Read error, device is disconnected
  1408. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  1409. }
  1410. return (size >= 0);
  1411. }
  1412. static void HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1413. {
  1414. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  1415. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE_HOME_LED,
  1416. SDL_HomeLEDHintChanged, ctx);
  1417. }
  1418. static void HIDAPI_DriverXboxOne_FreeDevice(SDL_HIDAPI_Device *device)
  1419. {
  1420. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  1421. HIDAPI_GIP_DestroyChunkBuffer(ctx);
  1422. }
  1423. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne = {
  1424. SDL_HINT_JOYSTICK_HIDAPI_XBOX_ONE,
  1425. true,
  1426. HIDAPI_DriverXboxOne_RegisterHints,
  1427. HIDAPI_DriverXboxOne_UnregisterHints,
  1428. HIDAPI_DriverXboxOne_IsEnabled,
  1429. HIDAPI_DriverXboxOne_IsSupportedDevice,
  1430. HIDAPI_DriverXboxOne_InitDevice,
  1431. HIDAPI_DriverXboxOne_GetDevicePlayerIndex,
  1432. HIDAPI_DriverXboxOne_SetDevicePlayerIndex,
  1433. HIDAPI_DriverXboxOne_UpdateDevice,
  1434. HIDAPI_DriverXboxOne_OpenJoystick,
  1435. HIDAPI_DriverXboxOne_RumbleJoystick,
  1436. HIDAPI_DriverXboxOne_RumbleJoystickTriggers,
  1437. HIDAPI_DriverXboxOne_GetJoystickCapabilities,
  1438. HIDAPI_DriverXboxOne_SetJoystickLED,
  1439. HIDAPI_DriverXboxOne_SendJoystickEffect,
  1440. HIDAPI_DriverXboxOne_SetJoystickSensorsEnabled,
  1441. HIDAPI_DriverXboxOne_CloseJoystick,
  1442. HIDAPI_DriverXboxOne_FreeDevice,
  1443. };
  1444. #endif // SDL_JOYSTICK_HIDAPI_XBOXONE
  1445. #endif // SDL_JOYSTICK_HIDAPI