Try up to 20 times to read the controller type

It takes a while for Joy-Cons to initialize when plugged in via the Nintendo Joy-Con Charging Grip.
This commit is contained in:
Sam Lantinga 2022-08-31 13:24:23 -07:00
parent e5f161bda4
commit 62f2379e4c
1 changed files with 31 additions and 18 deletions

View File

@ -208,12 +208,12 @@ typedef struct
typedef struct typedef struct
{ {
Uint8 ucPacketType; Uint8 ucPacketType;
Uint8 ucCommandID; Uint8 ucCommandID;
Uint8 ucFiller; Uint8 ucFiller;
Uint8 ucDeviceType; Uint8 ucDeviceType;
Uint8 rgucMACAddress[6]; Uint8 rgucMACAddress[6];
} SwitchProprietaryStatusPacket_t; } SwitchProprietaryStatusPacket_t;
typedef struct typedef struct
@ -931,29 +931,42 @@ ReadJoyConControllerType(SDL_HIDAPI_Device *device)
device->dev = SDL_hid_open_path(device->path, 0); device->dev = SDL_hid_open_path(device->path, 0);
if (device->dev) { if (device->dev) {
if (WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Status, NULL, 0, SDL_TRUE)) { const int MAX_ATTEMPTS = 20;
SwitchProprietaryStatusPacket_t *status = (SwitchProprietaryStatusPacket_t *)&ctx->m_rgucReadBuffer[0]; int attempts = 0;
for (attempts = 0; attempts < MAX_ATTEMPTS; ++attempts) {
if (WriteProprietary(ctx, k_eSwitchProprietaryCommandIDs_Status, NULL, 0, SDL_TRUE)) {
SwitchProprietaryStatusPacket_t *status = (SwitchProprietaryStatusPacket_t *)&ctx->m_rgucReadBuffer[0];
eControllerType = (ESwitchDeviceInfoControllerType) status->ucDeviceType; eControllerType = (ESwitchDeviceInfoControllerType) status->ucDeviceType;
/* The N64 controller reports as a Pro controller over USB */ /* The N64 controller reports as a Pro controller over USB */
if (eControllerType == k_eSwitchDeviceInfoControllerType_ProController && if (eControllerType == k_eSwitchDeviceInfoControllerType_ProController &&
device->product_id == USB_PRODUCT_NINTENDO_N64_CONTROLLER) { device->product_id == USB_PRODUCT_NINTENDO_N64_CONTROLLER) {
eControllerType = k_eSwitchDeviceInfoControllerType_N64; eControllerType = k_eSwitchDeviceInfoControllerType_N64;
}
} else {
SwitchSubcommandInputPacket_t *reply = NULL;
ctx->m_bUsingBluetooth = SDL_TRUE;
if (WriteSubcommand(ctx, k_eSwitchSubcommandIDs_RequestDeviceInfo, NULL, 0, &reply)) {
eControllerType = (ESwitchDeviceInfoControllerType)reply->deviceInfo.ucDeviceType;
}
} }
} else { if (eControllerType == k_eSwitchDeviceInfoControllerType_Unknown) {
SwitchSubcommandInputPacket_t *reply = NULL; /* Wait a bit and try again */
SDL_Delay(100);
ctx->m_bUsingBluetooth = SDL_TRUE; continue;
if (WriteSubcommand(ctx, k_eSwitchSubcommandIDs_RequestDeviceInfo, NULL, 0, &reply)) { } else {
eControllerType = (ESwitchDeviceInfoControllerType)reply->deviceInfo.ucDeviceType; break;
} }
} }
printf("Attempts: %d\n", attempts);
SDL_hid_close(device->dev); SDL_hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
} }
SDL_free(ctx); SDL_free(ctx);
} }
printf("Controller type %d\n", eControllerType);
return eControllerType; return eControllerType;
} }