InputManager: Add UpdateSettings() to sources

This commit is contained in:
Connor McLaughlin 2022-01-26 21:28:16 +10:00 committed by refractionpcsx2
parent dbc176fe25
commit 7555091d56
6 changed files with 63 additions and 22 deletions

View File

@ -887,26 +887,32 @@ void InputManager::PollSources()
template <typename T>
static void UpdateInputSourceState(SettingsInterface& si, InputSourceType type, bool default_state)
{
const bool old_state = (s_input_sources[static_cast<u32>(type)] != nullptr);
const bool new_state = si.GetBoolValue("InputSources", InputManager::InputSourceToString(type), default_state);
if (old_state == new_state)
return;
if (new_state)
const bool enabled = si.GetBoolValue("InputSources", InputManager::InputSourceToString(type), default_state);
if (enabled)
{
std::unique_ptr<InputSource> source = std::make_unique<T>();
if (!source->Initialize(si))
if (s_input_sources[static_cast<u32>(type)])
{
Console.Error("(InputManager) Source '%s' failed to initialize.", InputManager::InputSourceToString(type));
return;
s_input_sources[static_cast<u32>(type)]->UpdateSettings(si);
}
else
{
std::unique_ptr<InputSource> source = std::make_unique<T>();
if (!source->Initialize(si))
{
Console.Error("(InputManager) Source '%s' failed to initialize.", InputManager::InputSourceToString(type));
return;
}
s_input_sources[static_cast<u32>(type)] = std::move(source);
s_input_sources[static_cast<u32>(type)] = std::move(source);
}
}
else
{
s_input_sources[static_cast<u32>(type)]->Shutdown();
s_input_sources[static_cast<u32>(type)].reset();
if (s_input_sources[static_cast<u32>(type)])
{
s_input_sources[static_cast<u32>(type)]->Shutdown();
s_input_sources[static_cast<u32>(type)].reset();
}
}
}

View File

@ -31,6 +31,7 @@ public:
virtual ~InputSource();
virtual bool Initialize(SettingsInterface& si) = 0;
virtual void UpdateSettings(SettingsInterface& si) = 0;
virtual void Shutdown() = 0;
virtual void PollEvents() = 0;

View File

@ -74,20 +74,42 @@ bool SDLInputSource::Initialize(SettingsInterface& si)
Console.Error("Controller database resource is missing.");
}
const bool ds4_rumble_enabled = si.GetBoolValue("InputSources", "SDLControllerEnhancedMode", false);
if (ds4_rumble_enabled)
LoadSettings(si);
SetHints();
return InitializeSubsystem();
}
void SDLInputSource::UpdateSettings(SettingsInterface& si)
{
const bool old_controller_enhanced_mode = m_controller_enhanced_mode;
LoadSettings(si);
if (m_controller_enhanced_mode != old_controller_enhanced_mode)
{
Console.WriteLn("Enabling PS4/PS5 enhanced mode.");
ShutdownSubsystem();
SetHints();
InitializeSubsystem();
}
}
void SDLInputSource::LoadSettings(SettingsInterface& si)
{
m_controller_enhanced_mode = si.GetBoolValue("InputSources", "SDLControllerEnhancedMode", false);
}
void SDLInputSource::SetHints()
{
#if SDL_VERSION_ATLEAST(2, 0, 9)
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4, "true");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "true");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, m_controller_enhanced_mode ? "1" : "0");
#endif
#if SDL_VERSION_ATLEAST(2, 0, 16)
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5, "true");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "true");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, m_controller_enhanced_mode ? "1" : "0");
#endif
}
}
bool SDLInputSource::InitializeSubsystem()
{
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) < 0)
{
Console.Error("SDL_InitSubSystem(SDL_INIT_JOYSTICK |SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) failed");
@ -99,7 +121,7 @@ bool SDLInputSource::Initialize(SettingsInterface& si)
return true;
}
void SDLInputSource::Shutdown()
void SDLInputSource::ShutdownSubsystem()
{
while (!m_controllers.empty())
CloseGameController(m_controllers.begin()->joystick_id);

View File

@ -30,6 +30,7 @@ public:
~SDLInputSource();
bool Initialize(SettingsInterface& si) override;
void UpdateSettings(SettingsInterface& si) override;
void Shutdown() override;
void PollEvents() override;
@ -62,6 +63,11 @@ private:
using ControllerDataVector = std::vector<ControllerData>;
bool InitializeSubsystem();
void ShutdownSubsystem();
void LoadSettings(SettingsInterface& si);
void SetHints();
ControllerDataVector::iterator GetControllerDataForJoystickId(int id);
ControllerDataVector::iterator GetControllerDataForPlayerId(int id);
int GetFreePlayerId() const;
@ -75,4 +81,5 @@ private:
ControllerDataVector m_controllers;
bool m_sdl_subsystem_initialized = false;
bool m_controller_enhanced_mode = false;
};

View File

@ -110,6 +110,10 @@ bool XInputSource::Initialize(SettingsInterface& si)
return true;
}
void XInputSource::UpdateSettings(SettingsInterface& si)
{
}
void XInputSource::Shutdown()
{
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)

View File

@ -30,6 +30,7 @@ public:
~XInputSource();
bool Initialize(SettingsInterface& si) override;
void UpdateSettings(SettingsInterface& si) override;
void Shutdown() override;
void PollEvents() override;