1 // FGEventInput.hxx -- handle event driven input devices
3 // Written by Torsten Dreyer, started July 2009
5 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #ifndef __FGEVENTINPUT_HXX
24 #define __FGEVENTINPUT_HXX
26 #include "FGCommonInput.hxx"
27 #include "FGButton.hxx"
28 #include "FGDeviceConfigurationMap.hxx"
29 #include <simgear/structure/subsystem_mgr.hxx>
32 * A base structure for event data.
33 * To be extended for O/S specific implementation data
36 FGEventData( double aValue, double aDt, int aModifiers ) : value(aValue), dt(aDt), modifiers(aModifiers) {}
42 class FGEventSetting : public SGReferenced {
44 FGEventSetting( SGPropertyNode_ptr base );
49 * access for the value property
55 SGPropertyNode_ptr valueNode;
56 SGSharedPtr<const SGCondition> condition;
59 typedef SGSharedPtr<FGEventSetting> FGEventSetting_ptr;
60 typedef vector<FGEventSetting_ptr> setting_list_t;
63 * A wrapper class for a configured event.
66 * <desc>Change the view pitch</desc>
67 * <name>rel-x-rotate</name>
69 * <command>property-adjust</command>
70 * <property>sim/current-view/pitch-offset-deg</property>
71 * <factor type="double">0.01</factor>
72 * <min type="double">-90.0</min>
73 * <max type="double">90.0</max>
74 * <wrap type="bool">false</wrap>
84 class FGInputEvent : public SGReferenced,FGCommonInput {
87 * Constructor for the class. The arg node shall point
88 * to the property corresponding to the <event> node
90 FGInputEvent( FGInputDevice * device, SGPropertyNode_ptr node );
91 virtual ~FGInputEvent();
94 * dispatch the event value through all bindings
96 virtual void fire( FGEventData & eventData );
99 * access for the name property
101 string GetName() const { return name; }
104 * access for the description property
106 string GetDescription() const { return desc; }
108 virtual void update( double dt );
110 static FGInputEvent * NewObject( FGInputDevice * device, SGPropertyNode_ptr node );
113 /* A more or less meaningfull description of the event */
116 /* One of the predefined names of the event */
119 /* A list of SGBinding objects */
120 binding_list_t bindings[KEYMOD_MAX];
122 /* A list of FGEventSetting objects */
123 setting_list_t settings;
125 /* A pointer to the associated device */
126 FGInputDevice * device;
130 double lastSettingValue;
133 class FGButtonEvent : public FGInputEvent {
135 FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node );
136 virtual void fire( FGEventData & eventData );
143 class FGAxisEvent : public FGInputEvent {
145 FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node );
147 virtual void fire( FGEventData & eventData );
154 double highThreshold;
158 typedef class SGSharedPtr<FGInputEvent> FGInputEvent_ptr;
161 * A abstract class implementing basic functionality of input devices for
162 * all operating systems. This is the base class for the O/S-specific
163 * implementation of input device handlers
165 class FGInputDevice : public SGReferenced {
167 FGInputDevice() : debugEvents(false) {}
168 FGInputDevice( string aName ) : name(aName) {}
170 virtual ~FGInputDevice();
172 virtual void Open() = 0;
173 virtual void Close() = 0;
175 virtual void Send( const char * eventName, double value ) = 0;
177 inline void Send( const string & eventName, double value ) {
178 Send( eventName.c_str(), value );
181 virtual const char * TranslateEventName( FGEventData & eventData ) = 0;
184 void SetName( string name );
185 string & GetName() { return name; }
187 void HandleEvent( FGEventData & eventData );
189 void AddHandledEvent( FGInputEvent_ptr handledEvent ) {
190 if( handledEvents.count( handledEvent->GetName() ) == 0 )
191 handledEvents[handledEvent->GetName()] = handledEvent;
194 virtual void update( double dt );
196 bool GetDebugEvents () const { return debugEvents; }
197 void SetDebugEvents( bool value ) { debugEvents = value; }
200 // A map of events, this device handles
201 map<string,FGInputEvent_ptr> handledEvents;
203 // the device has a name to be recognized
206 // print out events comming in from the device
211 typedef SGSharedPtr<FGInputDevice> FGInputDevice_ptr;
215 * The Subsystem for the event input device
217 class FGEventInput : public SGSubsystem,FGCommonInput {
220 virtual ~FGEventInput();
222 virtual void postinit();
223 virtual void update( double dt );
226 static const char * PROPERTY_ROOT;
228 void AddDevice( FGInputDevice * inputDevice );
229 map<int,FGInputDevice*> input_devices;
230 FGDeviceConfigurationMap configMap;