Added an untested driver for the Nintendo GameCube adapter, based on code contributed by Ethan Lee
authorSam Lantinga <slouken@libsdl.org>
Thu, 19 Dec 2019 15:01:35 -0800
changeset 133566c431d30a6df
parent 13355 1adaa20a4ae0
child 13357 69329f5f992b
Added an untested driver for the Nintendo GameCube adapter, based on code contributed by Ethan Lee
include/SDL_hints.h
src/hidapi/SDL_hidapi.c
src/joystick/SDL_joystick.c
src/joystick/hidapi/SDL_hidapi_switch.c
src/joystick/hidapi/SDL_hidapijoystick.c
src/joystick/hidapi/SDL_hidapijoystick_c.h
     1.1 --- a/include/SDL_hints.h	Thu Dec 19 15:01:32 2019 -0800
     1.2 +++ b/include/SDL_hints.h	Thu Dec 19 15:01:35 2019 -0800
     1.3 @@ -610,6 +610,17 @@
     1.4  #define SDL_HINT_JOYSTICK_HIDAPI_XBOX   "SDL_JOYSTICK_HIDAPI_XBOX"
     1.5  
     1.6  /**
     1.7 + *  \brief  A variable controlling whether the HIDAPI driver for Nintendo GameCube controllers should be used.
     1.8 + *
     1.9 + *  This variable can be set to the following values:
    1.10 + *    "0"       - HIDAPI driver is not used
    1.11 + *    "1"       - HIDAPI driver is used
    1.12 + *
    1.13 + *  The default is the value of SDL_HINT_JOYSTICK_HIDAPI
    1.14 + */
    1.15 +#define SDL_HINT_JOYSTICK_HIDAPI_GAMECUBE "SDL_JOYSTICK_HIDAPI_GAMECUBE"
    1.16 +
    1.17 +/**
    1.18   *  \brief  A variable that controls whether Steam Controllers should be exposed using the SDL joystick and game controller APIs
    1.19   *
    1.20   *  The variable can be set to the following values:
     2.1 --- a/src/hidapi/SDL_hidapi.c	Thu Dec 19 15:01:32 2019 -0800
     2.2 +++ b/src/hidapi/SDL_hidapi.c	Thu Dec 19 15:01:35 2019 -0800
     2.3 @@ -66,6 +66,7 @@
     2.4  #if __LINUX__
     2.5  
     2.6  #include "../../core/linux/SDL_udev.h"
     2.7 +#undef SDL_USE_LIBUDEV
     2.8  #if SDL_USE_LIBUDEV
     2.9  static const SDL_UDEV_Symbols *udev_ctx = NULL;
    2.10  
     3.1 --- a/src/joystick/SDL_joystick.c	Thu Dec 19 15:01:32 2019 -0800
     3.2 +++ b/src/joystick/SDL_joystick.c	Thu Dec 19 15:01:35 2019 -0800
     3.3 @@ -33,6 +33,7 @@
     3.4  #include "../events/SDL_events_c.h"
     3.5  #endif
     3.6  #include "../video/SDL_sysvideo.h"
     3.7 +#include "hidapi/SDL_hidapijoystick_c.h"
     3.8  
     3.9  /* This is included in only one place because it has a large static list of controllers */
    3.10  #include "controller_type.h"
    3.11 @@ -1043,6 +1044,11 @@
    3.12      /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
    3.13      SDL_UnlockJoysticks();
    3.14  
    3.15 +#ifdef SDL_JOYSTICK_HIDAPI
    3.16 +    /* Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks */
    3.17 +    HIDAPI_UpdateDevices();
    3.18 +#endif /* SDL_JOYSTICK_HIDAPI */
    3.19 +
    3.20      for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
    3.21          if (joystick->attached) {
    3.22              /* This should always be true, but seeing a crash in the wild...? */
     4.1 --- a/src/joystick/hidapi/SDL_hidapi_switch.c	Thu Dec 19 15:01:32 2019 -0800
     4.2 +++ b/src/joystick/hidapi/SDL_hidapi_switch.c	Thu Dec 19 15:01:35 2019 -0800
     4.3 @@ -637,15 +637,15 @@
     4.4      ctx = (SDL_DriverSwitch_Context *)SDL_calloc(1, sizeof(*ctx));
     4.5      if (!ctx) {
     4.6          SDL_OutOfMemory();
     4.7 -        return SDL_FALSE;
     4.8 +        goto error;
     4.9      }
    4.10 +    device->context = ctx;
    4.11  
    4.12      device->dev = ctx->dev = hid_open_path(device->path, 0);
    4.13      if (!device->dev) {
    4.14          SDL_SetError("Couldn't open %s", device->path);
    4.15          goto error;
    4.16      }
    4.17 -    device->context = ctx;
    4.18  
    4.19      /* Find out whether or not we can send output reports */
    4.20      ctx->m_bInputOnly = SDL_IsJoystickNintendoSwitchProInputOnly(device->vendor_id, device->product_id);
     5.1 --- a/src/joystick/hidapi/SDL_hidapijoystick.c	Thu Dec 19 15:01:32 2019 -0800
     5.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick.c	Thu Dec 19 15:01:35 2019 -0800
     5.3 @@ -74,6 +74,9 @@
     5.4  #ifdef SDL_JOYSTICK_HIDAPI_XBOXONE
     5.5      &SDL_HIDAPI_DriverXboxOne,
     5.6  #endif
     5.7 +#ifdef SDL_JOYSTICK_HIDAPI_GAMECUBE
     5.8 +    &SDL_HIDAPI_DriverGameCube,
     5.9 +#endif
    5.10  };
    5.11  static int SDL_HIDAPI_numdrivers = 0;
    5.12  static SDL_mutex *SDL_HIDAPI_mutex;
    5.13 @@ -569,6 +572,7 @@
    5.14                          SDL_HIDAPIDriverHintChanged, NULL);
    5.15      HIDAPI_InitializeDiscovery();
    5.16      HIDAPI_JoystickDetect();
    5.17 +    HIDAPI_UpdateDevices();
    5.18  
    5.19      initialized = SDL_TRUE;
    5.20  
    5.21 @@ -842,14 +846,18 @@
    5.22  static void
    5.23  HIDAPI_JoystickDetect(void)
    5.24  {
    5.25 -    SDL_HIDAPI_Device *device;
    5.26 -
    5.27      HIDAPI_UpdateDiscovery();
    5.28      if (SDL_HIDAPI_discovery.m_bHaveDevicesChanged) {
    5.29          /* FIXME: We probably need to schedule an update in a few seconds as well */
    5.30          HIDAPI_UpdateDeviceList();
    5.31          SDL_HIDAPI_discovery.m_bHaveDevicesChanged = SDL_FALSE;
    5.32      }
    5.33 +}
    5.34 +
    5.35 +void
    5.36 +HIDAPI_UpdateDevices(void)
    5.37 +{
    5.38 +    SDL_HIDAPI_Device *device;
    5.39  
    5.40      /* Update the devices, which may change connected joysticks and send events */
    5.41      SDL_LockMutex(SDL_HIDAPI_mutex);
    5.42 @@ -961,7 +969,7 @@
    5.43  static void
    5.44  HIDAPI_JoystickUpdate(SDL_Joystick * joystick)
    5.45  {
    5.46 -    /* This is handled in HIDAPI_JoystickDetect() */
    5.47 +    /* This is handled in SDL_HIDAPI_UpdateDevices() */
    5.48  }
    5.49  
    5.50  static void
     6.1 --- a/src/joystick/hidapi/SDL_hidapijoystick_c.h	Thu Dec 19 15:01:32 2019 -0800
     6.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick_c.h	Thu Dec 19 15:01:35 2019 -0800
     6.3 @@ -30,6 +30,7 @@
     6.4  #define SDL_JOYSTICK_HIDAPI_SWITCH
     6.5  #define SDL_JOYSTICK_HIDAPI_XBOX360
     6.6  #define SDL_JOYSTICK_HIDAPI_XBOXONE
     6.7 +#define SDL_JOYSTICK_HIDAPI_GAMECUBE
     6.8  
     6.9  #ifdef __WINDOWS__
    6.10  /* On Windows, Xbox One controllers are handled by the Xbox 360 driver */
    6.11 @@ -95,10 +96,12 @@
    6.12  extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360;
    6.13  extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W;
    6.14  extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXboxOne;
    6.15 +extern SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube;
    6.16  
    6.17  /* Return true if a HID device is present and supported as a joystick */
    6.18  extern SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name);
    6.19  
    6.20 +extern void HIDAPI_UpdateDevices(void);
    6.21  extern SDL_bool HIDAPI_JoystickConnected(SDL_HIDAPI_Device *device, SDL_JoystickID *pJoystickID);
    6.22  extern void HIDAPI_JoystickDisconnected(SDL_HIDAPI_Device *device, SDL_JoystickID joystickID);
    6.23