summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerInterface/Device.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Core/InputCommon/ControllerInterface/Device.cpp')
-rw-r--r--Source/Core/InputCommon/ControllerInterface/Device.cpp163
1 files changed, 142 insertions, 21 deletions
diff --git a/Source/Core/InputCommon/ControllerInterface/Device.cpp b/Source/Core/InputCommon/ControllerInterface/Device.cpp
index ebed1ad421..2fd7fb02e7 100644
--- a/Source/Core/InputCommon/ControllerInterface/Device.cpp
+++ b/Source/Core/InputCommon/ControllerInterface/Device.cpp
@@ -13,13 +13,47 @@
#include <fmt/format.h>
+#include "Common/MathUtil.h"
#include "Common/Thread.h"
namespace ciface::Core
{
// Compared to an input's current state (ideally 1.0) minus abs(initial_state) (ideally 0.0).
+// Note: Detect() logic assumes this is greater than 0.5.
constexpr ControlState INPUT_DETECT_THRESHOLD = 0.55;
+class CombinedInput final : public Device::Input
+{
+public:
+ using Inputs = std::pair<Device::Input*, Device::Input*>;
+
+ CombinedInput(std::string name, const Inputs& inputs) : m_name(std::move(name)), m_inputs(inputs)
+ {
+ }
+ ControlState GetState() const override
+ {
+ ControlState result = 0;
+
+ if (m_inputs.first)
+ result = m_inputs.first->GetState();
+
+ if (m_inputs.second)
+ result = std::max(result, m_inputs.second->GetState());
+
+ return result;
+ }
+ std::string GetName() const override { return m_name; }
+ bool IsDetectable() const override { return false; }
+ bool IsChild(const Input* input) const override
+ {
+ return m_inputs.first == input || m_inputs.second == input;
+ }
+
+private:
+ const std::string m_name;
+ const std::pair<Device::Input*, Device::Input*> m_inputs;
+};
+
Device::~Device()
{
// delete inputs
@@ -51,6 +85,20 @@ std::string Device::GetQualifiedName() const
return fmt::format("{}/{}/{}", GetSource(), GetId(), GetName());
}
+auto Device::GetParentMostInput(Input* child) const -> Input*
+{
+ for (auto* input : m_inputs)
+ {
+ if (input->IsChild(child))
+ {
+ // Running recursively is currently unnecessary but it doesn't hurt.
+ return GetParentMostInput(input);
+ }
+ }
+
+ return child;
+}
+
Device::Input* Device::FindInput(std::string_view name) const
{
for (Input* input : m_inputs)
@@ -102,6 +150,11 @@ bool Device::FullAnalogSurface::IsMatchingName(std::string_view name) const
return old_name == name;
}
+void Device::AddCombinedInput(std::string name, const std::pair<std::string, std::string>& inputs)
+{
+ AddInput(new CombinedInput(std::move(name), {FindInput(inputs.first), FindInput(inputs.second)}));
+}
+
//
// DeviceQualifier :: ToString
//
@@ -249,18 +302,54 @@ bool DeviceContainer::HasConnectedDevice(const DeviceQualifier& qualifier) const
return device != nullptr && device->IsValid();
}
-// Wait for input on a particular device.
-// Inputs are considered if they are first seen in a neutral state.
+// Wait for inputs on supplied devices.
+// Inputs are only considered if they are first seen in a neutral state.
// This is useful for crazy flightsticks that have certain buttons that are always held down
// and also properly handles detection when using "FullAnalogSurface" inputs.
-// Upon input, return the detected Device and Input, else return nullptrs
-std::pair<std::shared_ptr<Device>, Device::Input*>
-DeviceContainer::DetectInput(u32 wait_ms, const std::vector<std::string>& device_strings) const
+// Multiple detections are returned until the various timeouts have been reached.
+auto DeviceContainer::DetectInput(const std::vector<std::string>& device_strings,
+ std::chrono::milliseconds initial_wait,
+ std::chrono::milliseconds confirmation_wait,
+ std::chrono::milliseconds maximum_wait) const
+ -> std::vector<InputDetection>
{
struct InputState
{
- ciface::Core::Device::Input& input;
- ControlState initial_state;
+ InputState(ciface::Core::Device::Input* input_) : input{input_} { stats.Push(0.0); }
+
+ ciface::Core::Device::Input* input;
+ ControlState initial_state = input->GetState();
+ ControlState last_state = initial_state;
+ MathUtil::RunningVariance<ControlState> stats;
+
+ // Prevent multiiple detections until after release.
+ bool is_ready = true;
+
+ void Update()
+ {
+ const auto new_state = input->GetState();
+
+ if (!is_ready && new_state < (1 - INPUT_DETECT_THRESHOLD))
+ {
+ last_state = new_state;
+ is_ready = true;
+ stats.Clear();
+ }
+
+ const auto difference = new_state - last_state;
+ stats.Push(difference);
+ last_state = new_state;
+ }
+
+ bool IsPressed()
+ {
+ if (!is_ready)
+ return false;
+
+ // We want an input that was initially 0.0 and currently 1.0.
+ const auto detection_score = (last_state - std::abs(initial_state));
+ return detection_score > INPUT_DETECT_THRESHOLD;
+ }
};
struct DeviceState
@@ -285,13 +374,13 @@ DeviceContainer::DetectInput(u32 wait_ms, const std::vector<std::string>& device
for (auto* input : device->Inputs())
{
- // Don't detect things like absolute cursor position.
+ // Don't detect things like absolute cursor positions, accelerometers, or gyroscopes.
if (!input->IsDetectable())
continue;
// Undesirable axes will have negative values here when trying to map a
// "FullAnalogSurface".
- input_states.push_back({*input, input->GetState()});
+ input_states.push_back(InputState{input});
}
if (!input_states.empty())
@@ -301,27 +390,59 @@ DeviceContainer::DetectInput(u32 wait_ms, const std::vector<std::string>& device
if (device_states.empty())
return {};
- u32 time = 0;
- while (time < wait_ms)
+ std::vector<InputDetection> detections;
+
+ const auto start_time = Clock::now();
+ while (true)
{
+ const auto now = Clock::now();
+ const auto elapsed_time = now - start_time;
+
+ if (elapsed_time >= maximum_wait || (detections.empty() && elapsed_time >= initial_wait) ||
+ (!detections.empty() && detections.back().release_time.has_value() &&
+ now >= *detections.back().release_time + confirmation_wait))
+ {
+ break;
+ }
+
Common::SleepCurrentThread(10);
- time += 10;
for (auto& device_state : device_states)
{
- for (auto& input_state : device_state.input_states)
+ for (std::size_t i = 0; i != device_state.input_states.size(); ++i)
{
- // We want an input that was initially 0.0 and currently 1.0.
- const auto detection_score =
- (input_state.input.GetState() - std::abs(input_state.initial_state));
-
- if (detection_score > INPUT_DETECT_THRESHOLD)
- return {device_state.device, &input_state.input};
+ auto& input_state = device_state.input_states[i];
+ input_state.Update();
+
+ if (input_state.IsPressed())
+ {
+ input_state.is_ready = false;
+
+ // Digital presses will evaluate as 1 here.
+ // Analog presses will evaluate greater than 1.
+ const auto smoothness =
+ 1 / std::sqrt(input_state.stats.Variance() / input_state.stats.Mean());
+
+ InputDetection new_detection;
+ new_detection.device = device_state.device;
+ new_detection.input = input_state.input;
+ new_detection.press_time = Clock::now();
+ new_detection.smoothness = smoothness;
+
+ // We found an input. Add it to our detections.
+ detections.emplace_back(std::move(new_detection));
+ }
}
}
+
+ // Check for any releases of our detected inputs.
+ for (auto& d : detections)
+ {
+ if (!d.release_time.has_value() && d.input->GetState() < (1 - INPUT_DETECT_THRESHOLD))
+ d.release_time = Clock::now();
+ }
}
- // No input was detected. :'(
- return {};
+ return detections;
}
} // namespace ciface::Core