| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-10 13:54:46 -05:00
										 |  |  | #pragma once
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-26 05:34:09 +02:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2016-06-12 17:31:41 +02:00
										 |  |  | #include <mutex>
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 20:06:58 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | // idk in case I wanted to change it to double or something, idk what's best
 | 
					
						
							| 
									
										
										
										
											2014-08-11 13:43:26 -04:00
										 |  |  | typedef double ControlState; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace ciface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | namespace Core | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | // Forward declarations
 | 
					
						
							|  |  |  | class DeviceQualifier; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // Device
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // A device class
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							|  |  |  | class Device | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   class Input; | 
					
						
							|  |  |  |   class Output; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // Control
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // Control includes inputs and outputs
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   class Control  // input or output
 | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     virtual std::string GetName() const = 0; | 
					
						
							|  |  |  |     virtual ~Control() {} | 
					
						
							|  |  |  |     bool InputGateOn(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual Input* ToInput() { return nullptr; } | 
					
						
							|  |  |  |     virtual Output* ToOutput() { return nullptr; } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // Input
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // An input on a device
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   class Input : public Control | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     // things like absolute axes/ absolute mouse position will override this
 | 
					
						
							|  |  |  |     virtual bool IsDetectable() { return true; } | 
					
						
							|  |  |  |     virtual ControlState GetState() const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ControlState GetGatedState() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (InputGateOn()) | 
					
						
							|  |  |  |         return GetState(); | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         return 0.0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Input* ToInput() override { return this; } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // Output
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   // An output on a device
 | 
					
						
							|  |  |  |   //
 | 
					
						
							|  |  |  |   class Output : public Control | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     virtual ~Output() {} | 
					
						
							|  |  |  |     virtual void SetState(ControlState state) = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void SetGatedState(ControlState state) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (InputGateOn()) | 
					
						
							|  |  |  |         SetState(state); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Output* ToOutput() override { return this; } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   virtual ~Device(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-14 10:25:52 +02:00
										 |  |  |   int GetId() const { return m_id; } | 
					
						
							|  |  |  |   void SetId(int id) { m_id = id; } | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   virtual std::string GetName() const = 0; | 
					
						
							|  |  |  |   virtual std::string GetSource() const = 0; | 
					
						
							|  |  |  |   virtual void UpdateInput() {} | 
					
						
							| 
									
										
										
										
											2016-07-14 17:45:59 +02:00
										 |  |  |   virtual bool IsValid() const { return true; } | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   const std::vector<Input*>& Inputs() const { return m_inputs; } | 
					
						
							|  |  |  |   const std::vector<Output*>& Outputs() const { return m_outputs; } | 
					
						
							|  |  |  |   Input* FindInput(const std::string& name) const; | 
					
						
							|  |  |  |   Output* FindOutput(const std::string& name) const; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   void AddInput(Input* const i); | 
					
						
							|  |  |  |   void AddOutput(Output* const o); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   class FullAnalogSurface : public Input | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   public: | 
					
						
							|  |  |  |     FullAnalogSurface(Input* low, Input* high) : m_low(*low), m_high(*high) {} | 
					
						
							|  |  |  |     ControlState GetState() const override | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       return (1 + m_high.GetState() - m_low.GetState()) / 2; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     std::string GetName() const override { return m_low.GetName() + *m_high.GetName().rbegin(); } | 
					
						
							|  |  |  |   private: | 
					
						
							|  |  |  |     Input& m_low; | 
					
						
							|  |  |  |     Input& m_high; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void AddAnalogInputs(Input* low, Input* high) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     AddInput(low); | 
					
						
							|  |  |  |     AddInput(high); | 
					
						
							|  |  |  |     AddInput(new FullAnalogSurface(low, high)); | 
					
						
							|  |  |  |     AddInput(new FullAnalogSurface(high, low)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2016-07-14 10:25:52 +02:00
										 |  |  |   int m_id; | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::vector<Input*> m_inputs; | 
					
						
							|  |  |  |   std::vector<Output*> m_outputs; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // DeviceQualifier
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2014-01-30 19:51:21 -05:00
										 |  |  | // Device qualifier used to match devices.
 | 
					
						
							|  |  |  | // Currently has ( source, id, name ) properties which match a device
 | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | //
 | 
					
						
							|  |  |  | class DeviceQualifier | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   DeviceQualifier() : cid(-1) {} | 
					
						
							|  |  |  |   DeviceQualifier(const std::string& _source, const int _id, const std::string& _name) | 
					
						
							|  |  |  |       : source(_source), cid(_id), name(_name) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   void FromDevice(const Device* const dev); | 
					
						
							|  |  |  |   void FromString(const std::string& str); | 
					
						
							|  |  |  |   std::string ToString() const; | 
					
						
							|  |  |  |   bool operator==(const DeviceQualifier& devq) const; | 
					
						
							|  |  |  |   bool operator==(const Device* const dev) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   std::string source; | 
					
						
							|  |  |  |   int cid; | 
					
						
							|  |  |  |   std::string name; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DeviceContainer | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   Device::Input* FindInput(const std::string& name, const Device* def_dev) const; | 
					
						
							|  |  |  |   Device::Output* FindOutput(const std::string& name, const Device* def_dev) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |   std::vector<std::string> GetAllDeviceStrings() const; | 
					
						
							|  |  |  |   std::string GetDefaultDeviceString() const; | 
					
						
							|  |  |  |   std::shared_ptr<Device> FindDevice(const DeviceQualifier& devq) const; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2016-06-25 21:46:39 +02:00
										 |  |  |   mutable std::mutex m_devices_mutex; | 
					
						
							|  |  |  |   std::vector<std::shared_ptr<Device>> m_devices; | 
					
						
							| 
									
										
										
										
											2013-06-16 20:07:10 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } |