SDL_hidapi_xbox360.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #ifdef SDL_JOYSTICK_HIDAPI
  20. #include "../../SDL_hints_c.h"
  21. #include "../SDL_sysjoystick.h"
  22. #include "SDL_hidapijoystick_c.h"
  23. #include "SDL_hidapi_rumble.h"
  24. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  25. /* Define this if you want to log all packets from the controller */
  26. /*#define DEBUG_XBOX_PROTOCOL*/
  27. typedef struct
  28. {
  29. SDL_HIDAPI_Device *device;
  30. SDL_Joystick *joystick;
  31. int player_index;
  32. SDL_bool player_lights;
  33. Uint8 last_state[USB_PACKET_LENGTH];
  34. } SDL_DriverXbox360_Context;
  35. static void HIDAPI_DriverXbox360_RegisterHints(SDL_HintCallback callback, void *userdata)
  36. {
  37. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX, callback, userdata);
  38. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360, callback, userdata);
  39. }
  40. static void HIDAPI_DriverXbox360_UnregisterHints(SDL_HintCallback callback, void *userdata)
  41. {
  42. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX, callback, userdata);
  43. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360, callback, userdata);
  44. }
  45. static SDL_bool HIDAPI_DriverXbox360_IsEnabled(void)
  46. {
  47. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360,
  48. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT)));
  49. }
  50. static SDL_bool HIDAPI_DriverXbox360_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  51. {
  52. const int XB360W_IFACE_PROTOCOL = 129; /* Wireless */
  53. if (vendor_id == USB_VENDOR_NVIDIA) {
  54. /* This is the NVIDIA Shield controller which doesn't talk Xbox controller protocol */
  55. return SDL_FALSE;
  56. }
  57. if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == 0x0291 || product_id == 0x0719)) ||
  58. (type == SDL_GAMEPAD_TYPE_XBOX360 && interface_protocol == XB360W_IFACE_PROTOCOL)) {
  59. /* This is the wireless dongle, which talks a different protocol */
  60. return SDL_FALSE;
  61. }
  62. if (interface_number > 0) {
  63. /* This is the chatpad or other input interface, not the Xbox 360 interface */
  64. return SDL_FALSE;
  65. }
  66. #if defined(__MACOS__) || defined(__WIN32__)
  67. if (vendor_id == USB_VENDOR_MICROSOFT && product_id == 0x028e && version == 1) {
  68. /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
  69. return SDL_FALSE;
  70. }
  71. #endif
  72. #if defined(__MACOS__)
  73. /* Wired Xbox One controllers are handled by this driver, interfacing with
  74. the 360Controller driver available from:
  75. https://github.com/360Controller/360Controller/releases
  76. Bluetooth Xbox One controllers are handled by the SDL Xbox One driver
  77. */
  78. if (SDL_IsJoystickBluetoothXboxOne(vendor_id, product_id)) {
  79. return SDL_FALSE;
  80. }
  81. return (type == SDL_GAMEPAD_TYPE_XBOX360 || type == SDL_GAMEPAD_TYPE_XBOXONE) ? SDL_TRUE : SDL_FALSE;
  82. #else
  83. return (type == SDL_GAMEPAD_TYPE_XBOX360) ? SDL_TRUE : SDL_FALSE;
  84. #endif
  85. }
  86. static SDL_bool SetSlotLED(SDL_hid_device *dev, Uint8 slot, SDL_bool on)
  87. {
  88. const SDL_bool blink = SDL_FALSE;
  89. Uint8 mode = on ? ((blink ? 0x02 : 0x06) + slot) : 0;
  90. Uint8 led_packet[] = { 0x01, 0x03, 0x00 };
  91. led_packet[2] = mode;
  92. if (SDL_hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
  93. return SDL_FALSE;
  94. }
  95. return SDL_TRUE;
  96. }
  97. static void UpdateSlotLED(SDL_DriverXbox360_Context *ctx)
  98. {
  99. if (ctx->player_lights && ctx->player_lights >= 0) {
  100. SetSlotLED(ctx->device->dev, (ctx->player_index % 4), SDL_TRUE);
  101. } else {
  102. SetSlotLED(ctx->device->dev, 0, SDL_FALSE);
  103. }
  104. }
  105. static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  106. {
  107. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)userdata;
  108. SDL_bool player_lights = SDL_GetStringBoolean(hint, SDL_TRUE);
  109. if (player_lights != ctx->player_lights) {
  110. ctx->player_lights = player_lights;
  111. UpdateSlotLED(ctx);
  112. }
  113. }
  114. static SDL_bool HIDAPI_DriverXbox360_InitDevice(SDL_HIDAPI_Device *device)
  115. {
  116. SDL_DriverXbox360_Context *ctx;
  117. ctx = (SDL_DriverXbox360_Context *)SDL_calloc(1, sizeof(*ctx));
  118. if (ctx == NULL) {
  119. SDL_OutOfMemory();
  120. return SDL_FALSE;
  121. }
  122. ctx->device = device;
  123. device->context = ctx;
  124. device->type = SDL_GAMEPAD_TYPE_XBOX360;
  125. return HIDAPI_JoystickConnected(device, NULL);
  126. }
  127. static int HIDAPI_DriverXbox360_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  128. {
  129. return -1;
  130. }
  131. static void HIDAPI_DriverXbox360_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  132. {
  133. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  134. if (!ctx->joystick) {
  135. return;
  136. }
  137. ctx->player_index = player_index;
  138. UpdateSlotLED(ctx);
  139. }
  140. static SDL_bool HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  141. {
  142. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  143. SDL_AssertJoysticksLocked();
  144. ctx->joystick = joystick;
  145. SDL_zeroa(ctx->last_state);
  146. /* Initialize player index (needed for setting LEDs) */
  147. ctx->player_index = SDL_GetJoystickPlayerIndex(joystick);
  148. ctx->player_lights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED, SDL_TRUE);
  149. UpdateSlotLED(ctx);
  150. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED,
  151. SDL_PlayerLEDHintChanged, ctx);
  152. /* Initialize the joystick capabilities */
  153. joystick->nbuttons = 15;
  154. joystick->naxes = SDL_GAMEPAD_AXIS_MAX;
  155. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  156. return SDL_TRUE;
  157. }
  158. static int HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  159. {
  160. #ifdef __MACOS__
  161. if (SDL_IsJoystickBluetoothXboxOne(device->vendor_id, device->product_id)) {
  162. Uint8 rumble_packet[] = { 0x03, 0x0F, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00 };
  163. rumble_packet[4] = (low_frequency_rumble >> 8);
  164. rumble_packet[5] = (high_frequency_rumble >> 8);
  165. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  166. return SDL_SetError("Couldn't send rumble packet");
  167. }
  168. } else {
  169. /* On macOS the 360Controller driver uses this short report,
  170. and we need to prefix it with a magic token so hidapi passes it through untouched
  171. */
  172. Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
  173. rumble_packet[6 + 2] = (low_frequency_rumble >> 8);
  174. rumble_packet[6 + 3] = (high_frequency_rumble >> 8);
  175. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  176. return SDL_SetError("Couldn't send rumble packet");
  177. }
  178. }
  179. #else
  180. Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  181. rumble_packet[3] = (low_frequency_rumble >> 8);
  182. rumble_packet[4] = (high_frequency_rumble >> 8);
  183. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  184. return SDL_SetError("Couldn't send rumble packet");
  185. }
  186. #endif
  187. return 0;
  188. }
  189. static int HIDAPI_DriverXbox360_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  190. {
  191. return SDL_Unsupported();
  192. }
  193. static Uint32 HIDAPI_DriverXbox360_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  194. {
  195. /* Doesn't have an RGB LED, so don't return SDL_JOYCAP_LED here */
  196. return SDL_JOYCAP_RUMBLE;
  197. }
  198. static int HIDAPI_DriverXbox360_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  199. {
  200. return SDL_Unsupported();
  201. }
  202. static int HIDAPI_DriverXbox360_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  203. {
  204. return SDL_Unsupported();
  205. }
  206. static int HIDAPI_DriverXbox360_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
  207. {
  208. return SDL_Unsupported();
  209. }
  210. static void HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  211. {
  212. Sint16 axis;
  213. #ifdef __MACOS__
  214. const SDL_bool invert_y_axes = SDL_FALSE;
  215. #else
  216. const SDL_bool invert_y_axes = SDL_TRUE;
  217. #endif
  218. Uint64 timestamp = SDL_GetTicksNS();
  219. if (ctx->last_state[2] != data[2]) {
  220. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_UP, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  221. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_DOWN, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  222. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_LEFT, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  223. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_DPAD_RIGHT, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  224. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  225. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  226. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  227. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  228. }
  229. if (ctx->last_state[3] != data[3]) {
  230. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  231. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  232. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  233. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_A, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  234. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  235. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_X, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  236. SDL_PrivateJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_Y, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  237. }
  238. axis = ((int)data[4] * 257) - 32768;
  239. SDL_PrivateJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  240. axis = ((int)data[5] * 257) - 32768;
  241. SDL_PrivateJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  242. axis = SDL_SwapLE16(*(Sint16 *)(&data[6]));
  243. SDL_PrivateJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  244. axis = SDL_SwapLE16(*(Sint16 *)(&data[8]));
  245. if (invert_y_axes) {
  246. axis = ~axis;
  247. }
  248. SDL_PrivateJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  249. axis = SDL_SwapLE16(*(Sint16 *)(&data[10]));
  250. SDL_PrivateJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  251. axis = SDL_SwapLE16(*(Sint16 *)(&data[12]));
  252. if (invert_y_axes) {
  253. axis = ~axis;
  254. }
  255. SDL_PrivateJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  256. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  257. }
  258. static SDL_bool HIDAPI_DriverXbox360_UpdateDevice(SDL_HIDAPI_Device *device)
  259. {
  260. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  261. SDL_Joystick *joystick = NULL;
  262. Uint8 data[USB_PACKET_LENGTH];
  263. int size = 0;
  264. if (device->num_joysticks > 0) {
  265. joystick = SDL_GetJoystickFromInstanceID(device->joysticks[0]);
  266. } else {
  267. return SDL_FALSE;
  268. }
  269. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  270. #ifdef DEBUG_XBOX_PROTOCOL
  271. HIDAPI_DumpPacket("Xbox 360 packet: size = %d", data, size);
  272. #endif
  273. if (joystick == NULL) {
  274. continue;
  275. }
  276. if (data[0] == 0x00) {
  277. HIDAPI_DriverXbox360_HandleStatePacket(joystick, ctx, data, size);
  278. }
  279. }
  280. if (size < 0) {
  281. /* Read error, device is disconnected */
  282. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  283. }
  284. return size >= 0;
  285. }
  286. static void HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  287. {
  288. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)device->context;
  289. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED,
  290. SDL_PlayerLEDHintChanged, ctx);
  291. ctx->joystick = NULL;
  292. }
  293. static void HIDAPI_DriverXbox360_FreeDevice(SDL_HIDAPI_Device *device)
  294. {
  295. }
  296. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 = {
  297. SDL_HINT_JOYSTICK_HIDAPI_XBOX_360,
  298. SDL_TRUE,
  299. HIDAPI_DriverXbox360_RegisterHints,
  300. HIDAPI_DriverXbox360_UnregisterHints,
  301. HIDAPI_DriverXbox360_IsEnabled,
  302. HIDAPI_DriverXbox360_IsSupportedDevice,
  303. HIDAPI_DriverXbox360_InitDevice,
  304. HIDAPI_DriverXbox360_GetDevicePlayerIndex,
  305. HIDAPI_DriverXbox360_SetDevicePlayerIndex,
  306. HIDAPI_DriverXbox360_UpdateDevice,
  307. HIDAPI_DriverXbox360_OpenJoystick,
  308. HIDAPI_DriverXbox360_RumbleJoystick,
  309. HIDAPI_DriverXbox360_RumbleJoystickTriggers,
  310. HIDAPI_DriverXbox360_GetJoystickCapabilities,
  311. HIDAPI_DriverXbox360_SetJoystickLED,
  312. HIDAPI_DriverXbox360_SendJoystickEffect,
  313. HIDAPI_DriverXbox360_SetJoystickSensorsEnabled,
  314. HIDAPI_DriverXbox360_CloseJoystick,
  315. HIDAPI_DriverXbox360_FreeDevice,
  316. };
  317. #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */
  318. #endif /* SDL_JOYSTICK_HIDAPI */