SDL_hidapi_xboxone.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "SDL_hints.h"
  21. #include "SDL_log.h"
  22. #include "SDL_events.h"
  23. #include "SDL_timer.h"
  24. #include "SDL_joystick.h"
  25. #include "SDL_gamecontroller.h"
  26. #include "../SDL_sysjoystick.h"
  27. #include "SDL_hidapijoystick_c.h"
  28. #include "SDL_hidapi_rumble.h"
  29. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  30. /* Define this if you want to log all packets from the controller */
  31. /*#define DEBUG_XBOX_PROTOCOL*/
  32. /* The amount of time to wait after hotplug to send controller init sequence */
  33. #define CONTROLLER_INIT_DELAY_MS 1500 /* 475 for Xbox One S, 1275 for the PDP Battlefield 1 */
  34. /* Connect controller */
  35. static const Uint8 xboxone_init0[] = {
  36. 0x04, 0x20, 0x00, 0x00
  37. };
  38. /* Initial ack */
  39. static const Uint8 xboxone_init1[] = {
  40. 0x01, 0x20, 0x01, 0x09, 0x00, 0x04, 0x20, 0x3a,
  41. 0x00, 0x00, 0x00, 0x80, 0x00
  42. };
  43. /* Start controller - extended? */
  44. static const Uint8 xboxone_init2[] = {
  45. 0x05, 0x20, 0x00, 0x0F, 0x06, 0x00, 0x00, 0x00,
  46. 0x00, 0x00, 0x00, 0x55, 0x53, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00
  48. };
  49. /* Start controller with input */
  50. static const Uint8 xboxone_init3[] = {
  51. 0x05, 0x20, 0x03, 0x01, 0x00
  52. };
  53. /* Enable LED */
  54. static const Uint8 xboxone_init4[] = {
  55. 0x0A, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
  56. };
  57. /* Start input reports? */
  58. static const Uint8 xboxone_init5[] = {
  59. 0x06, 0x20, 0x00, 0x02, 0x01, 0x00
  60. };
  61. /* Start rumble? */
  62. static const Uint8 xboxone_init6[] = {
  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. Uint16 vendor_id;
  74. Uint16 product_id;
  75. Uint16 exclude_vendor_id;
  76. Uint16 exclude_product_id;
  77. const Uint8 *data;
  78. int size;
  79. const Uint8 response[2];
  80. } SDL_DriverXboxOne_InitPacket;
  81. static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
  82. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init0, sizeof(xboxone_init0), { 0x04, 0xf0 } },
  83. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init1, sizeof(xboxone_init1), { 0x04, 0xb0 } },
  84. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init2, sizeof(xboxone_init2), { 0x00, 0x00 } },
  85. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init3, sizeof(xboxone_init3), { 0x00, 0x00 } },
  86. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init4, sizeof(xboxone_init4), { 0x00, 0x00 } },
  87. /* These next packets are required for third party controllers (PowerA, PDP, HORI),
  88. but aren't the correct protocol for Microsoft Xbox controllers.
  89. */
  90. { 0x0000, 0x0000, 0x045e, 0x0000, xboxone_init5, sizeof(xboxone_init5), { 0x00, 0x00 } },
  91. { 0x0000, 0x0000, 0x045e, 0x0000, xboxone_init6, sizeof(xboxone_init6), { 0x00, 0x00 } },
  92. };
  93. typedef struct {
  94. Uint16 vendor_id;
  95. Uint16 product_id;
  96. SDL_bool bluetooth;
  97. SDL_bool initialized;
  98. Uint32 start_time;
  99. Uint8 sequence;
  100. Uint8 last_state[USB_PACKET_LENGTH];
  101. SDL_bool has_paddles;
  102. } SDL_DriverXboxOne_Context;
  103. #define DEBUG_XBOX_PROTOCOL
  104. #ifdef DEBUG_XBOX_PROTOCOL
  105. static void
  106. DumpPacket(const char *prefix, Uint8 *data, int size)
  107. {
  108. int i;
  109. char buffer[5*USB_PACKET_LENGTH];
  110. SDL_snprintf(buffer, sizeof(buffer), prefix, size);
  111. for (i = 0; i < size; ++i) {
  112. if ((i % 8) == 0) {
  113. SDL_snprintf(&buffer[SDL_strlen(buffer)], sizeof(buffer) - SDL_strlen(buffer), "\n%.2d: ", i);
  114. }
  115. SDL_snprintf(&buffer[SDL_strlen(buffer)], sizeof(buffer) - SDL_strlen(buffer), " 0x%.2x", data[i]);
  116. }
  117. SDL_strlcat(buffer, "\n", sizeof(buffer));
  118. SDL_Log("%s", buffer);
  119. }
  120. #endif /* DEBUG_XBOX_PROTOCOL */
  121. static SDL_bool
  122. IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
  123. {
  124. /* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
  125. if (vendor_id == USB_VENDOR_MICROSOFT) {
  126. if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
  127. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
  128. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
  129. return SDL_TRUE;
  130. }
  131. }
  132. return SDL_FALSE;
  133. }
  134. static SDL_bool
  135. ControllerHasPaddles(Uint16 vendor_id, Uint16 product_id)
  136. {
  137. if (vendor_id == USB_VENDOR_MICROSOFT) {
  138. if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 ||
  139. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2) {
  140. return SDL_TRUE;
  141. }
  142. }
  143. return SDL_FALSE;
  144. }
  145. /* Return true if this controller sends the 0x02 "waiting for init" packet */
  146. static SDL_bool
  147. ControllerSendsWaitingForInit(Uint16 vendor_id, Uint16 product_id)
  148. {
  149. if (vendor_id == USB_VENDOR_HYPERKIN) {
  150. /* The Hyperkin controllers always send 0x02 when waiting for init,
  151. and the Hyperkin Duke plays an Xbox startup animation, so we want
  152. to make sure we don't send the init sequence if it isn't needed.
  153. */
  154. return SDL_TRUE;
  155. }
  156. if (vendor_id == USB_VENDOR_PDP) {
  157. /* The PDP Rock Candy (PID 0x0246) doesn't send 0x02 on Linux for some reason */
  158. return SDL_FALSE;
  159. }
  160. /* It doesn't hurt to reinit, especially if a driver has misconfigured the controller */
  161. /*return SDL_TRUE;*/
  162. return SDL_FALSE;
  163. }
  164. static SDL_bool
  165. SendControllerInit(SDL_HIDAPI_Device *device, SDL_DriverXboxOne_Context *ctx)
  166. {
  167. Uint16 vendor_id = ctx->vendor_id;
  168. Uint16 product_id = ctx->product_id;
  169. int i;
  170. Uint8 init_packet[USB_PACKET_LENGTH];
  171. for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
  172. const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[i];
  173. if (packet->vendor_id && (vendor_id != packet->vendor_id)) {
  174. continue;
  175. }
  176. if (packet->product_id && (product_id != packet->product_id)) {
  177. continue;
  178. }
  179. if (packet->exclude_vendor_id && (vendor_id == packet->exclude_vendor_id)) {
  180. continue;
  181. }
  182. if (packet->exclude_product_id && (product_id == packet->exclude_product_id)) {
  183. continue;
  184. }
  185. SDL_memcpy(init_packet, packet->data, packet->size);
  186. if (init_packet[0] != 0x01) {
  187. init_packet[2] = ctx->sequence++;
  188. }
  189. if (hid_write(device->dev, init_packet, packet->size) != packet->size) {
  190. SDL_SetError("Couldn't write Xbox One initialization packet");
  191. return SDL_FALSE;
  192. }
  193. if (packet->response[0]) {
  194. const Uint32 RESPONSE_TIMEOUT_MS = 50;
  195. Uint32 start = SDL_GetTicks();
  196. SDL_bool got_response = SDL_FALSE;
  197. while (!got_response && !SDL_TICKS_PASSED(SDL_GetTicks(), start + RESPONSE_TIMEOUT_MS)) {
  198. Uint8 data[USB_PACKET_LENGTH];
  199. int size;
  200. while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  201. #ifdef DEBUG_XBOX_PROTOCOL
  202. DumpPacket("Xbox One INIT packet: size = %d", data, size);
  203. #endif
  204. if (size >= 2 && data[0] == packet->response[0] && data[1] == packet->response[1]) {
  205. got_response = SDL_TRUE;
  206. }
  207. }
  208. }
  209. #ifdef DEBUG_XBOX_PROTOCOL
  210. SDL_Log("Init sequence %d got response: %s\n", i, got_response ? "TRUE" : "FALSE");
  211. #endif
  212. }
  213. }
  214. return SDL_TRUE;
  215. }
  216. static SDL_bool
  217. HIDAPI_DriverXboxOne_IsSupportedDevice(const char *name, SDL_GameControllerType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  218. {
  219. #ifdef __LINUX__
  220. if (vendor_id == USB_VENDOR_POWERA && product_id == 0x541a) {
  221. /* The PowerA Mini controller, model 1240245-01, blocks while writing feature reports */
  222. return SDL_FALSE;
  223. }
  224. #endif
  225. #ifdef __MACOSX__
  226. /* Wired Xbox One controllers are handled by the 360Controller driver */
  227. if (!IsBluetoothXboxOneController(vendor_id, product_id)) {
  228. return SDL_FALSE;
  229. }
  230. #endif
  231. return (type == SDL_CONTROLLER_TYPE_XBOXONE);
  232. }
  233. static const char *
  234. HIDAPI_DriverXboxOne_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  235. {
  236. return NULL;
  237. }
  238. static SDL_bool
  239. HIDAPI_DriverXboxOne_InitDevice(SDL_HIDAPI_Device *device)
  240. {
  241. return HIDAPI_JoystickConnected(device, NULL);
  242. }
  243. static int
  244. HIDAPI_DriverXboxOne_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  245. {
  246. return -1;
  247. }
  248. static void
  249. HIDAPI_DriverXboxOne_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  250. {
  251. }
  252. static SDL_bool
  253. HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  254. {
  255. SDL_DriverXboxOne_Context *ctx;
  256. ctx = (SDL_DriverXboxOne_Context *)SDL_calloc(1, sizeof(*ctx));
  257. if (!ctx) {
  258. SDL_OutOfMemory();
  259. return SDL_FALSE;
  260. }
  261. device->dev = hid_open_path(device->path, 0);
  262. if (!device->dev) {
  263. SDL_free(ctx);
  264. SDL_SetError("Couldn't open %s", device->path);
  265. return SDL_FALSE;
  266. }
  267. device->context = ctx;
  268. ctx->vendor_id = device->vendor_id;
  269. ctx->product_id = device->product_id;
  270. ctx->bluetooth = IsBluetoothXboxOneController(device->vendor_id, device->product_id);
  271. ctx->initialized = ctx->bluetooth ? SDL_TRUE : SDL_FALSE;
  272. ctx->start_time = SDL_GetTicks();
  273. ctx->sequence = 1;
  274. ctx->has_paddles = ControllerHasPaddles(ctx->vendor_id, ctx->product_id);
  275. /* Initialize the joystick capabilities */
  276. joystick->nbuttons = ctx->has_paddles ? SDL_CONTROLLER_BUTTON_MAX : (SDL_CONTROLLER_BUTTON_MAX + 4);
  277. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  278. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  279. return SDL_TRUE;
  280. }
  281. static int
  282. HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  283. {
  284. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  285. if (ctx->bluetooth) {
  286. Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
  287. rumble_packet[4] = (low_frequency_rumble >> 8);
  288. rumble_packet[5] = (high_frequency_rumble >> 8);
  289. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  290. return SDL_SetError("Couldn't send rumble packet");
  291. }
  292. } else {
  293. Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF };
  294. /* Magnitude is 1..100 so scale the 16-bit input here */
  295. rumble_packet[8] = low_frequency_rumble / 655;
  296. rumble_packet[9] = high_frequency_rumble / 655;
  297. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  298. return SDL_SetError("Couldn't send rumble packet");
  299. }
  300. }
  301. return 0;
  302. }
  303. static void
  304. HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  305. {
  306. Sint16 axis;
  307. if (ctx->last_state[4] != data[4]) {
  308. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[4] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  309. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[4] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  310. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[4] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  311. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[4] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  312. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[4] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  313. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[4] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  314. }
  315. if (ctx->last_state[5] != data[5]) {
  316. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[5] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  317. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[5] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  318. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[5] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  319. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[5] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  320. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  321. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  322. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[5] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  323. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[5] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  324. }
  325. /* Xbox One S report is 18 bytes
  326. Xbox One Elite Series 1 report is 33 bytes, paddles in data[32], mode in data[32] & 0x10, both modes have mapped paddles by default
  327. Paddle bits:
  328. UL: 0x01 (A) UR: 0x02 (B)
  329. LL: 0x04 (X) LR: 0x08 (Y)
  330. Xbox One Elite Series 2 report is 38 bytes, paddles in data[18], mode in data[19], mode 0 has no mapped paddles by default
  331. Paddle bits:
  332. UL: 0x04 (A) UR: 0x01 (B)
  333. LL: 0x08 (X) LR: 0x02 (Y)
  334. */
  335. if (ctx->has_paddles && (size == 33 || size == 38)) {
  336. int paddle_index;
  337. int button1_bit;
  338. int button2_bit;
  339. int button3_bit;
  340. int button4_bit;
  341. SDL_bool paddles_mapped;
  342. if (size == 33) {
  343. /* XBox One Elite Series 1 */
  344. paddle_index = 32;
  345. button1_bit = 0x01;
  346. button2_bit = 0x02;
  347. button3_bit = 0x04;
  348. button4_bit = 0x08;
  349. /* The mapped controller state is at offset 4, the raw state is at offset 18, compare them to see if the paddles are mapped */
  350. paddles_mapped = (SDL_memcmp(&data[4], &data[18], 14) != 0);
  351. } else /* if (size == 38) */ {
  352. /* XBox One Elite Series 2 */
  353. paddle_index = 18;
  354. button1_bit = 0x04;
  355. button2_bit = 0x01;
  356. button3_bit = 0x08;
  357. button4_bit = 0x02;
  358. paddles_mapped = (data[19] != 0);
  359. }
  360. #ifdef DEBUG_XBOX_PROTOCOL
  361. SDL_Log(">>> Paddles: %d,%d,%d,%d mapped = %s\n",
  362. (data[paddle_index] & button1_bit) ? 1 : 0,
  363. (data[paddle_index] & button2_bit) ? 1 : 0,
  364. (data[paddle_index] & button3_bit) ? 1 : 0,
  365. (data[paddle_index] & button4_bit) ? 1 : 0,
  366. paddles_mapped ? "TRUE" : "FALSE"
  367. );
  368. #endif
  369. if (paddles_mapped) {
  370. /* Respect that the paddles are being used for other controls and don't pass them on to the app */
  371. data[paddle_index] = 0;
  372. }
  373. if (ctx->last_state[paddle_index] != data[paddle_index]) {
  374. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+0, (data[paddle_index] & button1_bit) ? SDL_PRESSED : SDL_RELEASED);
  375. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+1, (data[paddle_index] & button2_bit) ? SDL_PRESSED : SDL_RELEASED);
  376. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+2, (data[paddle_index] & button3_bit) ? SDL_PRESSED : SDL_RELEASED);
  377. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+3, (data[paddle_index] & button4_bit) ? SDL_PRESSED : SDL_RELEASED);
  378. }
  379. }
  380. axis = ((int)*(Sint16*)(&data[6]) * 64) - 32768;
  381. if (axis == 32704) {
  382. axis = 32767;
  383. }
  384. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  385. axis = ((int)*(Sint16*)(&data[8]) * 64) - 32768;
  386. if (axis == 32704) {
  387. axis = 32767;
  388. }
  389. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  390. axis = *(Sint16*)(&data[10]);
  391. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  392. axis = *(Sint16*)(&data[12]);
  393. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, ~axis);
  394. axis = *(Sint16*)(&data[14]);
  395. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  396. axis = *(Sint16*)(&data[16]);
  397. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, ~axis);
  398. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  399. }
  400. static void
  401. HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  402. {
  403. if (data[1] == 0x30) {
  404. /* The Xbox One S controller needs acks for mode reports */
  405. const Uint8 seqnum = data[2];
  406. const Uint8 ack[] = { 0x01, 0x20, seqnum, 0x09, 0x00, 0x07, 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
  407. hid_write(dev, ack, sizeof(ack));
  408. }
  409. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  410. }
  411. static void
  412. HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  413. {
  414. Sint16 axis;
  415. if (ctx->last_state[14] != data[14]) {
  416. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  417. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[14] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  418. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[14] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  419. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[14] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  420. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[14] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  421. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[14] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  422. }
  423. if (ctx->last_state[15] != data[15]) {
  424. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[15] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  425. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[15] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  426. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[15] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  427. }
  428. if (ctx->last_state[16] != data[16]) {
  429. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[16] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  430. }
  431. if (ctx->last_state[13] != data[13]) {
  432. SDL_bool dpad_up = SDL_FALSE;
  433. SDL_bool dpad_down = SDL_FALSE;
  434. SDL_bool dpad_left = SDL_FALSE;
  435. SDL_bool dpad_right = SDL_FALSE;
  436. switch (data[13]) {
  437. case 1:
  438. dpad_up = SDL_TRUE;
  439. break;
  440. case 2:
  441. dpad_up = SDL_TRUE;
  442. dpad_right = SDL_TRUE;
  443. break;
  444. case 3:
  445. dpad_right = SDL_TRUE;
  446. break;
  447. case 4:
  448. dpad_right = SDL_TRUE;
  449. dpad_down = SDL_TRUE;
  450. break;
  451. case 5:
  452. dpad_down = SDL_TRUE;
  453. break;
  454. case 6:
  455. dpad_left = SDL_TRUE;
  456. dpad_down = SDL_TRUE;
  457. break;
  458. case 7:
  459. dpad_left = SDL_TRUE;
  460. break;
  461. case 8:
  462. dpad_up = SDL_TRUE;
  463. dpad_left = SDL_TRUE;
  464. break;
  465. default:
  466. break;
  467. }
  468. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  469. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  470. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  471. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  472. }
  473. axis = (int)*(Uint16*)(&data[1]) - 0x8000;
  474. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  475. axis = (int)*(Uint16*)(&data[3]) - 0x8000;
  476. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  477. axis = (int)*(Uint16*)(&data[5]) - 0x8000;
  478. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  479. axis = (int)*(Uint16*)(&data[7]) - 0x8000;
  480. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  481. axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
  482. if (axis == 32704) {
  483. axis = 32767;
  484. }
  485. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  486. axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
  487. if (axis == 32704) {
  488. axis = 32767;
  489. }
  490. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  491. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  492. }
  493. static void
  494. HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  495. {
  496. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  497. }
  498. static SDL_bool
  499. HIDAPI_DriverXboxOne_UpdateDevice(SDL_HIDAPI_Device *device)
  500. {
  501. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  502. SDL_Joystick *joystick = NULL;
  503. Uint8 data[USB_PACKET_LENGTH];
  504. int size;
  505. if (device->num_joysticks > 0) {
  506. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  507. }
  508. if (!joystick) {
  509. return SDL_FALSE;
  510. }
  511. if (!ctx->initialized &&
  512. !ControllerSendsWaitingForInit(device->vendor_id, device->product_id)) {
  513. if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->start_time + CONTROLLER_INIT_DELAY_MS)) {
  514. if (!SendControllerInit(device, ctx)) {
  515. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  516. return SDL_FALSE;
  517. }
  518. ctx->initialized = SDL_TRUE;
  519. }
  520. }
  521. while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  522. #ifdef DEBUG_XBOX_PROTOCOL
  523. DumpPacket("Xbox One packet: size = %d", data, size);
  524. #endif
  525. if (ctx->bluetooth) {
  526. switch (data[0]) {
  527. case 0x01:
  528. HIDAPI_DriverXboxOneBluetooth_HandleStatePacket(joystick, device->dev, ctx, data, size);
  529. break;
  530. case 0x02:
  531. HIDAPI_DriverXboxOneBluetooth_HandleGuidePacket(joystick, device->dev, ctx, data, size);
  532. break;
  533. default:
  534. #ifdef DEBUG_JOYSTICK
  535. SDL_Log("Unknown Xbox One packet: 0x%.2x\n", data[0]);
  536. #endif
  537. break;
  538. }
  539. } else {
  540. switch (data[0]) {
  541. case 0x02:
  542. /* Controller is connected and waiting for initialization */
  543. if (!ctx->initialized) {
  544. #ifdef DEBUG_XBOX_PROTOCOL
  545. SDL_Log("Delay after init: %ums\n", SDL_GetTicks() - ctx->start_time);
  546. #endif
  547. if (!SendControllerInit(device, ctx)) {
  548. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  549. return SDL_FALSE;
  550. }
  551. ctx->initialized = SDL_TRUE;
  552. }
  553. break;
  554. case 0x03:
  555. /* Controller heartbeat */
  556. break;
  557. case 0x20:
  558. HIDAPI_DriverXboxOne_HandleStatePacket(joystick, device->dev, ctx, data, size);
  559. break;
  560. case 0x07:
  561. HIDAPI_DriverXboxOne_HandleModePacket(joystick, device->dev, ctx, data, size);
  562. break;
  563. default:
  564. #ifdef DEBUG_JOYSTICK
  565. SDL_Log("Unknown Xbox One packet: 0x%.2x\n", data[0]);
  566. #endif
  567. break;
  568. }
  569. }
  570. }
  571. if (size < 0) {
  572. /* Read error, device is disconnected */
  573. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  574. }
  575. return (size >= 0);
  576. }
  577. static void
  578. HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  579. {
  580. hid_close(device->dev);
  581. device->dev = NULL;
  582. SDL_free(device->context);
  583. device->context = NULL;
  584. }
  585. static void
  586. HIDAPI_DriverXboxOne_FreeDevice(SDL_HIDAPI_Device *device)
  587. {
  588. }
  589. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne =
  590. {
  591. SDL_HINT_JOYSTICK_HIDAPI_XBOX,
  592. SDL_TRUE,
  593. HIDAPI_DriverXboxOne_IsSupportedDevice,
  594. HIDAPI_DriverXboxOne_GetDeviceName,
  595. HIDAPI_DriverXboxOne_InitDevice,
  596. HIDAPI_DriverXboxOne_GetDevicePlayerIndex,
  597. HIDAPI_DriverXboxOne_SetDevicePlayerIndex,
  598. HIDAPI_DriverXboxOne_UpdateDevice,
  599. HIDAPI_DriverXboxOne_OpenJoystick,
  600. HIDAPI_DriverXboxOne_RumbleJoystick,
  601. HIDAPI_DriverXboxOne_CloseJoystick,
  602. HIDAPI_DriverXboxOne_FreeDevice
  603. };
  604. #endif /* SDL_JOYSTICK_HIDAPI_XBOXONE */
  605. #endif /* SDL_JOYSTICK_HIDAPI */
  606. /* vi: set ts=4 sw=4 expandtab: */