31 #if !SDL_EVENTS_DISABLED
32 #include "../events/SDL_events_c.h"
34 #define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
41 struct _SDL_HatMapping
47 #define k_nMaxReverseEntries 20
53 #define k_nMaxHatEntries 0x3f + 1
56 struct _SDL_ControllerMapping
67 struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX];
81 typedef struct _ControllerMapping_t
86 struct _ControllerMapping_t *next;
90 #ifdef SDL_JOYSTICK_DINPUT
95 struct _SDL_GameController
100 struct _SDL_ControllerMapping mapping;
101 struct _SDL_GameController *next;
113 switch( event->
type )
122 while ( controllerlist )
124 if ( controllerlist->joystick->instance_id == event->
jaxis.
which )
126 if ( controllerlist->mapping.raxes[event->
jaxis.
axis] >= 0 )
135 value = value / 2 + 16384;
141 else if ( controllerlist->mapping.raxesasbutton[event->
jaxis.
axis] >= 0 )
147 controllerlist = controllerlist->next;
159 while ( controllerlist )
161 if ( controllerlist->joystick->instance_id == event->
jbutton.
which )
163 if ( controllerlist->mapping.rbuttons[event->
jbutton.
button] >= 0 )
167 else if ( controllerlist->mapping.rbuttonasaxis[event->
jbutton.
button] >= 0 )
173 controllerlist = controllerlist->next;
181 if ( event->
jhat.
hat >= 4 )
break;
184 while ( controllerlist )
186 if ( controllerlist->joystick->instance_id == event->
jhat.
which )
188 Uint8 bSame = controllerlist->hatState[
event->jhat.hat] &
event->jhat.value;
190 Uint8 bChanged = controllerlist->hatState[
event->jhat.hat] ^ bSame;
192 int bHighHat =
event->jhat.hat << 4;
204 bChanged =
event->jhat.value ^ bSame;
206 if ( bChanged & SDL_HAT_DOWN )
208 if ( bChanged & SDL_HAT_UP )
210 if ( bChanged & SDL_HAT_LEFT )
212 if ( bChanged & SDL_HAT_RIGHT )
216 controllerlist->hatState[
event->jhat.hat] =
event->jhat.value;
220 controllerlist = controllerlist->next;
238 while ( controllerlist )
240 if ( controllerlist->joystick->instance_id == event->
jdevice.
which )
248 controllerlist = controllerlist->next;
265 while ( pSupportedController )
267 if ( !
SDL_memcmp( guid, &pSupportedController->guid,
sizeof(*guid) ) )
269 return pSupportedController;
271 pSupportedController = pSupportedController->next;
281 #ifdef SDL_JOYSTICK_DINPUT
282 if ( SDL_SYS_IsXInputDeviceIndex(device_index) && s_pXInputMapping )
284 return s_pXInputMapping;
312 if ( !pchString || !pchString[0] )
360 if ( !pchString || !pchString[0] )
393 iSDLButton =
SDL_atoi( &szJoystickButton[1] );
395 if ( szJoystickButton[0] ==
'a' )
404 pMapping->axes[ axis ] = iSDLButton;
405 pMapping->raxes[ iSDLButton ] = axis;
409 pMapping->axesasbutton[ button ] = iSDLButton;
410 pMapping->raxesasbutton[ iSDLButton ] = button;
418 else if ( szJoystickButton[0] ==
'b' )
422 SDL_SetError(
"Button index too large: %d", iSDLButton );
427 pMapping->buttons[ button ] = iSDLButton;
428 pMapping->rbuttons[ iSDLButton ] = button;
432 pMapping->buttonasaxis[ axis ] = iSDLButton;
433 pMapping->rbuttonasaxis[ iSDLButton ] = axis;
440 else if ( szJoystickButton[0] ==
'h' )
442 int hat =
SDL_atoi( &szJoystickButton[1] );
451 pMapping->hatasbutton[ button ].hat = hat;
452 pMapping->hatasbutton[ button ].mask =
mask;
453 ridx = (hat << 4) | mask;
454 pMapping->rhatasbutton[ ridx ] = button;
474 char szGameButton[20];
475 char szJoystickButton[20];
478 const char *pchPos = pchString;
481 SDL_memset( szJoystickButton, 0
x0,
sizeof(szJoystickButton) );
483 while ( pchPos && *pchPos )
485 if ( *pchPos ==
':' )
490 else if ( *pchPos ==
' ' )
494 else if ( *pchPos ==
',' )
500 SDL_memset( szJoystickButton, 0
x0,
sizeof(szJoystickButton) );
503 else if ( bGameButton )
505 if ( i >=
sizeof(szGameButton))
507 SDL_SetError(
"Button name too large: %s", szGameButton );
510 szGameButton[
i] = *pchPos;
515 if ( i >=
sizeof(szJoystickButton))
517 SDL_SetError(
"Joystick button name too large: %s", szJoystickButton );
520 szJoystickButton[
i] = *pchPos;
537 pMapping->guid = guid;
538 pMapping->name = pchName;
543 pMapping->axes[
j] = -1;
544 pMapping->buttonasaxis[
j] = -1;
548 pMapping->buttons[
j] = -1;
549 pMapping->axesasbutton[
j] = -1;
550 pMapping->hatasbutton[
j].hat = -1;
575 const char *pFirstComma =
SDL_strchr( pMapping,
',' );
578 char *pchGUID =
SDL_malloc( pFirstComma - pMapping + 1 );
584 SDL_memcpy( pchGUID, pMapping, pFirstComma - pMapping );
585 pchGUID[ pFirstComma - pMapping ] = 0;
597 const char *pFirstComma, *pSecondComma;
604 pSecondComma =
SDL_strchr( pFirstComma + 1,
',' );
608 pchName =
SDL_malloc( pSecondComma - pFirstComma );
614 SDL_memcpy( pchName, pFirstComma + 1, pSecondComma - pFirstComma );
615 pchName[ pSecondComma - pFirstComma - 1 ] = 0;
625 const char *pFirstComma, *pSecondComma;
631 pSecondComma =
SDL_strchr( pFirstComma + 1,
',' );
641 while ( gamecontrollerlist )
643 if ( !
SDL_memcmp( &gamecontrollerlist->mapping.guid, &pControllerMapping->guid,
sizeof(pControllerMapping->guid) ) )
647 event.cdevice.which = gamecontrollerlist->joystick->instance_id;
651 SDL_PrivateLoadButtonMapping(&gamecontrollerlist->mapping, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
654 gamecontrollerlist = gamecontrollerlist->next;
669 #ifdef SDL_JOYSTICK_DINPUT
677 #ifdef SDL_JOYSTICK_DINPUT
688 if (!pchName)
return -1;
696 if (pControllerMapping) {
698 SDL_free( pControllerMapping->name );
699 pControllerMapping->name = pchName;
700 SDL_free( pControllerMapping->mapping );
701 pControllerMapping->mapping = pchMapping;
706 pControllerMapping =
SDL_malloc(
sizeof(*pControllerMapping) );
707 if (!pControllerMapping) {
712 #ifdef SDL_JOYSTICK_DINPUT
713 if ( is_xinput_mapping )
715 s_pXInputMapping = pControllerMapping;
718 pControllerMapping->guid = jGUID;
719 pControllerMapping->name = pchName;
720 pControllerMapping->mapping = pchMapping;
733 char *pMappingString =
NULL;
742 SDL_snprintf( pMappingString, needed,
"%s,%s,%s", pchGUID, mapping->name, mapping->mapping );
744 return pMappingString;
760 if ( hint && hint[0] ) {
762 char *pUserMappings =
SDL_malloc( nchHints + 1 );
763 char *pTempMappings = pUserMappings;
765 while ( pUserMappings ) {
766 char *pchNewLine =
NULL;
768 pchNewLine =
SDL_strchr( pUserMappings,
'\n' );
775 pUserMappings = pchNewLine + 1;
777 pUserMappings =
NULL;
790 const char *pMappingString =
NULL;
793 while ( pMappingString )
818 if ( pSupportedController )
820 return pSupportedController->name;
833 if ( pSupportedController )
862 while ( gamecontrollerlist )
865 gamecontroller = gamecontrollerlist;
866 ++gamecontroller->ref_count;
867 return (gamecontroller);
869 gamecontrollerlist = gamecontrollerlist->next;
874 if ( !pSupportedController ) {
875 SDL_SetError(
"Couldn't find mapping for device (%d)", device_index );
881 if (gamecontroller ==
NULL) {
886 SDL_memset(gamecontroller, 0, (
sizeof *gamecontroller));
888 if ( !gamecontroller->joystick ) {
893 SDL_PrivateLoadButtonMapping( &gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping );
896 ++gamecontroller->ref_count;
903 return (gamecontroller);
923 if ( !gamecontroller )
926 if (gamecontroller->mapping.axes[axis] >= 0 )
934 value = value / 2 + 16384;
940 else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
943 value =
SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis] );
958 if ( !gamecontroller )
961 if ( gamecontroller->mapping.buttons[button] >= 0 )
963 return (
SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttons[button] ) );
965 else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
968 value =
SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button] );
969 if (
ABS(value) > 32768/2 )
973 else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
976 value =
SDL_JoystickGetHat( gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat );
978 if ( value & gamecontroller->mapping.hatasbutton[button].mask )
993 if ( !gamecontroller )
1006 if ( !gamecontroller )
1009 return (gamecontroller->mapping.name);
1018 if ( !gamecontroller )
1021 return gamecontroller->joystick;
1035 if (gamecontroller->mapping.axes[axis] >= 0 )
1038 bind.
value.
button = gamecontroller->mapping.axes[axis];
1040 else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
1043 bind.
value.
button = gamecontroller->mapping.buttonasaxis[axis];
1061 if ( gamecontroller->mapping.buttons[button] >= 0 )
1064 bind.
value.
button = gamecontroller->mapping.buttons[button];
1066 else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
1069 bind.
value.
axis = gamecontroller->mapping.axesasbutton[button];
1071 else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
1074 bind.
value.
hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
1075 bind.
value.
hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
1090 if ( !gamecontroller )
1094 if (--gamecontroller->ref_count > 0) {
1101 gamecontrollerlistprev =
NULL;
1102 while ( gamecontrollerlist )
1104 if (gamecontroller == gamecontrollerlist)
1106 if ( gamecontrollerlistprev )
1109 gamecontrollerlistprev->next = gamecontrollerlist->next;
1118 gamecontrollerlistprev = gamecontrollerlist;
1119 gamecontrollerlist = gamecontrollerlist->next;
1161 #if !SDL_EVENTS_DISABLED
1165 event.caxis.which = gamecontroller->joystick->instance_id;
1166 event.caxis.axis = axis;
1167 event.caxis.value =
value;
1182 #if !SDL_EVENTS_DISABLED
1203 #if !SDL_EVENTS_DISABLED
1205 event.cbutton.which = gamecontroller->joystick->instance_id;
1206 event.cbutton.button = button;
1207 event.cbutton.state = state;
1220 #if SDL_EVENTS_DISABLED
1223 const Uint32 event_list[] = {
static const char * map_StringForControllerAxis[]
char * SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
DECLSPEC void SDLCALL SDL_GameControllerClose(SDL_GameController *gamecontroller)
DECLSPEC void SDLCALL SDL_JoystickUpdate(void)
DECLSPEC int SDLCALL SDL_PushEvent(SDL_Event *event)
Add an event to the event queue.
SDL_JoyDeviceEvent jdevice
void SDL_PrivateGameControllerParseButton(const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping)
DECLSPEC void SDLCALL SDL_GameControllerUpdate(void)
DECLSPEC SDL_GameControllerAxis SDLCALL SDL_GameControllerGetAxisFromString(const char *pchString)
struct _ControllerMapping_t ControllerMapping_t
SDL_ControllerDeviceEvent cdevice
struct _SDL_GameController SDL_GameController
DECLSPEC int SDLCALL SDL_snprintf(char *text, size_t maxlen, const char *fmt,...)
SDL_JoyButtonEvent jbutton
DECLSPEC SDL_bool SDLCALL SDL_JoystickGetAttached(SDL_Joystick *joystick)
DECLSPEC void SDLCALL SDL_JoystickClose(SDL_Joystick *joystick)
DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index)
ControllerMapping_t * SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
DECLSPEC void SDLCALL SDL_free(void *mem)
DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetDeviceGUID(int device_index)
DECLSPEC SDL_JoystickGUID SDLCALL SDL_JoystickGetGUIDFromString(const char *pchGUID)
EGLImageKHR EGLint * name
DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index)
DECLSPEC Uint8 SDLCALL SDL_EventState(Uint32 type, int state)
DECLSPEC int SDLCALL SDL_GameControllerEventState(int state)
int SDL_PrivateGameControllerButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button, Uint8 state)
DECLSPEC int SDLCALL SDL_memcmp(const void *s1, const void *s2, size_t len)
DECLSPEC Sint16 SDLCALL SDL_JoystickGetAxis(SDL_Joystick *joystick, int axis)
static void SDL_GameControllerLoadHints()
DECLSPEC char *SDLCALL SDL_strchr(const char *str, int c)
DECLSPEC SDL_GameControllerButtonBind SDLCALL SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
DECLSPEC Uint8 SDLCALL SDL_JoystickGetButton(SDL_Joystick *joystick, int button)
DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index)
DECLSPEC const char *SDLCALL SDL_GameControllerGetStringForButton(SDL_GameControllerButton button)
uint32_t Uint32
An unsigned 32-bit integer type.
int SDL_PrivateGameControllerAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
DECLSPEC SDL_GameControllerButton SDLCALL SDL_GameControllerGetButtonFromString(const char *pchString)
DECLSPEC const char *SDLCALL SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
#define SDL_GetEventState(type)
static SDL_GameController * SDL_gamecontrollers
static ControllerMapping_t * s_pSupportedControllers
void SDL_GameControllerQuit(void)
DECLSPEC char *SDLCALL SDL_strdup(const char *str)
void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
void SDL_PrivateLoadButtonMapping(struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
DECLSPEC void *SDLCALL SDL_memset(void *dst, int c, size_t len)
DECLSPEC Sint16 SDLCALL SDL_GameControllerGetAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
ControllerMapping_t * SDL_PrivateGetControllerMapping(int device_index)
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
DECLSPEC void *SDLCALL SDL_malloc(size_t size)
DECLSPEC SDL_GameControllerButtonBind SDLCALL SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
static const char * map_StringForControllerButton[]
DECLSPEC const char *SDLCALL SDL_GetHint(const char *name)
Get a hint.
DECLSPEC char *SDLCALL SDL_GameControllerMapping(SDL_GameController *gamecontroller)
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
DECLSPEC void SDLCALL SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
#define SDL_assert(condition)
DECLSPEC size_t SDLCALL SDL_strlen(const char *str)
DECLSPEC void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
#define SDL_OutOfMemory()
#define SDL_arraysize(array)
static const char * s_ControllerMappings[]
EGLSurface EGLint void ** value
DECLSPEC void SDLCALL SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
char * SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
DECLSPEC void *SDLCALL SDL_memcpy(void *dst, const void *src, size_t len)
uint8_t Uint8
An unsigned 8-bit integer type.
DECLSPEC int SDLCALL SDL_atoi(const char *str)
DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController *gamecontroller)
DECLSPEC char *SDLCALL SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
int16_t Sint16
A signed 16-bit integer type.
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickOpen(int device_index)
GLenum GLenum GLenum GLenum mapping
DECLSPEC int SDLCALL SDL_GameControllerAddMapping(const char *mappingString)
#define SDL_HINT_GAMECONTROLLERCONFIG
A variable that lets you manually hint extra gamecontroller db entries.
int SDL_GameControllerInit(void)
#define k_nMaxReverseEntries
DECLSPEC SDL_Joystick *SDLCALL SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller)
DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button)
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
DECLSPEC int SDLCALL SDL_strcasecmp(const char *str1, const char *str2)
static void SDL_PrivateGameControllerParseControllerConfigString(struct _SDL_ControllerMapping *pMapping, const char *pchString)
char * SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
DECLSPEC Uint8 SDLCALL SDL_JoystickGetHat(SDL_Joystick *joystick, int hat)
DECLSPEC int SDLCALL SDL_NumJoysticks(void)
DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller)