Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
tree 97aca305664b
Browse files Browse the repository at this point in the history
parent 6a9ca692cd65
author Edward Rudd <urkle@outoforder.cc> 1358030048 18000
committer Edward Rudd <urkle@outoforder.cc> 1358030048 18000
revision 6822
branch default

rework and fix handling of hat to button mappings

- doesn't assume it's always mapped to dpad
- properly handles multiple hats (up to 4)
- properly handles multiple presses (e.g. up and right)
- properly handles multiple gamepads
  • Loading branch information
slouken committed Feb 11, 2013
1 parent 0014bb1 commit 7a31eb6
Showing 1 changed file with 74 additions and 41 deletions.
115 changes: 74 additions & 41 deletions src/joystick/SDL_gamecontroller.c
Expand Up @@ -36,15 +36,19 @@
/* a list of currently opened game controllers */
static SDL_GameController *SDL_gamecontrollers = NULL;

/* keep track of the hat and mask value that transforms this hat movement into a button press */
struct _SDL_HatAsButton
/* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
struct _SDL_HatMapping
{
int hat;
Uint8 mask;
};

#define k_nMaxReverseEntries 20

// We are encoding the "HAT" as 0xhm. where h == hat ID and m == mask
// MAX 4 hats supported
#define k_nMaxHatEntries 0x3f + 1

/* our in memory mapping db between joystick objects and controller mappings*/
struct _SDL_ControllerMapping
{
Expand All @@ -57,16 +61,16 @@ struct _SDL_ControllerMapping

int buttons[SDL_CONTROLLER_BUTTON_MAX];
int axesasbutton[SDL_CONTROLLER_BUTTON_MAX];
struct _SDL_HatAsButton hatasbutton[SDL_CONTROLLER_BUTTON_MAX];
struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX];

// reverse mapping, joystick indices to buttons
SDL_CONTROLLER_AXIS raxes[k_nMaxReverseEntries];
SDL_CONTROLLER_AXIS rbuttonasaxis[k_nMaxReverseEntries];

SDL_CONTROLLER_BUTTON rbuttons[k_nMaxReverseEntries];
SDL_CONTROLLER_BUTTON raxesasbutton[k_nMaxReverseEntries];
SDL_CONTROLLER_BUTTON rhatasbutton[k_nMaxHatEntries];

struct _SDL_HatAsButton rhatasbutton[k_nMaxReverseEntries];
};


Expand Down Expand Up @@ -107,6 +111,7 @@ struct _SDL_GameController
{
SDL_Joystick *joystick; /* underlying joystick device */
int ref_count;
Uint8 hatState[4]; /* the current hat state for this controller */
struct _SDL_ControllerMapping mapping; /* the mapping object for this controller */
struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
};
Expand All @@ -124,6 +129,8 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
{
case SDL_JOYAXISMOTION:
{
if ( event->jaxis.axis >= k_nMaxReverseEntries ) break;

SDL_GameController *controllerlist = SDL_gamecontrollers;
while ( controllerlist )
{
Expand All @@ -135,7 +142,7 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
}
else if ( controllerlist->mapping.raxesasbutton[event->jaxis.axis] >= 0 ) // simlate an axis as a button
{
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.raxesasbutton[event->jaxis.axis], ABS(event->jaxis.value) > 32768/2 ? 1 : 0 );
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.raxesasbutton[event->jaxis.axis], ABS(event->jaxis.value) > 32768/2 ? SDL_PRESSED : SDL_RELEASED );
}
break;
}
Expand All @@ -146,6 +153,8 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
{
if ( event->jbutton.button >= k_nMaxReverseEntries ) break;

SDL_GameController *controllerlist = SDL_gamecontrollers;
while ( controllerlist )
{
Expand All @@ -167,35 +176,46 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
break;
case SDL_JOYHATMOTION:
{
if ( event->jhat.hat == 0 ) // BUGBUG - multiple hat support??
if ( event->jhat.hat >= 4 ) break;

SDL_GameController *controllerlist = SDL_gamecontrollers;
while ( controllerlist )
{
SDL_GameController *controllerlist = SDL_gamecontrollers;
while ( controllerlist )
if ( controllerlist->joystick->instance_id == event->jhat.which )
{
if ( controllerlist->joystick->instance_id == event->jhat.which )
{
static Uint8 bHatsDown = 0;
if ( event->jhat.value == 0 )
{
if ( bHatsDown & SDL_HAT_DOWN )
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_DOWN, 0 );
if ( bHatsDown & SDL_HAT_UP )
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_UP, 0 );
if ( bHatsDown & SDL_HAT_LEFT )
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_LEFT, 0 );
if ( bHatsDown & SDL_HAT_RIGHT )
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, 0 );
bHatsDown = 0;
}
else if ( controllerlist->mapping.rhatasbutton[event->jhat.value].hat >= 0 )
{
bHatsDown |= event->jhat.value;
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[event->jhat.value].hat, (event->jhat.value & controllerlist->mapping.rhatasbutton[event->jhat.value].mask) > 0 ? 1 : 0 );
}
break;
}
controllerlist = controllerlist->next;
Uint8 bSame = controllerlist->hatState[event->jhat.hat] & event->jhat.value;
// Get list of removed bits (button release)
Uint8 bChanged = controllerlist->hatState[event->jhat.hat] ^ bSame;
// the hat idx in the high nibble
int bHighHat = event->jhat.hat << 4;

