Updated logic to match between PS4/PS5/Switch controllers

This commit is contained in:
Sam Lantinga 2022-09-26 23:00:58 -07:00
parent 8c40a6b0c7
commit 1e1be0b954
3 changed files with 57 additions and 55 deletions

View File

@ -1016,6 +1016,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
Uint8 data[USB_PACKET_LENGTH*2];
int size;
int packet_count = 0;
Uint32 now = SDL_GetTicks();
if (device->num_joysticks > 0) {
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
@ -1030,7 +1031,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
}
++packet_count;
ctx->last_packet = SDL_GetTicks();
ctx->last_packet = now;
if (!joystick) {
continue;
@ -1067,7 +1068,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
if (device->is_bluetooth) {
if (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)) {
if (SDL_TICKS_PASSED(now, ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
/* Send an empty output report to tickle the Bluetooth stack */
HIDAPI_DriverPS4_TickleBluetooth(device);
}
@ -1084,7 +1085,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
if (packet_count == 0) {
if (device->num_joysticks > 0) {
/* Check to see if it looks like the device disconnected */
if (SDL_TICKS_PASSED(SDL_GetTicks(), ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
if (SDL_TICKS_PASSED(now, ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
}
}

View File

@ -1302,6 +1302,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
Uint8 data[USB_PACKET_LENGTH*2];
int size;
int packet_count = 0;
Uint32 now = SDL_GetTicks();
if (device->num_joysticks > 0) {
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
@ -1316,7 +1317,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
}
++packet_count;
ctx->last_packet = SDL_GetTicks();
ctx->last_packet = now;
if (!joystick) {
continue;
@ -1361,7 +1362,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
if (device->is_bluetooth) {
if (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)) {
if (SDL_TICKS_PASSED(now, ctx->last_packet + BLUETOOTH_DISCONNECT_TIMEOUT_MS)) {
/* Send an empty output report to tickle the Bluetooth stack */
HIDAPI_DriverPS5_TickleBluetooth(device);
}

View File

@ -2083,30 +2083,24 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
SDL_Joystick *joystick = NULL;
int size;
Uint32 now;
/* Reconnect the Bluetooth device once the USB device is gone */
if (device->num_joysticks == 0 &&
device->is_bluetooth &&
!HIDAPI_HasConnectedUSBDevice(device->serial)) {
if (ReadInput(ctx) > 0) {
HIDAPI_JoystickConnected(device, NULL);
}
}
int packet_count = 0;
Uint32 now = SDL_GetTicks();
if (device->num_joysticks > 0) {
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
} else {
return SDL_FALSE;
}
now = SDL_GetTicks();
while ((size = ReadInput(ctx)) > 0) {
#ifdef DEBUG_SWITCH_PROTOCOL
HIDAPI_DumpPacket("Nintendo Switch packet: size = %d", ctx->m_rgucReadBuffer, size);
#endif
if (joystick) {
++packet_count;
ctx->m_unLastInput = now;
if (!joystick) {
continue;
}
if (ctx->m_bInputOnly) {
HandleInputOnlyControllerState(joystick, ctx, (SwitchInputOnlyControllerStatePacket_t *)&ctx->m_rgucReadBuffer[0]);
} else {
@ -2122,10 +2116,9 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
}
}
}
ctx->m_unLastInput = now;
}
if (joystick) {
if (packet_count == 0) {
if (!ctx->m_bInputOnly && !device->is_bluetooth &&
ctx->device->product_id != USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
const Uint32 INPUT_WAIT_TIMEOUT_MS = 100;
@ -2151,8 +2144,15 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
#endif
WriteRumble(ctx);
}
}
if (size < 0) {
/* Reconnect the Bluetooth device once the USB device is gone */
if (device->num_joysticks == 0 && device->is_bluetooth && packet_count > 0 &&
!HIDAPI_HasConnectedUSBDevice(device->serial)) {
HIDAPI_JoystickConnected(device, NULL);
}
if (size < 0 && device->num_joysticks > 0) {
/* Read error, device is disconnected */
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
}