Commit 1c9f5368 authored by Richard Gobeille's avatar Richard Gobeille Committed by Richard Gobeille
Browse files

engine: game controller hotplug support

parent de0ed712
......@@ -237,8 +237,9 @@ int32_t handleevents_peekkeys(void);
extern void (*keypresscallback)(int32_t,int32_t);
extern void (*g_mouseCallback)(int32_t,int32_t);
extern void (*g_controllerHotplugCallback)(void);
int32_t initinput(void);
int32_t initinput(void(*hotplugCallback)(void) = NULL);
void uninitinput(void);
void keySetCallback(void (*callback)(int32_t,int32_t));
void mouseSetCallback(void (*callback)(int32_t,int32_t));
......
......@@ -108,6 +108,8 @@ bool g_mouseLockedToWindow = 1;
void (*g_mouseCallback)(int32_t, int32_t);
void mouseSetCallback(void(*callback)(int32_t, int32_t)) { g_mouseCallback = callback; }
void (*g_controllerHotplugCallback)(void);
int32_t mouseAdvanceClickState(void)
{
switch (g_mouseClickState)
......
......@@ -907,6 +907,8 @@ static void LoadSDLControllerDB()
}
#endif
static int numjoysticks;
void joyScanDevices()
{
inputdevices &= ~4;
......@@ -924,7 +926,8 @@ void joyScanDevices()
joydev = nullptr;
}
int numjoysticks = SDL_NumJoysticks();
numjoysticks = SDL_NumJoysticks();
if (numjoysticks < 1)
{
buildputs("No game controllers found\n");
......@@ -1036,10 +1039,17 @@ void joyScanDevices()
//
// initinput() -- init input system
//
int32_t initinput(void)
int32_t initinput(void(*hotplugCallback)(void) /*= nullptr*/)
{
int32_t i;
#if SDL_MAJOR_VERSION >= 2
if (hotplugCallback)
g_controllerHotplugCallback = hotplugCallback;
#else
UNREFERENCED_PARAMETER(hotplugCallback);
#endif
#if defined EDUKE32_OSX
// force OS X to operate in >1 button mouse mode so that LMB isn't adulterated
if (!getenv("SDL_HAS3BUTTONMOUSE"))
......@@ -1079,7 +1089,6 @@ int32_t initinput(void)
#if SDL_MAJOR_VERSION >= 2
LoadSDLControllerDB();
#endif
joyScanDevices();
}
......@@ -2164,7 +2173,13 @@ int32_t handleevents_sdlcommon(SDL_Event *ev)
break;
# endif
#endif
#if SDL_MAJOR_VERSION >= 2
case SDL_CONTROLLERDEVICEADDED:
case SDL_CONTROLLERDEVICEREMOVED:
if (g_controllerHotplugCallback && SDL_NumJoysticks() != numjoysticks)
g_controllerHotplugCallback();
break;
#endif
case SDL_JOYAXISMOTION:
#if SDL_MAJOR_VERSION >= 2
if (joystick.isGameController)
......
......@@ -679,7 +679,7 @@ int32_t handleevents(void)
//
// initinput() -- init input system
//
int32_t initinput(void)
int32_t initinput(void(*hotplugCallback)(void) /*= NULL*/)
{
g_mouseEnabled=0;
memset(keystatus, 0, sizeof(keystatus));
......
......@@ -868,7 +868,7 @@ bool CONTROL_Startup(controltype which, int32_t(*TimeFunction)(void), int32_t ti
if (CONTROL_DoubleClickSpeed <= 0)
CONTROL_DoubleClickSpeed = 1;
if (initinput())
if (initinput(CONTROL_ScanForControllers))
return true;
KB_Startup();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment