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 string::size_type sep = rest.find('|');
151 if (sep == string::npos)
152 sep = rest.find(',');
153 while (sep != string::npos) {
154 name = rest.substr(0, sep);
155 rest = rest.substr(sep+1);
156 addLoggingClass(name);
157 sep = rest.find('|');
158 if (sep == string::npos)
159 sep = rest.find(',');
161 addLoggingClass(rest);
162 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
163 << getLoggingClasses());
168 * Get the logging priority.
171 getLoggingPriority ()
173 switch (logbuf::get_log_priority()) {
185 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
186 << logbuf::get_log_priority());
193 * Set the logging priority.
196 setLoggingPriority (const char * p)
201 if (priority == "bulk") {
202 logbuf::set_log_priority(SG_BULK);
203 } else if (priority == "debug") {
204 logbuf::set_log_priority(SG_DEBUG);
205 } else if (priority.empty() || priority == "info") { // default
206 logbuf::set_log_priority(SG_INFO);
207 } else if (priority == "warn") {
208 logbuf::set_log_priority(SG_WARN);
209 } else if (priority == "alert") {
210 logbuf::set_log_priority(SG_ALERT);
212 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
214 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
219 * Return the current frozen state.
229 * Set the current frozen state.
236 // Stop sound on a pause
237 SGSoundMgr *smgr = globals->get_soundmgr();
238 if ( smgr != NULL ) {
241 } else if (fgGetBool("/sim/sound/working")) {
246 // Pause the particle system
247 simgear::Particles::setFrozen(f);
252 * Return the number of milliseconds elapsed since simulation started.
255 getElapsedTime_sec ()
257 return globals->get_sim_time_sec();
262 * Return the current Zulu time.
267 static char buf[64]; // FIXME
269 SGTime * st = globals->get_time_params();
275 struct tm * t = st->getGmt();
276 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
277 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
278 t->tm_hour, t->tm_min, t->tm_sec);
284 * Set the current Zulu time.
287 setDateString (const char * date_string)
289 SGTime * st = globals->get_time_params();
290 struct tm * current_time = st->getGmt();
293 // Scan for basic ISO format
294 // YYYY-MM-DDTHH:MM:SS
295 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
296 &(new_time.tm_year), &(new_time.tm_mon),
297 &(new_time.tm_mday), &(new_time.tm_hour),
298 &(new_time.tm_min), &(new_time.tm_sec));
300 // Be pretty picky about this, so
301 // that strange things don't happen
302 // if the save file has been edited
305 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
306 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
310 // OK, it looks like we got six
311 // values, one way or another.
312 new_time.tm_year -= 1900;
313 new_time.tm_mon -= 1;
314 // Now, tell flight gear to use
315 // the new time. This was far
316 // too difficult, by the way.
318 mktime(&new_time) - mktime(current_time) + globals->get_warp();
320 fgSetInt("/sim/time/warp", warp);
324 * Return the GMT as a string.
330 SGTime * st = globals->get_time_params();
336 struct tm *t = st->getGmt();
337 snprintf(buf, 16, "%.2d:%.2d:%.2d",
338 t->tm_hour, t->tm_min, t->tm_sec);
343 * Return the magnetic variation
348 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
353 * Return the magnetic dip
358 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
363 * Return the current heading in degrees.
369 magheading = fgGetDouble("/orientation/heading-deg") - getMagVar();
370 if (magheading < 0) magheading += 360;
375 * Return the current track in degrees.
381 magtrack = fgGetDouble("/orientation/track-deg") - getMagVar();
382 if (magtrack < 0) magtrack += 360;
393 setWindingCCW (bool state)
397 glFrontFace ( GL_CCW );
399 glFrontFace ( GL_CW );
403 getLongitudeString ()
405 static SGConstPropertyNode_ptr n = fgGetNode("/position/longitude-deg", true);
406 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
408 double d = n->getDoubleValue();
409 int format = f->getIntValue();
410 char c = d < 0.0 ? 'W' : 'E';
413 snprintf(buf, 32, "%3.6f%c", d, c);
415 } else if (format == 1) {
416 // dd mm.mmm' (DMM-Format) -- uses a round-off factor tailored to the
417 // required precision of the minutes field (three decimal places),
418 // preventing minute values of 60.
419 double deg = fabs(d) + 5.0E-4 / 60.0;
420 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
421 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
424 // mm'ss.s'' (DMS-Format) -- uses a round-off factor tailored to the
425 // required precision of the seconds field (one decimal place),
426 // preventing second values of 60.
427 double deg = fabs(d) + 0.05 / 3600.0;
428 double min = (deg - int(deg)) * 60.0;
429 double sec = (min - int(min)) * 60.0 - 0.049;
430 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
431 int(min), fabs(sec), c);
440 static SGConstPropertyNode_ptr n = fgGetNode("/position/latitude-deg", true);
441 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
443 double d = n->getDoubleValue();
444 int format = f->getIntValue();
445 char c = d < 0.0 ? 'S' : 'N';
448 snprintf(buf, 32, "%3.6f%c", d, c);
450 } else if (format == 1) {
451 double deg = fabs(d) + 5.0E-4 / 60.0;
452 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
453 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
456 double deg = fabs(d) + 0.05 / 3600.0;
457 double min = (deg - int(deg)) * 60.0;
458 double sec = (min - int(min)) * 60.0 - 0.049;
459 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
460 int(min), fabs(sec), c);
469 ////////////////////////////////////////////////////////////////////////
470 // Tie the properties.
471 ////////////////////////////////////////////////////////////////////////
473 FGProperties::FGProperties ()
477 FGProperties::~FGProperties ()
482 FGProperties::init ()
487 FGProperties::bind ()
490 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
491 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
492 fgTie("/sim/freeze/master", getFreeze, setFreeze);
494 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
495 fgTie("/sim/time/gmt", getDateString, setDateString);
496 fgSetArchivable("/sim/time/gmt");
497 fgTie("/sim/time/gmt-string", getGMTString);
500 fgTie("/position/latitude-string", getLatitudeString);
501 fgTie("/position/longitude-string", getLongitudeString);
504 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
505 fgTie("/orientation/track-magnetic-deg", getTrackMag);
507 fgTie("/environment/magnetic-variation-deg", getMagVar);
508 fgTie("/environment/magnetic-dip-deg", getMagDip);
510 // Misc. Temporary junk.
511 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
515 FGProperties::unbind ()
518 fgUntie("/sim/logging/priority");
519 fgUntie("/sim/logging/classes");
520 fgUntie("/sim/freeze/master");
522 fgUntie("/sim/time/elapsed-sec");
523 fgUntie("/sim/time/gmt");
524 fgUntie("/sim/time/gmt-string");
526 fgUntie("/position/latitude-string");
527 fgUntie("/position/longitude-string");
530 fgUntie("/orientation/heading-magnetic-deg");
531 fgUntie("/orientation/track-magnetic-deg");
534 fgUntie("/environment/magnetic-variation-deg");
535 fgUntie("/environment/magnetic-dip-deg");
537 // Misc. Temporary junk.
538 fgUntie("/sim/temp/winding-ccw");
539 // fgUntie("/sim/temp/full-screen");
540 // fgUntie("/sim/temp/fdm-data-logging");
544 FGProperties::update (double dt)
546 static SGPropertyNode_ptr offset = fgGetNode("/sim/time/local-offset", true);
547 offset->setIntValue(globals->get_time_params()->get_local_offset());
551 static SGPropertyNode_ptr uyear = fgGetNode("/sim/time/utc/year", true);
552 static SGPropertyNode_ptr umonth = fgGetNode("/sim/time/utc/month", true);
553 static SGPropertyNode_ptr uday = fgGetNode("/sim/time/utc/day", true);
554 static SGPropertyNode_ptr uhour = fgGetNode("/sim/time/utc/hour", true);
555 static SGPropertyNode_ptr umin = fgGetNode("/sim/time/utc/minute", true);
556 static SGPropertyNode_ptr usec = fgGetNode("/sim/time/utc/second", true);
557 static SGPropertyNode_ptr uwday = fgGetNode("/sim/time/utc/weekday", true);
558 static SGPropertyNode_ptr udsec = fgGetNode("/sim/time/utc/day-seconds", true);
560 struct tm *u = globals->get_time_params()->getGmt();
561 uyear->setIntValue(u->tm_year + 1900);
562 umonth->setIntValue(u->tm_mon + 1);
563 uday->setIntValue(u->tm_mday);
564 uhour->setIntValue(u->tm_hour);
565 umin->setIntValue(u->tm_min);
566 usec->setIntValue(u->tm_sec);
567 uwday->setIntValue(u->tm_wday);
569 udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
572 // real local date/time
573 static SGPropertyNode_ptr ryear = fgGetNode("/sim/time/real/year", true);
574 static SGPropertyNode_ptr rmonth = fgGetNode("/sim/time/real/month", true);
575 static SGPropertyNode_ptr rday = fgGetNode("/sim/time/real/day", true);
576 static SGPropertyNode_ptr rhour = fgGetNode("/sim/time/real/hour", true);
577 static SGPropertyNode_ptr rmin = fgGetNode("/sim/time/real/minute", true);
578 static SGPropertyNode_ptr rsec = fgGetNode("/sim/time/real/second", true);
579 static SGPropertyNode_ptr rwday = fgGetNode("/sim/time/real/weekday", true);
581 time_t real = time(0);
582 struct tm *r = localtime(&real);
583 ryear->setIntValue(r->tm_year + 1900);
584 rmonth->setIntValue(r->tm_mon + 1);
585 rday->setIntValue(r->tm_mday);
586 rhour->setIntValue(r->tm_hour);
587 rmin->setIntValue(r->tm_min);
588 rsec->setIntValue(r->tm_sec);
589 rwday->setIntValue(r->tm_wday);
594 ////////////////////////////////////////////////////////////////////////
596 ////////////////////////////////////////////////////////////////////////
600 * Save the current state of the simulator to a stream.
603 fgSaveFlight (std::ostream &output, bool write_all)
606 fgSetBool("/sim/presets/onground", false);
607 fgSetArchivable("/sim/presets/onground");
608 fgSetBool("/sim/presets/trim", false);
609 fgSetArchivable("/sim/presets/trim");
610 fgSetString("/sim/presets/speed-set", "UVW");
611 fgSetArchivable("/sim/presets/speed-set");
614 writeProperties(output, globals->get_props(), write_all);
615 } catch (const sg_exception &e) {
616 guiErrorMessage("Error saving flight: ", e);
624 * Restore the current state of the simulator from a stream.
627 fgLoadFlight (std::istream &input)
629 SGPropertyNode props;
631 readProperties(input, &props);
632 } catch (const sg_exception &e) {
633 guiErrorMessage("Error reading saved flight: ", e);
637 fgSetBool("/sim/presets/onground", false);
638 fgSetBool("/sim/presets/trim", false);
639 fgSetString("/sim/presets/speed-set", "UVW");
641 copyProperties(&props, globals->get_props());
642 // When loading a flight, make it the
643 // new initial state.
644 globals->saveInitialState();
650 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
654 SGPath loadpath(globals->get_fg_root());
655 loadpath.append(path);
656 fullpath = loadpath.str();
662 readProperties(fullpath, props, default_mode);
663 } catch (const sg_exception &e) {
664 guiErrorMessage("Error reading properties: ", e);
672 ////////////////////////////////////////////////////////////////////////
673 // Property convenience functions.
674 ////////////////////////////////////////////////////////////////////////
677 fgGetNode (const char * path, bool create)
679 return globals->get_props()->getNode(path, create);
683 fgGetNode (const char * path, int index, bool create)
685 return globals->get_props()->getNode(path, index, create);
689 fgHasNode (const char * path)
691 return (fgGetNode(path, false) != 0);
695 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
697 fgGetNode(path, true)->addChangeListener(listener);
701 fgAddChangeListener (SGPropertyChangeListener * listener,
702 const char * path, int index)
704 fgGetNode(path, index, true)->addChangeListener(listener);
708 fgGetBool (const char * name, bool defaultValue)
710 return globals->get_props()->getBoolValue(name, defaultValue);
714 fgGetInt (const char * name, int defaultValue)
716 return globals->get_props()->getIntValue(name, defaultValue);
720 fgGetLong (const char * name, long defaultValue)
722 return globals->get_props()->getLongValue(name, defaultValue);
726 fgGetFloat (const char * name, float defaultValue)
728 return globals->get_props()->getFloatValue(name, defaultValue);
732 fgGetDouble (const char * name, double defaultValue)
734 return globals->get_props()->getDoubleValue(name, defaultValue);
738 fgGetString (const char * name, const char * defaultValue)
740 return globals->get_props()->getStringValue(name, defaultValue);
744 fgSetBool (const char * name, bool val)
746 return globals->get_props()->setBoolValue(name, val);
750 fgSetInt (const char * name, int val)
752 return globals->get_props()->setIntValue(name, val);
756 fgSetLong (const char * name, long val)
758 return globals->get_props()->setLongValue(name, val);
762 fgSetFloat (const char * name, float val)
764 return globals->get_props()->setFloatValue(name, val);
768 fgSetDouble (const char * name, double val)
770 return globals->get_props()->setDoubleValue(name, val);
774 fgSetString (const char * name, const char * val)
776 return globals->get_props()->setStringValue(name, val);
780 fgSetArchivable (const char * name, bool state)
782 SGPropertyNode * node = globals->get_props()->getNode(name);
784 SG_LOG(SG_GENERAL, SG_DEBUG,
785 "Attempt to set archive flag for non-existant property "
788 node->setAttribute(SGPropertyNode::ARCHIVE, state);
792 fgSetReadable (const char * name, bool state)
794 SGPropertyNode * node = globals->get_props()->getNode(name);
796 SG_LOG(SG_GENERAL, SG_DEBUG,
797 "Attempt to set read flag for non-existant property "
800 node->setAttribute(SGPropertyNode::READ, state);
804 fgSetWritable (const char * name, bool state)
806 SGPropertyNode * node = globals->get_props()->getNode(name);
808 SG_LOG(SG_GENERAL, SG_DEBUG,
809 "Attempt to set write flag for non-existant property "
812 node->setAttribute(SGPropertyNode::WRITE, state);
816 fgUntie(const char * name)
818 SGPropertyNode* node = globals->get_props()->getNode(name);
820 SG_LOG(SG_GENERAL, SG_WARN, "fgUntie: unknown property " << name);
824 if (!node->isTied()) {
828 if (!node->untie()) {
829 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
834 // end of fg_props.cxx