Skip to content

Commit

Permalink
Added support for the Steam Virtual Gamepad on macOS Sequoia
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Oct 16, 2024
1 parent 3ebfdb0 commit d7b1ba1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/joystick/SDL_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -3090,6 +3090,15 @@ bool SDL_IsJoystickNVIDIASHIELDController(Uint16 vendor_id, Uint16 product_id)
product_id == USB_PRODUCT_NVIDIA_SHIELD_CONTROLLER_V104));
}

bool SDL_IsJoystickSteamVirtualGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version)
{
#ifdef SDL_PLATFORM_MACOS
return (vendor_id == USB_VENDOR_MICROSOFT && product_id == USB_PRODUCT_XBOX360_WIRED_CONTROLLER && version == 0);
#else
return (vendor_id == USB_VENDOR_VALVE && product_id == USB_PRODUCT_STEAM_VIRTUAL_GAMEPAD);
#endif
}

bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id)
{
EControllerType eType = GuessControllerType(vendor_id, product_id);
Expand Down
3 changes: 3 additions & 0 deletions src/joystick/SDL_joystick_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ extern bool SDL_IsJoystickGoogleStadiaController(Uint16 vendor_id, Uint16 produc
// Function to return whether a joystick is an NVIDIA SHIELD controller
extern bool SDL_IsJoystickNVIDIASHIELDController(Uint16 vendor_id, Uint16 product_id);

// Function to return whether a joystick is a Steam Virtual Gamepad
extern bool SDL_IsJoystickSteamVirtualGamepad(Uint16 vendor_id, Uint16 product_id, Uint16 version);

// Function to return whether a joystick is a Steam Controller
extern bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id);

Expand Down
4 changes: 4 additions & 0 deletions src/joystick/apple/SDL_mfijoystick.m
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ static bool IOS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCControlle
return false;
}
#endif
if (device->is_xbox && SDL_strncmp(name, "GamePad-", 8) == 0) {
// This is a Steam Virtual Gamepad, which isn't supported by GCController
return false;
}
CheckControllerSiriRemote(controller, &device->is_siri_remote);

if (device->is_siri_remote && !SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, true)) {
Expand Down
22 changes: 19 additions & 3 deletions src/joystick/hidapi/SDL_hidapi_xbox360.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ static bool HIDAPI_DriverXbox360_IsSupportedDevice(SDL_HIDAPI_Device *device, co
return false;
}
#if defined(SDL_PLATFORM_MACOS) && defined(SDL_JOYSTICK_MFI)
// On macOS you can't write output reports to wired XBox controllers,
// so we'll just use the GCController support instead.
return false;
if (SDL_IsJoystickSteamVirtualGamepad(vendor_id, product_id, version)) {
// GCController support doesn't work with the Steam Virtual Gamepad
return true;
} else {
// On macOS you can't write output reports to wired XBox controllers,
// so we'll just use the GCController support instead.
return false;
}
#else
return (type == SDL_GAMEPAD_TYPE_XBOX360);
#endif
Expand Down Expand Up @@ -138,6 +143,13 @@ static bool HIDAPI_DriverXbox360_InitDevice(SDL_HIDAPI_Device *device)

device->type = SDL_GAMEPAD_TYPE_XBOX360;

if (SDL_IsJoystickSteamVirtualGamepad(device->vendor_id, device->product_id, device->version) &&
device->product_string && SDL_strncmp(device->product_string, "GamePad-", 8) == 0) {
int slot = 0;
SDL_sscanf(device->product_string, "GamePad-%d", &slot);
device->steam_virtual_gamepad_slot = (slot - 1);
}

return HIDAPI_JoystickConnected(device, NULL);
}

Expand Down Expand Up @@ -231,7 +243,11 @@ static bool HIDAPI_DriverXbox360_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *de
static void HIDAPI_DriverXbox360_HandleStatePacket(SDL_Joystick *joystick, SDL_DriverXbox360_Context *ctx, Uint8 *data, int size)
{
Sint16 axis;
#ifdef SDL_PLATFORM_MACOS
const bool invert_y_axes = false;
#else
const bool invert_y_axes = true;
#endif
Uint64 timestamp = SDL_GetTicksNS();

if (ctx->last_state[2] != data[2]) {
Expand Down
7 changes: 7 additions & 0 deletions src/joystick/hidapi/SDL_hidapijoystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ static SDL_HIDAPI_Device *HIDAPI_AddDevice(const struct SDL_hid_device_info *inf
device->guid = SDL_CreateJoystickGUID(bus, device->vendor_id, device->product_id, device->version, device->manufacturer_string, device->product_string, 'h', 0);
device->joystick_type = SDL_JOYSTICK_TYPE_GAMEPAD;
device->type = SDL_GetJoystickGameControllerProtocol(device->name, device->vendor_id, device->product_id, device->interface_number, device->interface_class, device->interface_subclass, device->interface_protocol);
device->steam_virtual_gamepad_slot = -1;

if (num_children > 0) {
int i;
Expand Down Expand Up @@ -1440,6 +1441,12 @@ static const char *HIDAPI_JoystickGetDevicePath(int device_index)

static int HIDAPI_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
{
SDL_HIDAPI_Device *device;

device = HIDAPI_GetDeviceByIndex(device_index, NULL);
if (device) {
return device->steam_virtual_gamepad_slot;
}
return -1;
}

Expand Down
1 change: 1 addition & 0 deletions src/joystick/hidapi/SDL_hidapijoystick_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ typedef struct SDL_HIDAPI_Device
bool is_bluetooth;
SDL_JoystickType joystick_type;
SDL_GamepadType type;
int steam_virtual_gamepad_slot;

struct SDL_HIDAPI_DeviceDriver *driver;
void *context;
Expand Down

0 comments on commit d7b1ba1

Please sign in to comment.