From 0da59802aa9572e5f7c4791c45f778f99456a00d Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 9 Nov 2015 18:13:47 -0400 Subject: [PATCH] iOS: Set the 'player index' of MFi game controllers when they're opened for use. MFi controllers display their player index via LEDs on the controller. --- src/joystick/iphoneos/SDL_sysjoystick.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/joystick/iphoneos/SDL_sysjoystick.m b/src/joystick/iphoneos/SDL_sysjoystick.m index 93f42b04b775b..688d3efbdf0a9 100644 --- a/src/joystick/iphoneos/SDL_sysjoystick.m +++ b/src/joystick/iphoneos/SDL_sysjoystick.m @@ -361,6 +361,25 @@ SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index) } else { #ifdef SDL_JOYSTICK_MFI GCController *controller = device->controller; + BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO}; + + /* Find the player index of all other connected controllers. */ + for (GCController *c in [GCController controllers]) { + if (c != controller && c.playerIndex >= 0) { + usedPlayerIndexSlots[c.playerIndex] = YES; + } + } + + /* Set this controller's player index to the first unused index. + * FIXME: This logic isn't great... but SDL doesn't expose this + * concept in its external API, so we don't have much to go on. */ + for (int i = 0; i < 4; i++) { + if (!usedPlayerIndexSlots[i]) { + controller.playerIndex = i; + break; + } + } + controller.controllerPausedHandler = ^(GCController *controller) { if (joystick->hwdata) { ++joystick->hwdata->num_pause_presses;