2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2010 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2010-06-12 11:15:16 -06:00
|
|
|
|
2017-01-29 20:32:04 -07:00
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
2017-02-08 20:15:43 -07:00
|
|
|
|
2015-10-03 13:37:25 -06:00
|
|
|
#include <memory>
|
2017-02-08 20:15:43 -07:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Common/IniFile.h"
|
|
|
|
|
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
|
|
|
#include "InputCommon/ControllerEmu/Control/Control.h"
|
2019-01-01 07:32:39 -07:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/Attachments.h"
|
2017-02-08 20:15:43 -07:00
|
|
|
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
|
2019-10-18 13:54:02 -06:00
|
|
|
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
|
2017-04-04 13:37:31 -06:00
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
2017-02-08 20:15:43 -07:00
|
|
|
|
|
|
|
namespace ControllerEmu
|
|
|
|
{
|
|
|
|
static std::recursive_mutex s_get_state_mutex;
|
|
|
|
|
2019-05-01 16:25:48 -06:00
|
|
|
std::string EmulatedController::GetDisplayName() const
|
|
|
|
{
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:15:43 -07:00
|
|
|
EmulatedController::~EmulatedController() = default;
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-07-14 09:45:59 -06: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-08 20:15:43 -07:00
|
|
|
std::unique_lock<std::recursive_mutex> EmulatedController::GetStateLock()
|
2016-07-14 09:45:59 -06:00
|
|
|
{
|
|
|
|
std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex);
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
2017-02-10 22:29:22 -07:00
|
|
|
void EmulatedController::UpdateReferences(const ControllerInterface& devi)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2017-02-07 17:25:27 -07:00
|
|
|
m_default_device_is_connected = devi.HasConnectedDevice(m_default_device);
|
|
|
|
|
2018-12-30 15:06:29 -07:00
|
|
|
ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars);
|
|
|
|
|
|
|
|
UpdateReferences(env);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedController::UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env)
|
|
|
|
{
|
|
|
|
const auto lock = GetStateLock();
|
|
|
|
|
2014-02-01 15:20:35 -07:00
|
|
|
for (auto& ctrlGroup : groups)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2014-02-01 15:20:35 -07:00
|
|
|
for (auto& control : ctrlGroup->controls)
|
2018-12-30 15:06:29 -07:00
|
|
|
control->control_ref->UpdateReference(env);
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2019-10-18 13:54:02 -06:00
|
|
|
for (auto& setting : ctrlGroup->numeric_settings)
|
|
|
|
setting->GetInputReference().UpdateReference(env);
|
|
|
|
|
2019-01-01 07:32:39 -07:00
|
|
|
// Attachments:
|
|
|
|
if (ctrlGroup->type == GroupType::Attachments)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2020-01-18 10:19:32 -07:00
|
|
|
auto* const attachments = static_cast<Attachments*>(ctrlGroup.get());
|
|
|
|
|
|
|
|
attachments->GetSelectionSetting().GetInputReference().UpdateReference(env);
|
|
|
|
|
|
|
|
for (auto& attachment : attachments->GetAttachmentList())
|
2018-12-30 15:06:29 -07:00
|
|
|
attachment->UpdateReferences(env);
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 15:06:29 -07:00
|
|
|
void EmulatedController::UpdateSingleControlReference(const ControllerInterface& devi,
|
|
|
|
ControlReference* ref)
|
|
|
|
{
|
|
|
|
ciface::ExpressionParser::ControlEnvironment env(devi, GetDefaultDevice(), m_expression_vars);
|
|
|
|
ref->UpdateReference(env);
|
|
|
|
}
|
|
|
|
|
2017-02-07 17:25:27 -07:00
|
|
|
bool EmulatedController::IsDefaultDeviceConnected() const
|
|
|
|
{
|
|
|
|
return m_default_device_is_connected;
|
|
|
|
}
|
|
|
|
|
2017-11-04 15:08:26 -06:00
|
|
|
const ciface::Core::DeviceQualifier& EmulatedController::GetDefaultDevice() const
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2017-11-04 15:08:26 -06: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 15:20:35 -07:00
|
|
|
for (auto& ctrlGroup : groups)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2019-01-01 07:32:39 -07:00
|
|
|
// Attachments:
|
|
|
|
if (ctrlGroup->type == GroupType::Attachments)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2019-01-01 07:32:39 -07:00
|
|
|
for (auto& ai : static_cast<Attachments*>(ctrlGroup.get())->GetAttachmentList())
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2017-11-04 15:08:26 -06:00
|
|
|
ai->SetDefaultDevice(m_default_device);
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 20:15:43 -07:00
|
|
|
void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& base)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2017-11-04 15:08:26 -06:00
|
|
|
std::string defdev = GetDefaultDevice().ToString();
|
2010-06-04 14:03:03 -06:00
|
|
|
if (base.empty())
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2014-02-07 22:50:37 -07:00
|
|
|
sec->Get(base + "Device", &defdev, "");
|
2017-11-04 15:08:26 -06:00
|
|
|
SetDefaultDevice(defdev);
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
2014-01-30 17:51:21 -07:00
|
|
|
|
2014-02-01 15:20:35 -07:00
|
|
|
for (auto& cg : groups)
|
2014-01-30 17:51:21 -07:00
|
|
|
cg->LoadConfig(sec, defdev, base);
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2017-02-08 20:15:43 -07:00
|
|
|
void EmulatedController::SaveConfig(IniFile::Section* sec, const std::string& base)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2017-11-04 15:08:26 -06:00
|
|
|
const std::string defdev = GetDefaultDevice().ToString();
|
2011-01-13 20:05:02 -07:00
|
|
|
if (base.empty())
|
2014-02-07 22:50:37 -07:00
|
|
|
sec->Set(/*std::string(" ") +*/ base + "Device", defdev, "");
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2014-02-01 15:20:35 -07:00
|
|
|
for (auto& ctrlGroup : groups)
|
|
|
|
ctrlGroup->SaveConfig(sec, defdev, base);
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2017-02-08 20:15:43 -07:00
|
|
|
void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
|
2010-07-10 00:48:24 -06:00
|
|
|
{
|
|
|
|
// load an empty inifile section, clears everything
|
|
|
|
IniFile::Section sec;
|
|
|
|
LoadConfig(&sec);
|
|
|
|
|
2016-12-27 18:46:40 -07:00
|
|
|
const std::string& default_device_string = ciface.GetDefaultDeviceString();
|
2016-06-25 13:46:39 -06:00
|
|
|
if (!default_device_string.empty())
|
2010-07-10 00:48:24 -06:00
|
|
|
{
|
2017-11-04 15:08:26 -06:00
|
|
|
SetDefaultDevice(default_device_string);
|
2010-07-10 00:48:24 -06:00
|
|
|
}
|
|
|
|
}
|
2017-02-08 20:15:43 -07:00
|
|
|
} // namespace ControllerEmu
|