Improved PS4 and PS5 third-party controller feature detection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c
index 0c0493a..63da278 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps4.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps4.c
@@ -69,6 +69,7 @@ typedef enum
typedef enum
{
k_ePS4FeatureReportIdGyroCalibration_USB = 0x02,
+ k_ePS4FeatureReportIdCapabilities = 0x03,
k_ePS4FeatureReportIdGyroCalibration_BT = 0x05,
k_ePS4FeatureReportIdSerialNumber = 0x12,
} EPS4FeatureReportID;
@@ -125,7 +126,6 @@ typedef struct {
SDL_bool is_dongle;
SDL_bool is_bluetooth;
SDL_bool official_controller;
- SDL_bool audio_supported;
SDL_bool effects_supported;
SDL_bool sensors_supported;
SDL_bool touchpad_supported;
@@ -192,42 +192,6 @@ static int ReadFeatureReport(SDL_hid_device *dev, Uint8 report_id, Uint8 *report
return SDL_hid_get_feature_report(dev, report, length);
}
-static SDL_bool HIDAPI_DriverPS4_CanRumble(Uint16 vendor_id, Uint16 product_id)
-{
- /* The Razer Panthera fight stick hangs when trying to rumble */
- if (vendor_id == USB_VENDOR_RAZER &&
- (product_id == USB_PRODUCT_RAZER_PANTHERA || product_id == USB_PRODUCT_RAZER_PANTHERA_EVO)) {
- return SDL_FALSE;
- }
-
- /* The Victrix Pro FS v2 will hang on reboot if we send output reports */
- if (vendor_id == USB_VENDOR_PDP && product_id == USB_PRODUCT_VICTRIX_FS_PRO_V2) {
- return SDL_FALSE;
- }
-
- /* The Hori controllers don't have any rumble hardware */
- if (vendor_id == USB_VENDOR_HORI) {
- return SDL_FALSE;
- }
-
- return SDL_TRUE;
-}
-
-static SDL_bool HIDAPI_DriverPS4_HasSensors(Uint16 vendor_id, Uint16 product_id)
-{
- /* The Hori controllers don't have any gyro or accelerometer */
- if (vendor_id == USB_VENDOR_HORI) {
- return SDL_FALSE;
- }
-
- return SDL_TRUE;
-}
-
-static SDL_bool HIDAPI_DriverPS4_HasTouchpad(Uint16 vendor_id, Uint16 product_id)
-{
- return SDL_TRUE;
-}
-
static void
SetLedsForPlayerIndex(DS4EffectsState_t *effects, int player_index)
{
@@ -521,6 +485,8 @@ static SDL_bool
HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_DriverPS4_Context *ctx;
+ Uint8 data[USB_PACKET_LENGTH];
+ int size;
SDL_bool enhanced_mode = SDL_FALSE;
ctx = (SDL_DriverPS4_Context *)SDL_calloc(1, sizeof(*ctx));
@@ -547,9 +513,6 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
ctx->official_controller = SDL_TRUE;
enhanced_mode = SDL_TRUE;
} else if (device->vendor_id == USB_VENDOR_SONY) {
- Uint8 data[USB_PACKET_LENGTH];
- int size;
-
/* This will fail if we're on Bluetooth */
size = ReadFeatureReport(device->dev, k_ePS4FeatureReportIdSerialNumber, data, sizeof(data));
if (size >= 7) {
@@ -588,22 +551,27 @@ HIDAPI_DriverPS4_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
SDL_Log("PS4 dongle = %s, bluetooth = %s\n", ctx->is_dongle ? "TRUE" : "FALSE", ctx->is_bluetooth ? "TRUE" : "FALSE");
#endif
- /* Check to see if audio is supported */
- if (device->vendor_id == USB_VENDOR_SONY &&
- (device->product_id == USB_PRODUCT_SONY_DS4_SLIM || device->product_id == USB_PRODUCT_SONY_DS4_DONGLE)) {
- ctx->audio_supported = SDL_TRUE;
- }
-
- if (HIDAPI_DriverPS4_CanRumble(device->vendor_id, device->product_id)) {
+ /* Get the device capabilities */
+ if (device->vendor_id == USB_VENDOR_SONY) {
ctx->effects_supported = SDL_TRUE;
- }
-
- if (HIDAPI_DriverPS4_HasSensors(device->vendor_id, device->product_id)) {
ctx->sensors_supported = SDL_TRUE;
- }
-
- if (HIDAPI_DriverPS4_HasTouchpad(device->vendor_id, device->product_id)) {
ctx->touchpad_supported = SDL_TRUE;
+ } else if (ReadFeatureReport(device->dev, k_ePS4FeatureReportIdCapabilities, data, sizeof(data)) == 48 &&
+ data[2] == 0x27) {
+ Uint8 capabilities = data[4];
+
+#ifdef DEBUG_PS4_PROTOCOL
+ HIDAPI_DumpPacket("PS4 capabilities: size = %d", data, size);
+#endif
+ if ((capabilities & 0x0C) != 0) {
+ ctx->effects_supported = SDL_TRUE;
+ }
+ if ((capabilities & 0x02) != 0) {
+ ctx->sensors_supported = SDL_TRUE;
+ }
+ if ((capabilities & 0x40) != 0) {
+ ctx->touchpad_supported = SDL_TRUE;
+ }
}
if (!joystick->serial && device->serial && SDL_strlen(device->serial) == 12) {
diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c
index 08f27fb..f3ac145 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps5.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps5.c
@@ -56,6 +56,7 @@ typedef enum
typedef enum
{
+ k_EPS5FeatureReportIdCapabilities = 0x03,
k_EPS5FeatureReportIdCalibration = 0x05,
k_EPS5FeatureReportIdSerialNumber = 0x09,
k_EPS5FeatureReportIdFirmwareInfo = 0x20,
@@ -227,41 +228,6 @@ typedef struct {
} last_state;
} SDL_DriverPS5_Context;
-static SDL_bool HIDAPI_DriverPS5_CanRumble(Uint16 vendor_id, Uint16 product_id)
-{
- /* The Hori controllers don't have any rumble hardware */
- if (vendor_id == USB_VENDOR_HORI) {
- return SDL_FALSE;
- }
-
- return SDL_TRUE;
-}
-
-static SDL_bool HIDAPI_DriverPS5_HasSensors(Uint16 vendor_id, Uint16 product_id)
-{
- /* The Hori controllers don't have any gyro or accelerometer */
- if (vendor_id == USB_VENDOR_HORI) {
- return SDL_FALSE;
- }
-
- return SDL_TRUE;
-}
-
-static SDL_bool HIDAPI_DriverPS5_HasTouchpad(Uint16 vendor_id, Uint16 product_id)
-{
- return SDL_TRUE;
-}
-
-static SDL_bool HIDAPI_DriverPS5_UseAlternateReport(Uint16 vendor_id, Uint16 product_id)
-{
- /* The Hori Fighting Stick Alpha and Fighting Commander OCTA report touchpad at a different offset than the PS5 controller */
- if (vendor_id == USB_VENDOR_HORI) {
- return SDL_TRUE;
- }
-
- return SDL_FALSE;
-}
-
static int HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *effect, int size);
static void
@@ -742,20 +708,29 @@ HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
}
}
- if (HIDAPI_DriverPS5_UseAlternateReport(device->vendor_id, device->product_id)) {
- ctx->use_alternate_report = SDL_TRUE;
- }
-
- if (HIDAPI_DriverPS5_CanRumble(device->vendor_id, device->product_id)) {
+ /* Get the device capabilities */
+ if (device->vendor_id == USB_VENDOR_SONY) {
ctx->effects_supported = SDL_TRUE;
- }
-
- if (HIDAPI_DriverPS5_HasSensors(device->vendor_id, device->product_id)) {
ctx->sensors_supported = SDL_TRUE;
- }
-
- if (HIDAPI_DriverPS5_HasTouchpad(device->vendor_id, device->product_id)) {
ctx->touchpad_supported = SDL_TRUE;
+ } else if (ReadFeatureReport(device->dev, k_EPS5FeatureReportIdCapabilities, data, sizeof(data)) == 48 &&
+ data[2] == 0x28) {
+ Uint8 capabilities = data[4];
+
+#ifdef DEBUG_PS5_PROTOCOL
+ HIDAPI_DumpPacket("PS5 capabilities: size = %d", data, size);
+#endif
+ if ((capabilities & 0x0C) != 0) {
+ ctx->effects_supported = SDL_TRUE;
+ }
+ if ((capabilities & 0x02) != 0) {
+ ctx->sensors_supported = SDL_TRUE;
+ }
+ if ((capabilities & 0x40) != 0) {
+ ctx->touchpad_supported = SDL_TRUE;
+ }
+
+ ctx->use_alternate_report = SDL_TRUE;
}
if (!joystick->serial && device->serial && SDL_strlen(device->serial) == 12) {