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 FGEventSetting::FGEventSetting( SGPropertyNode_ptr base ) :
37 if( (n = base->getNode( "value" )) != NULL ) {
39 value = n->getDoubleValue();
41 n = base->getNode( "property" );
43 SG_LOG( SG_INPUT, SG_WARN, "Neither <value> nor <property> defined for event setting." );
45 valueNode = fgGetNode( n->getStringValue(), true );
49 if( (n = base->getChild("condition")) != NULL )
50 condition = sgReadCondition(base, n);
52 SG_LOG( SG_INPUT, SG_ALERT, "No condition for event setting." );
55 double FGEventSetting::GetValue()
57 return valueNode == NULL ? value : valueNode->getDoubleValue();
60 bool FGEventSetting::Test()
62 return condition == NULL ? true : condition->test();
65 static inline bool StartsWith( string & s, const char * cp )
67 return s.compare( 0, strlen(cp), cp ) == 0;
70 FGInputEvent * FGInputEvent::NewObject( FGInputDevice * device, SGPropertyNode_ptr node )
72 string name = node->getStringValue( "name", "" );
73 if( StartsWith( name, "button-" ) )
74 return new FGButtonEvent( device, node );
76 if( StartsWith( name, "rel-" ) )
77 return new FGAxisEvent( device, node );
79 if( StartsWith( name, "abs-" ) )
80 return new FGAxisEvent( device, node );
82 return new FGInputEvent( device, node );
85 FGInputEvent::FGInputEvent( FGInputDevice * aDevice, SGPropertyNode_ptr node ) :
88 lastSettingValue(std::numeric_limits<float>::quiet_NaN())
90 name = node->getStringValue( "name", "" );
91 desc = node->getStringValue( "desc", "" );
92 intervalSec = node->getDoubleValue("interval-sec",0.0);
94 read_bindings( node, bindings, KEYMOD_NONE, device->GetNasalModule() );
96 vector<SGPropertyNode_ptr> settingNodes = node->getChildren("setting");
97 for( vector<SGPropertyNode_ptr>::iterator it = settingNodes.begin(); it != settingNodes.end(); it++ )
98 settings.push_back( new FGEventSetting( *it ) );
101 FGInputEvent::~FGInputEvent()
105 void FGInputEvent::update( double dt )
107 for( setting_list_t::iterator it = settings.begin(); it != settings.end(); it++ ) {
108 if( (*it)->Test() ) {
109 double value = (*it)->GetValue();
110 if( value != lastSettingValue ) {
111 device->Send( GetName(), (*it)->GetValue() );
112 lastSettingValue = value;
118 void FGInputEvent::fire( FGEventData & eventData )
120 lastDt += eventData.dt;
121 if( lastDt >= intervalSec ) {
123 for( binding_list_t::iterator it = bindings[eventData.modifiers].begin(); it != bindings[eventData.modifiers].end(); it++ )
124 (*it)->fire( eventData.value, 1.0 );
126 lastDt -= intervalSec;
130 FGAxisEvent::FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
131 FGInputEvent( device, node )
133 tolerance = node->getDoubleValue("tolerance", 0.002);
134 minRange = node->getDoubleValue("min-range", -1024.0);
135 maxRange = node->getDoubleValue("max-range", 1024.0);
136 center = node->getDoubleValue("center", 0.0);
137 deadband = node->getDoubleValue("dead-band", 0.0);
138 lowThreshold = node->getDoubleValue("low-threshold", -0.9);
139 highThreshold = node->getDoubleValue("high-threshold", 0.9);
143 void FGAxisEvent::fire( FGEventData & eventData )
145 if (fabs( eventData.value - lastValue) < tolerance)
147 lastValue = eventData.value;
148 FGInputEvent::fire( eventData );
151 FGButtonEvent::FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
152 FGInputEvent( device, node ),
156 repeatable = node->getBoolValue("repeatable", repeatable);
159 void FGButtonEvent::fire( FGEventData & eventData )
161 bool pressed = eventData.value > 0.0;
163 // The press event may be repeated.
164 if (!lastState || repeatable) {
165 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been pressed" );
166 FGInputEvent::fire( eventData );
169 // The release event is never repeated.
171 SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
172 eventData.modifiers|=KEYMOD_RELEASED;
173 FGInputEvent::fire( eventData );
180 FGInputDevice::~FGInputDevice()
182 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
183 if (nas && deviceNode ) {
184 SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
186 SGPropertyNode_ptr nasalClose = nasal->getNode("close");
188 const char *s = nasalClose->getStringValue();
189 nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s, strlen(s), deviceNode );
192 nas->deleteModule(nasalModule.c_str());
196 void FGInputDevice::Configure( SGPropertyNode_ptr aDeviceNode )
198 deviceNode = aDeviceNode;
200 nasalModule = string("__event:") + GetName();
202 vector<SGPropertyNode_ptr> eventNodes = deviceNode->getChildren( "event" );
203 for( vector<SGPropertyNode_ptr>::iterator it = eventNodes.begin(); it != eventNodes.end(); it++ )
204 AddHandledEvent( FGInputEvent::NewObject( this, *it ) );
206 debugEvents = deviceNode->getBoolValue("debug-events", debugEvents );
207 grab = deviceNode->getBoolValue("grab", grab );
210 // add nodes for the last event:
211 // last-event/name [string]
212 // last-event/value [double]
214 SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
216 SGPropertyNode_ptr open = nasal->getNode("open");
218 const char *s = open->getStringValue();
219 FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
221 nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s, strlen(s), deviceNode );
227 void FGInputDevice::update( double dt )
229 for( map<string,FGInputEvent_ptr>::iterator it = handledEvents.begin(); it != handledEvents.end(); it++ )
230 (*it).second->update( dt );
233 void FGInputDevice::HandleEvent( FGEventData & eventData )
235 string eventName = TranslateEventName( eventData );
237 cout << GetName() << " has event " <<
238 eventName << " modifiers=" << eventData.modifiers << " value=" << eventData.value << endl;
240 if( handledEvents.count( eventName ) > 0 ) {
241 handledEvents[ eventName ]->fire( eventData );
245 void FGInputDevice::SetName( string name )
250 const char * FGEventInput::PROPERTY_ROOT = "/input/event";
252 FGEventInput::FGEventInput() :
253 configMap( "Input/Event", fgGetNode( PROPERTY_ROOT, true ), "device-named" )
257 FGEventInput::~FGEventInput()
259 for( map<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
261 input_devices.clear();
264 void FGEventInput::init( )
266 SG_LOG(SG_INPUT, SG_DEBUG, "Initializing event bindings");
267 // SGPropertyNode * base = fgGetNode("/input/event", true);
271 void FGEventInput::postinit ()
275 void FGEventInput::update( double dt )
277 // call each associated device's update() method
278 for( map<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
279 (*it).second->update( dt );
282 unsigned FGEventInput::AddDevice( FGInputDevice * inputDevice )
284 SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
285 SGPropertyNode_ptr deviceNode = NULL;
287 // look for configuration in the device map
288 if( configMap.count( inputDevice->GetName() ) > 0 ) {
289 // found - copy to /input/event/device[n]
293 for( index = 0; index < MAX_DEVICES; index++ )
294 if( (deviceNode = baseNode->getNode( "device", index, false ) ) == NULL )
297 if( index == MAX_DEVICES ) {
298 SG_LOG(SG_INPUT, SG_WARN, "To many event devices - ignoring " << inputDevice->GetName() );
299 return INVALID_DEVICE_INDEX;
303 deviceNode = baseNode->getNode( "device", index, true );
305 // and copy the properties from the configuration tree
306 copyProperties( configMap[ inputDevice->GetName() ], deviceNode );
310 if( deviceNode == NULL ) {
311 SG_LOG(SG_INPUT, SG_DEBUG, "No configuration found for device " << inputDevice->GetName() );
313 return INVALID_DEVICE_INDEX;
316 inputDevice->Configure( deviceNode );
320 input_devices[ deviceNode->getIndex() ] = inputDevice;
324 SG_LOG(SG_INPUT, SG_ALERT, "can't open InputDevice " << inputDevice->GetName() );
325 return INVALID_DEVICE_INDEX;
328 SG_LOG(SG_INPUT, SG_DEBUG, "using InputDevice " << inputDevice->GetName() );
329 return deviceNode->getIndex();
332 void FGEventInput::RemoveDevice( unsigned index )
334 // not fully implemented yet
335 SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
336 SGPropertyNode_ptr deviceNode = NULL;
338 FGInputDevice *inputDevice = input_devices[index];
340 input_devices.erase(index);
344 deviceNode = baseNode->removeChild("device", index, false);