SDL_hidapi_ps5.c 59 KB

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