Fixed bug 5051 - Switch Pro Controller hidapi driver does not report battery levels when connected via Bluetooth
authorSam Lantinga <slouken@libsdl.org>
Sun, 22 Mar 2020 11:01:14 -0700
changeset 1366337af808e6222
parent 13662 d32d92e782ab
child 13664 1c73cc1e4a3a
Fixed bug 5051 - Switch Pro Controller hidapi driver does not report battery levels when connected via Bluetooth

bluenaxela+sdl

I've noticed that the Switch Pro Controller hidapi driver does not report battery levels when connected via Bluetooth, despite having code for setting joystick->epowerlevel.

This is caused by the driver always using k_eSwitchInputReportIDs_SimpleControllerState via Bluetooth. Using that mode means that the state reports you get back from the controller do not include battery state. Not using the full controller state over Bluetooth effectively makes this driver's support for setting joystick->epowerlevel entirely pointless, only ever reporting SDL_JOYSTICK_POWER_WIRED.

Is there a reason this was set to only use SimpleControllerState via Bluetooth?

I've attached a patch I'm using to allow getting battery level for the Switch Pro Controller.

A couple notes about this patch:
1) It changes LoadStickCalibration to accept the input_mode that is selected, because that's really what should determine what is used for stick extents, since stick extents differ between the modes.
2) In my patch I only use FullControllerState when the vid/pid matches the official Switch Pro Controller, as a cautionary measure in case some third-party controllers have problems with FullControllerState mode via Bluetooth (I noticed a HORI Wireless Switch Pad I had seemed to not read controller calibration correctly for stick extents. Maybe it's calibration data was uninitialized on account of having never been used with a Switch? I'm unsure, though if that guess is right maybe SDL2 should be detecting an uninitiated calibration state and using some sensible defaults)
src/joystick/hidapi/SDL_hidapi_switch.c
src/joystick/usb_ids.h
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_switch.c	Fri Mar 20 21:05:07 2020 -0700
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_switch.c	Sun Mar 22 11:01:14 2020 -0700
     1.3 @@ -229,17 +229,17 @@
     1.4  static SDL_bool
     1.5  HasHomeLED(int vendor_id, int product_id)
     1.6  {
     1.7 -	/* The Power A Nintendo Switch Pro controllers don't have a Home LED */
     1.8 -	if (vendor_id == 0 && product_id == 0) {
     1.9 -		return SDL_FALSE;
    1.10 -	}
    1.11 +    /* The Power A Nintendo Switch Pro controllers don't have a Home LED */
    1.12 +    if (vendor_id == 0 && product_id == 0) {
    1.13 +        return SDL_FALSE;
    1.14 +    }
    1.15  
    1.16 -	/* HORI Wireless Switch Pad */
    1.17 -	if (vendor_id == 0x0f0d && product_id == 0x00f6) {
    1.18 -		return SDL_FALSE;
    1.19 -	}
    1.20 +    /* HORI Wireless Switch Pad */
    1.21 +    if (vendor_id == 0x0f0d && product_id == 0x00f6) {
    1.22 +        return SDL_FALSE;
    1.23 +    }
    1.24  
    1.25 -	return SDL_TRUE;
    1.26 +    return SDL_TRUE;
    1.27  }
    1.28  
    1.29  static SDL_bool
    1.30 @@ -541,7 +541,7 @@
    1.31      return WriteSubcommand(ctx, k_eSwitchSubcommandIDs_SetPlayerLights, &led_data, sizeof(led_data), NULL);
    1.32  }
    1.33  
    1.34 -static SDL_bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx)
    1.35 +static SDL_bool LoadStickCalibration(SDL_DriverSwitch_Context *ctx, Uint8 input_mode)
    1.36  {
    1.37      Uint8 *pStickCal;
    1.38      size_t stick, axis;
    1.39 @@ -594,7 +594,7 @@
    1.40          }
    1.41      }
    1.42  
    1.43 -    if (ctx->m_bUsingBluetooth) {
    1.44 +    if (input_mode == k_eSwitchInputReportIDs_SimpleControllerState) {
    1.45          for (stick = 0; stick < 2; ++stick) {
    1.46              for(axis = 0; axis < 2; ++axis) {
    1.47                  ctx->m_StickExtents[stick].axis[axis].sMin = (Sint16)(SDL_MIN_SINT16 * 0.5f);
    1.48 @@ -736,7 +736,24 @@
    1.49              ctx->m_bUsingBluetooth = SDL_TRUE;
    1.50          }
    1.51  
    1.52 -        if (!LoadStickCalibration(ctx)) {
    1.53 +        /* Determine the desired input mode (needed before loading stick calibration) */
    1.54 +        if (ctx->m_bUsingBluetooth) {
    1.55 +            input_mode = k_eSwitchInputReportIDs_SimpleControllerState;
    1.56 +        } else {
    1.57 +            input_mode = k_eSwitchInputReportIDs_FullControllerState;
    1.58 +        }
    1.59 +
    1.60 +        /* The official Nintendo Switch Pro Controller supports FullControllerState over bluetooth
    1.61 +         * just fine. We really should use that, or else the epowerlevel code in
    1.62 +         * HandleFullControllerState is completely pointless. We need full state if we want battery
    1.63 +         * level and we only care about battery level over bluetooth anyway.
    1.64 +         */
    1.65 +        if (device->vendor_id == USB_VENDOR_NINTENDO &&
    1.66 +            device->product_id == USB_PRODUCT_NINTENDO_SWITCH_PRO) {
    1.67 +            input_mode = k_eSwitchInputReportIDs_FullControllerState;
    1.68 +        }
    1.69 +
    1.70 +        if (!LoadStickCalibration(ctx, input_mode)) {
    1.71              SDL_SetError("Couldn't load stick calibration");
    1.72              goto error;
    1.73          }
    1.74 @@ -746,12 +763,7 @@
    1.75              goto error;
    1.76          }
    1.77  
    1.78 -        /* Set the desired input mode */
    1.79 -        if (ctx->m_bUsingBluetooth) {
    1.80 -            input_mode = k_eSwitchInputReportIDs_SimpleControllerState;
    1.81 -        } else {
    1.82 -            input_mode = k_eSwitchInputReportIDs_FullControllerState;
    1.83 -        }
    1.84 +        /* Set desired input mode */
    1.85          if (!SetInputMode(ctx, input_mode)) {
    1.86              SDL_SetError("Couldn't set input mode");
    1.87              goto error;
     2.1 --- a/src/joystick/usb_ids.h	Fri Mar 20 21:05:07 2020 -0700
     2.2 +++ b/src/joystick/usb_ids.h	Sun Mar 22 11:01:14 2020 -0700
     2.3 @@ -35,6 +35,7 @@
     2.4  #define USB_VENDOR_VALVE        0x28de
     2.5  
     2.6  #define USB_PRODUCT_NINTENDO_GAMECUBE_ADAPTER           0x0337
     2.7 +#define USB_PRODUCT_NINTENDO_SWITCH_PRO                 0x2009
     2.8  #define USB_PRODUCT_RAZER_PANTHERA                      0x0401
     2.9  #define USB_PRODUCT_RAZER_PANTHERA_EVO                  0x1008
    2.10  #define USB_PRODUCT_SONY_DS4                            0x05c4