if ( bChanged & SDL_HAT_DOWN )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_RELEASED );
if ( bChanged & SDL_HAT_UP )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_RELEASED );
if ( bChanged & SDL_HAT_LEFT )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_RELEASED );
if ( bChanged & SDL_HAT_RIGHT )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_RELEASED );

// Get list of added bits (button press)
bChanged = event->jhat.value ^ bSame;

if ( bChanged & SDL_HAT_DOWN )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_PRESSED );
if ( bChanged & SDL_HAT_UP )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_PRESSED );
if ( bChanged & SDL_HAT_LEFT )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_PRESSED );
if ( bChanged & SDL_HAT_RIGHT )
SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_PRESSED );

// update our state cache
controllerlist->hatState[event->jhat.hat] = event->jhat.value;

break;
}
controllerlist = controllerlist->next;
}
}
break;
Expand Down Expand Up @@ -342,14 +362,13 @@ void SDL_PrivateGameControllerParseButton( const char *szGameButton, const char
axis = SDL_GameControllerGetAxisFromString( szGameButton );
iSDLButton = SDL_atoi( &szJoystickButton[1] );

if ( iSDLButton >= k_nMaxReverseEntries )
{
SDL_SetError("Button index too large: %d", iSDLButton );
return;
}

if ( szJoystickButton[0] == 'a' )
{
if ( iSDLButton >= k_nMaxReverseEntries )
{
SDL_SetError("Axis index too large: %d", iSDLButton );
return;
}
if ( axis != SDL_CONTROLLER_AXIS_INVALID )
{
pMapping->axes[ axis ] = iSDLButton;
Expand All @@ -368,6 +387,11 @@ void SDL_PrivateGameControllerParseButton( const char *szGameButton, const char
}
else if ( szJoystickButton[0] == 'b' )
{
if ( iSDLButton >= k_nMaxReverseEntries )
{
SDL_SetError("Button index too large: %d", iSDLButton );
return;
}
if ( button != SDL_CONTROLLER_BUTTON_INVALID )
{
pMapping->buttons[ button ] = iSDLButton;
Expand All @@ -387,17 +411,20 @@ void SDL_PrivateGameControllerParseButton( const char *szGameButton, const char
{
int hat = SDL_atoi( &szJoystickButton[1] );
int mask = SDL_atoi( &szJoystickButton[3] );
if (hat >= 4) {
SDL_SetError("Hat index too large: %d", iSDLButton );
}

if ( button != SDL_CONTROLLER_BUTTON_INVALID )
{
pMapping->hatasbutton[ button ].hat = hat;
pMapping->hatasbutton[ button ].mask = mask;
pMapping->rhatasbutton[ mask ].hat = button;
pMapping->rhatasbutton[ mask ].mask = mask;
int ridx = (hat << 4) | mask;
pMapping->rhatasbutton[ ridx ] = button;
}
else if ( axis != SDL_CONTROLLER_AXIS_INVALID )
{
SDL_assert( !"Support hat as axis" );
SDL_assert( !"Support has as axis" );
}
else
{
Expand Down Expand Up @@ -499,7 +526,11 @@ void SDL_PrivateLoadButtonMapping( struct _SDL_ControllerMapping *pMapping, SDL_
pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID;
pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID;
pMapping->raxesasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
pMapping->rhatasbutton[j].hat = -1;
}

for (j = 0; j < k_nMaxHatEntries; j++)
{
pMapping->rhatasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
}

SDL_PrivateGameControllerParseControllerConfigString( pMapping, pchMapping );
Expand Down Expand Up @@ -1034,6 +1065,8 @@ SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_CONTROLLE
int
SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_CONTROLLER_BUTTON button, Uint8 state)
{
if ( button == SDL_CONTROLLER_BUTTON_INVALID ) return;

int posted;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
Expand Down

0 comments on commit 7a31eb6

Please sign in to comment.