| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | #ifndef _DEVICEINTERFACE_H_
 | 
					
						
							|  |  |  | #define _DEVICEINTERFACE_H_
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <sstream>
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2010-07-26 05:30:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-10 20:44:56 +00:00
										 |  |  | #include "Common.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-26 05:30:50 +00:00
										 |  |  | #include "Thread.h"
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // enable disable sources
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	#define CIFACE_USE_XINPUT
 | 
					
						
							| 
									
										
										
										
											2010-07-03 08:04:10 +00:00
										 |  |  | 	#define CIFACE_USE_DINPUT_JOYSTICK
 | 
					
						
							|  |  |  | 	#define CIFACE_USE_DINPUT_KBM
 | 
					
						
							|  |  |  | 	#define CIFACE_USE_DINPUT
 | 
					
						
							| 
									
										
										
										
											2010-07-16 19:17:35 +00:00
										 |  |  | //#ifndef CIFACE_USE_DINPUT_JOYSTICK
 | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | // enable SDL 1.2 in addition to DirectInput on windows,
 | 
					
						
							|  |  |  | // to support a few gamepads that aren't behaving with DInput
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	#define CIFACE_USE_SDL
 | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | //#endif
 | 
					
						
							| 
									
										
										
										
											2010-07-16 19:17:35 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(HAVE_X11) && HAVE_X11
 | 
					
						
							|  |  |  | 	#define CIFACE_USE_XLIB
 | 
					
						
							|  |  |  | 	#define CIFACE_USE_SDL
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-04-02 09:41:43 +00:00
										 |  |  | #if defined(__APPLE__)
 | 
					
						
							|  |  |  | 	#define CIFACE_USE_OSX
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // idk in case i wanted to change it to double or somethin, idk what's best
 | 
					
						
							|  |  |  | typedef float ControlState; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //		ControllerInterface
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // some crazy shit i made to control different device inputs and outputs
 | 
					
						
							|  |  |  | // from lots of different sources, hopefully more easily
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | class ControllerInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 18:25:49 +00:00
										 |  |  | 	// Forward declarations
 | 
					
						
							|  |  |  | 	class DeviceQualifier; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	//
 | 
					
						
							|  |  |  | 	//		Device
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 	// a device class
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	//
 | 
					
						
							|  |  |  | 	class Device | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 		class Input; | 
					
						
							|  |  |  | 		class Output; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		//		Control
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		//  control includes inputs and outputs
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		class Control		// input or output
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		public: | 
					
						
							|  |  |  | 			virtual std::string GetName() const = 0; | 
					
						
							| 
									
										
										
										
											2010-04-24 00:44:10 +00:00
										 |  |  | 			virtual ~Control() {} | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			virtual Input* ToInput() { return NULL; } | 
					
						
							|  |  |  | 			virtual Output* ToOutput() { return NULL; } | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		//		Input
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		// an input on a device
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							|  |  |  | 		class Input : public Control | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		public: | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 			// things like absolute axes/ absolute mouse position will override this
 | 
					
						
							|  |  |  | 			virtual bool IsDetectable() { return true; } | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			virtual ControlState GetState() const = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Input* ToInput() { return this; } | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2013-01-17 15:40:44 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 		//
 | 
					
						
							|  |  |  | 		//		Output
 | 
					
						
							|  |  |  | 		//
 | 
					
						
							| 
									
										
										
										
											2010-04-29 18:51:04 +00:00
										 |  |  | 		// an output on a device
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 		//
 | 
					
						
							|  |  |  | 		class Output : public Control | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		public: | 
					
						
							|  |  |  | 			virtual ~Output() {} | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			virtual void SetState(ControlState state) = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Output* ToOutput() { return this; } | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		virtual ~Device(); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		virtual std::string GetName() const = 0; | 
					
						
							|  |  |  | 		virtual int GetId() const = 0; | 
					
						
							|  |  |  | 		virtual std::string GetSource() const = 0; | 
					
						
							|  |  |  | 		virtual bool UpdateInput() = 0; | 
					
						
							|  |  |  | 		virtual bool UpdateOutput() = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		virtual void ClearInputState(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		const std::vector<Input*>& Inputs() const { return m_inputs; } | 
					
						
							|  |  |  | 		const std::vector<Output*>& Outputs() const { return m_outputs; } | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 		Input* FindInput(const std::string& name) const; | 
					
						
							|  |  |  | 		Output* FindOutput(const std::string& name) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	protected: | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		void AddInput(Input* const i); | 
					
						
							|  |  |  | 		void AddOutput(Output* const o); | 
					
						
							| 
									
										
										
										
											2013-01-17 15:40:44 -06:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		class FullAnalogSurface : public Input | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		public: | 
					
						
							|  |  |  | 			FullAnalogSurface(Input* low, Input* high) | 
					
						
							|  |  |  | 				: m_low(*low), m_high(*high) | 
					
						
							|  |  |  | 			{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			ControlState GetState() const | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				return (1 + m_high.GetState() - m_low.GetState()) / 2; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			std::string GetName() const | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				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)); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	private: | 
					
						
							|  |  |  | 		std::vector<Input*>		m_inputs; | 
					
						
							|  |  |  | 		std::vector<Output*>	m_outputs; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	//		DeviceQualifier
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// device qualifier used to match devices
 | 
					
						
							|  |  |  | 	// currently has ( source, id, name ) properties which match a device
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	class DeviceQualifier | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		DeviceQualifier() : cid(-1) {} | 
					
						
							|  |  |  | 		DeviceQualifier(const std::string& _source, const int _id, const std::string& _name) | 
					
						
							| 
									
										
										
										
											2010-04-02 09:41:43 +00:00
										 |  |  | 			: source(_source), cid(_id), name(_name) {} | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 		void FromDevice(const Device* const dev); | 
					
						
							|  |  |  | 		void FromString(const std::string& str); | 
					
						
							|  |  |  | 		std::string ToString() const; | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		bool operator==(const DeviceQualifier& devq) const; | 
					
						
							| 
									
										
										
										
											2010-06-22 08:30:33 +00:00
										 |  |  | 		bool operator==(const Device* const dev) const; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		std::string		source; | 
					
						
							| 
									
										
										
										
											2010-04-02 09:41:43 +00:00
										 |  |  | 		int				cid; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 		std::string		name; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	//		ControlReference
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// these are what you create to actually use the inputs, InputReference or OutputReference
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// after being binded to devices and controls with ControllerInterface::UpdateReference,
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 	//		each one can link to multiple devices and controls
 | 
					
						
							|  |  |  | 	//		when you change a ControlReference's expression,
 | 
					
						
							|  |  |  | 	//		you must use ControllerInterface::UpdateReference on it to rebind controls
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	//
 | 
					
						
							|  |  |  | 	class ControlReference | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		friend class ControllerInterface; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2010-04-24 00:44:10 +00:00
										 |  |  | 		virtual ~ControlReference() {} | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		virtual ControlState State(const ControlState state = 0) = 0; | 
					
						
							|  |  |  | 		virtual Device::Control* Detect(const unsigned int ms, Device* const device) = 0; | 
					
						
							|  |  |  | 		size_t BoundCount() const { return m_controls.size(); } | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		ControlState		range; | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		std::string			expression; | 
					
						
							|  |  |  | 		const bool			is_input; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 	protected: | 
					
						
							|  |  |  | 		ControlReference(const bool _is_input) : range(1), is_input(_is_input) {} | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		struct DeviceControl | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | 			DeviceControl() : control(NULL), mode(0) {} | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 			Device::Control*	control; | 
					
						
							|  |  |  | 			int		mode; | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		std::vector<DeviceControl>	m_controls; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	//		InputReference
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// control reference for inputs
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	class InputReference : public ControlReference | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		InputReference() : ControlReference(true) {} | 
					
						
							|  |  |  | 		ControlState State(const ControlState state); | 
					
						
							|  |  |  | 		Device::Control* Detect(const unsigned int ms, Device* const device); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	//		OutputReference
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// control reference for outputs
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	class OutputReference : public ControlReference | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 		OutputReference() : ControlReference(false) {} | 
					
						
							|  |  |  | 		ControlState State(const ControlState state); | 
					
						
							|  |  |  | 		Device::Control* Detect(const unsigned int ms, Device* const device); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-10 06:48:24 +00:00
										 |  |  | 	ControllerInterface() : m_is_init(false), m_hwnd(NULL) {} | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 	void SetHwnd(void* const hwnd); | 
					
						
							| 
									
										
										
										
											2010-10-12 19:42:29 +00:00
										 |  |  | 	void Initialize(); | 
					
						
							|  |  |  | 	void Shutdown(); | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 	bool IsInit() const { return m_is_init; } | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 03:12:16 +00:00
										 |  |  | 	void UpdateReference(ControlReference* control, const DeviceQualifier& default_device) const; | 
					
						
							| 
									
										
										
										
											2010-07-26 05:30:50 +00:00
										 |  |  | 	bool UpdateInput(const bool force = false); | 
					
						
							|  |  |  | 	bool UpdateOutput(const bool force = false); | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00: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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-22 08:30:33 +00:00
										 |  |  | 	const std::vector<Device*>& Devices() const { return m_devices; } | 
					
						
							|  |  |  | 	Device* FindDevice(const DeviceQualifier& devq) const; | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-07 00:16:38 +00:00
										 |  |  | 	std::recursive_mutex update_lock; | 
					
						
							| 
									
										
										
										
											2010-07-26 05:30:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | private: | 
					
						
							|  |  |  | 	bool					m_is_init; | 
					
						
							|  |  |  | 	std::vector<Device*>	m_devices; | 
					
						
							|  |  |  | 	void*					m_hwnd; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-14 01:20:11 +00:00
										 |  |  | typedef std::vector<ControllerInterface::Device*> DeviceList; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 19:42:29 +00:00
										 |  |  | extern ControllerInterface g_controller_interface; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-02 02:48:24 +00:00
										 |  |  | #endif
 |