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.
27 #include "FGEventInput.hxx"
28 #include <Main/fg_props.hxx>
29 #include <simgear/io/sg_file.hxx>
30 #include <Scripting/NasalSys.hxx>
32 using simgear::PropertyList;
34 FGEventSetting::FGEventSetting( SGPropertyNode_ptr base ) :
39 if( (n = base->getNode( "value" )) != NULL ) {
41 value = n->getDoubleValue();
43 n = base->getNode( "property" );
45 SG_LOG( SG_INPUT, SG_WARN, "Neither <value> nor <property> defined for event setting." );
47 valueNode = fgGetNode( n->getStringValue(), true );
51 if( (n = base->getChild("condition")) != NULL )
52 condition = sgReadCondition(base, n);
54 SG_LOG( SG_INPUT, SG_ALERT, "No condition for event setting." );
57 double FGEventSetting::GetValue()
59 return valueNode == NULL ? value : valueNode->getDoubleValue();
62 bool FGEventSetting::Test()
64 return condition == NULL ? true : condition->test();
67 static inline bool StartsWith( string & s, const char * cp )
69 return s.compare( 0, strlen(cp), cp ) == 0;
72 FGInputEvent * FGInputEvent::NewObject( FGInputDevice * device, SGPropertyNode_ptr node )
74 string name = node->getStringValue( "name", "" );
75 if( StartsWith( name, "button-" ) )
76 return new FGButtonEvent( device, node );
78 if( StartsWith( name, "rel-" ) )
79 return new FGRelAxisEvent( device, node );
81 if( StartsWith( name, "abs-" ) )
82 return new FGAbsAxisEvent( device, node );
84 return new FGInputEvent( device, node );
87 FGInputEvent::FGInputEvent( FGInputDevice * aDevice, SGPropertyNode_ptr node ) :
90 lastSettingValue(std::numeric_limits<float>::quiet_NaN())
92 name = node->getStringValue( "name", "" );
93 desc = node->getStringValue( "desc", "" );
94 intervalSec = node->getDoubleValue("interval-sec",0.0);
96 read_bindings( node, bindings, KEYMOD_NONE, device->GetNasalModule() );
98 PropertyList settingNodes = node->getChildren("setting");
99 for( PropertyList::iterator it = settingNodes.begin(); it != settingNodes.end(); it++ )
100 settings.push_back( new FGEventSetting( *it ) );
103 FGInputEvent::~FGInputEvent()
107 void FGInputEvent::update( double dt )
109 for( setting_list_t::iterator it = settings.begin(); it != settings.end(); it++ ) {
110 if( (*it)->Test() ) {
111 double value = (*it)->GetValue();
112 if( value != lastSettingValue ) {
113 device->Send( GetName(), (*it)->GetValue() );
114 lastSettingValue = value;
120 void FGInputEvent::fire( FGEventData & eventData )
122 lastDt += eventData.dt;
123 if( lastDt >= intervalSec ) {
125 for( binding_list_t::iterator it = bindings[eventData.modifiers].begin(); it != bindings[eventData.modifiers].end(); it++ )
126 fire( *it, eventData );
128 lastDt -= intervalSec;
132 void FGInputEvent::fire( SGBinding * binding, FGEventData & eventData )
139 FGAxisEvent::FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
140 FGInputEvent( device, node )
142 tolerance = node->getDoubleValue("tolerance", 0.002);
143 minRange = node->getDoubleValue("min-range", 0.0 );
144 maxRange = node->getDoubleValue("max-range", 0.0 );
145 center = node->getDoubleValue("center", 0.0);
146 deadband = node->getDoubleValue("dead-band", 0.0);
147 lowThreshold = node->getDoubleValue("low-threshold", -0.9);
148 highThreshold = node->getDoubleValue("high-threshold", 0.9);
152 void FGAxisEvent::fire( FGEventData & eventData )
154 if (fabs( eventData.value - lastValue) < tolerance)
156 lastValue = eventData.value;
158 // We need a copy of the FGEventData struct to set the new value and to avoid side effects
159 FGEventData ed = eventData;
161 if( fabs(ed.value) < deadband )
164 if( minRange != maxRange )
165 ed.value = 2.0*(eventData.value-minRange)/(maxRange-minRange)-1.0;
167 FGInputEvent::fire( ed );
170 void FGAbsAxisEvent::fire( SGBinding * binding, FGEventData & eventData )
172 // sets the "setting" node
173 binding->fire( eventData.value );
176 FGRelAxisEvent::FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
177 FGAxisEvent( device, node )
179 // relative axes can't use tolerance
183 void FGRelAxisEvent::fire( SGBinding * binding, FGEventData & eventData )
185 // sets the "offset" node
186 binding->fire( eventData.value, 1.0 );
189 FGButtonEvent::FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
190 FGInputEvent( device, node ),
194 repeatable = node->getBoolValue("repeatable", repeatable);
197 void FGButtonEvent::fire( FGEventData & eventData )
199 bool pressed = eventData.value > 0.0;
201 // The press event may be repeated.
202 if (!lastState || repeatable) {
203 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
204 FGInputEvent::fire( eventData );
207 // The release event is never repeated.
209 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
210 eventData.modifiers|=KEYMOD_RELEASED;
211 FGInputEvent::fire( eventData );
218 FGInputDevice::~FGInputDevice()
220 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
221 if (nas && deviceNode ) {
222 SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
224 SGPropertyNode_ptr nasalClose = nasal->getNode("close");
226 const char *s = nasalClose->getStringValue();
227 nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s, strlen(s), deviceNode );
230 nas->deleteModule(nasalModule.c_str());
234 void FGInputDevice::Configure( SGPropertyNode_ptr aDeviceNode )
236 deviceNode = aDeviceNode;
238 nasalModule = string("__event:") + GetName();
240 PropertyList eventNodes = deviceNode->getChildren( "event" );
241 for( PropertyList::iterator it = eventNodes.begin(); it != eventNodes.end(); it++ )
242 AddHandledEvent( FGInputEvent::NewObject( this, *it ) );
244 debugEvents = deviceNode->getBoolValue("debug-events", debugEvents );
245 grab = deviceNode->getBoolValue("grab", grab );
248 // add nodes for the last event:
249 // last-event/name [string]
250 // last-event/value [double]
252 SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
254 SGPropertyNode_ptr open = nasal->getNode("open");
256 const char *s = open->getStringValue();
257 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
259 nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s, strlen(s), deviceNode );
265 void FGInputDevice::update( double dt )
267 for( map<string,FGInputEvent_ptr>::iterator it = handledEvents.begin(); it != handledEvents.end(); it++ )
268 (*it).second->update( dt );
271 void FGInputDevice::HandleEvent( FGEventData & eventData )
273 string eventName = TranslateEventName( eventData );
275 cout << GetName() << " has event " <<
276 eventName << " modifiers=" << eventData.modifiers << " value=" << eventData.value << endl;
278 if( handledEvents.count( eventName ) > 0 ) {
279 handledEvents[ eventName ]->fire( eventData );
283 void FGInputDevice::SetName( string name )
288 const char * FGEventInput::PROPERTY_ROOT = "/input/event";
290 FGEventInput::FGEventInput() :
291 configMap( "Input/Event", fgGetNode( PROPERTY_ROOT, true ), "device-named" )
295 FGEventInput::~FGEventInput()
297 for( map<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
299 input_devices.clear();
302 void FGEventInput::init( )
304 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing event bindings");
305 // SGPropertyNode * base = fgGetNode("/input/event", true);
309 void FGEventInput::postinit ()
313 void FGEventInput::update( double dt )
315 // call each associated device's update() method
316 for( map<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
317 (*it).second->update( dt );
320 unsigned FGEventInput::AddDevice( FGInputDevice * inputDevice )
322 SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
323 SGPropertyNode_ptr deviceNode = NULL;
325 // look for configuration in the device map
326 if( configMap.count( inputDevice->GetName() ) > 0 ) {
327 // found - copy to /input/event/device[n]
331 for( index = 0; index < MAX_DEVICES; index++ )
332 if( (deviceNode = baseNode->getNode( "device", index, false ) ) == NULL )
335 if( index == MAX_DEVICES ) {
336 SG_LOG(SG_INPUT, SG_WARN, "To many event devices - ignoring " << inputDevice->GetName() );
337 return INVALID_DEVICE_INDEX;
341 deviceNode = baseNode->getNode( "device", index, true );
343 // and copy the properties from the configuration tree
344 copyProperties( configMap[ inputDevice->GetName() ], deviceNode );
348 if( deviceNode == NULL ) {
349 SG_LOG(SG_INPUT, SG_DEBUG, "No configuration found for device " << inputDevice->GetName() );
351 return INVALID_DEVICE_INDEX;
354 inputDevice->Configure( deviceNode );
358 input_devices[ deviceNode->getIndex() ] = inputDevice;
362 SG_LOG(SG_INPUT, SG_ALERT, "can't open InputDevice " << inputDevice->GetName() );
363 return INVALID_DEVICE_INDEX;
366 SG_LOG(SG_INPUT, SG_DEBUG, "using InputDevice " << inputDevice->GetName() );
367 return deviceNode->getIndex();
370 void FGEventInput::RemoveDevice( unsigned index )
372 // not fully implemented yet
373 SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
374 SGPropertyNode_ptr deviceNode = NULL;
376 FGInputDevice *inputDevice = input_devices[index];
378 input_devices.erase(index);
382 deviceNode = baseNode->removeChild("device", index, false);