Skip to content

Commit

Permalink
Put the Nintendo Switch Pro controller back into enhanced mode as needed
Browse files Browse the repository at this point in the history
Fixes #3450
  • Loading branch information
slouken committed Jul 27, 2022
1 parent 5c70e45 commit c51464d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/joystick/hidapi/SDL_hidapi_switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ typedef struct {
Uint32 m_unRumblePending;
SDL_bool m_bHasSensors;
SDL_bool m_bReportSensors;
Uint32 m_unLastInput;

SwitchInputOnlyControllerStatePacket_t m_lastInputOnlyState;
SwitchSimpleStatePacket_t m_lastSimpleState;
Expand Down Expand Up @@ -1608,6 +1609,7 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
SDL_DriverSwitch_Context *ctx = (SDL_DriverSwitch_Context *)device->context;
SDL_Joystick *joystick = NULL;
int size;
Uint32 now;

if (device->num_joysticks > 0) {
joystick = SDL_JoystickFromInstanceID(device->joysticks[0]);
Expand All @@ -1616,6 +1618,8 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
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);
Expand All @@ -1626,22 +1630,34 @@ HIDAPI_DriverSwitch_UpdateDevice(SDL_HIDAPI_Device *device)
switch (ctx->m_rgucReadBuffer[0]) {
case k_eSwitchInputReportIDs_SimpleControllerState:
HandleSimpleControllerState(joystick, ctx, (SwitchSimpleStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
ctx->m_unLastInput = now;
break;
case k_eSwitchInputReportIDs_FullControllerState:
HandleFullControllerState(joystick, ctx, (SwitchStatePacket_t *)&ctx->m_rgucReadBuffer[1]);
ctx->m_unLastInput = now;
break;
default:
break;
}
}
}

if (!ctx->m_bInputOnly && !ctx->m_bUsingBluetooth) {
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 */
SDL_UnlockMutex(ctx->device->dev_lock);
WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_ForceUSB, NULL, 0, SDL_FALSE);
SDL_LockMutex(ctx->device->dev_lock);
}
}

if (ctx->m_bRumblePending || ctx->m_bRumbleZeroPending) {
HIDAPI_DriverSwitch_SendPendingRumble(ctx);
} else if (ctx->m_bRumbleActive &&
SDL_TICKS_PASSED(SDL_GetTicks(), ctx->m_unRumbleSent + RUMBLE_REFRESH_FREQUENCY_MS)) {
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", SDL_GetTicks() - ctx->m_unRumbleSent);
SDL_Log("Sent continuing rumble, %d ms after previous rumble\n", now - ctx->m_unRumbleSent);
#endif
WriteRumble(ctx);
}
Expand Down

0 comments on commit c51464d

Please sign in to comment.