SDL_hidapi_ps5.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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_timer.h"
  22. #include "SDL_joystick.h"
  23. #include "SDL_gamecontroller.h"
  24. #include "../../SDL_hints_c.h"
  25. #include "../SDL_sysjoystick.h"
  26. #include "SDL_hidapijoystick_c.h"
  27. #include "SDL_hidapi_rumble.h"
  28. #ifdef SDL_JOYSTICK_HIDAPI_PS5
  29. /* Define this if you want to log all packets from the controller */
  30. /*#define DEBUG_PS5_PROTOCOL*/
  31. /* Define this if you want to log calibration data */
  32. /*#define DEBUG_PS5_CALIBRATION*/
  33. #define GYRO_RES_PER_DEGREE 1024.0f
  34. #define ACCEL_RES_PER_G 8192.0f
  35. #define BLUETOOTH_DISCONNECT_TIMEOUT_MS 500
  36. #define LOAD16(A, B) (Sint16)((Uint16)(A) | (((Uint16)(B)) << 8))
  37. typedef enum
  38. {
  39. k_EPS5ReportIdState = 0x01,
  40. k_EPS5ReportIdUsbEffects = 0x02,
  41. k_EPS5ReportIdBluetoothEffects = 0x31,
  42. k_EPS5ReportIdBluetoothState = 0x31,
  43. } EPS5ReportId;
  44. typedef enum
  45. {
  46. k_EPS5FeatureReportIdCapabilities = 0x03,
  47. k_EPS5FeatureReportIdCalibration = 0x05,
  48. k_EPS5FeatureReportIdSerialNumber = 0x09,
  49. k_EPS5FeatureReportIdFirmwareInfo = 0x20,
  50. } EPS5FeatureReportId;
  51. typedef struct
  52. {
  53. Uint8 ucLeftJoystickX;
  54. Uint8 ucLeftJoystickY;
  55. Uint8 ucRightJoystickX;
  56. Uint8 ucRightJoystickY;
  57. Uint8 rgucButtonsHatAndCounter[3];
  58. Uint8 ucTriggerLeft;
  59. Uint8 ucTriggerRight;
  60. } PS5SimpleStatePacket_t;
  61. typedef struct
  62. {
  63. Uint8 ucLeftJoystickX; /* 0 */
  64. Uint8 ucLeftJoystickY; /* 1 */
  65. Uint8 ucRightJoystickX; /* 2 */
  66. Uint8 ucRightJoystickY; /* 3 */
  67. Uint8 ucTriggerLeft; /* 4 */
  68. Uint8 ucTriggerRight; /* 5 */
  69. Uint8 ucCounter; /* 6 */
  70. Uint8 rgucButtonsAndHat[4]; /* 7 */
  71. Uint8 rgucPacketSequence[4]; /* 11 - 32 bit little endian */
  72. Uint8 rgucGyroX[2]; /* 15 */
  73. Uint8 rgucGyroY[2]; /* 17 */
  74. Uint8 rgucGyroZ[2]; /* 19 */
  75. Uint8 rgucAccelX[2]; /* 21 */
  76. Uint8 rgucAccelY[2]; /* 23 */
  77. Uint8 rgucAccelZ[2]; /* 25 */
  78. } PS5StatePacketCommon_t;
  79. typedef struct
  80. {
  81. Uint8 ucLeftJoystickX; /* 0 */
  82. Uint8 ucLeftJoystickY; /* 1 */
  83. Uint8 ucRightJoystickX; /* 2 */
  84. Uint8 ucRightJoystickY; /* 3 */
  85. Uint8 ucTriggerLeft; /* 4 */
  86. Uint8 ucTriggerRight; /* 5 */
  87. Uint8 ucCounter; /* 6 */
  88. Uint8 rgucButtonsAndHat[4]; /* 7 */
  89. Uint8 rgucPacketSequence[4]; /* 11 - 32 bit little endian */
  90. Uint8 rgucGyroX[2]; /* 15 */
  91. Uint8 rgucGyroY[2]; /* 17 */
  92. Uint8 rgucGyroZ[2]; /* 19 */
  93. Uint8 rgucAccelX[2]; /* 21 */
  94. Uint8 rgucAccelY[2]; /* 23 */
  95. Uint8 rgucAccelZ[2]; /* 25 */
  96. Uint8 rgucTimer1[4]; /* 27 - 32 bit little endian */
  97. Uint8 ucBatteryTemp; /* 31 */
  98. Uint8 ucTouchpadCounter1; /* 32 - high bit clear + counter */
  99. Uint8 rgucTouchpadData1[3]; /* 33 - X/Y, 12 bits per axis */
  100. Uint8 ucTouchpadCounter2; /* 36 - high bit clear + counter */
  101. Uint8 rgucTouchpadData2[3]; /* 37 - X/Y, 12 bits per axis */
  102. Uint8 rgucUnknown1[8]; /* 40 */
  103. Uint8 rgucTimer2[4]; /* 48 - 32 bit little endian */
  104. Uint8 ucBatteryLevel; /* 52 */
  105. Uint8 ucConnectState; /* 53 - 0x08 = USB, 0x01 = headphone */
  106. /* There's more unknown data at the end, and a 32-bit CRC on Bluetooth */
  107. } PS5StatePacket_t;
  108. typedef struct
  109. {
  110. Uint8 ucLeftJoystickX; /* 0 */
  111. Uint8 ucLeftJoystickY; /* 1 */
  112. Uint8 ucRightJoystickX; /* 2 */
  113. Uint8 ucRightJoystickY; /* 3 */
  114. Uint8 ucTriggerLeft; /* 4 */
  115. Uint8 ucTriggerRight; /* 5 */
  116. Uint8 ucCounter; /* 6 */
  117. Uint8 rgucButtonsAndHat[4]; /* 7 */
  118. Uint8 rgucPacketSequence[4]; /* 11 - 32 bit little endian */
  119. Uint8 rgucGyroX[2]; /* 15 */
  120. Uint8 rgucGyroY[2]; /* 17 */
  121. Uint8 rgucGyroZ[2]; /* 19 */
  122. Uint8 rgucAccelX[2]; /* 21 */
  123. Uint8 rgucAccelY[2]; /* 23 */
  124. Uint8 rgucAccelZ[2]; /* 25 */
  125. Uint8 rgucUnknown1[4]; /* 27 */
  126. Uint8 ucTouchpadCounter1; /* 31 - high bit clear + counter */
  127. Uint8 rgucTouchpadData1[3]; /* 32 - X/Y, 12 bits per axis */
  128. Uint8 ucTouchpadCounter2; /* 35 - high bit clear + counter */
  129. Uint8 rgucTouchpadData2[3]; /* 36 - X/Y, 12 bits per axis */
  130. /* There's more unknown data at the end, and a 32-bit CRC on Bluetooth */
  131. } PS5StatePacketAlt_t;
  132. typedef struct
  133. {
  134. Uint8 ucEnableBits1; /* 0 */
  135. Uint8 ucEnableBits2; /* 1 */
  136. Uint8 ucRumbleRight; /* 2 */
  137. Uint8 ucRumbleLeft; /* 3 */
  138. Uint8 ucHeadphoneVolume; /* 4 */
  139. Uint8 ucSpeakerVolume; /* 5 */
  140. Uint8 ucMicrophoneVolume; /* 6 */
  141. Uint8 ucAudioEnableBits; /* 7 */
  142. Uint8 ucMicLightMode; /* 8 */
  143. Uint8 ucAudioMuteBits; /* 9 */
  144. Uint8 rgucRightTriggerEffect[11]; /* 10 */
  145. Uint8 rgucLeftTriggerEffect[11]; /* 21 */
  146. Uint8 rgucUnknown1[6]; /* 32 */
  147. Uint8 ucEnableBits3; /* 38 */
  148. Uint8 rgucUnknown2[2]; /* 39 */
  149. Uint8 ucLedAnim; /* 41 */
  150. Uint8 ucLedBrightness; /* 42 */
  151. Uint8 ucPadLights; /* 43 */
  152. Uint8 ucLedRed; /* 44 */
  153. Uint8 ucLedGreen; /* 45 */
  154. Uint8 ucLedBlue; /* 46 */
  155. } DS5EffectsState_t;
  156. typedef enum {
  157. k_EDS5EffectRumbleStart = (1 << 0),
  158. k_EDS5EffectRumble = (1 << 1),
  159. k_EDS5EffectLEDReset = (1 << 2),
  160. k_EDS5EffectLED = (1 << 3),
  161. k_EDS5EffectPadLights = (1 << 4),
  162. k_EDS5EffectMicLight = (1 << 5)
  163. } EDS5Effect;
  164. typedef enum {
  165. k_EDS5LEDResetStateNone,
  166. k_EDS5LEDResetStatePending,
  167. k_EDS5LEDResetStateComplete,
  168. } EDS5LEDResetState;
  169. typedef struct {
  170. Sint16 bias;
  171. float sensitivity;
  172. } IMUCalibrationData;
  173. typedef struct {
  174. SDL_HIDAPI_Device *device;
  175. SDL_Joystick *joystick;
  176. SDL_bool is_bluetooth;
  177. SDL_bool use_alternate_report;
  178. SDL_bool sensors_supported;
  179. SDL_bool lightbar_supported;
  180. SDL_bool vibration_supported;
  181. SDL_bool playerled_supported;
  182. SDL_bool touchpad_supported;
  183. SDL_bool effects_supported;
  184. SDL_bool enhanced_mode;
  185. SDL_bool report_sensors;
  186. SDL_bool report_touchpad;
  187. SDL_bool hardware_calibration;
  188. IMUCalibrationData calibration[6];
  189. Uint16 firmware_version;
  190. Uint32 last_packet;
  191. int player_index;
  192. SDL_bool player_lights;
  193. Uint8 rumble_left;
  194. Uint8 rumble_right;
  195. SDL_bool color_set;
  196. Uint8 led_red;
  197. Uint8 led_green;
  198. Uint8 led_blue;
  199. EDS5LEDResetState led_reset_state;
  200. union
  201. {
  202. PS5SimpleStatePacket_t simple;
  203. PS5StatePacketCommon_t state;
  204. PS5StatePacket_t full_state;
  205. Uint8 data[64];
  206. } last_state;
  207. } SDL_DriverPS5_Context;
  208. static int HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size);
  209. static void
  210. HIDAPI_DriverPS5_RegisterHints(SDL_HintCallback callback, void *userdata)
  211. {
  212. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5, callback, userdata);
  213. }
  214. static void
  215. HIDAPI_DriverPS5_UnregisterHints(SDL_HintCallback callback, void *userdata)
  216. {
  217. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5, callback, userdata);
  218. }
  219. static SDL_bool
  220. HIDAPI_DriverPS5_IsEnabled(void)
  221. {
  222. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5,
  223. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI,
  224. SDL_HIDAPI_DEFAULT));
  225. }
  226. static int ReadFeatureReport(SDL_hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
  227. {
  228. SDL_memset(report, 0, length);
  229. report[0] = report_id;
  230. return SDL_hid_get_feature_report(dev, report, length);
  231. }
  232. static SDL_bool
  233. HIDAPI_DriverPS5_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)
  234. {
  235. Uint8 data[USB_PACKET_LENGTH];
  236. int size;
  237. if (type == SDL_CONTROLLER_TYPE_PS5) {
  238. return SDL_TRUE;
  239. }
  240. if (SONY_THIRDPARTY_VENDOR(vendor_id)) {
  241. if (device) {
  242. if ((size = ReadFeatureReport(device->dev, k_EPS5FeatureReportIdCapabilities, data, sizeof(data))) == 48 &&
  243. data[2] == 0x28) {
  244. /* Supported third party controller */
  245. return SDL_TRUE;
  246. } else {
  247. return SDL_FALSE;
  248. }
  249. } else {
  250. /* Might be supported by this driver, enumerate and find out */
  251. return SDL_TRUE;
  252. }
  253. }
  254. return SDL_FALSE;
  255. }
  256. static void
  257. SetLedsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
  258. {
  259. /* This list is the same as what hid-sony.c uses in the Linux kernel.
  260. The first 4 values correspond to what the PS4 assigns.
  261. */
  262. static const Uint8 colors[7][3] = {
  263. { 0x00, 0x00, 0x40 }, /* Blue */
  264. { 0x40, 0x00, 0x00 }, /* Red */
  265. { 0x00, 0x40, 0x00 }, /* Green */
  266. { 0x20, 0x00, 0x20 }, /* Pink */
  267. { 0x20, 0x10, 0x00 }, /* Orange */
  268. { 0x00, 0x10, 0x10 }, /* Teal */
  269. { 0x10, 0x10, 0x10 } /* White */
  270. };
  271. if (player_index >= 0) {
  272. player_index %= SDL_arraysize(colors);
  273. } else {
  274. player_index = 0;
  275. }
  276. effects->ucLedRed = colors[player_index][0];
  277. effects->ucLedGreen = colors[player_index][1];
  278. effects->ucLedBlue = colors[player_index][2];
  279. }
  280. static void
  281. SetLightsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
  282. {
  283. static const Uint8 lights[] = {
  284. 0x04,
  285. 0x0A,
  286. 0x15,
  287. 0x1B
  288. };
  289. if (player_index >= 0) {
  290. /* Bitmask, 0x1F enables all lights, 0x20 changes instantly instead of fade */
  291. player_index %= SDL_arraysize(lights);
  292. effects->ucPadLights = lights[player_index] | 0x20;
  293. } else {
  294. effects->ucPadLights = 0x00;
  295. }
  296. }
  297. static SDL_bool
  298. HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
  299. {
  300. SDL_DriverPS5_Context *ctx;
  301. Uint8 data[USB_PACKET_LENGTH*2];
  302. int size;
  303. char serial[18];
  304. ctx = (SDL_DriverPS5_Context *)SDL_calloc(1, sizeof(*ctx));
  305. if (!ctx) {
  306. SDL_OutOfMemory();
  307. return SDL_FALSE;
  308. }
  309. ctx->device = device;
  310. device->context = ctx;
  311. if (device->serial && SDL_strlen(device->serial) == 12) {
  312. int i, j;
  313. j = -1;
  314. for (i = 0; i < 12; i += 2) {
  315. j += 1;
  316. SDL_memcpy(&serial[j], &device->serial[i], 2);
  317. j += 2;
  318. serial[j] = '-';
  319. }
  320. serial[j] = '\0';
  321. } else {
  322. serial[0] = '\0';
  323. }
  324. /* Read a report to see what mode we're in */
  325. size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 16);
  326. #ifdef DEBUG_PS5_PROTOCOL
  327. if (size > 0) {
  328. HIDAPI_DumpPacket("PS5 first packet: size = %d", data, size);
  329. } else {
  330. SDL_Log("PS5 first packet: size = %d\n", size);
  331. }
  332. #endif
  333. if (size == 64) {
  334. /* Connected over USB */
  335. ctx->is_bluetooth = SDL_FALSE;
  336. ctx->enhanced_mode = SDL_TRUE;
  337. } else if (size > 0 && data[0] == k_EPS5ReportIdBluetoothEffects) {
  338. /* Connected over Bluetooth, using enhanced reports */
  339. ctx->is_bluetooth = SDL_TRUE;
  340. ctx->enhanced_mode = SDL_TRUE;
  341. } else {
  342. /* Connected over Bluetooth, using simple reports (DirectInput enabled) */
  343. ctx->is_bluetooth = SDL_TRUE;
  344. /* Games written prior the introduction of PS5 controller support in SDL will not be aware of
  345. SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, but they did know SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE.
  346. To support apps that only knew about the PS4 hint, we'll use the PS4 hint as the default.
  347. */
  348. ctx->enhanced_mode = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
  349. SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_FALSE));
  350. }
  351. if (ctx->enhanced_mode) {
  352. /* Read the serial number (Bluetooth address in reverse byte order)
  353. This will also enable enhanced reports over Bluetooth
  354. */
  355. if (ReadFeatureReport(device->dev, k_EPS5FeatureReportIdSerialNumber, data, sizeof(data)) >= 7) {
  356. SDL_snprintf(serial, sizeof(serial), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
  357. data[6], data[5], data[4], data[3], data[2], data[1]);
  358. }
  359. /* Read the firmware version
  360. This will also enable enhanced reports over Bluetooth
  361. */
  362. if (ReadFeatureReport(device->dev, k_EPS5FeatureReportIdFirmwareInfo, data, USB_PACKET_LENGTH) >= 46) {
  363. ctx->firmware_version = (Uint16)data[44] | ((Uint16)data[45] << 8);
  364. }
  365. }
  366. /* Get the device capabilities */
  367. if (device->vendor_id == USB_VENDOR_SONY) {
  368. ctx->sensors_supported = SDL_TRUE;
  369. ctx->lightbar_supported = SDL_TRUE;
  370. ctx->vibration_supported = SDL_TRUE;
  371. ctx->playerled_supported = SDL_TRUE;
  372. ctx->touchpad_supported = SDL_TRUE;
  373. } else if ((size = ReadFeatureReport(device->dev, k_EPS5FeatureReportIdCapabilities, data, sizeof(data))) == 48 &&
  374. data[2] == 0x28) {
  375. Uint8 capabilities = data[4];
  376. Uint8 capabilities2 = data[20];
  377. #ifdef DEBUG_PS5_PROTOCOL
  378. HIDAPI_DumpPacket("PS5 capabilities: size = %d", data, size);
  379. #endif
  380. if ((capabilities & 0x02) != 0) {
  381. ctx->sensors_supported = SDL_TRUE;
  382. }
  383. if ((capabilities & 0x04) != 0) {
  384. ctx->lightbar_supported = SDL_TRUE;
  385. }
  386. if ((capabilities & 0x08) != 0) {
  387. ctx->vibration_supported = SDL_TRUE;
  388. }
  389. if ((capabilities & 0x40) != 0) {
  390. ctx->touchpad_supported = SDL_TRUE;
  391. }
  392. if ((capabilities2 & 0x80) != 0) {
  393. ctx->playerled_supported = SDL_TRUE;
  394. }
  395. ctx->use_alternate_report = SDL_TRUE;
  396. }
  397. ctx->effects_supported = (ctx->lightbar_supported || ctx->vibration_supported || ctx->playerled_supported);
  398. device->type = SDL_CONTROLLER_TYPE_PS5;
  399. if (device->vendor_id == USB_VENDOR_SONY) {
  400. HIDAPI_SetDeviceName(device, "PS5 Controller");
  401. }
  402. HIDAPI_SetDeviceSerial(device, serial);
  403. return HIDAPI_JoystickConnected(device, NULL);
  404. }
  405. static int
  406. HIDAPI_DriverPS5_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  407. {
  408. return -1;
  409. }
  410. static void
  411. HIDAPI_DriverPS5_LoadCalibrationData(SDL_HIDAPI_Device *device)
  412. {
  413. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  414. int i, size;
  415. Uint8 data[USB_PACKET_LENGTH];
  416. size = ReadFeatureReport(device->dev, k_EPS5FeatureReportIdCalibration, data, sizeof(data));
  417. if (size < 35) {
  418. #ifdef DEBUG_PS5_CALIBRATION
  419. SDL_Log("Short read of calibration data: %d, ignoring calibration\n", size);
  420. #endif
  421. return;
  422. }
  423. {
  424. Sint16 sGyroPitchBias, sGyroYawBias, sGyroRollBias;
  425. Sint16 sGyroPitchPlus, sGyroPitchMinus;
  426. Sint16 sGyroYawPlus, sGyroYawMinus;
  427. Sint16 sGyroRollPlus, sGyroRollMinus;
  428. Sint16 sGyroSpeedPlus, sGyroSpeedMinus;
  429. Sint16 sAccXPlus, sAccXMinus;
  430. Sint16 sAccYPlus, sAccYMinus;
  431. Sint16 sAccZPlus, sAccZMinus;
  432. float flNumerator;
  433. Sint16 sRange2g;
  434. #ifdef DEBUG_PS5_CALIBRATION
  435. HIDAPI_DumpPacket("PS5 calibration packet: size = %d", data, size);
  436. #endif
  437. sGyroPitchBias = LOAD16(data[1], data[2]);
  438. sGyroYawBias = LOAD16(data[3], data[4]);
  439. sGyroRollBias = LOAD16(data[5], data[6]);
  440. sGyroPitchPlus = LOAD16(data[7], data[8]);
  441. sGyroPitchMinus = LOAD16(data[9], data[10]);
  442. sGyroYawPlus = LOAD16(data[11], data[12]);
  443. sGyroYawMinus = LOAD16(data[13], data[14]);
  444. sGyroRollPlus = LOAD16(data[15], data[16]);
  445. sGyroRollMinus = LOAD16(data[17], data[18]);
  446. sGyroSpeedPlus = LOAD16(data[19], data[20]);
  447. sGyroSpeedMinus = LOAD16(data[21], data[22]);
  448. sAccXPlus = LOAD16(data[23], data[24]);
  449. sAccXMinus = LOAD16(data[25], data[26]);
  450. sAccYPlus = LOAD16(data[27], data[28]);
  451. sAccYMinus = LOAD16(data[29], data[30]);
  452. sAccZPlus = LOAD16(data[31], data[32]);
  453. sAccZMinus = LOAD16(data[33], data[34]);
  454. flNumerator = (sGyroSpeedPlus + sGyroSpeedMinus) * GYRO_RES_PER_DEGREE;
  455. ctx->calibration[0].bias = sGyroPitchBias;
  456. ctx->calibration[0].sensitivity = flNumerator / (sGyroPitchPlus - sGyroPitchMinus);
  457. ctx->calibration[1].bias = sGyroYawBias;
  458. ctx->calibration[1].sensitivity = flNumerator / (sGyroYawPlus - sGyroYawMinus);
  459. ctx->calibration[2].bias = sGyroRollBias;
  460. ctx->calibration[2].sensitivity = flNumerator / (sGyroRollPlus - sGyroRollMinus);
  461. sRange2g = sAccXPlus - sAccXMinus;
  462. ctx->calibration[3].bias = sAccXPlus - sRange2g / 2;
  463. ctx->calibration[3].sensitivity = 2.0f * ACCEL_RES_PER_G / (float)sRange2g;
  464. sRange2g = sAccYPlus - sAccYMinus;
  465. ctx->calibration[4].bias = sAccYPlus - sRange2g / 2;
  466. ctx->calibration[4].sensitivity = 2.0f * ACCEL_RES_PER_G / (float)sRange2g;
  467. sRange2g = sAccZPlus - sAccZMinus;
  468. ctx->calibration[5].bias = sAccZPlus - sRange2g / 2;
  469. ctx->calibration[5].sensitivity = 2.0f * ACCEL_RES_PER_G / (float)sRange2g;
  470. ctx->hardware_calibration = SDL_TRUE;
  471. for (i = 0; i < 6; ++i) {
  472. float divisor = (i < 3 ? 64.0f : 1.0f);
  473. #ifdef DEBUG_PS5_CALIBRATION
  474. SDL_Log("calibration[%d] bias = %d, sensitivity = %f\n", i, ctx->calibration[i].bias, ctx->calibration[i].sensitivity);
  475. #endif
  476. /* Some controllers have a bad calibration */
  477. if ((SDL_abs(ctx->calibration[i].bias) > 1024) || (SDL_fabs(1.0f - ctx->calibration[i].sensitivity / divisor) > 0.5f)) {
  478. #ifdef DEBUG_PS5_CALIBRATION
  479. SDL_Log("invalid calibration, ignoring\n");
  480. #endif
  481. ctx->hardware_calibration = SDL_FALSE;
  482. }
  483. }
  484. }
  485. }
  486. static float
  487. HIDAPI_DriverPS5_ApplyCalibrationData(SDL_DriverPS5_Context *ctx, int index, Sint16 value)
  488. {
  489. float result;
  490. if (ctx->hardware_calibration) {
  491. IMUCalibrationData *calibration = &ctx->calibration[index];
  492. result = (value - calibration->bias) * calibration->sensitivity;
  493. } else if (index < 3) {
  494. result = value * 64.f;
  495. } else {
  496. result = value;
  497. }
  498. /* Convert the raw data to the units expected by SDL */
  499. if (index < 3) {
  500. result = (result / GYRO_RES_PER_DEGREE) * (float)M_PI / 180.0f;
  501. } else {
  502. result = (result / ACCEL_RES_PER_G) * SDL_STANDARD_GRAVITY;
  503. }
  504. return result;
  505. }
  506. static int
  507. HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, int effect_mask)
  508. {
  509. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  510. DS5EffectsState_t effects;
  511. if (!ctx->enhanced_mode || !ctx->effects_supported) {
  512. return SDL_Unsupported();
  513. }
  514. SDL_zero(effects);
  515. /* Make sure the Bluetooth connection sequence has completed before sending LED color change */
  516. if (ctx->is_bluetooth &&
  517. (effect_mask & (k_EDS5EffectLED | k_EDS5EffectPadLights)) != 0) {
  518. if (ctx->led_reset_state != k_EDS5LEDResetStateComplete) {
  519. ctx->led_reset_state = k_EDS5LEDResetStatePending;
  520. return 0;
  521. }
  522. }
  523. if (ctx->vibration_supported) {
  524. if (ctx->rumble_left || ctx->rumble_right) {
  525. if (ctx->firmware_version < 0x0224) {
  526. effects.ucEnableBits1 |= 0x01; /* Enable rumble emulation */
  527. /* Shift to reduce effective rumble strength to match Xbox controllers */
  528. effects.ucRumbleLeft = ctx->rumble_left >> 1;
  529. effects.ucRumbleRight = ctx->rumble_right >> 1;
  530. } else {
  531. effects.ucEnableBits3 |= 0x04; /* Enable improved rumble emulation on 2.24 firmware and newer */
  532. effects.ucRumbleLeft = ctx->rumble_left;
  533. effects.ucRumbleRight = ctx->rumble_right;
  534. }
  535. effects.ucEnableBits1 |= 0x02; /* Disable audio haptics */
  536. } else {
  537. /* Leaving emulated rumble bits off will restore audio haptics */
  538. }
  539. if ((effect_mask & k_EDS5EffectRumbleStart) != 0) {
  540. effects.ucEnableBits1 |= 0x02; /* Disable audio haptics */
  541. }
  542. if ((effect_mask & k_EDS5EffectRumble) != 0) {
  543. /* Already handled above */
  544. }
  545. }
  546. if (ctx->lightbar_supported) {
  547. if ((effect_mask & k_EDS5EffectLEDReset) != 0) {
  548. effects.ucEnableBits2 |= 0x08; /* Reset LED state */
  549. }
  550. if ((effect_mask & k_EDS5EffectLED) != 0) {
  551. effects.ucEnableBits2 |= 0x04; /* Enable LED color */
  552. /* Populate the LED state with the appropriate color from our lookup table */
  553. if (ctx->color_set) {
  554. effects.ucLedRed = ctx->led_red;
  555. effects.ucLedGreen = ctx->led_green;
  556. effects.ucLedBlue = ctx->led_blue;
  557. } else {
  558. SetLedsForPlayerIndex(&effects, ctx->player_index);
  559. }
  560. }
  561. }
  562. if (ctx->playerled_supported) {
  563. if ((effect_mask & k_EDS5EffectPadLights) != 0) {
  564. effects.ucEnableBits2 |= 0x10; /* Enable touchpad lights */
  565. if (ctx->player_lights) {
  566. SetLightsForPlayerIndex(&effects, ctx->player_index);
  567. } else {
  568. effects.ucPadLights = 0x00;
  569. }
  570. }
  571. }
  572. if ((effect_mask & k_EDS5EffectMicLight) != 0) {
  573. effects.ucEnableBits2 |= 0x01; /* Enable microphone light */
  574. effects.ucMicLightMode = 0; /* Bitmask, 0x00 = off, 0x01 = solid, 0x02 = pulse */
  575. }
  576. return HIDAPI_DriverPS5_SendJoystickEffect(device, ctx->joystick, &effects, sizeof(effects));
  577. }
  578. static void
  579. HIDAPI_DriverPS5_CheckPendingLEDReset(SDL_HIDAPI_Device *device)
  580. {
  581. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  582. SDL_bool led_reset_complete = SDL_FALSE;
  583. if (ctx->use_alternate_report) {
  584. /* We don't know how to check the timer, just assume it's complete for now */
  585. led_reset_complete = SDL_TRUE;
  586. } else {
  587. const PS5StatePacket_t *packet = &ctx->last_state.full_state;
  588. /* Check the timer to make sure the Bluetooth connection LED animation is complete */
  589. const Uint32 connection_complete = 10200000;
  590. Uint32 timer = ((Uint32)packet->rgucTimer1[0] << 0) |
  591. ((Uint32)packet->rgucTimer1[1] << 8) |
  592. ((Uint32)packet->rgucTimer1[2] << 16) |
  593. ((Uint32)packet->rgucTimer1[3] << 24);
  594. if (SDL_TICKS_PASSED(timer, connection_complete)) {
  595. led_reset_complete = SDL_TRUE;
  596. }
  597. }
  598. if (led_reset_complete) {
  599. HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLEDReset);
  600. ctx->led_reset_state = k_EDS5LEDResetStateComplete;
  601. HIDAPI_DriverPS5_UpdateEffects(device, (k_EDS5EffectLED | k_EDS5EffectPadLights));
  602. }
  603. }
  604. static void
  605. HIDAPI_DriverPS5_TickleBluetooth(SDL_HIDAPI_Device *device)
  606. {
  607. /* This is just a dummy packet that should have no effect, since we don't set the CRC */
  608. Uint8 data[78];
  609. SDL_zeroa(data);
  610. data[0] = k_EPS5ReportIdBluetoothEffects;
  611. data[1] = 0x02; /* Magic value */
  612. SDL_HIDAPI_SendRumble(device, data, sizeof(data));
  613. }
  614. static void
  615. HIDAPI_DriverPS5_SetEnhancedMode(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  616. {
  617. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  618. if (!ctx->enhanced_mode) {
  619. ctx->enhanced_mode = SDL_TRUE;
  620. if (ctx->touchpad_supported) {
  621. SDL_PrivateJoystickAddTouchpad(joystick, 2);
  622. ctx->report_touchpad = SDL_TRUE;
  623. }
  624. if (ctx->sensors_supported) {
  625. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 250.0f);
  626. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 250.0f);
  627. }
  628. /* Switch into enhanced report mode */
  629. HIDAPI_DriverPS5_UpdateEffects(device, 0);
  630. /* Update the light effects */
  631. HIDAPI_DriverPS5_UpdateEffects(device, (k_EDS5EffectLED | k_EDS5EffectPadLights));
  632. }
  633. }
  634. static void SDLCALL SDL_PS5RumbleHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  635. {
  636. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)userdata;
  637. /* This is a one-way trip, you can't switch the controller back to simple report mode */
  638. if (SDL_GetStringBoolean(hint, SDL_FALSE)) {
  639. HIDAPI_DriverPS5_SetEnhancedMode(ctx->device, ctx->joystick);
  640. }
  641. }
  642. static void SDLCALL SDL_PS5PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  643. {
  644. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)userdata;
  645. SDL_bool player_lights = SDL_GetStringBoolean(hint, SDL_TRUE);
  646. if (player_lights != ctx->player_lights) {
  647. ctx->player_lights = player_lights;
  648. HIDAPI_DriverPS5_UpdateEffects(ctx->device, k_EDS5EffectPadLights);
  649. }
  650. }
  651. static void
  652. HIDAPI_DriverPS5_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  653. {
  654. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  655. if (!ctx->joystick) {
  656. return;
  657. }
  658. ctx->player_index = player_index;
  659. /* This will set the new LED state based on the new player index */
  660. HIDAPI_DriverPS5_UpdateEffects(device, (k_EDS5EffectLED | k_EDS5EffectPadLights));
  661. }
  662. static SDL_bool
  663. HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  664. {
  665. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  666. ctx->joystick = joystick;
  667. ctx->last_packet = SDL_GetTicks();
  668. ctx->report_sensors = SDL_FALSE;
  669. ctx->report_touchpad = SDL_FALSE;
  670. ctx->rumble_left = 0;
  671. ctx->rumble_right = 0;
  672. ctx->color_set = SDL_FALSE;
  673. ctx->led_reset_state = k_EDS5LEDResetStateNone;
  674. SDL_zero(ctx->last_state);
  675. /* Initialize player index (needed for setting LEDs) */
  676. ctx->player_index = SDL_JoystickGetPlayerIndex(joystick);
  677. ctx->player_lights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, SDL_TRUE);
  678. /* Initialize the joystick capabilities */
  679. joystick->nbuttons = ctx->touchpad_supported ? 17 : 15;
  680. joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
  681. joystick->epowerlevel = ctx->is_bluetooth ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
  682. joystick->firmware_version = ctx->firmware_version;
  683. if (ctx->enhanced_mode) {
  684. /* Force initialization when opening the joystick */
  685. ctx->enhanced_mode = SDL_FALSE;
  686. HIDAPI_DriverPS5_SetEnhancedMode(device, joystick);
  687. } else {
  688. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
  689. SDL_PS5RumbleHintChanged, ctx);
  690. }
  691. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
  692. SDL_PS5PlayerLEDHintChanged, ctx);
  693. return SDL_TRUE;
  694. }
  695. static int
  696. HIDAPI_DriverPS5_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  697. {
  698. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  699. if (!ctx->vibration_supported) {
  700. return SDL_Unsupported();
  701. }
  702. if (!ctx->rumble_left && !ctx->rumble_right) {
  703. HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectRumbleStart);
  704. }
  705. ctx->rumble_left = (low_frequency_rumble >> 8);
  706. ctx->rumble_right = (high_frequency_rumble >> 8);
  707. return HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectRumble);
  708. }
  709. static int
  710. HIDAPI_DriverPS5_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  711. {
  712. return SDL_Unsupported();
  713. }
  714. static Uint32
  715. HIDAPI_DriverPS5_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  716. {
  717. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  718. Uint32 result = 0;
  719. if (ctx->enhanced_mode) {
  720. if (ctx->lightbar_supported) {
  721. result |= SDL_JOYCAP_LED;
  722. }
  723. if (ctx->vibration_supported) {
  724. result |= SDL_JOYCAP_RUMBLE;
  725. }
  726. }
  727. return result;
  728. }
  729. static int
  730. HIDAPI_DriverPS5_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  731. {
  732. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  733. if (!ctx->lightbar_supported) {
  734. return SDL_Unsupported();
  735. }
  736. ctx->color_set = SDL_TRUE;
  737. ctx->led_red = red;
  738. ctx->led_green = green;
  739. ctx->led_blue = blue;
  740. return HIDAPI_DriverPS5_UpdateEffects(device, k_EDS5EffectLED);
  741. }
  742. static int
  743. HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
  744. {
  745. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  746. Uint8 data[78];
  747. int report_size, offset;
  748. Uint8 *pending_data;
  749. int *pending_size;
  750. int maximum_size;
  751. if (!ctx->effects_supported) {
  752. return SDL_Unsupported();
  753. }
  754. if (!ctx->enhanced_mode) {
  755. HIDAPI_DriverPS5_SetEnhancedMode(device, joystick);
  756. }
  757. SDL_zeroa(data);
  758. if (ctx->is_bluetooth) {
  759. data[0] = k_EPS5ReportIdBluetoothEffects;
  760. data[1] = 0x02; /* Magic value */
  761. report_size = 78;
  762. offset = 2;
  763. } else {
  764. data[0] = k_EPS5ReportIdUsbEffects;
  765. report_size = 48;
  766. offset = 1;
  767. }
  768. SDL_memcpy(&data[offset], effect, SDL_min((sizeof(data) - offset), (size_t)size));
  769. if (ctx->is_bluetooth) {
  770. /* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
  771. Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
  772. Uint32 unCRC;
  773. unCRC = SDL_crc32(0, &ubHdr, 1);
  774. unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
  775. SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
  776. }
  777. if (SDL_HIDAPI_LockRumble() < 0) {
  778. return -1;
  779. }
  780. /* See if we can update an existing pending request */
  781. if (SDL_HIDAPI_GetPendingRumbleLocked(device, &pending_data, &pending_size, &maximum_size)) {
  782. DS5EffectsState_t *effects = (DS5EffectsState_t *)&data[offset];
  783. DS5EffectsState_t *pending_effects = (DS5EffectsState_t *)&pending_data[offset];
  784. if (report_size == *pending_size &&
  785. effects->ucEnableBits1 == pending_effects->ucEnableBits1 &&
  786. effects->ucEnableBits2 == pending_effects->ucEnableBits2) {
  787. /* We're simply updating the data for this request */
  788. SDL_memcpy(pending_data, data, report_size);
  789. SDL_HIDAPI_UnlockRumble();
  790. return 0;
  791. }
  792. }
  793. if (SDL_HIDAPI_SendRumbleAndUnlock(device, data, report_size) != report_size) {
  794. return -1;
  795. }
  796. return 0;
  797. }
  798. static int
  799. HIDAPI_DriverPS5_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, SDL_bool enabled)
  800. {
  801. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  802. if (!ctx->enhanced_mode) {
  803. return SDL_Unsupported();
  804. }
  805. if (enabled) {
  806. HIDAPI_DriverPS5_LoadCalibrationData(device);
  807. }
  808. ctx->report_sensors = enabled;
  809. return 0;
  810. }
  811. static void
  812. HIDAPI_DriverPS5_HandleSimpleStatePacket(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5SimpleStatePacket_t *packet)
  813. {
  814. Sint16 axis;
  815. if (ctx->last_state.simple.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
  816. {
  817. Uint8 data = (packet->rgucButtonsHatAndCounter[0] >> 4);
  818. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  819. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  820. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  821. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  822. }
  823. {
  824. Uint8 data = (packet->rgucButtonsHatAndCounter[0] & 0x0F);
  825. SDL_bool dpad_up = SDL_FALSE;
  826. SDL_bool dpad_down = SDL_FALSE;
  827. SDL_bool dpad_left = SDL_FALSE;
  828. SDL_bool dpad_right = SDL_FALSE;
  829. switch (data) {
  830. case 0:
  831. dpad_up = SDL_TRUE;
  832. break;
  833. case 1:
  834. dpad_up = SDL_TRUE;
  835. dpad_right = SDL_TRUE;
  836. break;
  837. case 2:
  838. dpad_right = SDL_TRUE;
  839. break;
  840. case 3:
  841. dpad_right = SDL_TRUE;
  842. dpad_down = SDL_TRUE;
  843. break;
  844. case 4:
  845. dpad_down = SDL_TRUE;
  846. break;
  847. case 5:
  848. dpad_left = SDL_TRUE;
  849. dpad_down = SDL_TRUE;
  850. break;
  851. case 6:
  852. dpad_left = SDL_TRUE;
  853. break;
  854. case 7:
  855. dpad_up = SDL_TRUE;
  856. dpad_left = SDL_TRUE;
  857. break;
  858. default:
  859. break;
  860. }
  861. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  862. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  863. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  864. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  865. }
  866. }
  867. if (ctx->last_state.simple.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
  868. Uint8 data = packet->rgucButtonsHatAndCounter[1];
  869. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  870. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  871. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  872. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  873. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  874. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  875. }
  876. if (ctx->last_state.simple.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
  877. Uint8 data = (packet->rgucButtonsHatAndCounter[2] & 0x03);
  878. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  879. SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  880. }
  881. axis = ((int)packet->ucTriggerLeft * 257) - 32768;
  882. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  883. axis = ((int)packet->ucTriggerRight * 257) - 32768;
  884. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  885. axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
  886. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  887. axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
  888. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  889. axis = ((int)packet->ucRightJoystickX * 257) - 32768;
  890. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  891. axis = ((int)packet->ucRightJoystickY * 257) - 32768;
  892. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  893. SDL_memcpy(&ctx->last_state.simple, packet, sizeof(ctx->last_state.simple));
  894. }
  895. static void
  896. HIDAPI_DriverPS5_HandleStatePacketCommon(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacketCommon_t *packet)
  897. {
  898. Sint16 axis;
  899. if (ctx->last_state.state.rgucButtonsAndHat[0] != packet->rgucButtonsAndHat[0]) {
  900. {
  901. Uint8 data = (packet->rgucButtonsAndHat[0] >> 4);
  902. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_X, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  903. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_A, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  904. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_B, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  905. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_Y, (data & 0x08) ? SDL_PRESSED : SDL_RELEASED);
  906. }
  907. {
  908. Uint8 data = (packet->rgucButtonsAndHat[0] & 0x0F);
  909. SDL_bool dpad_up = SDL_FALSE;
  910. SDL_bool dpad_down = SDL_FALSE;
  911. SDL_bool dpad_left = SDL_FALSE;
  912. SDL_bool dpad_right = SDL_FALSE;
  913. switch (data) {
  914. case 0:
  915. dpad_up = SDL_TRUE;
  916. break;
  917. case 1:
  918. dpad_up = SDL_TRUE;
  919. dpad_right = SDL_TRUE;
  920. break;
  921. case 2:
  922. dpad_right = SDL_TRUE;
  923. break;
  924. case 3:
  925. dpad_right = SDL_TRUE;
  926. dpad_down = SDL_TRUE;
  927. break;
  928. case 4:
  929. dpad_down = SDL_TRUE;
  930. break;
  931. case 5:
  932. dpad_left = SDL_TRUE;
  933. dpad_down = SDL_TRUE;
  934. break;
  935. case 6:
  936. dpad_left = SDL_TRUE;
  937. break;
  938. case 7:
  939. dpad_up = SDL_TRUE;
  940. dpad_left = SDL_TRUE;
  941. break;
  942. default:
  943. break;
  944. }
  945. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN, dpad_down);
  946. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP, dpad_up);
  947. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, dpad_right);
  948. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT, dpad_left);
  949. }
  950. }
  951. if (ctx->last_state.state.rgucButtonsAndHat[1] != packet->rgucButtonsAndHat[1]) {
  952. Uint8 data = packet->rgucButtonsAndHat[1];
  953. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSHOULDER, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  954. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  955. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_BACK, (data & 0x10) ? SDL_PRESSED : SDL_RELEASED);
  956. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_START, (data & 0x20) ? SDL_PRESSED : SDL_RELEASED);
  957. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_LEFTSTICK, (data & 0x40) ? SDL_PRESSED : SDL_RELEASED);
  958. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_RIGHTSTICK, (data & 0x80) ? SDL_PRESSED : SDL_RELEASED);
  959. }
  960. if (ctx->last_state.state.rgucButtonsAndHat[2] != packet->rgucButtonsAndHat[2]) {
  961. Uint8 data = packet->rgucButtonsAndHat[2];
  962. SDL_PrivateJoystickButton(joystick, SDL_CONTROLLER_BUTTON_GUIDE, (data & 0x01) ? SDL_PRESSED : SDL_RELEASED);
  963. SDL_PrivateJoystickButton(joystick, 15, (data & 0x02) ? SDL_PRESSED : SDL_RELEASED);
  964. SDL_PrivateJoystickButton(joystick, 16, (data & 0x04) ? SDL_PRESSED : SDL_RELEASED);
  965. }
  966. axis = ((int)packet->ucTriggerLeft * 257) - 32768;
  967. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERLEFT, axis);
  968. axis = ((int)packet->ucTriggerRight * 257) - 32768;
  969. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, axis);
  970. axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
  971. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTX, axis);
  972. axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
  973. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_LEFTY, axis);
  974. axis = ((int)packet->ucRightJoystickX * 257) - 32768;
  975. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTX, axis);
  976. axis = ((int)packet->ucRightJoystickY * 257) - 32768;
  977. SDL_PrivateJoystickAxis(joystick, SDL_CONTROLLER_AXIS_RIGHTY, axis);
  978. if (ctx->report_sensors) {
  979. float data[3];
  980. data[0] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 0, LOAD16(packet->rgucGyroX[0], packet->rgucGyroX[1]));
  981. data[1] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 1, LOAD16(packet->rgucGyroY[0], packet->rgucGyroY[1]));
  982. data[2] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 2, LOAD16(packet->rgucGyroZ[0], packet->rgucGyroZ[1]));
  983. SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_GYRO, data, 3);
  984. data[0] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 3, LOAD16(packet->rgucAccelX[0], packet->rgucAccelX[1]));
  985. data[1] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 4, LOAD16(packet->rgucAccelY[0], packet->rgucAccelY[1]));
  986. data[2] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 5, LOAD16(packet->rgucAccelZ[0], packet->rgucAccelZ[1]));
  987. SDL_PrivateJoystickSensor(joystick, SDL_SENSOR_ACCEL, data, 3);
  988. }
  989. }
  990. static void
  991. HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacket_t *packet)
  992. {
  993. static const float TOUCHPAD_SCALEX = 1.0f / 1920;
  994. static const float TOUCHPAD_SCALEY = 1.0f / 1070;
  995. Uint8 touchpad_state;
  996. int touchpad_x, touchpad_y;
  997. if (ctx->report_touchpad) {
  998. touchpad_state = ((packet->ucTouchpadCounter1 & 0x80) == 0) ? SDL_PRESSED : SDL_RELEASED;
  999. touchpad_x = packet->rgucTouchpadData1[0] | (((int)packet->rgucTouchpadData1[1] & 0x0F) << 8);
  1000. touchpad_y = (packet->rgucTouchpadData1[1] >> 4) | ((int)packet->rgucTouchpadData1[2] << 4);
  1001. SDL_PrivateJoystickTouchpad(joystick, 0, 0, touchpad_state, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_state ? 1.0f : 0.0f);
  1002. touchpad_state = ((packet->ucTouchpadCounter2 & 0x80) == 0) ? SDL_PRESSED : SDL_RELEASED;
  1003. touchpad_x = packet->rgucTouchpadData2[0] | (((int)packet->rgucTouchpadData2[1] & 0x0F) << 8);
  1004. touchpad_y = (packet->rgucTouchpadData2[1] >> 4) | ((int)packet->rgucTouchpadData2[2] << 4);
  1005. SDL_PrivateJoystickTouchpad(joystick, 0, 1, touchpad_state, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_state ? 1.0f : 0.0f);
  1006. }
  1007. /* A check of packet->ucBatteryLevel & 0x10 should work as a check for BT vs USB but doesn't
  1008. * seem to always work. Possibly related to being 100% charged?
  1009. */
  1010. if (!ctx->is_bluetooth) {
  1011. /* 0x20 set means fully charged */
  1012. SDL_PrivateJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_WIRED);
  1013. } else {
  1014. /* Battery level ranges from 0 to 10 */
  1015. int level = (packet->ucBatteryLevel & 0xF);
  1016. if (level == 0) {
  1017. SDL_PrivateJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_EMPTY);
  1018. } else if (level <= 2) {
  1019. SDL_PrivateJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_LOW);
  1020. } else if (level <= 7) {
  1021. SDL_PrivateJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_MEDIUM);
  1022. } else {
  1023. SDL_PrivateJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_FULL);
  1024. }
  1025. }
  1026. SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
  1027. }
  1028. static void
  1029. HIDAPI_DriverPS5_HandleStatePacketAlt(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacketAlt_t *packet)
  1030. {
  1031. static const float TOUCHPAD_SCALEX = 1.0f / 1920;
  1032. static const float TOUCHPAD_SCALEY = 1.0f / 1070;
  1033. Uint8 touchpad_state;
  1034. int touchpad_x, touchpad_y;
  1035. if (ctx->report_touchpad) {
  1036. touchpad_state = ((packet->ucTouchpadCounter1 & 0x80) == 0) ? SDL_PRESSED : SDL_RELEASED;
  1037. touchpad_x = packet->rgucTouchpadData1[0] | (((int) packet->rgucTouchpadData1[1] & 0x0F) << 8);
  1038. touchpad_y = (packet->rgucTouchpadData1[1] >> 4) | ((int) packet->rgucTouchpadData1[2] << 4);
  1039. SDL_PrivateJoystickTouchpad(joystick, 0, 0, touchpad_state, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_state ? 1.0f : 0.0f);
  1040. touchpad_state = ((packet->ucTouchpadCounter2 & 0x80) == 0) ? SDL_PRESSED : SDL_RELEASED;
  1041. touchpad_x = packet->rgucTouchpadData2[0] | (((int) packet->rgucTouchpadData2[1] & 0x0F) << 8);
  1042. touchpad_y = (packet->rgucTouchpadData2[1] >> 4) | ((int) packet->rgucTouchpadData2[2] << 4);
  1043. SDL_PrivateJoystickTouchpad(joystick, 0, 1, touchpad_state, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_state ? 1.0f : 0.0f);
  1044. }
  1045. SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
  1046. }
  1047. static SDL_bool
  1048. HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
  1049. {
  1050. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  1051. SDL_Joystick *joystick = NULL;
  1052. Uint8 data[USB_PACKET_LENGTH*2];
  1053. int size;
  1054. int packet_count = 0;
  1055. if (device->num_joysticks > 0) {
  1056. joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
  1057. } else {
  1058. return SDL_FALSE;
  1059. }
  1060. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  1061. #ifdef DEBUG_PS5_PROTOCOL
  1062. HIDAPI_DumpPacket("PS5 packet: size = %d", data, size);
  1063. #endif
  1064. ++packet_count;
  1065. ctx->last_packet = SDL_GetTicks();
  1066. if (!joystick) {
  1067. continue;
  1068. }
  1069. switch (data[0]) {
  1070. case k_EPS5ReportIdState:
  1071. if (size == 10 || size == 78) {
  1072. HIDAPI_DriverPS5_HandleSimpleStatePacket(joystick, device->dev, ctx, (PS5SimpleStatePacket_t *)&data[1]);
  1073. } else {
  1074. HIDAPI_DriverPS5_HandleStatePacketCommon(joystick, device->dev, ctx, (PS5StatePacketCommon_t *)&data[1]);
  1075. if (ctx->use_alternate_report) {
  1076. HIDAPI_DriverPS5_HandleStatePacketAlt(joystick, device->dev, ctx, (PS5StatePacketAlt_t *)&data[1]);
  1077. } else {
  1078. HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[1]);
  1079. }
  1080. }
  1081. break;
  1082. case k_EPS5ReportIdBluetoothState:
  1083. if (!ctx->enhanced_mode) {
  1084. /* This is the extended report, we can enable effects now */
  1085. HIDAPI_DriverPS5_SetEnhancedMode(device, joystick);
  1086. }
  1087. if (ctx->led_reset_state == k_EDS5LEDResetStatePending) {
  1088. HIDAPI_DriverPS5_CheckPendingLEDReset(device);
  1089. }
  1090. HIDAPI_DriverPS5_HandleStatePacketCommon(joystick, device->dev, ctx, (PS5StatePacketCommon_t *)&data[2]);
  1091. if (ctx->use_alternate_report) {
  1092. HIDAPI_DriverPS5_HandleStatePacketAlt(joystick, device->dev, ctx, (PS5StatePacketAlt_t *)&data[2]);
  1093. } else {
  1094. HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[2]);
  1095. }
  1096. break;
  1097. default:
  1098. #ifdef DEBUG_JOYSTICK
  1099. SDL_Log("Unknown PS5 packet: 0x%.2x\n", data[0]);
  1100. #endif
  1101. break;
  1102. }
  1103. }
  1104. if (ctx->is_bluetooth && packet_count == 0) {
  1105. /* Check to see if it looks like the device disconnected */
  1106. if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
  1107. /* Send an empty output report to tickle the Bluetooth stack */
  1108. HIDAPI_DriverPS5_TickleBluetooth(device);
  1109. }
  1110. }
  1111. if (size < 0) {
  1112. /* Read error, device is disconnected */
  1113. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  1114. }
  1115. return (size >= 0);
  1116. }
  1117. static void
  1118. HIDAPI_DriverPS5_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1119. {
  1120. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  1121. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE,
  1122. SDL_PS5RumbleHintChanged, ctx);
  1123. SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
  1124. SDL_PS5PlayerLEDHintChanged, ctx);
  1125. ctx->joystick = NULL;
  1126. }
  1127. static void
  1128. HIDAPI_DriverPS5_FreeDevice(SDL_HIDAPI_Device *device)
  1129. {
  1130. }
  1131. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5 =
  1132. {
  1133. SDL_HINT_JOYSTICK_HIDAPI_PS5,
  1134. SDL_TRUE,
  1135. HIDAPI_DriverPS5_RegisterHints,
  1136. HIDAPI_DriverPS5_UnregisterHints,
  1137. HIDAPI_DriverPS5_IsEnabled,
  1138. HIDAPI_DriverPS5_IsSupportedDevice,
  1139. HIDAPI_DriverPS5_InitDevice,
  1140. HIDAPI_DriverPS5_GetDevicePlayerIndex,
  1141. HIDAPI_DriverPS5_SetDevicePlayerIndex,
  1142. HIDAPI_DriverPS5_UpdateDevice,
  1143. HIDAPI_DriverPS5_OpenJoystick,
  1144. HIDAPI_DriverPS5_RumbleJoystick,
  1145. HIDAPI_DriverPS5_RumbleJoystickTriggers,
  1146. HIDAPI_DriverPS5_GetJoystickCapabilities,
  1147. HIDAPI_DriverPS5_SetJoystickLED,
  1148. HIDAPI_DriverPS5_SendJoystickEffect,
  1149. HIDAPI_DriverPS5_SetJoystickSensorsEnabled,
  1150. HIDAPI_DriverPS5_CloseJoystick,
  1151. HIDAPI_DriverPS5_FreeDevice,
  1152. };
  1153. #endif /* SDL_JOYSTICK_HIDAPI_PS5 */
  1154. #endif /* SDL_JOYSTICK_HIDAPI */
  1155. /* vi: set ts=4 sw=4 expandtab: */