SDL_hidapi_xboxone.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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_ms_init0[] = {
  36. 0x04, 0x20, 0x01, 0x00
  37. };
  38. static const Uint8 xboxone_ms_init1[] = {
  39. 0x01, 0x20, 0x28, 0x09, 0x00, 0x04, 0x20, 0x3A,
  40. 0x00, 0x00, 0x00, 0x31, 0x01
  41. };
  42. static const Uint8 xboxone_ms_init2[] = {
  43. 0x01, 0x20, 0x28, 0x09, 0x00, 0x04, 0x20, 0x6B,
  44. 0x01, 0x00, 0x00, 0x00, 0x00
  45. };
  46. static const Uint8 xboxone_ms_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_ms_init4[] = {
  52. 0x05, 0x20, 0x03, 0x01, 0x00
  53. };
  54. static const Uint8 xboxone_ms_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, 0x0000, xboxone_ms_init0, sizeof(xboxone_ms_init0) },
  129. { 0x045e, 0x0000, xboxone_ms_init1, sizeof(xboxone_ms_init1) },
  130. { 0x045e, 0x0000, xboxone_ms_init2, sizeof(xboxone_ms_init2) },
  131. { 0x045e, 0x0000, xboxone_ms_init3, sizeof(xboxone_ms_init3) },
  132. { 0x045e, 0x0000, xboxone_ms_init4, sizeof(xboxone_ms_init4) },
  133. { 0x045e, 0x0000, xboxone_ms_init5, sizeof(xboxone_ms_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_BLUETOOTH = 0x02e0;
  156. const Uint16 USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH = 0x02fd;
  157. const Uint16 USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH = 0x0b05;
  158. if (vendor_id == USB_VENDOR_MICROSOFT) {
  159. if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
  160. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
  161. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
  162. return SDL_TRUE;
  163. }
  164. }
  165. return SDL_FALSE;
  166. }
  167. /* Return true if this controller sends the 0x02 "waiting for init" packet */
  168. static SDL_bool
  169. ControllerSendsWaitingForInit(Uint16 vendor_id, Uint16 product_id)
  170. {
  171. const Uint16 USB_VENDOR_HYPERKIN = 0x2e24;
  172. if (vendor_id == USB_VENDOR_HYPERKIN) {
  173. /* The Hyperkin controllers always send 0x02 when waiting for init,
  174. and the Hyperkin Duke plays an Xbox startup animation, so we want
  175. to make sure we don't send the init sequence if it isn't needed.
  176. */
  177. return SDL_TRUE;
  178. } else {
  179. /* Other controllers may or may not send 0x02, but it doesn't hurt */
  180. return SDL_FALSE;
  181. }
  182. }
  183. static SDL_bool
  184. SendControllerInit(hid_device *dev, SDL_DriverXboxOne_Context *ctx)
  185. {
  186. Uint16 vendor_id = ctx->vendor_id;
  187. Uint16 product_id = ctx->product_id;
  188. if (!IsBluetoothXboxOneController(vendor_id, product_id)) {
  189. int i, j;
  190. Uint8 init_packet[USB_PACKET_LENGTH];
  191. for (i = 0; i < SDL_arraysize(xboxone_init_packets); ++i) {
  192. const SDL_DriverXboxOne_InitPacket *packet = &xboxone_init_packets[i];
  193. if (packet->vendor_id && (vendor_id != packet->vendor_id)) {
  194. continue;
  195. }
  196. if (packet->product_id && (product_id != packet->product_id)) {
  197. continue;
  198. }
  199. SDL_memcpy(init_packet, packet->data, packet->size);
  200. init_packet[2] = ctx->sequence++;
  201. if (hid_write(dev, init_packet, packet->size) != packet->size) {
  202. SDL_SetError("Couldn't write Xbox One initialization packet");
  203. return SDL_FALSE;
  204. }
  205. /* After the init we need to sync up the rumble sequence */
  206. if (packet->data == xboxone_fw2015_init) {
  207. for (j = 0; j < 255; ++j) {
  208. if (hid_write(dev, xboxone_rumbleend_init, sizeof(xboxone_rumbleend_init)) != sizeof(xboxone_rumbleend_init)) {
  209. SDL_SetError("Couldn't write Xbox One initialization packet");
  210. return SDL_FALSE;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. return SDL_TRUE;
  217. }
  218. static SDL_bool
  219. HIDAPI_DriverXboxOne_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, const char *name)
  220. {
  221. #ifdef __LINUX__
  222. if (IsBluetoothXboxOneController(vendor_id, product_id)) {
  223. /* We can't do rumble on this device, hid_write() fails, so don't try to open it here */
  224. return SDL_FALSE;
  225. }
  226. if (vendor_id == 0x24c6 && product_id == 0x541a) {
  227. /* The PowerA Mini controller, model 1240245-01, blocks while writing feature reports */
  228. return SDL_FALSE;
  229. }
  230. #endif
  231. return (SDL_GetJoystickGameControllerType(vendor_id, product_id, name) == 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->start_time = SDL_GetTicks();
  271. /* Initialize the joystick capabilities */
  272. joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
  273. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  274. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  275. return SDL_TRUE;
  276. }
  277. static int
  278. HIDAPI_DriverXboxOne_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  279. {
  280. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  281. Uint8 rumble_packet[] = { 0x09, 0x00, 0x00, 0x09, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0xFF };
  282. if (!ctx->initialized) {
  283. return 0;
  284. }
  285. /* Magnitude is 1..100 so scale the 16-bit input here */
  286. rumble_packet[2] = ctx->sequence++;
  287. rumble_packet[8] = low_frequency_rumble / 655;
  288. rumble_packet[9] = high_frequency_rumble / 655;
  289. if (hid_write(device->dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  290. return SDL_SetError("Couldn't send rumble packet");
  291. }
  292. if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
  293. ctx->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
  294. if (!ctx->rumble_expiration) {
  295. ctx->rumble_expiration = 1;
  296. }
  297. } else {
  298. ctx->rumble_expiration = 0;
  299. }
  300. return 0;
  301. }
  302. static void
  303. HIDAPI_DriverXboxOne_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  304. {
  305. Sint16 axis;
  306. if (ctx->last_state[4] != data[4]) {
  307. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[4] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  308. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[4] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  309. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[4] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  310. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[4] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  311. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[4] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  312. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[4] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  313. }
  314. if (ctx->last_state[5] != data[5]) {
  315. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[5] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  316. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[5] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  317. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[5] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  318. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[5] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  319. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[5] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  320. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[5] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  321. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[5] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  322. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[5] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  323. }
  324. axis = ((int)*(Sint16*)(&data[6]) * 64) - 32768;
  325. if (axis == 32704) {
  326. axis = 32767;
  327. }
  328. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  329. axis = ((int)*(Sint16*)(&data[8]) * 64) - 32768;
  330. if (axis == 32704) {
  331. axis = 32767;
  332. }
  333. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  334. axis = *(Sint16*)(&data[10]);
  335. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  336. axis = *(Sint16*)(&data[12]);
  337. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, ~axis);
  338. axis = *(Sint16*)(&data[14]);
  339. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  340. axis = *(Sint16*)(&data[16]);
  341. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, ~axis);
  342. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  343. }
  344. static void
  345. HIDAPI_DriverXboxOne_HandleModePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXboxOne_Context *ctx, Uint8 *data, int size)
  346. {
  347. if (data[1] == 0x30) {
  348. /* The Xbox One S controller needs acks for mode reports */
  349. const Uint8 seqnum = data[2];
  350. const Uint8 ack[] = { 0x01, 0x20, seqnum, 0x09, 0x00, 0x07, 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 };
  351. hid_write(dev, ack, sizeof(ack));
  352. }
  353. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[4] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  354. }
  355. static SDL_bool
  356. HIDAPI_DriverXboxOne_UpdateDevice(SDL_HIDAPI_Device *device)
  357. {
  358. SDL_DriverXboxOne_Context *ctx = (SDL_DriverXboxOne_Context *)device->context;
  359. SDL_Joystick *joystick = NULL;
  360. Uint8 data[USB_PACKET_LENGTH];
  361. int size;
  362. if (device->num_joysticks > 0) {
  363. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  364. }
  365. if (!joystick) {
  366. return SDL_FALSE;
  367. }
  368. if (!ctx->initialized && !ControllerSendsWaitingForInit(device->vendor_id, device->product_id)) {
  369. if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->start_time + CONTROLLER_INIT_DELAY_MS)) {
  370. if (!SendControllerInit(device->dev, ctx)) {
  371. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  372. return SDL_FALSE;
  373. }
  374. ctx->initialized = SDL_TRUE;
  375. }
  376. }
  377. while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  378. #ifdef DEBUG_XBOX_PROTOCOL
  379. SDL_Log("Xbox One packet: size = %d\n"
  380. " 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n"
  381. " 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n"
  382. " 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
  383. size,
  384. data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
  385. data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],
  386. data[16], data[17], data[18], data[19]);
  387. #endif
  388. switch (data[0]) {
  389. case 0x02:
  390. /* Controller is connected and waiting for initialization */
  391. if (!SendControllerInit(device->dev, ctx)) {
  392. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  393. return SDL_FALSE;
  394. }
  395. ctx->initialized = SDL_TRUE;
  396. break;
  397. case 0x03:
  398. /* Controller heartbeat */
  399. break;
  400. case 0x20:
  401. HIDAPI_DriverXboxOne_HandleStatePacket(joystick, device->dev, ctx, data, size);
  402. break;
  403. case 0x07:
  404. HIDAPI_DriverXboxOne_HandleModePacket(joystick, device->dev, ctx, data, size);
  405. break;
  406. default:
  407. #ifdef DEBUG_JOYSTICK
  408. SDL_Log("Unknown Xbox One packet: 0x%.2x\n", data[0]);
  409. #endif
  410. break;
  411. }
  412. }
  413. if (ctx->rumble_expiration) {
  414. Uint32 now = SDL_GetTicks();
  415. if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
  416. HIDAPI_DriverXboxOne_RumbleJoystick(device, joystick, 0, 0, 0);
  417. }
  418. }
  419. if (size < 0) {
  420. /* Read error, device is disconnected */
  421. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  422. }
  423. return (size >= 0);
  424. }
  425. static void
  426. HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  427. {
  428. hid_close(device->dev);
  429. device->dev = NULL;
  430. SDL_free(device->context);
  431. device->context = NULL;
  432. }
  433. static void
  434. HIDAPI_DriverXboxOne_FreeDevice(SDL_HIDAPI_Device *device)
  435. {
  436. }
  437. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne =
  438. {
  439. SDL_HINT_JOYSTICK_HIDAPI_XBOX,
  440. SDL_TRUE,
  441. HIDAPI_DriverXboxOne_IsSupportedDevice,
  442. HIDAPI_DriverXboxOne_GetDeviceName,
  443. HIDAPI_DriverXboxOne_InitDevice,
  444. HIDAPI_DriverXboxOne_GetDevicePlayerIndex,
  445. HIDAPI_DriverXboxOne_SetDevicePlayerIndex,
  446. HIDAPI_DriverXboxOne_UpdateDevice,
  447. HIDAPI_DriverXboxOne_OpenJoystick,
  448. HIDAPI_DriverXboxOne_RumbleJoystick,
  449. HIDAPI_DriverXboxOne_CloseJoystick,
  450. HIDAPI_DriverXboxOne_FreeDevice
  451. };
  452. #endif /* SDL_JOYSTICK_HIDAPI_XBOXONE */
  453. #endif /* SDL_JOYSTICK_HIDAPI */
  454. /* vi: set ts=4 sw=4 expandtab: */