]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGEventInput.cxx
Initial commit of the new sound system, expect more updates to follow
[flightgear.git] / src / Input / FGEventInput.cxx
1 // FGEventInput.cxx -- handle event driven input devices
2 //
3 // Written by Torsten Dreyer, started July 2009.
4 //
5 // Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include "FGEventInput.hxx"
28 #include <Main/fg_props.hxx>
29 #include <simgear/io/sg_file.hxx>
30 #include <Scripting/NasalSys.hxx>
31
32 FGEventSetting::FGEventSetting( SGPropertyNode_ptr base ) :
33   value(0.0)
34 {
35   SGPropertyNode_ptr n;
36
37   if( (n = base->getNode( "value" )) != NULL ) {  
38     valueNode = NULL;
39     value = n->getDoubleValue();
40   } else {
41     n = base->getNode( "property" );
42     if( n == NULL ) {
43       SG_LOG( SG_INPUT, SG_WARN, "Neither <value> nor <property> defined for event setting." );
44     } else {
45       valueNode = fgGetNode( n->getStringValue(), true );
46     }
47   }
48
49   if( (n = base->getChild("condition")) != NULL )
50     condition = sgReadCondition(base, n);
51   else
52     SG_LOG( SG_INPUT, SG_ALERT, "No condition for event setting." );
53 }
54
55 double FGEventSetting::GetValue()
56 {
57   return valueNode == NULL ? value : valueNode->getDoubleValue();
58 }
59
60 bool FGEventSetting::Test()
61 {
62   return condition == NULL ? true : condition->test();
63 }
64
65 static inline bool StartsWith( string & s, const char * cp )
66 {
67   return s.compare( 0, strlen(cp), cp ) == 0;
68 }
69
70 FGInputEvent * FGInputEvent::NewObject( FGInputDevice * device, SGPropertyNode_ptr node )
71 {
72   string name = node->getStringValue( "name", "" );
73   if( StartsWith( name, "button-" ) )
74     return new FGButtonEvent( device, node );
75
76   if( StartsWith( name, "rel-" ) )
77     return new FGAxisEvent( device, node );
78
79   if( StartsWith( name, "abs-" ) )
80     return new FGAxisEvent( device, node );
81
82   return new FGInputEvent( device, node );
83 }
84
85 FGInputEvent::FGInputEvent( FGInputDevice * aDevice, SGPropertyNode_ptr node ) :
86   device( aDevice ),
87   lastDt(0.0), 
88   lastSettingValue(std::numeric_limits<float>::quiet_NaN())
89 {
90   name = node->getStringValue( "name", "" );
91   desc = node->getStringValue( "desc", "" );
92   intervalSec = node->getDoubleValue("interval-sec",0.0);
93   
94   read_bindings( node, bindings, KEYMOD_NONE, device->GetNasalModule() );
95
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 ) );
99 }
100
101 FGInputEvent::~FGInputEvent()
102 {
103 }
104
105 void FGInputEvent::update( double dt )
106 {
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;
113       }
114     }
115   }
116 }
117
118 void FGInputEvent::fire( FGEventData & eventData )
119 {
120   lastDt += eventData.dt;
121   if( lastDt >= intervalSec ) {
122
123     for( binding_list_t::iterator it = bindings[eventData.modifiers].begin(); it != bindings[eventData.modifiers].end(); it++ )
124       (*it)->fire( eventData.value, 1.0 );
125
126     lastDt -= intervalSec;
127   }
128 }
129
130 FGAxisEvent::FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
131   FGInputEvent( device, node )
132 {
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);
140   lastValue = 9999999;
141 }
142
143 void FGAxisEvent::fire( FGEventData & eventData )
144 {
145   if (fabs( eventData.value - lastValue) < tolerance)
146     return;
147   lastValue = eventData.value;
148   FGInputEvent::fire( eventData );
149 }
150
151 FGButtonEvent::FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node ) :
152   FGInputEvent( device, node ),
153   repeatable(false),
154   lastState(false)
155 {
156   repeatable = node->getBoolValue("repeatable", repeatable);
157 }
158
159 void FGButtonEvent::fire( FGEventData & eventData )
160 {
161   bool pressed = eventData.value > 0.0;
162   if (pressed) {
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 );
167     }
168   } else {
169     // The release event is never repeated.
170     if (lastState) {
171       SG_LOG( SG_INPUT, SG_DEBUG, "Button has been released" );
172       eventData.modifiers|=KEYMOD_RELEASED;
173       FGInputEvent::fire( eventData );
174     }
175   }
176           
177   lastState = pressed;
178 }
179
180 FGInputDevice::~FGInputDevice()
181 {
182   FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
183   if (nas && deviceNode ) {
184     SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
185     if( nasal ) {
186       SGPropertyNode_ptr nasalClose = nasal->getNode("close");
187       if (nasalClose) {
188         const char *s = nasalClose->getStringValue();
189         nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s, strlen(s), deviceNode );
190       }
191     }
192     nas->deleteModule(nasalModule.c_str());
193   }
194
195
196 void FGInputDevice::Configure( SGPropertyNode_ptr aDeviceNode )
197 {
198   deviceNode = aDeviceNode;
199
200   nasalModule = string("__event:") + GetName();
201
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 ) );
205
206   debugEvents = deviceNode->getBoolValue("debug-events", debugEvents );
207   grab = deviceNode->getBoolValue("grab", grab );
208
209   // TODO:
210   // add nodes for the last event:
211   // last-event/name [string]
212   // last-event/value [double]
213
214   SGPropertyNode_ptr nasal = deviceNode->getNode("nasal");
215   if (nasal) {
216     SGPropertyNode_ptr open = nasal->getNode("open");
217     if (open) {
218       const char *s = open->getStringValue();
219       FGNasalSys *nas = (FGNasalSys *)globals->get_subsystem("nasal");
220       if (nas)
221         nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s, strlen(s), deviceNode );
222     }
223   }
224
225 }
226
227 void FGInputDevice::update( double dt )
228 {
229   for( map<string,FGInputEvent_ptr>::iterator it = handledEvents.begin(); it != handledEvents.end(); it++ )
230     (*it).second->update( dt );
231 }
232
233 void FGInputDevice::HandleEvent( FGEventData & eventData )
234 {
235   string eventName = TranslateEventName( eventData );  
236   if( debugEvents )
237     cout << GetName() << " has event " << 
238     eventName << " modifiers=" << eventData.modifiers << " value=" << eventData.value << endl;
239
240   if( handledEvents.count( eventName ) > 0 ) {
241     handledEvents[ eventName ]->fire( eventData );
242   }
243 }
244
245 void FGInputDevice::SetName( string name )
246 {
247   this->name = name; 
248 }
249
250 const char * FGEventInput::PROPERTY_ROOT = "/input/event";
251
252 FGEventInput::FGEventInput() : 
253   configMap( "Input/Event", fgGetNode( PROPERTY_ROOT, true ), "device-named" )
254 {
255 }
256
257 FGEventInput::~FGEventInput()
258 {
259   for( map<int,FGInputDevice*>::iterator it = input_devices.begin(); it != input_devices.end(); it++ )
260     delete (*it).second;
261   input_devices.clear();
262 }
263
264 void FGEventInput::init( )
265 {
266   SG_LOG(SG_INPUT, SG_DEBUG, "Initializing event bindings");
267 //  SGPropertyNode * base = fgGetNode("/input/event", true);
268
269 }
270
271 void FGEventInput::postinit ()
272 {
273 }
274
275 void FGEventInput::update( double dt )
276 {
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 );
280 }
281
282 unsigned FGEventInput::AddDevice( FGInputDevice * inputDevice )
283 {
284   SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
285   SGPropertyNode_ptr deviceNode = NULL;
286
287   // look for configuration in the device map
288   if( configMap.count( inputDevice->GetName() ) > 0 ) {
289     // found - copy to /input/event/device[n]
290
291     // find a free index
292     unsigned index;
293     for( index = 0; index < MAX_DEVICES; index++ )
294       if( (deviceNode = baseNode->getNode( "device", index, false ) ) == NULL )
295         break;
296
297     if( index == MAX_DEVICES ) {
298       SG_LOG(SG_INPUT, SG_WARN, "To many event devices - ignoring " << inputDevice->GetName() );
299       return INVALID_DEVICE_INDEX;
300     }
301
302     // create this node 
303     deviceNode = baseNode->getNode( "device", index, true );
304
305     // and copy the properties from the configuration tree
306     copyProperties( configMap[ inputDevice->GetName() ], deviceNode );
307
308   }
309
310   if( deviceNode == NULL ) {
311     SG_LOG(SG_INPUT, SG_DEBUG, "No configuration found for device " << inputDevice->GetName() );
312     delete  inputDevice;
313     return INVALID_DEVICE_INDEX;
314   }
315
316   inputDevice->Configure( deviceNode );
317
318   try { 
319     inputDevice->Open();
320     input_devices[ deviceNode->getIndex() ] = inputDevice;
321   }
322   catch( ... ) {
323     delete  inputDevice;
324     SG_LOG(SG_INPUT, SG_ALERT, "can't open InputDevice " << inputDevice->GetName()  );
325     return INVALID_DEVICE_INDEX;
326   }
327
328   SG_LOG(SG_INPUT, SG_DEBUG, "using InputDevice " << inputDevice->GetName()  );
329   return deviceNode->getIndex();
330 }
331
332 void FGEventInput::RemoveDevice( unsigned index )
333 {
334   // not fully implemented yet
335   SGPropertyNode_ptr baseNode = fgGetNode( PROPERTY_ROOT, true );
336   SGPropertyNode_ptr deviceNode = NULL;
337
338   FGInputDevice *inputDevice = input_devices[index];
339   if (inputDevice) {
340     input_devices.erase(index);
341     delete inputDevice;
342     
343   }
344   deviceNode = baseNode->removeChild("device", index, false);
345 }
346