SDL_hidapi_xboxone.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 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 USB_PACKET_LENGTH 64
  30. /* The amount of time to wait after hotplug to send controller init sequence */
  31. #define CONTROLLER_INIT_DELAY_MS 100
  32. /* This is the full init sequence for the Xbox One Elite Series 2 controller.
  33. Normally it isn't needed, but this switches the controller back to wired report mode after being in Bluetooth mode.
  34. */
  35. static const Uint8 xboxone_elite_init0[] = {
  36. 0x04, 0x20, 0x01, 0x00
  37. };
  38. static const Uint8 xboxone_elite_init1[] = {
  39. 0x01, 0x20, 0x28, 0x09, 0x00, 0x04, 0x20, 0x3A,
  40. 0x00, 0x00, 0x00, 0x31, 0x01
  41. };
  42. static const Uint8 xboxone_elite_init2[] = {
  43. 0x01, 0x20, 0x28, 0x09, 0x00, 0x04, 0x20, 0x6B,
  44. 0x01, 0x00, 0x00, 0x00, 0x00
  45. };
  46. static const Uint8 xboxone_elite_init3[] = {
  47. 0x05, 0x20, 0x02, 0x0F, 0x06, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x55, 0x53, 0x00, 0x00, 0x00,
  49. 0x00, 0x00, 0x00
  50. };
  51. static const Uint8 xboxone_elite_init4[] = {
  52. 0x05, 0x20, 0x03, 0x01, 0x00
  53. };
  54. static const Uint8 xboxone_elite_init5[] = {
  55. 0x0A, 0x20, 0x04, 0x03, 0x00, 0x01, 0x14
  56. };
  57. /*
  58. * This packet is required for all Xbox One pads with 2015
  59. * or later firmware installed (or present from the factory).
  60. */
  61. static const Uint8 xboxone_fw2015_init[] = {
  62. 0x05, 0x20, 0x00, 0x01, 0x00
  63. };
  64. /*
  65. * This packet turns on the LED on some controllers, including PowerA
  66. */
  67. static const Uint8 xboxone_led_enable[] = {
  68. 0x0A, 0x20, 0x04, 0x03, 0x00, 0x01, 0x14
  69. };
  70. /*
  71. * This packet is required for the Titanfall 2 Xbox One pads
  72. * (0x0e6f:0x0165) to finish initialization and for Hori pads
  73. * (0x0f0d:0x0067) to make the analog sticks work.
  74. */
  75. static const Uint8 xboxone_hori_init[] = {
  76. 0x01, 0x20, 0x00, 0x09, 0x00, 0x04, 0x20, 0x3a,
  77. 0x00, 0x00, 0x00, 0x80, 0x00
  78. };
  79. /*
  80. * This packet is required for some of the PDP pads to start
  81. * sending input reports. These pads include: (0x0e6f:0x02ab),
  82. * (0x0e6f:0x02a4).
  83. */
  84. static const Uint8 xboxone_pdp_init1[] = {
  85. 0x0a, 0x20, 0x00, 0x03, 0x00, 0x01, 0x14
  86. };
  87. /*
  88. * This packet is required for some of the PDP pads to start
  89. * sending input reports. These pads include: (0x0e6f:0x02ab),
  90. * (0x0e6f:0x02a4).
  91. */
  92. static const Uint8 xboxone_pdp_init2[] = {
  93. 0x06, 0x20, 0x00, 0x02, 0x01, 0x00
  94. };
  95. /*
  96. * A specific rumble packet is required for some PowerA pads to start
  97. * sending input reports. One of those pads is (0x24c6:0x543a).
  98. */
  99. static const Uint8 xboxone_rumblebegin_init[] = {
  100. 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
  101. 0x1D, 0x1D, 0xFF, 0x00, 0x00
  102. };
  103. /*
  104. * A rumble packet with zero FF intensity will immediately
  105. * terminate the rumbling required to init PowerA pads.
  106. * This should happen fast enough that the motors don't
  107. * spin up to enough speed to actually vibrate the gamepad.
  108. */
  109. static const Uint8 xboxone_rumbleend_init[] = {
  110. 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x00, 0x00
  112. };
  113. /*
  114. * This specifies the selection of init packets that a gamepad
  115. * will be sent on init *and* the order in which they will be
  116. * sent. The correct sequence number will be added when the
  117. * packet is going to be sent.
  118. */
  119. typedef struct {
  120. Uint16 vendor_id;
  121. Uint16 product_id;
  122. const Uint8 *data;
  123. int size;
  124. } SDL_DriverXboxOne_InitPacket;
  125. static const SDL_DriverXboxOne_InitPacket xboxone_init_packets[] = {
  126. { 0x0e6f, 0x0165, xboxone_hori_init, sizeof(xboxone_hori_init) },
  127. { 0x0f0d, 0x0067, xboxone_hori_init, sizeof(xboxone_hori_init) },
  128. { 0x045e, 0x0b00, xboxone_elite_init0, sizeof(xboxone_elite_init0) },
  129. { 0x045e, 0x0b00, xboxone_elite_init1, sizeof(xboxone_elite_init1) },
  130. { 0x045e, 0x0b00, xboxone_elite_init2, sizeof(xboxone_elite_init2) },
  131. { 0x045e, 0x0b00, xboxone_elite_init3, sizeof(xboxone_elite_init3) },
  132. { 0x045e, 0x0b00, xboxone_elite_init4, sizeof(xboxone_elite_init4) },
  133. { 0x045e, 0x0b00, xboxone_elite_init5, sizeof(xboxone_elite_init5) },
  134. { 0x0000, 0x0000, xboxone_fw2015_init, sizeof(xboxone_fw2015_init) },
  135. { 0x0000, 0x0000, xboxone_led_enable, sizeof(xboxone_led_enable) },
  136. { 0x0e6f, 0x0000, xboxone_pdp_init1, sizeof(xboxone_pdp_init1) },
  137. { 0x0e6f, 0x0000, xboxone_pdp_init2, sizeof(xboxone_pdp_init2) },
  138. { 0x24c6, 0x0000, xboxone_rumblebegin_init, sizeof(xboxone_rumblebegin_init) },
  139. { 0x24c6, 0x0000, xboxone_rumbleend_init, sizeof(xboxone_rumbleend_init) },
  140. };
  141. typedef struct {
  142. Uint16 vendor_id;
  143. Uint16 product_id;
  144. Uint32 start_time;
  145. SDL_bool initialized;
  146. Uint8 sequence;
  147. Uint8 last_state[USB_PACKET_LENGTH];
  148. Uint32 rumble_expiration;
  149. } SDL_DriverXboxOne_Context;
  150. static SDL_bool
  151. IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
  152. {
  153. /* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
  154. const Uint16 USB_VENDOR_MICROSOFT = 0x045e;
  155. const Uint16 USB_PRODUCT_XBOX_ONE_S_REV1 = 0x02e0;
  156. const Uint16 USB_PRODUCT_XBOX_ONE_S_REV2 = 0x02fd;
  157. const Uint16 USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2 = 0x0b05;
  158. if (vendor_id == USB_VENDOR_MICROSOFT) {
  159. if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1 ||
  160. product_id == USB_PRODUCT_XBOX_ONE_S_REV2 ||
  161. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2) {
  162. return SDL_TRUE;
  163. }
  164. }
  165. return SDL_FALSE;
  166. }
  167. static SDL_bool
  168. SendControllerInit(hid_device *dev, SDL_DriverXboxOne_Context *ctx)
  169. {
  170. Uint16 vendor_id = ctx->vendor_id;
  171. Uint16 product_id = ctx->product_id;
  172. if (!IsBluetoothXboxOneController(vendor_id, product_id)) {
  173. int i, j;
  174. Uint8 init_packet[USB_PACKET_LENGTH];
  175. for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
  176. const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[i];
  177. if (packet->vendor_id && (vendor_id != packet->vendor_id)) {
  178. continue;
  179. }
  180. if (packet->product_id && (product_id != packet->product_id)) {
  181. continue;
  182. }
  183. SDL_memcpy(init_packet, packet->data, packet->size);
  184. init_packet[2] = ctx->sequence++;
  185. if (hid_write(dev, init_packet, packet->size) != packet->size) {
  186. SDL_SetError("Couldn't write Xbox One initialization packet");
  187. return SDL_FALSE;
  188. }
  189. /* After the init we need to sync up the rumble sequence */
  190. if (packet->data == xboxone_fw2015_init) {
  191. for (j = 0; j < 255; ++j) {
  192. if (hid_write(dev, xboxone_rumbleend_init, sizeof(xboxone_rumbleend_init)) != sizeof(xboxone_rumbleend_init)) {
  193. SDL_SetError("Couldn't write Xbox One initialization packet");
  194. return SDL_FALSE;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. return SDL_TRUE;
  201. }
  202. static SDL_bool
  203. HIDAPI_DriverXboxOne_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, const char *name)
  204. {
  205. #ifdef __LINUX__
  206. if (IsBluetoothXboxOneController(vendor_id, product_id)) {
  207. /* We can't do rumble on this device, hid_write() fails, so don't try to open it here */
  208. return SDL_FALSE;
  209. }
  210. if (vendor_id == 0x24c6 && product_id == 0x541a) {
  211. /* The PowerA Mini controller, model 1240245-01, blocks while writing feature reports */
  212. return SDL_FALSE;
  213. }
  214. #endif
  215. return (SDL_GetJoystickGameControllerType(vendor_id, product_id, name) == SDL_CONTROLLER_TYPE_XBOXONE);
  216. }
  217. static const char *
  218. HIDAPI_DriverXboxOne_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  219. {
  220. return NULL;
  221. }
  222. static SDL_bool
  223. HIDAPI_DriverXboxOne_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
  224. {
  225. SDL_DriverXboxOne_Context *ctx;
  226. ctx = (SDL_DriverXboxOne_Context *)SDL_calloc(1, sizeof(*ctx));
  227. if (!ctx) {
  228. SDL_OutOfMemory();
  229. return SDL_FALSE;
  230. }
  231. *context = ctx;
  232. ctx->vendor_id = vendor_id;
  233. ctx->product_id = product_id;
  234. ctx->start_time = SDL_GetTicks();
  235. /* Initialize the joystick capabilities */
  236. joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
  237. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  238. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  239. return SDL_TRUE;
  240. }
  241. static int
  242. HIDAPI_DriverXboxOne_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  243. {
  244. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context;
  245. Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF };
  246. if (!ctx->initialized) {
  247. return 0;
  248. }
  249. /* Magnitude is 1..100 so scale the 16-bit input here */
  250. rumble_packet[2] = ctx->sequence++;
  251. rumble_packet[8] = low_frequency_rumble / 655;
  252. rumble_packet[9] = high_frequency_rumble / 655;
  253. if (hid_write(dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  254. return SDL_SetError("Couldn't send rumble packet");
  255. }
  256. if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
  257. ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
  258. } else {
  259. ctx->rumble_expiration = 0;
  260. }
  261. return 0;
  262. }
  263. static void
  264. HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  265. {
  266. Sint16 axis;
  267. if (ctx->last_state[4] != data[4]) {
  268. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[4] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  269. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[4] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  270. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[4] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  271. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[4] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  272. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[4] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  273. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[4] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  274. }
  275. if (ctx->last_state[5] != data[5]) {
  276. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[5] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  277. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[5] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  278. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[5] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  279. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[5] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  280. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  281. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  282. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[5] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  283. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[5] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  284. }
  285. axis = ((int)*(Sint16*)(&data[6]) * 64) - 32768;
  286. if (axis == 32704) {
  287. axis = 32767;
  288. }
  289. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  290. axis = ((int)*(Sint16*)(&data[8]) * 64) - 32768;
  291. if (axis == 32704) {
  292. axis = 32767;
  293. }
  294. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  295. axis = *(Sint16*)(&data[10]);
  296. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  297. axis = *(Sint16*)(&data[12]);
  298. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, ~axis);
  299. axis = *(Sint16*)(&data[14]);
  300. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  301. axis = *(Sint16*)(&data[16]);
  302. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, ~axis);
  303. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  304. }
  305. static void
  306. HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  307. {
  308. if (data[1] == 0x30) {
  309. /* The Xbox One S controller needs acks for mode reports */
  310. const Uint8 seqnum = data[2];
  311. const Uint8 ack[] = { 0x01, 0x20, seqnum, 0x09, 0x00, 0x07, 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
  312. hid_write(dev, ack, sizeof(ack));
  313. }
  314. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  315. }
  316. static SDL_bool
  317. HIDAPI_DriverXboxOne_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
  318. {
  319. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)context;
  320. Uint8 data[USB_PACKET_LENGTH];
  321. int size;
  322. if (!ctx->initialized) {
  323. if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->start_time + CONTROLLER_INIT_DELAY_MS)) {
  324. if (!SendControllerInit(dev, ctx)) {
  325. return SDL_FALSE;
  326. }
  327. ctx->initialized = SDL_TRUE;
  328. }
  329. }
  330. while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
  331. #ifdef DEBUG_XBOX_PROTOCOL
  332. SDL_Log("Xbox One packet: size = %d\n"
  333. " 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n"
  334. " 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n"
  335. " 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
  336. size,
  337. data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
  338. data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
  339. data[16], data[17], data[18], data[19]);
  340. #endif
  341. switch (data[0]) {
  342. case 0x20:
  343. HIDAPI_DriverXboxOne_HandleStatePacket(joystick, dev, ctx, data, size);
  344. break;
  345. case 0x07:
  346. HIDAPI_DriverXboxOne_HandleModePacket(joystick, dev, ctx, data, size);
  347. break;
  348. default:
  349. #ifdef DEBUG_JOYSTICK
  350. SDL_Log("Unknown Xbox One packet: 0x%.2x\n", data[0]);
  351. #endif
  352. break;
  353. }
  354. }
  355. if (ctx->rumble_expiration) {
  356. Uint32 now = SDL_GetTicks();
  357. if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
  358. HIDAPI_DriverXboxOne_Rumble(joystick, dev, context, 0, 0, 0);
  359. }
  360. }
  361. return (size >= 0);
  362. }
  363. static void
  364. HIDAPI_DriverXboxOne_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
  365. {
  366. SDL_free(context);
  367. }
  368. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne =
  369. {
  370. SDL_HINT_JOYSTICK_HIDAPI_XBOX,
  371. SDL_TRUE,
  372. HIDAPI_DriverXboxOne_IsSupportedDevice,
  373. HIDAPI_DriverXboxOne_GetDeviceName,
  374. HIDAPI_DriverXboxOne_Init,
  375. HIDAPI_DriverXboxOne_Rumble,
  376. HIDAPI_DriverXboxOne_Update,
  377. HIDAPI_DriverXboxOne_Quit
  378. };
  379. #endif /* SDL_JOYSTICK_HIDAPI_XBOXONE */
  380. #endif /* SDL_JOYSTICK_HIDAPI */
  381. /* vi: set ts=4 sw=4 expandtab: */