2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-06-12 17:15:16 +00:00
|
|
|
|
2017-01-30 03:32:04 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
2017-02-09 03:15:43 +00:00
|
|
|
|
2015-10-03 19:37:25 +00:00
|
|
|
#include <memory>
|
2017-02-09 03:15:43 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2021-03-21 19:27:00 +00:00
|
|
|
#include <utility>
|
2017-02-09 03:15:43 +00:00
|
|
|
|
|
|
|
#include "Common/IniFile.h"
|
|
|
|
|
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
|
|
|
#include "InputCommon/ControllerEmu/Control/Control.h"
|
2019-01-01 14:32:39 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/Attachments.h"
|
2017-02-09 03:15:43 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
|
2019-10-18 19:54:02 +00:00
|
|
|
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
|
2017-04-04 19:37:31 +00:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2017-02-09 03:15:43 +00:00
|
|
|
|
|
|
|
namespace ControllerEmu
|
|
|
|
{
|
2021-11-17 20:55:02 +00:00
|
|
|
// This should theoretically be per EmulatedController instance,
|
|
|
|
// though no EmulatedController usually run in parallel, so it makes little difference
|
2017-02-09 03:15:43 +00:00
|
|
|
static std::recursive_mutex s_get_state_mutex;
|
|
|
|
|
2019-05-01 22:25:48 +00:00
|
|
|
std::string EmulatedController::GetDisplayName() const
|
|
|
|
{
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
2017-02-09 03:15:43 +00:00
|
|
|
EmulatedController::~EmulatedController() = default;
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2016-07-14 15:45:59 +00:00
|
|
|
// This should be called before calling GetState() or State() on a control reference
|
|
|
|
// to prevent a race condition.
|
|
|
|
// This is a recursive mutex because UpdateReferences is recursive.
|
2017-02-09 03:15:43 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> EmulatedController::GetStateLock()
|
2016-07-14 15:45:59 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex);
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
2017-02-11 05:29:22 +00:00
|
|
|
void EmulatedController::UpdateReferences(const ControllerInterface& devi)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2022-09-07 20:37:14 +00:00
|
|
|
std::scoped_lock lk(s_get_state_mutex, devi.GetDevicesMutex());
|
2022-06-01 01:17:01 +00:00
|
|
|
|
2017-02-08 00:25:27 +00:00
|
|
|
m_default_device_is_connected = devi.HasConnectedDevice(m_default_device);
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars);
|
|
|
|
|
|
|
|
UpdateReferences(env);
|
2021-03-11 22:01:18 +00:00
|
|
|
|
|
|
|
env.CleanUnusedVariables();
|
2018-12-30 22:06:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedController::UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env)
|
|
|
|
{
|
|
|
|
const auto lock = GetStateLock();
|
|
|
|
|
2014-02-01 22:20:35 +00:00
|
|
|
for (auto& ctrlGroup : groups)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2014-02-01 22:20:35 +00:00
|
|
|
for (auto& control : ctrlGroup->controls)
|
2018-12-30 22:06:29 +00:00
|
|
|
control->control_ref->UpdateReference(env);
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2019-10-18 19:54:02 +00:00
|
|
|
for (auto& setting : ctrlGroup->numeric_settings)
|
|
|
|
setting->GetInputReference().UpdateReference(env);
|
|
|
|
|
2019-01-01 14:32:39 +00:00
|
|
|
// Attachments:
|
|
|
|
if (ctrlGroup->type == GroupType::Attachments)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2020-01-18 17:19:32 +00:00
|
|
|
auto* const attachments = static_cast<Attachments*>(ctrlGroup.get());
|
|
|
|
|
|
|
|
attachments->GetSelectionSetting().GetInputReference().UpdateReference(env);
|
|
|
|
|
|
|
|
for (auto& attachment : attachments->GetAttachmentList())
|
2018-12-30 22:06:29 +00:00
|
|
|
attachment->UpdateReferences(env);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:06:29 +00:00
|
|
|
void EmulatedController::UpdateSingleControlReference(const ControllerInterface& devi,
|
|
|
|
ControlReference* ref)
|
|
|
|
{
|
|
|
|
ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars);
|
2021-03-11 22:01:18 +00:00
|
|
|
|
2021-11-17 20:55:02 +00:00
|
|
|
const auto lock = GetStateLock();
|
2018-12-30 22:06:29 +00:00
|
|
|
ref->UpdateReference(env);
|
2021-03-11 22:01:18 +00:00
|
|
|
|
|
|
|
env.CleanUnusedVariables();
|
|
|
|
}
|
|
|
|
|
|
|
|
const ciface::ExpressionParser::ControlEnvironment::VariableContainer&
|
|
|
|
EmulatedController::GetExpressionVariables() const
|
|
|
|
{
|
|
|
|
return m_expression_vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedController::ResetExpressionVariables()
|
|
|
|
{
|
|
|
|
for (auto& var : m_expression_vars)
|
|
|
|
{
|
|
|
|
if (var.second)
|
|
|
|
{
|
|
|
|
*var.second = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 22:06:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 00:25:27 +00:00
|
|
|
bool EmulatedController::IsDefaultDeviceConnected() const
|
|
|
|
{
|
|
|
|
return m_default_device_is_connected;
|
|
|
|
}
|
|
|
|
|
2017-11-04 21:08:26 +00:00
|
|
|
const ciface::Core::DeviceQualifier& EmulatedController::GetDefaultDevice() const
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2017-11-04 21:08:26 +00:00
|
|
|
return m_default_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedController::SetDefaultDevice(const std::string& device)
|
|
|
|
{
|
|
|
|
ciface::Core::DeviceQualifier devq;
|
|
|
|
devq.FromString(device);
|
|
|
|
SetDefaultDevice(std::move(devq));
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedController::SetDefaultDevice(ciface::Core::DeviceQualifier devq)
|
|
|
|
{
|
|
|
|
m_default_device = std::move(devq);
|
|
|
|
|
2014-02-01 22:20:35 +00:00
|
|
|
for (auto& ctrlGroup : groups)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2019-01-01 14:32:39 +00:00
|
|
|
// Attachments:
|
|
|
|
if (ctrlGroup->type == GroupType::Attachments)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2019-01-01 14:32:39 +00:00
|
|
|
for (auto& ai : static_cast<Attachments*>(ctrlGroup.get())->GetAttachmentList())
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2017-11-04 21:08:26 +00:00
|
|
|
ai->SetDefaultDevice(m_default_device);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
void EmulatedController::LoadConfig(Common::IniFile::Section* sec, const std::string& base)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2022-06-01 01:17:01 +00:00
|
|
|
const auto lock = GetStateLock();
|
2017-11-04 21:08:26 +00:00
|
|
|
std::string defdev = GetDefaultDevice().ToString();
|
2010-06-04 20:03:03 +00:00
|
|
|
if (base.empty())
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2014-02-08 05:50:37 +00:00
|
|
|
sec->Get(base + "Device", &defdev, "");
|
2017-11-04 21:08:26 +00:00
|
|
|
SetDefaultDevice(defdev);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
2014-01-31 00:51:21 +00:00
|
|
|
|
2014-02-01 22:20:35 +00:00
|
|
|
for (auto& cg : groups)
|
2014-01-31 00:51:21 +00:00
|
|
|
cg->LoadConfig(sec, defdev, base);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2023-04-13 13:38:09 +00:00
|
|
|
void EmulatedController::SaveConfig(Common::IniFile::Section* sec, const std::string& base)
|
2010-06-03 18:05:08 +00:00
|
|
|
{
|
2022-06-01 01:17:01 +00:00
|
|
|
const auto lock = GetStateLock();
|
2017-11-04 21:08:26 +00:00
|
|
|
const std::string defdev = GetDefaultDevice().ToString();
|
2011-01-14 03:05:02 +00:00
|
|
|
if (base.empty())
|
2014-02-08 05:50:37 +00:00
|
|
|
sec->Set(/*std::string(" ") +*/ base + "Device", defdev, "");
|
2010-06-03 18:05:08 +00:00
|
|
|
|
2014-02-01 22:20:35 +00:00
|
|
|
for (auto& ctrlGroup : groups)
|
|
|
|
ctrlGroup->SaveConfig(sec, defdev, base);
|
2010-06-03 18:05:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 03:15:43 +00:00
|
|
|
void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
|
2010-07-10 06:48:24 +00:00
|
|
|
{
|
2022-06-01 01:17:01 +00:00
|
|
|
const auto lock = GetStateLock();
|
2010-07-10 06:48:24 +00:00
|
|
|
// load an empty inifile section, clears everything
|
2023-04-13 13:38:09 +00:00
|
|
|
Common::IniFile::Section sec;
|
2010-07-10 06:48:24 +00:00
|
|
|
LoadConfig(&sec);
|
|
|
|
|
2016-12-28 01:46:40 +00:00
|
|
|
const std::string& default_device_string = ciface.GetDefaultDeviceString();
|
2016-06-25 19:46:39 +00:00
|
|
|
if (!default_device_string.empty())
|
2010-07-10 06:48:24 +00:00
|
|
|
{
|
2017-11-04 21:08:26 +00:00
|
|
|
SetDefaultDevice(default_device_string);
|
2010-07-10 06:48:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-21 19:27:00 +00:00
|
|
|
|
|
|
|
void EmulatedController::SetInputOverrideFunction(InputOverrideFunction override_func)
|
|
|
|
{
|
|
|
|
m_input_override_function = std::move(override_func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedController::ClearInputOverrideFunction()
|
|
|
|
{
|
|
|
|
m_input_override_function = {};
|
|
|
|
}
|
|
|
|
|
2017-02-09 03:15:43 +00:00
|
|
|
} // namespace ControllerEmu
|