mirror of https://github.com/encounter/SDL.git
Updated logic to match between PS4/PS5/Switch controllers
This commit is contained in:
parent
8c40a6b0c7
commit
1e1be0b954
|
@ -1016,6 +1016,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
Uint8 data[USB_PACKET_LENGTH*2];
|
Uint8 data[USB_PACKET_LENGTH*2];
|
||||||
int size;
|
int size;
|
||||||
int packet_count = 0;
|
int packet_count = 0;
|
||||||
|
Uint32 now = SDL_GetTicks();
|
||||||
|
|
||||||
if (device->num_joysticks > 0) {
|
if (device->num_joysticks > 0) {
|
||||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||||
|
@ -1030,7 +1031,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
}
|
}
|
||||||
|
|
||||||
++packet_count;
|
++packet_count;
|
||||||
ctx->last_packet = SDL_GetTicks();
|
ctx->last_packet = now;
|
||||||
|
|
||||||
if (!joystick) {
|
if (!joystick) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -1067,7 +1068,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
if (device->is_bluetooth) {
|
if (device->is_bluetooth) {
|
||||||
if (packet_count == 0) {
|
if (packet_count == 0) {
|
||||||
/* Check to see if it looks like the device disconnected */
|
/* 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 */
|
/* Send an empty output report to tickle the Bluetooth stack */
|
||||||
HIDAPI_DriverPS4_TickleBluetooth(device);
|
HIDAPI_DriverPS4_TickleBluetooth(device);
|
||||||
}
|
}
|
||||||
|
@ -1084,7 +1085,7 @@ HIDAPI_DriverPS4_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
if (packet_count == 0) {
|
if (packet_count == 0) {
|
||||||
if (device->num_joysticks > 0) {
|
if (device->num_joysticks > 0) {
|
||||||
/* Check to see if it looks like the device disconnected */
|
/* 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]);
|
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1302,6 +1302,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
Uint8 data[USB_PACKET_LENGTH*2];
|
Uint8 data[USB_PACKET_LENGTH*2];
|
||||||
int size;
|
int size;
|
||||||
int packet_count = 0;
|
int packet_count = 0;
|
||||||
|
Uint32 now = SDL_GetTicks();
|
||||||
|
|
||||||
if (device->num_joysticks > 0) {
|
if (device->num_joysticks > 0) {
|
||||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||||
|
@ -1316,7 +1317,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
}
|
}
|
||||||
|
|
||||||
++packet_count;
|
++packet_count;
|
||||||
ctx->last_packet = SDL_GetTicks();
|
ctx->last_packet = now;
|
||||||
|
|
||||||
if (!joystick) {
|
if (!joystick) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -1361,7 +1362,7 @@ HIDAPI_DriverPS5_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
if (device->is_bluetooth) {
|
if (device->is_bluetooth) {
|
||||||
if (packet_count == 0) {
|
if (packet_count == 0) {
|
||||||
/* Check to see if it looks like the device disconnected */
|
/* 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 */
|
/* Send an empty output report to tickle the Bluetooth stack */
|
||||||
HIDAPI_DriverPS5_TickleBluetooth(device);
|
HIDAPI_DriverPS5_TickleBluetooth(device);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2083,76 +2083,76 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
|
||||||
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
|
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
|
||||||
SDL_Joystick *joystick = NULL;
|
SDL_Joystick *joystick = NULL;
|
||||||
int size;
|
int size;
|
||||||
Uint32 now;
|
int packet_count = 0;
|
||||||
|
Uint32 now = SDL_GetTicks();
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (device->num_joysticks > 0) {
|
if (device->num_joysticks > 0) {
|
||||||
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
|
||||||
} else {
|
|
||||||
return SDL_FALSE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
now = SDL_GetTicks();
|
|
||||||
|
|
||||||
while ((size = ReadInput(ctx)) > 0) {
|
while ((size = ReadInput(ctx)) > 0) {
|
||||||
#ifdef DEBUG_SWITCH_PROTOCOL
|
#ifdef DEBUG_SWITCH_PROTOCOL
|
||||||
HIDAPI_DumpPacket("Nintendo Switch packet: size = %d", ctx->m_rgucReadBuffer, size);
|
HIDAPI_DumpPacket("Nintendo Switch packet: size = %d", ctx->m_rgucReadBuffer, size);
|
||||||
#endif
|
#endif
|
||||||
if (joystick) {
|
++packet_count;
|
||||||
if (ctx->m_bInputOnly) {
|
ctx->m_unLastInput = now;
|
||||||
HandleInputOnlyControllerState(joystick, ctx, (SwitchInputOnlyControllerStatePacket_t *)&ctx->m_rgucReadBuffer[0]);
|
|
||||||
} else {
|
if (!joystick) {
|
||||||
switch (ctx->m_rgucReadBuffer[0]) {
|
continue;
|
||||||
case k_eSwitchInputReportIDs_SimpleControllerState:
|
}
|
||||||
HandleSimpleControllerState(joystick, ctx, (SwitchSimpleStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
|
|
||||||
break;
|
if (ctx->m_bInputOnly) {
|
||||||
case k_eSwitchInputReportIDs_FullControllerState:
|
HandleInputOnlyControllerState(joystick, ctx, (SwitchInputOnlyControllerStatePacket_t *)&ctx->m_rgucReadBuffer[0]);
|
||||||
HandleFullControllerState(joystick, ctx, (SwitchStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
|
} else {
|
||||||
break;
|
switch (ctx->m_rgucReadBuffer[0]) {
|
||||||
default:
|
case k_eSwitchInputReportIDs_SimpleControllerState:
|
||||||
break;
|
HandleSimpleControllerState(joystick, ctx, (SwitchSimpleStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
|
||||||
}
|
break;
|
||||||
|
case k_eSwitchInputReportIDs_FullControllerState:
|
||||||
|
HandleFullControllerState(joystick, ctx, (SwitchStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ctx->m_unLastInput = now;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (joystick) {
|
if (joystick) {
|
||||||
if (!ctx->m_bInputOnly && !device->is_bluetooth &&
|
if (packet_count == 0) {
|
||||||
ctx->device->product_id != USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
|
if (!ctx->m_bInputOnly && !device->is_bluetooth &&
|
||||||
const Uint32 INPUT_WAIT_TIMEOUT_MS = 100;
|
ctx->device->product_id != USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
|
||||||
if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
const Uint32 INPUT_WAIT_TIMEOUT_MS = 100;
|
||||||
/* Steam may have put the controller back into non-reporting mode */
|
if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||||
WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, SDL_FALSE);
|
/* Steam may have put the controller back into non-reporting mode */
|
||||||
}
|
WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, SDL_FALSE);
|
||||||
} else if (device->is_bluetooth) {
|
}
|
||||||
const Uint32 INPUT_WAIT_TIMEOUT_MS = 3000;
|
} else if (device->is_bluetooth) {
|
||||||
if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
const Uint32 INPUT_WAIT_TIMEOUT_MS = 3000;
|
||||||
/* Bluetooth may have disconnected, try reopening the controller */
|
if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
|
||||||
size = -1;
|
/* Bluetooth may have disconnected, try reopening the controller */
|
||||||
|
size = -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ctx->m_bRumblePending || ctx->m_bRumbleZeroPending) {
|
||||||
|
HIDAPI_DriverSwitch_SendPendingRumble(ctx);
|
||||||
|
} else if (ctx->m_bRumbleActive &&
|
||||||
|
SDL_TICKS_PASSED(now, ctx->m_unRumbleSent + RUMBLE_REFRESH_FREQUENCY_MS)) {
|
||||||
|
#ifdef DEBUG_RUMBLE
|
||||||
|
SDL_Log("Sent continuing rumble, %d ms after previous rumble\n", now - ctx->m_unRumbleSent);
|
||||||
|
#endif
|
||||||
|
WriteRumble(ctx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->m_bRumblePending || ctx->m_bRumbleZeroPending) {
|
/* Reconnect the Bluetooth device once the USB device is gone */
|
||||||
HIDAPI_DriverSwitch_SendPendingRumble(ctx);
|
if (device->num_joysticks == 0 && device->is_bluetooth && packet_count > 0 &&
|
||||||
} else if (ctx->m_bRumbleActive &&
|
!HIDAPI_HasConnectedUSBDevice(device->serial)) {
|
||||||
SDL_TICKS_PASSED(now, ctx->m_unRumbleSent + RUMBLE_REFRESH_FREQUENCY_MS)) {
|
HIDAPI_JoystickConnected(device, NULL);
|
||||||
#ifdef DEBUG_RUMBLE
|
|
||||||
SDL_Log("Sent continuing rumble, %d ms after previous rumble\n", now - ctx->m_unRumbleSent);
|
|
||||||
#endif
|
|
||||||
WriteRumble(ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size < 0) {
|
if (size < 0 && device->num_joysticks > 0) {
|
||||||
/* Read error, device is disconnected */
|
/* Read error, device is disconnected */
|
||||||
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
|
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue