SDL_xinputjoystick.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #include "../SDL_sysjoystick.h"
  20. #if SDL_JOYSTICK_XINPUT
  21. #include "SDL_hints.h"
  22. #include "SDL_timer.h"
  23. #include "SDL_windowsjoystick_c.h"
  24. #include "SDL_xinputjoystick_c.h"
  25. #include "SDL_rawinputjoystick_c.h"
  26. #include "../hidapi/SDL_hidapijoystick_c.h"
  27. /*
  28. * Internal stuff.
  29. */
  30. static SDL_bool s_bXInputEnabled = SDL_TRUE;
  31. static char *s_arrXInputDevicePath[XUSER_MAX_COUNT];
  32. static SDL_bool
  33. SDL_XInputUseOldJoystickMapping()
  34. {
  35. #ifdef __WINRT__
  36. /* TODO: remove this __WINRT__ block, but only after integrating with UWP/WinRT's HID API */
  37. /* FIXME: Why are Win8/10 different here? -flibit */
  38. return (NTDDI_VERSION < NTDDI_WIN10);
  39. #else
  40. static int s_XInputUseOldJoystickMapping = -1;
  41. if (s_XInputUseOldJoystickMapping < 0) {
  42. s_XInputUseOldJoystickMapping = SDL_GetHintBoolean(SDL_HINT_XINPUT_USE_OLD_JOYSTICK_MAPPING, SDL_FALSE);
  43. }
  44. return (s_XInputUseOldJoystickMapping > 0);
  45. #endif
  46. }
  47. SDL_bool SDL_XINPUT_Enabled(void)
  48. {
  49. return s_bXInputEnabled;
  50. }
  51. int
  52. SDL_XINPUT_JoystickInit(void)
  53. {
  54. s_bXInputEnabled = SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE);
  55. #ifdef SDL_JOYSTICK_RAWINPUT
  56. if (RAWINPUT_IsEnabled()) {
  57. /* The raw input driver handles more than 4 controllers, so prefer that when available */
  58. s_bXInputEnabled = SDL_FALSE;
  59. }
  60. #endif
  61. if (s_bXInputEnabled && WIN_LoadXInputDLL() < 0) {
  62. s_bXInputEnabled = SDL_FALSE; /* oh well. */
  63. }
  64. return 0;
  65. }
  66. static const char *
  67. GetXInputName(const Uint8 userid, BYTE SubType)
  68. {
  69. static char name[32];
  70. if (SDL_XInputUseOldJoystickMapping()) {
  71. SDL_snprintf(name, sizeof(name), "X360 Controller #%u", 1 + userid);
  72. } else {
  73. switch (SubType) {
  74. case XINPUT_DEVSUBTYPE_GAMEPAD:
  75. SDL_snprintf(name, sizeof(name), "XInput Controller #%u", 1 + userid);
  76. break;
  77. case XINPUT_DEVSUBTYPE_WHEEL:
  78. SDL_snprintf(name, sizeof(name), "XInput Wheel #%u", 1 + userid);
  79. break;
  80. case XINPUT_DEVSUBTYPE_ARCADE_STICK:
  81. SDL_snprintf(name, sizeof(name), "XInput ArcadeStick #%u", 1 + userid);
  82. break;
  83. case XINPUT_DEVSUBTYPE_FLIGHT_STICK:
  84. SDL_snprintf(name, sizeof(name), "XInput FlightStick #%u", 1 + userid);
  85. break;
  86. case XINPUT_DEVSUBTYPE_DANCE_PAD:
  87. SDL_snprintf(name, sizeof(name), "XInput DancePad #%u", 1 + userid);
  88. break;
  89. case XINPUT_DEVSUBTYPE_GUITAR:
  90. case XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE:
  91. case XINPUT_DEVSUBTYPE_GUITAR_BASS:
  92. SDL_snprintf(name, sizeof(name), "XInput Guitar #%u", 1 + userid);
  93. break;
  94. case XINPUT_DEVSUBTYPE_DRUM_KIT:
  95. SDL_snprintf(name, sizeof(name), "XInput DrumKit #%u", 1 + userid);
  96. break;
  97. case XINPUT_DEVSUBTYPE_ARCADE_PAD:
  98. SDL_snprintf(name, sizeof(name), "XInput ArcadePad #%u", 1 + userid);
  99. break;
  100. default:
  101. SDL_snprintf(name, sizeof(name), "XInput Device #%u", 1 + userid);
  102. break;
  103. }
  104. }
  105. return name;
  106. }
  107. /* We can't really tell what device is being used for XInput, but we can guess
  108. and we'll be correct for the case where only one device is connected.
  109. */
  110. static void
  111. GuessXInputDevice(Uint8 userid, Uint16 *pVID, Uint16 *pPID, Uint16 *pVersion)
  112. {
  113. #ifndef __WINRT__ /* TODO: remove this ifndef __WINRT__ block, but only after integrating with UWP/WinRT's HID API */
  114. PRAWINPUTDEVICELIST devices = NULL;
  115. UINT i, j, device_count = 0;
  116. if ((GetRawInputDeviceList(NULL, &device_count, sizeof(RAWINPUTDEVICELIST)) == -1) || (!device_count)) {
  117. return; /* oh well. */
  118. }
  119. devices = (PRAWINPUTDEVICELIST)SDL_malloc(sizeof(RAWINPUTDEVICELIST) * device_count);
  120. if (devices == NULL) {
  121. return;
  122. }
  123. if (GetRawInputDeviceList(devices, &device_count, sizeof(RAWINPUTDEVICELIST)) == -1) {
  124. SDL_free(devices);
  125. return; /* oh well. */
  126. }
  127. /* First see if we have a cached entry for this index */
  128. if (s_arrXInputDevicePath[userid]) {
  129. for (i = 0; i < device_count; i++) {
  130. RID_DEVICE_INFO rdi;
  131. char devName[128];
  132. UINT rdiSize = sizeof(rdi);
  133. UINT nameSize = SDL_arraysize(devName);
  134. rdi.cbSize = sizeof(rdi);
  135. if (devices[i].dwType == RIM_TYPEHID &&
  136. GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1 &&
  137. GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICENAME, devName, &nameSize) != (UINT)-1) {
  138. if (SDL_strcmp(devName, s_arrXInputDevicePath[userid]) == 0) {
  139. *pVID = (Uint16)rdi.hid.dwVendorId;
  140. *pPID = (Uint16)rdi.hid.dwProductId;
  141. *pVersion = (Uint16)rdi.hid.dwVersionNumber;
  142. SDL_free(devices);
  143. return;
  144. }
  145. }
  146. }
  147. }
  148. for (i = 0; i < device_count; i++) {
  149. RID_DEVICE_INFO rdi;
  150. char devName[MAX_PATH];
  151. UINT rdiSize = sizeof(rdi);
  152. UINT nameSize = SDL_arraysize(devName);
  153. rdi.cbSize = sizeof(rdi);
  154. if (devices[i].dwType == RIM_TYPEHID &&
  155. GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICEINFO, &rdi, &rdiSize) != (UINT)-1 &&
  156. GetRawInputDeviceInfoA(devices[i].hDevice, RIDI_DEVICENAME, devName, &nameSize) != (UINT)-1) {
  157. #ifdef DEBUG_JOYSTICK
  158. SDL_Log("Raw input device: VID = 0x%x, PID = 0x%x, %s\n", rdi.hid.dwVendorId, rdi.hid.dwProductId, devName);
  159. #endif
  160. if (SDL_strstr(devName, "IG_") != NULL) {
  161. SDL_bool found = SDL_FALSE;
  162. for (j = 0; j < SDL_arraysize(s_arrXInputDevicePath); ++j) {
  163. if (!s_arrXInputDevicePath[j]) {
  164. continue;
  165. }
  166. if (SDL_strcmp(devName, s_arrXInputDevicePath[j]) == 0) {
  167. found = SDL_TRUE;
  168. break;
  169. }
  170. }
  171. if (found) {
  172. /* We already have this device in our XInput device list */
  173. continue;
  174. }
  175. /* We don't actually know if this is the right device for this
  176. * userid, but we'll record it so we'll at least be consistent
  177. * when the raw device list changes.
  178. */
  179. *pVID = (Uint16)rdi.hid.dwVendorId;
  180. *pPID = (Uint16)rdi.hid.dwProductId;
  181. *pVersion = (Uint16)rdi.hid.dwVersionNumber;
  182. if (s_arrXInputDevicePath[userid]) {
  183. SDL_free(s_arrXInputDevicePath[userid]);
  184. }
  185. s_arrXInputDevicePath[userid] = SDL_strdup(devName);
  186. SDL_free(devices);
  187. return;
  188. }
  189. }
  190. }
  191. SDL_free(devices);
  192. #endif /* !__WINRT__ */
  193. /* The device wasn't in the raw HID device list, it's probably Bluetooth */
  194. *pVID = 0x045e; /* Microsoft */
  195. *pPID = 0x02fd; /* XBox One S Bluetooth */
  196. *pVersion = 0;
  197. }
  198. static void
  199. AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pContext)
  200. {
  201. Uint16 vendor = 0;
  202. Uint16 product = 0;
  203. Uint16 version = 0;
  204. JoyStick_DeviceData *pPrevJoystick = NULL;
  205. JoyStick_DeviceData *pNewJoystick = *pContext;
  206. if (SDL_XInputUseOldJoystickMapping() && SubType != XINPUT_DEVSUBTYPE_GAMEPAD)
  207. return;
  208. if (SubType == XINPUT_DEVSUBTYPE_UNKNOWN)
  209. return;
  210. while (pNewJoystick) {
  211. if (pNewJoystick->bXInputDevice && (pNewJoystick->XInputUserId == userid) && (pNewJoystick->SubType == SubType)) {
  212. /* if we are replacing the front of the list then update it */
  213. if (pNewJoystick == *pContext) {
  214. *pContext = pNewJoystick->pNext;
  215. } else if (pPrevJoystick) {
  216. pPrevJoystick->pNext = pNewJoystick->pNext;
  217. }
  218. pNewJoystick->pNext = SYS_Joystick;
  219. SYS_Joystick = pNewJoystick;
  220. return; /* already in the list. */
  221. }
  222. pPrevJoystick = pNewJoystick;
  223. pNewJoystick = pNewJoystick->pNext;
  224. }
  225. pNewJoystick = (JoyStick_DeviceData *)SDL_calloc(1, sizeof(JoyStick_DeviceData));
  226. if (!pNewJoystick) {
  227. return; /* better luck next time? */
  228. }
  229. pNewJoystick->bXInputDevice = SDL_TRUE;
  230. if (!SDL_XInputUseOldJoystickMapping()) {
  231. Uint16 *guid16 = (Uint16 *)pNewJoystick->guid.data;
  232. GuessXInputDevice(userid, &vendor, &product, &version);
  233. *guid16++ = SDL_SwapLE16(SDL_HARDWARE_BUS_USB);
  234. *guid16++ = 0;
  235. *guid16++ = SDL_SwapLE16(vendor);
  236. *guid16++ = 0;
  237. *guid16++ = SDL_SwapLE16(product);
  238. *guid16++ = 0;
  239. *guid16++ = SDL_SwapLE16(version);
  240. *guid16++ = 0;
  241. /* Note that this is an XInput device and what subtype it is */
  242. pNewJoystick->guid.data[14] = 'x';
  243. pNewJoystick->guid.data[15] = SubType;
  244. }
  245. pNewJoystick->SubType = SubType;
  246. pNewJoystick->XInputUserId = userid;
  247. pNewJoystick->joystickname = SDL_CreateJoystickName(vendor, product, NULL, GetXInputName(userid, SubType));
  248. if (!pNewJoystick->joystickname) {
  249. SDL_free(pNewJoystick);
  250. return; /* better luck next time? */
  251. }
  252. if (SDL_ShouldIgnoreJoystick(pNewJoystick->joystickname, pNewJoystick->guid)) {
  253. SDL_free(pNewJoystick);
  254. return;
  255. }
  256. #ifdef SDL_JOYSTICK_HIDAPI
  257. /* Since we're guessing about the VID/PID, use a hard-coded VID/PID to represent XInput */
  258. if (HIDAPI_IsDevicePresent(USB_VENDOR_MICROSOFT, USB_PRODUCT_XBOX_ONE_XINPUT_CONTROLLER, version, pNewJoystick->joystickname)) {
  259. /* The HIDAPI driver is taking care of this device */
  260. SDL_free(pNewJoystick);
  261. return;
  262. }
  263. #endif
  264. #ifdef SDL_JOYSTICK_RAWINPUT
  265. if (RAWINPUT_IsDevicePresent(vendor, product, version, pNewJoystick->joystickname)) {
  266. /* The RAWINPUT driver is taking care of this device */
  267. SDL_free(pNewJoystick);
  268. return;
  269. }
  270. #endif
  271. WINDOWS_AddJoystickDevice(pNewJoystick);
  272. }
  273. static void
  274. DelXInputDevice(Uint8 userid)
  275. {
  276. if (s_arrXInputDevicePath[userid]) {
  277. SDL_free(s_arrXInputDevicePath[userid]);
  278. s_arrXInputDevicePath[userid] = NULL;
  279. }
  280. }
  281. void
  282. SDL_XINPUT_JoystickDetect(JoyStick_DeviceData **pContext)
  283. {
  284. int iuserid;
  285. if (!s_bXInputEnabled) {
  286. return;
  287. }
  288. /* iterate in reverse, so these are in the final list in ascending numeric order. */
  289. for (iuserid = XUSER_MAX_COUNT - 1; iuserid >= 0; iuserid--) {
  290. const Uint8 userid = (Uint8)iuserid;
  291. XINPUT_CAPABILITIES capabilities;
  292. if (XINPUTGETCAPABILITIES(userid, XINPUT_FLAG_GAMEPAD, &capabilities) == ERROR_SUCCESS) {
  293. /* Adding a new device, must handle all removes first, or GuessXInputDevice goes terribly wrong (returns
  294. a product/vendor ID that is not even attached to the system) when we get a remove and add on the same tick
  295. (e.g. when disconnecting a device and the OS reassigns which userid an already-attached controller is)
  296. */
  297. int iuserid2;
  298. for (iuserid2 = iuserid - 1; iuserid2 >= 0; iuserid2--) {
  299. const Uint8 userid2 = (Uint8)iuserid2;
  300. XINPUT_CAPABILITIES capabilities2;
  301. if (XINPUTGETCAPABILITIES(userid2, XINPUT_FLAG_GAMEPAD, &capabilities2) != ERROR_SUCCESS) {
  302. DelXInputDevice(userid2);
  303. }
  304. }
  305. AddXInputDevice(userid, capabilities.SubType, pContext);
  306. } else {
  307. DelXInputDevice(userid);
  308. }
  309. }
  310. }
  311. int
  312. SDL_XINPUT_JoystickOpen(SDL_Joystick * joystick, JoyStick_DeviceData *joystickdevice)
  313. {
  314. const Uint8 userId = joystickdevice->XInputUserId;
  315. XINPUT_CAPABILITIES capabilities;
  316. XINPUT_VIBRATION state;
  317. SDL_assert(s_bXInputEnabled);
  318. SDL_assert(XINPUTGETCAPABILITIES);
  319. SDL_assert(XINPUTSETSTATE);
  320. SDL_assert(userId < XUSER_MAX_COUNT);
  321. joystick->hwdata->bXInputDevice = SDL_TRUE;
  322. if (XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities) != ERROR_SUCCESS) {
  323. SDL_free(joystick->hwdata);
  324. joystick->hwdata = NULL;
  325. return SDL_SetError("Failed to obtain XInput device capabilities. Device disconnected?");
  326. }
  327. SDL_zero(state);
  328. joystick->hwdata->bXInputHaptic = (XINPUTSETSTATE(userId, &state) == ERROR_SUCCESS);
  329. joystick->hwdata->userid = userId;
  330. /* The XInput API has a hard coded button/axis mapping, so we just match it */
  331. if (SDL_XInputUseOldJoystickMapping()) {
  332. joystick->naxes = 6;
  333. joystick->nbuttons = 15;
  334. } else {
  335. joystick->naxes = 6;
  336. joystick->nbuttons = 11;
  337. joystick->nhats = 1;
  338. }
  339. return 0;
  340. }
  341. static void
  342. UpdateXInputJoystickBatteryInformation(SDL_Joystick * joystick, XINPUT_BATTERY_INFORMATION_EX *pBatteryInformation)
  343. {
  344. if (pBatteryInformation->BatteryType != BATTERY_TYPE_UNKNOWN) {
  345. SDL_JoystickPowerLevel ePowerLevel = SDL_JOYSTICK_POWER_UNKNOWN;
  346. if (pBatteryInformation->BatteryType == BATTERY_TYPE_WIRED) {
  347. ePowerLevel = SDL_JOYSTICK_POWER_WIRED;
  348. } else {
  349. switch (pBatteryInformation->BatteryLevel) {
  350. case BATTERY_LEVEL_EMPTY:
  351. ePowerLevel = SDL_JOYSTICK_POWER_EMPTY;
  352. break;
  353. case BATTERY_LEVEL_LOW:
  354. ePowerLevel = SDL_JOYSTICK_POWER_LOW;
  355. break;
  356. case BATTERY_LEVEL_MEDIUM:
  357. ePowerLevel = SDL_JOYSTICK_POWER_MEDIUM;
  358. break;
  359. default:
  360. case BATTERY_LEVEL_FULL:
  361. ePowerLevel = SDL_JOYSTICK_POWER_FULL;
  362. break;
  363. }
  364. }
  365. SDL_PrivateJoystickBatteryLevel(joystick, ePowerLevel);
  366. }
  367. }
  368. static void
  369. UpdateXInputJoystickState_OLD(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState, XINPUT_BATTERY_INFORMATION_EX *pBatteryInformation)
  370. {
  371. static WORD s_XInputButtons[] = {
  372. XINPUT_GAMEPAD_DPAD_UP, XINPUT_GAMEPAD_DPAD_DOWN, XINPUT_GAMEPAD_DPAD_LEFT, XINPUT_GAMEPAD_DPAD_RIGHT,
  373. XINPUT_GAMEPAD_START, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB,
  374. XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER,
  375. XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
  376. XINPUT_GAMEPAD_GUIDE
  377. };
  378. WORD wButtons = pXInputState->Gamepad.wButtons;
  379. Uint8 button;
  380. SDL_PrivateJoystickAxis(joystick, 0, (Sint16)pXInputState->Gamepad.sThumbLX);
  381. SDL_PrivateJoystickAxis(joystick, 1, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbLY)));
  382. SDL_PrivateJoystickAxis(joystick, 2, (Sint16)pXInputState->Gamepad.sThumbRX);
  383. SDL_PrivateJoystickAxis(joystick, 3, (Sint16)(-SDL_max(-32767, pXInputState->Gamepad.sThumbRY)));
  384. SDL_PrivateJoystickAxis(joystick, 4, (Sint16)(((int)pXInputState->Gamepad.bLeftTrigger * 65535 / 255) - 32768));
  385. SDL_PrivateJoystickAxis(joystick, 5, (Sint16)(((int)pXInputState->Gamepad.bRightTrigger * 65535 / 255) - 32768));
  386. for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) {
  387. SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED);
  388. }
  389. UpdateXInputJoystickBatteryInformation(joystick, pBatteryInformation);
  390. }
  391. static void
  392. UpdateXInputJoystickState(SDL_Joystick * joystick, XINPUT_STATE_EX *pXInputState, XINPUT_BATTERY_INFORMATION_EX *pBatteryInformation)
  393. {
  394. static WORD s_XInputButtons[] = {
  395. XINPUT_GAMEPAD_A, XINPUT_GAMEPAD_B, XINPUT_GAMEPAD_X, XINPUT_GAMEPAD_Y,
  396. XINPUT_GAMEPAD_LEFT_SHOULDER, XINPUT_GAMEPAD_RIGHT_SHOULDER, XINPUT_GAMEPAD_BACK, XINPUT_GAMEPAD_START,
  397. XINPUT_GAMEPAD_LEFT_THUMB, XINPUT_GAMEPAD_RIGHT_THUMB,
  398. XINPUT_GAMEPAD_GUIDE
  399. };
  400. WORD wButtons = pXInputState->Gamepad.wButtons;
  401. Uint8 button;
  402. Uint8 hat = SDL_HAT_CENTERED;
  403. SDL_PrivateJoystickAxis(joystick, 0, pXInputState->Gamepad.sThumbLX);
  404. SDL_PrivateJoystickAxis(joystick, 1, ~pXInputState->Gamepad.sThumbLY);
  405. SDL_PrivateJoystickAxis(joystick, 2, ((int)pXInputState->Gamepad.bLeftTrigger * 257) - 32768);
  406. SDL_PrivateJoystickAxis(joystick, 3, pXInputState->Gamepad.sThumbRX);
  407. SDL_PrivateJoystickAxis(joystick, 4, ~pXInputState->Gamepad.sThumbRY);
  408. SDL_PrivateJoystickAxis(joystick, 5, ((int)pXInputState->Gamepad.bRightTrigger * 257) - 32768);
  409. for (button = 0; button < SDL_arraysize(s_XInputButtons); ++button) {
  410. SDL_PrivateJoystickButton(joystick, button, (wButtons & s_XInputButtons[button]) ? SDL_PRESSED : SDL_RELEASED);
  411. }
  412. if (wButtons & XINPUT_GAMEPAD_DPAD_UP) {
  413. hat |= SDL_HAT_UP;
  414. }
  415. if (wButtons & XINPUT_GAMEPAD_DPAD_DOWN) {
  416. hat |= SDL_HAT_DOWN;
  417. }
  418. if (wButtons & XINPUT_GAMEPAD_DPAD_LEFT) {
  419. hat |= SDL_HAT_LEFT;
  420. }
  421. if (wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) {
  422. hat |= SDL_HAT_RIGHT;
  423. }
  424. SDL_PrivateJoystickHat(joystick, 0, hat);
  425. UpdateXInputJoystickBatteryInformation(joystick, pBatteryInformation);
  426. }
  427. int
  428. SDL_XINPUT_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  429. {
  430. XINPUT_VIBRATION XVibration;
  431. if (!XINPUTSETSTATE) {
  432. return SDL_Unsupported();
  433. }
  434. XVibration.wLeftMotorSpeed = low_frequency_rumble;
  435. XVibration.wRightMotorSpeed = high_frequency_rumble;
  436. if (XINPUTSETSTATE(joystick->hwdata->userid, &XVibration) != ERROR_SUCCESS) {
  437. return SDL_SetError("XInputSetState() failed");
  438. }
  439. return 0;
  440. }
  441. Uint32
  442. SDL_XINPUT_JoystickGetCapabilities(SDL_Joystick * joystick)
  443. {
  444. return SDL_JOYCAP_RUMBLE;
  445. }
  446. void
  447. SDL_XINPUT_JoystickUpdate(SDL_Joystick * joystick)
  448. {
  449. HRESULT result;
  450. XINPUT_STATE_EX XInputState;
  451. XINPUT_BATTERY_INFORMATION_EX XBatteryInformation;
  452. if (!XINPUTGETSTATE)
  453. return;
  454. result = XINPUTGETSTATE(joystick->hwdata->userid, &XInputState);
  455. if (result == ERROR_DEVICE_NOT_CONNECTED) {
  456. return;
  457. }
  458. SDL_zero(XBatteryInformation);
  459. if (XINPUTGETBATTERYINFORMATION) {
  460. result = XINPUTGETBATTERYINFORMATION(joystick->hwdata->userid, BATTERY_DEVTYPE_GAMEPAD, &XBatteryInformation);
  461. }
  462. /* only fire events if the data changed from last time */
  463. if (XInputState.dwPacketNumber && XInputState.dwPacketNumber != joystick->hwdata->dwPacketNumber) {
  464. if (SDL_XInputUseOldJoystickMapping()) {
  465. UpdateXInputJoystickState_OLD(joystick, &XInputState, &XBatteryInformation);
  466. } else {
  467. UpdateXInputJoystickState(joystick, &XInputState, &XBatteryInformation);
  468. }
  469. joystick->hwdata->dwPacketNumber = XInputState.dwPacketNumber;
  470. }
  471. }
  472. void
  473. SDL_XINPUT_JoystickClose(SDL_Joystick * joystick)
  474. {
  475. }
  476. void
  477. SDL_XINPUT_JoystickQuit(void)
  478. {
  479. if (s_bXInputEnabled) {
  480. WIN_UnloadXInputDLL();
  481. }
  482. }
  483. #else /* !SDL_JOYSTICK_XINPUT */
  484. typedef struct JoyStick_DeviceData JoyStick_DeviceData;
  485. SDL_bool SDL_XINPUT_Enabled(void)
  486. {
  487. return SDL_FALSE;
  488. }
  489. int
  490. SDL_XINPUT_JoystickInit(void)
  491. {
  492. return 0;
  493. }
  494. void
  495. SDL_XINPUT_JoystickDetect(JoyStick_DeviceData **pContext)
  496. {
  497. }
  498. int
  499. SDL_XINPUT_JoystickOpen(SDL_Joystick * joystick, JoyStick_DeviceData *joystickdevice)
  500. {
  501. return SDL_Unsupported();
  502. }
  503. int
  504. SDL_XINPUT_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  505. {
  506. return SDL_Unsupported();
  507. }
  508. Uint32
  509. SDL_XINPUT_JoystickGetCapabilities(SDL_Joystick * joystick)
  510. {
  511. return 0;
  512. }
  513. void
  514. SDL_XINPUT_JoystickUpdate(SDL_Joystick * joystick)
  515. {
  516. }
  517. void
  518. SDL_XINPUT_JoystickClose(SDL_Joystick * joystick)
  519. {
  520. }
  521. void
  522. SDL_XINPUT_JoystickQuit(void)
  523. {
  524. }
  525. #endif /* SDL_JOYSTICK_XINPUT */
  526. /* vi: set ts=4 sw=4 expandtab: */