Cleanups in the joystick code.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 24 Mar 2015 13:52:01 -0400
changeset 9433bd062398b648
parent 9432 5e8151f59e34
child 9434 0a695c9c3c71
Cleanups in the joystick code.

Removed some redundant state and other confusions.

Fixes Bugzilla #2738.
src/joystick/SDL_joystick.c
src/joystick/SDL_sysjoystick.h
src/joystick/android/SDL_sysjoystick.c
src/joystick/bsd/SDL_sysjoystick.c
src/joystick/darwin/SDL_sysjoystick.c
src/joystick/darwin/SDL_sysjoystick_c.h
src/joystick/dummy/SDL_sysjoystick.c
src/joystick/emscripten/SDL_sysjoystick.c
src/joystick/haiku/SDL_haikujoystick.cc
src/joystick/iphoneos/SDL_sysjoystick.m
src/joystick/linux/SDL_sysjoystick.c
src/joystick/psp/SDL_sysjoystick.c
src/joystick/windows/SDL_mmjoystick.c
src/joystick/windows/SDL_windowsjoystick.c
test/testjoystick.c
     1.1 --- a/src/joystick/SDL_joystick.c	Tue Mar 24 03:24:57 2015 -0400
     1.2 +++ b/src/joystick/SDL_joystick.c	Tue Mar 24 13:52:01 2015 -0400
     1.3 @@ -206,10 +206,6 @@
     1.4          valid = 1;
     1.5      }
     1.6  
     1.7 -    if (joystick && joystick->closed) {
     1.8 -        valid = 0;
     1.9 -    }
    1.10 -
    1.11      return valid;
    1.12  }
    1.13  
    1.14 @@ -412,6 +408,7 @@
    1.15      }
    1.16  
    1.17      SDL_SYS_JoystickClose(joystick);
    1.18 +    joystick->hwdata = NULL;
    1.19  
    1.20      joysticklist = SDL_joysticks;
    1.21      joysticklistprev = NULL;
    1.22 @@ -668,7 +665,7 @@
    1.23  
    1.24          SDL_SYS_JoystickUpdate(joystick);
    1.25  
    1.26 -        if (joystick->closed && joystick->uncentered) {
    1.27 +        if (joystick->force_recentering) {
    1.28              int i;
    1.29  
    1.30              /* Tell the app that everything is centered/unpressed...  */
    1.31 @@ -681,7 +678,7 @@
    1.32              for (i = 0; i < joystick->nhats; i++)
    1.33                  SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
    1.34  
    1.35 -            joystick->uncentered = SDL_FALSE;
    1.36 +            joystick->force_recentering = SDL_FALSE;
    1.37          }
    1.38  
    1.39          SDL_updating_joystick = NULL;
     2.1 --- a/src/joystick/SDL_sysjoystick.h	Tue Mar 24 03:24:57 2015 -0400
     2.2 +++ b/src/joystick/SDL_sysjoystick.h	Tue Mar 24 13:52:01 2015 -0400
     2.3 @@ -53,8 +53,7 @@
     2.4  
     2.5      int ref_count;              /* Reference count for multiple opens */
     2.6  
     2.7 -    SDL_bool closed;            /* SDL_TRUE if this device is no longer valid */
     2.8 -    SDL_bool uncentered;        /* SDL_TRUE if this device needs to have its state reset to 0 */
     2.9 +    SDL_bool force_recentering; /* SDL_TRUE if this device needs to have its state reset to 0 */
    2.10      struct _SDL_Joystick *next; /* pointer to next joystick we have allocated */
    2.11  };
    2.12  
     3.1 --- a/src/joystick/android/SDL_sysjoystick.c	Tue Mar 24 03:24:57 2015 -0400
     3.2 +++ b/src/joystick/android/SDL_sysjoystick.c	Tue Mar 24 13:52:01 2015 -0400
     3.3 @@ -498,7 +498,7 @@
     3.4  /* Function to determine is this joystick is attached to the system right now */
     3.5  SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
     3.6  {
     3.7 -    return !joystick->closed && (joystick->hwdata != NULL);
     3.8 +    return joystick->hwdata != NULL;
     3.9  }
    3.10  
    3.11  void
    3.12 @@ -529,11 +529,6 @@
    3.13  void
    3.14  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    3.15  {
    3.16 -    if (joystick->hwdata) {
    3.17 -        ((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
    3.18 -        joystick->hwdata = NULL;
    3.19 -    }
    3.20 -    joystick->closed = 1;
    3.21  }
    3.22  
    3.23  /* Function to perform any system-specific joystick related cleanup */
     4.1 --- a/src/joystick/bsd/SDL_sysjoystick.c	Tue Mar 24 03:24:57 2015 -0400
     4.2 +++ b/src/joystick/bsd/SDL_sysjoystick.c	Tue Mar 24 13:52:01 2015 -0400
     4.3 @@ -558,8 +558,6 @@
     4.4      close(joy->hwdata->fd);
     4.5      SDL_free(joy->hwdata->path);
     4.6      SDL_free(joy->hwdata);
     4.7 -
     4.8 -    return;
     4.9  }
    4.10  
    4.11  void
     5.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Tue Mar 24 03:24:57 2015 -0400
     5.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Tue Mar 24 13:52:01 2015 -0400
     5.3 @@ -138,7 +138,7 @@
     5.4  JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
     5.5  {
     5.6      recDevice *device = (recDevice *) ctx;
     5.7 -    device->removed = 1;
     5.8 +    device->removed = SDL_TRUE;
     5.9      device->deviceRef = NULL; // deviceRef was invalidated due to the remove
    5.10  #if SDL_HAPTIC_IOKIT
    5.11      MacHaptic_MaybeRemoveDevice(device->ffservice);
    5.12 @@ -677,16 +677,7 @@
    5.13  SDL_bool
    5.14  SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
    5.15  {
    5.16 -    recDevice *device = gpDeviceList;
    5.17 -
    5.18 -    while (device) {
    5.19 -        if (joystick->instance_id == device->instance_id) {
    5.20 -            return SDL_TRUE;
    5.21 -        }
    5.22 -        device = device->pNext;
    5.23 -    }
    5.24 -
    5.25 -    return SDL_FALSE;
    5.26 +    return joystick->hwdata != NULL;
    5.27  }
    5.28  
    5.29  /* Function to update the state of a joystick - called as a device poll.
    5.30 @@ -707,9 +698,10 @@
    5.31      }
    5.32  
    5.33      if (device->removed) {      /* device was unplugged; ignore it. */
    5.34 -        joystick->closed = 1;
    5.35 -        joystick->uncentered = 1;
    5.36 -        joystick->hwdata = NULL;
    5.37 +        if (joystick->hwdata) {
    5.38 +            joystick->force_recentering = SDL_TRUE;
    5.39 +            joystick->hwdata = NULL;
    5.40 +        }
    5.41          return;
    5.42      }
    5.43  
    5.44 @@ -797,7 +789,6 @@
    5.45  void
    5.46  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    5.47  {
    5.48 -    joystick->closed = 1;
    5.49  }
    5.50  
    5.51  /* Function to perform any system-specific joystick related cleanup */
     6.1 --- a/src/joystick/darwin/SDL_sysjoystick_c.h	Tue Mar 24 03:24:57 2015 -0400
     6.2 +++ b/src/joystick/darwin/SDL_sysjoystick_c.h	Tue Mar 24 13:52:01 2015 -0400
     6.3 @@ -58,8 +58,7 @@
     6.4      recElement *firstButton;
     6.5      recElement *firstHat;
     6.6  
     6.7 -    int removed;
     6.8 -    int uncentered;
     6.9 +    SDL_bool removed;
    6.10  
    6.11      int instance_id;
    6.12      SDL_JoystickGUID guid;
     7.1 --- a/src/joystick/dummy/SDL_sysjoystick.c	Tue Mar 24 03:24:57 2015 -0400
     7.2 +++ b/src/joystick/dummy/SDL_sysjoystick.c	Tue Mar 24 13:52:01 2015 -0400
     7.3 @@ -34,7 +34,7 @@
     7.4  int
     7.5  SDL_SYS_JoystickInit(void)
     7.6  {
     7.7 -    return (0);
     7.8 +    return 0;
     7.9  }
    7.10  
    7.11  int SDL_SYS_NumJoysticks()
    7.12 @@ -85,21 +85,18 @@
    7.13  void
    7.14  SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
    7.15  {
    7.16 -    return;
    7.17  }
    7.18  
    7.19  /* Function to close a joystick after use */
    7.20  void
    7.21  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    7.22  {
    7.23 -    return;
    7.24  }
    7.25  
    7.26  /* Function to perform any system-specific joystick related cleanup */
    7.27  void
    7.28  SDL_SYS_JoystickQuit(void)
    7.29  {
    7.30 -    return;
    7.31  }
    7.32  
    7.33  SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
     8.1 --- a/src/joystick/emscripten/SDL_sysjoystick.c	Tue Mar 24 03:24:57 2015 -0400
     8.2 +++ b/src/joystick/emscripten/SDL_sysjoystick.c	Tue Mar 24 13:52:01 2015 -0400
     8.3 @@ -326,7 +326,7 @@
     8.4  /* Function to determine is this joystick is attached to the system right now */
     8.5  SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
     8.6  {
     8.7 -    return !joystick->closed && (joystick->hwdata != NULL);
     8.8 +    return joystick->hwdata != NULL;
     8.9  }
    8.10  
    8.11  /* Function to update the state of a joystick - called as a device poll.
    8.12 @@ -378,11 +378,6 @@
    8.13  void
    8.14  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    8.15  {
    8.16 -    if (joystick->hwdata) {
    8.17 -        ((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
    8.18 -        joystick->hwdata = NULL;
    8.19 -    }
    8.20 -    joystick->closed = 1;
    8.21  }
    8.22  
    8.23  /* Function to perform any system-specific joystick related cleanup */
     9.1 --- a/src/joystick/haiku/SDL_haikujoystick.cc	Tue Mar 24 03:24:57 2015 -0400
     9.2 +++ b/src/joystick/haiku/SDL_haikujoystick.cc	Tue Mar 24 13:52:01 2015 -0400
     9.3 @@ -228,7 +228,6 @@
     9.4              SDL_free(joystick->hwdata->new_hats);
     9.5              SDL_free(joystick->hwdata->new_axes);
     9.6              SDL_free(joystick->hwdata);
     9.7 -            joystick->hwdata = NULL;
     9.8          }
     9.9      }
    9.10  
    10.1 --- a/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Mar 24 03:24:57 2015 -0400
    10.2 +++ b/src/joystick/iphoneos/SDL_sysjoystick.m	Tue Mar 24 13:52:01 2015 -0400
    10.3 @@ -153,7 +153,6 @@
    10.4  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    10.5  {
    10.6      [motionManager stopAccelerometerUpdates];
    10.7 -    joystick->closed = 1;
    10.8  }
    10.9  
   10.10  /* Function to perform any system-specific joystick related cleanup */
    11.1 --- a/src/joystick/linux/SDL_sysjoystick.c	Tue Mar 24 03:24:57 2015 -0400
    11.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Tue Mar 24 13:52:01 2015 -0400
    11.3 @@ -624,7 +624,7 @@
    11.4  /* Function to determine is this joystick is attached to the system right now */
    11.5  SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
    11.6  {
    11.7 -    return !joystick->closed && (joystick->hwdata->item != NULL);
    11.8 +    return joystick->hwdata->item != NULL;
    11.9  }
   11.10  
   11.11  static SDL_INLINE void
   11.12 @@ -841,9 +841,7 @@
   11.13          SDL_free(joystick->hwdata->balls);
   11.14          SDL_free(joystick->hwdata->fname);
   11.15          SDL_free(joystick->hwdata);
   11.16 -        joystick->hwdata = NULL;
   11.17      }
   11.18 -    joystick->closed = 1;
   11.19  }
   11.20  
   11.21  /* Function to perform any system-specific joystick related cleanup */
    12.1 --- a/src/joystick/psp/SDL_sysjoystick.c	Tue Mar 24 03:24:57 2015 -0400
    12.2 +++ b/src/joystick/psp/SDL_sysjoystick.c	Tue Mar 24 13:52:01 2015 -0400
    12.3 @@ -233,7 +233,6 @@
    12.4  /* Function to close a joystick after use */
    12.5  void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
    12.6  {
    12.7 -    /* Do nothing. */
    12.8  }
    12.9  
   12.10  /* Function to perform any system-specific joystick related cleanup */
    13.1 --- a/src/joystick/windows/SDL_mmjoystick.c	Tue Mar 24 03:24:57 2015 -0400
    13.2 +++ b/src/joystick/windows/SDL_mmjoystick.c	Tue Mar 24 13:52:01 2015 -0400
    13.3 @@ -383,9 +383,7 @@
    13.4  void
    13.5  SDL_SYS_JoystickClose(SDL_Joystick * joystick)
    13.6  {
    13.7 -    /* free system specific hardware data */
    13.8      SDL_free(joystick->hwdata);
    13.9 -    joystick->hwdata = NULL;
   13.10  }
   13.11  
   13.12  /* Function to perform any system-specific joystick related cleanup */
    14.1 --- a/src/joystick/windows/SDL_windowsjoystick.c	Tue Mar 24 03:24:57 2015 -0400
    14.2 +++ b/src/joystick/windows/SDL_windowsjoystick.c	Tue Mar 24 13:52:01 2015 -0400
    14.3 @@ -460,7 +460,6 @@
    14.4  
    14.5      /* allocate memory for system specific hardware data */
    14.6      joystick->instance_id = joystickdevice->nInstanceID;
    14.7 -    joystick->closed = SDL_FALSE;
    14.8      joystick->hwdata =
    14.9          (struct joystick_hwdata *) SDL_malloc(sizeof(struct joystick_hwdata));
   14.10      if (joystick->hwdata == NULL) {
   14.11 @@ -480,13 +479,13 @@
   14.12  SDL_bool 
   14.13  SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
   14.14  {
   14.15 -    return !joystick->closed && !joystick->hwdata->removed;
   14.16 +    return joystick->hwdata && !joystick->hwdata->removed;
   14.17  }
   14.18  
   14.19  void
   14.20  SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
   14.21  {
   14.22 -    if (joystick->closed || !joystick->hwdata) {
   14.23 +    if (!joystick->hwdata || joystick->hwdata->removed) {
   14.24          return;
   14.25      }
   14.26  
   14.27 @@ -497,8 +496,7 @@
   14.28      }
   14.29  
   14.30      if (joystick->hwdata->removed) {
   14.31 -        joystick->closed = SDL_TRUE;
   14.32 -        joystick->uncentered = SDL_TRUE;
   14.33 +        joystick->force_recentering = SDL_TRUE;
   14.34      }
   14.35  }
   14.36  
   14.37 @@ -512,10 +510,7 @@
   14.38          SDL_DINPUT_JoystickClose(joystick);
   14.39      }
   14.40  
   14.41 -    /* free system specific hardware data */
   14.42      SDL_free(joystick->hwdata);
   14.43 -
   14.44 -    joystick->closed = SDL_TRUE;
   14.45  }
   14.46  
   14.47  /* Function to perform any system-specific joystick related cleanup */
    15.1 --- a/test/testjoystick.c	Tue Mar 24 03:24:57 2015 -0400
    15.2 +++ b/test/testjoystick.c	Tue Mar 24 13:52:01 2015 -0400
    15.3 @@ -56,6 +56,12 @@
    15.4  
    15.5          while (SDL_PollEvent(&event)) {
    15.6              switch (event.type) {
    15.7 +
    15.8 +            case SDL_JOYDEVICEREMOVED:
    15.9 +                SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
   15.10 +                SDL_Log("Our instance ID is %d\n", (int) SDL_JoystickInstanceID(joystick));
   15.11 +                break;
   15.12 +
   15.13              case SDL_JOYAXISMOTION:
   15.14                  SDL_Log("Joystick %d axis %d value: %d\n",
   15.15                         event.jaxis.which,