2014-02-10 18:54:46 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-02-10 18:54:46 +00:00
|
|
|
// Refer to the license.txt file included.
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2017-11-09 20:14:21 +00:00
|
|
|
#include "InputCommon/ControllerInterface/Device.h"
|
|
|
|
|
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
|
|
|
|
2017-11-09 20:14:21 +00:00
|
|
|
#include "Common/StringUtil.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).
|
|
|
|
constexpr ControlState INPUT_DETECT_THRESHOLD = 0.55;
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
return StringFromFormat("%s/%i/%s", this->GetSource().c_str(), GetId(), this->GetName().c_str());
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
return "";
|
2014-01-31 00:51:21 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << source << '/';
|
2014-01-31 00:51:21 +00:00
|
|
|
if (cid > -1)
|
2013-06-17 00:07:10 +00:00
|
|
|
ss << cid;
|
|
|
|
ss << '/' << name;
|
2014-01-31 00:51:21 +00:00
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
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
|
|
|
{
|
2016-06-25 19:46:39 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
std::vector<std::string> DeviceContainer::GetAllDeviceStrings() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DeviceContainer::GetDefaultDeviceString() const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
if (m_devices.empty())
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-06-25 19:46:39 +00:00
|
|
|
std::lock_guard<std::mutex> lk(m_devices_mutex);
|
|
|
|
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
|
|
|
|
|
|
|
// Wait for input on a particular device.
|
|
|
|
// Inputs are 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*>
|
2019-05-29 23:23:51 +00:00
|
|
|
DeviceContainer::DetectInput(u32 wait_ms, const std::vector<std::string>& device_strings) const
|
2019-02-28 00:10:18 +00:00
|
|
|
{
|
|
|
|
struct InputState
|
|
|
|
{
|
|
|
|
ciface::Core::Device::Input& input;
|
|
|
|
ControlState initial_state;
|
|
|
|
};
|
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
// Don't detect things like absolute cursor position.
|
|
|
|
if (!input->IsDetectable())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Undesirable axes will have negative values here when trying to map a
|
|
|
|
// "FullAnalogSurface".
|
|
|
|
input_states.push_back({*input, input->GetState()});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!input_states.empty())
|
|
|
|
device_states.emplace_back(DeviceState{std::move(device), std::move(input_states)});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device_states.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
u32 time = 0;
|
|
|
|
while (time < wait_ms)
|
|
|
|
{
|
|
|
|
Common::SleepCurrentThread(10);
|
|
|
|
time += 10;
|
|
|
|
|
|
|
|
for (auto& device_state : device_states)
|
|
|
|
{
|
|
|
|
device_state.device->UpdateInput();
|
|
|
|
for (auto& input_state : device_state.input_states)
|
|
|
|
{
|
|
|
|
// 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};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No input was detected. :'(
|
|
|
|
return {};
|
|
|
|
}
|
2019-06-17 20:39:24 +00:00
|
|
|
} // namespace ciface::Core
|