2009-08-07 18:56:48 +00:00
|
|
|
// FGEventInput.hxx -- handle event driven input devices
|
|
|
|
//
|
|
|
|
// Written by Torsten Dreyer, started July 2009
|
|
|
|
//
|
|
|
|
// Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
|
|
|
|
#ifndef __FGEVENTINPUT_HXX
|
|
|
|
#define __FGEVENTINPUT_HXX
|
|
|
|
|
|
|
|
#include "FGCommonInput.hxx"
|
|
|
|
#include "FGButton.hxx"
|
|
|
|
#include "FGDeviceConfigurationMap.hxx"
|
|
|
|
#include <simgear/structure/subsystem_mgr.hxx>
|
|
|
|
|
|
|
|
/*
|
2009-08-11 15:59:30 +00:00
|
|
|
* A base structure for event data.
|
|
|
|
* To be extended for O/S specific implementation data
|
2009-08-07 18:56:48 +00:00
|
|
|
*/
|
|
|
|
struct FGEventData {
|
2009-08-20 22:06:38 +00:00
|
|
|
FGEventData( double aValue, double aDt, int aModifiers ) : modifiers(aModifiers), value(aValue), dt(aDt) {}
|
2009-08-11 15:59:30 +00:00
|
|
|
int modifiers;
|
2009-08-07 18:56:48 +00:00
|
|
|
double value;
|
|
|
|
double dt;
|
|
|
|
};
|
|
|
|
|
2009-08-11 15:59:30 +00:00
|
|
|
class FGEventSetting : public SGReferenced {
|
|
|
|
public:
|
|
|
|
FGEventSetting( SGPropertyNode_ptr base );
|
|
|
|
|
|
|
|
bool Test();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* access for the value property
|
|
|
|
*/
|
|
|
|
double GetValue();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
double value;
|
|
|
|
SGPropertyNode_ptr valueNode;
|
|
|
|
SGSharedPtr<const SGCondition> condition;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SGSharedPtr<FGEventSetting> FGEventSetting_ptr;
|
|
|
|
typedef vector<FGEventSetting_ptr> setting_list_t;
|
|
|
|
|
2009-08-07 18:56:48 +00:00
|
|
|
/*
|
|
|
|
* A wrapper class for a configured event.
|
|
|
|
*
|
|
|
|
* <event>
|
|
|
|
* <desc>Change the view pitch</desc>
|
|
|
|
* <name>rel-x-rotate</name>
|
|
|
|
* <binding>
|
|
|
|
* <command>property-adjust</command>
|
|
|
|
* <property>sim/current-view/pitch-offset-deg</property>
|
|
|
|
* <factor type="double">0.01</factor>
|
|
|
|
* <min type="double">-90.0</min>
|
|
|
|
* <max type="double">90.0</max>
|
|
|
|
* <wrap type="bool">false</wrap>
|
|
|
|
* </binding>
|
2009-08-11 15:59:30 +00:00
|
|
|
* <mod-xyz>
|
|
|
|
* <binding>
|
|
|
|
* ...
|
|
|
|
* </binding>
|
|
|
|
* </mod-xyz>
|
2009-08-07 18:56:48 +00:00
|
|
|
* </event>
|
|
|
|
*/
|
2009-08-11 15:59:30 +00:00
|
|
|
class FGInputDevice;
|
2009-08-07 18:56:48 +00:00
|
|
|
class FGInputEvent : public SGReferenced,FGCommonInput {
|
|
|
|
public:
|
|
|
|
/*
|
|
|
|
* Constructor for the class. The arg node shall point
|
|
|
|
* to the property corresponding to the <event> node
|
|
|
|
*/
|
2009-08-11 15:59:30 +00:00
|
|
|
FGInputEvent( FGInputDevice * device, SGPropertyNode_ptr node );
|
2009-08-07 18:56:48 +00:00
|
|
|
virtual ~FGInputEvent();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dispatch the event value through all bindings
|
|
|
|
*/
|
|
|
|
virtual void fire( FGEventData & eventData );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* access for the name property
|
|
|
|
*/
|
|
|
|
string GetName() const { return name; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* access for the description property
|
|
|
|
*/
|
|
|
|
string GetDescription() const { return desc; }
|
|
|
|
|
2009-08-11 15:59:30 +00:00
|
|
|
virtual void update( double dt );
|
|
|
|
|
|
|
|
static FGInputEvent * NewObject( FGInputDevice * device, SGPropertyNode_ptr node );
|
2009-08-07 18:56:48 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/* A more or less meaningfull description of the event */
|
|
|
|
string desc;
|
|
|
|
|
|
|
|
/* One of the predefined names of the event */
|
|
|
|
string name;
|
|
|
|
|
|
|
|
/* A list of SGBinding objects */
|
|
|
|
binding_list_t bindings[KEYMOD_MAX];
|
|
|
|
|
2009-08-11 15:59:30 +00:00
|
|
|
/* A list of FGEventSetting objects */
|
|
|
|
setting_list_t settings;
|
|
|
|
|
|
|
|
/* A pointer to the associated device */
|
|
|
|
FGInputDevice * device;
|
|
|
|
|
2009-08-07 18:56:48 +00:00
|
|
|
double lastDt;
|
|
|
|
double intervalSec;
|
2009-08-11 15:59:30 +00:00
|
|
|
double lastSettingValue;
|
2009-08-07 18:56:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FGButtonEvent : public FGInputEvent {
|
|
|
|
public:
|
2009-08-11 15:59:30 +00:00
|
|
|
FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node );
|
2009-08-07 18:56:48 +00:00
|
|
|
virtual void fire( FGEventData & eventData );
|
2009-08-11 15:59:30 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool repeatable;
|
|
|
|
bool lastState;
|
2009-08-07 18:56:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class FGAxisEvent : public FGInputEvent {
|
|
|
|
public:
|
2009-08-11 15:59:30 +00:00
|
|
|
FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr node );
|
2009-08-07 18:56:48 +00:00
|
|
|
protected:
|
|
|
|
virtual void fire( FGEventData & eventData );
|
|
|
|
double tolerance;
|
|
|
|
double minRange;
|
|
|
|
double maxRange;
|
|
|
|
double center;
|
|
|
|
double deadband;
|
|
|
|
double lowThreshold;
|
|
|
|
double highThreshold;
|
|
|
|
double lastValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef class SGSharedPtr<FGInputEvent> FGInputEvent_ptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A abstract class implementing basic functionality of input devices for
|
|
|
|
* all operating systems. This is the base class for the O/S-specific
|
|
|
|
* implementation of input device handlers
|
|
|
|
*/
|
|
|
|
class FGInputDevice : public SGReferenced {
|
|
|
|
public:
|
2009-08-22 08:32:36 +00:00
|
|
|
FGInputDevice() : debugEvents(false), grab(false) {}
|
2009-08-07 18:56:48 +00:00
|
|
|
FGInputDevice( string aName ) : name(aName) {}
|
|
|
|
|
|
|
|
virtual ~FGInputDevice();
|
|
|
|
|
|
|
|
virtual void Open() = 0;
|
|
|
|
virtual void Close() = 0;
|
2009-08-11 15:59:30 +00:00
|
|
|
|
|
|
|
virtual void Send( const char * eventName, double value ) = 0;
|
|
|
|
|
|
|
|
inline void Send( const string & eventName, double value ) {
|
|
|
|
Send( eventName.c_str(), value );
|
|
|
|
}
|
|
|
|
|
2009-08-07 18:56:48 +00:00
|
|
|
virtual const char * TranslateEventName( FGEventData & eventData ) = 0;
|
|
|
|
|
|
|
|
|
|
|
|
void SetName( string name );
|
|
|
|
string & GetName() { return name; }
|
|
|
|
|
|
|
|
void HandleEvent( FGEventData & eventData );
|
2009-08-11 15:59:30 +00:00
|
|
|
|
2009-08-07 18:56:48 +00:00
|
|
|
void AddHandledEvent( FGInputEvent_ptr handledEvent ) {
|
|
|
|
if( handledEvents.count( handledEvent->GetName() ) == 0 )
|
|
|
|
handledEvents[handledEvent->GetName()] = handledEvent;
|
|
|
|
}
|
|
|
|
|
2009-08-11 15:59:30 +00:00
|
|
|
virtual void update( double dt );
|
|
|
|
|
2009-08-12 06:08:12 +00:00
|
|
|
bool GetDebugEvents () const { return debugEvents; }
|
|
|
|
void SetDebugEvents( bool value ) { debugEvents = value; }
|
|
|
|
|
2009-08-22 08:32:36 +00:00
|
|
|
bool GetGrab() const { return grab; }
|
|
|
|
void SetGrab( bool value ) { grab = value; }
|
|
|
|
|
2009-08-07 18:56:48 +00:00
|
|
|
private:
|
2009-08-11 15:59:30 +00:00
|
|
|
// A map of events, this device handles
|
2009-08-07 18:56:48 +00:00
|
|
|
map<string,FGInputEvent_ptr> handledEvents;
|
2009-08-11 15:59:30 +00:00
|
|
|
|
|
|
|
// the device has a name to be recognized
|
2009-08-07 18:56:48 +00:00
|
|
|
string name;
|
2009-08-12 06:08:12 +00:00
|
|
|
|
|
|
|
// print out events comming in from the device
|
|
|
|
// if true
|
|
|
|
bool debugEvents;
|
2009-08-22 08:32:36 +00:00
|
|
|
|
|
|
|
// grab the device exclusively, if O/S supports this
|
|
|
|
// so events are not sent to other applications
|
|
|
|
bool grab;
|
2009-08-07 18:56:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef SGSharedPtr<FGInputDevice> FGInputDevice_ptr;
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Subsystem for the event input device
|
|
|
|
*/
|
|
|
|
class FGEventInput : public SGSubsystem,FGCommonInput {
|
|
|
|
public:
|
|
|
|
FGEventInput();
|
|
|
|
virtual ~FGEventInput();
|
|
|
|
virtual void init();
|
|
|
|
virtual void postinit();
|
2009-08-11 15:59:30 +00:00
|
|
|
virtual void update( double dt );
|
2009-08-07 18:56:48 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static const char * PROPERTY_ROOT;
|
|
|
|
|
|
|
|
void AddDevice( FGInputDevice * inputDevice );
|
|
|
|
map<int,FGInputDevice*> input_devices;
|
|
|
|
FGDeviceConfigurationMap configMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|