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
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "InputCommon/ControllerEmu.h"
|
2015-10-03 13:37:25 -06:00
|
|
|
#include <memory>
|
2015-09-28 09:57:16 -06:00
|
|
|
#include "Common/Common.h"
|
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.
|
|
|
|
static std::recursive_mutex s_get_state_mutex;
|
|
|
|
std::unique_lock<std::recursive_mutex> ControllerEmu::GetStateLock()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex);
|
|
|
|
return lock;
|
|
|
|
}
|
|
|
|
|
2010-06-20 21:12:16 -06:00
|
|
|
void ControllerEmu::UpdateReferences(ControllerInterface& devi)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-07-14 09:45:59 -06:00
|
|
|
auto lock = ControllerEmu::GetStateLock();
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& ctrlGroup : groups)
|
|
|
|
{
|
|
|
|
for (auto& control : ctrlGroup->controls)
|
|
|
|
devi.UpdateReference(control->control_ref.get(), default_device);
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// extension
|
|
|
|
if (ctrlGroup->type == GROUP_TYPE_EXTENSION)
|
|
|
|
{
|
|
|
|
for (auto& attachment : ((Extension*)ctrlGroup.get())->attachments)
|
|
|
|
attachment->UpdateReferences(devi);
|
|
|
|
}
|
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void ControllerEmu::UpdateDefaultDevice()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& ctrlGroup : groups)
|
|
|
|
{
|
|
|
|
// extension
|
|
|
|
if (ctrlGroup->type == GROUP_TYPE_EXTENSION)
|
|
|
|
{
|
|
|
|
for (auto& ai : ((Extension*)ctrlGroup.get())->attachments)
|
|
|
|
{
|
|
|
|
ai->default_device = default_device;
|
|
|
|
ai->UpdateDefaultDevice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void ControllerEmu::ControlGroup::LoadConfig(IniFile::Section* sec, const std::string& defdev,
|
|
|
|
const std::string& base)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
std::string group(base + name + "/");
|
|
|
|
|
|
|
|
// settings
|
2016-07-11 11:35:32 -06:00
|
|
|
for (auto& s : numeric_settings)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-07-11 11:35:32 -06:00
|
|
|
if (s->m_type == SettingType::VIRTUAL)
|
2016-06-24 02:43:46 -06:00
|
|
|
continue;
|
2016-07-11 11:35:32 -06:00
|
|
|
sec->Get(group + s->m_name, &s->m_value, s->m_default_value * 100);
|
|
|
|
s->m_value /= 100;
|
|
|
|
}
|
|
|
|
for (auto& s : boolean_settings)
|
|
|
|
{
|
|
|
|
if (s->m_type == SettingType::VIRTUAL)
|
2016-06-24 02:43:46 -06:00
|
|
|
continue;
|
2016-07-11 11:35:32 -06:00
|
|
|
sec->Get(group + s->m_name, &s->m_value, s->m_default_value);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& c : controls)
|
|
|
|
{
|
|
|
|
// control expression
|
|
|
|
sec->Get(group + c->name, &c->control_ref->expression, "");
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// range
|
|
|
|
sec->Get(group + c->name + "/Range", &c->control_ref->range, 100.0);
|
|
|
|
c->control_ref->range /= 100;
|
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// extensions
|
|
|
|
if (type == GROUP_TYPE_EXTENSION)
|
|
|
|
{
|
|
|
|
Extension* const ext = (Extension*)this;
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ext->switch_extension = 0;
|
|
|
|
unsigned int n = 0;
|
|
|
|
std::string extname;
|
|
|
|
sec->Get(base + name, &extname, "");
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& ai : ext->attachments)
|
|
|
|
{
|
|
|
|
ai->default_device.FromString(defdev);
|
|
|
|
ai->LoadConfig(sec, base + ai->GetName() + "/");
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
if (ai->GetName() == extname)
|
|
|
|
ext->switch_extension = n;
|
2014-01-30 17:51:21 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void ControllerEmu::LoadConfig(IniFile::Section* sec, const std::string& base)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
std::string defdev = default_device.ToString();
|
|
|
|
if (base.empty())
|
|
|
|
{
|
|
|
|
sec->Get(base + "Device", &defdev, "");
|
|
|
|
default_device.FromString(defdev);
|
|
|
|
}
|
2014-01-30 17:51:21 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& cg : groups)
|
|
|
|
cg->LoadConfig(sec, defdev, base);
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void ControllerEmu::ControlGroup::SaveConfig(IniFile::Section* sec, const std::string& defdev,
|
|
|
|
const std::string& base)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
std::string group(base + name + "/");
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-07-11 11:35:32 -06:00
|
|
|
for (auto& s : numeric_settings)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-07-11 11:35:32 -06:00
|
|
|
if (s->m_type == SettingType::VIRTUAL)
|
2016-06-24 02:43:46 -06:00
|
|
|
continue;
|
2016-07-11 11:35:32 -06:00
|
|
|
sec->Set(group + s->m_name, s->m_value * 100.0, s->m_default_value * 100.0);
|
|
|
|
}
|
|
|
|
for (auto& s : boolean_settings)
|
|
|
|
{
|
|
|
|
if (s->m_type == SettingType::VIRTUAL)
|
2016-06-24 02:43:46 -06:00
|
|
|
continue;
|
2016-07-11 11:35:32 -06:00
|
|
|
sec->Set(group + s->m_name, s->m_value, s->m_default_value);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& c : controls)
|
|
|
|
{
|
|
|
|
// control expression
|
|
|
|
sec->Set(group + c->name, c->control_ref->expression, "");
|
2010-04-12 23:15:38 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// range
|
|
|
|
sec->Set(group + c->name + "/Range", c->control_ref->range * 100.0, 100.0);
|
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// extensions
|
|
|
|
if (type == GROUP_TYPE_EXTENSION)
|
|
|
|
{
|
|
|
|
Extension* const ext = (Extension*)this;
|
|
|
|
sec->Set(base + name, ext->attachments[ext->switch_extension]->GetName(), "None");
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& ai : ext->attachments)
|
|
|
|
ai->SaveConfig(sec, base + ai->GetName() + "/");
|
|
|
|
}
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void ControllerEmu::SaveConfig(IniFile::Section* sec, const std::string& base)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
const std::string defdev = default_device.ToString();
|
|
|
|
if (base.empty())
|
|
|
|
sec->Set(/*std::string(" ") +*/ base + "Device", defdev, "");
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& ctrlGroup : groups)
|
|
|
|
ctrlGroup->SaveConfig(sec, defdev, base);
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2015-10-01 06:29:19 -06:00
|
|
|
void ControllerEmu::ControlGroup::SetControlExpression(int index, const std::string& expression)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
controls.at(index)->control_ref->expression = expression;
|
2015-10-01 06:29:19 -06:00
|
|
|
}
|
|
|
|
|
2014-08-15 11:03:56 -06:00
|
|
|
ControllerEmu::AnalogStick::AnalogStick(const char* const _name, ControlState default_radius)
|
2016-06-24 02:43:46 -06:00
|
|
|
: AnalogStick(_name, _name, GROUP_TYPE_STICK)
|
|
|
|
{
|
|
|
|
}
|
2015-09-12 14:45:06 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ControllerEmu::AnalogStick::AnalogStick(const char* const _name, const char* const _ui_name,
|
|
|
|
ControlState default_radius)
|
|
|
|
: ControlGroup(_name, _ui_name, GROUP_TYPE_STICK)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& named_direction : named_directions)
|
|
|
|
controls.emplace_back(std::make_unique<Input>(named_direction));
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Modifier")));
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(
|
|
|
|
std::make_unique<NumericSetting>(_trans("Radius"), default_radius, 0, 100));
|
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
ControllerEmu::Buttons::Buttons(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_BUTTONS)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Threshold"), 0.5));
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ControllerEmu::MixedTriggers::MixedTriggers(const std::string& _name)
|
|
|
|
: ControlGroup(_name, GROUP_TYPE_MIXED_TRIGGERS)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Threshold"), 0.9));
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ControllerEmu::Triggers::Triggers(const std::string& _name)
|
|
|
|
: ControlGroup(_name, GROUP_TYPE_TRIGGERS)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
ControllerEmu::Slider::Slider(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_SLIDER)
|
2010-10-05 15:43:51 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
controls.emplace_back(std::make_unique<Input>("Left"));
|
|
|
|
controls.emplace_back(std::make_unique<Input>("Right"));
|
2010-10-05 15:43:51 -06:00
|
|
|
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
|
2010-10-05 15:43:51 -06:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
ControllerEmu::Force::Force(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_FORCE)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
memset(m_swing, 0, sizeof(m_swing));
|
2010-07-25 23:30:50 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Up")));
|
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Down")));
|
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Left")));
|
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Right")));
|
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Forward")));
|
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Backward")));
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2014-06-16 08:15:56 -06:00
|
|
|
ControllerEmu::Tilt::Tilt(const std::string& _name) : ControlGroup(_name, GROUP_TYPE_TILT)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
memset(m_tilt, 0, sizeof(m_tilt));
|
2010-06-11 20:08:01 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
controls.emplace_back(std::make_unique<Input>("Forward"));
|
|
|
|
controls.emplace_back(std::make_unique<Input>("Backward"));
|
|
|
|
controls.emplace_back(std::make_unique<Input>("Left"));
|
|
|
|
controls.emplace_back(std::make_unique<Input>("Right"));
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Modifier")));
|
2010-06-03 12:05:08 -06:00
|
|
|
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Dead Zone"), 0, 0, 50));
|
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Circle Stick"), 0));
|
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Angle"), 0.9, 0, 180));
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
ControllerEmu::Cursor::Cursor(const std::string& _name)
|
2016-06-24 02:43:46 -06:00
|
|
|
: ControlGroup(_name, GROUP_TYPE_CURSOR), m_z(0)
|
2010-06-03 12:05:08 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
for (auto& named_direction : named_directions)
|
|
|
|
controls.emplace_back(std::make_unique<Input>(named_direction));
|
|
|
|
controls.emplace_back(std::make_unique<Input>("Forward"));
|
|
|
|
controls.emplace_back(std::make_unique<Input>("Backward"));
|
|
|
|
controls.emplace_back(std::make_unique<Input>(_trans("Hide")));
|
2015-10-03 13:37:25 -06:00
|
|
|
|
2016-07-11 11:35:32 -06:00
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Center"), 0.5));
|
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Width"), 0.5));
|
|
|
|
numeric_settings.emplace_back(std::make_unique<NumericSetting>(_trans("Height"), 0.5));
|
2010-06-03 12:05:08 -06:00
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void ControllerEmu::LoadDefaults(const ControllerInterface& ciface)
|
2010-07-10 00:48:24 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// load an empty inifile section, clears everything
|
|
|
|
IniFile::Section sec;
|
|
|
|
LoadConfig(&sec);
|
2010-07-10 00:48:24 -06:00
|
|
|
|
2016-06-25 13:46:39 -06:00
|
|
|
const std::string& default_device_string = g_controller_interface.GetDefaultDeviceString();
|
|
|
|
if (!default_device_string.empty())
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-06-25 13:46:39 -06:00
|
|
|
default_device.FromString(default_device_string);
|
2016-06-24 02:43:46 -06:00
|
|
|
UpdateDefaultDevice();
|
|
|
|
}
|
2010-07-10 00:48:24 -06:00
|
|
|
}
|