SDL_hidapi_xboxone.c 70 KB

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