Added support for the Steam Virtual Gamepad on macOS Sequoia

This commit is contained in:
Sam Lantinga 2024-10-16 10:46:08 -07:00
parent 3ebfdb04be
commit d7b1ba1bfc
6 changed files with 43 additions and 3 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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)) {

View file

@ -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
@ -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);
}
@ -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]) {

View file

@ -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;
@ -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;
}

View file

@ -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;