hidapi: Use GameCube adapter controller port for player index
authorZack Middleton
Sat, 08 Jun 2019 13:36:59 -0700
changeset 127870411f841b035
parent 12776 8eb9c7128f76
child 12788 bfd1b2fc6e2f
hidapi: Use GameCube adapter controller port for player index
The Nintendo USB GameCube adapter has four controller ports. Return
the port number as 0 to 3 from SDL_JoystickGetPlayerIndex() and
SDL_JoystickGetDevicePlayerIndex().
src/joystick/hidapi/SDL_hidapi_gamecube.c
src/joystick/hidapi/SDL_hidapi_ps4.c
src/joystick/hidapi/SDL_hidapi_switch.c
src/joystick/hidapi/SDL_hidapi_xbox360.c
src/joystick/hidapi/SDL_hidapi_xboxone.c
src/joystick/hidapi/SDL_hidapijoystick.c
src/joystick/hidapi/SDL_hidapijoystick_c.h
     1.1 --- a/src/joystick/hidapi/SDL_hidapi_gamecube.c	Sun Jan 13 23:36:31 2019 +0000
     1.2 +++ b/src/joystick/hidapi/SDL_hidapi_gamecube.c	Sat Jun 08 13:36:59 2019 -0700
     1.3 @@ -267,6 +267,22 @@
     1.4      return joysticks;
     1.5  }
     1.6  
     1.7 +static int
     1.8 +HIDAPI_DriverGameCube_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index)
     1.9 +{
    1.10 +    SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)context->context;
    1.11 +    Uint8 i;
    1.12 +    for (i = 0; i < 4; i += 1) {
    1.13 +        if (ctx->joysticks[i] != -1) {
    1.14 +            if (index == 0) {
    1.15 +                return i;
    1.16 +            }
    1.17 +            index -= 1;
    1.18 +        }
    1.19 +    }
    1.20 +    return -1; /* Should never get here! */
    1.21 +}
    1.22 +
    1.23  static SDL_JoystickID
    1.24  HIDAPI_DriverGameCube_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
    1.25  {
    1.26 @@ -294,6 +310,7 @@
    1.27              joystick->nbuttons = 12;
    1.28              joystick->naxes = 6;
    1.29              joystick->epowerlevel = SDL_JOYSTICK_POWER_WIRED;
    1.30 +            joystick->player_index = i;
    1.31              return SDL_TRUE;
    1.32          }
    1.33      }
    1.34 @@ -334,6 +351,7 @@
    1.35      HIDAPI_DriverGameCube_QuitDriver,
    1.36      HIDAPI_DriverGameCube_UpdateDriver,
    1.37      HIDAPI_DriverGameCube_NumJoysticks,
    1.38 +    HIDAPI_DriverGameCube_PlayerIndexForIndex,
    1.39      HIDAPI_DriverGameCube_InstanceIDForIndex,
    1.40      HIDAPI_DriverGameCube_OpenJoystick,
    1.41      HIDAPI_DriverGameCube_Rumble
     2.1 --- a/src/joystick/hidapi/SDL_hidapi_ps4.c	Sun Jan 13 23:36:31 2019 +0000
     2.2 +++ b/src/joystick/hidapi/SDL_hidapi_ps4.c	Sat Jun 08 13:36:59 2019 -0700
     2.3 @@ -338,6 +338,12 @@
     2.4      return 1;
     2.5  }
     2.6  
     2.7 +static int
     2.8 +HIDAPI_DriverPS4_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index)
     2.9 +{
    2.10 +    return -1;
    2.11 +}
    2.12 +
    2.13  static SDL_JoystickID
    2.14  HIDAPI_DriverPS4_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
    2.15  {
    2.16 @@ -592,6 +598,7 @@
    2.17      HIDAPI_DriverPS4_QuitDriver,
    2.18      HIDAPI_DriverPS4_UpdateDriver,
    2.19      HIDAPI_DriverPS4_NumJoysticks,
    2.20 +    HIDAPI_DriverPS4_PlayerIndexForIndex,
    2.21      HIDAPI_DriverPS4_InstanceIDForIndex,
    2.22      HIDAPI_DriverPS4_OpenJoystick,
    2.23      HIDAPI_DriverPS4_Rumble
     3.1 --- a/src/joystick/hidapi/SDL_hidapi_switch.c	Sun Jan 13 23:36:31 2019 +0000
     3.2 +++ b/src/joystick/hidapi/SDL_hidapi_switch.c	Sat Jun 08 13:36:59 2019 -0700
     3.3 @@ -914,6 +914,12 @@
     3.4      return 1;
     3.5  }
     3.6  
     3.7 +static int
     3.8 +HIDAPI_DriverSwitch_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index)
     3.9 +{
    3.10 +    return -1;
    3.11 +}
    3.12 +
    3.13  static SDL_JoystickID
    3.14  HIDAPI_DriverSwitch_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
    3.15  {
    3.16 @@ -931,6 +937,7 @@
    3.17      HIDAPI_DriverSwitch_QuitDriver,
    3.18      HIDAPI_DriverSwitch_UpdateDriver,
    3.19      HIDAPI_DriverSwitch_NumJoysticks,
    3.20 +    HIDAPI_DriverSwitch_PlayerIndexForIndex,
    3.21      HIDAPI_DriverSwitch_InstanceIDForIndex,
    3.22      HIDAPI_DriverSwitch_OpenJoystick,
    3.23      HIDAPI_DriverSwitch_Rumble
     4.1 --- a/src/joystick/hidapi/SDL_hidapi_xbox360.c	Sun Jan 13 23:36:31 2019 +0000
     4.2 +++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c	Sat Jun 08 13:36:59 2019 -0700
     4.3 @@ -330,6 +330,12 @@
     4.4      return 1;
     4.5  }
     4.6  
     4.7 +static int
     4.8 +HIDAPI_DriverXbox360_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index)
     4.9 +{
    4.10 +    return -1;
    4.11 +}
    4.12 +
    4.13  static SDL_JoystickID
    4.14  HIDAPI_DriverXbox360_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
    4.15  {
    4.16 @@ -813,6 +819,7 @@
    4.17      HIDAPI_DriverXbox360_QuitDriver,
    4.18      HIDAPI_DriverXbox360_UpdateDriver,
    4.19      HIDAPI_DriverXbox360_NumJoysticks,
    4.20 +    HIDAPI_DriverXbox360_PlayerIndexForIndex,
    4.21      HIDAPI_DriverXbox360_InstanceIDForIndex,
    4.22      HIDAPI_DriverXbox360_OpenJoystick,
    4.23      HIDAPI_DriverXbox360_Rumble
     5.1 --- a/src/joystick/hidapi/SDL_hidapi_xboxone.c	Sun Jan 13 23:36:31 2019 +0000
     5.2 +++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c	Sat Jun 08 13:36:59 2019 -0700
     5.3 @@ -207,6 +207,12 @@
     5.4      return 1;
     5.5  }
     5.6  
     5.7 +static int
     5.8 +HIDAPI_DriverXboxOne_PlayerIndexForIndex(SDL_HIDAPI_DriverData *context, int index)
     5.9 +{
    5.10 +    return -1;
    5.11 +}
    5.12 +
    5.13  static SDL_JoystickID
    5.14  HIDAPI_DriverXboxOne_InstanceIDForIndex(SDL_HIDAPI_DriverData *context, int index)
    5.15  {
    5.16 @@ -350,6 +356,7 @@
    5.17      HIDAPI_DriverXboxOne_QuitDriver,
    5.18      HIDAPI_DriverXboxOne_UpdateDriver,
    5.19      HIDAPI_DriverXboxOne_NumJoysticks,
    5.20 +    HIDAPI_DriverXboxOne_PlayerIndexForIndex,
    5.21      HIDAPI_DriverXboxOne_InstanceIDForIndex,
    5.22      HIDAPI_DriverXboxOne_OpenJoystick,
    5.23      HIDAPI_DriverXboxOne_Rumble
     6.1 --- a/src/joystick/hidapi/SDL_hidapijoystick.c	Sun Jan 13 23:36:31 2019 +0000
     6.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick.c	Sat Jun 08 13:36:59 2019 -0700
     6.3 @@ -948,7 +948,19 @@
     6.4  static int
     6.5  HIDAPI_JoystickGetDevicePlayerIndex(int device_index)
     6.6  {
     6.7 -    return -1;
     6.8 +    SDL_HIDAPI_Device *device = SDL_HIDAPI_devices;
     6.9 +    int joysticks;
    6.10 +    while (device) {
    6.11 +        if (device->driver) {
    6.12 +            joysticks = device->driver->NumJoysticks(&device->devdata);
    6.13 +            if (device_index < joysticks) {
    6.14 +                break;
    6.15 +            }
    6.16 +            device_index -= joysticks;
    6.17 +        }
    6.18 +        device = device->next;
    6.19 +    }
    6.20 +    return device->driver->PlayerIndexForIndex(&device->devdata, device_index);
    6.21  }
    6.22  
    6.23  static SDL_JoystickGUID
     7.1 --- a/src/joystick/hidapi/SDL_hidapijoystick_c.h	Sun Jan 13 23:36:31 2019 +0000
     7.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h	Sat Jun 08 13:36:59 2019 -0700
     7.3 @@ -65,6 +65,8 @@
     7.4      SDL_bool       (*UpdateDriver)(SDL_HIDAPI_DriverData *context,
     7.5                                     int *num_joysticks);
     7.6      int            (*NumJoysticks)(SDL_HIDAPI_DriverData *context);
     7.7 +    int            (*PlayerIndexForIndex)(SDL_HIDAPI_DriverData *context,
     7.8 +                                          int index);
     7.9      SDL_JoystickID (*InstanceIDForIndex)(SDL_HIDAPI_DriverData *context,
    7.10                                           int index);
    7.11      SDL_bool       (*OpenJoystick)(SDL_HIDAPI_DriverData *context,