mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-04 17:06:25 +00:00
Prefer USB input over Bluetooth for PS5/PS5/Switch controllers
Nintendo Switch controllers will automatically turn off Bluetooth when connected over USB, but this takes care of that a little more quickly. PS4 and PS5 controllers will happily send reports over both Bluetooth and USB, so we'll prefer USB if connected and switch back to Bluetooth if USB is disconnected.
This commit is contained in:
@@ -198,7 +198,6 @@ typedef struct {
|
||||
typedef struct {
|
||||
SDL_HIDAPI_Device *device;
|
||||
SDL_Joystick *joystick;
|
||||
SDL_bool is_bluetooth;
|
||||
SDL_bool use_alternate_report;
|
||||
SDL_bool sensors_supported;
|
||||
SDL_bool lightbar_supported;
|
||||
@@ -377,15 +376,15 @@ HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
|
||||
#endif
|
||||
if (size == 64) {
|
||||
/* Connected over USB */
|
||||
ctx->is_bluetooth = SDL_FALSE;
|
||||
device->is_bluetooth = SDL_FALSE;
|
||||
ctx->enhanced_mode = SDL_TRUE;
|
||||
} else if (size > 0 && data[0] == k_EPS5ReportIdBluetoothEffects) {
|
||||
/* Connected over Bluetooth, using enhanced reports */
|
||||
ctx->is_bluetooth = SDL_TRUE;
|
||||
device->is_bluetooth = SDL_TRUE;
|
||||
ctx->enhanced_mode = SDL_TRUE;
|
||||
} else {
|
||||
/* Connected over Bluetooth, using simple reports (DirectInput enabled) */
|
||||
ctx->is_bluetooth = SDL_TRUE;
|
||||
device->is_bluetooth = SDL_TRUE;
|
||||
|
||||
/* Games written prior the introduction of PS5 controller support in SDL will not be aware of
|
||||
SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, but they did know SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE.
|
||||
@@ -479,6 +478,14 @@ HIDAPI_DriverPS5_InitDevice(SDL_HIDAPI_Device *device)
|
||||
}
|
||||
HIDAPI_SetDeviceSerial(device, serial);
|
||||
|
||||
/* Prefer the USB device over the Bluetooth device */
|
||||
if (device->is_bluetooth) {
|
||||
if (HIDAPI_HasConnectedUSBDevice(device->serial)) {
|
||||
return SDL_TRUE;
|
||||
}
|
||||
} else {
|
||||
HIDAPI_DisconnectBluetoothDevice(device->serial);
|
||||
}
|
||||
return HIDAPI_JoystickConnected(device, NULL);
|
||||
}
|
||||
|
||||
@@ -618,7 +625,7 @@ HIDAPI_DriverPS5_UpdateEffects(SDL_HIDAPI_Device *device, int effect_mask)
|
||||
SDL_zero(effects);
|
||||
|
||||
/* Make sure the Bluetooth connection sequence has completed before sending LED color change */
|
||||
if (ctx->is_bluetooth &&
|
||||
if (device->is_bluetooth &&
|
||||
(effect_mask & (k_EDS5EffectLED | k_EDS5EffectPadLights)) != 0) {
|
||||
if (ctx->led_reset_state != k_EDS5LEDResetStateComplete) {
|
||||
ctx->led_reset_state = k_EDS5LEDResetStatePending;
|
||||
@@ -819,7 +826,7 @@ HIDAPI_DriverPS5_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
|
||||
/* Initialize the joystick capabilities */
|
||||
joystick->nbuttons = ctx->touchpad_supported ? 17 : 15;
|
||||
joystick->naxes = SDL_CONTROLLER_AXIS_MAX;
|
||||
joystick->epowerlevel = ctx->is_bluetooth ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
|
||||
joystick->epowerlevel = device->is_bluetooth ? SDL_JOYSTICK_POWER_UNKNOWN : SDL_JOYSTICK_POWER_WIRED;
|
||||
joystick->firmware_version = ctx->firmware_version;
|
||||
|
||||
if (ctx->enhanced_mode) {
|
||||
@@ -916,7 +923,7 @@ HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joy
|
||||
|
||||
SDL_zeroa(data);
|
||||
|
||||
if (ctx->is_bluetooth) {
|
||||
if (device->is_bluetooth) {
|
||||
data[0] = k_EPS5ReportIdBluetoothEffects;
|
||||
data[1] = 0x02; /* Magic value */
|
||||
|
||||
@@ -931,7 +938,7 @@ HIDAPI_DriverPS5_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joy
|
||||
|
||||
SDL_memcpy(&data[offset], effect, SDL_min((sizeof(data) - offset), (size_t)size));
|
||||
|
||||
if (ctx->is_bluetooth) {
|
||||
if (device->is_bluetooth) {
|
||||
/* Bluetooth reports need a CRC at the end of the packet (at least on Linux) */
|
||||
Uint8 ubHdr = 0xA2; /* hidp header is part of the CRC calculation */
|
||||
Uint32 unCRC;
|
||||
@@ -1206,7 +1213,7 @@ HIDAPI_DriverPS5_HandleStatePacket(SDL_Joystick *joystick, SDL_hid_device *dev,
|
||||
/* A check of packet->ucBatteryLevel & 0x10 should work as a check for BT vs USB but doesn't
|
||||
* seem to always work. Possibly related to being 100% charged?
|
||||
*/
|
||||
if (!ctx->is_bluetooth) {
|
||||
if (!ctx->device->is_bluetooth) {
|
||||
/* 0x20 set means fully charged */
|
||||
SDL_PrivateJoystickBatteryLevel(joystick, SDL_JOYSTICK_POWER_WIRED);
|
||||
} else {
|
||||
@@ -1258,6 +1265,13 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
int size;
|
||||
int packet_count = 0;
|
||||
|
||||
/* Reconnect the Bluetooth device once the USB device is gone */
|
||||
if (device->is_bluetooth &&
|
||||
device->num_joysticks == 0 &&
|
||||
!HIDAPI_HasConnectedUSBDevice(device->serial)) {
|
||||
HIDAPI_JoystickConnected(device, NULL);
|
||||
}
|
||||
|
||||
if (device->num_joysticks > 0) {
|
||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||
} else {
|
||||
@@ -1311,7 +1325,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||
}
|
||||
}
|
||||
|
||||
if (ctx->is_bluetooth && packet_count == 0) {
|
||||
if (device->is_bluetooth && packet_count == 0) {
|
||||
/* Check to see if it looks like the device disconnected */
|
||||
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
|
||||
/* Send an empty output report to tickle the Bluetooth stack */
|
||||
|
Reference in New Issue
Block a user