1
0

SDL_hidapi_ps5.c 42 KB

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