SDL_hidapi_xbox360w.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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_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. bool connected;
  31. int player_index;
  32. bool player_lights;
  33. Uint8 last_state[USB_PACKET_LENGTH];
  34. } SDL_DriverXbox360W_Context;
  35. static void HIDAPI_DriverXbox360W_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. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_WIRELESS, callback, userdata);
  40. }
  41. static void HIDAPI_DriverXbox360W_UnregisterHints(SDL_HintCallback callback, void *userdata)
  42. {
  43. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX, callback, userdata);
  44. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360, callback, userdata);
  45. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_WIRELESS, callback, userdata);
  46. }
  47. static bool HIDAPI_DriverXbox360W_IsEnabled(void)
  48. {
  49. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_WIRELESS,
  50. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT))));
  51. }
  52. static bool HIDAPI_DriverXbox360W_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)
  53. {
  54. const int XB360W_IFACE_PROTOCOL = 129; // Wireless
  55. if ((vendor_id == USB_VENDOR_MICROSOFT && (product_id == USB_PRODUCT_XBOX360_WIRELESS_RECEIVER_THIRDPARTY2 || product_id == USB_PRODUCT_XBOX360_WIRELESS_RECEIVER_THIRDPARTY1 || product_id == USB_PRODUCT_XBOX360_WIRELESS_RECEIVER) && interface_protocol == 0) ||
  56. (type == SDL_GAMEPAD_TYPE_XBOX360 && interface_protocol == XB360W_IFACE_PROTOCOL)) {
  57. return true;
  58. }
  59. return false;
  60. }
  61. static bool SetSlotLED(SDL_hid_device *dev, Uint8 slot, bool on)
  62. {
  63. const bool blink = false;
  64. Uint8 mode = on ? ((blink ? 0x02 : 0x06) + slot) : 0;
  65. Uint8 led_packet[] = { 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  66. led_packet[3] = 0x40 + (mode % 0x0e);
  67. if (SDL_hid_write(dev, led_packet, sizeof(led_packet)) != sizeof(led_packet)) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. static void UpdateSlotLED(SDL_DriverXbox360W_Context *ctx)
  73. {
  74. if (ctx->player_lights && ctx->player_index >= 0) {
  75. SetSlotLED(ctx->device->dev, (ctx->player_index % 4), true);
  76. } else {
  77. SetSlotLED(ctx->device->dev, 0, false);
  78. }
  79. }
  80. static void SDLCALL SDL_PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  81. {
  82. SDL_DriverXbox360W_Context *ctx = (SDL_DriverXbox360W_Context *)userdata;
  83. bool player_lights = SDL_GetStringBoolean(hint, true);
  84. if (player_lights != ctx->player_lights) {
  85. ctx->player_lights = player_lights;
  86. UpdateSlotLED(ctx);
  87. HIDAPI_UpdateDeviceProperties(ctx->device);
  88. }
  89. }
  90. static void UpdatePowerLevel(SDL_Joystick *joystick, Uint8 level)
  91. {
  92. int percent = (int)SDL_roundf((level / 255.0f) * 100.0f);
  93. SDL_SendJoystickPowerInfo(joystick, SDL_POWERSTATE_ON_BATTERY, percent);
  94. }
  95. static bool HIDAPI_DriverXbox360W_InitDevice(SDL_HIDAPI_Device *device)
  96. {
  97. SDL_DriverXbox360W_Context *ctx;
  98. // Requests controller presence information from the wireless dongle
  99. const Uint8 init_packet[] = { 0x08, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  100. HIDAPI_SetDeviceName(device, "Xbox 360 Wireless Controller");
  101. ctx = (SDL_DriverXbox360W_Context *)SDL_calloc(1, sizeof(*ctx));
  102. if (!ctx) {
  103. return false;
  104. }
  105. ctx->device = device;
  106. device->context = ctx;
  107. if (SDL_hid_write(device->dev, init_packet, sizeof(init_packet)) != sizeof(init_packet)) {
  108. SDL_SetError("Couldn't write init packet");
  109. return false;
  110. }
  111. device->type = SDL_GAMEPAD_TYPE_XBOX360;
  112. return true;
  113. }
  114. static int HIDAPI_DriverXbox360W_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  115. {
  116. return -1;
  117. }
  118. static void HIDAPI_DriverXbox360W_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  119. {
  120. SDL_DriverXbox360W_Context *ctx = (SDL_DriverXbox360W_Context *)device->context;
  121. if (!ctx) {
  122. return;
  123. }
  124. ctx->player_index = player_index;
  125. UpdateSlotLED(ctx);
  126. }
  127. static bool HIDAPI_DriverXbox360W_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  128. {
  129. SDL_DriverXbox360W_Context *ctx = (SDL_DriverXbox360W_Context *)device->context;
  130. SDL_AssertJoysticksLocked();
  131. SDL_zeroa(ctx->last_state);
  132. // Initialize player index (needed for setting LEDs)
  133. ctx->player_index = SDL_GetJoystickPlayerIndex(joystick);
  134. ctx->player_lights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED, true);
  135. UpdateSlotLED(ctx);
  136. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED,
  137. SDL_PlayerLEDHintChanged, ctx);
  138. // Initialize the joystick capabilities
  139. joystick->nbuttons = 11;
  140. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  141. joystick->nhats = 1;
  142. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
  143. return true;
  144. }
  145. static bool HIDAPI_DriverXbox360W_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  146. {
  147. Uint8 rumble_packet[] = { 0x00, 0x01, 0x0f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  148. rumble_packet[5] = (low_frequency_rumble >> 8);
  149. rumble_packet[6] = (high_frequency_rumble >> 8);
  150. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  151. return SDL_SetError("Couldn't send rumble packet");
  152. }
  153. return true;
  154. }
  155. static bool HIDAPI_DriverXbox360W_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  156. {
  157. return SDL_Unsupported();
  158. }
  159. static Uint32 HIDAPI_DriverXbox360W_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  160. {
  161. SDL_DriverXbox360W_Context *ctx = (SDL_DriverXbox360W_Context *)device->context;
  162. Uint32 result = SDL_JOYSTICK_CAP_RUMBLE;
  163. if (ctx->player_lights) {
  164. result |= SDL_JOYSTICK_CAP_PLAYER_LED;
  165. }
  166. return result;
  167. }
  168. static bool HIDAPI_DriverXbox360W_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  169. {
  170. return SDL_Unsupported();
  171. }
  172. static bool HIDAPI_DriverXbox360W_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  173. {
  174. return SDL_Unsupported();
  175. }
  176. static bool HIDAPI_DriverXbox360W_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  177. {
  178. return SDL_Unsupported();
  179. }
  180. static void HIDAPI_DriverXbox360W_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverXbox360W_Context *ctx, Uint8 *data, int size)
  181. {
  182. Sint16 axis;
  183. const bool invert_y_axes = true;
  184. Uint64 timestamp = SDL_GetTicksNS();
  185. if (ctx->last_state[2] != data[2]) {
  186. Uint8 hat = 0;
  187. if (data[2] & 0x01) {
  188. hat |= SDL_HAT_UP;
  189. }
  190. if (data[2] & 0x02) {
  191. hat |= SDL_HAT_DOWN;
  192. }
  193. if (data[2] & 0x04) {
  194. hat |= SDL_HAT_LEFT;
  195. }
  196. if (data[2] & 0x08) {
  197. hat |= SDL_HAT_RIGHT;
  198. }
  199. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  200. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[2] & 0x10) != 0));
  201. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[2] & 0x20) != 0));
  202. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[2] & 0x40) != 0));
  203. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[2] & 0x80) != 0));
  204. }
  205. if (ctx->last_state[3] != data[3]) {
  206. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[3] & 0x01) != 0));
  207. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[3] & 0x02) != 0));
  208. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[3] & 0x04) != 0));
  209. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[3] & 0x10) != 0));
  210. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[3] & 0x20) != 0));
  211. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[3] & 0x40) != 0));
  212. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[3] & 0x80) != 0));
  213. }
  214. axis = ((int)data[4] * 257) - 32768;
  215. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  216. axis = ((int)data[5] * 257) - 32768;
  217. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  218. axis = SDL_Swap16LE(*(Sint16 *)(&data[6]));
  219. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  220. axis = SDL_Swap16LE(*(Sint16 *)(&data[8]));
  221. if (invert_y_axes) {
  222. axis = ~axis;
  223. }
  224. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  225. axis = SDL_Swap16LE(*(Sint16 *)(&data[10]));
  226. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  227. axis = SDL_Swap16LE(*(Sint16 *)(&data[12]));
  228. if (invert_y_axes) {
  229. axis = ~axis;
  230. }
  231. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  232. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  233. }
  234. static bool HIDAPI_DriverXbox360W_UpdateDevice(SDL_HIDAPI_Device *device)
  235. {
  236. SDL_DriverXbox360W_Context *ctx = (SDL_DriverXbox360W_Context *)device->context;
  237. SDL_Joystick *joystick = NULL;
  238. Uint8 data[USB_PACKET_LENGTH];
  239. int size;
  240. if (device->num_joysticks > 0) {
  241. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  242. }
  243. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  244. #ifdef DEBUG_XBOX_PROTOCOL
  245. HIDAPI_DumpPacket("Xbox 360 wireless packet: size = %d", data, size);
  246. #endif
  247. if (size == 2 && data[0] == 0x08) {
  248. bool connected = (data[1] & 0x80) ? true : false;
  249. #ifdef DEBUG_JOYSTICK
  250. SDL_Log("Connected = %s", connected ? "TRUE" : "FALSE");
  251. #endif
  252. if (connected != ctx->connected) {
  253. ctx->connected = connected;
  254. if (connected) {
  255. SDL_JoystickID joystickID;
  256. HIDAPI_JoystickConnected(device, &joystickID);
  257. } else if (device->num_joysticks > 0) {
  258. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  259. }
  260. }
  261. } else if (size == 29 && data[0] == 0x00 && data[1] == 0x0f && data[2] == 0x00 && data[3] == 0xf0) {
  262. // Serial number is data[7-13]
  263. #ifdef DEBUG_JOYSTICK
  264. SDL_Log("Battery status (initial): %d", data[17]);
  265. #endif
  266. if (joystick) {
  267. UpdatePowerLevel(joystick, data[17]);
  268. }
  269. } else if (size == 29 && data[0] == 0x00 && data[1] == 0x00 && data[2] == 0x00 && data[3] == 0x13) {
  270. #ifdef DEBUG_JOYSTICK
  271. SDL_Log("Battery status: %d", data[4]);
  272. #endif
  273. if (joystick) {
  274. UpdatePowerLevel(joystick, data[4]);
  275. }
  276. } else if (size == 29 && data[0] == 0x00 && (data[1] & 0x01) == 0x01) {
  277. if (joystick) {
  278. HIDAPI_DriverXbox360W_HandleStatePacket(joystick, device->dev, ctx, data + 4, size - 4);
  279. }
  280. }
  281. }
  282. if (size < 0 && device->num_joysticks > 0) {
  283. // Read error, device is disconnected
  284. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  285. }
  286. return (size >= 0);
  287. }
  288. static void HIDAPI_DriverXbox360W_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  289. {
  290. SDL_DriverXbox360W_Context *ctx = (SDL_DriverXbox360W_Context *)device->context;
  291. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_PLAYER_LED,
  292. SDL_PlayerLEDHintChanged, ctx);
  293. }
  294. static void HIDAPI_DriverXbox360W_FreeDevice(SDL_HIDAPI_Device *device)
  295. {
  296. }
  297. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W = {
  298. SDL_HINT_JOYSTICK_HIDAPI_XBOX_360_WIRELESS,
  299. true,
  300. HIDAPI_DriverXbox360W_RegisterHints,
  301. HIDAPI_DriverXbox360W_UnregisterHints,
  302. HIDAPI_DriverXbox360W_IsEnabled,
  303. HIDAPI_DriverXbox360W_IsSupportedDevice,
  304. HIDAPI_DriverXbox360W_InitDevice,
  305. HIDAPI_DriverXbox360W_GetDevicePlayerIndex,
  306. HIDAPI_DriverXbox360W_SetDevicePlayerIndex,
  307. HIDAPI_DriverXbox360W_UpdateDevice,
  308. HIDAPI_DriverXbox360W_OpenJoystick,
  309. HIDAPI_DriverXbox360W_RumbleJoystick,
  310. HIDAPI_DriverXbox360W_RumbleJoystickTriggers,
  311. HIDAPI_DriverXbox360W_GetJoystickCapabilities,
  312. HIDAPI_DriverXbox360W_SetJoystickLED,
  313. HIDAPI_DriverXbox360W_SendJoystickEffect,
  314. HIDAPI_DriverXbox360W_SetJoystickSensorsEnabled,
  315. HIDAPI_DriverXbox360W_CloseJoystick,
  316. HIDAPI_DriverXbox360W_FreeDevice,
  317. };
  318. #endif // SDL_JOYSTICK_HIDAPI_XBOX360
  319. #endif // SDL_JOYSTICK_HIDAPI