SDL_hidapi_xbox360.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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_events.h"
  22. #include "SDL_timer.h"
  23. #include "SDL_joystick.h"
  24. #include "SDL_gamecontroller.h"
  25. #include "../SDL_sysjoystick.h"
  26. #include "SDL_hidapijoystick_c.h"
  27. #include "SDL_hidapi_rumble.h"
  28. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  29. #ifdef __WIN32__
  30. #define SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  31. /* This requires the Windows 10 SDK to build */
  32. /*#define SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT*/
  33. #endif
  34. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  35. #include "../../core/windows/SDL_xinput.h"
  36. #endif
  37. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  38. #include "../../core/windows/SDL_windows.h"
  39. typedef struct WindowsGamingInputGamepadState WindowsGamingInputGamepadState;
  40. #define GamepadButtons_GUIDE 0x40000000
  41. #define COBJMACROS
  42. #include "windows.gaming.input.h"
  43. #endif
  44. #if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
  45. #define SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  46. #endif
  47. typedef struct {
  48. Uint8 last_state[USB_PACKET_LENGTH];
  49. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  50. Uint32 match_state; /* Low 16 bits for button states, high 16 for 4 4bit axes */
  51. Uint32 last_state_packet;
  52. #endif
  53. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  54. SDL_bool xinput_enabled;
  55. SDL_bool xinput_correlated;
  56. Uint8 xinput_correlation_id;
  57. Uint8 xinput_correlation_count;
  58. Uint8 xinput_uncorrelate_count;
  59. Uint8 xinput_slot;
  60. #endif
  61. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  62. SDL_bool wgi_correlated;
  63. Uint8 wgi_correlation_id;
  64. Uint8 wgi_correlation_count;
  65. Uint8 wgi_uncorrelate_count;
  66. WindowsGamingInputGamepadState *wgi_slot;
  67. #endif
  68. } SDL_DriverXbox360_Context;
  69. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  70. static struct {
  71. Uint32 last_state_packet;
  72. SDL_Joystick *joystick;
  73. SDL_Joystick *last_joystick;
  74. } guide_button_candidate;
  75. typedef struct WindowsMatchState {
  76. SHORT match_axes[4];
  77. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  78. WORD xinput_buttons;
  79. #endif
  80. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  81. Uint32 wgi_buttons;
  82. #endif
  83. SDL_bool any_data;
  84. } WindowsMatchState;
  85. static void HIDAPI_DriverXbox360_FillMatchState(WindowsMatchState *state, Uint32 match_state)
  86. {
  87. int ii;
  88. state->any_data = SDL_FALSE;
  89. /* SHORT state->match_axes[4] = {
  90. (match_state & 0x000F0000) >> 4,
  91. (match_state & 0x00F00000) >> 8,
  92. (match_state & 0x0F000000) >> 12,
  93. (match_state & 0xF0000000) >> 16,
  94. }; */
  95. for (ii = 0; ii < 4; ii++) {
  96. state->match_axes[ii] = (match_state & (0x000F0000 << (ii * 4))) >> (4 + ii * 4);
  97. if ((Uint32)(state->match_axes[ii] + 0x1000) > 0x2000) { /* match_state bit is not 0xF, 0x1, or 0x2 */
  98. state->any_data = SDL_TRUE;
  99. }
  100. }
  101. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  102. /* Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less */
  103. #define XInputAxesMatch(gamepad) (\
  104. (Uint32)(gamepad.sThumbLX - state->match_axes[0] + 0x1000) <= 0x2fff && \
  105. (Uint32)(~gamepad.sThumbLY - state->match_axes[1] + 0x1000) <= 0x2fff && \
  106. (Uint32)(gamepad.sThumbRX - state->match_axes[2] + 0x1000) <= 0x2fff && \
  107. (Uint32)(~gamepad.sThumbRY - state->match_axes[3] + 0x1000) <= 0x2fff)
  108. /* Explicit
  109. #define XInputAxesMatch(gamepad) (\
  110. SDL_abs((Sint8)((gamepad.sThumbLX & 0xF000) >> 8) - ((match_state & 0x000F0000) >> 12)) <= 0x10 && \
  111. SDL_abs((Sint8)((~gamepad.sThumbLY & 0xF000) >> 8) - ((match_state & 0x00F00000) >> 16)) <= 0x10 && \
  112. SDL_abs((Sint8)((gamepad.sThumbRX & 0xF000) >> 8) - ((match_state & 0x0F000000) >> 20)) <= 0x10 && \
  113. SDL_abs((Sint8)((~gamepad.sThumbRY & 0xF000) >> 8) - ((match_state & 0xF0000000) >> 24)) <= 0x10) */
  114. state->xinput_buttons =
  115. /* Bitwise map .RLDUWVQTS.KYXBA -> YXBA..WVQTKSRLDU */
  116. match_state << 12 | (match_state & 0x0780) >> 1 | (match_state & 0x0010) << 1 | (match_state & 0x0040) >> 2 | (match_state & 0x7800) >> 11;
  117. /* Explicit
  118. ((match_state & (1<<SDL_CONTROLLER_BUTTON_A)) ? XINPUT_GAMEPAD_A : 0) |
  119. ((match_state & (1<<SDL_CONTROLLER_BUTTON_B)) ? XINPUT_GAMEPAD_B : 0) |
  120. ((match_state & (1<<SDL_CONTROLLER_BUTTON_X)) ? XINPUT_GAMEPAD_X : 0) |
  121. ((match_state & (1<<SDL_CONTROLLER_BUTTON_Y)) ? XINPUT_GAMEPAD_Y : 0) |
  122. ((match_state & (1<<SDL_CONTROLLER_BUTTON_BACK)) ? XINPUT_GAMEPAD_BACK : 0) |
  123. ((match_state & (1<<SDL_CONTROLLER_BUTTON_START)) ? XINPUT_GAMEPAD_START : 0) |
  124. ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSTICK)) ? XINPUT_GAMEPAD_LEFT_THUMB : 0) |
  125. ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSTICK)) ? XINPUT_GAMEPAD_RIGHT_THUMB: 0) |
  126. ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) ? XINPUT_GAMEPAD_LEFT_SHOULDER : 0) |
  127. ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) ? XINPUT_GAMEPAD_RIGHT_SHOULDER : 0) |
  128. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_UP)) ? XINPUT_GAMEPAD_DPAD_UP : 0) |
  129. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_DOWN)) ? XINPUT_GAMEPAD_DPAD_DOWN : 0) |
  130. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_LEFT)) ? XINPUT_GAMEPAD_DPAD_LEFT : 0) |
  131. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) ? XINPUT_GAMEPAD_DPAD_RIGHT : 0);
  132. */
  133. if (state->xinput_buttons)
  134. state->any_data = SDL_TRUE;
  135. #endif
  136. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  137. /* Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less */
  138. #define WindowsGamingInputAxesMatch(gamepad) (\
  139. (Uint16)(((Sint16)(gamepad.LeftThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[0] + 0x1000) <= 0x2fff && \
  140. (Uint16)((~(Sint16)(gamepad.LeftThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[1] + 0x1000) <= 0x2fff && \
  141. (Uint16)(((Sint16)(gamepad.RightThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[2] + 0x1000) <= 0x2fff && \
  142. (Uint16)((~(Sint16)(gamepad.RightThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[3] + 0x1000) <= 0x2fff)
  143. state->wgi_buttons =
  144. /* Bitwise map .RLD UWVQ TS.K YXBA -> ..QT WVRL DUYX BAKS */
  145. /* RStick/LStick (QT) RShould/LShould (WV) DPad R/L/D/U YXBA bac(K) (S)tart */
  146. (match_state & 0x0180) << 5 | (match_state & 0x0600) << 1 | (match_state & 0x7800) >> 5 | (match_state & 0x000F) << 2 | (match_state & 0x0010) >> 3 | (match_state & 0x0040) >> 6;
  147. /* Explicit
  148. ((match_state & (1<<SDL_CONTROLLER_BUTTON_A)) ? GamepadButtons_A : 0) |
  149. ((match_state & (1<<SDL_CONTROLLER_BUTTON_B)) ? GamepadButtons_B : 0) |
  150. ((match_state & (1<<SDL_CONTROLLER_BUTTON_X)) ? GamepadButtons_X : 0) |
  151. ((match_state & (1<<SDL_CONTROLLER_BUTTON_Y)) ? GamepadButtons_Y : 0) |
  152. ((match_state & (1<<SDL_CONTROLLER_BUTTON_BACK)) ? GamepadButtons_View : 0) |
  153. ((match_state & (1<<SDL_CONTROLLER_BUTTON_START)) ? GamepadButtons_Menu : 0) |
  154. ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSTICK)) ? GamepadButtons_LeftThumbstick : 0) |
  155. ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSTICK)) ? GamepadButtons_RightThumbstick: 0) |
  156. ((match_state & (1<<SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) ? GamepadButtons_LeftShoulder: 0) |
  157. ((match_state & (1<<SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) ? GamepadButtons_RightShoulder: 0) |
  158. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_UP)) ? GamepadButtons_DPadUp : 0) |
  159. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_DOWN)) ? GamepadButtons_DPadDown : 0) |
  160. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_LEFT)) ? GamepadButtons_DPadLeft : 0) |
  161. ((match_state & (1<<SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) ? GamepadButtons_DPadRight : 0); */
  162. if (state->wgi_buttons)
  163. state->any_data = SDL_TRUE;
  164. #endif
  165. }
  166. #endif
  167. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  168. static struct {
  169. XINPUT_STATE_EX state;
  170. SDL_bool connected; /* Currently has an active XInput device */
  171. SDL_bool used; /* Is currently mapped to an SDL device */
  172. Uint8 correlation_id;
  173. } xinput_state[XUSER_MAX_COUNT];
  174. static SDL_bool xinput_device_change = SDL_TRUE;
  175. static SDL_bool xinput_state_dirty = SDL_TRUE;
  176. static void
  177. HIDAPI_DriverXbox360_UpdateXInput()
  178. {
  179. DWORD user_index;
  180. if (xinput_device_change) {
  181. for (user_index = 0; user_index < XUSER_MAX_COUNT; user_index++) {
  182. XINPUT_CAPABILITIES capabilities;
  183. xinput_state[user_index].connected = XINPUTGETCAPABILITIES(user_index, XINPUT_FLAG_GAMEPAD, &capabilities) == ERROR_SUCCESS;
  184. }
  185. xinput_device_change = SDL_FALSE;
  186. xinput_state_dirty = SDL_TRUE;
  187. }
  188. if (xinput_state_dirty) {
  189. xinput_state_dirty = SDL_FALSE;
  190. for (user_index = 0; user_index < SDL_arraysize(xinput_state); ++user_index) {
  191. if (xinput_state[user_index].connected) {
  192. if (XINPUTGETSTATE(user_index, &xinput_state[user_index].state) != ERROR_SUCCESS) {
  193. xinput_state[user_index].connected = SDL_FALSE;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. static void
  200. HIDAPI_DriverXbox360_MarkXInputSlotUsed(Uint8 xinput_slot)
  201. {
  202. if (xinput_slot != XUSER_INDEX_ANY) {
  203. xinput_state[xinput_slot].used = SDL_TRUE;
  204. }
  205. }
  206. static void
  207. HIDAPI_DriverXbox360_MarkXInputSlotFree(Uint8 xinput_slot)
  208. {
  209. if (xinput_slot != XUSER_INDEX_ANY) {
  210. xinput_state[xinput_slot].used = SDL_FALSE;
  211. }
  212. }
  213. static SDL_bool
  214. HIDAPI_DriverXbox360_MissingXInputSlot()
  215. {
  216. int ii;
  217. for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
  218. if (xinput_state[ii].connected && !xinput_state[ii].used) {
  219. return SDL_TRUE;
  220. }
  221. }
  222. return SDL_FALSE;
  223. }
  224. static SDL_bool
  225. HIDAPI_DriverXbox360_XInputSlotMatches(const WindowsMatchState *state, Uint8 slot_idx)
  226. {
  227. if (xinput_state[slot_idx].connected) {
  228. WORD xinput_buttons = xinput_state[slot_idx].state.Gamepad.wButtons;
  229. if ((xinput_buttons & ~XINPUT_GAMEPAD_GUIDE) == state->xinput_buttons && XInputAxesMatch(xinput_state[slot_idx].state.Gamepad)) {
  230. return SDL_TRUE;
  231. }
  232. }
  233. return SDL_FALSE;
  234. }
  235. static SDL_bool
  236. HIDAPI_DriverXbox360_GuessXInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, Uint8 *slot_idx)
  237. {
  238. int user_index;
  239. int match_count;
  240. *slot_idx = 0;
  241. match_count = 0;
  242. for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
  243. if (!xinput_state[user_index].used && HIDAPI_DriverXbox360_XInputSlotMatches(state, user_index)) {
  244. ++match_count;
  245. *slot_idx = (Uint8)user_index;
  246. /* Incrementing correlation_id for any match, as negative evidence for others being correlated */
  247. *correlation_id = ++xinput_state[user_index].correlation_id;
  248. }
  249. }
  250. /* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
  251. Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
  252. data. */
  253. if (match_count == 1 && state->any_data) {
  254. return SDL_TRUE;
  255. }
  256. return SDL_FALSE;
  257. }
  258. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
  259. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  260. typedef struct WindowsGamingInputGamepadState {
  261. __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
  262. struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
  263. SDL_DriverXbox360_Context *correlated_context;
  264. SDL_bool used; /* Is currently mapped to an SDL device */
  265. SDL_bool connected; /* Just used during update to track disconnected */
  266. Uint8 correlation_id;
  267. struct __x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration;
  268. } WindowsGamingInputGamepadState;
  269. static struct {
  270. WindowsGamingInputGamepadState **per_gamepad;
  271. int per_gamepad_count;
  272. SDL_bool initialized;
  273. SDL_bool dirty;
  274. SDL_bool need_device_list_update;
  275. int ref_count;
  276. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics *gamepad_statics;
  277. } wgi_state;
  278. static void
  279. HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotUsed(WindowsGamingInputGamepadState *wgi_slot, SDL_DriverXbox360_Context *ctx)
  280. {
  281. wgi_slot->used = SDL_TRUE;
  282. wgi_slot->correlated_context = ctx;
  283. }
  284. static void
  285. HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotFree(WindowsGamingInputGamepadState *wgi_slot)
  286. {
  287. wgi_slot->used = SDL_FALSE;
  288. wgi_slot->correlated_context = NULL;
  289. }
  290. static SDL_bool
  291. HIDAPI_DriverXbox360_MissingWindowsGamingInputSlot()
  292. {
  293. int ii;
  294. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  295. if (!wgi_state.per_gamepad[ii]->used) {
  296. return SDL_TRUE;
  297. }
  298. }
  299. return SDL_FALSE;
  300. }
  301. static void
  302. HIDAPI_DriverXbox360_UpdateWindowsGamingInput()
  303. {
  304. int ii;
  305. if (!wgi_state.gamepad_statics)
  306. return;
  307. if (!wgi_state.dirty)
  308. return;
  309. wgi_state.dirty = SDL_FALSE;
  310. if (wgi_state.need_device_list_update) {
  311. wgi_state.need_device_list_update = SDL_FALSE;
  312. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  313. wgi_state.per_gamepad[ii]->connected = SDL_FALSE;
  314. }
  315. HRESULT hr;
  316. __FIVectorView_1_Windows__CGaming__CInput__CGamepad *gamepads;
  317. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_get_Gamepads(wgi_state.gamepad_statics, &gamepads);
  318. if (SUCCEEDED(hr)) {
  319. unsigned int num_gamepads;
  320. hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_get_Size(gamepads, &num_gamepads);
  321. if (SUCCEEDED(hr)) {
  322. unsigned int i;
  323. for (i = 0; i < num_gamepads; ++i) {
  324. __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
  325. hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, i, &gamepad);
  326. if (SUCCEEDED(hr)) {
  327. SDL_bool found = SDL_FALSE;
  328. int jj;
  329. for (jj = 0; jj < wgi_state.per_gamepad_count ; jj++) {
  330. if (wgi_state.per_gamepad[jj]->gamepad == gamepad) {
  331. found = SDL_TRUE;
  332. wgi_state.per_gamepad[jj]->connected = SDL_TRUE;
  333. break;
  334. }
  335. }
  336. if (!found) {
  337. /* New device, add it */
  338. wgi_state.per_gamepad_count++;
  339. wgi_state.per_gamepad = SDL_realloc(wgi_state.per_gamepad, sizeof(wgi_state.per_gamepad[0]) * wgi_state.per_gamepad_count);
  340. if (!wgi_state.per_gamepad) {
  341. SDL_OutOfMemory();
  342. return;
  343. }
  344. WindowsGamingInputGamepadState *gamepad_state = SDL_calloc(1, sizeof(*gamepad_state));
  345. if (!gamepad_state) {
  346. SDL_OutOfMemory();
  347. return;
  348. }
  349. wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1] = gamepad_state;
  350. gamepad_state->gamepad = gamepad;
  351. gamepad_state->connected = SDL_TRUE;
  352. } else {
  353. /* Already tracked */
  354. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
  355. }
  356. }
  357. }
  358. for (ii = wgi_state.per_gamepad_count - 1; ii >= 0; ii--) {
  359. WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
  360. if (!gamepad_state->connected) {
  361. /* Device missing, must be disconnected */
  362. if (gamepad_state->correlated_context) {
  363. gamepad_state->correlated_context->wgi_correlated = SDL_FALSE;
  364. gamepad_state->correlated_context->wgi_slot = NULL;
  365. }
  366. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad_state->gamepad);
  367. SDL_free(gamepad_state);
  368. wgi_state.per_gamepad[ii] = wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1];
  369. --wgi_state.per_gamepad_count;
  370. }
  371. }
  372. }
  373. __FIVectorView_1_Windows__CGaming__CInput__CGamepad_Release(gamepads);
  374. }
  375. } /* need_device_list_update */
  376. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  377. HRESULT hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(wgi_state.per_gamepad[ii]->gamepad, &wgi_state.per_gamepad[ii]->state);
  378. if (!SUCCEEDED(hr)) {
  379. wgi_state.per_gamepad[ii]->connected = SDL_FALSE; /* Not used by anything, currently */
  380. }
  381. }
  382. }
  383. static void
  384. HIDAPI_DriverXbox360_InitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
  385. {
  386. wgi_state.need_device_list_update = SDL_TRUE;
  387. wgi_state.ref_count++;
  388. if (!wgi_state.initialized) {
  389. /* I think this takes care of RoInitialize() in a way that is compatible with the rest of SDL */
  390. if (FAILED(WIN_CoInitialize())) {
  391. return;
  392. }
  393. wgi_state.initialized = SDL_TRUE;
  394. wgi_state.dirty = SDL_TRUE;
  395. static const IID SDL_IID_IGamepadStatics = { 0x8BBCE529, 0xD49C, 0x39E9, { 0x95, 0x60, 0xE4, 0x7D, 0xDE, 0x96, 0xB7, 0xC8 } };
  396. HRESULT hr;
  397. HMODULE hModule = LoadLibraryA("combase.dll");
  398. if (hModule != NULL) {
  399. typedef HRESULT (WINAPI *WindowsCreateString_t)(PCNZWCH sourceString, UINT32 length, HSTRING* string);
  400. typedef HRESULT (WINAPI *WindowsDeleteString_t)(HSTRING string);
  401. typedef HRESULT (WINAPI *RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void** factory);
  402. WindowsCreateString_t WindowsCreateStringFunc = (WindowsCreateString_t)GetProcAddress(hModule, "WindowsCreateString");
  403. WindowsDeleteString_t WindowsDeleteStringFunc = (WindowsDeleteString_t)GetProcAddress(hModule, "WindowsDeleteString");
  404. RoGetActivationFactory_t RoGetActivationFactoryFunc = (RoGetActivationFactory_t)GetProcAddress(hModule, "RoGetActivationFactory");
  405. if (WindowsCreateStringFunc && WindowsDeleteStringFunc && RoGetActivationFactoryFunc) {
  406. LPTSTR pNamespace = L"Windows.Gaming.Input.Gamepad";
  407. HSTRING hNamespaceString;
  408. hr = WindowsCreateStringFunc(pNamespace, SDL_wcslen(pNamespace), &hNamespaceString);
  409. if (SUCCEEDED(hr)) {
  410. RoGetActivationFactoryFunc(hNamespaceString, &SDL_IID_IGamepadStatics, &wgi_state.gamepad_statics);
  411. WindowsDeleteStringFunc(hNamespaceString);
  412. }
  413. }
  414. FreeLibrary(hModule);
  415. }
  416. }
  417. }
  418. static SDL_bool
  419. HIDAPI_DriverXbox360_WindowsGamingInputSlotMatches(const WindowsMatchState *state, WindowsGamingInputGamepadState *slot)
  420. {
  421. Uint32 wgi_buttons = slot->state.Buttons;
  422. if ((wgi_buttons & 0x3FFF) == state->wgi_buttons && WindowsGamingInputAxesMatch(slot->state)) {
  423. return SDL_TRUE;
  424. }
  425. return SDL_FALSE;
  426. }
  427. static SDL_bool
  428. HIDAPI_DriverXbox360_GuessWindowsGamingInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, WindowsGamingInputGamepadState **slot)
  429. {
  430. int match_count;
  431. match_count = 0;
  432. for (int user_index = 0; user_index < wgi_state.per_gamepad_count; ++user_index) {
  433. WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[user_index];
  434. if (HIDAPI_DriverXbox360_WindowsGamingInputSlotMatches(state, gamepad_state)) {
  435. ++match_count;
  436. *slot = gamepad_state;
  437. /* Incrementing correlation_id for any match, as negative evidence for others being correlated */
  438. *correlation_id = ++gamepad_state->correlation_id;
  439. }
  440. }
  441. /* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
  442. Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
  443. data. */
  444. if (match_count == 1 && state->any_data) {
  445. return SDL_TRUE;
  446. }
  447. return SDL_FALSE;
  448. }
  449. static void
  450. HIDAPI_DriverXbox360_QuitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
  451. {
  452. wgi_state.need_device_list_update = SDL_TRUE;
  453. --wgi_state.ref_count;
  454. if (!wgi_state.ref_count && wgi_state.initialized) {
  455. for (int ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  456. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(wgi_state.per_gamepad[ii]->gamepad);
  457. }
  458. if (wgi_state.per_gamepad) {
  459. SDL_free(wgi_state.per_gamepad);
  460. wgi_state.per_gamepad = NULL;
  461. }
  462. wgi_state.per_gamepad_count = 0;
  463. if (wgi_state.gamepad_statics) {
  464. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_Release(wgi_state.gamepad_statics);
  465. wgi_state.gamepad_statics = NULL;
  466. }
  467. WIN_CoUninitialize();
  468. wgi_state.initialized = SDL_FALSE;
  469. }
  470. }
  471. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
  472. static void
  473. HIDAPI_DriverXbox360_PostUpdate(void)
  474. {
  475. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  476. SDL_bool unmapped_guide_pressed = SDL_FALSE;
  477. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  478. if (!wgi_state.dirty) {
  479. int ii;
  480. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  481. WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
  482. if (!gamepad_state->used && (gamepad_state->state.Buttons & GamepadButtons_GUIDE)) {
  483. unmapped_guide_pressed = SDL_TRUE;
  484. break;
  485. }
  486. }
  487. }
  488. wgi_state.dirty = SDL_TRUE;
  489. #endif
  490. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  491. if (!xinput_state_dirty) {
  492. int ii;
  493. for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
  494. if (xinput_state[ii].connected && !xinput_state[ii].used && (xinput_state[ii].state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE)) {
  495. unmapped_guide_pressed = SDL_TRUE;
  496. break;
  497. }
  498. }
  499. }
  500. xinput_state_dirty = SDL_TRUE;
  501. #endif
  502. if (unmapped_guide_pressed) {
  503. if (guide_button_candidate.joystick && !guide_button_candidate.last_joystick) {
  504. SDL_PrivateJoystickButton(guide_button_candidate.joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_PRESSED);
  505. guide_button_candidate.last_joystick = guide_button_candidate.joystick;
  506. }
  507. } else if (guide_button_candidate.last_joystick) {
  508. SDL_PrivateJoystickButton(guide_button_candidate.last_joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
  509. guide_button_candidate.last_joystick = NULL;
  510. }
  511. guide_button_candidate.joystick = NULL;
  512. #endif
  513. }
  514. #if defined(__MACOSX__)
  515. static SDL_bool
  516. IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
  517. {
  518. /* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
  519. if (vendor_id == USB_VENDOR_MICROSOFT) {
  520. if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
  521. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
  522. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
  523. return SDL_TRUE;
  524. }
  525. }
  526. return SDL_FALSE;
  527. }
  528. #endif
  529. static SDL_bool
  530. HIDAPI_DriverXbox360_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)
  531. {
  532. const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
  533. if (vendor_id == USB_VENDOR_NVIDIA) {
  534. /* This is the NVIDIA Shield controller which doesn't talk Xbox controller protocol */
  535. return SDL_FALSE;
  536. }
  537. if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == 0x0291 || product_id == 0x0719)) ||
  538. (type == SDL_CONTROLLER_TYPE_XBOX360 && interface_protocol == XB360W_IFACE_PROTOCOL)) {
  539. /* This is the wireless dongle, which talks a different protocol */
  540. return SDL_FALSE;
  541. }
  542. if (interface_number > 0) {
  543. /* This is the chatpad or other input interface, not the Xbox 360 interface */
  544. return SDL_FALSE;
  545. }
  546. #if defined(__MACOSX__) || defined(__WIN32__)
  547. if (vendor_id == USB_VENDOR_MICROSOFT && product_id == 0x028e && version == 1) {
  548. /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
  549. return SDL_FALSE;
  550. }
  551. #if defined(__MACOSX__)
  552. /* Wired Xbox One controllers are handled by this driver, interfacing with
  553. the 360Controller driver available from:
  554. https://github.com/360Controller/360Controller/releases
  555. Bluetooth Xbox One controllers are handled by the SDL Xbox One driver
  556. */
  557. if (IsBluetoothXboxOneController(vendor_id, product_id)) {
  558. return SDL_FALSE;
  559. }
  560. #endif
  561. return (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE);
  562. #else
  563. return (type == SDL_CONTROLLER_TYPE_XBOX360);
  564. #endif
  565. }
  566. static const char *
  567. HIDAPI_DriverXbox360_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  568. {
  569. return NULL;
  570. }
  571. static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
  572. {
  573. Uint8 mode = 0x02 + slot;
  574. const Uint8 led_packet[] = { 0x01, 0x03, mode };
  575. if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
  576. return SDL_FALSE;
  577. }
  578. return SDL_TRUE;
  579. }
  580. static SDL_bool
  581. HIDAPI_DriverXbox360_InitDevice(SDL_HIDAPI_Device *device)
  582. {
  583. return HIDAPI_JoystickConnected(device, NULL, SDL_FALSE);
  584. }
  585. static int
  586. HIDAPI_DriverXbox360_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  587. {
  588. return -1;
  589. }
  590. static void
  591. HIDAPI_DriverXbox360_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  592. {
  593. if (device->dev) {
  594. SetSlotLED(device->dev, (player_index % 4));
  595. }
  596. }
  597. static SDL_bool
  598. HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  599. {
  600. SDL_DriverXbox360_Context *ctx;
  601. int player_index;
  602. ctx = (SDL_DriverXbox360_Context *)SDL_calloc(1, sizeof(*ctx));
  603. if (!ctx) {
  604. SDL_OutOfMemory();
  605. return SDL_FALSE;
  606. }
  607. if (device->path) { /* else opened for RAWINPUT driver */
  608. device->dev = hid_open_path(device->path, 0);
  609. if (!device->dev) {
  610. SDL_SetError("Couldn't open %s", device->path);
  611. SDL_free(ctx);
  612. return SDL_FALSE;
  613. }
  614. }
  615. device->context = ctx;
  616. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  617. xinput_device_change = SDL_TRUE;
  618. ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_CORRELATE_XINPUT, SDL_TRUE);
  619. if (ctx->xinput_enabled && (WIN_LoadXInputDLL() < 0 || !XINPUTGETSTATE)) {
  620. ctx->xinput_enabled = SDL_FALSE;
  621. }
  622. ctx->xinput_slot = XUSER_INDEX_ANY;
  623. #endif
  624. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  625. HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
  626. #endif
  627. /* Set the controller LED */
  628. player_index = SDL_JoystickGetPlayerIndex(joystick);
  629. if (player_index >= 0 && device->dev) {
  630. SetSlotLED(device->dev, (player_index % 4));
  631. }
  632. /* Initialize the joystick capabilities */
  633. joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
  634. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  635. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  636. return SDL_TRUE;
  637. }
  638. static int
  639. HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  640. {
  641. #if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT)
  642. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  643. #endif
  644. #ifdef __WIN32__
  645. SDL_bool rumbled = SDL_FALSE;
  646. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  647. if (!rumbled && ctx->wgi_correlated) {
  648. WindowsGamingInputGamepadState *gamepad_state = ctx->wgi_slot;
  649. HRESULT hr;
  650. gamepad_state->vibration.LeftMotor = (DOUBLE)low_frequency_rumble / SDL_MAX_UINT16;
  651. gamepad_state->vibration.RightMotor = (DOUBLE)high_frequency_rumble / SDL_MAX_UINT16;
  652. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(gamepad_state->gamepad, gamepad_state->vibration);
  653. if (SUCCEEDED(hr)) {
  654. rumbled = SDL_TRUE;
  655. }
  656. }
  657. #endif
  658. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  659. if (!rumbled && ctx->xinput_correlated) {
  660. XINPUT_VIBRATION XVibration;
  661. if (!XINPUTSETSTATE) {
  662. return SDL_Unsupported();
  663. }
  664. XVibration.wLeftMotorSpeed = low_frequency_rumble;
  665. XVibration.wRightMotorSpeed = high_frequency_rumble;
  666. if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) == ERROR_SUCCESS) {
  667. rumbled = SDL_TRUE;
  668. } else {
  669. return SDL_SetError("XInputSetState() failed");
  670. }
  671. }
  672. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
  673. #else /* !__WIN32__ */
  674. #ifdef __MACOSX__
  675. if (IsBluetoothXboxOneController(device->vendor_id, device->product_id)) {
  676. Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
  677. rumble_packet[4] = (low_frequency_rumble >> 8);
  678. rumble_packet[5] = (high_frequency_rumble >> 8);
  679. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  680. return SDL_SetError("Couldn't send rumble packet");
  681. }
  682. } else {
  683. /* On Mac OS X the 360Controller driver uses this short report,
  684. and we need to prefix it with a magic token so hidapi passes it through untouched
  685. */
  686. Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
  687. rumble_packet[6+2] = (low_frequency_rumble >> 8);
  688. rumble_packet[6+3] = (high_frequency_rumble >> 8);
  689. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  690. return SDL_SetError("Couldn't send rumble packet");
  691. }
  692. }
  693. #else
  694. Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  695. rumble_packet[3] = (low_frequency_rumble >> 8);
  696. rumble_packet[4] = (high_frequency_rumble >> 8);
  697. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  698. return SDL_SetError("Couldn't send rumble packet");
  699. }
  700. #endif
  701. #endif /* __WIN32__ */
  702. return 0;
  703. }
  704. static int
  705. HIDAPI_DriverXbox360_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  706. {
  707. return SDL_Unsupported();
  708. }
  709. #ifdef __WIN32__
  710. /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
  711. however with this interface there is no rumble support, no guide button,
  712. and the left and right triggers are tied together as a single axis.
  713. We use XInput and Windows.Gaming.Input to make up for these shortcomings.
  714. */
  715. static void
  716. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  717. {
  718. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  719. Uint32 match_state = ctx->match_state;
  720. /* Update match_state with button bit, then fall through */
  721. # define SDL_PrivateJoystickButton(joystick, button, state) if (state) match_state |= 1 << (button); else match_state &=~(1<<(button)); SDL_PrivateJoystickButton(joystick, button, state)
  722. /* Grab high 4 bits of value, then fall through */
  723. # define SDL_PrivateJoystickAxis(joystick, axis, value) if (axis < 4) match_state = (match_state & ~(0xF << (4 * axis + 16))) | ((value) & 0xF000) << (4 * axis + 4); SDL_PrivateJoystickAxis(joystick, axis, value)
  724. #endif
  725. Sint16 axis;
  726. SDL_bool has_trigger_data = SDL_FALSE;
  727. if (ctx->last_state[10] != data[10]) {
  728. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  729. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[10] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  730. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[10] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  731. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[10] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  732. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[10] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  733. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[10] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  734. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[10] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  735. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[10] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  736. }
  737. if (ctx->last_state[11] != data[11]) {
  738. SDL_bool dpad_up = SDL_FALSE;
  739. SDL_bool dpad_down = SDL_FALSE;
  740. SDL_bool dpad_left = SDL_FALSE;
  741. SDL_bool dpad_right = SDL_FALSE;
  742. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[11] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  743. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[11] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  744. switch (data[11] & 0x3C) {
  745. case 4:
  746. dpad_up = SDL_TRUE;
  747. break;
  748. case 8:
  749. dpad_up = SDL_TRUE;
  750. dpad_right = SDL_TRUE;
  751. break;
  752. case 12:
  753. dpad_right = SDL_TRUE;
  754. break;
  755. case 16:
  756. dpad_right = SDL_TRUE;
  757. dpad_down = SDL_TRUE;
  758. break;
  759. case 20:
  760. dpad_down = SDL_TRUE;
  761. break;
  762. case 24:
  763. dpad_left = SDL_TRUE;
  764. dpad_down = SDL_TRUE;
  765. break;
  766. case 28:
  767. dpad_left = SDL_TRUE;
  768. break;
  769. case 32:
  770. dpad_up = SDL_TRUE;
  771. dpad_left = SDL_TRUE;
  772. break;
  773. default:
  774. break;
  775. }
  776. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  777. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  778. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  779. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  780. }
  781. axis = (int)*(Uint16*)(&data[0]) - 0x8000;
  782. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  783. axis = (int)*(Uint16*)(&data[2]) - 0x8000;
  784. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  785. axis = (int)*(Uint16*)(&data[4]) - 0x8000;
  786. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  787. axis = (int)*(Uint16*)(&data[6]) - 0x8000;
  788. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  789. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  790. #undef SDL_PrivateJoystickAxis
  791. #endif
  792. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  793. /* Prefer XInput over WindowsGamingInput, it continues to provide data in the background */
  794. if (!has_trigger_data && ctx->xinput_enabled && ctx->xinput_correlated) {
  795. has_trigger_data = SDL_TRUE;
  796. }
  797. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
  798. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  799. if (!has_trigger_data && ctx->wgi_correlated) {
  800. has_trigger_data = SDL_TRUE;
  801. }
  802. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
  803. if (!has_trigger_data) {
  804. axis = (data[9] * 257) - 32768;
  805. if (data[9] < 0x80) {
  806. axis = -axis * 2 - 32769;
  807. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
  808. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  809. } else if (data[9] > 0x80) {
  810. axis = axis * 2 - 32767;
  811. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  812. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
  813. } else {
  814. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
  815. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
  816. }
  817. }
  818. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  819. ctx->match_state = match_state;
  820. ctx->last_state_packet = SDL_GetTicks();
  821. #undef SDL_PrivateJoystickButton
  822. #endif
  823. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  824. }
  825. #ifdef SDL_JOYSTICK_RAWINPUT
  826. static void
  827. HIDAPI_DriverXbox360_HandleStatePacketFromRAWINPUT(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 *data, int size)
  828. {
  829. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  830. HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, data, size);
  831. }
  832. #endif
  833. #else
  834. static void
  835. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  836. {
  837. Sint16 axis;
  838. #ifdef __MACOSX__
  839. const SDL_bool invert_y_axes = SDL_FALSE;
  840. #else
  841. const SDL_bool invert_y_axes = SDL_TRUE;
  842. #endif
  843. if (ctx->last_state[2] != data[2]) {
  844. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  845. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  846. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  847. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  848. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  849. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  850. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  851. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  852. }
  853. if (ctx->last_state[3] != data[3]) {
  854. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  855. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  856. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  857. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  858. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  859. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  860. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  861. }
  862. axis = ((int)data[4] * 257) - 32768;
  863. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  864. axis = ((int)data[5] * 257) - 32768;
  865. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  866. axis = *(Sint16*)(&data[6]);
  867. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  868. axis = *(Sint16*)(&data[8]);
  869. if (invert_y_axes) {
  870. axis = ~axis;
  871. }
  872. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  873. axis = *(Sint16*)(&data[10]);
  874. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  875. axis = *(Sint16*)(&data[12]);
  876. if (invert_y_axes) {
  877. axis = ~axis;
  878. }
  879. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  880. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  881. }
  882. #endif /* __WIN32__ */
  883. static void
  884. HIDAPI_DriverXbox360_UpdateOtherAPIs(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  885. {
  886. #if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
  887. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  888. SDL_bool has_trigger_data = SDL_FALSE;
  889. SDL_bool correlated = SDL_FALSE;
  890. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  891. WindowsMatchState match_state_xinput;
  892. #endif
  893. /* Poll for trigger data once (not per-state-packet) */
  894. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  895. /* Prefer XInput over WindowsGamingInput, it continues to provide data in the background */
  896. if (!has_trigger_data && ctx->xinput_enabled && ctx->xinput_correlated) {
  897. HIDAPI_DriverXbox360_UpdateXInput();
  898. if (xinput_state[ctx->xinput_slot].connected) {
  899. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state[ctx->xinput_slot].state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
  900. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state[ctx->xinput_slot].state.Gamepad.bLeftTrigger * 257) - 32768);
  901. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state[ctx->xinput_slot].state.Gamepad.bRightTrigger * 257) - 32768);
  902. has_trigger_data = SDL_TRUE;
  903. }
  904. }
  905. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
  906. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  907. if (!has_trigger_data && ctx->wgi_correlated) {
  908. HIDAPI_DriverXbox360_UpdateWindowsGamingInput(); /* May detect disconnect / cause uncorrelation */
  909. if (ctx->wgi_correlated) { /* Still connected */
  910. struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading *state = &ctx->wgi_slot->state;
  911. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (state->Buttons & GamepadButtons_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
  912. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)(state->LeftTrigger * SDL_MAX_UINT16)) - 32768);
  913. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)(state->RightTrigger * SDL_MAX_UINT16)) - 32768);
  914. has_trigger_data = SDL_TRUE;
  915. }
  916. }
  917. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
  918. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  919. HIDAPI_DriverXbox360_FillMatchState(&match_state_xinput, ctx->match_state);
  920. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  921. /* Parallel logic to WINDOWS_XINPUT below */
  922. HIDAPI_DriverXbox360_UpdateWindowsGamingInput();
  923. if (ctx->wgi_correlated) {
  924. /* We have been previously correlated, ensure we are still matching, see comments in XINPUT section */
  925. if (HIDAPI_DriverXbox360_WindowsGamingInputSlotMatches(&match_state_xinput, ctx->wgi_slot)) {
  926. ctx->wgi_uncorrelate_count = 0;
  927. } else {
  928. ++ctx->wgi_uncorrelate_count;
  929. /* Only un-correlate if this is consistent over multiple Update() calls - the timing of polling/event
  930. pumping can easily cause this to uncorrelate for a frame. 2 seemed reliable in my testing, but
  931. let's set it to 3 to be safe. An incorrect un-correlation will simply result in lower precision
  932. triggers for a frame. */
  933. if (ctx->wgi_uncorrelate_count >= 3) {
  934. #ifdef DEBUG_JOYSTICK
  935. SDL_Log("UN-Correlated joystick %d to WindowsGamingInput device #%d\n", joystick->instance_id, ctx->wgi_slot);
  936. #endif
  937. HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotFree(ctx->wgi_slot);
  938. ctx->wgi_correlated = SDL_FALSE;
  939. ctx->wgi_correlation_count = 0;
  940. /* Force immediate update of triggers */
  941. HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
  942. /* Force release of Guide button, it can't possibly be down on this device now. */
  943. /* It gets left down if we were actually correlated incorrectly and it was released on the WindowsGamingInput
  944. device but we didn't get a state packet. */
  945. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
  946. }
  947. }
  948. }
  949. if (!ctx->wgi_correlated) {
  950. SDL_bool new_correlation_count = 0;
  951. if (HIDAPI_DriverXbox360_MissingWindowsGamingInputSlot()) {
  952. Uint8 correlation_id;
  953. WindowsGamingInputGamepadState *slot_idx;
  954. if (HIDAPI_DriverXbox360_GuessWindowsGamingInputSlot(&match_state_xinput, &correlation_id, &slot_idx)) {
  955. /* we match exactly one WindowsGamingInput device */
  956. /* Probably can do without wgi_correlation_count, just check and clear wgi_slot to NULL, unless we need
  957. even more frames to be sure. */
  958. if (ctx->wgi_correlation_count && ctx->wgi_slot == slot_idx) {
  959. /* was correlated previously, and still the same device */
  960. if (ctx->wgi_correlation_id + 1 == correlation_id) {
  961. /* no one else was correlated in the meantime */
  962. new_correlation_count = ctx->wgi_correlation_count + 1;
  963. if (new_correlation_count == 2) {
  964. /* correlation stayed steady and uncontested across multiple frames, guaranteed match */
  965. ctx->wgi_correlated = SDL_TRUE;
  966. #ifdef DEBUG_JOYSTICK
  967. SDL_Log("Correlated joystick %d to WindowsGamingInput device #%d\n", joystick->instance_id, slot_idx);
  968. #endif
  969. correlated = SDL_TRUE;
  970. HIDAPI_DriverXbox360_MarkWindowsGamingInputSlotUsed(ctx->wgi_slot, ctx);
  971. /* If the generalized Guide button was using us, it doesn't need to anymore */
  972. if (guide_button_candidate.joystick == joystick)
  973. guide_button_candidate.joystick = NULL;
  974. if (guide_button_candidate.last_joystick == joystick)
  975. guide_button_candidate.last_joystick = NULL;
  976. /* Force immediate update of guide button / triggers */
  977. HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
  978. }
  979. } else {
  980. /* someone else also possibly correlated to this device, start over */
  981. new_correlation_count = 1;
  982. }
  983. } else {
  984. /* new possible correlation */
  985. new_correlation_count = 1;
  986. ctx->wgi_slot = slot_idx;
  987. }
  988. ctx->wgi_correlation_id = correlation_id;
  989. } else {
  990. /* Match multiple WindowsGamingInput devices, or none (possibly due to no buttons pressed) */
  991. }
  992. }
  993. ctx->wgi_correlation_count = new_correlation_count;
  994. } else {
  995. correlated = SDL_TRUE;
  996. }
  997. #endif
  998. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  999. /* Parallel logic to WINDOWS_GAMING_INPUT above */
  1000. if (ctx->xinput_enabled) {
  1001. HIDAPI_DriverXbox360_UpdateXInput();
  1002. if (ctx->xinput_correlated) {
  1003. /* We have been previously correlated, ensure we are still matching */
  1004. /* This is required to deal with two (mostly) un-preventable mis-correlation situations:
  1005. A) Since the HID data stream does not provide an initial state (but polling XInput does), if we open
  1006. 5 controllers (#1-4 XInput mapped, #5 is not), and controller 1 had the A button down (and we don't
  1007. know), and the user presses A on controller #5, we'll see exactly 1 controller with A down (#5) and
  1008. exactly 1 XInput device with A down (#1), and incorrectly correlate. This code will then un-correlate
  1009. when A is released from either controller #1 or #5.
  1010. B) Since the app may not open all controllers, we could have a similar situation where only controller #5
  1011. is opened, and the user holds A on controllers #1 and #5 simultaneously - again we see only 1 controller
  1012. with A down and 1 XInput device with A down, and incorrectly correlate. This should be very unusual
  1013. (only when apps do not open all controllers, yet are listening to Guide button presses, yet
  1014. for some reason want to ignore guide button presses on the un-opened controllers, yet users are
  1015. pressing buttons on the unopened controllers), and will resolve itself when either button is released
  1016. and we un-correlate. We could prevent this by processing the state packets for *all* controllers,
  1017. even un-opened ones, as that would allow more precise correlation.
  1018. */
  1019. if (HIDAPI_DriverXbox360_XInputSlotMatches(&match_state_xinput, ctx->xinput_slot)) {
  1020. ctx->xinput_uncorrelate_count = 0;
  1021. } else {
  1022. ++ctx->xinput_uncorrelate_count;
  1023. /* Only un-correlate if this is consistent over multiple Update() calls - the timing of polling/event
  1024. pumping can easily cause this to uncorrelate for a frame. 2 seemed reliable in my testing, but
  1025. let's set it to 3 to be safe. An incorrect un-correlation will simply result in lower precision
  1026. triggers for a frame. */
  1027. if (ctx->xinput_uncorrelate_count >= 3) {
  1028. #ifdef DEBUG_JOYSTICK
  1029. SDL_Log("UN-Correlated joystick %d to XInput device #%d\n", joystick->instance_id, ctx->xinput_slot);
  1030. #endif
  1031. HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
  1032. ctx->xinput_correlated = SDL_FALSE;
  1033. ctx->xinput_correlation_count = 0;
  1034. /* Force immediate update of triggers */
  1035. HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
  1036. /* Force release of Guide button, it can't possibly be down on this device now. */
  1037. /* It gets left down if we were actually correlated incorrectly and it was released on the XInput
  1038. device but we didn't get a state packet. */
  1039. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
  1040. }
  1041. }
  1042. }
  1043. if (!ctx->xinput_correlated) {
  1044. SDL_bool new_correlation_count = 0;
  1045. if (HIDAPI_DriverXbox360_MissingXInputSlot()) {
  1046. Uint8 correlation_id = 0;
  1047. Uint8 slot_idx = 0;
  1048. if (HIDAPI_DriverXbox360_GuessXInputSlot(&match_state_xinput, &correlation_id, &slot_idx)) {
  1049. /* we match exactly one XInput device */
  1050. /* Probably can do without xinput_correlation_count, just check and clear xinput_slot to ANY, unless
  1051. we need even more frames to be sure */
  1052. if (ctx->xinput_correlation_count && ctx->xinput_slot == slot_idx) {
  1053. /* was correlated previously, and still the same device */
  1054. if (ctx->xinput_correlation_id + 1 == correlation_id) {
  1055. /* no one else was correlated in the meantime */
  1056. new_correlation_count = ctx->xinput_correlation_count + 1;
  1057. if (new_correlation_count == 2) {
  1058. /* correlation stayed steady and uncontested across multiple frames, guaranteed match */
  1059. ctx->xinput_correlated = SDL_TRUE;
  1060. #ifdef DEBUG_JOYSTICK
  1061. SDL_Log("Correlated joystick %d to XInput device #%d\n", joystick->instance_id, slot_idx);
  1062. #endif
  1063. correlated = SDL_TRUE;
  1064. HIDAPI_DriverXbox360_MarkXInputSlotUsed(ctx->xinput_slot);
  1065. /* If the generalized Guide button was using us, it doesn't need to anymore */
  1066. if (guide_button_candidate.joystick == joystick)
  1067. guide_button_candidate.joystick = NULL;
  1068. if (guide_button_candidate.last_joystick == joystick)
  1069. guide_button_candidate.last_joystick = NULL;
  1070. /* Force immediate update of guide button / triggers */
  1071. HIDAPI_DriverXbox360_HandleStatePacket(joystick, NULL, ctx, ctx->last_state, sizeof(ctx->last_state));
  1072. }
  1073. } else {
  1074. /* someone else also possibly correlated to this device, start over */
  1075. new_correlation_count = 1;
  1076. }
  1077. } else {
  1078. /* new possible correlation */
  1079. new_correlation_count = 1;
  1080. ctx->xinput_slot = slot_idx;
  1081. }
  1082. ctx->xinput_correlation_id = correlation_id;
  1083. } else {
  1084. /* Match multiple XInput devices, or none (possibly due to no buttons pressed) */
  1085. }
  1086. }
  1087. ctx->xinput_correlation_count = new_correlation_count;
  1088. } else {
  1089. correlated = SDL_TRUE;
  1090. }
  1091. }
  1092. #endif
  1093. if (!correlated) {
  1094. if (!guide_button_candidate.joystick ||
  1095. (ctx->last_state_packet && (
  1096. !guide_button_candidate.last_state_packet ||
  1097. SDL_TICKS_PASSED(ctx->last_state_packet, guide_button_candidate.last_state_packet)
  1098. ))
  1099. ) {
  1100. guide_button_candidate.joystick = joystick;
  1101. guide_button_candidate.last_state_packet = ctx->last_state_packet;
  1102. }
  1103. }
  1104. #endif
  1105. #endif
  1106. }
  1107. static SDL_bool
  1108. HIDAPI_DriverXbox360_UpdateDevice(SDL_HIDAPI_Device *device)
  1109. {
  1110. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  1111. SDL_Joystick *joystick = NULL;
  1112. Uint8 data[USB_PACKET_LENGTH];
  1113. int size = 0;
  1114. if (device->num_joysticks > 0) {
  1115. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  1116. }
  1117. if (!joystick) {
  1118. return SDL_FALSE;
  1119. }
  1120. while (device->dev && (size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  1121. HIDAPI_DriverXbox360_HandleStatePacket(joystick, device->dev, ctx, data, size);
  1122. }
  1123. if (size < 0) {
  1124. /* Read error, device is disconnected */
  1125. HIDAPI_JoystickDisconnected(device, joystick->instance_id, SDL_FALSE);
  1126. } else {
  1127. HIDAPI_DriverXbox360_UpdateOtherAPIs(device, joystick);
  1128. }
  1129. return (size >= 0);
  1130. }
  1131. static void
  1132. HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1133. {
  1134. #if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
  1135. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  1136. #endif
  1137. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_MATCHING
  1138. if (guide_button_candidate.joystick == joystick)
  1139. guide_button_candidate.joystick = NULL;
  1140. if (guide_button_candidate.last_joystick == joystick)
  1141. guide_button_candidate.last_joystick = NULL;
  1142. #endif
  1143. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  1144. xinput_device_change = SDL_TRUE;
  1145. if (ctx->xinput_enabled) {
  1146. if (ctx->xinput_correlated) {
  1147. HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
  1148. }
  1149. WIN_UnloadXInputDLL();
  1150. }
  1151. #endif
  1152. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  1153. HIDAPI_DriverXbox360_QuitWindowsGamingInput(ctx);
  1154. #endif
  1155. if (device->dev) {
  1156. hid_close(device->dev);
  1157. device->dev = NULL;
  1158. }
  1159. SDL_free(device->context);
  1160. device->context = NULL;
  1161. }
  1162. static void
  1163. HIDAPI_DriverXbox360_FreeDevice(SDL_HIDAPI_Device *device)
  1164. {
  1165. }
  1166. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
  1167. {
  1168. SDL_HINT_JOYSTICK_HIDAPI_XBOX,
  1169. SDL_TRUE,
  1170. HIDAPI_DriverXbox360_IsSupportedDevice,
  1171. HIDAPI_DriverXbox360_GetDeviceName,
  1172. HIDAPI_DriverXbox360_InitDevice,
  1173. HIDAPI_DriverXbox360_GetDevicePlayerIndex,
  1174. HIDAPI_DriverXbox360_SetDevicePlayerIndex,
  1175. HIDAPI_DriverXbox360_UpdateDevice,
  1176. HIDAPI_DriverXbox360_OpenJoystick,
  1177. HIDAPI_DriverXbox360_RumbleJoystick,
  1178. HIDAPI_DriverXbox360_SetJoystickLED,
  1179. HIDAPI_DriverXbox360_CloseJoystick,
  1180. HIDAPI_DriverXbox360_FreeDevice,
  1181. HIDAPI_DriverXbox360_PostUpdate,
  1182. #ifdef SDL_JOYSTICK_RAWINPUT
  1183. HIDAPI_DriverXbox360_HandleStatePacketFromRAWINPUT,
  1184. #endif
  1185. };
  1186. #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */
  1187. #endif /* SDL_JOYSTICK_HIDAPI */
  1188. /* vi: set ts=4 sw=4 expandtab: */