Skip to content

Commit

Permalink
Rumbling the Google Stadia Controller over Bluetooth works on Linux a…
Browse files Browse the repository at this point in the history
…nd macOS
  • Loading branch information
slouken committed Feb 21, 2023
1 parent 021a7cf commit 4aeec9d
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/joystick/hidapi/SDL_hidapi_stadia.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ enum

typedef struct
{
SDL_bool rumble_supported;
Uint8 last_state[USB_PACKET_LENGTH];
} SDL_DriverStadia_Context;

Expand Down Expand Up @@ -74,12 +75,12 @@ static SDL_bool HIDAPI_DriverStadia_InitDevice(SDL_HIDAPI_Device *device)
}
device->context = ctx;

/* Check whether this is connected via USB or Bluetooth */
/* Check whether rumble is supported */
{
Uint8 rumble_packet[] = { 0x05, 0x00, 0x00, 0x00, 0x00 };

if (SDL_hid_write(device->dev, rumble_packet, sizeof(rumble_packet)) < 0) {
device->is_bluetooth = SDL_TRUE;
if (SDL_hid_write(device->dev, rumble_packet, sizeof(rumble_packet)) >= 0) {
ctx->rumble_supported = SDL_TRUE;
}
}

Expand Down Expand Up @@ -116,21 +117,24 @@ static SDL_bool HIDAPI_DriverStadia_OpenJoystick(SDL_HIDAPI_Device *device, SDL_

static int HIDAPI_DriverStadia_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
Uint8 rumble_packet[] = { 0x05, 0x00, 0x00, 0x00, 0x00 };
SDL_DriverStadia_Context *ctx = (SDL_DriverStadia_Context *)device->context;

if (ctx->rumble_supported) {
Uint8 rumble_packet[] = { 0x05, 0x00, 0x00, 0x00, 0x00 };

if (device->is_bluetooth) {
return SDL_Unsupported();
}

rumble_packet[1] = (low_frequency_rumble & 0xFF);
rumble_packet[2] = (low_frequency_rumble >> 8);
rumble_packet[3] = (high_frequency_rumble & 0xFF);
rumble_packet[4] = (high_frequency_rumble >> 8);
rumble_packet[1] = (low_frequency_rumble & 0xFF);
rumble_packet[2] = (low_frequency_rumble >> 8);
rumble_packet[3] = (high_frequency_rumble & 0xFF);
rumble_packet[4] = (high_frequency_rumble >> 8);

if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
return SDL_SetError("Couldn't send rumble packet");
if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
return SDL_SetError("Couldn't send rumble packet");
}
return 0;
} else {
return SDL_Unsupported();
}
return 0;
}

static int HIDAPI_DriverStadia_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
Expand All @@ -140,9 +144,10 @@ static int HIDAPI_DriverStadia_RumbleJoystickTriggers(SDL_HIDAPI_Device *device,

static Uint32 HIDAPI_DriverStadia_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_DriverStadia_Context *ctx = (SDL_DriverStadia_Context *)device->context;
Uint32 caps = 0;

if (!device->is_bluetooth) {
if (ctx->rumble_supported) {
caps |= SDL_JOYCAP_RUMBLE;
}
return caps;
Expand Down

0 comments on commit 4aeec9d

Please sign in to comment.