From 5a36a26dd116bdbddbf8b011f59597eb77e6d4cd Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Mon, 28 Dec 2020 09:46:27 +0000 Subject: [PATCH] Fix SDL_GAMECONTROLLER handling 1. Do not disable SDL_JOYSTICK events. Disabling these events causes game controller events to be disabled as well. Instead, filter these events out. 2. Fix button mapping (it was off by one). --- engine/platform/sdl/events.c | 14 +++++++++----- engine/platform/sdl/in_sdl.c | 1 - 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/engine/platform/sdl/events.c b/engine/platform/sdl/events.c index fa3f67ac..4607a740 100644 --- a/engine/platform/sdl/events.c +++ b/engine/platform/sdl/events.c @@ -390,20 +390,24 @@ static void SDLash_EventFilter( SDL_Event *event ) /* Joystick events */ case SDL_JOYAXISMOTION: - Joy_AxisMotionEvent( event->jaxis.axis, event->jaxis.value ); + if ( SDL_GameControllerFromInstanceID( event->jaxis.which ) == NULL ) + Joy_AxisMotionEvent( event->jaxis.axis, event->jaxis.value ); break; case SDL_JOYBALLMOTION: - Joy_BallMotionEvent( event->jball.ball, event->jball.xrel, event->jball.yrel ); + if ( SDL_GameControllerFromInstanceID( event->jball.which ) == NULL ) + Joy_BallMotionEvent( event->jball.ball, event->jball.xrel, event->jball.yrel ); break; case SDL_JOYHATMOTION: - Joy_HatMotionEvent( event->jhat.hat, event->jhat.value ); + if ( SDL_GameControllerFromInstanceID( event->jhat.which ) == NULL ) + Joy_HatMotionEvent( event->jhat.hat, event->jhat.value ); break; case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: - Joy_ButtonEvent( event->jbutton.button, event->jbutton.state ); + if ( SDL_GameControllerFromInstanceID( event->jbutton.which ) == NULL ) + Joy_ButtonEvent( event->jbutton.button, event->jbutton.state ); break; case SDL_QUIT: @@ -514,7 +518,7 @@ static void SDLash_EventFilter( SDL_Event *event ) // TODO: Use joyinput funcs, for future multiple gamepads support if( Joy_IsActive() ) - Key_Event( sdlControllerButtonToEngine[event->cbutton.button], event->cbutton.state ); + Key_Event( sdlControllerButtonToEngine[event->cbutton.button + 1], event->cbutton.state ); break; } diff --git a/engine/platform/sdl/in_sdl.c b/engine/platform/sdl/in_sdl.c index fb6cc98e..fa5eee48 100644 --- a/engine/platform/sdl/in_sdl.c +++ b/engine/platform/sdl/in_sdl.c @@ -260,7 +260,6 @@ static int SDLash_JoyInit_New( int numjoy ) SDL_GameControllerGetProductVersion( gamecontroller )); #endif // SDL_VERSION_ATLEAST( 2, 0, 6 ) SDL_GameControllerEventState( SDL_ENABLE ); - SDL_JoystickEventState( SDL_DISABLE ); return num; }