Browse Source

SDL GameController: Hotplug and multiple gamepads

1. Process events from all game controllers.
2. Support controller hotplug.
pull/2/head
Gleb Mazovetskiy 4 years ago committed by Alibek Omarov
parent
commit
9452a389a5
  1. 46
      engine/platform/sdl/events.c
  2. 61
      engine/platform/sdl/in_sdl.c

46
engine/platform/sdl/events.c

@ -356,6 +356,48 @@ static void SDLash_ActiveEvent( int gain )
} }
} }
#if SDL_VERSION_ATLEAST( 2, 0, 0 )
static size_t num_open_game_controllers = 0;
static void SDLash_GameController_Add( int index )
{
extern convar_t *joy_enable; // private to input system
SDL_GameController *controller;
if( !joy_enable->value )
return;
controller = SDL_GameControllerOpen( index );
if( !controller )
{
Con_Reportf( "Failed to open SDL GameController %d: %s\n", index, SDL_GetError( ) );
SDL_ClearError( );
return;
}
#if SDL_VERSION_ATLEAST( 2, 0, 6 )
Con_Reportf( "Added controller: %s (%i:%i:%i)\n",
SDL_GameControllerName( controller ),
SDL_GameControllerGetVendor( controller ),
SDL_GameControllerGetProduct( controller ),
SDL_GameControllerGetProductVersion( controller ));
#endif // SDL_VERSION_ATLEAST( 2, 0, 6 )
++num_open_game_controllers;
if( num_open_game_controllers == 1 )
Joy_AddEvent( );
}
static void SDLash_GameController_Remove( SDL_JoystickID joystick_id )
{
Con_Reportf( "Removed controller %i\n", joystick_id );
// `Joy_RemoveEvent` sets `joy_found` to `0`.
// We only want to do this when all the game controllers have been removed.
--num_open_game_controllers;
if( num_open_game_controllers == 0 )
Joy_RemoveEvent( );
}
#endif
/* /*
============= =============
SDLash_EventFilter SDLash_EventFilter
@ -523,11 +565,11 @@ static void SDLash_EventFilter( SDL_Event *event )
} }
case SDL_CONTROLLERDEVICEADDED: case SDL_CONTROLLERDEVICEADDED:
Joy_AddEvent( ); SDLash_GameController_Add( event->cdevice.which );
break; break;
case SDL_CONTROLLERDEVICEREMOVED: case SDL_CONTROLLERDEVICEREMOVED:
Joy_RemoveEvent( ); SDLash_GameController_Remove( event->cdevice.which );
break; break;
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:

61
engine/platform/sdl/in_sdl.c

@ -25,12 +25,8 @@ GNU General Public License for more details.
#include "vid_common.h" #include "vid_common.h"
static SDL_Joystick *joy; static SDL_Joystick *joy;
#if SDL_VERSION_ATLEAST( 2, 0, 0 ) #if !SDL_VERSION_ATLEAST( 2, 0, 0 )
static SDL_GameController *gamecontroller;
#else // SDL_VERSION_ATLEAST( 2, 0, 0 )
#define SDL_WarpMouseInWindow( win, x, y ) SDL_WarpMouse( ( x ), ( y ) ) #define SDL_WarpMouseInWindow( win, x, y ) SDL_WarpMouse( ( x ), ( y ) )
#endif #endif
/* /*
@ -202,11 +198,9 @@ SDLash_JoyInit_New
*/ */
static int SDLash_JoyInit_New( int numjoy ) static int SDLash_JoyInit_New( int numjoy )
{ {
int temp, num; int count, numJoysticks, i;
int i;
Con_Reportf( "Joystick: SDL GameController API\n" ); Con_Reportf( "Joystick: SDL GameController API\n" );
if( SDL_WasInit( SDL_INIT_GAMECONTROLLER ) != SDL_INIT_GAMECONTROLLER && if( SDL_WasInit( SDL_INIT_GAMECONTROLLER ) != SDL_INIT_GAMECONTROLLER &&
SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER ) ) SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER ) )
{ {
@ -214,54 +208,15 @@ static int SDLash_JoyInit_New( int numjoy )
return 0; return 0;
} }
// chance to add mappings from file
SDL_GameControllerAddMappingsFromFile( "controllermappings.txt" ); SDL_GameControllerAddMappingsFromFile( "controllermappings.txt" );
if( gamecontroller ) count = 0;
{ numJoysticks = SDL_NumJoysticks();
SDL_GameControllerClose( gamecontroller ); for ( i = 0; i < numJoysticks; i++ )
} if( SDL_IsGameController( i ) )
++count;
temp = SDL_NumJoysticks();
num = 0;
for( i = 0; i < temp; i++ )
{
if( SDL_IsGameController( i ))
num++;
}
if( num > 0 ) return count;
Con_Reportf( "%i joysticks found:\n", num );
else
{
Con_Reportf( "No joystick found.\n" );
return 0;
}
for( i = 0; i < num; i++ )
Con_Reportf( "%i\t: %s\n", i, SDL_GameControllerNameForIndex( i ) );
Con_Reportf( "Pass +set joy_index N to command line, where N is number, to select active joystick\n" );
gamecontroller = SDL_GameControllerOpen( numjoy );
if( !gamecontroller )
{
Con_Reportf( "Failed to select joystick: %s\n", SDL_GetError( ) );
return 0;
}
// was added in SDL2-2.0.6, allow build with earlier versions just in case
#if SDL_VERSION_ATLEAST( 2, 0, 6 )
Con_Reportf( "Selected joystick: %s (%i:%i:%i)\n",
SDL_GameControllerName( gamecontroller ),
SDL_GameControllerGetVendor( gamecontroller ),
SDL_GameControllerGetProduct( gamecontroller ),
SDL_GameControllerGetProductVersion( gamecontroller ));
#endif // SDL_VERSION_ATLEAST( 2, 0, 6 )
SDL_GameControllerEventState( SDL_ENABLE );
return num;
} }
#endif // SDL_VERSION_ATLEAST( 2, 0, 0 ) #endif // SDL_VERSION_ATLEAST( 2, 0, 0 )

Loading…
Cancel
Save