SDL_hidapi_ps5.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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_c.h"
  21. #include "../SDL_sysjoystick.h"
  22. #include "SDL_hidapijoystick_c.h"
  23. #include "SDL_hidapi_rumble.h"
  24. #ifdef SDL_JOYSTICK_HIDAPI_PS5
  25. // Define this if you want to log all packets from the controller
  26. #if 0
  27. #define DEBUG_PS5_PROTOCOL
  28. #endif
  29. // Define this if you want to log calibration data
  30. #if 0
  31. #define DEBUG_PS5_CALIBRATION
  32. #endif
  33. #define GYRO_RES_PER_DEGREE 1024.0f
  34. #define ACCEL_RES_PER_G 8192.0f
  35. #define BLUETOOTH_DISCONNECT_TIMEOUT_MS 500
  36. enum
  37. {
  38. SDL_GAMEPAD_BUTTON_PS5_TOUCHPAD = 11,
  39. SDL_GAMEPAD_BUTTON_PS5_MICROPHONE,
  40. SDL_GAMEPAD_BUTTON_PS5_LEFT_FUNCTION,
  41. SDL_GAMEPAD_BUTTON_PS5_RIGHT_FUNCTION,
  42. SDL_GAMEPAD_BUTTON_PS5_LEFT_PADDLE,
  43. SDL_GAMEPAD_BUTTON_PS5_RIGHT_PADDLE
  44. };
  45. typedef enum
  46. {
  47. k_EPS5ReportIdState = 0x01,
  48. k_EPS5ReportIdUsbEffects = 0x02,
  49. k_EPS5ReportIdBluetoothEffects = 0x31,
  50. k_EPS5ReportIdBluetoothState = 0x31,
  51. } EPS5ReportId;
  52. typedef enum
  53. {
  54. k_EPS5FeatureReportIdCapabilities = 0x03,
  55. k_EPS5FeatureReportIdCalibration = 0x05,
  56. k_EPS5FeatureReportIdSerialNumber = 0x09,
  57. k_EPS5FeatureReportIdFirmwareInfo = 0x20,
  58. } EPS5FeatureReportId;
  59. typedef struct
  60. {
  61. Uint8 ucLeftJoystickX;
  62. Uint8 ucLeftJoystickY;
  63. Uint8 ucRightJoystickX;
  64. Uint8 ucRightJoystickY;
  65. Uint8 rgucButtonsHatAndCounter[3];
  66. Uint8 ucTriggerLeft;
  67. Uint8 ucTriggerRight;
  68. } PS5SimpleStatePacket_t;
  69. typedef struct
  70. {
  71. Uint8 ucLeftJoystickX; // 0
  72. Uint8 ucLeftJoystickY; // 1
  73. Uint8 ucRightJoystickX; // 2
  74. Uint8 ucRightJoystickY; // 3
  75. Uint8 ucTriggerLeft; // 4
  76. Uint8 ucTriggerRight; // 5
  77. Uint8 ucCounter; // 6
  78. Uint8 rgucButtonsAndHat[4]; // 7
  79. Uint8 rgucPacketSequence[4]; // 11 - 32 bit little endian
  80. Uint8 rgucGyroX[2]; // 15
  81. Uint8 rgucGyroY[2]; // 17
  82. Uint8 rgucGyroZ[2]; // 19
  83. Uint8 rgucAccelX[2]; // 21
  84. Uint8 rgucAccelY[2]; // 23
  85. Uint8 rgucAccelZ[2]; // 25
  86. Uint8 rgucSensorTimestamp[4]; // 27 - 16/32 bit little endian
  87. } PS5StatePacketCommon_t;
  88. typedef struct
  89. {
  90. Uint8 ucLeftJoystickX; // 0
  91. Uint8 ucLeftJoystickY; // 1
  92. Uint8 ucRightJoystickX; // 2
  93. Uint8 ucRightJoystickY; // 3
  94. Uint8 ucTriggerLeft; // 4
  95. Uint8 ucTriggerRight; // 5
  96. Uint8 ucCounter; // 6
  97. Uint8 rgucButtonsAndHat[4]; // 7
  98. Uint8 rgucPacketSequence[4]; // 11 - 32 bit little endian
  99. Uint8 rgucGyroX[2]; // 15
  100. Uint8 rgucGyroY[2]; // 17
  101. Uint8 rgucGyroZ[2]; // 19
  102. Uint8 rgucAccelX[2]; // 21
  103. Uint8 rgucAccelY[2]; // 23
  104. Uint8 rgucAccelZ[2]; // 25
  105. Uint8 rgucSensorTimestamp[4]; // 27 - 32 bit little endian
  106. Uint8 ucSensorTemp; // 31
  107. Uint8 ucTouchpadCounter1; // 32 - high bit clear + counter
  108. Uint8 rgucTouchpadData1[3]; // 33 - X/Y, 12 bits per axis
  109. Uint8 ucTouchpadCounter2; // 36 - high bit clear + counter
  110. Uint8 rgucTouchpadData2[3]; // 37 - X/Y, 12 bits per axis
  111. Uint8 rgucUnknown1[8]; // 40
  112. Uint8 rgucTimer2[4]; // 48 - 32 bit little endian
  113. Uint8 ucBatteryLevel; // 52
  114. Uint8 ucConnectState; // 53 - 0x08 = USB, 0x01 = headphone
  115. // There's more unknown data at the end, and a 32-bit CRC on Bluetooth
  116. } PS5StatePacket_t;
  117. typedef struct
  118. {
  119. Uint8 ucLeftJoystickX; // 0
  120. Uint8 ucLeftJoystickY; // 1
  121. Uint8 ucRightJoystickX; // 2
  122. Uint8 ucRightJoystickY; // 3
  123. Uint8 ucTriggerLeft; // 4
  124. Uint8 ucTriggerRight; // 5
  125. Uint8 ucCounter; // 6
  126. Uint8 rgucButtonsAndHat[4]; // 7
  127. Uint8 rgucPacketSequence[4]; // 11 - 32 bit little endian
  128. Uint8 rgucGyroX[2]; // 15
  129. Uint8 rgucGyroY[2]; // 17
  130. Uint8 rgucGyroZ[2]; // 19
  131. Uint8 rgucAccelX[2]; // 21
  132. Uint8 rgucAccelY[2]; // 23
  133. Uint8 rgucAccelZ[2]; // 25
  134. Uint8 rgucSensorTimestamp[2]; // 27 - 16 bit little endian
  135. Uint8 ucBatteryLevel; // 29
  136. Uint8 ucUnknown; // 30
  137. Uint8 ucTouchpadCounter1; // 31 - high bit clear + counter
  138. Uint8 rgucTouchpadData1[3]; // 32 - X/Y, 12 bits per axis
  139. Uint8 ucTouchpadCounter2; // 35 - high bit clear + counter
  140. Uint8 rgucTouchpadData2[3]; // 36 - X/Y, 12 bits per axis
  141. // There's more unknown data at the end, and a 32-bit CRC on Bluetooth
  142. } PS5StatePacketAlt_t;
  143. typedef struct
  144. {
  145. Uint8 ucEnableBits1; // 0
  146. Uint8 ucEnableBits2; // 1
  147. Uint8 ucRumbleRight; // 2
  148. Uint8 ucRumbleLeft; // 3
  149. Uint8 ucHeadphoneVolume; // 4
  150. Uint8 ucSpeakerVolume; // 5
  151. Uint8 ucMicrophoneVolume; // 6
  152. Uint8 ucAudioEnableBits; // 7
  153. Uint8 ucMicLightMode; // 8
  154. Uint8 ucAudioMuteBits; // 9
  155. Uint8 rgucRightTriggerEffect[11]; // 10
  156. Uint8 rgucLeftTriggerEffect[11]; // 21
  157. Uint8 rgucUnknown1[6]; // 32
  158. Uint8 ucEnableBits3; // 38
  159. Uint8 rgucUnknown2[2]; // 39
  160. Uint8 ucLedAnim; // 41
  161. Uint8 ucLedBrightness; // 42
  162. Uint8 ucPadLights; // 43
  163. Uint8 ucLedRed; // 44
  164. Uint8 ucLedGreen; // 45
  165. Uint8 ucLedBlue; // 46
  166. } DS5EffectsState_t;
  167. typedef enum
  168. {
  169. k_EDS5EffectRumbleStart = (1 << 0),
  170. k_EDS5EffectRumble = (1 << 1),
  171. k_EDS5EffectLEDReset = (1 << 2),
  172. k_EDS5EffectLED = (1 << 3),
  173. k_EDS5EffectPadLights = (1 << 4),
  174. k_EDS5EffectMicLight = (1 << 5)
  175. } EDS5Effect;
  176. typedef enum
  177. {
  178. k_EDS5LEDResetStateNone,
  179. k_EDS5LEDResetStatePending,
  180. k_EDS5LEDResetStateComplete,
  181. } EDS5LEDResetState;
  182. typedef struct
  183. {
  184. Sint16 bias;
  185. float sensitivity;
  186. } IMUCalibrationData;
  187. /* Rumble hint mode:
  188. * "0": enhanced features are never used
  189. * "1": enhanced features are always used
  190. * "auto": enhanced features are advertised to the application, but SDL doesn't touch the controller state unless the application explicitly requests it.
  191. */
  192. typedef enum
  193. {
  194. PS5_ENHANCED_REPORT_HINT_OFF,
  195. PS5_ENHANCED_REPORT_HINT_ON,
  196. PS5_ENHANCED_REPORT_HINT_AUTO
  197. } HIDAPI_PS5_EnhancedReportHint;
  198. typedef struct
  199. {
  200. SDL_HIDAPI_Device *device;
  201. SDL_Joystick *joystick;
  202. bool is_dongle;
  203. bool use_alternate_report;
  204. bool sensors_supported;
  205. bool lightbar_supported;
  206. bool vibration_supported;
  207. bool playerled_supported;
  208. bool touchpad_supported;
  209. bool effects_supported;
  210. HIDAPI_PS5_EnhancedReportHint enhanced_report_hint;
  211. bool enhanced_reports;
  212. bool enhanced_mode;
  213. bool enhanced_mode_available;
  214. bool report_sensors;
  215. bool report_touchpad;
  216. bool report_battery;
  217. bool hardware_calibration;
  218. IMUCalibrationData calibration[6];
  219. Uint16 firmware_version;
  220. Uint64 last_packet;
  221. int player_index;
  222. bool player_lights;
  223. bool enhanced_rumble;
  224. Uint8 rumble_left;
  225. Uint8 rumble_right;
  226. bool color_set;
  227. Uint8 led_red;
  228. Uint8 led_green;
  229. Uint8 led_blue;
  230. EDS5LEDResetState led_reset_state;
  231. Uint64 sensor_ticks;
  232. Uint32 last_tick;
  233. union
  234. {
  235. PS5SimpleStatePacket_t simple;
  236. PS5StatePacketCommon_t state;
  237. PS5StatePacketAlt_t alt_state;
  238. PS5StatePacket_t full_state;
  239. Uint8 data[64];
  240. } last_state;
  241. } SDL_DriverPS5_Context;
  242. static bool HIDAPI_DriverPS5_InternalSendJoystickEffect(SDL_DriverPS5_Context *ctx, const void *effect, int size, bool application_usage);
  243. static void HIDAPI_DriverPS5_RegisterHints(SDL_HintCallback callback, void *userdata)
  244. {
  245. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5, callback, userdata);
  246. }
  247. static void HIDAPI_DriverPS5_UnregisterHints(SDL_HintCallback callback, void *userdata)
  248. {
  249. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5, callback, userdata);
  250. }
  251. static bool HIDAPI_DriverPS5_IsEnabled(void)
  252. {
  253. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  254. }
  255. static int ReadFeatureReport(SDL_hid_device *dev, Uint8 report_id, Uint8 *report, size_t length)
  256. {
  257. SDL_memset(report, 0, length);
  258. report[0] = report_id;
  259. return SDL_hid_get_feature_report(dev, report, length);
  260. }
  261. static bool HIDAPI_DriverPS5_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  262. {
  263. Uint8 data[USB_PACKET_LENGTH];
  264. int size;
  265. if (type == SDL_GAMEPAD_TYPE_PS5) {
  266. return true;
  267. }
  268. if (HIDAPI_SupportsPlaystationDetection(vendor_id, product_id)) {
  269. if (device && device->dev) {
  270. size = ReadFeatureReport(device->dev, k_EPS5FeatureReportIdCapabilities, data, sizeof(data));
  271. if (size == 48 && data[2] == 0x28) {
  272. // Supported third party controller
  273. return true;
  274. } else {
  275. return false;
  276. }
  277. } else {
  278. // Might be supported by this driver, enumerate and find out
  279. return true;
  280. }
  281. }
  282. return false;
  283. }
  284. static void SetLedsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
  285. {
  286. /* This list is the same as what hid-sony.c uses in the Linux kernel.
  287. The first 4 values correspond to what the PS4 assigns.
  288. */
  289. static const Uint8 colors[7][3] = {
  290. { 0x00, 0x00, 0x40 }, // Blue
  291. { 0x40, 0x00, 0x00 }, // Red
  292. { 0x00, 0x40, 0x00 }, // Green
  293. { 0x20, 0x00, 0x20 }, // Pink
  294. { 0x20, 0x10, 0x00 }, // Orange
  295. { 0x00, 0x10, 0x10 }, // Teal
  296. { 0x10, 0x10, 0x10 } // White
  297. };
  298. if (player_index >= 0) {
  299. player_index %= SDL_arraysize(colors);
  300. } else {
  301. player_index = 0;
  302. }
  303. effects->ucLedRed = colors[player_index][0];
  304. effects->ucLedGreen = colors[player_index][1];
  305. effects->ucLedBlue = colors[player_index][2];
  306. }
  307. static void SetLightsForPlayerIndex(DS5EffectsState_t *effects, int player_index)
  308. {
  309. static const Uint8 lights[] = {
  310. 0x04,
  311. 0x0A,
  312. 0x15,
  313. 0x1B,
  314. 0x1F
  315. };
  316. if (player_index >= 0) {
  317. // Bitmask, 0x1F enables all lights, 0x20 changes instantly instead of fade
  318. player_index %= SDL_arraysize(lights);
  319. effects->ucPadLights = lights[player_index] | 0x20;
  320. } else {
  321. effects->ucPadLights = 0x00;
  322. }
  323. }
  324. static bool HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
  325. {
  326. SDL_DriverPS5_Context *ctx;
  327. Uint8 data[USB_PACKET_LENGTH * 2];
  328. int size;
  329. char serial[18];
  330. SDL_JoystickType joystick_type = SDL_JOYSTICK_TYPE_GAMEPAD;
  331. ctx = (SDL_DriverPS5_Context *)SDL_calloc(1, sizeof(*ctx));
  332. if (!ctx) {
  333. return false;
  334. }
  335. ctx->device = device;
  336. device->context = ctx;
  337. if (device->serial && SDL_strlen(device->serial) == 12) {
  338. int i, j;
  339. j = -1;
  340. for (i = 0; i < 12; i += 2) {
  341. j += 1;
  342. SDL_memmove(&serial[j], &device->serial[i], 2);
  343. j += 2;
  344. serial[j] = '-';
  345. }
  346. serial[j] = '\0';
  347. } else {
  348. serial[0] = '\0';
  349. }
  350. // Read a report to see what mode we're in
  351. size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 16);
  352. #ifdef DEBUG_PS5_PROTOCOL
  353. if (size > 0) {
  354. HIDAPI_DumpPacket("PS5 first packet: size = %d", data, size);
  355. } else {
  356. SDL_Log("PS5 first packet: size = %d", size);
  357. }
  358. #endif
  359. if (size == 64) {
  360. // Connected over USB
  361. ctx->enhanced_reports = true;
  362. } else if (size > 0 && data[0] == k_EPS5ReportIdBluetoothEffects) {
  363. // Connected over Bluetooth, using enhanced reports
  364. ctx->enhanced_reports = true;
  365. } else {
  366. // Connected over Bluetooth, using simple reports (DirectInput enabled)
  367. }
  368. if (device->vendor_id == USB_VENDOR_SONY && ctx->enhanced_reports) {
  369. /* Read the serial number (Bluetooth address in reverse byte order)
  370. This will also enable enhanced reports over Bluetooth
  371. */
  372. if (ReadFeatureReport(device->dev, k_EPS5FeatureReportIdSerialNumber, data, sizeof(data)) >= 7) {
  373. (void)SDL_snprintf(serial, sizeof(serial), "%.2x-%.2x-%.2x-%.2x-%.2x-%.2x",
  374. data[6], data[5], data[4], data[3], data[2], data[1]);
  375. }
  376. /* Read the firmware version
  377. This will also enable enhanced reports over Bluetooth
  378. */
  379. if (ReadFeatureReport(device->dev, k_EPS5FeatureReportIdFirmwareInfo, data, USB_PACKET_LENGTH) >= 46) {
  380. ctx->firmware_version = (Uint16)data[44] | ((Uint16)data[45] << 8);
  381. }
  382. }
  383. if (device->vendor_id == USB_VENDOR_SONY) {
  384. if (device->product_id == USB_PRODUCT_SONY_DS5_EDGE ||
  385. ctx->firmware_version == 0 || // Assume that it's updated firmware over Bluetooth
  386. ctx->firmware_version >= 0x0224) {
  387. ctx->enhanced_rumble = true;
  388. }
  389. }
  390. // Get the device capabilities
  391. if (device->vendor_id == USB_VENDOR_SONY) {
  392. ctx->sensors_supported = true;
  393. ctx->lightbar_supported = true;
  394. ctx->vibration_supported = true;
  395. ctx->playerled_supported = true;
  396. ctx->touchpad_supported = true;
  397. } else {
  398. // Third party controller capability request
  399. size = ReadFeatureReport(device->dev, k_EPS5FeatureReportIdCapabilities, data, sizeof(data));
  400. if (size == 48 && data[2] == 0x28) {
  401. Uint8 capabilities = data[4];
  402. Uint8 capabilities2 = data[20];
  403. Uint8 device_type = data[5];
  404. #ifdef DEBUG_PS5_PROTOCOL
  405. HIDAPI_DumpPacket("PS5 capabilities: size = %d", data, size);
  406. #endif
  407. if (capabilities & 0x02) {
  408. ctx->sensors_supported = true;
  409. }
  410. if (capabilities & 0x04) {
  411. ctx->lightbar_supported = true;
  412. }
  413. if (capabilities & 0x08) {
  414. ctx->vibration_supported = true;
  415. }
  416. if (capabilities & 0x40) {
  417. ctx->touchpad_supported = true;
  418. }
  419. if (capabilities2 & 0x80) {
  420. ctx->playerled_supported = true;
  421. }
  422. switch (device_type) {
  423. case 0x00:
  424. joystick_type = SDL_JOYSTICK_TYPE_GAMEPAD;
  425. break;
  426. case 0x01:
  427. joystick_type = SDL_JOYSTICK_TYPE_GUITAR;
  428. break;
  429. case 0x02:
  430. joystick_type = SDL_JOYSTICK_TYPE_DRUM_KIT;
  431. break;
  432. case 0x06:
  433. joystick_type = SDL_JOYSTICK_TYPE_WHEEL;
  434. break;
  435. case 0x07:
  436. joystick_type = SDL_JOYSTICK_TYPE_ARCADE_STICK;
  437. break;
  438. case 0x08:
  439. joystick_type = SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  440. break;
  441. default:
  442. joystick_type = SDL_JOYSTICK_TYPE_UNKNOWN;
  443. break;
  444. }
  445. ctx->use_alternate_report = true;
  446. if (device->vendor_id == USB_VENDOR_NACON_ALT &&
  447. (device->product_id == USB_PRODUCT_NACON_REVOLUTION_5_PRO_PS5_WIRED ||
  448. device->product_id == USB_PRODUCT_NACON_REVOLUTION_5_PRO_PS5_WIRELESS)) {
  449. // This doesn't report vibration capability, but it can do rumble
  450. ctx->vibration_supported = true;
  451. }
  452. } else if (device->vendor_id == USB_VENDOR_RAZER &&
  453. (device->product_id == USB_PRODUCT_RAZER_WOLVERINE_V2_PRO_PS5_WIRED ||
  454. device->product_id == USB_PRODUCT_RAZER_WOLVERINE_V2_PRO_PS5_WIRELESS)) {
  455. // The Razer Wolverine V2 Pro doesn't respond to the detection protocol, but has a touchpad and sensors and no vibration
  456. ctx->sensors_supported = true;
  457. ctx->touchpad_supported = true;
  458. ctx->use_alternate_report = true;
  459. } else if (device->vendor_id == USB_VENDOR_RAZER &&
  460. (device->product_id == USB_PRODUCT_RAZER_KITSUNE ||
  461. device->product_id == USB_PRODUCT_RAZER_RAIJU_V3_PRO_PS5_WIRED ||
  462. device->product_id == USB_PRODUCT_RAZER_RAIJU_V3_PRO_PS5_WIRELESS)) {
  463. // The Razer Kitsune and Raiju don't respond to the detection protocol, but have a touchpad
  464. joystick_type = SDL_JOYSTICK_TYPE_ARCADE_STICK;
  465. ctx->touchpad_supported = true;
  466. ctx->use_alternate_report = true;
  467. }
  468. }
  469. ctx->effects_supported = (ctx->lightbar_supported || ctx->vibration_supported || ctx->playerled_supported);
  470. if ((device->vendor_id == USB_VENDOR_NACON_ALT &&
  471. device->product_id == USB_PRODUCT_NACON_REVOLUTION_5_PRO_PS5_WIRELESS) ||
  472. (device->vendor_id == USB_VENDOR_RAZER &&
  473. (device->product_id == USB_PRODUCT_NACON_REVOLUTION_5_PRO_PS5_WIRELESS ||
  474. device->product_id == USB_PRODUCT_RAZER_WOLVERINE_V2_PRO_PS5_WIRELESS ||
  475. device->product_id == USB_PRODUCT_RAZER_RAIJU_V3_PRO_PS5_WIRELESS))) {
  476. ctx->is_dongle = true;
  477. }
  478. device->joystick_type = joystick_type;
  479. device->type = SDL_GAMEPAD_TYPE_PS5;
  480. if (device->vendor_id == USB_VENDOR_SONY) {
  481. if (SDL_IsJoystickDualSenseEdge(device->vendor_id, device->product_id)) {
  482. HIDAPI_SetDeviceName(device, "DualSense Edge Wireless Controller");
  483. } else {
  484. HIDAPI_SetDeviceName(device, "DualSense Wireless Controller");
  485. }
  486. }
  487. HIDAPI_SetDeviceSerial(device, serial);
  488. if (ctx->is_dongle) {
  489. // We don't know if this is connected yet, wait for reports
  490. return true;
  491. }
  492. // Prefer the USB device over the Bluetooth device
  493. if (device->is_bluetooth) {
  494. if (HIDAPI_HasConnectedUSBDevice(device->serial)) {
  495. return true;
  496. }
  497. } else {
  498. HIDAPI_DisconnectBluetoothDevice(device->serial);
  499. }
  500. return HIDAPI_JoystickConnected(device, NULL);
  501. }
  502. static int HIDAPI_DriverPS5_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  503. {
  504. return -1;
  505. }
  506. static void HIDAPI_DriverPS5_LoadCalibrationData(SDL_HIDAPI_Device *device)
  507. {
  508. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  509. int i, size;
  510. Uint8 data[USB_PACKET_LENGTH];
  511. size = ReadFeatureReport(device->dev, k_EPS5FeatureReportIdCalibration, data, sizeof(data));
  512. if (size < 35) {
  513. #ifdef DEBUG_PS5_CALIBRATION
  514. SDL_Log("Short read of calibration data: %d, ignoring calibration", size);
  515. #endif
  516. return;
  517. }
  518. {
  519. Sint16 sGyroPitchBias, sGyroYawBias, sGyroRollBias;
  520. Sint16 sGyroPitchPlus, sGyroPitchMinus;
  521. Sint16 sGyroYawPlus, sGyroYawMinus;
  522. Sint16 sGyroRollPlus, sGyroRollMinus;
  523. Sint16 sGyroSpeedPlus, sGyroSpeedMinus;
  524. Sint16 sAccXPlus, sAccXMinus;
  525. Sint16 sAccYPlus, sAccYMinus;
  526. Sint16 sAccZPlus, sAccZMinus;
  527. float flNumerator;
  528. Sint16 sRange2g;
  529. #ifdef DEBUG_PS5_CALIBRATION
  530. HIDAPI_DumpPacket("PS5 calibration packet: size = %d", data, size);
  531. #endif
  532. sGyroPitchBias = LOAD16(data[1], data[2]);
  533. sGyroYawBias = LOAD16(data[3], data[4]);
  534. sGyroRollBias = LOAD16(data[5], data[6]);
  535. sGyroPitchPlus = LOAD16(data[7], data[8]);
  536. sGyroPitchMinus = LOAD16(data[9], data[10]);
  537. sGyroYawPlus = LOAD16(data[11], data[12]);
  538. sGyroYawMinus = LOAD16(data[13], data[14]);
  539. sGyroRollPlus = LOAD16(data[15], data[16]);
  540. sGyroRollMinus = LOAD16(data[17], data[18]);
  541. sGyroSpeedPlus = LOAD16(data[19], data[20]);
  542. sGyroSpeedMinus = LOAD16(data[21], data[22]);
  543. sAccXPlus = LOAD16(data[23], data[24]);
  544. sAccXMinus = LOAD16(data[25], data[26]);
  545. sAccYPlus = LOAD16(data[27], data[28]);
  546. sAccYMinus = LOAD16(data[29], data[30]);
  547. sAccZPlus = LOAD16(data[31], data[32]);
  548. sAccZMinus = LOAD16(data[33], data[34]);
  549. flNumerator = (sGyroSpeedPlus + sGyroSpeedMinus) * GYRO_RES_PER_DEGREE;
  550. ctx->calibration[0].bias = sGyroPitchBias;
  551. ctx->calibration[0].sensitivity = flNumerator / (sGyroPitchPlus - sGyroPitchMinus);
  552. ctx->calibration[1].bias = sGyroYawBias;
  553. ctx->calibration[1].sensitivity = flNumerator / (sGyroYawPlus - sGyroYawMinus);
  554. ctx->calibration[2].bias = sGyroRollBias;
  555. ctx->calibration[2].sensitivity = flNumerator / (sGyroRollPlus - sGyroRollMinus);
  556. sRange2g = sAccXPlus - sAccXMinus;
  557. ctx->calibration[3].bias = sAccXPlus - sRange2g / 2;
  558. ctx->calibration[3].sensitivity = 2.0f * ACCEL_RES_PER_G / (float)sRange2g;
  559. sRange2g = sAccYPlus - sAccYMinus;
  560. ctx->calibration[4].bias = sAccYPlus - sRange2g / 2;
  561. ctx->calibration[4].sensitivity = 2.0f * ACCEL_RES_PER_G / (float)sRange2g;
  562. sRange2g = sAccZPlus - sAccZMinus;
  563. ctx->calibration[5].bias = sAccZPlus - sRange2g / 2;
  564. ctx->calibration[5].sensitivity = 2.0f * ACCEL_RES_PER_G / (float)sRange2g;
  565. ctx->hardware_calibration = true;
  566. for (i = 0; i < 6; ++i) {
  567. float divisor = (i < 3 ? 64.0f : 1.0f);
  568. #ifdef DEBUG_PS5_CALIBRATION
  569. SDL_Log("calibration[%d] bias = %d, sensitivity = %f", i, ctx->calibration[i].bias, ctx->calibration[i].sensitivity);
  570. #endif
  571. // Some controllers have a bad calibration
  572. if ((SDL_abs(ctx->calibration[i].bias) > 1024) || (SDL_fabsf(1.0f - ctx->calibration[i].sensitivity / divisor) > 0.5f)) {
  573. #ifdef DEBUG_PS5_CALIBRATION
  574. SDL_Log("invalid calibration, ignoring");
  575. #endif
  576. ctx->hardware_calibration = false;
  577. }
  578. }
  579. }
  580. }
  581. static float HIDAPI_DriverPS5_ApplyCalibrationData(SDL_DriverPS5_Context *ctx, int index, Sint16 value)
  582. {
  583. float result;
  584. if (ctx->hardware_calibration) {
  585. IMUCalibrationData *calibration = &ctx->calibration[index];
  586. result = (value - calibration->bias) * calibration->sensitivity;
  587. } else if (index < 3) {
  588. result = value * 64.f;
  589. } else {
  590. result = value;
  591. }
  592. // Convert the raw data to the units expected by SDL
  593. if (index < 3) {
  594. result = (result / GYRO_RES_PER_DEGREE) * SDL_PI_F / 180.0f;
  595. } else {
  596. result = (result / ACCEL_RES_PER_G) * SDL_STANDARD_GRAVITY;
  597. }
  598. return result;
  599. }
  600. static bool HIDAPI_DriverPS5_UpdateEffects(SDL_DriverPS5_Context *ctx, int effect_mask, bool application_usage)
  601. {
  602. DS5EffectsState_t effects;
  603. // Make sure the Bluetooth connection sequence has completed before sending LED color change
  604. if (ctx->device->is_bluetooth && ctx->enhanced_reports &&
  605. (effect_mask & (k_EDS5EffectLED | k_EDS5EffectPadLights)) != 0) {
  606. if (ctx->led_reset_state != k_EDS5LEDResetStateComplete) {
  607. ctx->led_reset_state = k_EDS5LEDResetStatePending;
  608. return true;
  609. }
  610. }
  611. SDL_zero(effects);
  612. if (ctx->vibration_supported) {
  613. if (ctx->rumble_left || ctx->rumble_right) {
  614. if (ctx->enhanced_rumble) {
  615. effects.ucEnableBits3 |= 0x04; // Enable improved rumble emulation on 2.24 firmware and newer
  616. effects.ucRumbleLeft = ctx->rumble_left;
  617. effects.ucRumbleRight = ctx->rumble_right;
  618. } else {
  619. effects.ucEnableBits1 |= 0x01; // Enable rumble emulation
  620. // Shift to reduce effective rumble strength to match Xbox controllers
  621. effects.ucRumbleLeft = ctx->rumble_left >> 1;
  622. effects.ucRumbleRight = ctx->rumble_right >> 1;
  623. }
  624. effects.ucEnableBits1 |= 0x02; // Disable audio haptics
  625. } else {
  626. // Leaving emulated rumble bits off will restore audio haptics
  627. }
  628. if ((effect_mask & k_EDS5EffectRumbleStart) != 0) {
  629. effects.ucEnableBits1 |= 0x02; // Disable audio haptics
  630. }
  631. if ((effect_mask & k_EDS5EffectRumble) != 0) {
  632. // Already handled above
  633. }
  634. }
  635. if (ctx->lightbar_supported) {
  636. if ((effect_mask & k_EDS5EffectLEDReset) != 0) {
  637. effects.ucEnableBits2 |= 0x08; // Reset LED state
  638. }
  639. if ((effect_mask & k_EDS5EffectLED) != 0) {
  640. effects.ucEnableBits2 |= 0x04; // Enable LED color
  641. // Populate the LED state with the appropriate color from our lookup table
  642. if (ctx->color_set) {
  643. effects.ucLedRed = ctx->led_red;
  644. effects.ucLedGreen = ctx->led_green;
  645. effects.ucLedBlue = ctx->led_blue;
  646. } else {
  647. SetLedsForPlayerIndex(&effects, ctx->player_index);
  648. }
  649. }
  650. }
  651. if (ctx->playerled_supported) {
  652. if ((effect_mask & k_EDS5EffectPadLights) != 0) {
  653. effects.ucEnableBits2 |= 0x10; // Enable touchpad lights
  654. if (ctx->player_lights) {
  655. SetLightsForPlayerIndex(&effects, ctx->player_index);
  656. } else {
  657. effects.ucPadLights = 0x00;
  658. }
  659. }
  660. }
  661. if ((effect_mask & k_EDS5EffectMicLight) != 0) {
  662. effects.ucEnableBits2 |= 0x01; // Enable microphone light
  663. effects.ucMicLightMode = 0; // Bitmask, 0x00 = off, 0x01 = solid, 0x02 = pulse
  664. }
  665. return HIDAPI_DriverPS5_InternalSendJoystickEffect(ctx, &effects, sizeof(effects), application_usage);
  666. }
  667. static void HIDAPI_DriverPS5_CheckPendingLEDReset(SDL_DriverPS5_Context *ctx)
  668. {
  669. bool led_reset_complete = false;
  670. if (ctx->enhanced_reports && ctx->sensors_supported && !ctx->use_alternate_report) {
  671. const PS5StatePacketCommon_t *packet = &ctx->last_state.state;
  672. // Check the timer to make sure the Bluetooth connection LED animation is complete
  673. const Uint32 connection_complete = 10200000;
  674. Uint32 timestamp = LOAD32(packet->rgucSensorTimestamp[0],
  675. packet->rgucSensorTimestamp[1],
  676. packet->rgucSensorTimestamp[2],
  677. packet->rgucSensorTimestamp[3]);
  678. if (timestamp >= connection_complete) {
  679. led_reset_complete = true;
  680. }
  681. } else {
  682. // We don't know how to check the timer, just assume it's complete for now
  683. led_reset_complete = true;
  684. }
  685. if (led_reset_complete) {
  686. HIDAPI_DriverPS5_UpdateEffects(ctx, k_EDS5EffectLEDReset, false);
  687. ctx->led_reset_state = k_EDS5LEDResetStateComplete;
  688. HIDAPI_DriverPS5_UpdateEffects(ctx, (k_EDS5EffectLED | k_EDS5EffectPadLights), false);
  689. }
  690. }
  691. static void HIDAPI_DriverPS5_TickleBluetooth(SDL_HIDAPI_Device *device)
  692. {
  693. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  694. if (ctx->enhanced_reports) {
  695. // This is just a dummy packet that should have no effect, since we don't set the CRC
  696. Uint8 data[78];
  697. SDL_zeroa(data);
  698. data[0] = k_EPS5ReportIdBluetoothEffects;
  699. data[1] = 0x02; // Magic value
  700. if (SDL_HIDAPI_LockRumble()) {
  701. SDL_HIDAPI_SendRumbleAndUnlock(device, data, sizeof(data));
  702. }
  703. } else {
  704. // We can't even send an invalid effects packet, or it will put the controller in enhanced mode
  705. if (device->num_joysticks > 0) {
  706. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  707. }
  708. }
  709. }
  710. static void HIDAPI_DriverPS5_SetEnhancedModeAvailable(SDL_DriverPS5_Context *ctx)
  711. {
  712. if (ctx->enhanced_mode_available) {
  713. return;
  714. }
  715. ctx->enhanced_mode_available = true;
  716. if (ctx->touchpad_supported) {
  717. SDL_PrivateJoystickAddTouchpad(ctx->joystick, 2);
  718. ctx->report_touchpad = true;
  719. }
  720. if (ctx->sensors_supported) {
  721. // Standard DualSense sensor update rate is 250 Hz over USB
  722. float update_rate = 250.0f;
  723. if (ctx->device->is_bluetooth) {
  724. // Bluetooth sensor update rate appears to be 1000 Hz
  725. update_rate = 1000.0f;
  726. } else if (SDL_IsJoystickDualSenseEdge(ctx->device->vendor_id, ctx->device->product_id)) {
  727. // DualSense Edge sensor update rate is 1000 Hz over USB
  728. update_rate = 1000.0f;
  729. }
  730. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_GYRO, update_rate);
  731. SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL, update_rate);
  732. }
  733. ctx->report_battery = true;
  734. HIDAPI_UpdateDeviceProperties(ctx->device);
  735. }
  736. static void HIDAPI_DriverPS5_SetEnhancedMode(SDL_DriverPS5_Context *ctx)
  737. {
  738. HIDAPI_DriverPS5_SetEnhancedModeAvailable(ctx);
  739. if (!ctx->enhanced_mode) {
  740. ctx->enhanced_mode = true;
  741. // Switch into enhanced report mode
  742. HIDAPI_DriverPS5_UpdateEffects(ctx, 0, false);
  743. // Update the light effects
  744. HIDAPI_DriverPS5_UpdateEffects(ctx, (k_EDS5EffectLED | k_EDS5EffectPadLights), false);
  745. }
  746. }
  747. static void HIDAPI_DriverPS5_SetEnhancedReportHint(SDL_DriverPS5_Context *ctx, HIDAPI_PS5_EnhancedReportHint enhanced_report_hint)
  748. {
  749. switch (enhanced_report_hint) {
  750. case PS5_ENHANCED_REPORT_HINT_OFF:
  751. // Nothing to do, enhanced mode is a one-way ticket
  752. break;
  753. case PS5_ENHANCED_REPORT_HINT_ON:
  754. HIDAPI_DriverPS5_SetEnhancedMode(ctx);
  755. break;
  756. case PS5_ENHANCED_REPORT_HINT_AUTO:
  757. HIDAPI_DriverPS5_SetEnhancedModeAvailable(ctx);
  758. break;
  759. }
  760. ctx->enhanced_report_hint = enhanced_report_hint;
  761. }
  762. static void HIDAPI_DriverPS5_UpdateEnhancedModeOnEnhancedReport(SDL_DriverPS5_Context *ctx)
  763. {
  764. ctx->enhanced_reports = true;
  765. if (ctx->enhanced_report_hint == PS5_ENHANCED_REPORT_HINT_AUTO) {
  766. HIDAPI_DriverPS5_SetEnhancedReportHint(ctx, PS5_ENHANCED_REPORT_HINT_ON);
  767. }
  768. }
  769. static void HIDAPI_DriverPS5_UpdateEnhancedModeOnApplicationUsage(SDL_DriverPS5_Context *ctx)
  770. {
  771. if (ctx->enhanced_report_hint == PS5_ENHANCED_REPORT_HINT_AUTO) {
  772. HIDAPI_DriverPS5_SetEnhancedReportHint(ctx, PS5_ENHANCED_REPORT_HINT_ON);
  773. }
  774. }
  775. static void SDLCALL SDL_PS5EnhancedReportsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  776. {
  777. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)userdata;
  778. if (ctx->device->is_bluetooth) {
  779. if (hint && SDL_strcasecmp(hint, "auto") == 0) {
  780. HIDAPI_DriverPS5_SetEnhancedReportHint(ctx, PS5_ENHANCED_REPORT_HINT_AUTO);
  781. } else if (SDL_GetStringBoolean(hint, true)) {
  782. HIDAPI_DriverPS5_SetEnhancedReportHint(ctx, PS5_ENHANCED_REPORT_HINT_ON);
  783. } else {
  784. HIDAPI_DriverPS5_SetEnhancedReportHint(ctx, PS5_ENHANCED_REPORT_HINT_OFF);
  785. }
  786. } else {
  787. HIDAPI_DriverPS5_SetEnhancedReportHint(ctx, PS5_ENHANCED_REPORT_HINT_ON);
  788. }
  789. }
  790. static void SDLCALL SDL_PS5PlayerLEDHintChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  791. {
  792. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)userdata;
  793. bool player_lights = SDL_GetStringBoolean(hint, true);
  794. if (player_lights != ctx->player_lights) {
  795. ctx->player_lights = player_lights;
  796. HIDAPI_DriverPS5_UpdateEffects(ctx, k_EDS5EffectPadLights, false);
  797. }
  798. }
  799. static void HIDAPI_DriverPS5_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  800. {
  801. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  802. if (!ctx->joystick) {
  803. return;
  804. }
  805. ctx->player_index = player_index;
  806. // This will set the new LED state based on the new player index
  807. // SDL automatically calls this, so it doesn't count as an application action to enable enhanced mode
  808. HIDAPI_DriverPS5_UpdateEffects(ctx, (k_EDS5EffectLED | k_EDS5EffectPadLights), false);
  809. }
  810. static bool HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  811. {
  812. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  813. SDL_AssertJoysticksLocked();
  814. ctx->joystick = joystick;
  815. ctx->last_packet = SDL_GetTicks();
  816. ctx->report_sensors = false;
  817. ctx->report_touchpad = false;
  818. ctx->rumble_left = 0;
  819. ctx->rumble_right = 0;
  820. ctx->color_set = false;
  821. ctx->led_reset_state = k_EDS5LEDResetStateNone;
  822. SDL_zero(ctx->last_state);
  823. // Initialize player index (needed for setting LEDs)
  824. ctx->player_index = SDL_GetJoystickPlayerIndex(joystick);
  825. ctx->player_lights = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, true);
  826. // Initialize the joystick capabilities
  827. if (SDL_IsJoystickDualSenseEdge(device->vendor_id, device->product_id)) {
  828. joystick->nbuttons = 17; // paddles and touchpad and microphone
  829. } else if (ctx->touchpad_supported) {
  830. joystick->nbuttons = 13; // touchpad and microphone
  831. } else {
  832. joystick->nbuttons = 11;
  833. }
  834. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  835. joystick->nhats = 1;
  836. joystick->firmware_version = ctx->firmware_version;
  837. if (ctx->is_dongle) {
  838. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
  839. }
  840. SDL_AddHintCallback(SDL_HINT_JOYSTICK_ENHANCED_REPORTS,
  841. SDL_PS5EnhancedReportsChanged, ctx);
  842. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
  843. SDL_PS5PlayerLEDHintChanged, ctx);
  844. return true;
  845. }
  846. static bool HIDAPI_DriverPS5_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  847. {
  848. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  849. if (!ctx->vibration_supported) {
  850. return SDL_Unsupported();
  851. }
  852. if (!ctx->rumble_left && !ctx->rumble_right) {
  853. HIDAPI_DriverPS5_UpdateEffects(ctx, k_EDS5EffectRumbleStart, true);
  854. }
  855. ctx->rumble_left = (low_frequency_rumble >> 8);
  856. ctx->rumble_right = (high_frequency_rumble >> 8);
  857. return HIDAPI_DriverPS5_UpdateEffects(ctx, k_EDS5EffectRumble, true);
  858. }
  859. static bool HIDAPI_DriverPS5_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  860. {
  861. return SDL_Unsupported();
  862. }
  863. static Uint32 HIDAPI_DriverPS5_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  864. {
  865. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  866. Uint32 result = 0;
  867. if (ctx->enhanced_mode_available) {
  868. if (ctx->lightbar_supported) {
  869. result |= SDL_JOYSTICK_CAP_RGB_LED;
  870. }
  871. if (ctx->playerled_supported) {
  872. result |= SDL_JOYSTICK_CAP_PLAYER_LED;
  873. }
  874. if (ctx->vibration_supported) {
  875. result |= SDL_JOYSTICK_CAP_RUMBLE;
  876. }
  877. }
  878. return result;
  879. }
  880. static bool HIDAPI_DriverPS5_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  881. {
  882. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  883. if (!ctx->lightbar_supported) {
  884. return SDL_Unsupported();
  885. }
  886. ctx->color_set = true;
  887. ctx->led_red = red;
  888. ctx->led_green = green;
  889. ctx->led_blue = blue;
  890. return HIDAPI_DriverPS5_UpdateEffects(ctx, k_EDS5EffectLED, true);
  891. }
  892. static bool HIDAPI_DriverPS5_InternalSendJoystickEffect(SDL_DriverPS5_Context *ctx, const void *effect, int size, bool application_usage)
  893. {
  894. Uint8 data[78];
  895. int report_size, offset;
  896. Uint8 *pending_data;
  897. int *pending_size;
  898. int maximum_size;
  899. if (!ctx->effects_supported) {
  900. // We shouldn't be sending packets to this controller
  901. return SDL_Unsupported();
  902. }
  903. if (!ctx->enhanced_mode) {
  904. if (application_usage) {
  905. HIDAPI_DriverPS5_UpdateEnhancedModeOnApplicationUsage(ctx);
  906. // Wait briefly before sending additional effects
  907. SDL_Delay(10);
  908. }
  909. if (!ctx->enhanced_mode) {
  910. // We're not in enhanced mode, effects aren't allowed
  911. return SDL_Unsupported();
  912. }
  913. }
  914. SDL_zeroa(data);
  915. if (ctx->device->is_bluetooth) {
  916. data[0] = k_EPS5ReportIdBluetoothEffects;
  917. data[1] = 0x00; // Tag and sequence
  918. data[2] = 0x10; // Magic value
  919. report_size = 78;
  920. offset = 3;
  921. } else {
  922. data[0] = k_EPS5ReportIdUsbEffects;
  923. report_size = 48;
  924. offset = 1;
  925. }
  926. SDL_memcpy(&data[offset], effect, SDL_min((sizeof(data) - offset), (size_t)size));
  927. if (ctx->device->is_bluetooth) {
  928. // Bluetooth reports need a CRC at the end of the packet (at least on Linux)
  929. Uint8 ubHdr = 0xA2; // hidp header is part of the CRC calculation
  930. Uint32 unCRC;
  931. unCRC = SDL_crc32(0, &ubHdr, 1);
  932. unCRC = SDL_crc32(unCRC, data, (size_t)(report_size - sizeof(unCRC)));
  933. SDL_memcpy(&data[report_size - sizeof(unCRC)], &unCRC, sizeof(unCRC));
  934. }
  935. if (!SDL_HIDAPI_LockRumble()) {
  936. return false;
  937. }
  938. // See if we can update an existing pending request
  939. if (SDL_HIDAPI_GetPendingRumbleLocked(ctx->device, &pending_data, &pending_size, &maximum_size)) {
  940. DS5EffectsState_t *effects = (DS5EffectsState_t *)&data[offset];
  941. DS5EffectsState_t *pending_effects = (DS5EffectsState_t *)&pending_data[offset];
  942. if (report_size == *pending_size &&
  943. effects->ucEnableBits1 == pending_effects->ucEnableBits1 &&
  944. effects->ucEnableBits2 == pending_effects->ucEnableBits2) {
  945. // We're simply updating the data for this request
  946. SDL_memcpy(pending_data, data, report_size);
  947. SDL_HIDAPI_UnlockRumble();
  948. return true;
  949. }
  950. }
  951. if (SDL_HIDAPI_SendRumbleAndUnlock(ctx->device, data, report_size) != report_size) {
  952. return false;
  953. }
  954. return true;
  955. }
  956. static bool HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size)
  957. {
  958. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  959. return HIDAPI_DriverPS5_InternalSendJoystickEffect(ctx, effect, size, true);
  960. }
  961. static bool HIDAPI_DriverPS5_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  962. {
  963. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  964. HIDAPI_DriverPS5_UpdateEnhancedModeOnApplicationUsage(ctx);
  965. if (!ctx->sensors_supported || (enabled && !ctx->enhanced_mode)) {
  966. return SDL_Unsupported();
  967. }
  968. if (enabled) {
  969. HIDAPI_DriverPS5_LoadCalibrationData(device);
  970. }
  971. ctx->report_sensors = enabled;
  972. return true;
  973. }
  974. static void HIDAPI_DriverPS5_HandleSimpleStatePacket(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5SimpleStatePacket_t *packet, Uint64 timestamp)
  975. {
  976. Sint16 axis;
  977. if (ctx->last_state.simple.rgucButtonsHatAndCounter[0] != packet->rgucButtonsHatAndCounter[0]) {
  978. {
  979. Uint8 data = (packet->rgucButtonsHatAndCounter[0] >> 4);
  980. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data & 0x01) != 0));
  981. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data & 0x02) != 0));
  982. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data & 0x04) != 0));
  983. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data & 0x08) != 0));
  984. }
  985. {
  986. Uint8 data = (packet->rgucButtonsHatAndCounter[0] & 0x0F);
  987. Uint8 hat;
  988. switch (data) {
  989. case 0:
  990. hat = SDL_HAT_UP;
  991. break;
  992. case 1:
  993. hat = SDL_HAT_RIGHTUP;
  994. break;
  995. case 2:
  996. hat = SDL_HAT_RIGHT;
  997. break;
  998. case 3:
  999. hat = SDL_HAT_RIGHTDOWN;
  1000. break;
  1001. case 4:
  1002. hat = SDL_HAT_DOWN;
  1003. break;
  1004. case 5:
  1005. hat = SDL_HAT_LEFTDOWN;
  1006. break;
  1007. case 6:
  1008. hat = SDL_HAT_LEFT;
  1009. break;
  1010. case 7:
  1011. hat = SDL_HAT_LEFTUP;
  1012. break;
  1013. default:
  1014. hat = SDL_HAT_CENTERED;
  1015. break;
  1016. }
  1017. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1018. }
  1019. }
  1020. if (ctx->last_state.simple.rgucButtonsHatAndCounter[1] != packet->rgucButtonsHatAndCounter[1]) {
  1021. Uint8 data = packet->rgucButtonsHatAndCounter[1];
  1022. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x01) != 0));
  1023. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x02) != 0));
  1024. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data & 0x10) != 0));
  1025. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x20) != 0));
  1026. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x40) != 0));
  1027. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data & 0x80) != 0));
  1028. }
  1029. if (ctx->last_state.simple.rgucButtonsHatAndCounter[2] != packet->rgucButtonsHatAndCounter[2]) {
  1030. Uint8 data = (packet->rgucButtonsHatAndCounter[2] & 0x03);
  1031. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x01) != 0));
  1032. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_PS5_TOUCHPAD, ((data & 0x02) != 0));
  1033. }
  1034. if (packet->ucTriggerLeft == 0 && (packet->rgucButtonsHatAndCounter[1] & 0x04)) {
  1035. axis = SDL_JOYSTICK_AXIS_MAX;
  1036. } else {
  1037. axis = ((int)packet->ucTriggerLeft * 257) - 32768;
  1038. }
  1039. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  1040. if (packet->ucTriggerRight == 0 && (packet->rgucButtonsHatAndCounter[1] & 0x08)) {
  1041. axis = SDL_JOYSTICK_AXIS_MAX;
  1042. } else {
  1043. axis = ((int)packet->ucTriggerRight * 257) - 32768;
  1044. }
  1045. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  1046. axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
  1047. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  1048. axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
  1049. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  1050. axis = ((int)packet->ucRightJoystickX * 257) - 32768;
  1051. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  1052. axis = ((int)packet->ucRightJoystickY * 257) - 32768;
  1053. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  1054. SDL_memcpy(&ctx->last_state.simple, packet, sizeof(ctx->last_state.simple));
  1055. }
  1056. static void HIDAPI_DriverPS5_HandleStatePacketCommon(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacketCommon_t *packet, Uint64 timestamp)
  1057. {
  1058. Sint16 axis;
  1059. if (ctx->last_state.state.rgucButtonsAndHat[0] != packet->rgucButtonsAndHat[0]) {
  1060. {
  1061. Uint8 data = (packet->rgucButtonsAndHat[0] >> 4);
  1062. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data & 0x01) != 0));
  1063. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data & 0x02) != 0));
  1064. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data & 0x04) != 0));
  1065. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data & 0x08) != 0));
  1066. }
  1067. {
  1068. Uint8 data = (packet->rgucButtonsAndHat[0] & 0x0F);
  1069. Uint8 hat;
  1070. switch (data) {
  1071. case 0:
  1072. hat = SDL_HAT_UP;
  1073. break;
  1074. case 1:
  1075. hat = SDL_HAT_RIGHTUP;
  1076. break;
  1077. case 2:
  1078. hat = SDL_HAT_RIGHT;
  1079. break;
  1080. case 3:
  1081. hat = SDL_HAT_RIGHTDOWN;
  1082. break;
  1083. case 4:
  1084. hat = SDL_HAT_DOWN;
  1085. break;
  1086. case 5:
  1087. hat = SDL_HAT_LEFTDOWN;
  1088. break;
  1089. case 6:
  1090. hat = SDL_HAT_LEFT;
  1091. break;
  1092. case 7:
  1093. hat = SDL_HAT_LEFTUP;
  1094. break;
  1095. default:
  1096. hat = SDL_HAT_CENTERED;
  1097. break;
  1098. }
  1099. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  1100. }
  1101. }
  1102. if (ctx->last_state.state.rgucButtonsAndHat[1] != packet->rgucButtonsAndHat[1]) {
  1103. Uint8 data = packet->rgucButtonsAndHat[1];
  1104. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data & 0x01) != 0));
  1105. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data & 0x02) != 0));
  1106. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data & 0x10) != 0));
  1107. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data & 0x20) != 0));
  1108. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data & 0x40) != 0));
  1109. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data & 0x80) != 0));
  1110. }
  1111. if (ctx->last_state.state.rgucButtonsAndHat[2] != packet->rgucButtonsAndHat[2]) {
  1112. Uint8 data = packet->rgucButtonsAndHat[2];
  1113. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data & 0x01) != 0));
  1114. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_PS5_TOUCHPAD, ((data & 0x02) != 0));
  1115. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_PS5_MICROPHONE, ((data & 0x04) != 0));
  1116. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_PS5_LEFT_FUNCTION, ((data & 0x10) != 0));
  1117. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_PS5_RIGHT_FUNCTION, ((data & 0x20) != 0));
  1118. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_PS5_LEFT_PADDLE, ((data & 0x40) != 0));
  1119. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_PS5_RIGHT_PADDLE, ((data & 0x80) != 0));
  1120. }
  1121. if (packet->ucTriggerLeft == 0 && (packet->rgucButtonsAndHat[1] & 0x04)) {
  1122. axis = SDL_JOYSTICK_AXIS_MAX;
  1123. } else {
  1124. axis = ((int)packet->ucTriggerLeft * 257) - 32768;
  1125. }
  1126. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  1127. if (packet->ucTriggerRight == 0 && (packet->rgucButtonsAndHat[1] & 0x08)) {
  1128. axis = SDL_JOYSTICK_AXIS_MAX;
  1129. } else {
  1130. axis = ((int)packet->ucTriggerRight * 257) - 32768;
  1131. }
  1132. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  1133. axis = ((int)packet->ucLeftJoystickX * 257) - 32768;
  1134. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  1135. axis = ((int)packet->ucLeftJoystickY * 257) - 32768;
  1136. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  1137. axis = ((int)packet->ucRightJoystickX * 257) - 32768;
  1138. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  1139. axis = ((int)packet->ucRightJoystickY * 257) - 32768;
  1140. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  1141. if (ctx->report_sensors) {
  1142. Uint64 sensor_timestamp;
  1143. float data[3];
  1144. if (ctx->use_alternate_report) {
  1145. // 16-bit timestamp
  1146. Uint32 delta;
  1147. Uint16 tick = LOAD16(packet->rgucSensorTimestamp[0],
  1148. packet->rgucSensorTimestamp[1]);
  1149. if (ctx->last_tick < tick) {
  1150. delta = (tick - ctx->last_tick);
  1151. } else {
  1152. delta = (SDL_MAX_UINT16 - ctx->last_tick + tick + 1);
  1153. }
  1154. ctx->last_tick = tick;
  1155. ctx->sensor_ticks += delta;
  1156. // Sensor timestamp is in 1us units
  1157. sensor_timestamp = SDL_US_TO_NS(ctx->sensor_ticks);
  1158. } else {
  1159. // 32-bit timestamp
  1160. Uint32 delta;
  1161. Uint32 tick = LOAD32(packet->rgucSensorTimestamp[0],
  1162. packet->rgucSensorTimestamp[1],
  1163. packet->rgucSensorTimestamp[2],
  1164. packet->rgucSensorTimestamp[3]);
  1165. if (ctx->last_tick < tick) {
  1166. delta = (tick - ctx->last_tick);
  1167. } else {
  1168. delta = (SDL_MAX_UINT32 - ctx->last_tick + tick + 1);
  1169. }
  1170. ctx->last_tick = tick;
  1171. ctx->sensor_ticks += delta;
  1172. // Sensor timestamp is in 0.33us units
  1173. sensor_timestamp = (ctx->sensor_ticks * SDL_NS_PER_US) / 3;
  1174. }
  1175. data[0] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 0, LOAD16(packet->rgucGyroX[0], packet->rgucGyroX[1]));
  1176. data[1] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 1, LOAD16(packet->rgucGyroY[0], packet->rgucGyroY[1]));
  1177. data[2] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 2, LOAD16(packet->rgucGyroZ[0], packet->rgucGyroZ[1]));
  1178. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, data, 3);
  1179. data[0] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 3, LOAD16(packet->rgucAccelX[0], packet->rgucAccelX[1]));
  1180. data[1] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 4, LOAD16(packet->rgucAccelY[0], packet->rgucAccelY[1]));
  1181. data[2] = HIDAPI_DriverPS5_ApplyCalibrationData(ctx, 5, LOAD16(packet->rgucAccelZ[0], packet->rgucAccelZ[1]));
  1182. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, data, 3);
  1183. }
  1184. }
  1185. static void HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacket_t *packet, Uint64 timestamp)
  1186. {
  1187. static const float TOUCHPAD_SCALEX = 5.20833333e-4f; // 1.0f / 1920
  1188. static const float TOUCHPAD_SCALEY = 9.34579439e-4f; // 1.0f / 1070
  1189. bool touchpad_down;
  1190. int touchpad_x, touchpad_y;
  1191. if (ctx->report_touchpad) {
  1192. touchpad_down = ((packet->ucTouchpadCounter1 & 0x80) == 0);
  1193. touchpad_x = packet->rgucTouchpadData1[0] | (((int)packet->rgucTouchpadData1[1] & 0x0F) << 8);
  1194. touchpad_y = (packet->rgucTouchpadData1[1] >> 4) | ((int)packet->rgucTouchpadData1[2] << 4);
  1195. SDL_SendJoystickTouchpad(timestamp, joystick, 0, 0, touchpad_down, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_down ? 1.0f : 0.0f);
  1196. touchpad_down = ((packet->ucTouchpadCounter2 & 0x80) == 0);
  1197. touchpad_x = packet->rgucTouchpadData2[0] | (((int)packet->rgucTouchpadData2[1] & 0x0F) << 8);
  1198. touchpad_y = (packet->rgucTouchpadData2[1] >> 4) | ((int)packet->rgucTouchpadData2[2] << 4);
  1199. SDL_SendJoystickTouchpad(timestamp, joystick, 0, 1, touchpad_down, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_down ? 1.0f : 0.0f);
  1200. }
  1201. if (ctx->report_battery) {
  1202. SDL_PowerState state;
  1203. int percent;
  1204. Uint8 status = (packet->ucBatteryLevel >> 4) & 0x0F;
  1205. Uint8 level = (packet->ucBatteryLevel & 0x0F);
  1206. switch (status) {
  1207. case 0:
  1208. state = SDL_POWERSTATE_ON_BATTERY;
  1209. percent = SDL_min(level * 10 + 5, 100);
  1210. break;
  1211. case 1:
  1212. state = SDL_POWERSTATE_CHARGING;
  1213. percent = SDL_min(level * 10 + 5, 100);
  1214. break;
  1215. case 2:
  1216. state = SDL_POWERSTATE_CHARGED;
  1217. percent = 100;
  1218. break;
  1219. default:
  1220. state = SDL_POWERSTATE_UNKNOWN;
  1221. percent = 0;
  1222. break;
  1223. }
  1224. SDL_SendJoystickPowerInfo(joystick, state, percent);
  1225. }
  1226. HIDAPI_DriverPS5_HandleStatePacketCommon(joystick, dev, ctx, (PS5StatePacketCommon_t *)packet, timestamp);
  1227. SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
  1228. }
  1229. static void HIDAPI_DriverPS5_HandleStatePacketAlt(SDL_Joystick *joystick, SDL_hid_device *dev, SDL_DriverPS5_Context *ctx, PS5StatePacketAlt_t *packet, Uint64 timestamp)
  1230. {
  1231. static const float TOUCHPAD_SCALEX = 5.20833333e-4f; // 1.0f / 1920
  1232. static const float TOUCHPAD_SCALEY = 9.34579439e-4f; // 1.0f / 1070
  1233. bool touchpad_down;
  1234. int touchpad_x, touchpad_y;
  1235. if (ctx->report_touchpad) {
  1236. touchpad_down = ((packet->ucTouchpadCounter1 & 0x80) == 0);
  1237. touchpad_x = packet->rgucTouchpadData1[0] | (((int)packet->rgucTouchpadData1[1] & 0x0F) << 8);
  1238. touchpad_y = (packet->rgucTouchpadData1[1] >> 4) | ((int)packet->rgucTouchpadData1[2] << 4);
  1239. SDL_SendJoystickTouchpad(timestamp, joystick, 0, 0, touchpad_down, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_down ? 1.0f : 0.0f);
  1240. touchpad_down = ((packet->ucTouchpadCounter2 & 0x80) == 0);
  1241. touchpad_x = packet->rgucTouchpadData2[0] | (((int)packet->rgucTouchpadData2[1] & 0x0F) << 8);
  1242. touchpad_y = (packet->rgucTouchpadData2[1] >> 4) | ((int)packet->rgucTouchpadData2[2] << 4);
  1243. SDL_SendJoystickTouchpad(timestamp, joystick, 0, 1, touchpad_down, touchpad_x * TOUCHPAD_SCALEX, touchpad_y * TOUCHPAD_SCALEY, touchpad_down ? 1.0f : 0.0f);
  1244. }
  1245. HIDAPI_DriverPS5_HandleStatePacketCommon(joystick, dev, ctx, (PS5StatePacketCommon_t *)packet, timestamp);
  1246. SDL_memcpy(&ctx->last_state, packet, sizeof(ctx->last_state));
  1247. }
  1248. static bool VerifyCRC(Uint8 *data, int size)
  1249. {
  1250. Uint8 ubHdr = 0xA1; // hidp header is part of the CRC calculation
  1251. Uint32 unCRC, unPacketCRC;
  1252. Uint8 *packetCRC = data + size - sizeof(unPacketCRC);
  1253. unCRC = SDL_crc32(0, &ubHdr, 1);
  1254. unCRC = SDL_crc32(unCRC, data, (size_t)(size - sizeof(unCRC)));
  1255. unPacketCRC = LOAD32(packetCRC[0],
  1256. packetCRC[1],
  1257. packetCRC[2],
  1258. packetCRC[3]);
  1259. return (unCRC == unPacketCRC);
  1260. }
  1261. static bool HIDAPI_DriverPS5_IsPacketValid(SDL_DriverPS5_Context *ctx, Uint8 *data, int size)
  1262. {
  1263. switch (data[0]) {
  1264. case k_EPS5ReportIdState:
  1265. if (ctx->is_dongle && size >= (1 + sizeof(PS5StatePacketAlt_t))) {
  1266. // The report timestamp doesn't change when the controller isn't connected
  1267. PS5StatePacketAlt_t *packet = (PS5StatePacketAlt_t *)&data[1];
  1268. if (SDL_memcmp(packet->rgucPacketSequence, ctx->last_state.state.rgucPacketSequence, sizeof(packet->rgucPacketSequence)) == 0) {
  1269. return false;
  1270. }
  1271. if (ctx->last_state.state.rgucPacketSequence[0] == 0 &&
  1272. ctx->last_state.state.rgucPacketSequence[1] == 0 &&
  1273. ctx->last_state.state.rgucPacketSequence[2] == 0 &&
  1274. ctx->last_state.state.rgucPacketSequence[3] == 0) {
  1275. // We don't have any state to compare yet, go ahead and copy it
  1276. SDL_memcpy(&ctx->last_state, &data[1], sizeof(PS5StatePacketAlt_t));
  1277. return false;
  1278. }
  1279. }
  1280. return true;
  1281. case k_EPS5ReportIdBluetoothState:
  1282. if (VerifyCRC(data, size)) {
  1283. return true;
  1284. }
  1285. break;
  1286. default:
  1287. break;
  1288. }
  1289. return false;
  1290. }
  1291. static bool HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
  1292. {
  1293. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  1294. SDL_Joystick *joystick = NULL;
  1295. Uint8 data[USB_PACKET_LENGTH * 2];
  1296. int size;
  1297. int packet_count = 0;
  1298. Uint64 now = SDL_GetTicks();
  1299. if (device->num_joysticks > 0) {
  1300. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  1301. }
  1302. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  1303. Uint64 timestamp = SDL_GetTicksNS();
  1304. #ifdef DEBUG_PS5_PROTOCOL
  1305. HIDAPI_DumpPacket("PS5 packet: size = %d", data, size);
  1306. #endif
  1307. if (!HIDAPI_DriverPS5_IsPacketValid(ctx, data, size)) {
  1308. continue;
  1309. }
  1310. ++packet_count;
  1311. ctx->last_packet = now;
  1312. if (!joystick) {
  1313. continue;
  1314. }
  1315. switch (data[0]) {
  1316. case k_EPS5ReportIdState:
  1317. if (size == 10 || size == 78) {
  1318. HIDAPI_DriverPS5_HandleSimpleStatePacket(joystick, device->dev, ctx, (PS5SimpleStatePacket_t *)&data[1], timestamp);
  1319. } else {
  1320. if (ctx->use_alternate_report) {
  1321. HIDAPI_DriverPS5_HandleStatePacketAlt(joystick, device->dev, ctx, (PS5StatePacketAlt_t *)&data[1], timestamp);
  1322. } else {
  1323. HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[1], timestamp);
  1324. }
  1325. }
  1326. break;
  1327. case k_EPS5ReportIdBluetoothState:
  1328. // This is the extended report, we can enable effects now in auto mode
  1329. HIDAPI_DriverPS5_UpdateEnhancedModeOnEnhancedReport(ctx);
  1330. if (ctx->use_alternate_report) {
  1331. HIDAPI_DriverPS5_HandleStatePacketAlt(joystick, device->dev, ctx, (PS5StatePacketAlt_t *)&data[2], timestamp);
  1332. } else {
  1333. HIDAPI_DriverPS5_HandleStatePacket(joystick, device->dev, ctx, (PS5StatePacket_t *)&data[2], timestamp);
  1334. }
  1335. if (ctx->led_reset_state == k_EDS5LEDResetStatePending) {
  1336. HIDAPI_DriverPS5_CheckPendingLEDReset(ctx);
  1337. }
  1338. break;
  1339. default:
  1340. #ifdef DEBUG_JOYSTICK
  1341. SDL_Log("Unknown PS5 packet: 0x%.2x", data[0]);
  1342. #endif
  1343. break;
  1344. }
  1345. }
  1346. if (device->is_bluetooth) {
  1347. if (packet_count == 0) {
  1348. // Check to see if it looks like the device disconnected
  1349. if (now >= (ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
  1350. // Send an empty output report to tickle the Bluetooth stack
  1351. HIDAPI_DriverPS5_TickleBluetooth(device);
  1352. ctx->last_packet = now;
  1353. }
  1354. } else {
  1355. // Reconnect the Bluetooth device once the USB device is gone
  1356. if (device->num_joysticks == 0 &&
  1357. !HIDAPI_HasConnectedUSBDevice(device->serial)) {
  1358. HIDAPI_JoystickConnected(device, NULL);
  1359. }
  1360. }
  1361. }
  1362. if (ctx->is_dongle) {
  1363. if (packet_count == 0) {
  1364. if (device->num_joysticks > 0) {
  1365. // Check to see if it looks like the device disconnected
  1366. if (now >= (ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
  1367. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  1368. }
  1369. }
  1370. } else {
  1371. if (device->num_joysticks == 0) {
  1372. HIDAPI_JoystickConnected(device, NULL);
  1373. }
  1374. }
  1375. }
  1376. if (packet_count == 0 && size < 0 && device->num_joysticks > 0) {
  1377. // Read error, device is disconnected
  1378. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  1379. }
  1380. return (size >= 0);
  1381. }
  1382. static void HIDAPI_DriverPS5_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  1383. {
  1384. SDL_DriverPS5_Context *ctx = (SDL_DriverPS5_Context *)device->context;
  1385. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_ENHANCED_REPORTS,
  1386. SDL_PS5EnhancedReportsChanged, ctx);
  1387. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
  1388. SDL_PS5PlayerLEDHintChanged, ctx);
  1389. ctx->joystick = NULL;
  1390. ctx->report_sensors = false;
  1391. ctx->enhanced_mode = false;
  1392. ctx->enhanced_mode_available = false;
  1393. }
  1394. static void HIDAPI_DriverPS5_FreeDevice(SDL_HIDAPI_Device *device)
  1395. {
  1396. }
  1397. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverPS5 = {
  1398. SDL_HINT_JOYSTICK_HIDAPI_PS5,
  1399. true,
  1400. HIDAPI_DriverPS5_RegisterHints,
  1401. HIDAPI_DriverPS5_UnregisterHints,
  1402. HIDAPI_DriverPS5_IsEnabled,
  1403. HIDAPI_DriverPS5_IsSupportedDevice,
  1404. HIDAPI_DriverPS5_InitDevice,
  1405. HIDAPI_DriverPS5_GetDevicePlayerIndex,
  1406. HIDAPI_DriverPS5_SetDevicePlayerIndex,
  1407. HIDAPI_DriverPS5_UpdateDevice,
  1408. HIDAPI_DriverPS5_OpenJoystick,
  1409. HIDAPI_DriverPS5_RumbleJoystick,
  1410. HIDAPI_DriverPS5_RumbleJoystickTriggers,
  1411. HIDAPI_DriverPS5_GetJoystickCapabilities,
  1412. HIDAPI_DriverPS5_SetJoystickLED,
  1413. HIDAPI_DriverPS5_SendJoystickEffect,
  1414. HIDAPI_DriverPS5_SetJoystickSensorsEnabled,
  1415. HIDAPI_DriverPS5_CloseJoystick,
  1416. HIDAPI_DriverPS5_FreeDevice,
  1417. };
  1418. #endif // SDL_JOYSTICK_HIDAPI_PS5
  1419. #endif // SDL_JOYSTICK_HIDAPI