dolphin/Source/Core/InputCommon/ControllerEmu/ControllerEmu.cpp

128 lines
3.4 KiB
C++

// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include <memory>
#include <mutex>
#include <string>
#include "Common/IniFile.h"
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerEmu/Control/Control.h"
#include "InputCommon/ControllerEmu/ControlGroup/Attachments.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
namespace ControllerEmu
{
static std::recursive_mutex s_get_state_mutex;
std::string EmulatedController::GetDisplayName() const
{
return GetName();
}
EmulatedController::~EmulatedController() = default;
// 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.
std::unique_lock<std::recursive_mutex> EmulatedController::GetStateLock()
{
std::unique_lock<std::recursive_mutex> lock(s_get_state_mutex);
return lock;
}
void EmulatedController::UpdateReferences(const ControllerInterface& devi)
{
const auto lock = GetStateLock();
m_default_device_is_connected = devi.HasConnectedDevice(m_default_device);
for (auto& ctrlGroup : groups)
{
for (auto& control : ctrlGroup->controls)
control->control_ref.get()->UpdateReference(devi, GetDefaultDevice());
// Attachments:
if (ctrlGroup->type == GroupType::Attachments)
{
for (auto& attachment : static_cast<Attachments*>(ctrlGroup.get())->GetAttachmentList())
attachment->UpdateReferences(devi);
}
}
}
bool EmulatedController::IsDefaultDeviceConnected() const
{
return m_default_device_is_connected;
}
const ciface::Core::DeviceQualifier& EmulatedController::GetDefaultDevice() const
{
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);
for (auto& ctrlGroup : groups)
{
// Attachments:
if (ctrlGroup->type == GroupType::Attachments)
{
for (auto& ai : static_cast<Attachments*>(ctrlGroup.get())->GetAttachmentList())
{
ai->SetDefaultDevice(m_default_device);
}
}
}
}
void EmulatedController::LoadConfig(IniFile::Section* sec, const std::string& base)
{
std::string defdev = GetDefaultDevice().ToString();
if (base.empty())
{
sec->Get(base + "Device", &defdev, "");
SetDefaultDevice(defdev);
}
for (auto& cg : groups)
cg->LoadConfig(sec, defdev, base);
}
void EmulatedController::SaveConfig(IniFile::Section* sec, const std::string& base)
{
const std::string defdev = GetDefaultDevice().ToString();
if (base.empty())
sec->Set(/*std::string(" ") +*/ base + "Device", defdev, "");
for (auto& ctrlGroup : groups)
ctrlGroup->SaveConfig(sec, defdev, base);
}
void EmulatedController::LoadDefaults(const ControllerInterface& ciface)
{
// load an empty inifile section, clears everything
IniFile::Section sec;
LoadConfig(&sec);
const std::string& default_device_string = ciface.GetDefaultDeviceString();
if (!default_device_string.empty())
{
SetDefaultDevice(default_device_string);
}
}
} // namespace ControllerEmu