Added SDL_JoystickGetAxisInitialState() to get a joystick axis' initial value.
authorSam Lantinga <slouken@libsdl.org>
Wed, 04 Jan 2017 10:28:07 -0800
changeset 10752c9d3ff50dfcf
parent 10751 2096b5cdba69
child 10753 220a81177cfb
Added SDL_JoystickGetAxisInitialState() to get a joystick axis' initial value.
This is useful for controller mapping programs to determine an axis' zero state
include/SDL_joystick.h
src/dynapi/SDL_dynapi_overrides.h
src/dynapi/SDL_dynapi_procs.h
src/joystick/SDL_joystick.c
src/joystick/SDL_sysjoystick.h
test/controllermap.c
     1.1 --- a/include/SDL_joystick.h	Wed Jan 04 07:06:48 2017 -0800
     1.2 +++ b/include/SDL_joystick.h	Wed Jan 04 10:28:07 2017 -0800
     1.3 @@ -245,6 +245,18 @@
     1.4                                                     int axis);
     1.5  
     1.6  /**
     1.7 + *  Get the initial state of an axis control on a joystick.
     1.8 + *
     1.9 + *  The state is a value ranging from -32768 to 32767.
    1.10 + *
    1.11 + *  The axis indices start at index 0.
    1.12 + *
    1.13 + *  \return SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
    1.14 + */
    1.15 +extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick,
    1.16 +                                                   int axis, Sint16 *state);
    1.17 +
    1.18 +/**
    1.19   *  \name Hat positions
    1.20   */
    1.21  /* @{ */
     2.1 --- a/src/dynapi/SDL_dynapi_overrides.h	Wed Jan 04 07:06:48 2017 -0800
     2.2 +++ b/src/dynapi/SDL_dynapi_overrides.h	Wed Jan 04 10:28:07 2017 -0800
     2.3 @@ -624,3 +624,4 @@
     2.4  #define SDL_HasNEON SDL_HasNEON_REAL
     2.5  #define SDL_GameControllerNumMappings SDL_GameControllerNumMappings_REAL
     2.6  #define SDL_GameControllerMappingForIndex SDL_GameControllerMappingForIndex_REAL
     2.7 +#define SDL_JoystickGetAxisInitialState SDL_JoystickGetAxisInitialState_REAL
     3.1 --- a/src/dynapi/SDL_dynapi_procs.h	Wed Jan 04 07:06:48 2017 -0800
     3.2 +++ b/src/dynapi/SDL_dynapi_procs.h	Wed Jan 04 10:28:07 2017 -0800
     3.3 @@ -656,3 +656,4 @@
     3.4  SDL_DYNAPI_PROC(SDL_bool,SDL_HasNEON,(void),(),return)
     3.5  SDL_DYNAPI_PROC(int,SDL_GameControllerNumMappings,(void),(),return)
     3.6  SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForIndex,(int a),(a),return)
     3.7 +SDL_DYNAPI_PROC(SDL_bool,SDL_JoystickGetAxisInitialState,(SDL_Joystick *a, int b, Sint16 *c),(a,b,c),return)
     4.1 --- a/src/joystick/SDL_joystick.c	Wed Jan 04 07:06:48 2017 -0800
     4.2 +++ b/src/joystick/SDL_joystick.c	Wed Jan 04 10:28:07 2017 -0800
     4.3 @@ -335,6 +335,25 @@
     4.4  }
     4.5  
     4.6  /*
     4.7 + * Get the initial state of an axis control on a joystick
     4.8 + */
     4.9 +SDL_bool
    4.10 +SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick, int axis, Sint16 *state)
    4.11 +{
    4.12 +    if (!SDL_PrivateJoystickValid(joystick)) {
    4.13 +        return SDL_FALSE;
    4.14 +    }
    4.15 +    if (axis >= joystick->naxes) {
    4.16 +        SDL_SetError("Joystick only has %d axes", joystick->naxes);
    4.17 +        return SDL_FALSE;
    4.18 +    }
    4.19 +    if (state) {
    4.20 +        *state = joystick->axes[axis].initial_value;
    4.21 +    }
    4.22 +    return joystick->axes[axis].has_initial_value;
    4.23 +}
    4.24 +
    4.25 +/*
    4.26   * Get the current state of a hat on a joystick
    4.27   */
    4.28  Uint8
    4.29 @@ -646,6 +665,7 @@
    4.30          return 0;
    4.31      }
    4.32      if (!joystick->axes[axis].has_initial_value) {
    4.33 +        joystick->axes[axis].initial_value = value;
    4.34          joystick->axes[axis].value = value;
    4.35          joystick->axes[axis].zero = value;
    4.36          joystick->axes[axis].has_initial_value = SDL_TRUE;
    4.37 @@ -654,10 +674,9 @@
    4.38          return 0;
    4.39      }
    4.40      if (!joystick->axes[axis].sent_initial_value) {
    4.41 -        int initial_value = joystick->axes[axis].value;
    4.42          joystick->axes[axis].sent_initial_value = SDL_TRUE;
    4.43          joystick->axes[axis].value = value; /* Just so we pass the check above */
    4.44 -        SDL_PrivateJoystickAxis(joystick, axis, initial_value);
    4.45 +        SDL_PrivateJoystickAxis(joystick, axis, joystick->axes[axis].initial_value);
    4.46      }
    4.47  
    4.48      /* We ignore events if we don't have keyboard focus, except for centering
     5.1 --- a/src/joystick/SDL_sysjoystick.h	Wed Jan 04 07:06:48 2017 -0800
     5.2 +++ b/src/joystick/SDL_sysjoystick.h	Wed Jan 04 10:28:07 2017 -0800
     5.3 @@ -31,7 +31,8 @@
     5.4  /* The SDL joystick structure */
     5.5  typedef struct _SDL_JoystickAxisInfo
     5.6  {
     5.7 -    Sint16 value;               /* Current axis states */
     5.8 +    Sint16 initial_value;       /* Initial axis state */
     5.9 +    Sint16 value;               /* Current axis state */
    5.10      Sint16 zero;                /* Zero point on the axis (-32768 for triggers) */
    5.11      SDL_bool has_initial_value; /* Whether we've seen a value on the axis yet */
    5.12      SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */
     6.1 --- a/test/controllermap.c	Wed Jan 04 07:06:48 2017 -0800
     6.2 +++ b/test/controllermap.c	Wed Jan 04 10:28:07 2017 -0800
     6.3 @@ -337,6 +337,7 @@
     6.4      Uint8 alpha=200, alpha_step = -1;
     6.5      Uint32 alpha_ticks = 0;
     6.6      SDL_JoystickID nJoystickID;
     6.7 +    int iIndex;
     6.8  
     6.9      /* Create a window to display joystick axis position */
    6.10      window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
    6.11 @@ -383,6 +384,13 @@
    6.12  
    6.13      s_nNumAxes = SDL_JoystickNumAxes(joystick);
    6.14      s_arrAxisState = SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
    6.15 +    for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
    6.16 +        AxisState *pAxisState = &s_arrAxisState[iIndex];
    6.17 +        Sint16 nInitialValue;
    6.18 +        pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, iIndex, &nInitialValue);
    6.19 +        pAxisState->m_nStartingValue = nInitialValue;
    6.20 +        pAxisState->m_nFarthestValue = nInitialValue;
    6.21 +    }
    6.22  
    6.23      /* Loop, getting joystick events! */
    6.24      while (!done && !s_bBindingComplete) {