From c6a61d212a0bb7b56e53ea3802d2244309577c05 Mon Sep 17 00:00:00 2001 From: Zack Middleton Date: Fri, 18 Oct 2013 16:31:19 -0500 Subject: [PATCH] Fix SDL2 losing event subsystem Quiting SDL Video or Joystick subsystem implies quiting the Event subsystem in SDL2. SDL keeps track of number of init and shutdown calls for each subsystem. Shuting down video or joystick more or equal to number of times they're inited will lead to event shutdown. Toggling in and out of fullscreen or running in_restart twice causes SDL event subsystem to shutdown, making input not work. If the console is closed, IN_GobbleMotionEvents gets stuck in a loop. SDL_PeepEvents returns -1 when there is an error, but we assume non-0 means read more events. IN_ShutdownJoystick needs to check if joystick subsystem was inited before quitting it, otherwise we may cause SDL event subsystem to shutdown. --- code/sdl/sdl_input.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/code/sdl/sdl_input.c b/code/sdl/sdl_input.c index 814550c1..dcd21439 100644 --- a/code/sdl/sdl_input.c +++ b/code/sdl/sdl_input.c @@ -297,11 +297,15 @@ IN_GobbleMotionEvents static void IN_GobbleMotionEvents( void ) { SDL_Event dummy[ 1 ]; + int val = 0; // Gobble any mouse motion events SDL_PumpEvents( ); - while( SDL_PeepEvents( dummy, 1, SDL_GETEVENT, - SDL_MOUSEMOTION, SDL_MOUSEMOTION ) ) { } + while( ( val = SDL_PeepEvents( dummy, 1, SDL_GETEVENT, + SDL_MOUSEMOTION, SDL_MOUSEMOTION ) ) > 0 ) { } + + if ( val < 0 ) + Com_Printf( "IN_GobbleMotionEvents failed: %s\n", SDL_GetError( ) ); } /* @@ -484,6 +488,9 @@ IN_ShutdownJoystick */ static void IN_ShutdownJoystick( void ) { + if ( !SDL_WasInit( SDL_INIT_JOYSTICK ) ) + return; + if (stick) { SDL_JoystickClose(stick);