SDL_hidapi_xboxone.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
  29. /* Define this if you want to log all packets from the controller */
  30. /*#define DEBUG_XBOX_PROTOCOL*/
  31. #define USB_PACKET_LENGTH 64
  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. static const Uint8 xboxone_rumble_reset[] = {
  67. 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00
  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. Uint16 vendor_id;
  78. Uint16 product_id;
  79. Uint16 exclude_vendor_id;
  80. Uint16 exclude_product_id;
  81. const Uint8 *data;
  82. int size;
  83. const Uint8 response[2];
  84. } SDL_DriverXboxOne_InitPacket;
  85. static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
  86. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init0, sizeof(xboxone_init0), { 0x04, 0xf0 } },
  87. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init1, sizeof(xboxone_init1), { 0x04, 0xb0 } },
  88. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init2, sizeof(xboxone_init2), { 0x00, 0x00 } },
  89. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init3, sizeof(xboxone_init3), { 0x00, 0x00 } },
  90. { 0x0000, 0x0000, 0x0000, 0x0000, xboxone_init4, sizeof(xboxone_init4), { 0x00, 0x00 } },
  91. /* These next packets are required for third party controllers (PowerA, PDP, HORI),
  92. but aren't the correct protocol for Microsoft Xbox controllers.
  93. */
  94. { 0x0000, 0x0000, 0x045e, 0x0000, xboxone_init5, sizeof(xboxone_init5), { 0x00, 0x00 } },
  95. { 0x0000, 0x0000, 0x045e, 0x0000, xboxone_init6, sizeof(xboxone_init6), { 0x00, 0x00 } },
  96. };
  97. typedef struct {
  98. Uint16 vendor_id;
  99. Uint16 product_id;
  100. Uint32 start_time;
  101. SDL_bool initialized;
  102. Uint8 sequence;
  103. Uint8 last_state[USB_PACKET_LENGTH];
  104. SDL_bool rumble_synchronized;
  105. Uint32 rumble_expiration;
  106. SDL_bool has_paddles;
  107. } SDL_DriverXboxOne_Context;
  108. #ifdef DEBUG_XBOX_PROTOCOL
  109. static void
  110. DumpPacket(const char *prefix, Uint8 *data, int size)
  111. {
  112. int i;
  113. char buffer[5*USB_PACKET_LENGTH];
  114. SDL_snprintf(buffer, sizeof(buffer), prefix, size);
  115. for (i = 0; i < size; ++i) {
  116. if ((i % 8) == 0) {
  117. SDL_snprintf(&buffer[SDL_strlen(buffer)], sizeof(buffer) - SDL_strlen(buffer), "\n%.2d: ", i);
  118. }
  119. SDL_snprintf(&buffer[SDL_strlen(buffer)], sizeof(buffer) - SDL_strlen(buffer), " 0x%.2x", data[i]);
  120. }
  121. SDL_strlcat(buffer, "\n", sizeof(buffer));
  122. SDL_Log("%s", buffer);
  123. }
  124. #endif /* DEBUG_XBOX_PROTOCOL */
  125. static SDL_bool
  126. IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
  127. {
  128. /* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
  129. if (vendor_id == USB_VENDOR_MICROSOFT) {
  130. if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
  131. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
  132. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
  133. return SDL_TRUE;
  134. }
  135. }
  136. return SDL_FALSE;
  137. }
  138. static SDL_bool
  139. ControllerHasPaddles(Uint16 vendor_id, Uint16 product_id)
  140. {
  141. if (vendor_id == USB_VENDOR_MICROSOFT) {
  142. if (product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_1 ||
  143. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2) {
  144. return SDL_TRUE;
  145. }
  146. }
  147. return SDL_FALSE;
  148. }
  149. static SDL_bool
  150. ControllerNeedsRumbleSequenceSynchronized(Uint16 vendor_id, Uint16 product_id)
  151. {
  152. if (vendor_id == USB_VENDOR_MICROSOFT) {
  153. /* All Xbox One controllers, from model 1537 through Elite Series 2, appear to need this */
  154. return SDL_TRUE;
  155. }
  156. return SDL_FALSE;
  157. }
  158. static SDL_bool
  159. SynchronizeRumbleSequence(hid_device *dev, SDL_DriverXboxOne_Context *ctx)
  160. {
  161. Uint16 vendor_id = ctx->vendor_id;
  162. Uint16 product_id = ctx->product_id;
  163. if (ctx->rumble_synchronized) {
  164. return SDL_TRUE;
  165. }
  166. if (ControllerNeedsRumbleSequenceSynchronized(vendor_id, product_id)) {
  167. int i;
  168. Uint8 init_packet[USB_PACKET_LENGTH];
  169. SDL_memcpy(init_packet, xboxone_rumble_reset, sizeof(xboxone_rumble_reset));
  170. for (i = 0; i < 255; ++i) {
  171. init_packet[2] = ((ctx->sequence + i) % 255);
  172. if (hid_write(dev, init_packet, sizeof(xboxone_rumble_reset)) != sizeof(xboxone_rumble_reset)) {
  173. SDL_SetError("Couldn't write Xbox One initialization packet");
  174. return SDL_FALSE;
  175. }
  176. }
  177. }
  178. ctx->rumble_synchronized = SDL_TRUE;
  179. return SDL_TRUE;
  180. }
  181. /* Return true if this controller sends the 0x02 "waiting for init" packet */
  182. static SDL_bool
  183. ControllerSendsWaitingForInit(Uint16 vendor_id, Uint16 product_id)
  184. {
  185. if (vendor_id == USB_VENDOR_HYPERKIN) {
  186. /* The Hyperkin controllers always send 0x02 when waiting for init,
  187. and the Hyperkin Duke plays an Xbox startup animation, so we want
  188. to make sure we don't send the init sequence if it isn't needed.
  189. */
  190. return SDL_TRUE;
  191. }
  192. if (vendor_id == USB_VENDOR_PDP) {
  193. /* The PDP Rock Candy (PID 0x0246) doesn't send 0x02 on Linux for some reason */
  194. return SDL_FALSE;
  195. }
  196. /* It doesn't hurt to reinit, especially if a driver has misconfigured the controller */
  197. /*return SDL_TRUE;*/
  198. return SDL_FALSE;
  199. }
  200. static SDL_bool
  201. SendControllerInit(hid_device *dev, SDL_DriverXboxOne_Context *ctx)
  202. {
  203. Uint16 vendor_id = ctx->vendor_id;
  204. Uint16 product_id = ctx->product_id;
  205. if (!IsBluetoothXboxOneController(vendor_id, product_id)) {
  206. int i;
  207. Uint8 init_packet[USB_PACKET_LENGTH];
  208. for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
  209. const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[i];
  210. if (packet->vendor_id && (vendor_id != packet->vendor_id)) {
  211. continue;
  212. }
  213. if (packet->product_id && (product_id != packet->product_id)) {
  214. continue;
  215. }
  216. if (packet->exclude_vendor_id && (vendor_id == packet->exclude_vendor_id)) {
  217. continue;
  218. }
  219. if (packet->exclude_product_id && (product_id == packet->exclude_product_id)) {
  220. continue;
  221. }
  222. SDL_memcpy(init_packet, packet->data, packet->size);
  223. if (init_packet[0] != 0x01) {
  224. init_packet[2] = ctx->sequence++;
  225. }
  226. if (hid_write(dev, init_packet, packet->size) != packet->size) {
  227. SDL_SetError("Couldn't write Xbox One initialization packet");
  228. return SDL_FALSE;
  229. }
  230. if (packet->response[0]) {
  231. const Uint32 RESPONSE_TIMEOUT_MS = 50;
  232. Uint32 start = SDL_GetTicks();
  233. SDL_bool got_response = SDL_FALSE;
  234. while (!got_response && !SDL_TICKS_PASSED(SDL_GetTicks(), start + RESPONSE_TIMEOUT_MS)) {
  235. Uint8 data[USB_PACKET_LENGTH];
  236. int size;
  237. while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
  238. #ifdef DEBUG_XBOX_PROTOCOL
  239. DumpPacket("Xbox One INIT packet: size = %d", data, size);
  240. #endif
  241. if (size >= 2 && data[0] == packet->response[0] && data[1] == packet->response[1]) {
  242. got_response = SDL_TRUE;
  243. }
  244. }
  245. }
  246. #ifdef DEBUG_XBOX_PROTOCOL
  247. SDL_Log("Init sequence %d got response: %s\n", i, got_response ? "TRUE" : "FALSE");
  248. #endif
  249. }
  250. }
  251. }
  252. SynchronizeRumbleSequence(dev, ctx);
  253. return SDL_TRUE;
  254. }
  255. static SDL_bool
  256. 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)
  257. {
  258. #ifdef __LINUX__
  259. if (IsBluetoothXboxOneController(vendor_id, product_id)) {
  260. /* We can't do rumble on this device, hid_write() fails, so don't try to open it here */
  261. return SDL_FALSE;
  262. }
  263. if (vendor_id == USB_VENDOR_POWERA && product_id == 0x541a) {
  264. /* The PowerA Mini controller, model 1240245-01, blocks while writing feature reports */
  265. return SDL_FALSE;
  266. }
  267. #endif
  268. return (type == SDL_CONTROLLER_TYPE_XBOXONE);
  269. }
  270. static const char *
  271. HIDAPI_DriverXboxOne_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  272. {
  273. return NULL;
  274. }
  275. static SDL_bool
  276. HIDAPI_DriverXboxOne_InitDevice(SDL_HIDAPI_Device *device)
  277. {
  278. return HIDAPI_JoystickConnected(device, NULL);
  279. }
  280. static int
  281. HIDAPI_DriverXboxOne_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  282. {
  283. return -1;
  284. }
  285. static void
  286. HIDAPI_DriverXboxOne_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  287. {
  288. }
  289. static SDL_bool
  290. HIDAPI_DriverXboxOne_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  291. {
  292. SDL_DriverXboxOne_Context *ctx;
  293. ctx = (SDL_DriverXboxOne_Context *)SDL_calloc(1, sizeof(*ctx));
  294. if (!ctx) {
  295. SDL_OutOfMemory();
  296. return SDL_FALSE;
  297. }
  298. device->dev = hid_open_path(device->path, 0);
  299. if (!device->dev) {
  300. SDL_free(ctx);
  301. SDL_SetError("Couldn't open %s", device->path);
  302. return SDL_FALSE;
  303. }
  304. device->context = ctx;
  305. ctx->vendor_id = device->vendor_id;
  306. ctx->product_id = device->product_id;
  307. ctx->start_time = SDL_GetTicks();
  308. ctx->sequence = 1;
  309. ctx->has_paddles = ControllerHasPaddles(ctx->vendor_id, ctx->product_id);
  310. /* Initialize the joystick capabilities */
  311. joystick->nbuttons = ctx->has_paddles ? SDL_CONTROLLER_BUTTON_MAX : (SDL_CONTROLLER_BUTTON_MAX + 4);
  312. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  313. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  314. return SDL_TRUE;
  315. }
  316. static int
  317. HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  318. {
  319. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  320. Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF };
  321. SynchronizeRumbleSequence(device->dev, ctx);
  322. /* Magnitude is 1..100 so scale the 16-bit input here */
  323. rumble_packet[2] = ctx->sequence++;
  324. rumble_packet[8] = low_frequency_rumble / 655;
  325. rumble_packet[9] = high_frequency_rumble / 655;
  326. if (hid_write(device->dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  327. return SDL_SetError("Couldn't send rumble packet");
  328. }
  329. if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
  330. ctx->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
  331. if (!ctx->rumble_expiration) {
  332. ctx->rumble_expiration = 1;
  333. }
  334. } else {
  335. ctx->rumble_expiration = 0;
  336. }
  337. return 0;
  338. }
  339. static void
  340. HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  341. {
  342. Sint16 axis;
  343. if (ctx->last_state[4] != data[4]) {
  344. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[4] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  345. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[4] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  346. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[4] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  347. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[4] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  348. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[4] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  349. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[4] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  350. }
  351. if (ctx->last_state[5] != data[5]) {
  352. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[5] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  353. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[5] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  354. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[5] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  355. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[5] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  356. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  357. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  358. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[5] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  359. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[5] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  360. }
  361. /* Xbox One S report is 18 bytes
  362. 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
  363. Paddle bits:
  364. UL: 0x01 (A) UR: 0x02 (B)
  365. LL: 0x04 (X) LR: 0x08 (Y)
  366. 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
  367. Paddle bits:
  368. UL: 0x04 (A) UR: 0x01 (B)
  369. LL: 0x08 (X) LR: 0x02 (Y)
  370. */
  371. if (ctx->has_paddles && (size == 33 || size == 38)) {
  372. int paddle_index;
  373. int button1_bit;
  374. int button2_bit;
  375. int button3_bit;
  376. int button4_bit;
  377. SDL_bool paddles_mapped;
  378. if (size == 33) {
  379. /* XBox One Elite Series 1 */
  380. paddle_index = 32;
  381. button1_bit = 0x01;
  382. button2_bit = 0x02;
  383. button3_bit = 0x04;
  384. button4_bit = 0x08;
  385. /* The mapped controller state is at offset 4, the raw state is at offset 18, compare them to see if the paddles are mapped */
  386. paddles_mapped = (SDL_memcmp(&data[4], &data[18], 14) != 0);
  387. } else if (size == 38) {
  388. /* XBox One Elite Series 2 */
  389. paddle_index = 18;
  390. button1_bit = 0x04;
  391. button2_bit = 0x01;
  392. button3_bit = 0x08;
  393. button4_bit = 0x02;
  394. paddles_mapped = (data[19] != 0);
  395. }
  396. #ifdef DEBUG_XBOX_PROTOCOL
  397. SDL_Log(">>> Paddles: %d,%d,%d,%d mapped = %s\n",
  398. (data[paddle_index] & button1_bit) ? 1 : 0,
  399. (data[paddle_index] & button2_bit) ? 1 : 0,
  400. (data[paddle_index] & button3_bit) ? 1 : 0,
  401. (data[paddle_index] & button4_bit) ? 1 : 0,
  402. paddles_mapped ? "TRUE" : "FALSE"
  403. );
  404. #endif
  405. if (paddles_mapped) {
  406. /* Respect that the paddles are being used for other controls and don't pass them on to the app */
  407. data[paddle_index] = 0;
  408. }
  409. if (ctx->last_state[paddle_index] != data[paddle_index]) {
  410. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+0, (data[paddle_index] & button1_bit) ? SDL_PRESSED : SDL_RELEASED);
  411. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+1, (data[paddle_index] & button2_bit) ? SDL_PRESSED : SDL_RELEASED);
  412. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+2, (data[paddle_index] & button3_bit) ? SDL_PRESSED : SDL_RELEASED);
  413. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_MAX+3, (data[paddle_index] & button4_bit) ? SDL_PRESSED : SDL_RELEASED);
  414. }
  415. }
  416. axis = ((int)*(Sint16*)(&data[6]) * 64) - 32768;
  417. if (axis == 32704) {
  418. axis = 32767;
  419. }
  420. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  421. axis = ((int)*(Sint16*)(&data[8]) * 64) - 32768;
  422. if (axis == 32704) {
  423. axis = 32767;
  424. }
  425. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  426. axis = *(Sint16*)(&data[10]);
  427. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  428. axis = *(Sint16*)(&data[12]);
  429. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, ~axis);
  430. axis = *(Sint16*)(&data[14]);
  431. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  432. axis = *(Sint16*)(&data[16]);
  433. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, ~axis);
  434. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  435. }
  436. static void
  437. HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  438. {
  439. if (data[1] == 0x30) {
  440. /* The Xbox One S controller needs acks for mode reports */
  441. const Uint8 seqnum = data[2];
  442. const Uint8 ack[] = { 0x01, 0x20, seqnum, 0x09, 0x00, 0x07, 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
  443. hid_write(dev, ack, sizeof(ack));
  444. }
  445. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  446. }
  447. static SDL_bool
  448. HIDAPI_DriverXboxOne_UpdateDevice(SDL_HIDAPI_Device *device)
  449. {
  450. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  451. SDL_Joystick *joystick = NULL;
  452. Uint8 data[USB_PACKET_LENGTH];
  453. int size;
  454. if (device->num_joysticks > 0) {
  455. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  456. }
  457. if (!joystick) {
  458. return SDL_FALSE;
  459. }
  460. if (!ctx->initialized &&
  461. !ControllerSendsWaitingForInit(device->vendor_id, device->product_id)) {
  462. if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->start_time + CONTROLLER_INIT_DELAY_MS)) {
  463. if (!SendControllerInit(device->dev, ctx)) {
  464. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  465. return SDL_FALSE;
  466. }
  467. ctx->initialized = SDL_TRUE;
  468. }
  469. }
  470. while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  471. #ifdef DEBUG_XBOX_PROTOCOL
  472. DumpPacket("Xbox One packet: size = %d", data, size);
  473. #endif
  474. switch (data[0]) {
  475. case 0x02:
  476. /* Controller is connected and waiting for initialization */
  477. if (!ctx->initialized) {
  478. #ifdef DEBUG_XBOX_PROTOCOL
  479. SDL_Log("Delay after init: %ums\n", SDL_GetTicks() - ctx->start_time);
  480. #endif
  481. if (!SendControllerInit(device->dev, ctx)) {
  482. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  483. return SDL_FALSE;
  484. }
  485. ctx->initialized = SDL_TRUE;
  486. }
  487. break;
  488. case 0x03:
  489. /* Controller heartbeat */
  490. break;
  491. case 0x20:
  492. HIDAPI_DriverXboxOne_HandleStatePacket(joystick, device->dev, ctx, data, size);
  493. break;
  494. case 0x07:
  495. HIDAPI_DriverXboxOne_HandleModePacket(joystick, device->dev, ctx, data, size);
  496. break;
  497. default:
  498. #ifdef DEBUG_JOYSTICK
  499. SDL_Log("Unknown Xbox One packet: 0x%.2x\n", data[0]);
  500. #endif
  501. break;
  502. }
  503. }
  504. if (ctx->rumble_expiration) {
  505. Uint32 now = SDL_GetTicks();
  506. if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
  507. HIDAPI_DriverXboxOne_RumbleJoystick(device, joystick, 0, 0, 0);
  508. }
  509. }
  510. if (size < 0) {
  511. /* Read error, device is disconnected */
  512. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  513. }
  514. return (size >= 0);
  515. }
  516. static void
  517. HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  518. {
  519. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  520. if (ctx->rumble_expiration) {
  521. HIDAPI_DriverXboxOne_RumbleJoystick(device, joystick, 0, 0, 0);
  522. }
  523. hid_close(device->dev);
  524. device->dev = NULL;
  525. SDL_free(device->context);
  526. device->context = NULL;
  527. }
  528. static void
  529. HIDAPI_DriverXboxOne_FreeDevice(SDL_HIDAPI_Device *device)
  530. {
  531. }
  532. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne =
  533. {
  534. SDL_HINT_JOYSTICK_HIDAPI_XBOX,
  535. SDL_TRUE,
  536. HIDAPI_DriverXboxOne_IsSupportedDevice,
  537. HIDAPI_DriverXboxOne_GetDeviceName,
  538. HIDAPI_DriverXboxOne_InitDevice,
  539. HIDAPI_DriverXboxOne_GetDevicePlayerIndex,
  540. HIDAPI_DriverXboxOne_SetDevicePlayerIndex,
  541. HIDAPI_DriverXboxOne_UpdateDevice,
  542. HIDAPI_DriverXboxOne_OpenJoystick,
  543. HIDAPI_DriverXboxOne_RumbleJoystick,
  544. HIDAPI_DriverXboxOne_CloseJoystick,
  545. HIDAPI_DriverXboxOne_FreeDevice
  546. };
  547. #endif /* SDL_JOYSTICK_HIDAPI_XBOXONE */
  548. #endif /* SDL_JOYSTICK_HIDAPI */
  549. /* vi: set ts=4 sw=4 expandtab: */