From fa943c70e3ced17df256c3b408bfd43187e4df34 Mon Sep 17 00:00:00 2001 From: Benau Date: Wed, 15 Jul 2020 14:16:19 +0800 Subject: [PATCH] Fix missing joystick if device is reinitialized --- .../source/Irrlicht/CIrrDeviceSDL.cpp | 2 +- src/graphics/irr_driver.cpp | 3 ++ src/input/input_manager.cpp | 32 +++++++++++++++++++ src/input/input_manager.hpp | 1 + 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp b/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp index 2e5dcc86f..18a6e49c0 100644 --- a/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp +++ b/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp @@ -61,7 +61,7 @@ CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param) // noparachute prevents SDL from catching fatal errors. SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1"); SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); - u32 init_flags = SDL_INIT_TIMER| SDL_INIT_VIDEO| SDL_INIT_GAMECONTROLLER; + u32 init_flags = SDL_INIT_TIMER | SDL_INIT_VIDEO; #if SDL_VERSION_ATLEAST(2, 0, 9) init_flags |= SDL_INIT_SENSOR; #endif diff --git a/src/graphics/irr_driver.cpp b/src/graphics/irr_driver.cpp index 674701c92..2cc8240fe 100644 --- a/src/graphics/irr_driver.cpp +++ b/src/graphics/irr_driver.cpp @@ -1027,6 +1027,9 @@ void IrrDriver::applyResolutionSettings(bool recreate_device) // Input manager set first so it recieves SDL joystick event // Re-init GUI engine GUIEngine::init(m_device, m_video_driver, StateManager::get()); + // If not recreate device we need to add the previous joystick manually + if (!recreate_device) + input_manager->addJoystick(); setMaxTextureSize(); //material_manager->reInit(); diff --git a/src/input/input_manager.cpp b/src/input/input_manager.cpp index 4fe67eb88..2f0c28e58 100644 --- a/src/input/input_manager.cpp +++ b/src/input/input_manager.cpp @@ -87,6 +87,38 @@ InputManager::InputManager() : m_mode(BOOTSTRAP), m_timer_in_use = false; m_master_player_only = false; m_timer = 0; +#ifndef SERVER_ONLY + SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER); +#endif +} + +// ----------------------------------------------------------------------------- +void InputManager::addJoystick() +{ +#ifndef SERVER_ONLY + // When irrlicht device is reinitialized the joystick added event may be + // lost, we look for them and add it back + for (int i = 0; i < SDL_NumJoysticks(); i++) + { + try + { + SDL_Joystick* joystick = SDL_JoystickOpen(i); + if (!joystick) + continue; + SDL_JoystickID id = SDL_JoystickInstanceID(joystick); + if (m_sdl_controller.find(id) != m_sdl_controller.end()) + continue; + std::unique_ptr c( + new SDLController(i)); + id = c->getInstanceID(); + m_sdl_controller[id] = std::move(c); + } + catch (std::exception& e) + { + Log::error("SDLController", "%s", e.what()); + } + } +#endif } // ----------------------------------------------------------------------------- diff --git a/src/input/input_manager.hpp b/src/input/input_manager.hpp index 5c2e38663..38f64a007 100644 --- a/src/input/input_manager.hpp +++ b/src/input/input_manager.hpp @@ -126,6 +126,7 @@ public: void dispatchInput(Input::InputType, int deviceID, int btnID, Input::AxisDirection direction, int value, bool shift_mask = false); + void addJoystick(); }; extern InputManager *input_manager;