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 ) : modifiers(aModifiers), value(aValue), dt(aDt) {}
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 {
88 * Constructor for the class. The arg node shall point
89 * to the property corresponding to the <event> node
91 FGInputEvent( FGInputDevice * device, SGPropertyNode_ptr node );
92 virtual ~FGInputEvent();
95 * dispatch the event value through all bindings
97 virtual void fire( FGEventData & eventData );
100 * access for the name property
102 string GetName() const { return name; }
105 * access for the description property
107 string GetDescription() const { return desc; }
109 virtual void update( double dt );
111 static FGInputEvent * NewObject( FGInputDevice * device, SGPropertyNode_ptr node );
114 virtual void fire( SGBinding * binding, FGEventData & eventData );
115 /* A more or less meaningfull description of the event */
118 /* One of the predefined names of the event */
121 /* A list of SGBinding objects */
122 binding_list_t bindings[KEYMOD_MAX];
124 /* A list of FGEventSetting objects */
125 setting_list_t settings;
127 /* A pointer to the associated device */
128 FGInputDevice * device;
132 double lastSettingValue;
135 class FGButtonEvent : public FGInputEvent {
137 FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node );
138 virtual void fire( FGEventData & eventData );
145 class FGAxisEvent : public FGInputEvent {
147 FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node );
148 void SetMaxRange( double value ) { maxRange = value; }
149 void SetMinRange( double value ) { minRange = value; }
150 void SetRange( double min, double max ) { minRange = min; maxRange = max; }
152 virtual void fire( FGEventData & eventData );
159 double highThreshold;
163 class FGRelAxisEvent : public FGAxisEvent {
165 FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node );
167 virtual void fire( SGBinding * binding, FGEventData & eventData );
170 class FGAbsAxisEvent : public FGAxisEvent {
172 FGAbsAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) : FGAxisEvent( device, node ) {}
174 virtual void fire( SGBinding * binding, FGEventData & eventData );
177 typedef class SGSharedPtr<FGInputEvent> FGInputEvent_ptr;
180 * A abstract class implementing basic functionality of input devices for
181 * all operating systems. This is the base class for the O/S-specific
182 * implementation of input device handlers
184 class FGInputDevice : public SGReferenced {
186 FGInputDevice() : debugEvents(false), grab(false) {}
187 FGInputDevice( string aName ) : name(aName) {}
189 virtual ~FGInputDevice();
191 virtual void Open() = 0;
192 virtual void Close() = 0;
194 virtual void Send( const char * eventName, double value ) = 0;
196 inline void Send( const string & eventName, double value ) {
197 Send( eventName.c_str(), value );
200 virtual const char * TranslateEventName( FGEventData & eventData ) = 0;
203 void SetName( string name );
204 string & GetName() { return name; }
206 void HandleEvent( FGEventData & eventData );
208 void AddHandledEvent( FGInputEvent_ptr handledEvent ) {
209 if( handledEvents.count( handledEvent->GetName() ) == 0 )
210 handledEvents[handledEvent->GetName()] = handledEvent;
213 virtual void Configure( SGPropertyNode_ptr deviceNode );
215 virtual void update( double dt );
217 bool GetDebugEvents () const { return debugEvents; }
219 bool GetGrab() const { return grab; }
221 const string & GetNasalModule() const { return nasalModule; }
224 // A map of events, this device handles
225 map<string,FGInputEvent_ptr> handledEvents;
227 // the device has a name to be recognized
230 // print out events comming in from the device
234 // grab the device exclusively, if O/S supports this
235 // so events are not sent to other applications
238 SGPropertyNode_ptr deviceNode;
242 typedef SGSharedPtr<FGInputDevice> FGInputDevice_ptr;
246 * The Subsystem for the event input device
248 class FGEventInput : public SGSubsystem,FGCommonInput {
251 virtual ~FGEventInput();
253 virtual void postinit();
254 virtual void update( double dt );
256 const static unsigned MAX_DEVICES = 1000;
257 const static unsigned INVALID_DEVICE_INDEX = MAX_DEVICES + 1;
259 static const char * PROPERTY_ROOT;
261 unsigned AddDevice( FGInputDevice * inputDevice );
262 void RemoveDevice( unsigned index );
264 map<int,FGInputDevice*> input_devices;
265 FGDeviceConfigurationMap configMap;
267 SGPropertyNode_ptr nasalClose;