From 5a68558f4c4e9e40682d786ce4c8d842df193f9b Mon Sep 17 00:00:00 2001 From: krzys-h Date: Fri, 10 Jul 2015 00:02:39 +0200 Subject: [PATCH] Implemented basic joystick selection You can select joystick in the config file, there is still no GUI. By default, the first available joystick is used. issue #171 --- src/app/app.cpp | 23 +++++++++++++++++++++++ src/ui/maindialog.cpp | 27 ++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 0942cc1b..fcd38128 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -516,6 +516,23 @@ bool CApplication::Create() // Don't generate joystick events SDL_JoystickEventState(SDL_IGNORE); + // Report joystick list to log, since we still don't have a GUI for them so you have to set the ID manually in the config + auto joysticks = GetJoystickList(); + bool first = true; + for (const auto& joystick : joysticks) + { + if (first) + { + ChangeJoystick(joystick); + first = false; + } + GetLogger()->Info("Detected joystick: %s [ID %d]\n", joystick.name.c_str(), joystick.index); + } + if (first) + { + GetLogger()->Info("No joysticks detected\n"); + } + if (!m_headless) { m_device = Gfx::CreateDevice(m_deviceConfig, m_graphics.c_str()); @@ -725,6 +742,8 @@ bool CApplication::OpenJoystick() if ( (m_joystick.index < 0) || (m_joystick.index >= SDL_NumJoysticks()) ) return false; + GetLogger()->Info("Opening joystick %d\n", m_joystick.index); + m_private->joystick = SDL_JoystickOpen(m_joystick.index); if (m_private->joystick == nullptr) return false; @@ -746,6 +765,8 @@ void CApplication::CloseJoystick() { // Timer will remove itself automatically + GetLogger()->Info("Closing joystick\n"); + SDL_JoystickClose(m_private->joystick); m_private->joystick = nullptr; } @@ -755,6 +776,8 @@ bool CApplication::ChangeJoystick(const JoystickDevice &newJoystick) if ( (newJoystick.index < 0) || (newJoystick.index >= SDL_NumJoysticks()) ) return false; + m_joystick = newJoystick; + if (m_private->joystick != nullptr) CloseJoystick(); diff --git a/src/ui/maindialog.cpp b/src/ui/maindialog.cpp index 190ca981..2d64f3a6 100644 --- a/src/ui/maindialog.cpp +++ b/src/ui/maindialog.cpp @@ -4821,6 +4821,7 @@ void CMainDialog::UpdateSetupButtons() pc = static_cast(pw->SearchControl(EVENT_INTERFACE_JOYSTICK)); if ( pc != 0 ) { + pc->SetState(STATE_ENABLE, m_app->GetJoystick().index >= 0); pc->SetState(STATE_CHECK, m_app->GetJoystickEnabled()); } @@ -4981,6 +4982,7 @@ void CMainDialog::SetupMemorize() GetProfile().SetIntProperty("Setup", "SkyMode", m_engine->GetSkyMode()); GetProfile().SetIntProperty("Setup", "PlanetMode", m_engine->GetPlanetMode()); GetProfile().SetIntProperty("Setup", "LightMode", m_engine->GetLightMode()); + GetProfile().SetIntProperty("Setup", "UseJoystick", m_app->GetJoystickEnabled() ? m_app->GetJoystick().index : -1); GetProfile().SetFloatProperty("Setup", "ParticleDensity", m_engine->GetParticleDensity()); GetProfile().SetFloatProperty("Setup", "ClippingDistance", m_engine->GetClippingDistance()); GetProfile().SetFloatProperty("Setup", "ObjectDetail", m_engine->GetObjectDetail()); @@ -5168,11 +5170,26 @@ void CMainDialog::SetupRecall() { m_engine->SetLightMode(iValue); } - // TODO - // if ( GetProfile().GetLocalProfileInt("Setup", "UseJoystick", iValue) ) - // { - // m_engine->SetJoystick(iValue); - // } + + if ( GetProfile().GetIntProperty("Setup", "UseJoystick", iValue) ) + { + if(iValue >= 0) + { + auto joysticks = m_app->GetJoystickList(); + for(const auto& joystick : joysticks) + { + if (joystick.index == iValue) + { + m_app->ChangeJoystick(joystick); + m_app->SetJoystickEnabled(true); + } + } + } + else + { + m_app->SetJoystickEnabled(false); + } + } if ( GetProfile().GetFloatProperty("Setup", "ParticleDensity", fValue) ) {