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/magvar/magvar.hxx>
32 #include <simgear/timing/sg_time.hxx>
33 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/scene/model/particles.hxx>
35 #include <simgear/sound/soundmgr_openal.hxx>
39 #include "globals.hxx"
40 #include "fg_props.hxx"
43 static bool winding_ccw = true; // FIXME: temporary
45 static bool frozen = false; // FIXME: temporary
49 ////////////////////////////////////////////////////////////////////////
50 // Default property bindings (not yet handled by any module).
51 ////////////////////////////////////////////////////////////////////////
53 struct LogClassMapping {
56 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
59 LogClassMapping log_class_mappings [] = {
60 LogClassMapping(SG_NONE, "none"),
61 LogClassMapping(SG_TERRAIN, "terrain"),
62 LogClassMapping(SG_ASTRO, "astro"),
63 LogClassMapping(SG_FLIGHT, "flight"),
64 LogClassMapping(SG_INPUT, "input"),
65 LogClassMapping(SG_GL, "gl"),
66 LogClassMapping(SG_VIEW, "view"),
67 LogClassMapping(SG_COCKPIT, "cockpit"),
68 LogClassMapping(SG_GENERAL, "general"),
69 LogClassMapping(SG_MATH, "math"),
70 LogClassMapping(SG_EVENT, "event"),
71 LogClassMapping(SG_AIRCRAFT, "aircraft"),
72 LogClassMapping(SG_AUTOPILOT, "autopilot"),
73 LogClassMapping(SG_IO, "io"),
74 LogClassMapping(SG_CLIPPER, "clipper"),
75 LogClassMapping(SG_NETWORK, "network"),
76 LogClassMapping(SG_INSTR, "instrumentation"),
77 LogClassMapping(SG_ATC, "atc"),
78 LogClassMapping(SG_NASAL, "nasal"),
79 LogClassMapping(SG_SYSTEMS, "systems"),
80 LogClassMapping(SG_AI, "ai"),
81 LogClassMapping(SG_ENVIRONMENT, "environment"),
82 LogClassMapping(SG_SOUND, "sound"),
83 LogClassMapping(SG_UNDEFD, "")
88 * Get the logging classes.
90 // XXX Making the result buffer be global is a band-aid that hopefully
91 // delays its destruction 'til after its last use.
100 sgDebugClass classes = logbuf::get_log_classes();
101 loggingResult.clear();
102 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
103 if ((classes&log_class_mappings[i].c) > 0) {
104 if (!loggingResult.empty())
105 loggingResult += '|';
106 loggingResult += log_class_mappings[i].name;
109 return loggingResult.c_str();
114 addLoggingClass (const string &name)
116 sgDebugClass classes = logbuf::get_log_classes();
117 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
118 if (name == log_class_mappings[i].name) {
119 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
123 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
128 * Set the logging classes.
131 setLoggingClasses (const char * c)
134 logbuf::set_log_classes(SG_NONE);
136 if (classes == "none") {
137 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
141 if (classes.empty() || classes == "all") { // default
142 logbuf::set_log_classes(SG_ALL);
143 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
144 << getLoggingClasses());
148 string rest = classes;
150 int sep = rest.find('|');
152 name = rest.substr(0, sep);
153 rest = rest.substr(sep+1);
154 addLoggingClass(name);
155 sep = rest.find('|');
157 addLoggingClass(rest);
158 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
159 << getLoggingClasses());
164 * Get the logging priority.
167 getLoggingPriority ()
169 switch (logbuf::get_log_priority()) {
181 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
182 << logbuf::get_log_priority());
189 * Set the logging priority.
192 setLoggingPriority (const char * p)
197 if (priority == "bulk") {
198 logbuf::set_log_priority(SG_BULK);
199 } else if (priority == "debug") {
200 logbuf::set_log_priority(SG_DEBUG);
201 } else if (priority.empty() || priority == "info") { // default
202 logbuf::set_log_priority(SG_INFO);
203 } else if (priority == "warn") {
204 logbuf::set_log_priority(SG_WARN);
205 } else if (priority == "alert") {
206 logbuf::set_log_priority(SG_ALERT);
208 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
210 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
215 * Return the current frozen state.
225 * Set the current frozen state.
232 // Stop sound on a pause
233 SGSoundMgr *smgr = globals->get_soundmgr();
234 if ( smgr != NULL ) {
237 } else if (fgGetBool("/sim/sound/working")) {
242 // Pause the particle system
243 simgear::Particles::setFrozen(f);
248 * Return the number of milliseconds elapsed since simulation started.
251 getElapsedTime_sec ()
253 return globals->get_sim_time_sec();
258 * Return the current Zulu time.
263 static char buf[64]; // FIXME
265 SGTime * st = globals->get_time_params();
271 struct tm * t = st->getGmt();
272 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
273 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
274 t->tm_hour, t->tm_min, t->tm_sec);
280 * Set the current Zulu time.
283 setDateString (const char * date_string)
285 SGTime * st = globals->get_time_params();
286 struct tm * current_time = st->getGmt();
289 // Scan for basic ISO format
290 // YYYY-MM-DDTHH:MM:SS
291 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
292 &(new_time.tm_year), &(new_time.tm_mon),
293 &(new_time.tm_mday), &(new_time.tm_hour),
294 &(new_time.tm_min), &(new_time.tm_sec));
296 // Be pretty picky about this, so
297 // that strange things don't happen
298 // if the save file has been edited
301 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
302 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
306 // OK, it looks like we got six
307 // values, one way or another.
308 new_time.tm_year -= 1900;
309 new_time.tm_mon -= 1;
310 // Now, tell flight gear to use
311 // the new time. This was far
312 // too difficult, by the way.
314 mktime(&new_time) - mktime(current_time) + globals->get_warp();
316 fgSetInt("/sim/time/warp", warp);
320 * Return the GMT as a string.
326 SGTime * st = globals->get_time_params();
332 struct tm *t = st->getGmt();
333 snprintf(buf, 16, "%.2d:%.2d:%.2d",
334 t->tm_hour, t->tm_min, t->tm_sec);
339 * Return the magnetic variation
344 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
349 * Return the magnetic dip
354 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
359 * Return the current heading in degrees.
365 magheading = fgGetDouble("/orientation/heading-deg") - getMagVar();
366 if (magheading < 0) magheading += 360;
371 * Return the current track in degrees.
377 magtrack = fgGetDouble("/orientation/track-deg") - getMagVar();
378 if (magtrack < 0) magtrack += 360;
389 setWindingCCW (bool state)
393 glFrontFace ( GL_CCW );
395 glFrontFace ( GL_CW );
399 getLongitudeString ()
401 static SGConstPropertyNode_ptr n = fgGetNode("/position/longitude-deg", true);
402 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
404 double d = n->getDoubleValue();
405 int format = f->getIntValue();
406 char c = d < 0.0 ? 'W' : 'E';
409 snprintf(buf, 32, "%3.6f%c", d, c);
411 } else if (format == 1) {
412 // dd mm.mmm' (DMM-Format) -- uses a round-off factor tailored to the
413 // required precision of the minutes field (three decimal places),
414 // preventing minute values of 60.
415 double deg = fabs(d) + 5.0E-4 / 60.0;
416 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
417 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
420 // mm'ss.s'' (DMS-Format) -- uses a round-off factor tailored to the
421 // required precision of the seconds field (one decimal place),
422 // preventing second values of 60.
423 double deg = fabs(d) + 0.05 / 3600.0;
424 double min = (deg - int(deg)) * 60.0;
425 double sec = (min - int(min)) * 60.0 - 0.049;
426 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
427 int(min), fabs(sec), c);
436 static SGConstPropertyNode_ptr n = fgGetNode("/position/latitude-deg", true);
437 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
439 double d = n->getDoubleValue();
440 int format = f->getIntValue();
441 char c = d < 0.0 ? 'S' : 'N';
444 snprintf(buf, 32, "%3.6f%c", d, c);
446 } else if (format == 1) {
447 double deg = fabs(d) + 5.0E-4 / 60.0;
448 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
449 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
452 double deg = fabs(d) + 0.05 / 3600.0;
453 double min = (deg - int(deg)) * 60.0;
454 double sec = (min - int(min)) * 60.0 - 0.049;
455 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
456 int(min), fabs(sec), c);
465 ////////////////////////////////////////////////////////////////////////
466 // Tie the properties.
467 ////////////////////////////////////////////////////////////////////////
469 FGProperties::FGProperties ()
473 FGProperties::~FGProperties ()
478 FGProperties::init ()
483 FGProperties::bind ()
486 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
487 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
488 fgTie("/sim/freeze/master", getFreeze, setFreeze);
490 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
491 fgTie("/sim/time/gmt", getDateString, setDateString);
492 fgSetArchivable("/sim/time/gmt");
493 fgTie("/sim/time/gmt-string", getGMTString);
496 fgTie("/position/latitude-string", getLatitudeString);
497 fgTie("/position/longitude-string", getLongitudeString);
500 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
501 fgTie("/orientation/track-magnetic-deg", getTrackMag);
503 fgTie("/environment/magnetic-variation-deg", getMagVar);
504 fgTie("/environment/magnetic-dip-deg", getMagDip);
506 // Misc. Temporary junk.
507 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
511 FGProperties::unbind ()
514 fgUntie("/sim/logging/priority");
515 fgUntie("/sim/logging/classes");
516 fgUntie("/sim/freeze/master");
518 fgUntie("/sim/time/elapsed-sec");
519 fgUntie("/sim/time/gmt");
520 fgUntie("/sim/time/gmt-string");
522 fgUntie("/position/latitude-string");
523 fgUntie("/position/longitude-string");
526 fgUntie("/orientation/heading-magnetic-deg");
527 fgUntie("/orientation/track-magnetic-deg");
530 fgUntie("/environment/magnetic-variation-deg");
531 fgUntie("/environment/magnetic-dip-deg");
533 // Misc. Temporary junk.
534 fgUntie("/sim/temp/winding-ccw");
535 // fgUntie("/sim/temp/full-screen");
536 // fgUntie("/sim/temp/fdm-data-logging");
540 FGProperties::update (double dt)
542 static SGPropertyNode_ptr offset = fgGetNode("/sim/time/local-offset", true);
543 offset->setIntValue(globals->get_time_params()->get_local_offset());
547 static SGPropertyNode_ptr uyear = fgGetNode("/sim/time/utc/year", true);
548 static SGPropertyNode_ptr umonth = fgGetNode("/sim/time/utc/month", true);
549 static SGPropertyNode_ptr uday = fgGetNode("/sim/time/utc/day", true);
550 static SGPropertyNode_ptr uhour = fgGetNode("/sim/time/utc/hour", true);
551 static SGPropertyNode_ptr umin = fgGetNode("/sim/time/utc/minute", true);
552 static SGPropertyNode_ptr usec = fgGetNode("/sim/time/utc/second", true);
553 static SGPropertyNode_ptr uwday = fgGetNode("/sim/time/utc/weekday", true);
554 static SGPropertyNode_ptr udsec = fgGetNode("/sim/time/utc/day-seconds", true);
556 struct tm *u = globals->get_time_params()->getGmt();
557 uyear->setIntValue(u->tm_year + 1900);
558 umonth->setIntValue(u->tm_mon + 1);
559 uday->setIntValue(u->tm_mday);
560 uhour->setIntValue(u->tm_hour);
561 umin->setIntValue(u->tm_min);
562 usec->setIntValue(u->tm_sec);
563 uwday->setIntValue(u->tm_wday);
565 udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
568 // real local date/time
569 static SGPropertyNode_ptr ryear = fgGetNode("/sim/time/real/year", true);
570 static SGPropertyNode_ptr rmonth = fgGetNode("/sim/time/real/month", true);
571 static SGPropertyNode_ptr rday = fgGetNode("/sim/time/real/day", true);
572 static SGPropertyNode_ptr rhour = fgGetNode("/sim/time/real/hour", true);
573 static SGPropertyNode_ptr rmin = fgGetNode("/sim/time/real/minute", true);
574 static SGPropertyNode_ptr rsec = fgGetNode("/sim/time/real/second", true);
575 static SGPropertyNode_ptr rwday = fgGetNode("/sim/time/real/weekday", true);
577 time_t real = time(0);
578 struct tm *r = localtime(&real);
579 ryear->setIntValue(r->tm_year + 1900);
580 rmonth->setIntValue(r->tm_mon + 1);
581 rday->setIntValue(r->tm_mday);
582 rhour->setIntValue(r->tm_hour);
583 rmin->setIntValue(r->tm_min);
584 rsec->setIntValue(r->tm_sec);
585 rwday->setIntValue(r->tm_wday);
590 ////////////////////////////////////////////////////////////////////////
592 ////////////////////////////////////////////////////////////////////////
596 * Save the current state of the simulator to a stream.
599 fgSaveFlight (std::ostream &output, bool write_all)
602 fgSetBool("/sim/presets/onground", false);
603 fgSetArchivable("/sim/presets/onground");
604 fgSetBool("/sim/presets/trim", false);
605 fgSetArchivable("/sim/presets/trim");
606 fgSetString("/sim/presets/speed-set", "UVW");
607 fgSetArchivable("/sim/presets/speed-set");
610 writeProperties(output, globals->get_props(), write_all);
611 } catch (const sg_exception &e) {
612 guiErrorMessage("Error saving flight: ", e);
620 * Restore the current state of the simulator from a stream.
623 fgLoadFlight (std::istream &input)
625 SGPropertyNode props;
627 readProperties(input, &props);
628 } catch (const sg_exception &e) {
629 guiErrorMessage("Error reading saved flight: ", e);
633 fgSetBool("/sim/presets/onground", false);
634 fgSetBool("/sim/presets/trim", false);
635 fgSetString("/sim/presets/speed-set", "UVW");
637 copyProperties(&props, globals->get_props());
638 // When loading a flight, make it the
639 // new initial state.
640 globals->saveInitialState();
646 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
650 SGPath loadpath(globals->get_fg_root());
651 loadpath.append(path);
652 fullpath = loadpath.str();
658 readProperties(fullpath, props, default_mode);
659 } catch (const sg_exception &e) {
660 guiErrorMessage("Error reading properties: ", e);
668 ////////////////////////////////////////////////////////////////////////
669 // Property convenience functions.
670 ////////////////////////////////////////////////////////////////////////
673 fgGetNode (const char * path, bool create)
675 return globals->get_props()->getNode(path, create);
679 fgGetNode (const char * path, int index, bool create)
681 return globals->get_props()->getNode(path, index, create);
685 fgHasNode (const char * path)
687 return (fgGetNode(path, false) != 0);
691 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
693 fgGetNode(path, true)->addChangeListener(listener);
697 fgAddChangeListener (SGPropertyChangeListener * listener,
698 const char * path, int index)
700 fgGetNode(path, index, true)->addChangeListener(listener);
704 fgGetBool (const char * name, bool defaultValue)
706 return globals->get_props()->getBoolValue(name, defaultValue);
710 fgGetInt (const char * name, int defaultValue)
712 return globals->get_props()->getIntValue(name, defaultValue);
716 fgGetLong (const char * name, long defaultValue)
718 return globals->get_props()->getLongValue(name, defaultValue);
722 fgGetFloat (const char * name, float defaultValue)
724 return globals->get_props()->getFloatValue(name, defaultValue);
728 fgGetDouble (const char * name, double defaultValue)
730 return globals->get_props()->getDoubleValue(name, defaultValue);
734 fgGetString (const char * name, const char * defaultValue)
736 return globals->get_props()->getStringValue(name, defaultValue);
740 fgSetBool (const char * name, bool val)
742 return globals->get_props()->setBoolValue(name, val);
746 fgSetInt (const char * name, int val)
748 return globals->get_props()->setIntValue(name, val);
752 fgSetLong (const char * name, long val)
754 return globals->get_props()->setLongValue(name, val);
758 fgSetFloat (const char * name, float val)
760 return globals->get_props()->setFloatValue(name, val);
764 fgSetDouble (const char * name, double val)
766 return globals->get_props()->setDoubleValue(name, val);
770 fgSetString (const char * name, const char * val)
772 return globals->get_props()->setStringValue(name, val);
776 fgSetArchivable (const char * name, bool state)
778 SGPropertyNode * node = globals->get_props()->getNode(name);
780 SG_LOG(SG_GENERAL, SG_DEBUG,
781 "Attempt to set archive flag for non-existant property "
784 node->setAttribute(SGPropertyNode::ARCHIVE, state);
788 fgSetReadable (const char * name, bool state)
790 SGPropertyNode * node = globals->get_props()->getNode(name);
792 SG_LOG(SG_GENERAL, SG_DEBUG,
793 "Attempt to set read flag for non-existant property "
796 node->setAttribute(SGPropertyNode::READ, state);
800 fgSetWritable (const char * name, bool state)
802 SGPropertyNode * node = globals->get_props()->getNode(name);
804 SG_LOG(SG_GENERAL, SG_DEBUG,
805 "Attempt to set write flag for non-existant property "
808 node->setAttribute(SGPropertyNode::WRITE, state);
812 fgUntie(const char * name)
814 SGPropertyNode* node = globals->get_props()->getNode(name);
816 SG_LOG(SG_GENERAL, SG_WARN, "fgUntie: unknown property " << name);
820 if (!node->isTied()) {
824 if (!node->untie()) {
825 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
830 // end of fg_props.cxx