mirror of
https://github.com/fabiangreffrath/woof.git
synced 2025-09-24 04:29:34 -04:00
implement hot plugging of the game controller (#485)
* implement hot plugging of the game controller * missed newline * close only open controller Fix multiple controllers (only the first one connected works)
This commit is contained in:
parent
7930b0557e
commit
9799ef65cd
@ -127,28 +127,19 @@ static int I_GetFracScaledTime(void)
|
||||
|
||||
int (*I_GetFracTime)(void) = I_GetFracRealTime;
|
||||
|
||||
int controllerpresent; // phares 4/3/98
|
||||
|
||||
int leds_always_off; // Tells it not to update LEDs
|
||||
|
||||
// pointer to current joystick device information
|
||||
SDL_GameController *controller = NULL;
|
||||
static int controller_index = -1;
|
||||
|
||||
static SDL_Keymod oldmod; // haleyjd: save old modifier key state
|
||||
|
||||
static void I_ShutdownJoystick(void)
|
||||
{
|
||||
if (controller != NULL)
|
||||
{
|
||||
SDL_GameControllerClose(controller);
|
||||
controller = NULL;
|
||||
}
|
||||
I_CloseController(controller_index);
|
||||
|
||||
if (controllerpresent)
|
||||
{
|
||||
SDL_QuitSubSystem(SDL_INIT_GAMECONTROLLER);
|
||||
controllerpresent = false;
|
||||
}
|
||||
}
|
||||
|
||||
void I_Shutdown(void)
|
||||
@ -158,10 +149,43 @@ void I_Shutdown(void)
|
||||
I_ShutdownJoystick();
|
||||
}
|
||||
|
||||
void I_OpenController(int which)
|
||||
{
|
||||
if (controller)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (SDL_IsGameController(which))
|
||||
{
|
||||
controller = SDL_GameControllerOpen(which);
|
||||
if (controller)
|
||||
{
|
||||
controller_index = which;
|
||||
printf("I_OpenController: Found a valid game controller, named: %s\n",
|
||||
SDL_GameControllerName(controller));
|
||||
}
|
||||
}
|
||||
|
||||
if (controller == NULL)
|
||||
{
|
||||
printf("I_OpenController: Could not open game controller %i: %s\n",
|
||||
which, SDL_GetError());
|
||||
}
|
||||
}
|
||||
|
||||
void I_CloseController(int which)
|
||||
{
|
||||
if (controller != NULL && controller_index == which)
|
||||
{
|
||||
SDL_GameControllerClose(controller);
|
||||
controller = NULL;
|
||||
controller_index = -1;
|
||||
}
|
||||
}
|
||||
|
||||
void I_InitJoystick(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0)
|
||||
{
|
||||
printf("I_InitJoystick: Failed to initialize game controller: %s\n",
|
||||
@ -169,37 +193,9 @@ void I_InitJoystick(void)
|
||||
return;
|
||||
}
|
||||
|
||||
controllerpresent = true;
|
||||
|
||||
// Open the joystick
|
||||
|
||||
for (i = 0; i < SDL_NumJoysticks(); ++i)
|
||||
{
|
||||
if (SDL_IsGameController(i))
|
||||
{
|
||||
controller = SDL_GameControllerOpen(i);
|
||||
if (controller)
|
||||
{
|
||||
printf("I_InitJoystick: Found a valid game controller, named: %s\n",
|
||||
SDL_GameControllerName(controller));
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("I_InitJoystick: Could not open game controller %i: %s\n",
|
||||
i, SDL_GetError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (controller == NULL)
|
||||
{
|
||||
printf("I_InitJoystick: Failed to open game controller.\n");
|
||||
I_ShutdownJoystick();
|
||||
return;
|
||||
}
|
||||
|
||||
SDL_GameControllerEventState(SDL_ENABLE);
|
||||
|
||||
printf("I_InitJoystick: Initialize game controller.\n");
|
||||
}
|
||||
|
||||
// haleyjd
|
||||
|
@ -35,6 +35,9 @@
|
||||
void I_Init(void);
|
||||
void I_InitJoystick(void);
|
||||
|
||||
void I_OpenController(int which);
|
||||
void I_CloseController(int which);
|
||||
|
||||
// Called by D_DoomLoop,
|
||||
// returns current time in tics.
|
||||
// int I_GetTime (void);
|
||||
|
@ -726,6 +726,14 @@ void I_GetEvent(void)
|
||||
}
|
||||
break;
|
||||
|
||||
case SDL_CONTROLLERDEVICEADDED:
|
||||
I_OpenController(sdlevent.cdevice.which);
|
||||
break;
|
||||
|
||||
case SDL_CONTROLLERDEVICEREMOVED:
|
||||
I_CloseController(sdlevent.cdevice.which);
|
||||
break;
|
||||
|
||||
case SDL_CONTROLLERBUTTONDOWN:
|
||||
case SDL_CONTROLLERBUTTONUP:
|
||||
case SDL_CONTROLLERAXISMOTION:
|
||||
|
Loading…
x
Reference in New Issue
Block a user