| 1 | // FGEventInput.cxx -- handle event driven input devices |
| 2 | // |
| 3 | // Written by Torsten Dreyer, started July 2009. |
| 4 | // |
| 5 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | // SPDX-FileCopyrightText: 2009 Torsten Dreyer |
| 7 | |
| 8 | #include "simgear/debug/debug_types.h" |
| 9 | #include "simgear/misc/strutils.hxx" |
| 10 | #include "simgear/nasal/nasal.h" |
| 11 | #include "simgear/structure/exception.hxx" |
| 12 | |
| 13 | #include <config.h> |
| 14 | |
| 15 | #include "FGEventInput.hxx" |
| 16 | #include <Main/fg_props.hxx> |
| 17 | #include <Scripting/NasalSys.hxx> |
| 18 | #include <cstdint> |
| 19 | #include <cstring> |
| 20 | #include <simgear/debug/ErrorReportingCallback.hxx> |
| 21 | #include <simgear/io/sg_file.hxx> |
| 22 | #include <simgear/math/SGMath.hxx> |
| 23 | #include <simgear/math/interpolater.hxx> |
| 24 | #include <simgear/misc/strutils.hxx> |
| 25 | #include <simgear/props/props_io.hxx> |
| 26 | #include <utility> |
| 27 | |
| 28 | using simgear::PropertyList; |
| 29 | using std::cout; |
| 30 | using std::endl; |
| 31 | using std::map; |
| 32 | using std::string; |
| 33 | using namespace std::string_literals; |
| 34 | |
| 35 | FGEventSetting::FGEventSetting( SGPropertyNode_ptr base ) : |
| 36 | value(0.0) |
| 37 | { |
| 38 | SGPropertyNode_ptr n; |
| 39 | |
| 40 | if( (n = base->getNode( "value" )) != NULL ) { |
| 41 | valueNode = NULL; |
| 42 | value = n->getDoubleValue(); |
| 43 | } else { |
| 44 | n = base->getNode( "property" ); |
| 45 | if( n == NULL ) { |
| 46 | SG_LOG(SG_INPUT, SG_WARN, "Neither <value> nor <property> defined for event setting:" << base->getLocation()); |
| 47 | } else { |
| 48 | valueNode = fgGetNode( n->getStringValue(), true ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if ((n = base->getChild("condition")) != NULL) { |
| 53 | condition = sgReadCondition(base, n); |
| 54 | } else { |
| 55 | simgear::reportFailure(simgear::LoadFailure::Misconfigured, |
| 56 | simgear::ErrorCode::InputDeviceConfig, |
| 57 | "No condition for event setting", |
| 58 | sg_location(base)); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | double FGEventSetting::GetValue() |
| 63 | { |
| 64 | return valueNode == NULL ? value : valueNode->getDoubleValue(); |
| 65 | } |
| 66 | |
| 67 | bool FGEventSetting::Test() |
| 68 | { |
| 69 | return condition == NULL ? true : condition->test(); |
| 70 | } |
| 71 | |
| 72 | static inline bool StartsWith( string & s, const char * cp ) |
| 73 | { |
| 74 | return s.find( cp ) == 0; |
| 75 | } |
| 76 | |
| 77 | FGInputEvent * FGInputEvent::NewObject( FGInputDevice * device, SGPropertyNode_ptr eventNode ) |
| 78 | { |
| 79 | string name = eventNode->getStringValue( "name", "" ); |
| 80 | if( StartsWith( name, "button-" ) ) |
| 81 | return new FGButtonEvent( device, eventNode ); |
| 82 | |
| 83 | if( StartsWith( name, "rel-" ) ) |
| 84 | return new FGRelAxisEvent( device, eventNode ); |
| 85 | |
| 86 | if( StartsWith( name, "abs-" ) ) |
| 87 | return new FGAbsAxisEvent( device, eventNode ); |
| 88 | |
| 89 | return new FGInputEvent( device, eventNode ); |
| 90 | } |
| 91 | |
| 92 | FGInputEvent::FGInputEvent( FGInputDevice * aDevice, SGPropertyNode_ptr eventNode ) : |
| 93 | device( aDevice ), |
| 94 | lastDt(0.0), |
| 95 | lastSettingValue(std::numeric_limits<float>::quiet_NaN()) |
| 96 | { |
| 97 | name = eventNode->getStringValue( "name", "" ); |
| 98 | desc = eventNode->getStringValue( "desc", "" ); |
| 99 | intervalSec = eventNode->getDoubleValue("interval-sec",0.0); |
| 100 | |
| 101 | read_bindings( eventNode, bindings, KEYMOD_NONE, device->GetNasalModule() ); |
| 102 | |
| 103 | for (auto child : eventNode->getChildren("setting")) |
| 104 | settings.push_back( new FGEventSetting(child) ); |
| 105 | } |
| 106 | |
| 107 | FGInputEvent::~FGInputEvent() |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | // send changed value to device (if condition matches) |
| 112 | void FGInputEvent::update( double dt ) |
| 113 | { |
| 114 | for (auto setting : settings) { |
| 115 | if( setting->Test() ) { |
| 116 | const double value = setting->GetValue(); |
| 117 | if( value != lastSettingValue ) { |
| 118 | device->Send( GetName(), value ); |
| 119 | lastSettingValue = value; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | void FGInputEvent::fire( FGEventData & eventData ) |
| 126 | { |
| 127 | lastDt += eventData.dt; |
| 128 | if( lastDt >= intervalSec ) { |
| 129 | for (auto b : bindings[eventData.modifiers]) { |
| 130 | fire(b, eventData); |
| 131 | } |
| 132 | |
| 133 | lastDt -= intervalSec; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void FGInputEvent::fire(SGAbstractBinding* binding, FGEventData& eventData) |
| 138 | { |
| 139 | binding->fire(); |
| 140 | } |
| 141 | |
| 142 | FGAxisEvent::FGAxisEvent( FGInputDevice * device, SGPropertyNode_ptr eventNode ) : |
| 143 | FGInputEvent( device, eventNode ) |
| 144 | { |
| 145 | tolerance = eventNode->getDoubleValue("tolerance", 0.002); |
| 146 | minRange = eventNode->getDoubleValue("min-range", 0.0 ); |
| 147 | maxRange = eventNode->getDoubleValue("max-range", 0.0 ); |
| 148 | center = eventNode->getDoubleValue("center", 0.0); |
| 149 | deadband = eventNode->getDoubleValue("dead-band", 0.0); |
| 150 | lowThreshold = eventNode->getDoubleValue("low-threshold", -0.9); |
| 151 | highThreshold = eventNode->getDoubleValue("high-threshold", 0.9); |
| 152 | lastValue = 9999999; |
| 153 | |
| 154 | // interpolation of values |
| 155 | if (eventNode->hasChild("interpolater")) { |
| 156 | interpolater.reset(new SGInterpTable{eventNode->getChild("interpolater")}); |
| 157 | mirrorInterpolater = eventNode->getBoolValue("interpolater/mirrored", false); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | FGAxisEvent::~FGAxisEvent() = default; |
| 162 | |
| 163 | void FGAxisEvent::fire( FGEventData & eventData ) |
| 164 | { |
| 165 | if (fabs( eventData.value - lastValue) < tolerance) |
| 166 | return; |
| 167 | lastValue = eventData.value; |
| 168 | |
| 169 | // We need a copy of the FGEventData struct to set the new value and to avoid side effects |
| 170 | FGEventData ed = eventData; |
| 171 | |
| 172 | if( minRange != maxRange ) |
| 173 | ed.value = 2.0*(eventData.value-minRange)/(maxRange-minRange)-1.0; |
| 174 | |
| 175 | if( fabs(ed.value) < deadband ) |
| 176 | ed.value = 0.0; |
| 177 | |
| 178 | if (interpolater) { |
| 179 | if ((ed.value < 0.0) && mirrorInterpolater) { |
| 180 | // mirror the positive interpolation for negative values |
| 181 | ed.value = -interpolater->interpolate(fabs(ed.value)); |
| 182 | } else { |
| 183 | ed.value = interpolater->interpolate(ed.value); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | FGInputEvent::fire( ed ); |
| 188 | } |
| 189 | |
| 190 | void FGAbsAxisEvent::fire(SGAbstractBinding* binding, FGEventData& eventData) |
| 191 | { |
| 192 | // sets the "setting" node |
| 193 | binding->fire( eventData.value ); |
| 194 | } |
| 195 | |
| 196 | FGRelAxisEvent::FGRelAxisEvent( FGInputDevice * device, SGPropertyNode_ptr eventNode ) : |
| 197 | FGAxisEvent( device, eventNode ) |
| 198 | { |
| 199 | // relative axes can't use tolerance |
| 200 | tolerance = 0.0; |
| 201 | } |
| 202 | |
| 203 | void FGRelAxisEvent::fire(SGAbstractBinding* binding, FGEventData& eventData) |
| 204 | { |
| 205 | // sets the "offset" node |
| 206 | binding->fire( eventData.value, 1.0 ); |
| 207 | } |
| 208 | |
| 209 | FGButtonEvent::FGButtonEvent( FGInputDevice * device, SGPropertyNode_ptr eventNode ) : |
| 210 | FGInputEvent( device, eventNode ), |
| 211 | repeatable(false), |
| 212 | lastState(false) |
| 213 | { |
| 214 | repeatable = eventNode->getBoolValue("repeatable", repeatable); |
| 215 | } |
| 216 | |
| 217 | void FGButtonEvent::fire( FGEventData & eventData ) |
| 218 | { |
| 219 | bool pressed = eventData.value > 0.0; |
| 220 | if (pressed) { |
| 221 | // The press event may be repeated. |
| 222 | if (!lastState || repeatable) { |
| 223 | SG_LOG( SG_INPUT, SG_DEBUG, "Button '" << this->name << "' has been pressed" ); |
| 224 | FGInputEvent::fire( eventData ); |
| 225 | } |
| 226 | } else { |
| 227 | // The release event is never repeated. |
| 228 | if (lastState) { |
| 229 | SG_LOG( SG_INPUT, SG_DEBUG, "Button '" << this->name << "' has been released" ); |
| 230 | eventData.modifiers|=KEYMOD_RELEASED; |
| 231 | FGInputEvent::fire( eventData ); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | lastState = pressed; |
| 236 | } |
| 237 | |
| 238 | void FGButtonEvent::update( double dt ) |
| 239 | { |
| 240 | if (repeatable && lastState) { |
| 241 | // interval / dt handling is done by base ::fire method |
| 242 | FGEventData ed{1.0, dt, 0 /* modifiers */}; |
| 243 | FGInputEvent::fire( ed ); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | FGInputDevice::~FGInputDevice() |
| 248 | { |
| 249 | auto nas = globals->get_subsystem<FGNasalSys>(); |
| 250 | if (nas && deviceNode ) { |
| 251 | SGPropertyNode_ptr nasal = deviceNode->getNode("nasal"); |
| 252 | if( nasal ) { |
| 253 | SGPropertyNode_ptr nasalClose = nasal->getNode("close"); |
| 254 | if (nasalClose) { |
| 255 | const string s = nasalClose->getStringValue(); |
| 256 | nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s.c_str(), s.length(), deviceNode ); |
| 257 | } |
| 258 | } |
| 259 | nas->deleteModule(nasalModule.c_str()); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | void FGInputDevice::Configure( SGPropertyNode_ptr aDeviceNode ) |
| 264 | { |
| 265 | deviceNode = aDeviceNode; |
| 266 | |
| 267 | // export our class_id to property tree |
| 268 | deviceNode->setStringValue("_class-id", class_id); |
| 269 | deviceNode->setStringValue("serial-number", serialNumber); |
| 270 | deviceNode->setStringValue("unique-name", _uniqueName); |
| 271 | |
| 272 | SG_LOG(SG_INPUT, SG_DEBUG, "FGInputDevice::Configure"); |
| 273 | |
| 274 | // use _uniqueName here so each loaded device gets its own Nasal module |
| 275 | nasalModule = string("__event:") + _uniqueName; |
| 276 | |
| 277 | for (auto ev : deviceNode->getChildren( "event" )) { |
| 278 | AddHandledEvent( FGInputEvent::NewObject( this, ev) ); |
| 279 | } |
| 280 | |
| 281 | debugEvents = deviceNode->getBoolValue("debug-events", debugEvents ); |
| 282 | grab = deviceNode->getBoolValue("grab", grab ); |
| 283 | |
| 284 | auto reportNodes = deviceNode->getChildren("report"); |
| 285 | for (auto repNode : reportNodes) { |
| 286 | FGReportSetting_ptr r = new FGReportSetting(repNode); |
| 287 | reportSettings.push_back(r); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | lastEventName = deviceNode->getNode("last-event", true)->getNode("name", true); |
| 292 | lastEventName->setStringValue(""); |
| 293 | lastEventValue = deviceNode->getNode("last-event")->getNode("value", true); |
| 294 | lastEventValue->setDoubleValue(0.0); |
| 295 | |
| 296 | SGPropertyNode_ptr nasal = deviceNode->getNode("nasal"); |
| 297 | if (nasal) { |
| 298 | SGPropertyNode_ptr open = nasal->getNode("open"); |
| 299 | if (open) { |
| 300 | const string s = open->getStringValue(); |
| 301 | auto nas = globals->get_subsystem<FGNasalSys>(); |
| 302 | if (nas) |
| 303 | nas->createModule(nasalModule.c_str(), nasalModule.c_str(), s.c_str(), s.length(), deviceNode ); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void FGInputDevice::AddHandledEvent( FGInputEvent_ptr event ) |
| 309 | { |
| 310 | auto it = handledEvents.find(event->GetName()); |
| 311 | if (it == handledEvents.end()) { |
| 312 | handledEvents.insert(it, std::make_pair(event->GetName(), event)); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void FGInputDevice::update( double dt ) |
| 317 | { |
| 318 | for( map<string,FGInputEvent_ptr>::iterator it = handledEvents.begin(); it != handledEvents.end(); it++ ) |
| 319 | (*it).second->update( dt ); |
| 320 | |
| 321 | for (auto r : reportSettings) { |
| 322 | if (r->hasError()) { |
| 323 | continue; |
| 324 | } |
| 325 | |
| 326 | try { |
| 327 | if (r->Test()) { |
| 328 | auto reportData = r->reportBytes(nasalModule); |
| 329 | if (debugEvents) { |
| 330 | SG_LOG(SG_INPUT, SG_INFO, class_id << " " << GetUniqueName() << ": Sending report " << r->getReportId() << simgear::strutils::encodeHex(reportData)); |
| 331 | } |
| 332 | if (r->getReportType() == FGReportSetting::Type::Feature) { |
| 333 | SendFeatureReport(r->getReportId(), reportData); |
| 334 | } else { |
| 335 | SendOutputReport(r->getReportId(), reportData); |
| 336 | } |
| 337 | } |
| 338 | } catch (sg_exception& e) { |
| 339 | r->markAsError(); |
| 340 | simgear::reportFailure(simgear::LoadFailure::Unknown, |
| 341 | simgear::ErrorCode::InputDeviceConfig, |
| 342 | "Failed to send report:"s + e.getMessage(), |
| 343 | e.getLocation()); |
| 344 | } |
| 345 | } // of report setting iteration |
| 346 | } |
| 347 | |
| 348 | void FGInputDevice::HandleEvent( FGEventData & eventData ) |
| 349 | { |
| 350 | string eventName = TranslateEventName( eventData ); |
| 351 | if( debugEvents ) { |
| 352 | SG_LOG(SG_INPUT, SG_INFO, class_id << " " << GetUniqueName() << " has event " << |
| 353 | eventName << " modifiers=" << eventData.modifiers << " value=" << eventData.value); |
| 354 | } |
| 355 | lastEventName->setStringValue(eventName); |
| 356 | lastEventValue->setValue(eventData.value); |
| 357 | if( handledEvents.count( eventName ) > 0 ) { |
| 358 | handledEvents[ eventName ]->fire( eventData ); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void FGInputDevice::SetName( string name ) |
| 363 | { |
| 364 | this->name = name; |
| 365 | } |
| 366 | |
| 367 | void FGInputDevice::SetUniqueName(const std::string &name) |
| 368 | { |
| 369 | _uniqueName = name; |
| 370 | } |
| 371 | |
| 372 | void FGInputDevice::SetSerialNumber( std::string serial ) |
| 373 | { |
| 374 | serialNumber = serial; |
| 375 | } |
| 376 | |
| 377 | void FGInputDevice::SendFeatureReport(unsigned int reportId, const simgear::UInt8Vector& data) |
| 378 | { |
| 379 | SG_LOG(SG_INPUT, SG_WARN, "SendFeatureReport not implemented"); |
| 380 | } |
| 381 | |
| 382 | void FGInputDevice::SendOutputReport(unsigned int reportId, const simgear::UInt8Vector& data) |
| 383 | { |
| 384 | SG_LOG(SG_INPUT, SG_WARN, "SendOutputReport not implemented"); |
| 385 | } |
| 386 | |
| 387 | FGEventInput::FGEventInput() = default; |
| 388 | |
| 389 | FGEventInput::FGEventInput(const char* filePath, const char* propertyRoot): |
| 390 | filePath(filePath), |
| 391 | propertyRoot(propertyRoot) |
| 392 | { |
| 393 | } |
| 394 | |
| 395 | FGEventInput::~FGEventInput() = default; |
| 396 | |
| 397 | void FGEventInput::shutdown() |
| 398 | { |
| 399 | SG_LOG(SG_INPUT, SG_DEBUG, "FGEventInput::shutdown()"); |
| 400 | auto tmp = inputDevices; |
| 401 | for (auto it : tmp) { |
| 402 | RemoveDevice(it.first); |
| 403 | } |
| 404 | inputDevices.clear(); |
| 405 | } |
| 406 | |
| 407 | void FGEventInput::init( ) |
| 408 | { |
| 409 | configMap = FGDeviceConfigurationMap(filePath, fgGetNode(propertyRoot, true), "device-named"); |
| 410 | } |
| 411 | |
| 412 | void FGEventInput::postinit () |
| 413 | { |
| 414 | } |
| 415 | |
| 416 | void FGEventInput::update( double dt ) |
| 417 | { |
| 418 | for (auto it : inputDevices) { |
| 419 | it.second->update(dt); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | std::string FGEventInput::computeDeviceIndexName(FGInputDevice* dev) const |
| 424 | { |
| 425 | int count = 0; |
| 426 | const auto devName = dev->GetName(); |
| 427 | for (auto it : inputDevices) { |
| 428 | if (it.second->GetName() == devName) { |
| 429 | ++count; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | std::ostringstream os; |
| 434 | os << devName << "_" << count; |
| 435 | return os.str(); |
| 436 | } |
| 437 | |
| 438 | unsigned FGEventInput::AddDevice( FGInputDevice * inputDevice ) |
| 439 | { |
| 440 | SGPropertyNode_ptr baseNode = fgGetNode( propertyRoot, true ); |
| 441 | SGPropertyNode_ptr deviceNode = nullptr; |
| 442 | |
| 443 | const string deviceName = inputDevice->GetName(); |
| 444 | SGPropertyNode_ptr configNode = nullptr; |
| 445 | |
| 446 | // if we have a serial number set, try using that to select a specific configuration |
| 447 | if (!inputDevice->GetSerialNumber().empty()) { |
| 448 | const string nameWithSerial = deviceName + "::" + inputDevice->GetSerialNumber(); |
| 449 | if (configMap.hasConfiguration(nameWithSerial)) { |
| 450 | configNode = configMap.configurationForDeviceName(nameWithSerial); |
| 451 | SG_LOG(SG_INPUT, SG_INFO, "using instance-specific configuration for device " |
| 452 | << nameWithSerial << " : " << configNode->getStringValue("source")); |
| 453 | inputDevice->SetUniqueName(nameWithSerial); |
| 454 | } |
| 455 | } |
| 456 | if (configNode == nullptr) { |
| 457 | const auto nameWithIndex = computeDeviceIndexName(inputDevice); |
| 458 | // try instanced (counted) name |
| 459 | if (configMap.hasConfiguration(nameWithIndex)) { |
| 460 | configNode = configMap.configurationForDeviceName(nameWithIndex); |
| 461 | SG_LOG(SG_INPUT, SG_INFO, "using instance-specific configuration for device " |
| 462 | << nameWithIndex << " : " << configNode->getStringValue("source")); |
| 463 | } |
| 464 | // otherwise try the unmodified name for the device |
| 465 | else if (configMap.hasConfiguration(deviceName)) { |
| 466 | configNode = configMap.configurationForDeviceName(deviceName); |
| 467 | } else { |
| 468 | SG_LOG(SG_INPUT, SG_INFO, "No configuration found for device " << deviceName); |
| 469 | delete inputDevice; |
| 470 | return INVALID_DEVICE_INDEX; |
| 471 | } |
| 472 | inputDevice->SetUniqueName(nameWithIndex); |
| 473 | } |
| 474 | |
| 475 | // found - copy to /input/event/device[n] |
| 476 | // find a free index |
| 477 | unsigned int index; |
| 478 | for (index = 0; index < MAX_DEVICES; index++) { |
| 479 | if ((deviceNode = baseNode->getNode("device", index, false)) == nullptr) |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | if (index == MAX_DEVICES) { |
| 484 | SG_LOG(SG_INPUT, SG_WARN, "To many event devices - ignoring " << inputDevice->GetUniqueName() ); |
| 485 | delete inputDevice; |
| 486 | return INVALID_DEVICE_INDEX; |
| 487 | } |
| 488 | |
| 489 | // create this node |
| 490 | deviceNode = baseNode->getNode( "device", index, true ); |
| 491 | |
| 492 | // and copy the properties from the configuration tree |
| 493 | copyProperties(configNode, deviceNode ); |
| 494 | |
| 495 | inputDevice->Configure( deviceNode ); |
| 496 | |
| 497 | bool ok = inputDevice->Open(); |
| 498 | if (!ok) { |
| 499 | // TODO repot a better error here, to the user |
| 500 | SG_LOG(SG_INPUT, SG_ALERT, "can't open InputDevice " << inputDevice->GetUniqueName()); |
| 501 | delete inputDevice; |
| 502 | return INVALID_DEVICE_INDEX; |
| 503 | } |
| 504 | |
| 505 | inputDevices[deviceNode->getIndex()] = inputDevice; |
| 506 | |
| 507 | SG_LOG(SG_INPUT, SG_INFO, inputDevice->class_id << "::AddDevice '" << inputDevice->GetUniqueName() << "' s/n: " << inputDevice->GetSerialNumber() ); |
| 508 | return deviceNode->getIndex(); |
| 509 | } |
| 510 | |
| 511 | void FGEventInput::RemoveDevice( unsigned index ) |
| 512 | { |
| 513 | // not fully implemented yet |
| 514 | SGPropertyNode_ptr baseNode = fgGetNode( propertyRoot, true ); |
| 515 | SGPropertyNode_ptr deviceNode = NULL; |
| 516 | |
| 517 | SG_LOG(SG_INPUT, SG_DEBUG, "FGEventInput::RemoveDevice(" << index << ") "); |
| 518 | FGInputDevice *inputDevice = inputDevices[index]; |
| 519 | if (inputDevice) { |
| 520 | SG_LOG(SG_INPUT, SG_DEBUG, "\tremoving (" << index << ") " << inputDevice->GetUniqueName()); |
| 521 | inputDevice->Close(); |
| 522 | inputDevices.erase(index); |
| 523 | delete inputDevice; |
| 524 | } |
| 525 | deviceNode = baseNode->removeChild("device", index); |
| 526 | } |
| 527 | |
| 528 | FGReportSetting::FGReportSetting( SGPropertyNode_ptr base ) |
| 529 | { |
| 530 | location = base->getLocation(); |
| 531 | reportId = base->getIntValue("report-id"); |
| 532 | nasalFunction = base->getStringValue("nasal-function"); |
| 533 | |
| 534 | if (base->hasChild("report-type")) { |
| 535 | const auto s = base->getStringValue("report-type"); |
| 536 | if (s == "output") { |
| 537 | _type = Type::Output; |
| 538 | } else if (s == "feature") { |
| 539 | _type = Type::Feature; |
| 540 | } else { |
| 541 | simgear::reportFailure(simgear::LoadFailure::Misconfigured, |
| 542 | simgear::ErrorCode::InputDeviceConfig, |
| 543 | "Invalid report type:" + s, |
| 544 | sg_location(base)); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | auto watchNodes = base->getChildren("watch"); |
| 549 | for (auto w : watchNodes) { |
| 550 | std::string path = w->getStringValue(); |
| 551 | SGPropertyNode_ptr n = globals->get_props()->getNode(path, true); |
| 552 | n->addChangeListener(this); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | bool FGReportSetting::Test() |
| 557 | { |
| 558 | bool d = dirty; |
| 559 | dirty = false; |
| 560 | return d; |
| 561 | } |
| 562 | |
| 563 | simgear::UInt8Vector FGReportSetting::reportBytes(const std::string& moduleName) const |
| 564 | { |
| 565 | auto nas = globals->get_subsystem<FGNasalSys>(); |
| 566 | if (!nas) { |
| 567 | return {}; |
| 568 | } |
| 569 | |
| 570 | naRef module = nas->getModule(moduleName.c_str()); |
| 571 | if (naIsNil(module)) { |
| 572 | throw sg_exception("Unknown Nasal module:" + moduleName, nasalFunction, sg_location(location)); |
| 573 | } |
| 574 | |
| 575 | naRef func = naHash_cget(module, (char*) nasalFunction.c_str()); |
| 576 | if (!naIsFunc(func)) { |
| 577 | throw sg_exception("Not a Nasal function:" + nasalFunction, nasalFunction, sg_location(location)); |
| 578 | } |
| 579 | |
| 580 | naRef result = nas->call(func, 0, 0, naNil()); |
| 581 | if (naIsString(result)) { |
| 582 | size_t len = naStr_len(result); |
| 583 | char* bytes = naStr_data(result); |
| 584 | char* endByte = bytes + len; |
| 585 | return simgear::UInt8Vector( |
| 586 | reinterpret_cast<uint8_t*>(bytes), |
| 587 | reinterpret_cast<uint8_t*>(endByte)); |
| 588 | } |
| 589 | |
| 590 | if (naIsVector(result)) { |
| 591 | int len = naVec_size(result); |
| 592 | simgear::UInt8Vector d; |
| 593 | for (int b=0; b < len; ++b) { |
| 594 | int num = naNumValue(naVec_get(result, b)).num; |
| 595 | d.push_back(static_cast<uint8_t>(num)); |
| 596 | } |
| 597 | |
| 598 | return d; |
| 599 | } |
| 600 | |
| 601 | // allow returning nil to mean no data |
| 602 | if (naIsNil(result)) { |
| 603 | return {}; |
| 604 | } |
| 605 | |
| 606 | throw sg_exception("Bad data from report setting", nasalFunction, |
| 607 | sg_location(location)); |
| 608 | } |
| 609 | |
| 610 | void FGReportSetting::valueChanged(SGPropertyNode* n) |
| 611 | { |
| 612 | auto it = watchValueCache.find(n); |
| 613 | const auto val = n->getStringValue(); |
| 614 | if (it == watchValueCache.end()) { |
| 615 | watchValueCache.insert(std::make_pair(n, val)); |
| 616 | dirty = true; |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | // because we use string equality, for floating-point values, we will |
| 621 | // quantise to the precision of the string representation. |
| 622 | // that's an almost-feature, until we define explicit precision on properties |
| 623 | if (val == it->second) { |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | it->second = val; |
| 628 | dirty = true; |
| 629 | } |