SDL_hidapi_xbox360.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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. #ifdef SDL_JOYSTICK_HIDAPI_XBOX360
  29. #define USB_PACKET_LENGTH 64
  30. typedef struct {
  31. Uint8 last_state[USB_PACKET_LENGTH];
  32. Uint32 rumble_expiration;
  33. } SDL_DriverXbox360_Context;
  34. static SDL_bool
  35. HIDAPI_DriverXbox360_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, Uint16 usage_page, Uint16 usage)
  36. {
  37. #if defined(__MACOSX__) || defined(__WIN32__)
  38. if (vendor_id == 0x045e && product_id == 0x028e && version == 1) {
  39. /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
  40. return SDL_FALSE;
  41. }
  42. return SDL_IsJoystickXbox360(vendor_id, product_id) || SDL_IsJoystickXboxOne(vendor_id, product_id);
  43. #else
  44. return SDL_IsJoystickXbox360(vendor_id, product_id);
  45. #endif
  46. }
  47. static const char *
  48. HIDAPI_DriverXbox360_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  49. {
  50. return HIDAPI_XboxControllerName(vendor_id, product_id);
  51. }
  52. static SDL_bool SetSlotLED(hid_device *dev, Uint8 slot)
  53. {
  54. const Uint8 led_packet[] = { 0x01, 0x03, (2 + slot) };
  55. if (hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
  56. return SDL_FALSE;
  57. }
  58. return SDL_TRUE;
  59. }
  60. static SDL_bool
  61. HIDAPI_DriverXbox360_Init(SDL_Joystick *joystick, hid_device *dev, Uint16 vendor_id, Uint16 product_id, void **context)
  62. {
  63. SDL_DriverXbox360_Context *ctx;
  64. ctx = (SDL_DriverXbox360_Context *)SDL_calloc(1, sizeof(*ctx));
  65. if (!ctx) {
  66. SDL_OutOfMemory();
  67. return SDL_FALSE;
  68. }
  69. *context = ctx;
  70. /* Set the controller LED */
  71. SetSlotLED(dev, (joystick->instance_id % 4));
  72. /* Initialize the joystick capabilities */
  73. joystick->nbuttons = SDL_CONTROLLER_BUTTON_MAX;
  74. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  75. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  76. return SDL_TRUE;
  77. }
  78. static int
  79. HIDAPI_DriverXbox360_Rumble(SDL_Joystick *joystick, hid_device *dev, void *context, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
  80. {
  81. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
  82. #ifdef __MACOSX__
  83. /* On Mac OS X the 360Controller driver uses this short report,
  84. and we need to prefix it with a magic token so hidapi passes it through untouched
  85. */
  86. Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
  87. rumble_packet[6+2] = (low_frequency_rumble >> 8);
  88. rumble_packet[6+3] = (high_frequency_rumble >> 8);
  89. #else
  90. Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  91. rumble_packet[3] = (low_frequency_rumble >> 8);
  92. rumble_packet[4] = (high_frequency_rumble >> 8);
  93. #endif
  94. if (hid_write(dev, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  95. return SDL_SetError("Couldn't send rumble packet");
  96. }
  97. if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
  98. ctx->rumble_expiration = SDL_GetTicks() + duration_ms;
  99. } else {
  100. ctx->rumble_expiration = 0;
  101. }
  102. return 0;
  103. }
  104. #ifdef __WIN32__
  105. /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
  106. however with this interface there is no rumble support, no guide button,
  107. and the left and right triggers are tied together as a single axis.
  108. */
  109. static void
  110. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  111. {
  112. Sint16 axis;
  113. if (ctx->last_state[10] != data[10]) {
  114. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[10] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  115. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[10] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  116. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[10] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  117. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[10] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  118. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[10] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  119. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[10] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  120. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[10] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  121. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[10] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  122. }
  123. if (ctx->last_state[11] != data[11]) {
  124. SDL_bool dpad_up = SDL_FALSE;
  125. SDL_bool dpad_down = SDL_FALSE;
  126. SDL_bool dpad_left = SDL_FALSE;
  127. SDL_bool dpad_right = SDL_FALSE;
  128. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[11] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  129. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[11] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  130. switch (data[11] & 0x3C) {
  131. case 4:
  132. dpad_up = SDL_TRUE;
  133. break;
  134. case 8:
  135. dpad_up = SDL_TRUE;
  136. dpad_right = SDL_TRUE;
  137. break;
  138. case 12:
  139. dpad_right = SDL_TRUE;
  140. break;
  141. case 16:
  142. dpad_right = SDL_TRUE;
  143. dpad_down = SDL_TRUE;
  144. break;
  145. case 20:
  146. dpad_down = SDL_TRUE;
  147. break;
  148. case 24:
  149. dpad_left = SDL_TRUE;
  150. dpad_down = SDL_TRUE;
  151. break;
  152. case 28:
  153. dpad_left = SDL_TRUE;
  154. break;
  155. case 32:
  156. dpad_up = SDL_TRUE;
  157. dpad_left = SDL_TRUE;
  158. break;
  159. default:
  160. break;
  161. }
  162. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  163. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  164. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  165. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  166. }
  167. axis = (int)*(Uint16*)(&data[0]) - 0x8000;
  168. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  169. axis = (int)*(Uint16*)(&data[2]) - 0x8000;
  170. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  171. axis = (int)*(Uint16*)(&data[4]) - 0x8000;
  172. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  173. axis = (int)*(Uint16*)(&data[6]) - 0x8000;
  174. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  175. axis = (data[9] * 257) - 32768;
  176. if (data[9] < 0x80) {
  177. axis = -axis * 2 - 32769;
  178. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  179. } else if (data[9] > 0x80) {
  180. axis = axis * 2 - 32767;
  181. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  182. } else {
  183. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_MIN_SINT16);
  184. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_MIN_SINT16);
  185. }
  186. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  187. }
  188. #else
  189. static void
  190. HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
  191. {
  192. Sint16 axis;
  193. #ifdef __MACOSX__
  194. const SDL_bool invert_y_axes = SDL_FALSE;
  195. #else
  196. const SDL_bool invert_y_axes = SDL_TRUE;
  197. #endif
  198. if (ctx->last_state[2] != data[2]) {
  199. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  200. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  201. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, (data[2] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  202. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, (data[2] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  203. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  204. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  205. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  206. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  207. }
  208. if (ctx->last_state[3] != data[3]) {
  209. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  210. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  211. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  212. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  213. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  214. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  215. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  216. }
  217. axis = ((int)data[4] * 257) - 32768;
  218. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  219. axis = ((int)data[5] * 257) - 32768;
  220. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  221. axis = *(Sint16*)(&data[6]);
  222. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  223. axis = *(Sint16*)(&data[8]);
  224. if (invert_y_axes) {
  225. axis = ~axis;
  226. }
  227. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  228. axis = *(Sint16*)(&data[10]);
  229. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  230. axis = *(Sint16*)(&data[12]);
  231. if (invert_y_axes) {
  232. axis = ~axis;
  233. }
  234. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  235. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  236. }
  237. #endif /* __WIN32__ */
  238. static SDL_bool
  239. HIDAPI_DriverXbox360_Update(SDL_Joystick *joystick, hid_device *dev, void *context)
  240. {
  241. SDL_DriverXbox360_Context *ctx = (SDL_DriverXbox360_Context *)context;
  242. Uint8 data[USB_PACKET_LENGTH];
  243. int size;
  244. while ((size = hid_read_timeout(dev, data, sizeof(data), 0)) > 0) {
  245. #ifdef __WIN32__
  246. HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size);
  247. #else
  248. switch (data[0]) {
  249. case 0x00:
  250. HIDAPI_DriverXbox360_HandleStatePacket(joystick, dev, ctx, data, size);
  251. break;
  252. default:
  253. #ifdef DEBUG_JOYSTICK
  254. SDL_Log("Unknown Xbox 360 packet: 0x%.2x\n", data[0]);
  255. #endif
  256. break;
  257. }
  258. #endif /* __WIN32__ */
  259. }
  260. if (ctx->rumble_expiration) {
  261. Uint32 now = SDL_GetTicks();
  262. if (SDL_TICKS_PASSED(now, ctx->rumble_expiration)) {
  263. HIDAPI_DriverXbox360_Rumble(joystick, dev, context, 0, 0, 0);
  264. }
  265. }
  266. return (size >= 0);
  267. }
  268. static void
  269. HIDAPI_DriverXbox360_Quit(SDL_Joystick *joystick, hid_device *dev, void *context)
  270. {
  271. SDL_free(context);
  272. }
  273. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360 =
  274. {
  275. SDL_HINT_JOYSTICK_HIDAPI_XBOX360,
  276. SDL_TRUE,
  277. HIDAPI_DriverXbox360_IsSupportedDevice,
  278. HIDAPI_DriverXbox360_GetDeviceName,
  279. HIDAPI_DriverXbox360_Init,
  280. HIDAPI_DriverXbox360_Rumble,
  281. HIDAPI_DriverXbox360_Update,
  282. HIDAPI_DriverXbox360_Quit
  283. };
  284. #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 */
  285. #endif /* SDL_JOYSTICK_HIDAPI */
  286. /* vi: set ts=4 sw=4 expandtab: */