joystick: Ensure HIDAPI is initialized before calling it
authorAndrew Eikum <aeikum@codeweavers.com>
Wed, 31 Jul 2019 11:14:48 -0500
changeset 12972c3c029a0b94b
parent 12971 2fbd3d8432ab
child 12973 c52f7214f707
joystick: Ensure HIDAPI is initialized before calling it
src/joystick/hidapi/SDL_hidapijoystick.c
src/joystick/linux/SDL_sysjoystick.c
     1.1 --- a/src/joystick/hidapi/SDL_hidapijoystick.c	Wed Jul 31 11:34:00 2019 -0500
     1.2 +++ b/src/joystick/hidapi/SDL_hidapijoystick.c	Wed Jul 31 11:14:48 2019 -0500
     1.3 @@ -99,6 +99,7 @@
     1.4  static int SDL_HIDAPI_numdrivers = 0;
     1.5  static SDL_HIDAPI_Device *SDL_HIDAPI_devices;
     1.6  static int SDL_HIDAPI_numjoysticks = 0;
     1.7 +static SDL_bool initialized = SDL_FALSE;
     1.8  
     1.9  #if defined(SDL_USE_LIBUDEV)
    1.10  static const SDL_UDEV_Symbols * usyms = NULL;
    1.11 @@ -696,6 +697,10 @@
    1.12  {
    1.13      int i;
    1.14  
    1.15 +    if (initialized) {
    1.16 +        return 0;
    1.17 +    }
    1.18 +
    1.19      if (hid_init() < 0) {
    1.20          SDL_SetError("Couldn't initialize hidapi");
    1.21          return -1;
    1.22 @@ -709,6 +714,9 @@
    1.23                          SDL_HIDAPIDriverHintChanged, NULL);
    1.24      HIDAPI_InitializeDiscovery();
    1.25      HIDAPI_JoystickDetect();
    1.26 +
    1.27 +    initialized = SDL_TRUE;
    1.28 +
    1.29      return 0;
    1.30  }
    1.31  
    1.32 @@ -1059,6 +1067,8 @@
    1.33      SDL_HIDAPI_numjoysticks = 0;
    1.34  
    1.35      hid_exit();
    1.36 +
    1.37 +    initialized = SDL_FALSE;
    1.38  }
    1.39  
    1.40  SDL_JoystickDriver SDL_HIDAPI_JoystickDriver =
     2.1 --- a/src/joystick/linux/SDL_sysjoystick.c	Wed Jul 31 11:34:00 2019 -0500
     2.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Wed Jul 31 11:14:48 2019 -0500
     2.3 @@ -120,6 +120,8 @@
     2.4      }
     2.5  
     2.6  #ifdef SDL_JOYSTICK_HIDAPI
     2.7 +    SDL_HIDAPI_JoystickDriver.Init();
     2.8 +
     2.9      if (HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version)) {
    2.10          /* The HIDAPI driver is taking care of this device */
    2.11          return 0;