SDL_xinputjoystick.c 20 KB

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