Skip to content

Commit

Permalink
Added SDL_JoystickGetAxisInitialState() to get a joystick axis' initi…
Browse files Browse the repository at this point in the history
…al value.

This is useful for controller mapping programs to determine an axis' zero state
  • Loading branch information
slouken committed Jan 4, 2017
1 parent 99e10ef commit 4938c50
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
12 changes: 12 additions & 0 deletions include/SDL_joystick.h
Expand Up @@ -244,6 +244,18 @@ extern DECLSPEC int SDLCALL SDL_JoystickEventState(int state);
extern DECLSPEC Sint16 SDLCALL SDL_JoystickGetAxis(SDL_Joystick * joystick,
int axis);

/**
* Get the initial state of an axis control on a joystick.
*
* The state is a value ranging from -32768 to 32767.
*
* The axis indices start at index 0.
*
* \return SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.
*/
extern DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick,
int axis, Sint16 *state);

/**
* \name Hat positions
*/
Expand Down
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_overrides.h
Expand Up @@ -624,3 +624,4 @@
#define SDL_HasNEON SDL_HasNEON_REAL
#define SDL_GameControllerNumMappings SDL_GameControllerNumMappings_REAL
#define SDL_GameControllerMappingForIndex SDL_GameControllerMappingForIndex_REAL
#define SDL_JoystickGetAxisInitialState SDL_JoystickGetAxisInitialState_REAL
1 change: 1 addition & 0 deletions src/dynapi/SDL_dynapi_procs.h
Expand Up @@ -656,3 +656,4 @@ SDL_DYNAPI_PROC(Uint16,SDL_GameControllerGetProductVersion,(SDL_GameController *
SDL_DYNAPI_PROC(SDL_bool,SDL_HasNEON,(void),(),return)
SDL_DYNAPI_PROC(int,SDL_GameControllerNumMappings,(void),(),return)
SDL_DYNAPI_PROC(char*,SDL_GameControllerMappingForIndex,(int a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_JoystickGetAxisInitialState,(SDL_Joystick *a, int b, Sint16 *c),(a,b,c),return)
23 changes: 21 additions & 2 deletions src/joystick/SDL_joystick.c
Expand Up @@ -334,6 +334,25 @@ SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
return (state);
}

/*
* Get the initial state of an axis control on a joystick
*/
SDL_bool
SDL_JoystickGetAxisInitialState(SDL_Joystick * joystick, int axis, Sint16 *state)
{
if (!SDL_PrivateJoystickValid(joystick)) {
return SDL_FALSE;
}
if (axis >= joystick->naxes) {
SDL_SetError("Joystick only has %d axes", joystick->naxes);
return SDL_FALSE;
}
if (state) {
*state = joystick->axes[axis].initial_value;
}
return joystick->axes[axis].has_initial_value;
}

/*
* Get the current state of a hat on a joystick
*/
Expand Down Expand Up @@ -646,6 +665,7 @@ SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
return 0;
}
if (!joystick->axes[axis].has_initial_value) {
joystick->axes[axis].initial_value = value;
joystick->axes[axis].value = value;
joystick->axes[axis].zero = value;
joystick->axes[axis].has_initial_value = SDL_TRUE;
Expand All @@ -654,10 +674,9 @@ SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
return 0;
}
if (!joystick->axes[axis].sent_initial_value) {
int initial_value = joystick->axes[axis].value;
joystick->axes[axis].sent_initial_value = SDL_TRUE;
joystick->axes[axis].value = value; /* Just so we pass the check above */
SDL_PrivateJoystickAxis(joystick, axis, initial_value);
SDL_PrivateJoystickAxis(joystick, axis, joystick->axes[axis].initial_value);
}

/* We ignore events if we don't have keyboard focus, except for centering
Expand Down
3 changes: 2 additions & 1 deletion src/joystick/SDL_sysjoystick.h
Expand Up @@ -31,7 +31,8 @@
/* The SDL joystick structure */
typedef struct _SDL_JoystickAxisInfo
{
Sint16 value; /* Current axis states */
Sint16 initial_value; /* Initial axis state */
Sint16 value; /* Current axis state */
Sint16 zero; /* Zero point on the axis (-32768 for triggers) */
SDL_bool has_initial_value; /* Whether we've seen a value on the axis yet */
SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */
Expand Down
8 changes: 8 additions & 0 deletions test/controllermap.c
Expand Up @@ -337,6 +337,7 @@ WatchJoystick(SDL_Joystick * joystick)
Uint8 alpha=200, alpha_step = -1;
Uint32 alpha_ticks = 0;
SDL_JoystickID nJoystickID;
int iIndex;

/* Create a window to display joystick axis position */
window = SDL_CreateWindow("Game Controller Map", SDL_WINDOWPOS_CENTERED,
Expand Down Expand Up @@ -383,6 +384,13 @@ WatchJoystick(SDL_Joystick * joystick)

s_nNumAxes = SDL_JoystickNumAxes(joystick);
s_arrAxisState = SDL_calloc(s_nNumAxes, sizeof(*s_arrAxisState));
for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
AxisState *pAxisState = &s_arrAxisState[iIndex];
Sint16 nInitialValue;
pAxisState->m_bMoving = SDL_JoystickGetAxisInitialState(joystick, iIndex, &nInitialValue);
pAxisState->m_nStartingValue = nInitialValue;
pAxisState->m_nFarthestValue = nInitialValue;
}

/* Loop, getting joystick events! */
while (!done && !s_bBindingComplete) {
Expand Down

0 comments on commit 4938c50

Please sign in to comment.