From 26205b659dbd88065e0da2ce550b7549e97a98a4 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 7 Aug 2023 10:48:52 -0700 Subject: [PATCH] Fixed PS4/PS5 touchpad for third party controllers We should always report touchpad and battery status if they are available. We just want to make sure we don't enable enhanced reports unless the application wants that behavior. --- src/joystick/hidapi/SDL_hidapi_ps4.c | 42 +++++++++++++++------------- src/joystick/hidapi/SDL_hidapi_ps5.c | 35 ++++++++++++----------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c index aa64d1c0c4..47ea29042b 100644 --- a/src/joystick/hidapi/SDL_hidapi_ps4.c +++ b/src/joystick/hidapi/SDL_hidapi_ps4.c @@ -694,20 +694,21 @@ static void HIDAPI_DriverPS4_TickleBluetooth(SDL_HIDAPI_Device *device) static void HIDAPI_DriverPS4_SetEnhancedModeAvailable(SDL_DriverPS4_Context *ctx) { - if (!ctx->effects_supported) { - /* We shouldn't be sending any packets to the controller */ - return; - } - ctx->enhanced_mode_available = SDL_TRUE; if (ctx->touchpad_supported) { SDL_PrivateJoystickAddTouchpad(ctx->joystick, 2); + ctx->report_touchpad = SDL_TRUE; } + if (ctx->sensors_supported) { SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_GYRO, 250.0f); SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL, 250.0f); } + + if (ctx->device->is_bluetooth && ctx->official_controller) { + ctx->report_battery = SDL_TRUE; + } } static void HIDAPI_DriverPS4_SetEnhancedMode(SDL_DriverPS4_Context *ctx) @@ -716,17 +717,10 @@ static void HIDAPI_DriverPS4_SetEnhancedMode(SDL_DriverPS4_Context *ctx) HIDAPI_DriverPS4_SetEnhancedModeAvailable(ctx); } - if (!ctx->enhanced_mode && ctx->enhanced_mode_available) { + if (!ctx->enhanced_mode) { ctx->enhanced_mode = SDL_TRUE; - if (ctx->touchpad_supported) { - ctx->report_touchpad = SDL_TRUE; - } - - if (ctx->device->is_bluetooth && ctx->official_controller) { - ctx->report_battery = SDL_TRUE; - } - + /* Switch into enhanced report mode */ HIDAPI_DriverPS4_UpdateEffects(ctx, SDL_FALSE); } } @@ -888,12 +882,20 @@ static int HIDAPI_DriverPS4_InternalSendJoystickEffect(SDL_DriverPS4_Context *ct Uint8 data[78]; int report_size, offset; - if (application_usage) { - HIDAPI_DriverPS4_UpdateEnhancedModeOnApplicationUsage(ctx); + if (!ctx->effects_supported) { + /* We shouldn't be sending packets to this controller */ + return SDL_Unsupported(); } - if (!ctx->enhanced_mode_available) { - return SDL_Unsupported(); + if (!ctx->enhanced_mode) { + if (application_usage) { + HIDAPI_DriverPS4_UpdateEnhancedModeOnApplicationUsage(ctx); + } + + if (!ctx->enhanced_mode) { + /* We're not in enhanced mode, effects aren't allowed */ + return SDL_Unsupported(); + } } SDL_zeroa(data); @@ -1059,7 +1061,7 @@ static void HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_d axis = ((int)packet->ucRightJoystickY * 257) - 32768; SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis); - if (size > 9 && ctx->report_battery) { + if (size > 9 && ctx->report_battery && ctx->enhanced_reports) { /* Battery level ranges from 0 to 10 */ int level = (packet->ucBatteryLevel & 0xF); if (level == 0) { @@ -1073,7 +1075,7 @@ static void HIDAPI_DriverPS4_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_d } } - if (size > 9 && ctx->report_touchpad) { + if (size > 9 && ctx->report_touchpad && ctx->enhanced_reports) { touchpad_state = !(packet->ucTouchpadCounter1 & 0x80) ? SDL_PRESSED : SDL_RELEASED; touchpad_x = packet->rgucTouchpadData1[0] | (((int)packet->rgucTouchpadData1[1] & 0x0F) << 8); touchpad_y = (packet->rgucTouchpadData1[1] >> 4) | ((int)packet->rgucTouchpadData1[2] << 4); diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c index 650d5f5205..8c46f4200b 100644 --- a/src/joystick/hidapi/SDL_hidapi_ps5.c +++ b/src/joystick/hidapi/SDL_hidapi_ps5.c @@ -774,16 +774,13 @@ static void HIDAPI_DriverPS5_TickleBluetooth(SDL_HIDAPI_Device *device) static void HIDAPI_DriverPS5_SetEnhancedModeAvailable(SDL_DriverPS5_Context *ctx) { - if (!ctx->effects_supported) { - /* We shouldn't be sending any packets to the controller */ - return; - } - ctx->enhanced_mode_available = SDL_TRUE; if (ctx->touchpad_supported) { SDL_PrivateJoystickAddTouchpad(ctx->joystick, 2); + ctx->report_touchpad = SDL_TRUE; } + if (ctx->sensors_supported) { if (ctx->device->is_bluetooth) { /* Bluetooth sensor update rate appears to be 1000 Hz */ @@ -794,6 +791,10 @@ static void HIDAPI_DriverPS5_SetEnhancedModeAvailable(SDL_DriverPS5_Context *ctx SDL_PrivateJoystickAddSensor(ctx->joystick, SDL_SENSOR_ACCEL, 250.0f); } } + + if (ctx->device->is_bluetooth) { + ctx->report_battery = SDL_TRUE; + } } static void HIDAPI_DriverPS5_SetEnhancedMode(SDL_DriverPS5_Context *ctx) @@ -805,14 +806,6 @@ static void HIDAPI_DriverPS5_SetEnhancedMode(SDL_DriverPS5_Context *ctx) if (!ctx->enhanced_mode && ctx->enhanced_mode_available) { ctx->enhanced_mode = SDL_TRUE; - if (ctx->touchpad_supported) { - ctx->report_touchpad = SDL_TRUE; - } - - if (ctx->device->is_bluetooth) { - ctx->report_battery = SDL_TRUE; - } - /* Switch into enhanced report mode */ HIDAPI_DriverPS5_UpdateEffects(ctx, 0, SDL_FALSE); @@ -1017,12 +1010,20 @@ static int HIDAPI_DriverPS5_InternalSendJoystickEffect(SDL_DriverPS5_Context *ct int *pending_size; int maximum_size; - if (application_usage) { - HIDAPI_DriverPS5_UpdateEnhancedModeOnApplicationUsage(ctx); + if (!ctx->effects_supported) { + /* We shouldn't be sending packets to this controller */ + return SDL_Unsupported(); } - if (!ctx->enhanced_mode_available) { - return SDL_Unsupported(); + if (!ctx->enhanced_mode) { + if (application_usage) { + HIDAPI_DriverPS5_UpdateEnhancedModeOnApplicationUsage(ctx); + } + + if (!ctx->enhanced_mode) { + /* We're not in enhanced mode, effects aren't allowed */ + return SDL_Unsupported(); + } } SDL_zeroa(data);