SDL_hidapi_xbox360.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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_log.h"
  22. #include "SDL_events.h"
  23. #include "SDL_timer.h"
  24. #include "SDL_joystick.h"
  25. #include "SDL_gamecontroller.h"
  26. #include "../SDL_sysjoystick.h"
  27. #include "SDL_hidapijoystick_c.h"
  28. #include "SDL_hidapi_rumble.h"
  29. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  30. #ifdef __WIN32__
  31. #define SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  32. /* This requires the Windows 10 SDK to build */
  33. /*#define SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT*/
  34. #endif
  35. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  36. #include "../../core/windows/SDL_xinput.h"
  37. #endif
  38. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  39. #include "../../core/windows/SDL_windows.h"
  40. #define COBJMACROS
  41. #include "windows.gaming.input.h"
  42. #endif
  43. typedef struct {
  44. Uint8 last_state[USB_PACKET_LENGTH];
  45. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  46. SDL_bool xinput_enabled;
  47. Uint8 xinput_slot;
  48. #endif
  49. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  50. SDL_bool coinitialized;
  51. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics *gamepad_statics;
  52. __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
  53. struct __x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration;
  54. #endif
  55. } SDL_DriverXbox360_Context;
  56. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  57. static Uint8 xinput_slots;
  58. static void
  59. HIDAPI_DriverXbox360_MarkXInputSlotUsed(Uint8 xinput_slot)
  60. {
  61. if (xinput_slot != XUSER_INDEX_ANY) {
  62. xinput_slots |= (0x01 << xinput_slot);
  63. }
  64. }
  65. static void
  66. HIDAPI_DriverXbox360_MarkXInputSlotFree(Uint8 xinput_slot)
  67. {
  68. if (xinput_slot != XUSER_INDEX_ANY) {
  69. xinput_slots &= ~(0x01 << xinput_slot);
  70. }
  71. }
  72. static SDL_bool
  73. HIDAPI_DriverXbox360_MissingXInputSlot()
  74. {
  75. return xinput_slots != 0x0F;
  76. }
  77. static Uint8
  78. HIDAPI_DriverXbox360_GuessXInputSlot(WORD wButtons)
  79. {
  80. DWORD user_index;
  81. int match_count;
  82. Uint8 match_slot;
  83. if (!XINPUTGETSTATE) {
  84. return XUSER_INDEX_ANY;
  85. }
  86. match_count = 0;
  87. for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
  88. XINPUT_STATE_EX xinput_state;
  89. if (XINPUTGETSTATE(user_index, &xinput_state) == ERROR_SUCCESS) {
  90. if (xinput_state.Gamepad.wButtons == wButtons) {
  91. ++match_count;
  92. match_slot = (Uint8)user_index;
  93. }
  94. }
  95. }
  96. if (match_count == 1) {
  97. return match_slot;
  98. }
  99. return XUSER_INDEX_ANY;
  100. }
  101. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
  102. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  103. static void
  104. HIDAPI_DriverXbox360_InitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
  105. {
  106. /* I think this takes care of RoInitialize() in a way that is compatible with the rest of SDL */
  107. if (FAILED(WIN_CoInitialize())) {
  108. return;
  109. }
  110. ctx->coinitialized = SDL_TRUE;
  111. {
  112. static const IID SDL_IID_IGamepadStatics = { 0x8BBCE529, 0xD49C, 0x39E9, { 0x95, 0x60, 0xE4, 0x7D, 0xDE, 0x96, 0xB7, 0xC8 } };
  113. HRESULT hr;
  114. HMODULE hModule = LoadLibraryA("combase.dll");
  115. if (hModule != NULL) {
  116. typedef HRESULT (WINAPI *WindowsCreateString_t)(PCNZWCH sourceString, UINT32 length, HSTRING* string);
  117. typedef HRESULT (WINAPI *WindowsDeleteString_t)(HSTRING string);
  118. typedef HRESULT (WINAPI *RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void** factory);
  119. WindowsCreateString_t WindowsCreateStringFunc = (WindowsCreateString_t)GetProcAddress(hModule, "WindowsCreateString");
  120. WindowsDeleteString_t WindowsDeleteStringFunc = (WindowsDeleteString_t)GetProcAddress(hModule, "WindowsDeleteString");
  121. RoGetActivationFactory_t RoGetActivationFactoryFunc = (RoGetActivationFactory_t)GetProcAddress(hModule, "RoGetActivationFactory");
  122. if (WindowsCreateStringFunc && WindowsDeleteStringFunc && RoGetActivationFactoryFunc) {
  123. LPTSTR pNamespace = L"Windows.Gaming.Input.Gamepad";
  124. HSTRING hNamespaceString;
  125. hr = WindowsCreateStringFunc(pNamespace, SDL_wcslen(pNamespace), &hNamespaceString);
  126. if (SUCCEEDED(hr)) {
  127. RoGetActivationFactoryFunc(hNamespaceString, &SDL_IID_IGamepadStatics, &ctx->gamepad_statics);
  128. WindowsDeleteStringFunc(hNamespaceString);
  129. }
  130. }
  131. FreeLibrary(hModule);
  132. }
  133. }
  134. }
  135. static Uint8
  136. HIDAPI_DriverXbox360_GetGamepadButtonsForMatch(__x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad)
  137. {
  138. HRESULT hr;
  139. struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
  140. Uint8 buttons = 0;
  141. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(gamepad, &state);
  142. if (SUCCEEDED(hr)) {
  143. if (state.Buttons & GamepadButtons_A) {
  144. buttons |= (1 << SDL_CONTROLLER_BUTTON_A);
  145. }
  146. if (state.Buttons & GamepadButtons_B) {
  147. buttons |= (1 << SDL_CONTROLLER_BUTTON_B);
  148. }
  149. if (state.Buttons & GamepadButtons_X) {
  150. buttons |= (1 << SDL_CONTROLLER_BUTTON_X);
  151. }
  152. if (state.Buttons & GamepadButtons_Y) {
  153. buttons |= (1 << SDL_CONTROLLER_BUTTON_Y);
  154. }
  155. }
  156. return buttons;
  157. }
  158. static void
  159. HIDAPI_DriverXbox360_GuessGamepad(SDL_DriverXbox360_Context *ctx, Uint8 buttons)
  160. {
  161. HRESULT hr;
  162. __FIVectorView_1_Windows__CGaming__CInput__CGamepad *gamepads;
  163. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_get_Gamepads(ctx->gamepad_statics, &gamepads);
  164. if (SUCCEEDED(hr)) {
  165. unsigned int i, num_gamepads;
  166. hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_get_Size(gamepads, &num_gamepads);
  167. if (SUCCEEDED(hr)) {
  168. int match_count;
  169. unsigned int match_slot;
  170. match_count = 0;
  171. for (i = 0; i < num_gamepads; ++i) {
  172. __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
  173. hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, i, &gamepad);
  174. if (SUCCEEDED(hr)) {
  175. Uint8 gamepad_buttons = HIDAPI_DriverXbox360_GetGamepadButtonsForMatch(gamepad);
  176. if (buttons == gamepad_buttons) {
  177. ++match_count;
  178. match_slot = i;
  179. }
  180. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
  181. }
  182. }
  183. if (match_count == 1) {
  184. hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, match_slot, &ctx->gamepad);
  185. if (SUCCEEDED(hr)) {
  186. }
  187. }
  188. }
  189. __FIVectorView_1_Windows__CGaming__CInput__CGamepad_Release(gamepads);
  190. }
  191. }
  192. static void
  193. HIDAPI_DriverXbox360_QuitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
  194. {
  195. if (ctx->gamepad_statics) {
  196. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_Release(ctx->gamepad_statics);
  197. ctx->gamepad_statics = NULL;
  198. }
  199. if (ctx->gamepad) {
  200. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(ctx->gamepad);
  201. ctx->gamepad = NULL;
  202. }
  203. if (ctx->coinitialized) {
  204. WIN_CoUninitialize();
  205. ctx->coinitialized = SDL_FALSE;
  206. }
  207. }
  208. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
  209. #if defined(__MACOSX__)
  210. static SDL_bool
  211. IsBluetoothXboxOneController(Uint16 vendor_id, Uint16 product_id)
  212. {
  213. /* Check to see if it's the Xbox One S or Xbox One Elite Series 2 in Bluetooth mode */
  214. if (vendor_id == USB_VENDOR_MICROSOFT) {
  215. if (product_id == USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH ||
  216. product_id == USB_PRODUCT_XBOX_ONE_S_REV2_BLUETOOTH ||
  217. product_id == USB_PRODUCT_XBOX_ONE_ELITE_SERIES_2_BLUETOOTH) {
  218. return SDL_TRUE;
  219. }
  220. }
  221. return SDL_FALSE;
  222. }
  223. #endif
  224. static SDL_bool
  225. 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)
  226. {
  227. const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
  228. if (vendor_id == USB_VENDOR_NVIDIA) {
  229. /* This is the NVIDIA Shield controller which doesn't talk Xbox controller protocol */
  230. return SDL_FALSE;
  231. }
  232. if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == 0x0291 || product_id == 0x0719)) ||
  233. (type == SDL_CONTROLLER_TYPE_XBOX360 && interface_protocol == XB360W_IFACE_PROTOCOL)) {
  234. /* This is the wireless dongle, which talks a different protocol */
  235. return SDL_FALSE;
  236. }
  237. if (interface_number > 0) {
  238. /* This is the chatpad or other input interface, not the Xbox 360 interface */
  239. return SDL_FALSE;
  240. }
  241. #if defined(__MACOSX__) || defined(__WIN32__)
  242. if (vendor_id == USB_VENDOR_MICROSOFT && product_id == 0x028e && version == 1) {
  243. /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
  244. return SDL_FALSE;
  245. }
  246. if (vendor_id == USB_VENDOR_MICROSOFT && product_id == 0x02e0) {
  247. /* This is the old Bluetooth Xbox One S firmware, which isn't supported by this driver */
  248. return SDL_FALSE;
  249. }
  250. #if defined(__MACOSX__)
  251. /* Wired Xbox One controllers are handled by this driver, interfacing with
  252. the 360Controller driver available from:
  253. https://github.com/360Controller/360Controller/releases
  254. Bluetooth Xbox One controllers are handled by the SDL Xbox One driver
  255. */
  256. if (IsBluetoothXboxOneController(vendor_id, product_id)) {
  257. return SDL_FALSE;
  258. }
  259. #endif
  260. return (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE);
  261. #else
  262. return (type == SDL_CONTROLLER_TYPE_XBOX360);
  263. #endif
  264. }
  265. static const char *
  266. HIDAPI_DriverXbox360_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  267. {
  268. return NULL;
  269. }
  270. static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
  271. {
  272. Uint8 mode = 0x02 + slot;
  273. const Uint8 led_packet[] = { 0x01, 0x03, mode };
  274. if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
  275. return SDL_FALSE;
  276. }
  277. return SDL_TRUE;
  278. }
  279. static SDL_bool
  280. HIDAPI_DriverXbox360_InitDevice(SDL_HIDAPI_Device *device)
  281. {
  282. return HIDAPI_JoystickConnected(device, NULL);
  283. }
  284. static int
  285. HIDAPI_DriverXbox360_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  286. {
  287. return -1;
  288. }
  289. static void
  290. HIDAPI_DriverXbox360_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  291. {
  292. if (device->dev) {
  293. SetSlotLED(device->dev, (player_index % 4));
  294. }
  295. }
  296. static SDL_bool
  297. HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  298. {
  299. SDL_DriverXbox360_Context *ctx;
  300. int player_index;
  301. ctx = (SDL_DriverXbox360_Context *)SDL_calloc(1, sizeof(*ctx));
  302. if (!ctx) {
  303. SDL_OutOfMemory();
  304. return SDL_FALSE;
  305. }
  306. device->dev = hid_open_path(device->path, 0);
  307. if (!device->dev) {
  308. SDL_free(ctx);
  309. SDL_SetError("Couldn't open %s", device->path);
  310. return SDL_FALSE;
  311. }
  312. device->context = ctx;
  313. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  314. ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_XINPUT_ENABLED, SDL_TRUE);
  315. if (ctx->xinput_enabled && WIN_LoadXInputDLL() < 0) {
  316. ctx->xinput_enabled = SDL_FALSE;
  317. }
  318. ctx->xinput_slot = XUSER_INDEX_ANY;
  319. #endif
  320. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  321. HIDAPI_DriverXbox360_InitWindowsGamingInput(ctx);
  322. #endif
  323. /* Set the controller LED */
  324. player_index = SDL_JoystickGetPlayerIndex(joystick);
  325. if (player_index >= 0) {
  326. SetSlotLED(device->dev, (player_index % 4));
  327. }
  328. /* Initialize the joystick capabilities */
  329. joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
  330. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  331. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  332. return SDL_TRUE;
  333. }
  334. static int
  335. HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  336. {
  337. #if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT)
  338. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  339. #endif
  340. #ifdef __WIN32__
  341. SDL_bool rumbled = SDL_FALSE;
  342. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  343. if (!rumbled && ctx->gamepad) {
  344. HRESULT hr;
  345. ctx->vibration.LeftMotor = (DOUBLE)low_frequency_rumble / SDL_MAX_UINT16;
  346. ctx->vibration.RightMotor = (DOUBLE)high_frequency_rumble / SDL_MAX_UINT16;
  347. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(ctx->gamepad, ctx->vibration);
  348. if (SUCCEEDED(hr)) {
  349. rumbled = SDL_TRUE;
  350. }
  351. }
  352. #endif
  353. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  354. if (!rumbled && ctx->xinput_slot != XUSER_INDEX_ANY) {
  355. XINPUT_VIBRATION XVibration;
  356. if (!XINPUTSETSTATE) {
  357. return SDL_Unsupported();
  358. }
  359. XVibration.wLeftMotorSpeed = low_frequency_rumble;
  360. XVibration.wRightMotorSpeed = high_frequency_rumble;
  361. if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) == ERROR_SUCCESS) {
  362. rumbled = SDL_TRUE;
  363. } else {
  364. return SDL_SetError("XInputSetState() failed");
  365. }
  366. }
  367. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
  368. #else /* !__WIN32__ */
  369. #ifdef __MACOSX__
  370. if (IsBluetoothXboxOneController(device->vendor_id, device->product_id)) {
  371. Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
  372. rumble_packet[4] = (low_frequency_rumble >> 8);
  373. rumble_packet[5] = (high_frequency_rumble >> 8);
  374. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  375. return SDL_SetError("Couldn't send rumble packet");
  376. }
  377. } else {
  378. /* On Mac OS X the 360Controller driver uses this short report,
  379. and we need to prefix it with a magic token so hidapi passes it through untouched
  380. */
  381. Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
  382. rumble_packet[6+2] = (low_frequency_rumble >> 8);
  383. rumble_packet[6+3] = (high_frequency_rumble >> 8);
  384. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  385. return SDL_SetError("Couldn't send rumble packet");
  386. }
  387. }
  388. #else
  389. Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  390. rumble_packet[3] = (low_frequency_rumble >> 8);
  391. rumble_packet[4] = (high_frequency_rumble >> 8);
  392. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  393. return SDL_SetError("Couldn't send rumble packet");
  394. }
  395. #endif
  396. #endif /* __WIN32__ */
  397. return 0;
  398. }
  399. #ifdef __WIN32__
  400. /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
  401. however with this interface there is no rumble support, no guide button,
  402. and the left and right triggers are tied together as a single axis.
  403. We use XInput and Windows.Gaming.Input to make up for these shortcomings.
  404. */
  405. static void
  406. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  407. {
  408. Sint16 axis;
  409. SDL_bool has_trigger_data = SDL_FALSE;
  410. if (ctx->last_state[10] != data[10]) {
  411. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  412. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[10] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  413. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[10] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  414. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[10] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  415. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[10] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  416. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[10] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  417. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[10] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  418. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[10] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  419. }
  420. if (ctx->last_state[11] != data[11]) {
  421. SDL_bool dpad_up = SDL_FALSE;
  422. SDL_bool dpad_down = SDL_FALSE;
  423. SDL_bool dpad_left = SDL_FALSE;
  424. SDL_bool dpad_right = SDL_FALSE;
  425. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[11] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  426. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[11] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  427. switch (data[11] & 0x3C) {
  428. case 4:
  429. dpad_up = SDL_TRUE;
  430. break;
  431. case 8:
  432. dpad_up = SDL_TRUE;
  433. dpad_right = SDL_TRUE;
  434. break;
  435. case 12:
  436. dpad_right = SDL_TRUE;
  437. break;
  438. case 16:
  439. dpad_right = SDL_TRUE;
  440. dpad_down = SDL_TRUE;
  441. break;
  442. case 20:
  443. dpad_down = SDL_TRUE;
  444. break;
  445. case 24:
  446. dpad_left = SDL_TRUE;
  447. dpad_down = SDL_TRUE;
  448. break;
  449. case 28:
  450. dpad_left = SDL_TRUE;
  451. break;
  452. case 32:
  453. dpad_up = SDL_TRUE;
  454. dpad_left = SDL_TRUE;
  455. break;
  456. default:
  457. break;
  458. }
  459. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  460. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  461. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  462. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  463. }
  464. axis = (int)*(Uint16*)(&data[0]) - 0x8000;
  465. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  466. axis = (int)*(Uint16*)(&data[2]) - 0x8000;
  467. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  468. axis = (int)*(Uint16*)(&data[4]) - 0x8000;
  469. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  470. axis = (int)*(Uint16*)(&data[6]) - 0x8000;
  471. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  472. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  473. if (ctx->gamepad_statics && !ctx->gamepad) {
  474. Uint8 buttons = 0;
  475. if (data[10] & 0x01) {
  476. buttons |= (1 << SDL_CONTROLLER_BUTTON_A);
  477. }
  478. if (data[10] & 0x02) {
  479. buttons |= (1 << SDL_CONTROLLER_BUTTON_B);
  480. }
  481. if (data[10] & 0x04) {
  482. buttons |= (1 << SDL_CONTROLLER_BUTTON_X);
  483. }
  484. if (data[10] & 0x08) {
  485. buttons |= (1 << SDL_CONTROLLER_BUTTON_Y);
  486. }
  487. if (buttons != 0) {
  488. HIDAPI_DriverXbox360_GuessGamepad(ctx, buttons);
  489. }
  490. }
  491. if (ctx->gamepad) {
  492. HRESULT hr;
  493. struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
  494. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(ctx->gamepad, &state);
  495. if (SUCCEEDED(hr)) {
  496. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (state.Buttons & 0x40000000) ? SDL_PRESSED : SDL_RELEASED);
  497. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)(state.LeftTrigger * SDL_MAX_UINT16)) - 32768);
  498. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)(state.RightTrigger * SDL_MAX_UINT16)) - 32768);
  499. has_trigger_data = SDL_TRUE;
  500. }
  501. }
  502. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT */
  503. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  504. if (ctx->xinput_enabled) {
  505. if (ctx->xinput_slot == XUSER_INDEX_ANY && HIDAPI_DriverXbox360_MissingXInputSlot()) {
  506. WORD wButtons = 0;
  507. if (data[10] & 0x01) {
  508. wButtons |= XINPUT_GAMEPAD_A;
  509. }
  510. if (data[10] & 0x02) {
  511. wButtons |= XINPUT_GAMEPAD_B;
  512. }
  513. if (data[10] & 0x04) {
  514. wButtons |= XINPUT_GAMEPAD_X;
  515. }
  516. if (data[10] & 0x08) {
  517. wButtons |= XINPUT_GAMEPAD_Y;
  518. }
  519. if (wButtons != 0) {
  520. Uint8 xinput_slot = HIDAPI_DriverXbox360_GuessXInputSlot(wButtons);
  521. if (xinput_slot != XUSER_INDEX_ANY) {
  522. HIDAPI_DriverXbox360_MarkXInputSlotUsed(xinput_slot);
  523. ctx->xinput_slot = xinput_slot;
  524. }
  525. }
  526. }
  527. if (!has_trigger_data && ctx->xinput_slot != XUSER_INDEX_ANY) {
  528. XINPUT_STATE_EX xinput_state;
  529. if (XINPUTGETSTATE(ctx->xinput_slot, &xinput_state) == ERROR_SUCCESS) {
  530. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (xinput_state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) ? SDL_PRESSED : SDL_RELEASED);
  531. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, ((int)xinput_state.Gamepad.bLeftTrigger * 257) - 32768);
  532. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, ((int)xinput_state.Gamepad.bRightTrigger * 257) - 32768);
  533. has_trigger_data = SDL_TRUE;
  534. }
  535. }
  536. }
  537. #endif /* SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT */
  538. if (!has_trigger_data) {
  539. axis = (data[9] * 257) - 32768;
  540. if (data[9] < 0x80) {
  541. axis = -axis * 2 - 32769;
  542. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  543. } else if (data[9] > 0x80) {
  544. axis = axis * 2 - 32767;
  545. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  546. } else {
  547. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
  548. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
  549. }
  550. }
  551. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  552. }
  553. #else
  554. static void
  555. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  556. {
  557. Sint16 axis;
  558. #ifdef __MACOSX__
  559. const SDL_bool invert_y_axes = SDL_FALSE;
  560. #else
  561. const SDL_bool invert_y_axes = SDL_TRUE;
  562. #endif
  563. if (ctx->last_state[2] != data[2]) {
  564. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  565. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  566. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  567. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  568. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  569. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  570. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  571. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  572. }
  573. if (ctx->last_state[3] != data[3]) {
  574. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  575. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  576. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  577. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  578. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  579. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  580. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  581. }
  582. axis = ((int)data[4] * 257) - 32768;
  583. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  584. axis = ((int)data[5] * 257) - 32768;
  585. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  586. axis = *(Sint16*)(&data[6]);
  587. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  588. axis = *(Sint16*)(&data[8]);
  589. if (invert_y_axes) {
  590. axis = ~axis;
  591. }
  592. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  593. axis = *(Sint16*)(&data[10]);
  594. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  595. axis = *(Sint16*)(&data[12]);
  596. if (invert_y_axes) {
  597. axis = ~axis;
  598. }
  599. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  600. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  601. }
  602. #endif /* __WIN32__ */
  603. static SDL_bool
  604. HIDAPI_DriverXbox360_UpdateDevice(SDL_HIDAPI_Device *device)
  605. {
  606. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  607. SDL_Joystick *joystick = NULL;
  608. Uint8 data[USB_PACKET_LENGTH];
  609. int size;
  610. if (device->num_joysticks > 0) {
  611. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  612. }
  613. if (!joystick) {
  614. return SDL_FALSE;
  615. }
  616. while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  617. HIDAPI_DriverXbox360_HandleStatePacket(joystick, device->dev, ctx, data, size);
  618. }
  619. if (size < 0) {
  620. /* Read error, device is disconnected */
  621. HIDAPI_JoystickDisconnected(device, joystick->instance_id);
  622. }
  623. return (size >= 0);
  624. }
  625. static void
  626. HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  627. {
  628. #if defined(SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT) || defined(SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT)
  629. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  630. #endif
  631. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_XINPUT
  632. if (ctx->xinput_enabled) {
  633. HIDAPI_DriverXbox360_MarkXInputSlotFree(ctx->xinput_slot);
  634. WIN_UnloadXInputDLL();
  635. }
  636. #endif
  637. #ifdef SDL_JOYSTICK_HIDAPI_WINDOWS_GAMING_INPUT
  638. HIDAPI_DriverXbox360_QuitWindowsGamingInput(ctx);
  639. #endif
  640. hid_close(device->dev);
  641. device->dev = NULL;
  642. SDL_free(device->context);
  643. device->context = NULL;
  644. }
  645. static void
  646. HIDAPI_DriverXbox360_FreeDevice(SDL_HIDAPI_Device *device)
  647. {
  648. }
  649. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
  650. {
  651. SDL_HINT_JOYSTICK_HIDAPI_XBOX,
  652. SDL_TRUE,
  653. HIDAPI_DriverXbox360_IsSupportedDevice,
  654. HIDAPI_DriverXbox360_GetDeviceName,
  655. HIDAPI_DriverXbox360_InitDevice,
  656. HIDAPI_DriverXbox360_GetDevicePlayerIndex,
  657. HIDAPI_DriverXbox360_SetDevicePlayerIndex,
  658. HIDAPI_DriverXbox360_UpdateDevice,
  659. HIDAPI_DriverXbox360_OpenJoystick,
  660. HIDAPI_DriverXbox360_RumbleJoystick,
  661. HIDAPI_DriverXbox360_CloseJoystick,
  662. HIDAPI_DriverXbox360_FreeDevice
  663. };
  664. #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */
  665. #endif /* SDL_JOYSTICK_HIDAPI */
  666. /* vi: set ts=4 sw=4 expandtab: */