1 // fg_props.cxx -- support for FlightGear properties.
3 // Written by David Megginson, started 2000.
5 // Copyright (C) 2000, 2001 David Megginson - david@megginson.com
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.
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.
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.
27 #include <simgear/compiler.h>
28 #include <simgear/structure/exception.hxx>
29 #include <simgear/props/props_io.hxx>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/misc/sg_path.hxx>
33 #include <simgear/scene/model/particles.hxx>
34 #include <simgear/sound/soundmgr_openal.hxx>
38 #include "globals.hxx"
39 #include "fg_props.hxx"
41 static bool frozen = false; // FIXME: temporary
44 ////////////////////////////////////////////////////////////////////////
45 // Default property bindings (not yet handled by any module).
46 ////////////////////////////////////////////////////////////////////////
48 struct LogClassMapping {
51 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
54 LogClassMapping log_class_mappings [] = {
55 LogClassMapping(SG_NONE, "none"),
56 LogClassMapping(SG_TERRAIN, "terrain"),
57 LogClassMapping(SG_ASTRO, "astro"),
58 LogClassMapping(SG_FLIGHT, "flight"),
59 LogClassMapping(SG_INPUT, "input"),
60 LogClassMapping(SG_GL, "gl"),
61 LogClassMapping(SG_VIEW, "view"),
62 LogClassMapping(SG_COCKPIT, "cockpit"),
63 LogClassMapping(SG_GENERAL, "general"),
64 LogClassMapping(SG_MATH, "math"),
65 LogClassMapping(SG_EVENT, "event"),
66 LogClassMapping(SG_AIRCRAFT, "aircraft"),
67 LogClassMapping(SG_AUTOPILOT, "autopilot"),
68 LogClassMapping(SG_IO, "io"),
69 LogClassMapping(SG_CLIPPER, "clipper"),
70 LogClassMapping(SG_NETWORK, "network"),
71 LogClassMapping(SG_INSTR, "instrumentation"),
72 LogClassMapping(SG_ATC, "atc"),
73 LogClassMapping(SG_NASAL, "nasal"),
74 LogClassMapping(SG_SYSTEMS, "systems"),
75 LogClassMapping(SG_AI, "ai"),
76 LogClassMapping(SG_ENVIRONMENT, "environment"),
77 LogClassMapping(SG_SOUND, "sound"),
78 LogClassMapping(SG_NAVAID, "navaid"),
79 LogClassMapping(SG_GUI, "gui"),
80 LogClassMapping(SG_UNDEFD, "")
85 * Get the logging classes.
87 // XXX Making the result buffer be global is a band-aid that hopefully
88 // delays its destruction 'til after its last use.
97 sgDebugClass classes = sglog().get_log_classes();
98 loggingResult.clear();
99 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
100 if ((classes&log_class_mappings[i].c) > 0) {
101 if (!loggingResult.empty())
102 loggingResult += '|';
103 loggingResult += log_class_mappings[i].name;
106 return loggingResult.c_str();
111 addLoggingClass (const string &name)
113 sgDebugClass classes = sglog().get_log_classes();
114 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
115 if (name == log_class_mappings[i].name) {
116 sglog().set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
120 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
125 * Set the logging classes.
128 setLoggingClasses (const char * c)
131 sglog().set_log_classes(SG_NONE);
133 if (classes == "none") {
134 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
138 if (classes.empty() || classes == "all") { // default
139 sglog().set_log_classes(SG_ALL);
140 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
141 << getLoggingClasses());
145 string rest = classes;
147 string::size_type sep = rest.find('|');
148 if (sep == string::npos)
149 sep = rest.find(',');
150 while (sep != string::npos) {
151 name = rest.substr(0, sep);
152 rest = rest.substr(sep+1);
153 addLoggingClass(name);
154 sep = rest.find('|');
155 if (sep == string::npos)
156 sep = rest.find(',');
158 addLoggingClass(rest);
159 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
160 << getLoggingClasses());
165 * Get the logging priority.
168 getLoggingPriority ()
170 switch (sglog().get_log_priority()) {
182 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
183 << sglog().get_log_priority());
190 * Set the logging priority.
193 setLoggingPriority (const char * p)
198 if (priority == "bulk") {
199 sglog().set_log_priority(SG_BULK);
200 } else if (priority == "debug") {
201 sglog().set_log_priority(SG_DEBUG);
202 } else if (priority.empty() || priority == "info") { // default
203 sglog().set_log_priority(SG_INFO);
204 } else if (priority == "warn") {
205 sglog().set_log_priority(SG_WARN);
206 } else if (priority == "alert") {
207 sglog().set_log_priority(SG_ALERT);
209 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
211 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
216 * Return the current frozen state.
226 * Set the current frozen state.
233 // Stop sound on a pause
234 SGSoundMgr *smgr = globals->get_soundmgr();
235 if ( smgr != NULL ) {
238 } else if (fgGetBool("/sim/sound/working")) {
243 // Pause the particle system
244 simgear::Particles::setFrozen(f);
249 * Return the number of milliseconds elapsed since simulation started.
252 getElapsedTime_sec ()
254 return globals->get_sim_time_sec();
259 * Return the current Zulu time.
264 static char buf[64]; // FIXME
266 SGTime * st = globals->get_time_params();
272 struct tm * t = st->getGmt();
273 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
274 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
275 t->tm_hour, t->tm_min, t->tm_sec);
281 * Set the current Zulu time.
284 setDateString (const char * date_string)
286 SGTime * st = globals->get_time_params();
287 struct tm * current_time = st->getGmt();
290 // Scan for basic ISO format
291 // YYYY-MM-DDTHH:MM:SS
292 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
293 &(new_time.tm_year), &(new_time.tm_mon),
294 &(new_time.tm_mday), &(new_time.tm_hour),
295 &(new_time.tm_min), &(new_time.tm_sec));
297 // Be pretty picky about this, so
298 // that strange things don't happen
299 // if the save file has been edited
302 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
303 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
307 // OK, it looks like we got six
308 // values, one way or another.
309 new_time.tm_year -= 1900;
310 new_time.tm_mon -= 1;
311 // Now, tell flight gear to use
312 // the new time. This was far
313 // too difficult, by the way.
315 mktime(&new_time) - mktime(current_time) + globals->get_warp();
317 fgSetInt("/sim/time/warp", warp);
321 * Return the GMT as a string.
327 SGTime * st = globals->get_time_params();
333 struct tm *t = st->getGmt();
334 snprintf(buf, 16, "%.2d:%.2d:%.2d",
335 t->tm_hour, t->tm_min, t->tm_sec);
340 * Return the current heading in degrees.
345 double magheading = fgGetDouble("/orientation/heading-deg") -
346 fgGetDouble("/environment/magnetic-variation-deg");
347 return SGMiscd::normalizePeriodic(0, 360, magheading );
351 * Return the current track in degrees.
356 double magtrack = fgGetDouble("/orientation/track-deg") -
357 fgGetDouble("/environment/magnetic-variation-deg");
358 return SGMiscd::normalizePeriodic(0, 360, magtrack );
361 ////////////////////////////////////////////////////////////////////////
362 // Tie the properties.
363 ////////////////////////////////////////////////////////////////////////
364 SGConstPropertyNode_ptr FGProperties::_longDeg;
365 SGConstPropertyNode_ptr FGProperties::_latDeg;
366 SGConstPropertyNode_ptr FGProperties::_lonLatformat;
369 FGProperties::getLongitudeString ()
372 double d = _longDeg->getDoubleValue();
373 int format = _lonLatformat->getIntValue();
374 char c = d < 0.0 ? 'W' : 'E';
378 snprintf(buf, 32, "%3.6f%c", d, c);
380 } else if (format == 1) {
381 // dd mm.mmm' (DMM-Format) -- uses a round-off factor tailored to the
382 // required precision of the minutes field (three decimal places),
383 // preventing minute values of 60.
384 double deg = d + 5.0E-4 / 60.0;
385 double min = (deg - int(deg)) * 60.0 - 4.999E-4;
386 snprintf(buf, 32, "%d*%06.3f%c", int(deg), min, c);
389 // mm'ss.s'' (DMS-Format) -- uses a round-off factor tailored to the
390 // required precision of the seconds field (one decimal place),
391 // preventing second values of 60.
392 double deg = d + 0.05 / 3600.0;
393 double min = (deg - int(deg)) * 60.0;
394 double sec = (min - int(min)) * 60.0 - 0.049;
395 snprintf(buf, 32, "%d*%02d %04.1f%c", int(deg),
396 int(min), fabs(sec), c);
403 FGProperties::getLatitudeString ()
406 double d = _latDeg->getDoubleValue();
407 int format = _lonLatformat->getIntValue();
408 char c = d < 0.0 ? 'S' : 'N';
412 snprintf(buf, 32, "%3.6f%c", d, c);
414 } else if (format == 1) {
415 double deg = d + 5.0E-4 / 60.0;
416 double min = (deg - int(deg)) * 60.0 - 4.999E-4;
417 snprintf(buf, 32, "%d*%06.3f%c", int(deg), min, c);
420 double deg = d + 0.05 / 3600.0;
421 double min = (deg - int(deg)) * 60.0;
422 double sec = (min - int(min)) * 60.0 - 0.049;
423 snprintf(buf, 32, "%d*%02d %04.1f%c", int(deg),
424 int(min), fabs(sec), c);
434 FGProperties::FGProperties ()
438 FGProperties::~FGProperties ()
443 FGProperties::init ()
448 FGProperties::bind ()
450 _longDeg = fgGetNode("/position/longitude-deg", true);
451 _latDeg = fgGetNode("/position/latitude-deg", true);
452 _lonLatformat = fgGetNode("/sim/lon-lat-format", true);
454 _offset = fgGetNode("/sim/time/local-offset", true);
457 _uyear = fgGetNode("/sim/time/utc/year", true);
458 _umonth = fgGetNode("/sim/time/utc/month", true);
459 _uday = fgGetNode("/sim/time/utc/day", true);
460 _uhour = fgGetNode("/sim/time/utc/hour", true);
461 _umin = fgGetNode("/sim/time/utc/minute", true);
462 _usec = fgGetNode("/sim/time/utc/second", true);
463 _uwday = fgGetNode("/sim/time/utc/weekday", true);
464 _udsec = fgGetNode("/sim/time/utc/day-seconds", true);
466 // real local date/time
467 _ryear = fgGetNode("/sim/time/real/year", true);
468 _rmonth = fgGetNode("/sim/time/real/month", true);
469 _rday = fgGetNode("/sim/time/real/day", true);
470 _rhour = fgGetNode("/sim/time/real/hour", true);
471 _rmin = fgGetNode("/sim/time/real/minute", true);
472 _rsec = fgGetNode("/sim/time/real/second", true);
473 _rwday = fgGetNode("/sim/time/real/weekday", true);
475 _tiedProperties.setRoot(globals->get_props());
478 _tiedProperties.Tie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
479 _tiedProperties.Tie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
480 _tiedProperties.Tie("/sim/freeze/master", getFreeze, setFreeze);
482 _tiedProperties.Tie("/sim/time/elapsed-sec", getElapsedTime_sec);
483 _tiedProperties.Tie("/sim/time/gmt", getDateString, setDateString);
484 fgSetArchivable("/sim/time/gmt");
485 _tiedProperties.Tie("/sim/time/gmt-string", getGMTString);
488 _tiedProperties.Tie("/position/latitude-string", getLatitudeString);
489 _tiedProperties.Tie("/position/longitude-string", getLongitudeString);
492 _tiedProperties.Tie("/orientation/heading-magnetic-deg", getHeadingMag);
493 _tiedProperties.Tie("/orientation/track-magnetic-deg", getTrackMag);
497 FGProperties::unbind ()
499 _tiedProperties.Untie();
501 // drop static references to properties
508 FGProperties::update (double dt)
510 _offset->setIntValue(globals->get_time_params()->get_local_offset());
513 struct tm *u = globals->get_time_params()->getGmt();
514 _uyear->setIntValue(u->tm_year + 1900);
515 _umonth->setIntValue(u->tm_mon + 1);
516 _uday->setIntValue(u->tm_mday);
517 _uhour->setIntValue(u->tm_hour);
518 _umin->setIntValue(u->tm_min);
519 _usec->setIntValue(u->tm_sec);
520 _uwday->setIntValue(u->tm_wday);
521 _udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
523 // real local date/time
524 time_t real = time(0);
525 struct tm *r = localtime(&real);
526 _ryear->setIntValue(r->tm_year + 1900);
527 _rmonth->setIntValue(r->tm_mon + 1);
528 _rday->setIntValue(r->tm_mday);
529 _rhour->setIntValue(r->tm_hour);
530 _rmin->setIntValue(r->tm_min);
531 _rsec->setIntValue(r->tm_sec);
532 _rwday->setIntValue(r->tm_wday);
537 ////////////////////////////////////////////////////////////////////////
539 ////////////////////////////////////////////////////////////////////////
543 * Save the current state of the simulator to a stream.
546 fgSaveFlight (std::ostream &output, bool write_all)
549 fgSetBool("/sim/presets/onground", false);
550 fgSetArchivable("/sim/presets/onground");
551 fgSetBool("/sim/presets/trim", false);
552 fgSetArchivable("/sim/presets/trim");
553 fgSetString("/sim/presets/speed-set", "UVW");
554 fgSetArchivable("/sim/presets/speed-set");
557 writeProperties(output, globals->get_props(), write_all);
558 } catch (const sg_exception &e) {
559 guiErrorMessage("Error saving flight: ", e);
567 * Restore the current state of the simulator from a stream.
570 fgLoadFlight (std::istream &input)
572 SGPropertyNode props;
574 readProperties(input, &props);
575 } catch (const sg_exception &e) {
576 guiErrorMessage("Error reading saved flight: ", e);
580 fgSetBool("/sim/presets/onground", false);
581 fgSetBool("/sim/presets/trim", false);
582 fgSetString("/sim/presets/speed-set", "UVW");
584 copyProperties(&props, globals->get_props());
591 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
595 SGPath loadpath(globals->get_fg_root());
596 loadpath.append(path);
597 fullpath = loadpath.str();
603 readProperties(fullpath, props, default_mode);
604 } catch (const sg_exception &e) {
605 guiErrorMessage("Error reading properties: ", e);
613 ////////////////////////////////////////////////////////////////////////
614 // Property convenience functions.
615 ////////////////////////////////////////////////////////////////////////
618 fgGetNode (const char * path, bool create)
620 return globals->get_props()->getNode(path, create);
624 fgGetNode (const char * path, int index, bool create)
626 return globals->get_props()->getNode(path, index, create);
630 fgHasNode (const char * path)
632 return (fgGetNode(path, false) != 0);
636 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
638 fgGetNode(path, true)->addChangeListener(listener);
642 fgAddChangeListener (SGPropertyChangeListener * listener,
643 const char * path, int index)
645 fgGetNode(path, index, true)->addChangeListener(listener);
649 fgGetBool (const char * name, bool defaultValue)
651 return globals->get_props()->getBoolValue(name, defaultValue);
655 fgGetInt (const char * name, int defaultValue)
657 return globals->get_props()->getIntValue(name, defaultValue);
661 fgGetLong (const char * name, long defaultValue)
663 return globals->get_props()->getLongValue(name, defaultValue);
667 fgGetFloat (const char * name, float defaultValue)
669 return globals->get_props()->getFloatValue(name, defaultValue);
673 fgGetDouble (const char * name, double defaultValue)
675 return globals->get_props()->getDoubleValue(name, defaultValue);
679 fgGetString (const char * name, const char * defaultValue)
681 return globals->get_props()->getStringValue(name, defaultValue);
685 fgSetBool (const char * name, bool val)
687 return globals->get_props()->setBoolValue(name, val);
691 fgSetInt (const char * name, int val)
693 return globals->get_props()->setIntValue(name, val);
697 fgSetLong (const char * name, long val)
699 return globals->get_props()->setLongValue(name, val);
703 fgSetFloat (const char * name, float val)
705 return globals->get_props()->setFloatValue(name, val);
709 fgSetDouble (const char * name, double val)
711 return globals->get_props()->setDoubleValue(name, val);
715 fgSetString (const char * name, const char * val)
717 return globals->get_props()->setStringValue(name, val);
721 fgSetArchivable (const char * name, bool state)
723 SGPropertyNode * node = globals->get_props()->getNode(name);
725 SG_LOG(SG_GENERAL, SG_DEBUG,
726 "Attempt to set archive flag for non-existant property "
729 node->setAttribute(SGPropertyNode::ARCHIVE, state);
733 fgSetReadable (const char * name, bool state)
735 SGPropertyNode * node = globals->get_props()->getNode(name);
737 SG_LOG(SG_GENERAL, SG_DEBUG,
738 "Attempt to set read flag for non-existant property "
741 node->setAttribute(SGPropertyNode::READ, state);
745 fgSetWritable (const char * name, bool state)
747 SGPropertyNode * node = globals->get_props()->getNode(name);
749 SG_LOG(SG_GENERAL, SG_DEBUG,
750 "Attempt to set write flag for non-existant property "
753 node->setAttribute(SGPropertyNode::WRITE, state);
757 fgUntie(const char * name)
759 SGPropertyNode* node = globals->get_props()->getNode(name);
761 SG_LOG(SG_GENERAL, SG_WARN, "fgUntie: unknown property " << name);
765 if (!node->isTied()) {
769 if (!node->untie()) {
770 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
775 // end of fg_props.cxx