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