mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
ControllerInterface: Add RemoveDevice()
This adds RemoveDevice() to ControllerInterface, fixes ExpressionParser and some other code to support device removals without crashing, and adds an IsValid() method to Device, to prepare for hotplugging.
This commit is contained in:
@ -6,8 +6,19 @@
|
||||
#include <memory>
|
||||
#include "Common/Common.h"
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
void ControllerEmu::UpdateReferences(ControllerInterface& devi)
|
||||
{
|
||||
auto lock = ControllerEmu::GetStateLock();
|
||||
for (auto& ctrlGroup : groups)
|
||||
{
|
||||
for (auto& control : ctrlGroup->controls)
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@ -444,6 +445,12 @@ public:
|
||||
|
||||
void UpdateReferences(ControllerInterface& devi);
|
||||
|
||||
// This returns a lock that should be held before calling State() on any control
|
||||
// references and GetState(), by extension. This prevents a race condition
|
||||
// which happens while handling a hotplug event because a control reference's State()
|
||||
// could be called before we have finished updating the reference.
|
||||
static std::unique_lock<std::recursive_mutex> GetStateLock();
|
||||
|
||||
std::vector<std::unique_ptr<ControlGroup>> groups;
|
||||
|
||||
ciface::Core::DeviceQualifier default_device;
|
||||
|
@ -160,6 +160,14 @@ void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device
|
||||
m_devices.emplace_back(std::move(device));
|
||||
}
|
||||
|
||||
void ControllerInterface::RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
||||
m_devices.erase(std::remove_if(m_devices.begin(), m_devices.end(),
|
||||
[&callback](const auto& dev) { return callback(dev.get()); }),
|
||||
m_devices.end());
|
||||
}
|
||||
|
||||
//
|
||||
// UpdateInput
|
||||
//
|
||||
|
@ -122,6 +122,7 @@ public:
|
||||
void Reinitialize();
|
||||
void Shutdown();
|
||||
void AddDevice(std::shared_ptr<ciface::Core::Device> device);
|
||||
void RemoveDevice(std::function<bool(const ciface::Core::Device*)> callback);
|
||||
bool IsInit() const { return m_is_init; }
|
||||
void UpdateReference(ControlReference* control,
|
||||
const ciface::Core::DeviceQualifier& default_device) const;
|
||||
|
@ -98,6 +98,7 @@ public:
|
||||
virtual std::string GetName() const = 0;
|
||||
virtual std::string GetSource() const = 0;
|
||||
virtual void UpdateInput() {}
|
||||
virtual bool IsValid() const { return true; }
|
||||
const std::vector<Input*>& Inputs() const { return m_inputs; }
|
||||
const std::vector<Output*>& Outputs() const { return m_outputs; }
|
||||
Input* FindInput(const std::string& name) const;
|
||||
|
@ -235,8 +235,9 @@ public:
|
||||
ControlQualifier qualifier;
|
||||
Device::Control* control;
|
||||
|
||||
ControlExpression(ControlQualifier qualifier_, Device::Control* control_)
|
||||
: qualifier(qualifier_), control(control_)
|
||||
ControlExpression(ControlQualifier qualifier_, std::shared_ptr<Device> device,
|
||||
Device::Control* control_)
|
||||
: qualifier(qualifier_), control(control_), m_device(device)
|
||||
{
|
||||
}
|
||||
|
||||
@ -244,6 +245,8 @@ public:
|
||||
void SetValue(ControlState value) override { control->ToOutput()->SetGatedState(value); }
|
||||
int CountNumControls() override { return 1; }
|
||||
operator std::string() override { return "`" + (std::string)qualifier + "`"; }
|
||||
private:
|
||||
std::shared_ptr<Device> m_device;
|
||||
};
|
||||
|
||||
class BinaryExpression : public ExpressionNode
|
||||
@ -393,6 +396,7 @@ private:
|
||||
{
|
||||
case TOK_CONTROL:
|
||||
{
|
||||
std::shared_ptr<Device> device = finder.FindDevice(tok.qualifier);
|
||||
Device::Control* control = finder.FindControl(tok.qualifier);
|
||||
if (control == nullptr)
|
||||
{
|
||||
@ -400,7 +404,7 @@ private:
|
||||
return EXPRESSION_PARSE_SUCCESS;
|
||||
}
|
||||
|
||||
*expr_out = new ControlExpression(tok.qualifier, control);
|
||||
*expr_out = new ControlExpression(tok.qualifier, device, control);
|
||||
return EXPRESSION_PARSE_SUCCESS;
|
||||
}
|
||||
case TOK_LPAREN:
|
||||
@ -550,10 +554,11 @@ ExpressionParseStatus ParseExpression(const std::string& str, ControlFinder& fin
|
||||
qualifier.control_name = str;
|
||||
qualifier.has_device = false;
|
||||
|
||||
std::shared_ptr<Device> device = finder.FindDevice(qualifier);
|
||||
Device::Control* control = finder.FindControl(qualifier);
|
||||
if (control)
|
||||
{
|
||||
*expr_out = new Expression(new ControlExpression(qualifier, control));
|
||||
*expr_out = new Expression(new ControlExpression(qualifier, device, control));
|
||||
return EXPRESSION_PARSE_SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -37,10 +37,10 @@ public:
|
||||
: container(container_), default_device(default_), is_input(is_input_)
|
||||
{
|
||||
}
|
||||
std::shared_ptr<Core::Device> FindDevice(ControlQualifier qualifier);
|
||||
Core::Device::Control* FindControl(ControlQualifier qualifier);
|
||||
|
||||
private:
|
||||
std::shared_ptr<Core::Device> FindDevice(ControlQualifier qualifier);
|
||||
const Core::DeviceContainer& container;
|
||||
const Core::DeviceQualifier& default_device;
|
||||
bool is_input;
|
||||
|
Reference in New Issue
Block a user