SDL_hidapi_stadia.c 11 KB

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