iOS: Set the 'player index' of MFi game controllers when they're opened for use.
authorAlex Szpakowski <slime73@gmail.com>
Mon, 09 Nov 2015 18:13:47 -0400
changeset 9909b2c000b256ea
parent 9908 b63158b01a7d
child 9910 3ef321f52ca5
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
     1.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Mon Nov 09 17:41:54 2015 -0400
     1.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Mon Nov 09 18:13:47 2015 -0400
     1.3 @@ -361,6 +361,25 @@
     1.4          } else {
     1.5  #ifdef SDL_JOYSTICK_MFI
     1.6              GCController *controller = device->controller;
     1.7 +            BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
     1.8 +
     1.9 +            /* Find the player index of all other connected controllers. */
    1.10 +            for (GCController *c in [GCController controllers]) {
    1.11 +                if (c != controller && c.playerIndex >= 0) {
    1.12 +                    usedPlayerIndexSlots[c.playerIndex] = YES;
    1.13 +                }
    1.14 +            }
    1.15 +
    1.16 +            /* Set this controller's player index to the first unused index.
    1.17 +             * FIXME: This logic isn't great... but SDL doesn't expose this
    1.18 +             * concept in its external API, so we don't have much to go on. */
    1.19 +            for (int i = 0; i < 4; i++) {
    1.20 +                if (!usedPlayerIndexSlots[i]) {
    1.21 +                    controller.playerIndex = i;
    1.22 +                    break;
    1.23 +                }
    1.24 +            }
    1.25 +
    1.26              controller.controllerPausedHandler = ^(GCController *controller) {
    1.27                  if (joystick->hwdata) {
    1.28                      ++joystick->hwdata->num_pause_presses;