| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-11 10:53:51 -04:00
										 |  |  | // For InputGateOn()
 | 
					
						
							|  |  |  | // This is a really bad layering violation, but it's the cleanest
 | 
					
						
							|  |  |  | // place I could find to put it.
 | 
					
						
							|  |  |  | #include "Core/ConfigManager.h"
 | 
					
						
							|  |  |  | #include "Core/Host.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// delete inputs
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 	for (Device::Input* input : m_inputs) | 
					
						
							|  |  |  | 		delete input; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// delete outputs
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 	for (Device::Output* output: m_outputs) | 
					
						
							|  |  |  | 		delete output; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04: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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Device::Input* Device::FindInput(const std::string &name) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 	for (Input* input : m_inputs) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (input->GetName() == name) | 
					
						
							|  |  |  | 			return input; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Device::Output* Device::FindOutput(const std::string &name) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 	for (Output* output : m_outputs) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (output->GetName() == name) | 
					
						
							|  |  |  | 			return output; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // Device :: ClearInputState
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Device classes should override this function
 | 
					
						
							|  |  |  | // ControllerInterface will call this when the device returns failure during UpdateInput
 | 
					
						
							|  |  |  | // used to try to set all buttons and axes to their default state when user unplugs a gamepad during play
 | 
					
						
							|  |  |  | // buttons/axes that were held down at the time of unplugging should be seen as not pressed after unplugging
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | void Device::ClearInputState() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// this is going to be called for every UpdateInput call that fails
 | 
					
						
							|  |  |  | 	// kinda slow but, w/e, should only happen when user unplugs a device while playing
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-11 10:53:51 -04:00
										 |  |  | bool Device::Control::InputGateOn() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (SConfig::GetInstance().m_BackgroundInput) | 
					
						
							|  |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2014-07-16 10:24:40 -04:00
										 |  |  | 	else if (Host_RendererHasFocus() || Host_UIHasFocus()) | 
					
						
							| 
									
										
										
										
											2014-07-11 10:53:51 -04:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (source.empty() && (cid < 0) && name.empty()) | 
					
						
							|  |  |  | 		return ""; | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 	std::ostringstream ss; | 
					
						
							|  |  |  | 	ss << source << '/'; | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 	if (cid > -1) | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 		ss << cid; | 
					
						
							|  |  |  | 	ss << '/' << name; | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 	return ss.str(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::istringstream ss(str); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::getline(ss, source = "", '/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// silly
 | 
					
						
							|  |  |  | 	std::getline(ss, name, '/'); | 
					
						
							|  |  |  | 	std::istringstream(name) >> (cid = -1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::getline(ss, name = ""); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	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-30 19:51:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DeviceQualifier::operator==(const DeviceQualifier& devq) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (cid == devq.cid) | 
					
						
							|  |  |  | 		if (name == devq.name) | 
					
						
							|  |  |  | 			if (source == devq.source) | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Device* DeviceContainer::FindDevice(const DeviceQualifier& devq) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 	for (Device* d : m_devices) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (devq == d) | 
					
						
							|  |  |  | 			return d; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Device::Input* DeviceContainer::FindInput(const std::string& name, const Device* def_dev) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (def_dev) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		Device::Input* const inp = def_dev->FindInput(name); | 
					
						
							|  |  |  | 		if (inp) | 
					
						
							|  |  |  | 			return inp; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 	for (Device* d : m_devices) | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | 		Device::Input* const i = d->FindInput(name); | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (i) | 
					
						
							|  |  |  | 			return i; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	return nullptr; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Device::Output* DeviceContainer::FindOutput(const std::string& name, const Device* def_dev) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return def_dev->FindOutput(name); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } |