Qt: Fix controller hotplugging

This commit is contained in:
Vicki Pfau 2023-01-28 23:42:56 -08:00
parent ff6c74397a
commit 38fa501a08
2 changed files with 12 additions and 5 deletions

View File

@ -89,6 +89,7 @@ QString SDLInputDriver::currentProfile() const {
} }
void SDLInputDriver::loadConfiguration(ConfigController* config) { void SDLInputDriver::loadConfiguration(ConfigController* config) {
m_config = config;
mSDLEventsLoadConfig(&s_sdlEvents, config->input()); mSDLEventsLoadConfig(&s_sdlEvents, config->input());
if (!m_playerAttached) { if (!m_playerAttached) {
m_playerAttached = mSDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer); m_playerAttached = mSDLAttachPlayer(&s_sdlEvents, &m_sdlPlayer);
@ -126,7 +127,7 @@ mRotationSource* SDLInputDriver::rotationSource() {
bool SDLInputDriver::update() { bool SDLInputDriver::update() {
if (!m_playerAttached || !m_sdlPlayer.joystick) { if (!m_playerAttached) {
return false; return false;
} }
@ -148,6 +149,9 @@ QList<Gamepad*> SDLInputDriver::connectedGamepads() const {
#if SDL_VERSION_ATLEAST(2, 0, 0) #if SDL_VERSION_ATLEAST(2, 0, 0)
void SDLInputDriver::updateGamepads() { void SDLInputDriver::updateGamepads() {
if (m_config) {
mSDLUpdateJoysticks(&s_sdlEvents, m_config->input());
}
for (int i = 0; i < m_gamepads.size(); ++i) { for (int i = 0; i < m_gamepads.size(); ++i) {
if (m_gamepads.at(i)->updateIndex()) { if (m_gamepads.at(i)->updateIndex()) {
continue; continue;
@ -160,11 +164,13 @@ void SDLInputDriver::updateGamepads() {
}); });
for (size_t i = 0, j = 0; i < SDL_JoystickListSize(&s_sdlEvents.joysticks); ++i) { for (size_t i = 0, j = 0; i < SDL_JoystickListSize(&s_sdlEvents.joysticks); ++i) {
if ((ssize_t) j < m_gamepads.size()) {
std::shared_ptr<SDLGamepad> gamepad = m_gamepads.at(j); std::shared_ptr<SDLGamepad> gamepad = m_gamepads.at(j);
if (gamepad->m_index == i) { if (gamepad->m_index == i) {
++j; ++j;
continue; continue;
} }
}
m_gamepads.append(std::make_shared<SDLGamepad>(this, i)); m_gamepads.append(std::make_shared<SDLGamepad>(this, i));
} }
std::sort(m_gamepads.begin(), m_gamepads.end(), [](const auto& a, const auto b) { std::sort(m_gamepads.begin(), m_gamepads.end(), [](const auto& a, const auto b) {

View File

@ -62,6 +62,7 @@ public:
mRotationSource* rotationSource() override; mRotationSource* rotationSource() override;
private: private:
ConfigController* m_config = nullptr;
InputController* m_controller; InputController* m_controller;
mSDLPlayer m_sdlPlayer{}; mSDLPlayer m_sdlPlayer{};
bool m_playerAttached = false; bool m_playerAttached = false;