InputCommon: Extract ControlReference from ControllerInterface

Better separation of concerns. Relegates `ControllerInterface` to
enumerating input controls, and the new `ControlReference` deals with
combining inputs and configuration expression parsing.
This commit is contained in:
Michael Maltese
2017-02-07 22:59:10 -08:00
parent f621a6af43
commit a509f56116
15 changed files with 274 additions and 282 deletions
@@ -4,7 +4,6 @@
#include <mutex>
#include "Common/Thread.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#ifdef CIFACE_USE_XINPUT
@@ -33,13 +32,6 @@
#include "InputCommon/ControllerInterface/Pipes/Pipes.h"
#endif
using namespace ciface::ExpressionParser;
namespace
{
const ControlState INPUT_DETECT_THRESHOLD = 0.55;
}
ControllerInterface g_controller_interface;
//
@@ -236,133 +228,3 @@ void ControllerInterface::InvokeHotplugCallbacks() const
for (const auto& callback : m_hotplug_callbacks)
callback();
}
//
// InputReference :: State
//
// Gets the state of an input reference
// override function for ControlReference::State ...
//
ControlState ControllerInterface::InputReference::State(const ControlState ignore)
{
if (parsed_expression)
return parsed_expression->GetValue() * range;
else
return 0.0;
}
//
// OutputReference :: State
//
// Set the state of all binded outputs
// overrides ControlReference::State .. combined them so I could make the GUI simple / inputs ==
// same as outputs one list
// I was lazy and it works so watever
//
ControlState ControllerInterface::OutputReference::State(const ControlState state)
{
if (parsed_expression)
parsed_expression->SetValue(state);
return 0.0;
}
//
// UpdateReference
//
// Updates a controlreference's binded devices/controls
// need to call this to re-parse a control reference's expression after changing it
//
void ControllerInterface::UpdateReference(ControllerInterface::ControlReference* ref,
const ciface::Core::DeviceQualifier& default_device) const
{
delete ref->parsed_expression;
ref->parsed_expression = nullptr;
ControlFinder finder(*this, default_device, ref->is_input);
ref->parse_error = ParseExpression(ref->expression, finder, &ref->parsed_expression);
}
//
// InputReference :: Detect
//
// Wait for input on all binded devices
// supports not detecting inputs that were held down at the time of Detect start,
// which is useful for those crazy flightsticks that have certain buttons that are always held down
// or some crazy axes or something
// upon input, return pointer to detected Control
// else return nullptr
//
ciface::Core::Device::Control*
ControllerInterface::InputReference::Detect(const unsigned int ms,
ciface::Core::Device* const device)
{
unsigned int time = 0;
std::vector<bool> states(device->Inputs().size());
if (device->Inputs().size() == 0)
return nullptr;
// get starting state of all inputs,
// so we can ignore those that were activated at time of Detect start
std::vector<ciface::Core::Device::Input *>::const_iterator i = device->Inputs().begin(),
e = device->Inputs().end();
for (std::vector<bool>::iterator state = states.begin(); i != e; ++i)
*state++ = ((*i)->GetState() > (1 - INPUT_DETECT_THRESHOLD));
while (time < ms)
{
device->UpdateInput();
i = device->Inputs().begin();
for (std::vector<bool>::iterator state = states.begin(); i != e; ++i, ++state)
{
// detected an input
if ((*i)->IsDetectable() && (*i)->GetState() > INPUT_DETECT_THRESHOLD)
{
// input was released at some point during Detect call
// return the detected input
if (false == *state)
return *i;
}
else if ((*i)->GetState() < (1 - INPUT_DETECT_THRESHOLD))
{
*state = false;
}
}
Common::SleepCurrentThread(10);
time += 10;
}
// no input was detected
return nullptr;
}
//
// OutputReference :: Detect
//
// Totally different from the inputReference detect / I have them combined so it was simpler to make
// the GUI.
// The GUI doesn't know the difference between an input and an output / it's odd but I was lazy and
// it was easy
//
// set all binded outputs to <range> power for x milliseconds return false
//
ciface::Core::Device::Control*
ControllerInterface::OutputReference::Detect(const unsigned int ms,
ciface::Core::Device* const device)
{
// ignore device
// don't hang if we don't even have any controls mapped
if (BoundCount() > 0)
{
State(1);
unsigned int slept = 0;
// this loop is to make stuff like flashing keyboard LEDs work
while (ms > (slept += 10))
Common::SleepCurrentThread(10);
State(0);
}
return nullptr;
}
@@ -11,9 +11,7 @@
#include <vector>
#include "Common/CommonTypes.h"
#include "Common/Thread.h"
#include "InputCommon/ControllerInterface/Device.h"
#include "InputCommon/ControllerInterface/ExpressionParser.h"
// enable disable sources
#ifdef _WIN32
@@ -45,75 +43,6 @@
class ControllerInterface : public ciface::Core::DeviceContainer
{
public:
//
// ControlReference
//
// These are what you create to actually use the inputs, InputReference or OutputReference.
//
// After being bound to devices and controls with ControllerInterface::UpdateReference,
// each one can link to multiple devices and controls
// when you change a ControlReference's expression,
// you must use ControllerInterface::UpdateReference on it to rebind controls
//
class ControlReference
{
friend class ControllerInterface;
public:
virtual ControlState State(const ControlState state = 0) = 0;
virtual ciface::Core::Device::Control* Detect(const unsigned int ms,
ciface::Core::Device* const device) = 0;
ControlState range;
std::string expression;
const bool is_input;
ciface::ExpressionParser::ExpressionParseStatus parse_error;
virtual ~ControlReference() { delete parsed_expression; }
int BoundCount()
{
if (parsed_expression)
return parsed_expression->num_controls;
else
return 0;
}
protected:
ControlReference(const bool _is_input)
: range(1), is_input(_is_input), parsed_expression(nullptr)
{
}
ciface::ExpressionParser::Expression* parsed_expression;
};
//
// InputReference
//
// Control reference for inputs
//
class InputReference : public ControlReference
{
public:
InputReference() : ControlReference(true) {}
ControlState State(const ControlState state) override;
ciface::Core::Device::Control* Detect(const unsigned int ms,
ciface::Core::Device* const device) override;
};
//
// OutputReference
//
// Control reference for outputs
//
class OutputReference : public ControlReference
{
public:
OutputReference() : ControlReference(false) {}
ControlState State(const ControlState state) override;
ciface::Core::Device::Control* Detect(const unsigned int ms,
ciface::Core::Device* const device) override;
};
ControllerInterface() : m_is_init(false), m_hwnd(nullptr) {}
void Initialize(void* const hwnd);
void RefreshDevices();
@@ -121,8 +50,6 @@ public:
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;
void UpdateInput();
void RegisterHotplugCallback(std::function<void(void)> callback);
@@ -7,12 +7,6 @@
#include <string>
#include <tuple>
// For InputGateOn()
// This is a really bad layering violation, but it's the cleanest
// place I could find to put it.
#include "Core/ConfigManager.h"
#include "Core/Host.h"
#include "InputCommon/ControllerInterface/Device.h"
namespace ciface
@@ -67,16 +61,6 @@ Device::Output* Device::FindOutput(const std::string& name) const
return nullptr;
}
bool Device::Control::InputGateOn()
{
if (SConfig::GetInstance().m_BackgroundInput)
return true;
else if (Host_RendererHasFocus() || Host_UIHasFocus())
return true;
else
return false;
}
//
// DeviceQualifier :: ToString
//
@@ -42,8 +42,6 @@ public:
public:
virtual std::string GetName() const = 0;
virtual ~Control() {}
bool InputGateOn();
virtual Input* ToInput() { return nullptr; }
virtual Output* ToOutput() { return nullptr; }
};
@@ -59,15 +57,6 @@ public:
// things like absolute axes/ absolute mouse position will override this
virtual bool IsDetectable() { return true; }
virtual ControlState GetState() const = 0;
ControlState GetGatedState()
{
if (InputGateOn())
return GetState();
else
return 0.0;
}
Input* ToInput() override { return this; }
};
@@ -81,13 +70,6 @@ public:
public:
virtual ~Output() {}
virtual void SetState(ControlState state) = 0;
void SetGatedState(ControlState state)
{
if (InputGateOn())
SetState(state);
}
Output* ToOutput() override { return this; }
};
File diff suppressed because it is too large Load Diff
@@ -1,72 +0,0 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <memory>
#include <string>
#include "InputCommon/ControllerInterface/Device.h"
namespace ciface
{
namespace ExpressionParser
{
class ControlQualifier
{
public:
bool has_device;
Core::DeviceQualifier device_qualifier;
std::string control_name;
ControlQualifier() : has_device(false) {}
operator std::string()
{
if (has_device)
return device_qualifier.ToString() + ":" + control_name;
else
return control_name;
}
};
class ControlFinder
{
public:
ControlFinder(const Core::DeviceContainer& container_, const Core::DeviceQualifier& default_,
const bool is_input_)
: container(container_), default_device(default_), is_input(is_input_)
{
}
std::shared_ptr<Core::Device> FindDevice(ControlQualifier qualifier);
Core::Device::Control* FindControl(ControlQualifier qualifier);
private:
const Core::DeviceContainer& container;
const Core::DeviceQualifier& default_device;
bool is_input;
};
class ExpressionNode;
class Expression
{
public:
Expression() : node(nullptr) {}
Expression(ExpressionNode* node);
~Expression();
ControlState GetValue();
void SetValue(ControlState state);
int num_controls;
ExpressionNode* node;
};
enum ExpressionParseStatus
{
EXPRESSION_PARSE_SUCCESS = 0,
EXPRESSION_PARSE_SYNTAX_ERROR,
EXPRESSION_PARSE_NO_DEVICE,
};
ExpressionParseStatus ParseExpression(const std::string& expr, ControlFinder& finder,
Expression** expr_out);
}
}