SDL_hidapi_ps5.c 39 KB

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