[Android] Fixes #2264, handle joystick open/closed state properly
authorGabriel Jacobo <gabomdq@gmail.com>
Sat, 23 Nov 2013 09:47:25 -0300
changeset 8024ffbdb99af7bd
parent 8023 7e9b97eb8aa3
child 8025 a867f1891d2a
[Android] Fixes #2264, handle joystick open/closed state properly
src/joystick/android/SDL_sysjoystick.c
     1.1 --- a/src/joystick/android/SDL_sysjoystick.c	Sat Nov 23 02:02:29 2013 -0800
     1.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Sat Nov 23 09:47:25 2013 -0300
     1.3 @@ -161,20 +161,18 @@
     1.4      if (SYS_accelAsJoy) {
     1.5          SYS_numjoysticks++;
     1.6      }
     1.7 -    SYS_Joysticks = (SDL_Joystick **)SDL_malloc(SYS_numjoysticks*sizeof(SDL_Joystick *));
     1.8 +    SYS_Joysticks = (SDL_Joystick **)SDL_calloc(1, SYS_numjoysticks*sizeof(SDL_Joystick *));
     1.9      if (SYS_Joysticks == NULL)
    1.10      {
    1.11          return SDL_OutOfMemory();
    1.12      }
    1.13 -    SYS_JoystickNames = (char **)SDL_malloc(SYS_numjoysticks*sizeof(char *));
    1.14 +    SYS_JoystickNames = (char **)SDL_calloc(1, SYS_numjoysticks*sizeof(char *));
    1.15      if (SYS_JoystickNames == NULL)
    1.16      {
    1.17          SDL_free(SYS_Joysticks);
    1.18          SYS_Joysticks = NULL;
    1.19          return SDL_OutOfMemory();
    1.20      }
    1.21 -    SDL_memset(SYS_JoystickNames, 0, (SYS_numjoysticks*sizeof(char *)));
    1.22 -    SDL_memset(SYS_Joysticks, 0, (SYS_numjoysticks*sizeof(SDL_Joystick *)));
    1.23      
    1.24      for (i = 0; i < SYS_numjoysticks; i++)
    1.25      {
    1.26 @@ -274,6 +272,15 @@
    1.27  void
    1.28  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    1.29  {
    1.30 +    int device_index;
    1.31 +    
    1.32 +    for (device_index = 0; device_index < SYS_numjoysticks; device_index++) {
    1.33 +        if ( SYS_Joysticks[device_index] == joystick ) {
    1.34 +            SYS_Joysticks[device_index] = NULL;
    1.35 +        }
    1.36 +    }
    1.37 +    
    1.38 +    joystick->closed = 1;
    1.39  }
    1.40  
    1.41  /* Function to perform any system-specific joystick related cleanup */
    1.42 @@ -311,7 +318,9 @@
    1.43  {
    1.44      int button = keycode_to_SDL(keycode);
    1.45      if (button >= 0) {
    1.46 -        SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
    1.47 +        if (SYS_Joysticks[padId]) {
    1.48 +            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
    1.49 +        }
    1.50          return 0;
    1.51      }
    1.52      
    1.53 @@ -323,7 +332,9 @@
    1.54  {
    1.55      int button = keycode_to_SDL(keycode);
    1.56      if (button >= 0) {
    1.57 -        SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
    1.58 +        if (SYS_Joysticks[padId]) {
    1.59 +            SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
    1.60 +        }
    1.61          return 0;
    1.62      }
    1.63      
    1.64 @@ -335,7 +346,9 @@
    1.65  {
    1.66      /* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
    1.67      /* TODO: Are the reported values right? */
    1.68 -    SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
    1.69 +    if (SYS_Joysticks[joyId]) {
    1.70 +        SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
    1.71 +    }
    1.72      
    1.73      return 0;
    1.74  }