| 1 | // FGEventInput.hxx -- 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 | #ifndef __FGEVENTINPUT_HXX |
| 24 | #define __FGEVENTINPUT_HXX |
| 25 | |
| 26 | #include "FGCommonInput.hxx" |
| 27 | |
| 28 | #include <vector> |
| 29 | #include <memory> |
| 30 | |
| 31 | #include "FGButton.hxx" |
| 32 | #include "FGDeviceConfigurationMap.hxx" |
| 33 | #include "simgear/structure/SGSourceLocation.hxx" |
| 34 | #include <simgear/misc/strutils.hxx> |
| 35 | #include <simgear/structure/subsystem_mgr.hxx> |
| 36 | |
| 37 | #include "FGEventInput_private.hxx" |
| 38 | |
| 39 | // forward decls |
| 40 | class SGInterpTable; |
| 41 | |
| 42 | /* |
| 43 | * A base structure for event data. |
| 44 | * To be extended for O/S specific implementation data |
| 45 | */ |
| 46 | struct FGEventData { |
| 47 | FGEventData( double aValue, double aDt, int aModifiers ) : modifiers(aModifiers), value(aValue), dt(aDt) {} |
| 48 | int modifiers {0}; |
| 49 | double value {0.0}; |
| 50 | double dt {0.0}; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | /* |
| 55 | * A wrapper class for a configured event. |
| 56 | * |
| 57 | * <event> |
| 58 | * <desc>Change the view pitch</desc> |
| 59 | * <name>rel-x-rotate</name> |
| 60 | * <binding> |
| 61 | * <command>property-adjust</command> |
| 62 | * <property>sim/current-view/pitch-offset-deg</property> |
| 63 | * <factor type="double">0.01</factor> |
| 64 | * <min type="double">-90.0</min> |
| 65 | * <max type="double">90.0</max> |
| 66 | * <wrap type="bool">false</wrap> |
| 67 | * </binding> |
| 68 | * <mod-xyz> |
| 69 | * <binding> |
| 70 | * ... |
| 71 | * </binding> |
| 72 | * </mod-xyz> |
| 73 | * </event> |
| 74 | */ |
| 75 | class FGInputDevice; |
| 76 | class FGInputEvent : public SGReferenced, |
| 77 | FGCommonInput |
| 78 | { |
| 79 | public: |
| 80 | FGInputEvent( FGInputDevice * device, SGPropertyNode_ptr eventNode ); |
| 81 | virtual ~FGInputEvent(); |
| 82 | |
| 83 | // dispatch the event value through all bindings |
| 84 | virtual void fire( FGEventData & eventData ); |
| 85 | |
| 86 | std::string GetName() const { return name; } |
| 87 | std::string GetDescription() const { return desc; } |
| 88 | |
| 89 | virtual void update( double dt ); |
| 90 | static FGInputEvent * NewObject( FGInputDevice * device, SGPropertyNode_ptr node ); |
| 91 | |
| 92 | protected: |
| 93 | virtual void fire(SGAbstractBinding* binding, FGEventData& eventData); |
| 94 | /* A more or less meaningfull description of the event */ |
| 95 | std::string desc; |
| 96 | |
| 97 | /* One of the predefined names of the event */ |
| 98 | std::string name; |
| 99 | |
| 100 | /* A list of SGBinding objects */ |
| 101 | binding_list_t bindings[KEYMOD_MAX]; |
| 102 | |
| 103 | /* A list of FGEventSetting objects */ |
| 104 | setting_list_t settings; |
| 105 | |
| 106 | /* A pointer to the associated device */ |
| 107 | FGInputDevice * device; |
| 108 | |
| 109 | double lastDt; |
| 110 | double intervalSec; |
| 111 | double lastSettingValue; |
| 112 | }; |
| 113 | |
| 114 | class FGButtonEvent : public FGInputEvent |
| 115 | { |
| 116 | public: |
| 117 | FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr node ); |
| 118 | virtual void fire( FGEventData & eventData ); |
| 119 | |
| 120 | void update( double dt ) override; |
| 121 | protected: |
| 122 | bool repeatable; |
| 123 | bool lastState; |
| 124 | }; |
| 125 | |
| 126 | class FGAxisEvent : public FGInputEvent |
| 127 | { |
| 128 | public: |
| 129 | FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr eventNode ); |
| 130 | ~FGAxisEvent(); |
| 131 | |
| 132 | void SetMaxRange( double value ) { maxRange = value; } |
| 133 | void SetMinRange( double value ) { minRange = value; } |
| 134 | void SetRange( double min, double max ) { minRange = min; maxRange = max; } |
| 135 | |
| 136 | protected: |
| 137 | virtual void fire( FGEventData & eventData ); |
| 138 | double tolerance; |
| 139 | double minRange; |
| 140 | double maxRange; |
| 141 | double center; |
| 142 | double deadband; |
| 143 | double lowThreshold; |
| 144 | double highThreshold; |
| 145 | double lastValue; |
| 146 | std::unique_ptr<SGInterpTable> interpolater; |
| 147 | bool mirrorInterpolater = false; |
| 148 | }; |
| 149 | |
| 150 | class FGRelAxisEvent : public FGAxisEvent |
| 151 | { |
| 152 | public: |
| 153 | FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr eventNode ); |
| 154 | |
| 155 | protected: |
| 156 | void fire(SGAbstractBinding* binding, FGEventData& eventData) override; |
| 157 | }; |
| 158 | |
| 159 | class FGAbsAxisEvent : public FGAxisEvent |
| 160 | { |
| 161 | public: |
| 162 | FGAbsAxisEvent( FGInputDevice * device, SGPropertyNode_ptr eventNode ) : FGAxisEvent( device, eventNode ) {} |
| 163 | |
| 164 | protected: |
| 165 | void fire(SGAbstractBinding* binding, FGEventData& eventData) override; |
| 166 | }; |
| 167 | |
| 168 | typedef class SGSharedPtr<FGInputEvent> FGInputEvent_ptr; |
| 169 | |
| 170 | /* |
| 171 | * A abstract class implementing basic functionality of input devices for |
| 172 | * all operating systems. This is the base class for the O/S-specific |
| 173 | * implementation of input device handlers |
| 174 | */ |
| 175 | class FGInputDevice : public SGReferenced |
| 176 | { |
| 177 | public: |
| 178 | FGInputDevice() {} |
| 179 | FGInputDevice( std::string aName, std::string aSerial = {} ) : |
| 180 | name(aName), serialNumber(aSerial) {} |
| 181 | |
| 182 | virtual ~FGInputDevice(); |
| 183 | |
| 184 | virtual bool Open() = 0; |
| 185 | virtual void Close() = 0; |
| 186 | |
| 187 | virtual void Send( const char * eventName, double value ) = 0; |
| 188 | |
| 189 | inline void Send( const std::string & eventName, double value ) { |
| 190 | Send( eventName.c_str(), value ); |
| 191 | } |
| 192 | |
| 193 | virtual void SendFeatureReport(unsigned int reportId, const simgear::UInt8Vector& data); |
| 194 | virtual void SendOutputReport(unsigned int reportId, const simgear::UInt8Vector& data); |
| 195 | |
| 196 | virtual const char * TranslateEventName( FGEventData & eventData ) = 0; |
| 197 | |
| 198 | |
| 199 | void SetName( std::string name ); |
| 200 | std::string & GetName() { return name; } |
| 201 | |
| 202 | void SetUniqueName(const std::string& name); |
| 203 | const std::string GetUniqueName() const { return _uniqueName; } |
| 204 | |
| 205 | void SetSerialNumber( std::string serial ); |
| 206 | std::string& GetSerialNumber() { return serialNumber; } |
| 207 | |
| 208 | void HandleEvent( FGEventData & eventData ); |
| 209 | |
| 210 | virtual void AddHandledEvent( FGInputEvent_ptr handledEvent ); |
| 211 | |
| 212 | virtual void Configure( SGPropertyNode_ptr deviceNode ); |
| 213 | |
| 214 | virtual void update( double dt ); |
| 215 | |
| 216 | bool GetDebugEvents () const { return debugEvents; } |
| 217 | |
| 218 | bool GetGrab() const { return grab; } |
| 219 | |
| 220 | const std::string & GetNasalModule() const { return nasalModule; } |
| 221 | std::string class_id = "FGInputDevice"; |
| 222 | protected: |
| 223 | // A map of events, this device handles |
| 224 | std::map<std::string,FGInputEvent_ptr> handledEvents; |
| 225 | |
| 226 | // the device has a name to be recognized |
| 227 | std::string name; |
| 228 | |
| 229 | // serial number string to disambiguate multiple instances |
| 230 | // of the same device |
| 231 | std::string serialNumber; |
| 232 | |
| 233 | // print out events comming in from the device |
| 234 | // if true |
| 235 | bool debugEvents = false; |
| 236 | |
| 237 | // grab the device exclusively, if O/S supports this |
| 238 | // so events are not sent to other applications |
| 239 | bool grab = false; |
| 240 | |
| 241 | //configuration in property tree |
| 242 | SGPropertyNode_ptr deviceNode; |
| 243 | SGPropertyNode_ptr lastEventName; |
| 244 | SGPropertyNode_ptr lastEventValue; |
| 245 | |
| 246 | std::string nasalModule; |
| 247 | |
| 248 | report_setting_list_t reportSettings; |
| 249 | |
| 250 | /// name, but with suffix / serial appended. This is important |
| 251 | /// when loading the device multiple times, to ensure the Nasal |
| 252 | /// module is unique |
| 253 | std::string _uniqueName; |
| 254 | }; |
| 255 | |
| 256 | typedef SGSharedPtr<FGInputDevice> FGInputDevice_ptr; |
| 257 | |
| 258 | |
| 259 | /* |
| 260 | * The Subsystem for the event input device |
| 261 | */ |
| 262 | class FGEventInput : public SGSubsystem, |
| 263 | FGCommonInput |
| 264 | { |
| 265 | public: |
| 266 | FGEventInput(); |
| 267 | FGEventInput(const char* filePath, const char* propertyRoot); |
| 268 | virtual ~FGEventInput(); |
| 269 | |
| 270 | // Subsystem API. |
| 271 | void init() override; |
| 272 | void postinit() override; |
| 273 | void shutdown() override; |
| 274 | void update(double dt) override; |
| 275 | |
| 276 | const static unsigned MAX_DEVICES = 1000; |
| 277 | const static unsigned INVALID_DEVICE_INDEX = MAX_DEVICES + 1; |
| 278 | |
| 279 | protected: |
| 280 | // where to search for configs and where to put them in the property tree |
| 281 | const char* filePath; |
| 282 | const char* propertyRoot; |
| 283 | |
| 284 | unsigned AddDevice(FGInputDevice* inputDevice); |
| 285 | void RemoveDevice( unsigned index ); |
| 286 | |
| 287 | std::map<int,FGInputDevice*> inputDevices; |
| 288 | FGDeviceConfigurationMap configMap; |
| 289 | |
| 290 | SGPropertyNode_ptr nasalClose; |
| 291 | |
| 292 | private: |
| 293 | std::string computeDeviceIndexName(FGInputDevice *dev) const; |
| 294 | }; |
| 295 | |
| 296 | #endif |