SDL_hidapi_xbox360.c 13 KB

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