Updated logic to match between PS4/PS5/Switch controllers
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
diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c
index 05ce68c..6b4fee3 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps4.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps4.c
@@ -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]);
}
}
diff --git a/src/joystick/hidapi/SDL_hidapi_ps5.c b/src/joystick/hidapi/SDL_hidapi_ps5.c
index 93b59ce..3892764 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps5.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps5.c
@@ -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);
}
diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c
index 66feeab..8a7c9f0 100644
--- a/src/joystick/hidapi/SDL_hidapi_switch.c
+++ b/src/joystick/hidapi/SDL_hidapi_switch.c
@@ -2083,76 +2083,76 @@ 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) {
- if (ctx->m_bInputOnly) {
- HandleInputOnlyControllerState(joystick, ctx, (SwitchInputOnlyControllerStatePacket_t *)&ctx->m_rgucReadBuffer[0]);
- } else {
- switch (ctx->m_rgucReadBuffer[0]) {
- case k_eSwitchInputReportIDs_SimpleControllerState:
- 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;
- }
+ ++packet_count;
+ ctx->m_unLastInput = now;
+
+ if (!joystick) {
+ continue;
+ }
+
+ if (ctx->m_bInputOnly) {
+ HandleInputOnlyControllerState(joystick, ctx, (SwitchInputOnlyControllerStatePacket_t *)&ctx->m_rgucReadBuffer[0]);
+ } else {
+ switch (ctx->m_rgucReadBuffer[0]) {
+ case k_eSwitchInputReportIDs_SimpleControllerState:
+ 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 (!ctx->m_bInputOnly && !device->is_bluetooth &&
- ctx->device->product_id != USB_PRODUCT_NINTENDO_SWITCH_JOYCON_GRIP) {
- const Uint32 INPUT_WAIT_TIMEOUT_MS = 100;
- if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
- /* 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;
- if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
- /* Bluetooth may have disconnected, try reopening the controller */
- size = -1;
+ 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;
+ if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
+ /* 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;
+ if (SDL_TICKS_PASSED(now, ctx->m_unLastInput + INPUT_WAIT_TIMEOUT_MS)) {
+ /* 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)) {
+ 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);
+ SDL_Log("Sent continuing rumble, %d ms after previous rumble\n", now - ctx->m_unRumbleSent);
#endif
- WriteRumble(ctx);
+ WriteRumble(ctx);
+ }
+ }
+
+ /* 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) {
+ if (size < 0 && device->num_joysticks > 0) {
/* Read error, device is disconnected */
HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
}