1 // FGEventInput.cxx -- 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.
28 #include "FGEventInput.hxx"
29 #include <Main/fg_props.hxx>
30 #include <simgear/io/sg_file.hxx>
31 #include <simgear/props/props_io.hxx>
32 #include <Scripting/NasalSys.hxx>
34 using simgear::PropertyList;
36 FGEventSetting::FGEventSetting( SGPropertyNode_ptr base ) :
41 if( (n = base->getNode( "value" )) != NULL ) {
43 value = n->getDoubleValue();
45 n = base->getNode( "property" );
47 SG_LOG( SG_INPUT, SG_WARN, "Neither <value> nor <property> defined for event setting." );
49 valueNode = fgGetNode( n->getStringValue(), true );
53 if( (n = base->getChild("condition")) != NULL )
54 condition = sgReadCondition(base, n);
56 SG_LOG( SG_INPUT, SG_ALERT, "No condition for event setting." );
59 double FGEventSetting::GetValue()
61 return valueNode == NULL ? value : valueNode->getDoubleValue();
64 bool FGEventSetting::Test()
66 return condition == NULL ? true : condition->test();
69 static inline bool StartsWith( string & s, const char * cp )
71 return s.find( cp ) == 0;
74 FGInputEvent * FGInputEvent::NewObject( FGInputDevice * device, SGPropertyNode_ptr node )
76 string name = node->getStringValue( "name", "" );
77 if( StartsWith( name, "button-" ) )
78 return new FGButtonEvent( device, node );
80 if( StartsWith( name, "rel-" ) )
81 return new FGRelAxisEvent( device, node );
83 if( StartsWith( name, "abs-" ) )
84 return new FGAbsAxisEvent( device, node );
86 return new FGInputEvent( device, node );
89 FGInputEvent::FGInputEvent( FGInputDevice * aDevice, SGPropertyNode_ptr node ) :
92 lastSettingValue(std::numeric_limits<float>::quiet_NaN())
94 name = node->getStringValue( "name", "" );
95 desc = node->getStringValue( "desc", "" );
96 intervalSec = node->getDoubleValue("interval-sec",0.0);
98 read_bindings( node, bindings, KEYMOD_NONE, device->GetNasalModule() );
100 PropertyList settingNodes = node->getChildren("setting");
101 for( PropertyList::iterator it = settingNodes.begin(); it != settingNodes.end(); it++ )
102 settings.push_back( new FGEventSetting( *it ) );
105 FGInputEvent::~FGInputEvent()
109 void FGInputEvent::update( double dt )
111 for( setting_list_t::iterator it = settings.begin(); it != settings.end(); it++ ) {
112 if( (*it)->Test() ) {
113 double value = (*it)->GetValue();
114 if( value != lastSettingValue ) {
115 device->Send( GetName(), (*it)->GetValue() );
116 lastSettingValue = value;
122 void FGInputEvent::fire( FGEventData & eventData )
124 lastDt += eventData.dt;
125 if( lastDt >= intervalSec ) {
127 for( binding_list_t::iterator it = bindings[eventData.modifiers].begin(); it != bindings[eventData.modifiers].end(); it++ )
128 fire( *it, eventData );
130 lastDt -= intervalSec;
134 void FGInputEvent::fire( SGBinding * binding, FGEventData & eventData )
141 FGAxisEvent::FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
142 FGInputEvent( device, node )
144 tolerance = node->getDoubleValue("tolerance", 0.002);
145 minRange = node->getDoubleValue("min-range", 0.0 );
146 maxRange = node->getDoubleValue("max-range", 0.0 );
147 center = node->getDoubleValue("center", 0.0);
148 deadband = node->getDoubleValue("dead-band", 0.0);
149 lowThreshold = node->getDoubleValue("low-threshold", -0.9);
150 highThreshold = node->getDoubleValue("high-threshold", 0.9);
154 void FGAxisEvent::fire( FGEventData & eventData )
156 if (fabs( eventData.value - lastValue) < tolerance)
158 lastValue = eventData.value;
160 // We need a copy of the FGEventData struct to set the new value and to avoid side effects
161 FGEventData ed = eventData;
163 if( fabs(ed.value) < deadband )
166 if( minRange != maxRange )
167 ed.value = 2.0*(eventData.value-minRange)/(maxRange-minRange)-1.0;
169 FGInputEvent::fire( ed );
172 void FGAbsAxisEvent::fire( SGBinding * binding, FGEventData & eventData )
174 // sets the "setting" node
175 binding->fire( eventData.value );
178 FGRelAxisEvent::FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
179 FGAxisEvent( device, node )
181 // relative axes can't use tolerance
185 void FGRelAxisEvent::fire( SGBinding * binding, FGEventData & eventData )
187 // sets the "offset" node
188 binding->fire( eventData.value, 1.0 );
191 FGButtonEvent::FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
192 FGInputEvent( device, node ),
196 repeatable = node->getBoolValue("repeatable", repeatable);
199 void FGButtonEvent::fire( FGEventData & eventData )
201 bool pressed = eventData.value > 0.0;
203 // The press event may be repeated.
204 if (!lastState || repeatable) {
205 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
206 FGInputEvent::fire( eventData );
209 // The release event is never repeated.
211 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
212 eventData.modifiers|=KEYMOD_RELEASED;
213 FGInputEvent::fire( eventData );
220 FGInputDevice::~FGInputDevice()
222 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
223 if (nas && deviceNode ) {
224 SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
226 SGPropertyNode_ptr nasalClose = nasal->getNode("close");
228 const string s = nasalClose->getStringValue();
229 nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s.c_str(), s.length(), deviceNode );
232 nas->deleteModule(nasalModule.c_str());
236 void FGInputDevice::Configure( SGPropertyNode_ptr aDeviceNode )
238 deviceNode = aDeviceNode;
240 nasalModule = string("__event:") + GetName();
242 PropertyList eventNodes = deviceNode->getChildren( "event" );
243 for( PropertyList::iterator it = eventNodes.begin(); it != eventNodes.end(); it++ )
244 AddHandledEvent( FGInputEvent::NewObject( this, *it ) );
246 debugEvents = deviceNode->getBoolValue("debug-events", debugEvents );
247 grab = deviceNode->getBoolValue("grab", grab );
250 // add nodes for the last event:
251 // last-event/name [string]
252 // last-event/value [double]
254 SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
256 SGPropertyNode_ptr open = nasal->getNode("open");
258 const string s = open->getStringValue();
259 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
261 nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s.c_str(), s.length(), deviceNode );
267 void FGInputDevice::update( double dt )
269 for( map<string,FGInputEvent_ptr>::iterator it = handledEvents.begin(); it != handledEvents.end(); it++ )
270 (*it).second->update( dt );
273 void FGInputDevice::HandleEvent( FGEventData & eventData )
275 string eventName = TranslateEventName( eventData );
277 cout << GetName() << " has event " <<
278 eventName << " modifiers=" << eventData.modifiers << " value=" << eventData.value << endl;
280 if( handledEvents.count( eventName ) > 0 ) {
281 handledEvents[ eventName ]->fire( eventData );
285 void FGInputDevice::SetName( string name )
290 const char * FGEventInput::PROPERTY_ROOT = "/input/event";
292 FGEventInput::FGEventInput() :
293 configMap( "Input/Event", fgGetNode( PROPERTY_ROOT, true ), "device-named" )
297 FGEventInput::~FGEventInput()
299 for( map<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
301 input_devices.clear();
304 void FGEventInput::init( )
306 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing event bindings");
307 // SGPropertyNode * base = fgGetNode("/input/event", true);
311 void FGEventInput::postinit ()
315 void FGEventInput::update( double dt )
317 // call each associated device's update() method
318 for( map<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
319 (*it).second->update( dt );
322 unsigned FGEventInput::AddDevice( FGInputDevice * inputDevice )
324 SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
325 SGPropertyNode_ptr deviceNode = NULL;
327 // look for configuration in the device map
328 if( configMap.count( inputDevice->GetName() ) > 0 ) {
329 // found - copy to /input/event/device[n]
333 for( index = 0; index < MAX_DEVICES; index++ )
334 if( (deviceNode = baseNode->getNode( "device", index, false ) ) == NULL )
337 if( index == MAX_DEVICES ) {
338 SG_LOG(SG_INPUT, SG_WARN, "To many event devices - ignoring " << inputDevice->GetName() );
339 return INVALID_DEVICE_INDEX;
343 deviceNode = baseNode->getNode( "device", index, true );
345 // and copy the properties from the configuration tree
346 copyProperties( configMap[ inputDevice->GetName() ], deviceNode );
350 if( deviceNode == NULL ) {
351 SG_LOG(SG_INPUT, SG_DEBUG, "No configuration found for device " << inputDevice->GetName() );
353 return INVALID_DEVICE_INDEX;
356 inputDevice->Configure( deviceNode );
360 input_devices[ deviceNode->getIndex() ] = inputDevice;
364 SG_LOG(SG_INPUT, SG_ALERT, "can't open InputDevice " << inputDevice->GetName() );
365 return INVALID_DEVICE_INDEX;
368 SG_LOG(SG_INPUT, SG_DEBUG, "using InputDevice " << inputDevice->GetName() );
369 return deviceNode->getIndex();
372 void FGEventInput::RemoveDevice( unsigned index )
374 // not fully implemented yet
375 SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
376 SGPropertyNode_ptr deviceNode = NULL;
378 FGInputDevice *inputDevice = input_devices[index];
380 input_devices.erase(index);
384 deviceNode = baseNode->removeChild("device", index, false);