SDL_hidapi_ps5.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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. #ifdef SDL_JOYSTICK_HIDAPI_PS5
  27. /* Define this if you want to log all packets from the controller */
  28. /*#define DEBUG_PS5_PROTOCOL*/
  29. typedef enum
  30. {
  31. k_EPS5ReportIdState = 0x01,
  32. k_EPS5ReportIdBluetoothState = 0x31,
  33. } EPS5ReportId;
  34. typedef enum
  35. {
  36. k_EPS5FeatureReportIdSerialNumber = 0x09,
  37. } EPS5FeatureReportId;
  38. typedef struct
  39. {
  40. Uint8 ucLeftJoystickX;
  41. Uint8 ucLeftJoystickY;
  42. Uint8 ucRightJoystickX;
  43. Uint8 ucRightJoystickY;
  44. Uint8 rgucButtonsHatAndCounter[3];
  45. Uint8 ucTriggerLeft;
  46. Uint8 ucTriggerRight;
  47. } PS5SimpleStatePacket_t;
  48. typedef struct
  49. {
  50. Uint8 ucLeftJoystickX; /* 0 */
  51. Uint8 ucLeftJoystickY; /* 1 */
  52. Uint8 ucRightJoystickX; /* 2 */
  53. Uint8 ucRightJoystickY; /* 3 */
  54. Uint8 ucTriggerLeft; /* 4 */
  55. Uint8 ucTriggerRight; /* 5 */
  56. Uint8 ucCounter; /* 6 */
  57. Uint8 rgucButtonsAndHat[3]; /* 7 */
  58. Uint8 ucZero; /* 10 */
  59. Uint8 rgucPacketSequence[4]; /* 11 - 32 bit little endian */
  60. Uint8 rgucAccel[6]; /* 15 */
  61. Uint8 rgucGyro[6]; /* 21 */
  62. Uint8 rgucTimer1[4]; /* 27 - 32 bit little endian */
  63. Uint8 ucBatteryTemp; /* 31 */
  64. Uint8 ucTouchpadCounter1; /* 32 - high bit clear + counter */
  65. Uint8 rgucTouchpadData1[3]; /* 33 - X/Y, 12 bits per axis */
  66. Uint8 ucTouchpadCounter2; /* 36 - high bit clear + counter */
  67. Uint8 rgucTouchpadData2[3]; /* 37 - X/Y, 12 bits per axis */
  68. Uint8 rgucUnknown1[8]; /* 40 */
  69. Uint8 rgucTimer2[4]; /* 48 - 32 bit little endian */
  70. Uint8 ucBatteryLevel; /* 52 */
  71. Uint8 ucConnectState; /* 53 - 0x08 = USB, 0x03 = headphone */
  72. /* There's more unknown data at the end, and a 32-bit CRC on Bluetooth */
  73. } PS5StatePacket_t;
  74. static void ReadFeatureReport(hid_device *dev, Uint8 report_id)
  75. {
  76. Uint8 report[USB_PACKET_LENGTH + 1];
  77. int size;
  78. SDL_memset(report, 0, sizeof(report));
  79. report[0] = report_id;
  80. size = hid_get_feature_report(dev, report, sizeof(report));
  81. if (size > 0) {
  82. #ifdef DEBUG_PS5_PROTOCOL
  83. SDL_Log("Report %d\n", report_id);
  84. HIDAPI_DumpPacket("Report: size = %d", report, size);
  85. #endif
  86. }
  87. }
  88. typedef struct {
  89. union
  90. {
  91. PS5SimpleStatePacket_t simple;
  92. PS5StatePacket_t state;
  93. } last_state;
  94. } SDL_DriverPS5_Context;
  95. static SDL_bool
  96. HIDAPI_DriverPS5_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)
  97. {
  98. return (type == SDL_CONTROLLER_TYPE_PS5);
  99. }
  100. static const char *
  101. HIDAPI_DriverPS5_GetDeviceName(Uint16 vendor_id, Uint16 product_id)
  102. {
  103. if (vendor_id == USB_VENDOR_SONY) {
  104. return "PS5 Controller";
  105. }
  106. return NULL;
  107. }
  108. static SDL_bool
  109. HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
  110. {
  111. return HIDAPI_JoystickConnected(device, NULL, SDL_FALSE);
  112. }
  113. static int
  114. HIDAPI_DriverPS5_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  115. {
  116. return -1;
  117. }
  118. static void
  119. HIDAPI_DriverPS5_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  120. {
  121. }
  122. static SDL_bool
  123. HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  124. {
  125. SDL_DriverPS5_Context *ctx;
  126. ctx = (SDL_DriverPS5_Context *)SDL_calloc(1, sizeof(*ctx));
  127. if (!ctx) {
  128. SDL_OutOfMemory();
  129. return SDL_FALSE;
  130. }
  131. device->dev = hid_open_path(device->path, 0);
  132. if (!device->dev) {
  133. SDL_free(ctx);
  134. SDL_SetError("Couldn't open %s", device->path);
  135. return SDL_FALSE;
  136. }
  137. device->context = ctx;
  138. /* Read the serial number (Bluetooth address in reverse byte order)
  139. This will also enable enhanced reports over Bluetooth
  140. */
  141. ReadFeatureReport(device->dev, k_EPS5FeatureReportIdSerialNumber);
  142. /* Initialize the joystick capabilities */
  143. joystick->nbuttons = 17;
  144. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  145. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  146. SDL_PrivateJoystickAddTouchpad(joystick, 2);
  147. return SDL_TRUE;
  148. }
  149. static int
  150. HIDAPI_DriverPS5_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  151. {
  152. return SDL_Unsupported();
  153. }
  154. static int
  155. HIDAPI_DriverPS5_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  156. {
  157. return SDL_Unsupported();
  158. }
  159. static SDL_bool
  160. HIDAPI_DriverPS5_HasJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  161. {
  162. return SDL_FALSE;
  163. }
  164. static int
  165. HIDAPI_DriverPS5_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  166. {
  167. return SDL_Unsupported();
  168. }
  169. static void
  170. HIDAPI_DriverPS5_HandleSimpleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS5_Context *ctx, PS5SimpleStatePacket_t *packet)
  171. {
  172. Sint16 axis;
  173. if (ctx->last_state.simple.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
  174. {
  175. Uint8 data = (packet->rgucButtonsHatAndCounter[0] >> 4);
  176. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  177. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  178. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  179. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  180. }
  181. {
  182. Uint8 data = (packet->rgucButtonsHatAndCounter[0] & 0x0F);
  183. SDL_bool dpad_up = SDL_FALSE;
  184. SDL_bool dpad_down = SDL_FALSE;
  185. SDL_bool dpad_left = SDL_FALSE;
  186. SDL_bool dpad_right = SDL_FALSE;
  187. switch (data) {
  188. case 0:
  189. dpad_up = SDL_TRUE;
  190. break;
  191. case 1:
  192. dpad_up = SDL_TRUE;
  193. dpad_right = SDL_TRUE;
  194. break;
  195. case 2:
  196. dpad_right = SDL_TRUE;
  197. break;
  198. case 3:
  199. dpad_right = SDL_TRUE;
  200. dpad_down = SDL_TRUE;
  201. break;
  202. case 4:
  203. dpad_down = SDL_TRUE;
  204. break;
  205. case 5:
  206. dpad_left = SDL_TRUE;
  207. dpad_down = SDL_TRUE;
  208. break;
  209. case 6:
  210. dpad_left = SDL_TRUE;
  211. break;
  212. case 7:
  213. dpad_up = SDL_TRUE;
  214. dpad_left = SDL_TRUE;
  215. break;
  216. default:
  217. break;
  218. }
  219. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  220. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  221. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  222. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  223. }
  224. }
  225. if (ctx->last_state.simple.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
  226. Uint8 data = packet->rgucButtonsHatAndCounter[1];
  227. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  228. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  229. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  230. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  231. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  232. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  233. }
  234. if (ctx->last_state.simple.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
  235. Uint8 data = (packet->rgucButtonsHatAndCounter[2] & 0x03);
  236. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  237. SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  238. }
  239. axis = ((int)packet->ucTriggerLeft * 257) - 32768;
  240. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  241. axis = ((int)packet->ucTriggerRight * 257) - 32768;
  242. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  243. axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
  244. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  245. axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
  246. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  247. axis = ((int)packet->ucRightJoystickX * 257) - 32768;
  248. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  249. axis = ((int)packet->ucRightJoystickY * 257) - 32768;
  250. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  251. SDL_memcpy(&ctx->last_state.simple, packet, sizeof(ctx->last_state.simple));
  252. }
  253. static void
  254. HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacket_t *packet)
  255. {
  256. static const float TOUCHPAD_SCALEX = 1.0f / 1920;
  257. static const float TOUCHPAD_SCALEY = 1.0f / 1070;
  258. Sint16 axis;
  259. Uint8 touchpad_state;
  260. int touchpad_x, touchpad_y;
  261. if (ctx->last_state.state.rgucButtonsAndHat[0] != packet->rgucButtonsAndHat[0]) {
  262. {
  263. Uint8 data = (packet->rgucButtonsAndHat[0] >> 4);
  264. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  265. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  266. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  267. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  268. }
  269. {
  270. Uint8 data = (packet->rgucButtonsAndHat[0] & 0x0F);
  271. SDL_bool dpad_up = SDL_FALSE;
  272. SDL_bool dpad_down = SDL_FALSE;
  273. SDL_bool dpad_left = SDL_FALSE;
  274. SDL_bool dpad_right = SDL_FALSE;
  275. switch (data) {
  276. case 0:
  277. dpad_up = SDL_TRUE;
  278. break;
  279. case 1:
  280. dpad_up = SDL_TRUE;
  281. dpad_right = SDL_TRUE;
  282. break;
  283. case 2:
  284. dpad_right = SDL_TRUE;
  285. break;
  286. case 3:
  287. dpad_right = SDL_TRUE;
  288. dpad_down = SDL_TRUE;
  289. break;
  290. case 4:
  291. dpad_down = SDL_TRUE;
  292. break;
  293. case 5:
  294. dpad_left = SDL_TRUE;
  295. dpad_down = SDL_TRUE;
  296. break;
  297. case 6:
  298. dpad_left = SDL_TRUE;
  299. break;
  300. case 7:
  301. dpad_up = SDL_TRUE;
  302. dpad_left = SDL_TRUE;
  303. break;
  304. default:
  305. break;
  306. }
  307. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  308. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  309. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  310. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  311. }
  312. }
  313. if (ctx->last_state.state.rgucButtonsAndHat[1] != packet->rgucButtonsAndHat[1]) {
  314. Uint8 data = packet->rgucButtonsAndHat[1];
  315. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  316. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  317. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  318. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  319. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  320. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  321. }
  322. if (ctx->last_state.state.rgucButtonsAndHat[2] != packet->rgucButtonsAndHat[2]) {
  323. Uint8 data = packet->rgucButtonsAndHat[2];
  324. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  325. SDL_PrivateJoystickButton(joystick, 15, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  326. SDL_PrivateJoystickButton(joystick, 16, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  327. }
  328. axis = ((int)packet->ucTriggerLeft * 257) - 32768;
  329. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  330. axis = ((int)packet->ucTriggerRight * 257) - 32768;
  331. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  332. axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
  333. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  334. axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
  335. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  336. axis = ((int)packet->ucRightJoystickX * 257) - 32768;
  337. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  338. axis = ((int)packet->ucRightJoystickY * 257) - 32768;
  339. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  340. if (packet->ucBatteryLevel & 0x10) {
  341. joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
  342. } else {
  343. /* Battery level ranges from 0 to 10 */
  344. int level = (packet->ucBatteryLevel & 0xF);
  345. if (level == 0) {
  346. joystick->epowerlevel = SDL_JOYSTICK_POWER_EMPTY;
  347. } else if (level <= 2) {
  348. joystick->epowerlevel = SDL_JOYSTICK_POWER_LOW;
  349. } else if (level <= 7) {
  350. joystick->epowerlevel = SDL_JOYSTICK_POWER_MEDIUM;
  351. } else {
  352. joystick->epowerlevel = SDL_JOYSTICK_POWER_FULL;
  353. }
  354. }
  355. touchpad_state = ((packet->ucTouchpadCounter1 & 0x80) == 0) ? SDL_PRESSED : SDL_RELEASED;
  356. touchpad_x = packet->rgucTouchpadData1[0] | (((int)packet->rgucTouchpadData1[1] & 0x0F) << 8);
  357. touchpad_y = (packet->rgucTouchpadData1[1] >> 4) | ((int)packet->rgucTouchpadData1[2] << 4);
  358. SDL_PrivateJoystickTouchpad(joystick, 0, 0, touchpad_state, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_state ? 1.0f : 0.0f);
  359. touchpad_state = ((packet->ucTouchpadCounter2 & 0x80) == 0) ? SDL_PRESSED : SDL_RELEASED;
  360. touchpad_x = packet->rgucTouchpadData2[0] | (((int)packet->rgucTouchpadData2[1] & 0x0F) << 8);
  361. touchpad_y = (packet->rgucTouchpadData2[1] >> 4) | ((int)packet->rgucTouchpadData2[2] << 4);
  362. SDL_PrivateJoystickTouchpad(joystick, 0, 1, touchpad_state, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_state ? 1.0f : 0.0f);
  363. SDL_memcpy(&ctx->last_state.state, packet, sizeof(ctx->last_state.state));
  364. }
  365. static SDL_bool
  366. HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
  367. {
  368. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  369. SDL_Joystick *joystick = NULL;
  370. Uint8 data[USB_PACKET_LENGTH*2];
  371. int size;
  372. if (device->num_joysticks > 0) {
  373. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  374. }
  375. if (!joystick) {
  376. return SDL_FALSE;
  377. }
  378. while ((size = hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  379. #ifdef DEBUG_PS5_PROTOCOL
  380. HIDAPI_DumpPacket("PS5 packet: size = %d", data, size);
  381. #endif
  382. switch (data[0]) {
  383. case k_EPS5ReportIdState:
  384. if (size == 10) {
  385. HIDAPI_DriverPS5_HandleSimpleStatePacket(joystick, device->dev, ctx, (PS5SimpleStatePacket_t *)&data[1]);
  386. } else {
  387. HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[1]);
  388. }
  389. break;
  390. case k_EPS5ReportIdBluetoothState:
  391. HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[2]);
  392. break;
  393. default:
  394. #ifdef DEBUG_JOYSTICK
  395. SDL_Log("Unknown PS5 packet: 0x%.2x\n", data[0]);
  396. #endif
  397. break;
  398. }
  399. }
  400. if (size < 0) {
  401. /* Read error, device is disconnected */
  402. HIDAPI_JoystickDisconnected(device, joystick->instance_id, SDL_FALSE);
  403. }
  404. return (size >= 0);
  405. }
  406. static void
  407. HIDAPI_DriverPS5_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  408. {
  409. hid_close(device->dev);
  410. device->dev = NULL;
  411. SDL_free(device->context);
  412. device->context = NULL;
  413. }
  414. static void
  415. HIDAPI_DriverPS5_FreeDevice(SDL_HIDAPI_Device *device)
  416. {
  417. }
  418. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5 =
  419. {
  420. SDL_HINT_JOYSTICK_HIDAPI_PS5,
  421. SDL_TRUE,
  422. HIDAPI_DriverPS5_IsSupportedDevice,
  423. HIDAPI_DriverPS5_GetDeviceName,
  424. HIDAPI_DriverPS5_InitDevice,
  425. HIDAPI_DriverPS5_GetDevicePlayerIndex,
  426. HIDAPI_DriverPS5_SetDevicePlayerIndex,
  427. HIDAPI_DriverPS5_UpdateDevice,
  428. HIDAPI_DriverPS5_OpenJoystick,
  429. HIDAPI_DriverPS5_RumbleJoystick,
  430. HIDAPI_DriverPS5_RumbleJoystickTriggers,
  431. HIDAPI_DriverPS5_HasJoystickLED,
  432. HIDAPI_DriverPS5_SetJoystickLED,
  433. HIDAPI_DriverPS5_CloseJoystick,
  434. HIDAPI_DriverPS5_FreeDevice,
  435. NULL
  436. };
  437. #endif /* SDL_JOYSTICK_HIDAPI_PS5 */
  438. #endif /* SDL_JOYSTICK_HIDAPI */
  439. /* vi: set ts=4 sw=4 expandtab: */