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-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
|
|
|
|
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace Core
|
|
|
|
{
|
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()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// delete inputs
|
|
|
|
for (Device::Input* input : m_inputs)
|
|
|
|
delete input;
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// delete outputs
|
|
|
|
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)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_inputs.push_back(i);
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Device::AddOutput(Device::Output* const o)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
m_outputs.push_back(o);
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Device::Input* Device::FindInput(const std::string& name) const
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (Input* input : m_inputs)
|
|
|
|
{
|
|
|
|
if (input->GetName() == name)
|
|
|
|
return input;
|
|
|
|
}
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return nullptr;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Device::Output* Device::FindOutput(const std::string& name) const
|
2013-06-17 00:07:10 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
for (Output* output : m_outputs)
|
|
|
|
{
|
|
|
|
if (output->GetName() == name)
|
|
|
|
return output;
|
|
|
|
}
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return nullptr;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (source.empty() && (cid < 0) && name.empty())
|
|
|
|
return "";
|
2014-01-31 00:51:21 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::ostringstream ss;
|
|
|
|
ss << source << '/';
|
|
|
|
if (cid > -1)
|
|
|
|
ss << cid;
|
|
|
|
ss << '/' << name;
|
2014-01-31 00:51:21 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return ss.str();
|
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 = {};
|
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
std::istringstream ss(str);
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2017-11-04 20:49:07 +00:00
|
|
|
std::getline(ss, source, '/');
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +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)
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
name = dev->GetName();
|
|
|
|
cid = dev->GetId();
|
|
|
|
source = dev->GetSource();
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeviceQualifier::operator==(const Device* const dev) const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (dev->GetId() == cid)
|
|
|
|
if (dev->GetName() == name)
|
|
|
|
if (dev->GetSource() == source)
|
|
|
|
return true;
|
2014-01-31 00:51:21 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return false;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
2016-06-25 19:46:39 +00:00
|
|
|
if (devq == d.get())
|
2016-06-24 08:43:46 +00:00
|
|
|
return d;
|
|
|
|
}
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +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();
|
|
|
|
}
|
|
|
|
|
2013-06-17 00:07:10 +00:00
|
|
|
Device::Input* DeviceContainer::FindInput(const std::string& name, const Device* def_dev) const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
if (def_dev)
|
|
|
|
{
|
|
|
|
Device::Input* const inp = def_dev->FindInput(name);
|
|
|
|
if (inp)
|
|
|
|
return inp;
|
|
|
|
}
|
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)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
Device::Input* const i = d->FindInput(name);
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (i)
|
|
|
|
return i;
|
|
|
|
}
|
2013-06-17 00:07:10 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
return nullptr;
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Device::Output* DeviceContainer::FindOutput(const std::string& name, const Device* def_dev) const
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
return def_dev->FindOutput(name);
|
2013-06-17 00:07:10 +00:00
|
|
|
}
|
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*>
|
|
|
|
DeviceContainer::DetectInput(u32 wait_ms, std::vector<std::string> device_strings)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
for (auto& device_string : device_strings)
|
|
|
|
{
|
|
|
|
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-02-27 01:46:21 +00:00
|
|
|
} // namespace Core
|
|
|
|
} // namespace ciface
|