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"
|
2013-06-17 00:07:10 +00:00
|
|
|
|
|
|
|
namespace ciface
|
|
|
|
{
|
|
|
|
namespace Core
|
|
|
|
{
|
|
|
|
//
|
2014-01-31 00:51:21 +00:00
|
|
|
// Device :: ~Device
|
2013-06-17 00:07:10 +00:00
|
|
|
//
|
|
|
|
// Destructor, delete all inputs/outputs on device destruction
|
|
|
|
//
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-27 01:46:21 +00:00
|
|
|
} // namespace Core
|
|
|
|
} // namespace ciface
|