2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2020-09-15 11:34:41 +00:00
|
|
|
#include "InputCommon/ControllerInterface/CoreDevice.h"
|
2017-11-09 20:14:21 +00:00
|
|
|
|
2019-05-08 19:02:26 +00:00
|
|
|
#include <algorithm>
|
2019-02-27 01:46:21 +00:00
|
|
|
#include <cmath>
|
2016-06-26 03:34:09 +00:00
|
|
|
#include <memory>
|
2013-06-17 00:07:10 +00:00
|
|
|
#include <sstream>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <string>
|
2016-06-26 02:59:07 +00:00
|
|
|
#include <tuple>
|
2014-02-17 10:18:15 +00:00
|
|
|
|
2019-11-22 19:10:28 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
#include "Common/MathUtil.h"
|
2019-02-28 00:10:18 +00:00
|
|
|
#include "Common/Thread.h"
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2019-06-17 20:39:24 +00:00
|
|
|
namespace ciface::Core
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
2019-02-28 00:10:18 +00:00
|
|
|
// Compared to an input's current state (ideally 1.0) minus abs(initial_state) (ideally 0.0).
|
2019-10-23 22:20:42 +00:00
|
|
|
// Note: Detect() logic assumes this is greater than 0.5.
|
2019-02-28 00:10:18 +00:00
|
|
|
constexpr ControlState INPUT_DETECT_THRESHOLD = 0.55;
|
|
|
|
|
2020-01-26 19:58:20 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
Device::~Device()
|
|
|
|
{
|
|
|
|
// delete inputs
|
2014-01-31 00:51:21 +00:00
|
|
|
for (Device::Input* input : m_inputs)
|
|
|
|
delete input;
|
2013-06-17 00:07:10 +00:00
|
|
|
|
|
|
|
// delete outputs
|
2014-01-31 00:51:21 +00:00
|
|
|
for (Device::Output* output : m_outputs)
|
|
|
|
delete output;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:57:37 +00:00
|
|
|
std::optional<int> Device::GetPreferredId() const
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
void Device::AddInput(Device::Input* const i)
|
|
|
|
{
|
|
|
|
m_inputs.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Device::AddOutput(Device::Output* const o)
|
|
|
|
{
|
|
|
|
m_outputs.push_back(o);
|
|
|
|
}
|
|
|
|
|
2017-11-09 20:14:21 +00:00
|
|
|
std::string Device::GetQualifiedName() const
|
|
|
|
{
|
2019-11-22 19:10:28 +00:00
|
|
|
return fmt::format("{}/{}/{}", GetSource(), GetId(), GetName());
|
2017-11-09 20:14:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 19:58:20 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
Device::Input* Device::FindInput(std::string_view name) const
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
for (Input* input : m_inputs)
|
|
|
|
{
|
2019-03-14 00:47:52 +00:00
|
|
|
if (input->IsMatchingName(name))
|
2014-01-31 00:51:21 +00:00
|
|
|
return input;
|
|
|
|
}
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
Device::Output* Device::FindOutput(std::string_view name) const
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
for (Output* output : m_outputs)
|
|
|
|
{
|
2019-03-14 00:47:52 +00:00
|
|
|
if (output->IsMatchingName(name))
|
2014-01-31 00:51:21 +00:00
|
|
|
return output;
|
|
|
|
}
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
bool Device::Control::IsMatchingName(std::string_view name) const
|
2019-03-14 00:47:52 +00:00
|
|
|
{
|
|
|
|
return GetName() == name;
|
|
|
|
}
|
|
|
|
|
2019-02-27 01:46:21 +00:00
|
|
|
ControlState Device::FullAnalogSurface::GetState() const
|
|
|
|
{
|
|
|
|
return (1 + std::max(0.0, m_high.GetState()) - std::max(0.0, m_low.GetState())) / 2;
|
|
|
|
}
|
|
|
|
|
2019-03-14 00:47:52 +00:00
|
|
|
std::string Device::FullAnalogSurface::GetName() const
|
|
|
|
{
|
|
|
|
// E.g. "Full Axis X+"
|
|
|
|
return "Full " + m_high.GetName();
|
|
|
|
}
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
bool Device::FullAnalogSurface::IsMatchingName(std::string_view name) const
|
2019-03-14 00:47:52 +00:00
|
|
|
{
|
|
|
|
if (Control::IsMatchingName(name))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Old naming scheme was "Axis X-+" which is too visually similar to "Axis X+".
|
|
|
|
// This has caused countless problems for users with mysterious misconfigurations.
|
|
|
|
// We match this old name to support old configurations.
|
|
|
|
const auto old_name = m_low.GetName() + *m_high.GetName().rbegin();
|
|
|
|
|
|
|
|
return old_name == name;
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:33:59 +00:00
|
|
|
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)}));
|
|
|
|
}
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// DeviceQualifier :: ToString
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Get string from a device qualifier / serialize
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
|
|
|
std::string DeviceQualifier::ToString() const
|
|
|
|
{
|
|
|
|
if (source.empty() && (cid < 0) && name.empty())
|
2023-06-18 00:23:49 +00:00
|
|
|
return {};
|
2014-01-31 00:51:21 +00:00
|
|
|
|
|
|
|
if (cid > -1)
|
2023-06-18 00:23:49 +00:00
|
|
|
return fmt::format("{}/{}/{}", source, cid, name);
|
|
|
|
else
|
|
|
|
return fmt::format("{}//{}", source, name);
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// DeviceQualifier :: FromString
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Set a device qualifier from a string / unserialize
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
|
|
|
void DeviceQualifier::FromString(const std::string& str)
|
|
|
|
{
|
2017-11-04 20:49:07 +00:00
|
|
|
*this = {};
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
std::istringstream ss(str);
|
|
|
|
|
2017-11-04 20:49:07 +00:00
|
|
|
std::getline(ss, source, '/');
|
2013-06-17 00:07:10 +00:00
|
|
|
|
|
|
|
// silly
|
|
|
|
std::getline(ss, name, '/');
|
2017-11-04 20:49:07 +00:00
|
|
|
std::istringstream(name) >> cid;
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2017-11-04 20:49:07 +00:00
|
|
|
std::getline(ss, name);
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// DeviceQualifier :: FromDevice
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Set a device qualifier from a device
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
|
|
|
void DeviceQualifier::FromDevice(const Device* const dev)
|
|
|
|
{
|
|
|
|
name = dev->GetName();
|
|
|
|
cid = dev->GetId();
|
|
|
|
source = dev->GetSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeviceQualifier::operator==(const Device* const dev) const
|
|
|
|
{
|
|
|
|
if (dev->GetId() == cid)
|
|
|
|
if (dev->GetName() == name)
|
|
|
|
if (dev->GetSource() == source)
|
|
|
|
return true;
|
2014-01-31 00:51:21 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-03 23:32:46 +00:00
|
|
|
bool DeviceQualifier::operator!=(const Device* const dev) const
|
|
|
|
{
|
|
|
|
return !operator==(dev);
|
|
|
|
}
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
bool DeviceQualifier::operator==(const DeviceQualifier& devq) const
|
|
|
|
{
|
2016-06-26 02:59:07 +00:00
|
|
|
return std::tie(cid, name, source) == std::tie(devq.cid, devq.name, devq.source);
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 23:32:46 +00:00
|
|
|
bool DeviceQualifier::operator!=(const DeviceQualifier& devq) const
|
|
|
|
{
|
|
|
|
return !operator==(devq);
|
|
|
|
}
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
std::shared_ptr<Device> DeviceContainer::FindDevice(const DeviceQualifier& devq) const
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
2016-06-25 19:46:39 +00:00
|
|
|
for (const auto& d : m_devices)
|
2014-01-31 00:51:21 +00:00
|
|
|
{
|
2016-06-25 19:46:39 +00:00
|
|
|
if (devq == d.get())
|
2014-01-31 00:51:21 +00:00
|
|
|
return d;
|
|
|
|
}
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 21:02:30 +00:00
|
|
|
std::vector<std::shared_ptr<Device>> DeviceContainer::GetAllDevices() const
|
|
|
|
{
|
|
|
|
std::lock_guard lk(m_devices_mutex);
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<Device>> devices;
|
|
|
|
|
|
|
|
for (const auto& d : m_devices)
|
|
|
|
devices.emplace_back(d);
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
}
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
std::vector<std::string> DeviceContainer::GetAllDeviceStrings() const
|
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
2016-06-25 19:46:39 +00:00
|
|
|
|
|
|
|
std::vector<std::string> device_strings;
|
|
|
|
DeviceQualifier device_qualifier;
|
|
|
|
|
|
|
|
for (const auto& d : m_devices)
|
|
|
|
{
|
|
|
|
device_qualifier.FromDevice(d.get());
|
|
|
|
device_strings.emplace_back(device_qualifier.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return device_strings;
|
|
|
|
}
|
|
|
|
|
InputCommon: fix default input config default device not being loaded/found
Fixes bug: https://bugs.dolphin-emu.org/issues/12744
Before https://github.com/dolphin-emu/dolphin/commit/e1e3db13baabefa89991388d37db0bb260c4f535
the ControllerInterface m_devices_mutex was "wrongfully" locked for the whole Initialize() call, which included the first device population refresh,
this has the unwanted (accidental) consequence of often preventing the different pads (GC Pad, Wii Contollers, ...) input configs from loading
until that mutex was released (the input config defaults loading was blocked in EmulatedController::LoadDefaults()), which meant that the devices
population would often have the time to finish adding its first device, which would then be selected as default device (by design, the first device
added to the CI is the default default device, usually the "Keyboard and Mouse" device).
After the commit mentioned above removed the unnecessary m_devices_mutex calls, the default default device would fail to load (be found)
causing the default input mappings, which are specifically written for the default default device on every platform, to not be bound to any
physical device input, breaking input on new dolphin installations (until a user tried to customize the default device manually).
Default devices are now always added synchronously to avoid the problem, and so they should in the future (I added comments and warnings to help with that)
2021-11-27 12:31:04 +00:00
|
|
|
bool DeviceContainer::HasDefaultDevice() const
|
|
|
|
{
|
|
|
|
std::lock_guard lk(m_devices_mutex);
|
|
|
|
// Devices are already sorted by priority
|
|
|
|
return !m_devices.empty() && m_devices[0]->GetSortPriority() >= 0;
|
|
|
|
}
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
std::string DeviceContainer::GetDefaultDeviceString() const
|
|
|
|
{
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
InputCommon: fix default input config default device not being loaded/found
Fixes bug: https://bugs.dolphin-emu.org/issues/12744
Before https://github.com/dolphin-emu/dolphin/commit/e1e3db13baabefa89991388d37db0bb260c4f535
the ControllerInterface m_devices_mutex was "wrongfully" locked for the whole Initialize() call, which included the first device population refresh,
this has the unwanted (accidental) consequence of often preventing the different pads (GC Pad, Wii Contollers, ...) input configs from loading
until that mutex was released (the input config defaults loading was blocked in EmulatedController::LoadDefaults()), which meant that the devices
population would often have the time to finish adding its first device, which would then be selected as default device (by design, the first device
added to the CI is the default default device, usually the "Keyboard and Mouse" device).
After the commit mentioned above removed the unnecessary m_devices_mutex calls, the default default device would fail to load (be found)
causing the default input mappings, which are specifically written for the default default device on every platform, to not be bound to any
physical device input, breaking input on new dolphin installations (until a user tried to customize the default device manually).
Default devices are now always added synchronously to avoid the problem, and so they should in the future (I added comments and warnings to help with that)
2021-11-27 12:31:04 +00:00
|
|
|
// Devices are already sorted by priority
|
|
|
|
if (m_devices.empty() || m_devices[0]->GetSortPriority() < 0)
|
2016-06-25 19:46:39 +00:00
|
|
|
return "";
|
|
|
|
|
|
|
|
DeviceQualifier device_qualifier;
|
|
|
|
device_qualifier.FromDevice(m_devices[0].get());
|
|
|
|
return device_qualifier.ToString();
|
|
|
|
}
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
Device::Input* DeviceContainer::FindInput(std::string_view name, const Device* def_dev) const
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
|
|
|
if (def_dev)
|
|
|
|
{
|
|
|
|
Device::Input* const inp = def_dev->FindInput(name);
|
|
|
|
if (inp)
|
|
|
|
return inp;
|
|
|
|
}
|
|
|
|
|
2020-01-22 00:52:58 +00:00
|
|
|
std::lock_guard lk(m_devices_mutex);
|
2016-06-25 19:46:39 +00:00
|
|
|
for (const auto& d : m_devices)
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
2014-01-31 00:51:21 +00:00
|
|
|
Device::Input* const i = d->FindInput(name);
|
2013-06-17 00:07:10 +00:00
|
|
|
|
|
|
|
if (i)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2014-03-09 20:14:26 +00:00
|
|
|
return nullptr;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 22:42:22 +00:00
|
|
|
Device::Output* DeviceContainer::FindOutput(std::string_view name, const Device* def_dev) const
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
|
|
|
return def_dev->FindOutput(name);
|
|
|
|
}
|
2017-02-08 00:25:27 +00:00
|
|
|
|
|
|
|
bool DeviceContainer::HasConnectedDevice(const DeviceQualifier& qualifier) const
|
|
|
|
{
|
|
|
|
const auto device = FindDevice(qualifier);
|
|
|
|
return device != nullptr && device->IsValid();
|
|
|
|
}
|
2019-02-28 00:10:18 +00:00
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
// Wait for inputs on supplied devices.
|
|
|
|
// Inputs are only considered if they are first seen in a neutral state.
|
2019-02-28 00:10:18 +00:00
|
|
|
// This is useful for crazy flightsticks that have certain buttons that are always held down
|
|
|
|
// and also properly handles detection when using "FullAnalogSurface" inputs.
|
2020-02-12 22:49:17 +00:00
|
|
|
// 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>
|
2019-02-28 00:10:18 +00:00
|
|
|
{
|
|
|
|
struct InputState
|
|
|
|
{
|
2020-02-12 22:49:17 +00:00
|
|
|
InputState(ciface::Core::Device::Input* input_) : input{input_} { stats.Push(0.0); }
|
|
|
|
|
2019-10-23 22:20:42 +00:00
|
|
|
ciface::Core::Device::Input* input;
|
2020-02-12 22:49:17 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-02-28 00:10:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct DeviceState
|
|
|
|
{
|
|
|
|
std::shared_ptr<Device> device;
|
|
|
|
|
|
|
|
std::vector<InputState> input_states;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Acquire devices and initial input states.
|
|
|
|
std::vector<DeviceState> device_states;
|
2019-05-29 23:10:50 +00:00
|
|
|
for (const auto& device_string : device_strings)
|
2019-02-28 00:10:18 +00:00
|
|
|
{
|
|
|
|
DeviceQualifier dq;
|
|
|
|
dq.FromString(device_string);
|
|
|
|
auto device = FindDevice(dq);
|
|
|
|
|
|
|
|
if (!device)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<InputState> input_states;
|
|
|
|
|
|
|
|
for (auto* input : device->Inputs())
|
|
|
|
{
|
2020-02-12 22:49:17 +00:00
|
|
|
// Don't detect things like absolute cursor positions, accelerometers, or gyroscopes.
|
2019-02-28 00:10:18 +00:00
|
|
|
if (!input->IsDetectable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Undesirable axes will have negative values here when trying to map a
|
|
|
|
// "FullAnalogSurface".
|
2020-02-12 22:49:17 +00:00
|
|
|
input_states.push_back(InputState{input});
|
2019-02-28 00:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!input_states.empty())
|
|
|
|
device_states.emplace_back(DeviceState{std::move(device), std::move(input_states)});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device_states.empty())
|
|
|
|
return {};
|
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
std::vector<InputDetection> detections;
|
2019-10-23 22:20:42 +00:00
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
const auto start_time = Clock::now();
|
|
|
|
while (true)
|
2019-02-28 00:10:18 +00:00
|
|
|
{
|
2020-02-12 22:49:17 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
Common::SleepCurrentThread(10);
|
|
|
|
|
|
|
|
for (auto& device_state : device_states)
|
|
|
|
{
|
2019-10-23 22:20:42 +00:00
|
|
|
for (std::size_t i = 0; i != device_state.input_states.size(); ++i)
|
2019-02-28 00:10:18 +00:00
|
|
|
{
|
2019-10-23 22:20:42 +00:00
|
|
|
auto& input_state = device_state.input_states[i];
|
2020-02-12 22:49:17 +00:00
|
|
|
input_state.Update();
|
2019-10-23 22:20:42 +00:00
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
if (input_state.IsPressed())
|
2019-10-23 22:20:42 +00:00
|
|
|
{
|
2020-02-12 22:49:17 +00:00
|
|
|
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;
|
2019-10-23 22:20:42 +00:00
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
// We found an input. Add it to our detections.
|
|
|
|
detections.emplace_back(std::move(new_detection));
|
2019-10-23 22:20:42 +00:00
|
|
|
}
|
2019-02-28 00:10:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-23 22:20:42 +00:00
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
// Check for any releases of our detected inputs.
|
|
|
|
for (auto& d : detections)
|
2019-10-23 22:20:42 +00:00
|
|
|
{
|
2020-02-12 22:49:17 +00:00
|
|
|
if (!d.release_time.has_value() && d.input->GetState() < (1 - INPUT_DETECT_THRESHOLD))
|
|
|
|
d.release_time = Clock::now();
|
2019-10-23 22:20:42 +00:00
|
|
|
}
|
2019-02-28 00:10:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 22:49:17 +00:00
|
|
|
return detections;
|
2019-02-28 00:10:18 +00:00
|
|
|
}
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::Core
|