Debian Codegraph @

src/Main/fg_props.cxx

raw symbols SCIP
1 // fg_props.cxx -- support for FlightGear properties.
2 //
3 // Written by David Megginson, started 2000.
4 //
5 // Copyright (C) 2000, 2001 David Megginson - [email protected]
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 #include "config.h"
24
25 #include <simgear/compiler.h>
26 #include <simgear/structure/exception.hxx>
27 #include <simgear/props/props_io.hxx>
28 #include <simgear/misc/strutils.hxx>
29 #include <simgear/timing/sg_time.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/scene/model/particles.hxx>
32 #include <simgear/sound/soundmgr.hxx>
33
34 #include <GUI/gui.h>
35
36 #include "globals.hxx"
37 #include "fg_props.hxx"
38
39 using std::string;
40 ////////////////////////////////////////////////////////////////////////
41 // Default property bindings (not yet handled by any module).
42 ////////////////////////////////////////////////////////////////////////
43
44 /**
45 * Get the logging classes.
46 */
47 // XXX Making the result buffer be global is a band-aid that hopefully
48 // delays its destruction 'til after its last use.
49 namespace
50 {
51 string loggingResult;
52 }
53
54 static const char *
55 getLoggingClasses ()
56 {
57 loggingResult = sglog().getLogClassesAsString();
58 return loggingResult.c_str();
59 }
60
61 static void
62 addLoggingClass (const string &name)
63 {
64 sglog().addLogClass(name);
65 }
66
67
68 /**
69 * Set the logging classes.
70 */
71 void
72 setLoggingClasses (const char * c)
73 {
74 sglog().parseLogClasses(c);
75 }
76
77 /**
78 * Get the logging priority.
79 */
80 static const char *
81 getLoggingPriority ()
82 {
83 switch (sglog().get_log_priority()) {
84 case SG_BULK:
85 return "bulk";
86 case SG_DEBUG:
87 return "debug";
88 case SG_INFO:
89 return "info";
90 case SG_WARN:
91 return "warn";
92 case SG_ALERT:
93 case SG_POPUP:
94 return "alert";
95 default:
96 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
97 << sglog().get_log_priority());
98 return "unknown";
99 }
100 }
101
102
103 /**
104 * Set the logging priority.
105 */
106 void
107 setLoggingPriority (const char * p)
108 {
109 if (p == 0)
110 return;
111
112 string priority = p;
113 if (priority.empty()) {
114 sglog().set_log_priority(SG_INFO);
115 } else {
116 try {
117 sglog().set_log_priority(logstream::priorityFromString(priority));
118 } catch (std::exception& e) {
119 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority: " << priority);
120 }
121 }
122
123 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
124 }
125
126
127 /**
128 * Return the current frozen state.
129 */
130 bool FGProperties::getFreeze() const
131 {
132 return _simFreeze;
133 }
134
135
136 /**
137 * Set the current frozen state.
138 */
139 void FGProperties::setFreeze(bool f)
140 {
141 if (_simFreeze == f)
142 return;
143
144 _simFreeze = f;
145
146 // Pause the particle system
147 auto p = simgear::ParticlesGlobalManager::instance();
148 p->setFrozen(f);
149
150 _simFreezeNode->fireValueChanged();
151 }
152
153
154 /**
155 * Return the number of milliseconds elapsed since simulation started.
156 */
157 static double
158 getElapsedTime_sec ()
159 {
160 return globals->get_sim_time_sec();
161 }
162
163
164 /**
165 * Return the current Zulu time.
166 */
167 static const char *
168 getDateString ()
169 {
170 static char buf[64]; // FIXME
171
172 SGTime * st = globals->get_time_params();
173 if (!st) {
174 buf[0] = 0;
175 return buf;
176 }
177
178 struct tm * t = st->getGmt();
179 snprintf(buf, 64, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
180 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
181 t->tm_hour, t->tm_min, t->tm_sec);
182 return buf;
183 }
184
185
186 /**
187 * Set the current Zulu time.
188 */
189 static void
190 setDateString (const char * date_string)
191 {
192 SGTime * st = globals->get_time_params();
193 struct tm * current_time = st->getGmt();
194 struct tm new_time;
195
196 // Scan for basic ISO format
197 // YYYY-MM-DDTHH:MM:SS
198 new_time.tm_isdst = 0;
199 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
200 &(new_time.tm_year), &(new_time.tm_mon),
201 &(new_time.tm_mday), &(new_time.tm_hour),
202 &(new_time.tm_min), &(new_time.tm_sec));
203
204 // Be pretty picky about this, so
205 // that strange things don't happen
206 // if the save file has been edited
207 // by hand.
208 if (ret != 6) {
209 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
210 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
211 return;
212 }
213 // OK, it looks like we got six
214 // values, one way or another.
215 new_time.tm_year -= 1900;
216 new_time.tm_mon -= 1;
217 // Now, tell flight gear to use
218 // the new time. This was far
219 // too difficult, by the way.
220 long int warp =
221 mktime(&new_time) - mktime(current_time) + globals->get_warp();
222
223 fgSetInt("/sim/time/warp", warp);
224 }
225
226 /**
227 * Return the GMT as a string.
228 */
229 static const char *
230 getGMTString ()
231 {
232 static char buf[16];
233 SGTime * st = globals->get_time_params();
234 if (!st) {
235 buf[0] = 0;
236 return buf;
237 }
238
239 struct tm *t = st->getGmt();
240 snprintf(buf, 16, "%.2d:%.2d:%.2d",
241 t->tm_hour, t->tm_min, t->tm_sec);
242 return buf;
243 }
244
245 ////////////////////////////////////////////////////////////////////////
246 // Tie the properties.
247 ////////////////////////////////////////////////////////////////////////
248 SGConstPropertyNode_ptr FGProperties::_longDeg;
249 SGConstPropertyNode_ptr FGProperties::_latDeg;
250 SGConstPropertyNode_ptr FGProperties::_lonLatformat;
251
252 using namespace simgear;
253
254 const char* FGProperties::getLongitudeString ()
255 {
256 const double d = _longDeg->getDoubleValue();
257 auto format = static_cast<strutils::LatLonFormat>(_lonLatformat->getIntValue());
258 const char c = d < 0.0 ? 'W' : 'E';
259
260 static char longitudeBuffer[64];
261 const auto s = strutils::formatLatLonValueAsString(d, format, c);
262 memcpy(longitudeBuffer, s.c_str(), s.size() + 1);
263 return longitudeBuffer;
264 }
265
266 const char* FGProperties::getLatitudeString ()
267 {
268 const double d = _latDeg->getDoubleValue();
269 auto format = static_cast<strutils::LatLonFormat>(_lonLatformat->getIntValue());
270 const char c = d < 0.0 ? 'S' : 'N';
271
272 static char latitudeBuffer[64];
273 const auto s = strutils::formatLatLonValueAsString(d, format, c);
274 memcpy(latitudeBuffer, s.c_str(), s.size() + 1);
275 return latitudeBuffer;
276 }
277
278
279 FGProperties::FGProperties ()
280 {
281 }
282
283 FGProperties::~FGProperties ()
284 {
285 }
286
287 void
288 FGProperties::init ()
289 {
290 memset(&_lastUtc, 0, sizeof(struct tm));
291 memset(&_lastRealTime, 0, sizeof(struct tm));
292 _simFreeze = false;
293 }
294
295 static SGPropertyNode_ptr initDoubleNode(const std::string& path, const double v)
296 {
297 auto r = fgGetNode(path, true);
298 if (r->getType() == simgear::props::NONE) {
299 r->setDoubleValue(v);
300 }
301 return r;
302 }
303
304 void
305 FGProperties::bind ()
306 {
307 _longDeg = fgGetNode("/position/longitude-deg", true);
308 _latDeg = fgGetNode("/position/latitude-deg", true);
309 _lonLatformat = fgGetNode("/sim/lon-lat-format", true);
310
311 _offset = fgGetNode("/sim/time/local-offset", true);
312
313 // utc date/time
314 _uyear = fgGetNode("/sim/time/utc/year", true);
315 _umonth = fgGetNode("/sim/time/utc/month", true);
316 _uday = fgGetNode("/sim/time/utc/day", true);
317 _uhour = fgGetNode("/sim/time/utc/hour", true);
318 _umin = fgGetNode("/sim/time/utc/minute", true);
319 _usec = fgGetNode("/sim/time/utc/second", true);
320 _uwday = fgGetNode("/sim/time/utc/weekday", true);
321 _udsec = fgGetNode("/sim/time/utc/day-seconds", true);
322
323 // real local date/time
324 _ryear = fgGetNode("/sim/time/real/year", true);
325 _rmonth = fgGetNode("/sim/time/real/month", true);
326 _rday = fgGetNode("/sim/time/real/day", true);
327 _rhour = fgGetNode("/sim/time/real/hour", true);
328 _rmin = fgGetNode("/sim/time/real/minute", true);
329 _rsec = fgGetNode("/sim/time/real/second", true);
330 _rwday = fgGetNode("/sim/time/real/weekday", true);
331
332 _tiedProperties.setRoot(globals->get_props());
333
334 // Simulation
335 _tiedProperties.Tie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
336 _tiedProperties.Tie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
337 _simFreezeNode = fgGetNode("/sim/freeze/master", true);
338 _tiedProperties.Tie(_simFreezeNode, this, &FGProperties::getFreeze, &FGProperties::setFreeze);
339 _simFreezeNode->setAttribute(SGPropertyNode::LISTENER_SAFE, true);
340
341 _tiedProperties.Tie<double>("/sim/time/elapsed-sec", getElapsedTime_sec);
342 _timeGmtNode = _tiedProperties.Tie("/sim/time/gmt", getDateString, setDateString);
343 _timeGmtNode->setAttribute(SGPropertyNode::LISTENER_SAFE, true);
344 fgSetArchivable("/sim/time/gmt");
345 _timeGmtStringNode = _tiedProperties.Tie<const char*>("/sim/time/gmt-string", getGMTString);
346 _timeGmtStringNode->setAttribute(SGPropertyNode::LISTENER_SAFE, true);
347 // Position
348 _tiedProperties.Tie<const char*>("/position/latitude-string", getLatitudeString);
349 _tiedProperties.Tie<const char*>("/position/longitude-string", getLongitudeString);
350
351 _headingMagnetic = initDoubleNode("/orientation/heading-magnetic-deg", 0.0);
352 _trackMagnetic = initDoubleNode("/orientation/track-magnetic-deg", 0.0);
353 _magVar = initDoubleNode("/environment/magnetic-variation-deg", 0.0);
354 _trueHeading = initDoubleNode("/orientation/heading-deg", 0.0);
355 _trueTrack = initDoubleNode("/orientation/track-deg", 0.0);
356 }
357
358 void
359 FGProperties::unbind ()
360 {
361 _tiedProperties.Untie();
362
363 // drop static references to properties
364 _longDeg = 0;
365 _latDeg = 0;
366 _lonLatformat = 0;
367
368 _timeGmtNode.clear();
369 _timeGmtStringNode.clear();
370 _simFreezeNode.clear();
371 }
372
373 void
374 FGProperties::update (double dt)
375 {
376 _offset->setIntValue(globals->get_time_params()->get_local_offset());
377
378 // utc date/time
379 struct tm *u = globals->get_time_params()->getGmt();
380 if (memcmp(u, &_lastUtc, sizeof(struct tm)) != 0) {
381 _lastUtc = *u;
382 _uyear->setIntValue(u->tm_year + 1900);
383 _umonth->setIntValue(u->tm_mon + 1);
384 _uday->setIntValue(u->tm_mday);
385 _uhour->setIntValue(u->tm_hour);
386 _umin->setIntValue(u->tm_min);
387 _usec->setIntValue(u->tm_sec);
388 _uwday->setIntValue(u->tm_wday);
389 _udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
390
391 _timeGmtNode->fireValueChanged();
392 _timeGmtStringNode->fireValueChanged();
393 }
394
395
396 // real local date/time
397 time_t real = time(0);
398 struct tm *r = localtime(&real);
399 if (memcmp(r, &_lastRealTime, sizeof(struct tm)) != 0) {
400 _lastRealTime = *r;
401 _ryear->setIntValue(r->tm_year + 1900);
402 _rmonth->setIntValue(r->tm_mon + 1);
403 _rday->setIntValue(r->tm_mday);
404 _rhour->setIntValue(r->tm_hour);
405 _rmin->setIntValue(r->tm_min);
406 _rsec->setIntValue(r->tm_sec);
407 _rwday->setIntValue(r->tm_wday);
408 }
409
410 const double magvar = _magVar->getDoubleValue();
411 const auto hdgMag = SGMiscd::normalizePeriodic(0, 360.0, _trueHeading->getDoubleValue() - magvar);
412 _headingMagnetic->setDoubleValue(hdgMag);
413
414 const auto trackMag = SGMiscd::normalizePeriodic(0, 360.0, _trueTrack->getDoubleValue() - magvar);
415 _trackMagnetic->setDoubleValue(trackMag);
416 }
417
418
419 // Register the subsystem.
420 SGSubsystemMgr::Registrant<FGProperties> registrantFGProperties;
421
422 ////////////////////////////////////////////////////////////////////////
423 // Save and restore.
424 ////////////////////////////////////////////////////////////////////////
425
426
427 /**
428 * Save the current state of the simulator to a stream.
429 */
430 bool
431 fgSaveFlight (std::ostream &output, bool write_all)
432 {
433
434 fgSetBool("/sim/presets/onground", false);
435 fgSetArchivable("/sim/presets/onground");
436 fgSetBool("/sim/presets/trim", false);
437 fgSetArchivable("/sim/presets/trim");
438 fgSetString("/sim/presets/speed-set", "UVW");
439 fgSetArchivable("/sim/presets/speed-set");
440
441 try {
442 writeProperties(output, globals->get_props(), write_all);
443 } catch (const sg_exception &e) {
444 guiErrorMessage("Error saving flight: ", e);
445 return false;
446 }
447 return true;
448 }
449
450
451 /**
452 * Restore the current state of the simulator from a stream.
453 */
454 bool
455 fgLoadFlight (std::istream &input)
456 {
457 SGPropertyNode props;
458 try {
459 readProperties(input, &props);
460 } catch (const sg_exception &e) {
461 guiErrorMessage("Error reading saved flight: ", e);
462 return false;
463 }
464
465 fgSetBool("/sim/presets/onground", false);
466 fgSetBool("/sim/presets/trim", false);
467 fgSetString("/sim/presets/speed-set", "UVW");
468
469 copyProperties(&props, globals->get_props());
470
471 return true;
472 }
473
474
475 bool
476 fgLoadProps (const std::string& path, SGPropertyNode * props, bool in_fg_root, int default_mode)
477 {
478 SGPath fullpath;
479 if (in_fg_root) {
480 SGPath loadpath(globals->get_fg_root());
481 loadpath.append(path);
482 fullpath = loadpath;
483 } else {
484 fullpath = SGPath::fromUtf8(path);
485 }
486
487 try {
488 readProperties(fullpath, props, default_mode);
489 } catch (const sg_exception &e) {
490 guiErrorMessage("Error reading properties: ", e);
491 return false;
492 }
493 return true;
494 }
495
496
497 ////////////////////////////////////////////////////////////////////////
498 // Property convenience functions.
499 ////////////////////////////////////////////////////////////////////////
500
501 SGPropertyNode *
502 fgGetNode (const char * path, bool create)
503 {
504 return globals->get_props()->getNode(path, create);
505 }
506
507 SGPropertyNode *
508 fgGetNode (const char * path, int index, bool create)
509 {
510 return globals->get_props()->getNode(path, index, create);
511 }
512
513 bool
514 fgHasNode (const char * path)
515 {
516 return (fgGetNode(path, false) != 0);
517 }
518
519 void
520 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
521 {
522 fgGetNode(path, true)->addChangeListener(listener);
523 }
524
525 void
526 fgAddChangeListener (SGPropertyChangeListener * listener,
527 const char * path, int index)
528 {
529 fgGetNode(path, index, true)->addChangeListener(listener);
530 }
531
532 bool
533 fgGetBool (const char * name, bool defaultValue)
534 {
535 return globals->get_props()->getBoolValue(name, defaultValue);
536 }
537
538 int
539 fgGetInt (const char * name, int defaultValue)
540 {
541 return globals->get_props()->getIntValue(name, defaultValue);
542 }
543
544 long
545 fgGetLong (const char * name, long defaultValue)
546 {
547 return globals->get_props()->getLongValue(name, defaultValue);
548 }
549
550 float
551 fgGetFloat (const char * name, float defaultValue)
552 {
553 return globals->get_props()->getFloatValue(name, defaultValue);
554 }
555
556 double
557 fgGetDouble (const char * name, double defaultValue)
558 {
559 return globals->get_props()->getDoubleValue(name, defaultValue);
560 }
561
562 std::string
563 fgGetString (const char * name, const char * defaultValue)
564 {
565 return globals->get_props()->getStringValue(name, defaultValue);
566 }
567
568 bool
569 fgSetBool (const char * name, bool val)
570 {
571 return globals->get_props()->setBoolValue(name, val);
572 }
573
574 bool
575 fgSetInt (const char * name, int val)
576 {
577 return globals->get_props()->setIntValue(name, val);
578 }
579
580 bool
581 fgSetLong (const char * name, long val)
582 {
583 return globals->get_props()->setLongValue(name, val);
584 }
585
586 bool
587 fgSetFloat (const char * name, float val)
588 {
589 return globals->get_props()->setFloatValue(name, val);
590 }
591
592 bool
593 fgSetDouble (const char * name, double val)
594 {
595 return globals->get_props()->setDoubleValue(name, val);
596 }
597
598 bool
599 fgSetString (const char * name, const char * val)
600 {
601 return globals->get_props()->setStringValue(name, val);
602 }
603
604 void
605 fgSetArchivable (const char * name, bool state)
606 {
607 SGPropertyNode * node = globals->get_props()->getNode(name);
608 if (node == 0)
609 SG_LOG(SG_GENERAL, SG_DEBUG,
610 "Attempt to set archive flag for non-existant property "
611 << name);
612 else
613 node->setAttribute(SGPropertyNode::ARCHIVE, state);
614 }
615
616 void
617 fgSetReadable (const char * name, bool state)
618 {
619 SGPropertyNode * node = globals->get_props()->getNode(name);
620 if (node == 0)
621 SG_LOG(SG_GENERAL, SG_DEBUG,
622 "Attempt to set read flag for non-existant property "
623 << name);
624 else
625 node->setAttribute(SGPropertyNode::READ, state);
626 }
627
628 void
629 fgSetWritable (const char * name, bool state)
630 {
631 SGPropertyNode * node = globals->get_props()->getNode(name);
632 if (node == 0)
633 SG_LOG(SG_GENERAL, SG_DEBUG,
634 "Attempt to set write flag for non-existant property "
635 << name);
636 else
637 node->setAttribute(SGPropertyNode::WRITE, state);
638 }
639
640 void
641 fgUntie(const char * name)
642 {
643 SGPropertyNode* node = globals->get_props()->getNode(name);
644 if (!node) {
645 SG_LOG(SG_GENERAL, SG_WARN, "fgUntie: unknown property " << name);
646 return;
647 }
648
649 if (!node->isTied()) {
650 return;
651 }
652
653 if (!node->untie()) {
654 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
655 }
656 }
657
658 void fgUntieIfDefined(const std::string& name)
659 {
660 SGPropertyNode* node = globals->get_props()->getNode(name);
661 if (!node) {
662 return;
663 }
664
665 if (!node->isTied()) {
666 return;
667 }
668
669 if (!node->untie()) {
670 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
671 }
672 }
673
674
675 // end of fg_props.cxx