From 45b01d16b1132b586fb739748c574b13e7df147f Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 26 Feb 2025 11:21:34 -0800 Subject: [PATCH] Fixed reliability of initializing Switch controllers on macOS It looks like both macOS (15.1.1) and SDL are trying to talk to the controller at the same time, which can cause interleaved replies or even locking up the controller. Waiting a bit before talking to the controller seems to take care of this. --- src/joystick/hidapi/SDL_hidapi_switch.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/joystick/hidapi/SDL_hidapi_switch.c b/src/joystick/hidapi/SDL_hidapi_switch.c index 54c6d96367..253c4f9599 100644 --- a/src/joystick/hidapi/SDL_hidapi_switch.c +++ b/src/joystick/hidapi/SDL_hidapi_switch.c @@ -817,7 +817,9 @@ static SDL_bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx) SwitchSubcommandInputPacket_t *factory_reply = NULL; SwitchSPIOpData_t readUserParams; SwitchSPIOpData_t readFactoryParams; - + const int MAX_ATTEMPTS = 3; + int attempt; + /* Read User Calibration Info */ readUserParams.unAddress = k_unSPIStickUserCalibrationStartOffset; readUserParams.ucLength = k_unSPIStickUserCalibrationLength; @@ -829,8 +831,19 @@ static SDL_bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx) readFactoryParams.unAddress = k_unSPIStickFactoryCalibrationStartOffset; readFactoryParams.ucLength = k_unSPIStickFactoryCalibrationLength; - if (!WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readFactoryParams, sizeof(readFactoryParams), &factory_reply)) { - return SDL_FALSE; + for (attempt = 0; ; ++attempt) { + if (!WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SPIFlashRead, (uint8_t *)&readFactoryParams, sizeof(readFactoryParams), &factory_reply)) { + return SDL_FALSE; + } + + if (factory_reply->stickFactoryCalibration.opData.unAddress == k_unSPIStickFactoryCalibrationStartOffset) { + /* We successfully read the calibration data */ + break; + } + + if (attempt == MAX_ATTEMPTS) { + return SDL_FALSE; + } } /* Automatically select the user calibration if magic bytes are set */ @@ -1426,6 +1439,10 @@ static SDL_bool HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_ ctx->m_bSyncWrite = SDL_TRUE; if (!ctx->m_bInputOnly) { +#ifdef SDL_PLATFORM_MACOS + // Wait for the OS to finish its handshake with the controller + SDL_Delay(250); +#endif GetInitialInputMode(ctx); ctx->m_nCurrentInputMode = ctx->m_nInitialInputMode;