tree 97aca305664b

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
This commit is contained in:
Sam Lantinga 2013-02-11 11:21:54 -08:00
parent 290c391df5
commit b73214bd02

View file

@ -36,8 +36,8 @@
/* a list of currently opened game controllers */ /* a list of currently opened game controllers */
static SDL_GameController *SDL_gamecontrollers = NULL; static SDL_GameController *SDL_gamecontrollers = NULL;
/* keep track of the hat and mask value that transforms this hat movement into a button press */ /* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
struct _SDL_HatAsButton struct _SDL_HatMapping
{ {
int hat; int hat;
Uint8 mask; Uint8 mask;
@ -45,6 +45,10 @@ struct _SDL_HatAsButton
#define k_nMaxReverseEntries 20 #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*/ /* our in memory mapping db between joystick objects and controller mappings*/
struct _SDL_ControllerMapping struct _SDL_ControllerMapping
{ {
@ -57,7 +61,7 @@ struct _SDL_ControllerMapping
int buttons[SDL_CONTROLLER_BUTTON_MAX]; int buttons[SDL_CONTROLLER_BUTTON_MAX];
int axesasbutton[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 // reverse mapping, joystick indices to buttons
SDL_CONTROLLER_AXIS raxes[k_nMaxReverseEntries]; SDL_CONTROLLER_AXIS raxes[k_nMaxReverseEntries];
@ -65,8 +69,8 @@ struct _SDL_ControllerMapping
SDL_CONTROLLER_BUTTON rbuttons[k_nMaxReverseEntries]; SDL_CONTROLLER_BUTTON rbuttons[k_nMaxReverseEntries];
SDL_CONTROLLER_BUTTON raxesasbutton[k_nMaxReverseEntries]; SDL_CONTROLLER_BUTTON raxesasbutton[k_nMaxReverseEntries];
SDL_CONTROLLER_BUTTON rhatasbutton[k_nMaxHatEntries];
struct _SDL_HatAsButton rhatasbutton[k_nMaxReverseEntries];
}; };
@ -107,6 +111,7 @@ struct _SDL_GameController
{ {
SDL_Joystick *joystick; /* underlying joystick device */ SDL_Joystick *joystick; /* underlying joystick device */
int ref_count; 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_ControllerMapping mapping; /* the mapping object for this controller */
struct _SDL_GameController *next; /* pointer to next game controller we have allocated */ struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
}; };
@ -124,6 +129,8 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
{ {
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
{ {
if ( event->jaxis.axis >= k_nMaxReverseEntries ) break;
SDL_GameController *controllerlist = SDL_gamecontrollers; SDL_GameController *controllerlist = SDL_gamecontrollers;
while ( controllerlist ) while ( controllerlist )
{ {
@ -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 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; break;
} }
@ -146,6 +153,8 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP: case SDL_JOYBUTTONUP:
{ {
if ( event->jbutton.button >= k_nMaxReverseEntries ) break;
SDL_GameController *controllerlist = SDL_gamecontrollers; SDL_GameController *controllerlist = SDL_gamecontrollers;
while ( controllerlist ) while ( controllerlist )
{ {
@ -167,35 +176,46 @@ int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
break; break;
case SDL_JOYHATMOTION: 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; if ( controllerlist->joystick->instance_id == event->jhat.which )
while ( controllerlist )
{ {
if ( controllerlist->joystick->instance_id == event->jhat.which ) Uint8 bSame = controllerlist->hatState[event->jhat.hat] & event->jhat.value;
{ // Get list of removed bits (button release)
static Uint8 bHatsDown = 0; Uint8 bChanged = controllerlist->hatState[event->jhat.hat] ^ bSame;
if ( event->jhat.value == 0 ) // the hat idx in the high nibble
{ int bHighHat = event->jhat.hat << 4;
if ( bHatsDown & SDL_HAT_DOWN )
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_DOWN, 0 ); if ( bChanged & SDL_HAT_DOWN )
if ( bHatsDown & SDL_HAT_UP ) SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_RELEASED );
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_UP, 0 ); if ( bChanged & SDL_HAT_UP )
if ( bHatsDown & SDL_HAT_LEFT ) SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_RELEASED );
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_LEFT, 0 ); if ( bChanged & SDL_HAT_LEFT )
if ( bHatsDown & SDL_HAT_RIGHT ) SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_RELEASED );
SDL_PrivateGameControllerButton( controllerlist, SDL_CONTROLLER_BUTTON_DPAD_RIGHT, 0 ); if ( bChanged & SDL_HAT_RIGHT )
bHatsDown = 0; SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_RELEASED );
}
else if ( controllerlist->mapping.rhatasbutton[event->jhat.value].hat >= 0 ) // Get list of added bits (button press)
{ bChanged = event->jhat.value ^ bSame;
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 ); if ( bChanged & SDL_HAT_DOWN )
} SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_PRESSED );
break; if ( bChanged & SDL_HAT_UP )
} SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_PRESSED );
controllerlist = controllerlist->next; 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; break;
@ -342,14 +362,13 @@ void SDL_PrivateGameControllerParseButton( const char *szGameButton, const char
axis = SDL_GameControllerGetAxisFromString( szGameButton ); axis = SDL_GameControllerGetAxisFromString( szGameButton );
iSDLButton = SDL_atoi( &szJoystickButton[1] ); iSDLButton = SDL_atoi( &szJoystickButton[1] );
if ( iSDLButton >= k_nMaxReverseEntries )
{
SDL_SetError("Button index too large: %d", iSDLButton );
return;
}
if ( szJoystickButton[0] == 'a' ) if ( szJoystickButton[0] == 'a' )
{ {
if ( iSDLButton >= k_nMaxReverseEntries )
{
SDL_SetError("Axis index too large: %d", iSDLButton );
return;
}
if ( axis != SDL_CONTROLLER_AXIS_INVALID ) if ( axis != SDL_CONTROLLER_AXIS_INVALID )
{ {
pMapping->axes[ axis ] = iSDLButton; pMapping->axes[ axis ] = iSDLButton;
@ -368,6 +387,11 @@ void SDL_PrivateGameControllerParseButton( const char *szGameButton, const char
} }
else if ( szJoystickButton[0] == 'b' ) else if ( szJoystickButton[0] == 'b' )
{ {
if ( iSDLButton >= k_nMaxReverseEntries )
{
SDL_SetError("Button index too large: %d", iSDLButton );
return;
}
if ( button != SDL_CONTROLLER_BUTTON_INVALID ) if ( button != SDL_CONTROLLER_BUTTON_INVALID )
{ {
pMapping->buttons[ button ] = iSDLButton; pMapping->buttons[ button ] = iSDLButton;
@ -387,17 +411,20 @@ void SDL_PrivateGameControllerParseButton( const char *szGameButton, const char
{ {
int hat = SDL_atoi( &szJoystickButton[1] ); int hat = SDL_atoi( &szJoystickButton[1] );
int mask = SDL_atoi( &szJoystickButton[3] ); int mask = SDL_atoi( &szJoystickButton[3] );
if (hat >= 4) {
SDL_SetError("Hat index too large: %d", iSDLButton );
}
if ( button != SDL_CONTROLLER_BUTTON_INVALID ) if ( button != SDL_CONTROLLER_BUTTON_INVALID )
{ {
pMapping->hatasbutton[ button ].hat = hat; pMapping->hatasbutton[ button ].hat = hat;
pMapping->hatasbutton[ button ].mask = mask; pMapping->hatasbutton[ button ].mask = mask;
pMapping->rhatasbutton[ mask ].hat = button; int ridx = (hat << 4) | mask;
pMapping->rhatasbutton[ mask ].mask = mask; pMapping->rhatasbutton[ ridx ] = button;
} }
else if ( axis != SDL_CONTROLLER_AXIS_INVALID ) else if ( axis != SDL_CONTROLLER_AXIS_INVALID )
{ {
SDL_assert( !"Support hat as axis" ); SDL_assert( !"Support has as axis" );
} }
else else
{ {
@ -499,7 +526,11 @@ void SDL_PrivateLoadButtonMapping( struct _SDL_ControllerMapping *pMapping, SDL_
pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID; pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID;
pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID; pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID;
pMapping->raxesasbutton[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 ); SDL_PrivateGameControllerParseControllerConfigString( pMapping, pchMapping );
@ -1034,6 +1065,8 @@ SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_CONTROLLE
int int
SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_CONTROLLER_BUTTON button, Uint8 state) SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_CONTROLLER_BUTTON button, Uint8 state)
{ {
if ( button == SDL_CONTROLLER_BUTTON_INVALID ) return;
int posted; int posted;
#if !SDL_EVENTS_DISABLED #if !SDL_EVENTS_DISABLED
SDL_Event event; SDL_Event event;