1
0

SDL_hidapi_stadia.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_events.h"
  21. #include "SDL_joystick.h"
  22. #include "SDL_gamecontroller.h"
  23. #include "../SDL_sysjoystick.h"
  24. #include "SDL_hidapijoystick_c.h"
  25. #include "SDL_hidapi_rumble.h"
  26. #ifdef SDL_JOYSTICK_HIDAPI_STADIA
  27. /* Define this if you want to log all packets from the controller */
  28. /*#define DEBUG_STADIA_PROTOCOL*/
  29. enum
  30. {
  31. SDL_CONTROLLER_BUTTON_STADIA_SHARE = 15,
  32. SDL_CONTROLLER_BUTTON_STADIA_GOOGLE_ASSISTANT,
  33. SDL_CONTROLLER_NUM_STADIA_BUTTONS,
  34. };
  35. typedef struct {
  36. Uint8 last_state[USB_PACKET_LENGTH];
  37. } SDL_DriverStadia_Context;
  38. static void
  39. HIDAPI_DriverStadia_RegisterHints(SDL_HintCallback callback, void *userdata)
  40. {
  41. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STADIA, callback, userdata);
  42. }
  43. static void
  44. HIDAPI_DriverStadia_UnregisterHints(SDL_HintCallback callback, void *userdata)
  45. {
  46. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_STADIA, callback, userdata);
  47. }
  48. static SDL_bool
  49. HIDAPI_DriverStadia_IsEnabled(void)
  50. {
  51. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_STADIA, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  52. }
  53. static SDL_bool
  54. HIDAPI_DriverStadia_IsSupportedDevice(SDL_HIDAPI_Device *device, 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)
  55. {
  56. return (type == SDL_CONTROLLER_TYPE_GOOGLE_STADIA) ? SDL_TRUE : SDL_FALSE;
  57. }
  58. static SDL_bool
  59. HIDAPI_DriverStadia_InitDevice(SDL_HIDAPI_Device *device)
  60. {
  61. SDL_DriverStadia_Context *ctx;
  62. ctx = (SDL_DriverStadia_Context *)SDL_calloc(1, sizeof(*ctx));
  63. if (ctx == NULL) {
  64. SDL_OutOfMemory();
  65. return SDL_FALSE;
  66. }
  67. device->context = ctx;
  68. device->type = SDL_CONTROLLER_TYPE_GOOGLE_STADIA;
  69. HIDAPI_SetDeviceName(device, "Google Stadia Controller");
  70. return HIDAPI_JoystickConnected(device, NULL);
  71. }
  72. static int
  73. HIDAPI_DriverStadia_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  74. {
  75. return -1;
  76. }
  77. static void
  78. HIDAPI_DriverStadia_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  79. {
  80. }
  81. static SDL_bool
  82. HIDAPI_DriverStadia_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  83. {
  84. SDL_DriverStadia_Context *ctx = (SDL_DriverStadia_Context *)device->context;
  85. SDL_zeroa(ctx->last_state);
  86. /* Initialize the joystick capabilities */
  87. joystick->nbuttons = SDL_CONTROLLER_NUM_STADIA_BUTTONS;
  88. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  89. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  90. return SDL_TRUE;
  91. }
  92. static int
  93. HIDAPI_DriverStadia_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  94. {
  95. Uint8 rumble_packet[] = { 0x05, 0x00, 0x00, 0x00, 0x00 };
  96. rumble_packet[1] = (low_frequency_rumble & 0xFF);
  97. rumble_packet[2] = (low_frequency_rumble >> 8);
  98. rumble_packet[3] = (high_frequency_rumble & 0xFF);
  99. rumble_packet[4] = (high_frequency_rumble >> 8);
  100. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  101. return SDL_SetError("Couldn't send rumble packet");
  102. }
  103. return 0;
  104. }
  105. static int
  106. HIDAPI_DriverStadia_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  107. {
  108. return SDL_Unsupported();
  109. }
  110. static Uint32
  111. HIDAPI_DriverStadia_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  112. {
  113. return SDL_JOYCAP_RUMBLE;
  114. }
  115. static int
  116. HIDAPI_DriverStadia_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  117. {
  118. return SDL_Unsupported();
  119. }
  120. static int
  121. HIDAPI_DriverStadia_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  122. {
  123. return SDL_Unsupported();
  124. }
  125. static int
  126. HIDAPI_DriverStadia_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
  127. {
  128. return SDL_Unsupported();
  129. }
  130. static void
  131. HIDAPI_DriverStadia_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverStadia_Context *ctx, Uint8 *data, int size)
  132. {
  133. Sint16 axis;
  134. // The format is the same but the original FW will send 10 bytes and January '21 FW update will send 11
  135. if (size < 10 || data[0] != 0x03) {
  136. /* We don't know how to handle this report */
  137. return;
  138. }
  139. if (ctx->last_state[1] != data[1]) {
  140. SDL_bool dpad_up = SDL_FALSE;
  141. SDL_bool dpad_down = SDL_FALSE;
  142. SDL_bool dpad_left = SDL_FALSE;
  143. SDL_bool dpad_right = SDL_FALSE;
  144. switch (data[1]) {
  145. case 0:
  146. dpad_up = SDL_TRUE;
  147. break;
  148. case 1:
  149. dpad_up = SDL_TRUE;
  150. dpad_right = SDL_TRUE;
  151. break;
  152. case 2:
  153. dpad_right = SDL_TRUE;
  154. break;
  155. case 3:
  156. dpad_right = SDL_TRUE;
  157. dpad_down = SDL_TRUE;
  158. break;
  159. case 4:
  160. dpad_down = SDL_TRUE;
  161. break;
  162. case 5:
  163. dpad_left = SDL_TRUE;
  164. dpad_down = SDL_TRUE;
  165. break;
  166. case 6:
  167. dpad_left = SDL_TRUE;
  168. break;
  169. case 7:
  170. dpad_up = SDL_TRUE;
  171. dpad_left = SDL_TRUE;
  172. break;
  173. default:
  174. break;
  175. }
  176. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  177. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  178. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  179. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  180. }
  181. if (ctx->last_state[2] != data[2]) {
  182. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data[2] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  183. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data[2] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  184. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data[2] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  185. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data[2] & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  186. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_STADIA_SHARE, (data[2] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  187. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_STADIA_GOOGLE_ASSISTANT, (data[2] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  188. }
  189. if (ctx->last_state[3] != data[3]) {
  190. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data[3] & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  191. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data[3] & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  192. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data[3] & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  193. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data[3] & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  194. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data[3] & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  195. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data[3] & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  196. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data[3] & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  197. }
  198. #define READ_STICK_AXIS(offset) \
  199. (data[offset] == 0x80 ? 0 : \
  200. (Sint16)HIDAPI_RemapVal((float)((int)data[offset] - 0x80), 0x01 - 0x80, 0xff - 0x80, SDL_MIN_SINT16, SDL_MAX_SINT16))
  201. {
  202. axis = READ_STICK_AXIS(4);
  203. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  204. axis = READ_STICK_AXIS(5);
  205. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  206. axis = READ_STICK_AXIS(6);
  207. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  208. axis = READ_STICK_AXIS(7);
  209. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  210. }
  211. #undef READ_STICK_AXIS
  212. #define READ_TRIGGER_AXIS(offset) \
  213. (Sint16)(((int)data[offset] * 257) - 32768)
  214. {
  215. axis = READ_TRIGGER_AXIS(8);
  216. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  217. axis = READ_TRIGGER_AXIS(9);
  218. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  219. }
  220. #undef READ_TRIGGER_AXIS
  221. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  222. }
  223. static SDL_bool
  224. HIDAPI_DriverStadia_UpdateDevice(SDL_HIDAPI_Device *device)
  225. {
  226. SDL_DriverStadia_Context *ctx = (SDL_DriverStadia_Context *)device->context;
  227. SDL_Joystick *joystick = NULL;
  228. Uint8 data[USB_PACKET_LENGTH];
  229. int size = 0;
  230. if (device->num_joysticks > 0) {
  231. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  232. } else {
  233. return SDL_FALSE;
  234. }
  235. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  236. #ifdef DEBUG_STADIA_PROTOCOL
  237. HIDAPI_DumpPacket("Google Stadia packet: size = %d", data, size);
  238. #endif
  239. if (joystick == NULL) {
  240. continue;
  241. }
  242. HIDAPI_DriverStadia_HandleStatePacket(joystick, ctx, data, size);
  243. }
  244. if (size < 0) {
  245. /* Read error, device is disconnected */
  246. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  247. }
  248. return size >= 0;
  249. }
  250. static void
  251. HIDAPI_DriverStadia_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  252. {
  253. }
  254. static void
  255. HIDAPI_DriverStadia_FreeDevice(SDL_HIDAPI_Device *device)
  256. {
  257. }
  258. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverStadia =
  259. {
  260. SDL_HINT_JOYSTICK_HIDAPI_STADIA,
  261. SDL_TRUE,
  262. HIDAPI_DriverStadia_RegisterHints,
  263. HIDAPI_DriverStadia_UnregisterHints,
  264. HIDAPI_DriverStadia_IsEnabled,
  265. HIDAPI_DriverStadia_IsSupportedDevice,
  266. HIDAPI_DriverStadia_InitDevice,
  267. HIDAPI_DriverStadia_GetDevicePlayerIndex,
  268. HIDAPI_DriverStadia_SetDevicePlayerIndex,
  269. HIDAPI_DriverStadia_UpdateDevice,
  270. HIDAPI_DriverStadia_OpenJoystick,
  271. HIDAPI_DriverStadia_RumbleJoystick,
  272. HIDAPI_DriverStadia_RumbleJoystickTriggers,
  273. HIDAPI_DriverStadia_GetJoystickCapabilities,
  274. HIDAPI_DriverStadia_SetJoystickLED,
  275. HIDAPI_DriverStadia_SendJoystickEffect,
  276. HIDAPI_DriverStadia_SetJoystickSensorsEnabled,
  277. HIDAPI_DriverStadia_CloseJoystick,
  278. HIDAPI_DriverStadia_FreeDevice,
  279. };
  280. #endif /* SDL_JOYSTICK_HIDAPI_STADIA */
  281. #endif /* SDL_JOYSTICK_HIDAPI */
  282. /* vi: set ts=4 sw=4 expandtab: */