diff --git a/source/build/include/baselayer.h b/source/build/include/baselayer.h index 2d0da3fbd..638befe78 100644 --- a/source/build/include/baselayer.h +++ b/source/build/include/baselayer.h @@ -222,8 +222,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)); diff --git a/source/build/src/baselayer.cpp b/source/build/src/baselayer.cpp index 760364c73..e98d20bd5 100644 --- a/source/build/src/baselayer.cpp +++ b/source/build/src/baselayer.cpp @@ -102,6 +102,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) diff --git a/source/build/src/sdlayer.cpp b/source/build/src/sdlayer.cpp index 47c09c113..59e27fe7c 100644 --- a/source/build/src/sdlayer.cpp +++ b/source/build/src/sdlayer.cpp @@ -825,6 +825,8 @@ static void LoadSDLControllerDB() } #endif +static int numjoysticks; + void joyScanDevices() { inputdevices &= ~4; @@ -840,7 +842,8 @@ void joyScanDevices() joydev = nullptr; } - int numjoysticks = SDL_NumJoysticks(); + numjoysticks = SDL_NumJoysticks(); + if (numjoysticks < 1) { buildputs("No game controllers found\n"); @@ -925,10 +928,20 @@ void joyScanDevices() // // initinput() -- init input system // -int32_t initinput(void) +int32_t initinput(void(*hotplugCallback)(void) /*= NULL*/) { int32_t i; +#if SDL_MAJOR_VERSION >= 2 + if (hotplugCallback) + { + g_controllerHotplugCallback = hotplugCallback; + SDL_JoystickEventState(SDL_ENABLE); + } +#else + UNREFERENCED_PARAMETER(hotplugCallback); +#endif + #ifdef _WIN32 Win_GetOriginalLayoutName(); Win_SetKeyboardLayoutUS(1); @@ -974,7 +987,6 @@ int32_t initinput(void) #if SDL_MAJOR_VERSION >= 2 LoadSDLControllerDB(); #endif - joyScanDevices(); } @@ -2099,7 +2111,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) @@ -2187,6 +2205,9 @@ int32_t handleevents_pollsdl(void) int32_t code, rv=0, j; SDL_Event ev; + if (g_controllerHotplugCallback && SDL_NumJoysticks() != numjoysticks) + g_controllerHotplugCallback(); + while (SDL_PollEvent(&ev)) { switch (ev.type) diff --git a/source/build/src/winlayer.cpp b/source/build/src/winlayer.cpp index 074e66061..962fbffd6 100644 --- a/source/build/src/winlayer.cpp +++ b/source/build/src/winlayer.cpp @@ -683,7 +683,7 @@ int32_t handleevents(void) // // initinput() -- init input system // -int32_t initinput(void) +int32_t initinput(void(*hotplugCallback)(void) /*= NULL*/) { Win_GetOriginalLayoutName(); Win_SetKeyboardLayoutUS(1); diff --git a/source/mact/src/control.cpp b/source/mact/src/control.cpp index 9abcd6281..f1fc11ef5 100644 --- a/source/mact/src/control.cpp +++ b/source/mact/src/control.cpp @@ -955,7 +955,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();