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_TERRASYNC, "terrasync"),
81 LogClassMapping(SG_UNDEFD, "")
86 * Get the logging classes.
88 // XXX Making the result buffer be global is a band-aid that hopefully
89 // delays its destruction 'til after its last use.
98 sgDebugClass classes = sglog().get_log_classes();
99 loggingResult.clear();
100 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
101 if ((classes&log_class_mappings[i].c) > 0) {
102 if (!loggingResult.empty())
103 loggingResult += '|';
104 loggingResult += log_class_mappings[i].name;
107 return loggingResult.c_str();
112 addLoggingClass (const string &name)
114 sgDebugClass classes = sglog().get_log_classes();
115 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
116 if (name == log_class_mappings[i].name) {
117 sglog().set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
121 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
126 * Set the logging classes.
129 setLoggingClasses (const char * c)
132 sglog().set_log_classes(SG_NONE);
134 if (classes == "none") {
135 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
139 if (classes.empty() || classes == "all") { // default
140 sglog().set_log_classes(SG_ALL);
141 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
142 << getLoggingClasses());
146 string rest = classes;
148 string::size_type sep = rest.find('|');
149 if (sep == string::npos)
150 sep = rest.find(',');
151 while (sep != string::npos) {
152 name = rest.substr(0, sep);
153 rest = rest.substr(sep+1);
154 addLoggingClass(name);
155 sep = rest.find('|');
156 if (sep == string::npos)
157 sep = rest.find(',');
159 addLoggingClass(rest);
160 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
161 << getLoggingClasses());
166 * Get the logging priority.
169 getLoggingPriority ()
171 switch (sglog().get_log_priority()) {
183 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
184 << sglog().get_log_priority());
191 * Set the logging priority.
194 setLoggingPriority (const char * p)
199 if (priority == "bulk") {
200 sglog().set_log_priority(SG_BULK);
201 } else if (priority == "debug") {
202 sglog().set_log_priority(SG_DEBUG);
203 } else if (priority.empty() || priority == "info") { // default
204 sglog().set_log_priority(SG_INFO);
205 } else if (priority == "warn") {
206 sglog().set_log_priority(SG_WARN);
207 } else if (priority == "alert") {
208 sglog().set_log_priority(SG_ALERT);
210 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
212 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
217 * Return the current frozen state.
227 * Set the current frozen state.
234 // Stop sound on a pause
235 SGSoundMgr *smgr = globals->get_subsystem<SGSoundMgr>();
236 if ( smgr != NULL ) {
239 } else if (fgGetBool("/sim/sound/working")) {
244 // Pause the particle system
245 simgear::Particles::setFrozen(f);
250 * Return the number of milliseconds elapsed since simulation started.
253 getElapsedTime_sec ()
255 return globals->get_sim_time_sec();
260 * Return the current Zulu time.
265 static char buf[64]; // FIXME
267 SGTime * st = globals->get_time_params();
273 struct tm * t = st->getGmt();
274 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
275 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
276 t->tm_hour, t->tm_min, t->tm_sec);
282 * Set the current Zulu time.
285 setDateString (const char * date_string)
287 SGTime * st = globals->get_time_params();
288 struct tm * current_time = st->getGmt();
291 // Scan for basic ISO format
292 // YYYY-MM-DDTHH:MM:SS
293 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
294 &(new_time.tm_year), &(new_time.tm_mon),
295 &(new_time.tm_mday), &(new_time.tm_hour),
296 &(new_time.tm_min), &(new_time.tm_sec));
298 // Be pretty picky about this, so
299 // that strange things don't happen
300 // if the save file has been edited
303 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
304 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
308 // OK, it looks like we got six
309 // values, one way or another.
310 new_time.tm_year -= 1900;
311 new_time.tm_mon -= 1;
312 // Now, tell flight gear to use
313 // the new time. This was far
314 // too difficult, by the way.
316 mktime(&new_time) - mktime(current_time) + globals->get_warp();
318 fgSetInt("/sim/time/warp", warp);
322 * Return the GMT as a string.
328 SGTime * st = globals->get_time_params();
334 struct tm *t = st->getGmt();
335 snprintf(buf, 16, "%.2d:%.2d:%.2d",
336 t->tm_hour, t->tm_min, t->tm_sec);
341 * Return the current heading in degrees.
346 double magheading = fgGetDouble("/orientation/heading-deg") -
347 fgGetDouble("/environment/magnetic-variation-deg");
348 return SGMiscd::normalizePeriodic(0, 360, magheading );
352 * Return the current track in degrees.
357 double magtrack = fgGetDouble("/orientation/track-deg") -
358 fgGetDouble("/environment/magnetic-variation-deg");
359 return SGMiscd::normalizePeriodic(0, 360, magtrack );
362 ////////////////////////////////////////////////////////////////////////
363 // Tie the properties.
364 ////////////////////////////////////////////////////////////////////////
365 SGConstPropertyNode_ptr FGProperties::_longDeg;
366 SGConstPropertyNode_ptr FGProperties::_latDeg;
367 SGConstPropertyNode_ptr FGProperties::_lonLatformat;
370 * Format the latitude and longitude floats into a character array using a variety of coordinate formats.
373 formatLatLongString (double deg, int format, char *buf, char c) {
375 int sign = deg < 0.0 ? -1 : 1;
379 // d.dddddd' (DDD format).
380 snprintf(buf, 32, "%3.6f%c", deg, c);
382 } else if (format == 1) {
383 // d mm.mmm' (DMM format) -- uses a round-off factor tailored to the
384 // required precision of the minutes field (three decimal places),
385 // preventing minute values of 60.
386 min = (deg - int(deg)) * 60.0;
387 if (min >= 59.9995) {
391 snprintf(buf, 32, "%d*%06.3f'%c", int(deg), fabs(min), c);
393 } else if (format == 2) {
394 // d mm'ss.s" (DMS format) -- uses a round-off factor tailored to the
395 // required precision of the seconds field (one decimal place),
396 // preventing second values of 60.
397 min = (deg - int(deg)) * 60.0;
398 sec = (min - int(min)) * 60.0;
407 snprintf(buf, 32, "%d*%02d'%04.1f\"%c", int(deg), int(min), fabs(sec), c);
409 } else if (format == 3) {
410 // d.dddddd' (signed DDD format).
411 snprintf(buf, 32, "%3.6f", sign*deg);
413 } else if (format == 4) {
414 // d mm.mmm' (signed DMM format).
415 min = (deg - int(deg)) * 60.0;
416 if (min >= 59.9995) {
421 snprintf(buf, 32, "%d*%06.3f'", int(deg), fabs(min));
423 snprintf(buf, 32, "-%d*%06.3f'", int(deg), fabs(min));
426 } else if (format == 5) {
427 // d mm'ss.s" (signed DMS format).
428 min = (deg - int(deg)) * 60.0;
429 sec = (min - int(min)) * 60.0;
439 snprintf(buf, 32, "%d*%02d'%04.1f\"", int(deg), int(min), fabs(sec));
441 snprintf(buf, 32, "-%d*%02d'%04.1f\"", int(deg), int(min), fabs(sec));
444 } else if (format == 6) {
445 // dd.dddddd X, ddd.dddddd X (zero padded DDD format).
446 if (c == 'N' || c == 'S') {
447 snprintf(buf, 32, "%09.6f%c", deg, c);
449 snprintf(buf, 32, "%010.6f%c", deg, c);
452 } else if (format == 7) {
453 // dd mm.mmm' X, ddd mm.mmm' X (zero padded DMM format).
454 min = (deg - int(deg)) * 60.0;
455 if (min >= 59.9995) {
459 if (c == 'N' || c == 'S') {
460 snprintf(buf, 32, "%02d*%06.3f'%c", int(deg), fabs(min), c);
462 snprintf(buf, 32, "%03d*%06.3f'%c", int(deg), fabs(min), c);
465 } else if (format == 8) {
466 // dd mm'ss.s" X, dd mm'ss.s" X (zero padded DMS format).
467 min = (deg - int(deg)) * 60.0;
468 sec = (min - int(min)) * 60.0;
477 if (c == 'N' || c == 'S') {
478 snprintf(buf, 32, "%02d*%02d'%04.1f\"%c", int(deg), int(min), fabs(sec), c);
480 snprintf(buf, 32, "%03d*%02d'%04.1f\"%c", int(deg), int(min), fabs(sec), c);
483 } else if (format == 9) {
484 // dd* mm'.mmm X, ddd* mm'.mmm X (Trinity House Navigation standard).
485 min = (deg - int(deg)) * 60.0;
486 if (min >= 59.9995) {
490 if (c == 'N' || c == 'S') {
491 snprintf(buf, 32, "%02d* %02d'.%03d%c", int(deg), int(min), int(SGMisc<double>::round((min-int(min))*1000)), c);
493 snprintf(buf, 32, "%03d* %02d'.%03d%c", int(deg), int(min), int(SGMisc<double>::round((min-int(min))*1000)), c);
497 // Empty the buffer for all invalid formats.
503 FGProperties::getLongitudeString ()
506 double d = _longDeg->getDoubleValue();
507 int format = _lonLatformat->getIntValue();
509 char c = d < 0.0 ? 'W' : 'E';
510 formatLatLongString(d, format, buf, c);
515 FGProperties::getLatitudeString ()
518 double d = _latDeg->getDoubleValue();
519 int format = _lonLatformat->getIntValue();
521 char c = d < 0.0 ? 'S' : 'N';
522 formatLatLongString(d, format, buf, c);
530 FGProperties::FGProperties ()
534 FGProperties::~FGProperties ()
539 FGProperties::init ()
544 FGProperties::bind ()
546 _longDeg = fgGetNode("/position/longitude-deg", true);
547 _latDeg = fgGetNode("/position/latitude-deg", true);
548 _lonLatformat = fgGetNode("/sim/lon-lat-format", true);
550 _offset = fgGetNode("/sim/time/local-offset", true);
553 _uyear = fgGetNode("/sim/time/utc/year", true);
554 _umonth = fgGetNode("/sim/time/utc/month", true);
555 _uday = fgGetNode("/sim/time/utc/day", true);
556 _uhour = fgGetNode("/sim/time/utc/hour", true);
557 _umin = fgGetNode("/sim/time/utc/minute", true);
558 _usec = fgGetNode("/sim/time/utc/second", true);
559 _uwday = fgGetNode("/sim/time/utc/weekday", true);
560 _udsec = fgGetNode("/sim/time/utc/day-seconds", true);
562 // real local date/time
563 _ryear = fgGetNode("/sim/time/real/year", true);
564 _rmonth = fgGetNode("/sim/time/real/month", true);
565 _rday = fgGetNode("/sim/time/real/day", true);
566 _rhour = fgGetNode("/sim/time/real/hour", true);
567 _rmin = fgGetNode("/sim/time/real/minute", true);
568 _rsec = fgGetNode("/sim/time/real/second", true);
569 _rwday = fgGetNode("/sim/time/real/weekday", true);
571 _tiedProperties.setRoot(globals->get_props());
574 _tiedProperties.Tie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
575 _tiedProperties.Tie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
576 _tiedProperties.Tie("/sim/freeze/master", getFreeze, setFreeze);
578 _tiedProperties.Tie("/sim/time/elapsed-sec", getElapsedTime_sec);
579 _tiedProperties.Tie("/sim/time/gmt", getDateString, setDateString);
580 fgSetArchivable("/sim/time/gmt");
581 _tiedProperties.Tie("/sim/time/gmt-string", getGMTString);
584 _tiedProperties.Tie("/position/latitude-string", getLatitudeString);
585 _tiedProperties.Tie("/position/longitude-string", getLongitudeString);
588 _tiedProperties.Tie("/orientation/heading-magnetic-deg", getHeadingMag);
589 _tiedProperties.Tie("/orientation/track-magnetic-deg", getTrackMag);
593 FGProperties::unbind ()
595 _tiedProperties.Untie();
597 // drop static references to properties
604 FGProperties::update (double dt)
606 _offset->setIntValue(globals->get_time_params()->get_local_offset());
609 struct tm *u = globals->get_time_params()->getGmt();
610 _uyear->setIntValue(u->tm_year + 1900);
611 _umonth->setIntValue(u->tm_mon + 1);
612 _uday->setIntValue(u->tm_mday);
613 _uhour->setIntValue(u->tm_hour);
614 _umin->setIntValue(u->tm_min);
615 _usec->setIntValue(u->tm_sec);
616 _uwday->setIntValue(u->tm_wday);
617 _udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
619 // real local date/time
620 time_t real = time(0);
621 struct tm *r = localtime(&real);
622 _ryear->setIntValue(r->tm_year + 1900);
623 _rmonth->setIntValue(r->tm_mon + 1);
624 _rday->setIntValue(r->tm_mday);
625 _rhour->setIntValue(r->tm_hour);
626 _rmin->setIntValue(r->tm_min);
627 _rsec->setIntValue(r->tm_sec);
628 _rwday->setIntValue(r->tm_wday);
633 ////////////////////////////////////////////////////////////////////////
635 ////////////////////////////////////////////////////////////////////////
639 * Save the current state of the simulator to a stream.
642 fgSaveFlight (std::ostream &output, bool write_all)
645 fgSetBool("/sim/presets/onground", false);
646 fgSetArchivable("/sim/presets/onground");
647 fgSetBool("/sim/presets/trim", false);
648 fgSetArchivable("/sim/presets/trim");
649 fgSetString("/sim/presets/speed-set", "UVW");
650 fgSetArchivable("/sim/presets/speed-set");
653 writeProperties(output, globals->get_props(), write_all);
654 } catch (const sg_exception &e) {
655 guiErrorMessage("Error saving flight: ", e);
663 * Restore the current state of the simulator from a stream.
666 fgLoadFlight (std::istream &input)
668 SGPropertyNode props;
670 readProperties(input, &props);
671 } catch (const sg_exception &e) {
672 guiErrorMessage("Error reading saved flight: ", e);
676 fgSetBool("/sim/presets/onground", false);
677 fgSetBool("/sim/presets/trim", false);
678 fgSetString("/sim/presets/speed-set", "UVW");
680 copyProperties(&props, globals->get_props());
687 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
691 SGPath loadpath(globals->get_fg_root());
692 loadpath.append(path);
693 fullpath = loadpath.str();
699 readProperties(fullpath, props, default_mode);
700 } catch (const sg_exception &e) {
701 guiErrorMessage("Error reading properties: ", e);
709 ////////////////////////////////////////////////////////////////////////
710 // Property convenience functions.
711 ////////////////////////////////////////////////////////////////////////
714 fgGetNode (const char * path, bool create)
716 return globals->get_props()->getNode(path, create);
720 fgGetNode (const char * path, int index, bool create)
722 return globals->get_props()->getNode(path, index, create);
726 fgHasNode (const char * path)
728 return (fgGetNode(path, false) != 0);
732 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
734 fgGetNode(path, true)->addChangeListener(listener);
738 fgAddChangeListener (SGPropertyChangeListener * listener,
739 const char * path, int index)
741 fgGetNode(path, index, true)->addChangeListener(listener);
745 fgGetBool (const char * name, bool defaultValue)
747 return globals->get_props()->getBoolValue(name, defaultValue);
751 fgGetInt (const char * name, int defaultValue)
753 return globals->get_props()->getIntValue(name, defaultValue);
757 fgGetLong (const char * name, long defaultValue)
759 return globals->get_props()->getLongValue(name, defaultValue);
763 fgGetFloat (const char * name, float defaultValue)
765 return globals->get_props()->getFloatValue(name, defaultValue);
769 fgGetDouble (const char * name, double defaultValue)
771 return globals->get_props()->getDoubleValue(name, defaultValue);
775 fgGetString (const char * name, const char * defaultValue)
777 return globals->get_props()->getStringValue(name, defaultValue);
781 fgSetBool (const char * name, bool val)
783 return globals->get_props()->setBoolValue(name, val);
787 fgSetInt (const char * name, int val)
789 return globals->get_props()->setIntValue(name, val);
793 fgSetLong (const char * name, long val)
795 return globals->get_props()->setLongValue(name, val);
799 fgSetFloat (const char * name, float val)
801 return globals->get_props()->setFloatValue(name, val);
805 fgSetDouble (const char * name, double val)
807 return globals->get_props()->setDoubleValue(name, val);
811 fgSetString (const char * name, const char * val)
813 return globals->get_props()->setStringValue(name, val);
817 fgSetArchivable (const char * name, bool state)
819 SGPropertyNode * node = globals->get_props()->getNode(name);
821 SG_LOG(SG_GENERAL, SG_DEBUG,
822 "Attempt to set archive flag for non-existant property "
825 node->setAttribute(SGPropertyNode::ARCHIVE, state);
829 fgSetReadable (const char * name, bool state)
831 SGPropertyNode * node = globals->get_props()->getNode(name);
833 SG_LOG(SG_GENERAL, SG_DEBUG,
834 "Attempt to set read flag for non-existant property "
837 node->setAttribute(SGPropertyNode::READ, state);
841 fgSetWritable (const char * name, bool state)
843 SGPropertyNode * node = globals->get_props()->getNode(name);
845 SG_LOG(SG_GENERAL, SG_DEBUG,
846 "Attempt to set write flag for non-existant property "
849 node->setAttribute(SGPropertyNode::WRITE, state);
853 fgUntie(const char * name)
855 SGPropertyNode* node = globals->get_props()->getNode(name);
857 SG_LOG(SG_GENERAL, SG_WARN, "fgUntie: unknown property " << name);
861 if (!node->isTied()) {
865 if (!node->untie()) {
866 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
871 // end of fg_props.cxx