| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-26 05:34:09 +02:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | #include <sstream>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2016-06-25 22:59:07 -04:00
										 |  |  | #include <tuple>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "InputCommon/ControllerInterface/Device.h"
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace ciface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | namespace Core | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // Device :: ~Device
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Destructor, delete all inputs/outputs on device destruction
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | Device::~Device() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // delete inputs
 | 
					
						
							|  |  |  |   for (Device::Input* input : m_inputs) | 
					
						
							|  |  |  |     delete input; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // delete outputs
 | 
					
						
							|  |  |  |   for (Device::Output* output : m_outputs) | 
					
						
							|  |  |  |     delete output; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Device::AddInput(Device::Input* const i) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_inputs.push_back(i); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Device::AddOutput(Device::Output* const o) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   m_outputs.push_back(o); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | Device::Input* Device::FindInput(const std::string& name) const | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (Input* input : m_inputs) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (input->GetName() == name) | 
					
						
							|  |  |  |       return input; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | Device::Output* Device::FindOutput(const std::string& name) const | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (Output* output : m_outputs) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (output->GetName() == name) | 
					
						
							|  |  |  |       return output; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // DeviceQualifier :: ToString
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // Get string from a device qualifier / serialize
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							|  |  |  | std::string DeviceQualifier::ToString() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (source.empty() && (cid < 0) && name.empty()) | 
					
						
							|  |  |  |     return ""; | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::ostringstream ss; | 
					
						
							|  |  |  |   ss << source << '/'; | 
					
						
							|  |  |  |   if (cid > -1) | 
					
						
							|  |  |  |     ss << cid; | 
					
						
							|  |  |  |   ss << '/' << name; | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return ss.str(); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // DeviceQualifier :: FromString
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // Set a device qualifier from a string / unserialize
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							|  |  |  | void DeviceQualifier::FromString(const std::string& str) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::istringstream ss(str); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::getline(ss, source = "", '/'); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   // silly
 | 
					
						
							|  |  |  |   std::getline(ss, name, '/'); | 
					
						
							|  |  |  |   std::istringstream(name) >> (cid = -1); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::getline(ss, name = ""); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // DeviceQualifier :: FromDevice
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // Set a device qualifier from a device
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							|  |  |  | void DeviceQualifier::FromDevice(const Device* const dev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   name = dev->GetName(); | 
					
						
							|  |  |  |   cid = dev->GetId(); | 
					
						
							|  |  |  |   source = dev->GetSource(); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DeviceQualifier::operator==(const Device* const dev) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (dev->GetId() == cid) | 
					
						
							|  |  |  |     if (dev->GetName() == name) | 
					
						
							|  |  |  |       if (dev->GetSource() == source) | 
					
						
							|  |  |  |         return true; | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DeviceQualifier::operator==(const DeviceQualifier& devq) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-25 22:59:07 -04:00
										 |  |  |   return std::tie(cid, name, source) == std::tie(devq.cid, devq.name, devq.source); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  | std::shared_ptr<Device> DeviceContainer::FindDevice(const DeviceQualifier& devq) const | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |   std::lock_guard<std::mutex> lk(m_devices_mutex); | 
					
						
							|  |  |  |   for (const auto& d : m_devices) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |     if (devq == d.get()) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |       return d; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02: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-16 20:07:10 -04:00
										 |  |  | Device::Input* DeviceContainer::FindInput(const std::string& name, const Device* def_dev) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (def_dev) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     Device::Input* const inp = def_dev->FindInput(name); | 
					
						
							|  |  |  |     if (inp) | 
					
						
							|  |  |  |       return inp; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |   std::lock_guard<std::mutex> lk(m_devices_mutex); | 
					
						
							|  |  |  |   for (const auto& d : m_devices) | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     Device::Input* const i = d->FindInput(name); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     if (i) | 
					
						
							|  |  |  |       return i; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Device::Output* DeviceContainer::FindOutput(const std::string& name, const Device* def_dev) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return def_dev->FindOutput(name); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } |