SDL_hidapi_xbox360.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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_XBOX360
  29. #ifdef __WIN32__
  30. #include "../../core/windows/SDL_xinput.h"
  31. #endif
  32. #define USB_PACKET_LENGTH 64
  33. typedef struct {
  34. Uint8 last_state[USB_PACKET_LENGTH];
  35. Uint32 rumble_expiration;
  36. #ifdef __WIN32__
  37. SDL_bool xinput_enabled;
  38. Uint8 xinput_slot;
  39. #endif
  40. } SDL_DriverXbox360_Context;
  41. #ifdef __WIN32__
  42. static Uint8 xinput_slots;
  43. static void
  44. HIDAPI_DriverXbox360_MarkXInputSlotUsed(Uint8 xinput_slot)
  45. {
  46. if (xinput_slot != XUSER_INDEX_ANY) {
  47. xinput_slots |= (0x01 << xinput_slot);
  48. }
  49. }
  50. static void
  51. HIDAPI_DriverXbox360_MarkXInputSlotFree(Uint8 xinput_slot)
  52. {
  53. if (xinput_slot != XUSER_INDEX_ANY) {
  54. xinput_slots &= ~(0x01 << xinput_slot);
  55. }
  56. }
  57. static SDL_bool
  58. HIDAPI_DriverXbox360_MissingXInputSlot()
  59. {
  60. return xinput_slots != 0x0F;
  61. }
  62. static Uint8
  63. HIDAPI_DriverXbox360_GuessXInputSlot(WORD wButtons)
  64. {
  65. DWORD user_index;
  66. int match_count;
  67. Uint8 match_slot;
  68. if (!XINPUTGETSTATE) {
  69. return XUSER_INDEX_ANY;
  70. }
  71. match_count = 0;
  72. for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
  73. XINPUT_STATE_EX xinput_state;
  74. if (XINPUTGETSTATE(user_index, &xinput_state) == ERROR_SUCCESS) {
  75. if (xinput_state.Gamepad.wButtons == wButtons) {
  76. ++match_count;
  77. match_slot = (Uint8)user_index;
  78. }
  79. }
  80. }
  81. if (match_count == 1) {
  82. return match_slot;
  83. }
  84. return XUSER_INDEX_ANY;
  85. }
  86. #endif /* __WIN32__ */
  87. static SDL_bool
  88. HIDAPI_DriverXbox360_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, Uint16 usage_page, Uint16 usage)
  89. {
  90. #if defined(__MACOSX__) || defined(__WIN32__)
  91. if (vendor_id == 0x045e && product_id == 0x028e && version == 1) {
  92. /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
  93. return SDL_FALSE;
  94. }
  95. return SDL_IsJoystickXbox360(vendor_id, product_id) || SDL_IsJoystickXboxOne(vendor_id, product_id);
  96. #else
  97. return SDL_IsJoystickXbox360(vendor_id, product_id);
  98. #endif
  99. }
  100. static const char *
  101. HIDAPI_DriverXbox360_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  102. {
  103. return HIDAPI_XboxControllerName(vendor_id, product_id);
  104. }
  105. static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
  106. {
  107. const Uint8 led_packet[] = { 0x01, 0x03, (2 + slot) };
  108. if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
  109. return SDL_FALSE;
  110. }
  111. return SDL_TRUE;
  112. }
  113. static SDL_bool
  114. HIDAPI_DriverXbox360_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
  115. {
  116. SDL_DriverXbox360_Context *ctx;
  117. ctx = (SDL_DriverXbox360_Context *)SDL_calloc(1, sizeof(*ctx));
  118. if (!ctx) {
  119. SDL_OutOfMemory();
  120. return SDL_FALSE;
  121. }
  122. #ifdef __WIN32__
  123. ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE);
  124. if (ctx->xinput_enabled && WIN_LoadXInputDLL() < 0) {
  125. ctx->xinput_enabled = SDL_FALSE;
  126. }
  127. ctx->xinput_slot = XUSER_INDEX_ANY;
  128. #endif
  129. *context = ctx;
  130. /* Set the controller LED */
  131. SetSlotLED(dev, (joystick->instance_id % 4));
  132. /* Initialize the joystick capabilities */
  133. joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
  134. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  135. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  136. return SDL_TRUE;
  137. }
  138. static int
  139. HIDAPI_DriverXbox360_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  140. {
  141. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
  142. #ifdef __WIN32__
  143. if (ctx->xinput_slot != XUSER_INDEX_ANY) {
  144. XINPUT_VIBRATION XVibration;
  145. if (!XINPUTSETSTATE) {
  146. return SDL_Unsupported();
  147. }
  148. XVibration.wLeftMotorSpeed = low_frequency_rumble;
  149. XVibration.wRightMotorSpeed = high_frequency_rumble;
  150. if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) != ERROR_SUCCESS) {
  151. return SDL_SetError("XInputSetState() failed");
  152. }
  153. }
  154. #else
  155. #ifdef __MACOSX__
  156. /* On Mac OS X the 360Controller driver uses this short report,
  157. and we need to prefix it with a magic token so hidapi passes it through untouched
  158. */
  159. Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
  160. rumble_packet[6+2] = (low_frequency_rumble >> 8);
  161. rumble_packet[6+3] = (high_frequency_rumble >> 8);
  162. #else
  163. Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  164. rumble_packet[3] = (low_frequency_rumble >> 8);
  165. rumble_packet[4] = (high_frequency_rumble >> 8);
  166. #endif
  167. if (hid_write(dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  168. return SDL_SetError("Couldn't send rumble packet");
  169. }
  170. #endif /* __WIN32__ */
  171. if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
  172. ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
  173. } else {
  174. ctx->rumble_expiration = 0;
  175. }
  176. return 0;
  177. }
  178. #ifdef __WIN32__
  179. /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
  180. however with this interface there is no rumble support, no guide button,
  181. and the left and right triggers are tied together as a single axis.
  182. */
  183. static void
  184. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  185. {
  186. Sint16 axis;
  187. if (ctx->last_state[10] != data[10]) {
  188. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  189. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[10] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  190. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[10] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  191. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[10] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  192. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[10] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  193. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[10] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  194. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[10] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  195. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[10] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  196. }
  197. if (ctx->last_state[11] != data[11]) {
  198. SDL_bool dpad_up = SDL_FALSE;
  199. SDL_bool dpad_down = SDL_FALSE;
  200. SDL_bool dpad_left = SDL_FALSE;
  201. SDL_bool dpad_right = SDL_FALSE;
  202. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[11] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  203. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[11] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  204. switch (data[11] & 0x3C) {
  205. case 4:
  206. dpad_up = SDL_TRUE;
  207. break;
  208. case 8:
  209. dpad_up = SDL_TRUE;
  210. dpad_right = SDL_TRUE;
  211. break;
  212. case 12:
  213. dpad_right = SDL_TRUE;
  214. break;
  215. case 16:
  216. dpad_right = SDL_TRUE;
  217. dpad_down = SDL_TRUE;
  218. break;
  219. case 20:
  220. dpad_down = SDL_TRUE;
  221. break;
  222. case 24:
  223. dpad_left = SDL_TRUE;
  224. dpad_down = SDL_TRUE;
  225. break;
  226. case 28:
  227. dpad_left = SDL_TRUE;
  228. break;
  229. case 32:
  230. dpad_up = SDL_TRUE;
  231. dpad_left = SDL_TRUE;
  232. break;
  233. default:
  234. break;
  235. }
  236. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  237. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  238. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  239. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  240. }
  241. axis = (int)*(Uint16*)(&data[0]) - 0x8000;
  242. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  243. axis = (int)*(Uint16*)(&data[2]) - 0x8000;
  244. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  245. axis = (int)*(Uint16*)(&data[4]) - 0x8000;
  246. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  247. axis = (int)*(Uint16*)(&data[6]) - 0x8000;
  248. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  249. if (ctx->xinput_slot == XUSER_INDEX_ANY && HIDAPI_DriverXbox360_MissingXInputSlot()) {
  250. WORD wButtons = 0;
  251. if (data[10] & 0x01) {
  252. wButtons |= XINPUT_GAMEPAD_A;
  253. }
  254. if (data[10] & 0x02) {
  255. wButtons |= XINPUT_GAMEPAD_B;
  256. }
  257. if (data[10] & 0x04) {
  258. wButtons |= XINPUT_GAMEPAD_X;
  259. }
  260. if (data[10] & 0x08) {
  261. wButtons |= XINPUT_GAMEPAD_Y;
  262. }
  263. if (wButtons != 0) {
  264. Uint8 xinput_slot = HIDAPI_DriverXbox360_GuessXInputSlot(wButtons);
  265. if (xinput_slot != XUSER_INDEX_ANY) {
  266. HIDAPI_DriverXbox360_MarkXInputSlotUsed(xinput_slot);
  267. ctx->xinput_slot = xinput_slot;
  268. }
  269. }
  270. }
  271. if (ctx->xinput_slot == XUSER_INDEX_ANY) {
  272. axis = (data[9] * 257) - 32768;
  273. if (data[9] < 0x80) {
  274. axis = -axis * 2 - 32769;
  275. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  276. } else if (data[9] > 0x80) {
  277. axis = axis * 2 - 32767;
  278. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  279. } else {
  280. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
  281. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
  282. }
  283. } else {
  284. XINPUT_STATE_EX xinput_state;
  285. if (XINPUTGETSTATE(ctx->xinput_slot, &xinput_state) == ERROR_SUCCESS) {
  286. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
  287. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state.Gamepad.bLeftTrigger * 257) - 32768);
  288. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state.Gamepad.bRightTrigger * 257) - 32768);
  289. }
  290. }
  291. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  292. }
  293. #else
  294. static void
  295. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  296. {
  297. Sint16 axis;
  298. #ifdef __MACOSX__
  299. const SDL_bool invert_y_axes = SDL_FALSE;
  300. #else
  301. const SDL_bool invert_y_axes = SDL_TRUE;
  302. #endif
  303. if (ctx->last_state[2] != data[2]) {
  304. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  305. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  306. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  307. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  308. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  309. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  310. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  311. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  312. }
  313. if (ctx->last_state[3] != data[3]) {
  314. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  315. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  316. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  317. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  318. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  319. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  320. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  321. }
  322. axis = ((int)data[4] * 257) - 32768;
  323. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  324. axis = ((int)data[5] * 257) - 32768;
  325. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  326. axis = *(Sint16*)(&data[6]);
  327. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  328. axis = *(Sint16*)(&data[8]);
  329. if (invert_y_axes) {
  330. axis = ~axis;
  331. }
  332. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  333. axis = *(Sint16*)(&data[10]);
  334. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  335. axis = *(Sint16*)(&data[12]);
  336. if (invert_y_axes) {
  337. axis = ~axis;
  338. }
  339. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  340. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  341. }
  342. #endif /* __WIN32__ */
  343. #ifdef __MACOSX__
  344. static void
  345. HIDAPI_DriverXboxOneS_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  346. {
  347. Sint16 axis;
  348. if (ctx->last_state[14] != data[14]) {
  349. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[14] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  350. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[14] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  351. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[14] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  352. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[14] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  353. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[14] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  354. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[14] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  355. }
  356. if (ctx->last_state[15] != data[15]) {
  357. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[15] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  358. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[15] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  359. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[15] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  360. }
  361. if (ctx->last_state[16] != data[16]) {
  362. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[16] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  363. }
  364. if (ctx->last_state[13] != data[13]) {
  365. SDL_bool dpad_up = SDL_FALSE;
  366. SDL_bool dpad_down = SDL_FALSE;
  367. SDL_bool dpad_left = SDL_FALSE;
  368. SDL_bool dpad_right = SDL_FALSE;
  369. switch (data[13]) {
  370. case 1:
  371. dpad_up = SDL_TRUE;
  372. break;
  373. case 2:
  374. dpad_up = SDL_TRUE;
  375. dpad_right = SDL_TRUE;
  376. break;
  377. case 3:
  378. dpad_right = SDL_TRUE;
  379. break;
  380. case 4:
  381. dpad_right = SDL_TRUE;
  382. dpad_down = SDL_TRUE;
  383. break;
  384. case 5:
  385. dpad_down = SDL_TRUE;
  386. break;
  387. case 6:
  388. dpad_left = SDL_TRUE;
  389. dpad_down = SDL_TRUE;
  390. break;
  391. case 7:
  392. dpad_left = SDL_TRUE;
  393. break;
  394. case 8:
  395. dpad_up = SDL_TRUE;
  396. dpad_left = SDL_TRUE;
  397. break;
  398. default:
  399. break;
  400. }
  401. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  402. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  403. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  404. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  405. }
  406. axis = (int)*(Uint16*)(&data[1]) - 0x8000;
  407. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  408. axis = (int)*(Uint16*)(&data[3]) - 0x8000;
  409. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  410. axis = (int)*(Uint16*)(&data[5]) - 0x8000;
  411. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  412. axis = (int)*(Uint16*)(&data[7]) - 0x8000;
  413. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  414. axis = ((int)*(Sint16*)(&data[9]) * 64) - 32768;
  415. if (axis == 32704) {
  416. axis = 32767;
  417. }
  418. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  419. axis = ((int)*(Sint16*)(&data[11]) * 64) - 32768;
  420. if (axis == 32704) {
  421. axis = 32767;
  422. }
  423. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  424. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  425. }
  426. static void
  427. HIDAPI_DriverXboxOneS_HandleGuidePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  428. {
  429. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[1] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  430. }
  431. #endif /* __MACOSX__ */
  432. static SDL_bool
  433. HIDAPI_DriverXbox360_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
  434. {
  435. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
  436. Uint8 data[USB_PACKET_LENGTH];
  437. int size;
  438. while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
  439. #ifdef __WIN32__
  440. HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size);
  441. #else
  442. switch (data[0]) {
  443. case 0x00:
  444. HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size);
  445. break;
  446. #ifdef __MACOSX__
  447. case 0x01:
  448. HIDAPI_DriverXboxOneS_HandleStatePacket(joystick, dev, ctx, data, size);
  449. break;
  450. case 0x02:
  451. HIDAPI_DriverXboxOneS_HandleGuidePacket(joystick, dev, ctx, data, size);
  452. break;
  453. #endif
  454. default:
  455. #ifdef DEBUG_JOYSTICK
  456. SDL_Log("Unknown Xbox 360 packet, size = %d\n", size);
  457. SDL_Log("%.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
  458. data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],
  459. data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15], data[16]);
  460. #endif
  461. break;
  462. }
  463. #endif /* __WIN32__ */
  464. }
  465. if (ctx->rumble_expiration) {
  466. Uint32 now = SDL_GetTicks();
  467. if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
  468. HIDAPI_DriverXbox360_Rumble(joystick, dev, context, 0, 0, 0);
  469. }
  470. }
  471. return (size >= 0);
  472. }
  473. static void
  474. HIDAPI_DriverXbox360_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
  475. {
  476. #ifdef __WIN32__
  477. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
  478. if (ctx->xinput_enabled) {
  479. HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
  480. WIN_UnloadXInputDLL();
  481. }
  482. #endif
  483. SDL_free(context);
  484. }
  485. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
  486. {
  487. SDL_HINT_JOYSTICK_HIDAPI_XBOX,
  488. SDL_TRUE,
  489. HIDAPI_DriverXbox360_IsSupportedDevice,
  490. HIDAPI_DriverXbox360_GetDeviceName,
  491. HIDAPI_DriverXbox360_Init,
  492. HIDAPI_DriverXbox360_Rumble,
  493. HIDAPI_DriverXbox360_Update,
  494. HIDAPI_DriverXbox360_Quit
  495. };
  496. #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */
  497. #endif /* SDL_JOYSTICK_HIDAPI */
  498. /* vi: set ts=4 sw=4 expandtab: */