|
|
@@ -32,6 +32,10 @@
|
|
|
// Define this if you want to log all packets from the controller
|
|
|
// #define DEBUG_XBOX_PROTOCOL
|
|
|
|
|
|
+#ifdef SDL_PLATFORM_MACOS
|
|
|
+#include <IOKit/IOKitLib.h>
|
|
|
+#endif
|
|
|
+
|
|
|
typedef struct
|
|
|
{
|
|
|
SDL_HIDAPI_Device *device;
|
|
|
@@ -39,6 +43,9 @@ typedef struct
|
|
|
int player_index;
|
|
|
bool player_lights;
|
|
|
Uint8 last_state[USB_PACKET_LENGTH];
|
|
|
+#ifdef SDL_PLATFORM_MACOS
|
|
|
+ bool controlled_by_360controller;
|
|
|
+#endif
|
|
|
} SDL_DriverXbox360_Context;
|
|
|
|
|
|
static void HIDAPI_DriverXbox360_RegisterHints(SDL_HintCallback callback, void *userdata)
|
|
|
@@ -59,6 +66,22 @@ static bool HIDAPI_DriverXbox360_IsEnabled(void)
|
|
|
SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_XBOX, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT)));
|
|
|
}
|
|
|
|
|
|
+#ifdef SDL_PLATFORM_MACOS
|
|
|
+static bool IsControlledBy360ControllerDriverMacOS(SDL_HIDAPI_Device *device)
|
|
|
+{
|
|
|
+ bool controlled_by_360controller = false;
|
|
|
+ if (device && device->path && SDL_strncmp("DevSrvsID:", device->path, 10) == 0) {
|
|
|
+ uint64_t entry_id = SDL_strtoull(device->path + 10, NULL, 10);
|
|
|
+ io_service_t service = IOServiceGetMatchingService(0, IORegistryEntryIDMatching(entry_id));
|
|
|
+ if (service != MACH_PORT_NULL) {
|
|
|
+ controlled_by_360controller = IOObjectConformsTo(service, "Xbox360ControllerClass");
|
|
|
+ IOObjectRelease(service);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return controlled_by_360controller;
|
|
|
+}
|
|
|
+#endif
|
|
|
+
|
|
|
static bool HIDAPI_DriverXbox360_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
|
|
|
{
|
|
|
const int XB360W_IFACE_PROTOCOL = 129; // Wireless
|
|
|
@@ -80,13 +103,22 @@ static bool HIDAPI_DriverXbox360_IsSupportedDevice(SDL_HIDAPI_Device *device, co
|
|
|
// This is the chatpad or other input interface, not the Xbox 360 interface
|
|
|
return false;
|
|
|
}
|
|
|
+#ifdef SDL_PLATFORM_MACOS
|
|
|
+ if (IsControlledBy360ControllerDriverMacOS(device)) {
|
|
|
+ // Wired Xbox controllers are handled by this driver, when they are
|
|
|
+ // controlled by the 360Controller driver available from:
|
|
|
+ // https://github.com/360Controller/360Controller/releases
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+#endif
|
|
|
#if defined(SDL_PLATFORM_MACOS) && defined(SDL_JOYSTICK_MFI)
|
|
|
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.
|
|
|
+ // On macOS when it isn't controlled by the 360Controller driver and
|
|
|
+ // it doesn't look like a Steam virtual gamepad we should rely on
|
|
|
+ // GCController support instead.
|
|
|
return false;
|
|
|
}
|
|
|
#else
|
|
|
@@ -138,6 +170,9 @@ static bool HIDAPI_DriverXbox360_InitDevice(SDL_HIDAPI_Device *device)
|
|
|
return false;
|
|
|
}
|
|
|
ctx->device = device;
|
|
|
+#ifdef SDL_PLATFORM_MACOS
|
|
|
+ ctx->controlled_by_360controller = IsControlledBy360ControllerDriverMacOS(device);
|
|
|
+#endif
|
|
|
|
|
|
device->context = ctx;
|
|
|
|
|
|
@@ -198,6 +233,19 @@ static bool HIDAPI_DriverXbox360_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joy
|
|
|
|
|
|
static bool HIDAPI_DriverXbox360_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
|
|
|
{
|
|
|
+#ifdef SDL_PLATFORM_MACOS
|
|
|
+ if (((SDL_DriverXbox360_Context *)device->context)->controlled_by_360controller) {
|
|
|
+ // On macOS the 360Controller driver uses this short report,
|
|
|
+ // and we need to prefix it with a magic token so hidapi passes it through untouched
|
|
|
+ Uint8 rumble_packet[] = { 'M', 'A', 'G', 'I', 'C', '0', 0x00, 0x04, 0x00, 0x00 };
|
|
|
+ rumble_packet[6 + 2] = (low_frequency_rumble >> 8);
|
|
|
+ rumble_packet[6 + 3] = (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");
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+#endif
|
|
|
Uint8 rumble_packet[] = { 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
|
|
rumble_packet[3] = (low_frequency_rumble >> 8);
|