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>
37 #include <Aircraft/aircraft.hxx>
38 #include <FDM/flight.hxx>
41 #include "globals.hxx"
42 #include "fg_props.hxx"
45 static bool winding_ccw = true; // FIXME: temporary
47 static bool fdm_data_logging = false; // FIXME: temporary
49 static bool frozen = false; // FIXME: temporary
53 ////////////////////////////////////////////////////////////////////////
54 // Default property bindings (not yet handled by any module).
55 ////////////////////////////////////////////////////////////////////////
57 struct LogClassMapping {
60 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
63 LogClassMapping log_class_mappings [] = {
64 LogClassMapping(SG_NONE, "none"),
65 LogClassMapping(SG_TERRAIN, "terrain"),
66 LogClassMapping(SG_ASTRO, "astro"),
67 LogClassMapping(SG_FLIGHT, "flight"),
68 LogClassMapping(SG_INPUT, "input"),
69 LogClassMapping(SG_GL, "gl"),
70 LogClassMapping(SG_VIEW, "view"),
71 LogClassMapping(SG_COCKPIT, "cockpit"),
72 LogClassMapping(SG_GENERAL, "general"),
73 LogClassMapping(SG_MATH, "math"),
74 LogClassMapping(SG_EVENT, "event"),
75 LogClassMapping(SG_AIRCRAFT, "aircraft"),
76 LogClassMapping(SG_AUTOPILOT, "autopilot"),
77 LogClassMapping(SG_IO, "io"),
78 LogClassMapping(SG_CLIPPER, "clipper"),
79 LogClassMapping(SG_NETWORK, "network"),
80 LogClassMapping(SG_INSTR, "instrumentation"),
81 LogClassMapping(SG_SYSTEMS, "systems"),
82 LogClassMapping(SG_UNDEFD, "")
87 * Get the logging classes.
89 // XXX Making the result buffer be global is a band-aid that hopefully
90 // delays its destruction 'til after its last use.
99 sgDebugClass classes = logbuf::get_log_classes();
100 loggingResult.clear();
101 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
102 if ((classes&log_class_mappings[i].c) > 0) {
103 if (!loggingResult.empty())
104 loggingResult += '|';
105 loggingResult += log_class_mappings[i].name;
108 return loggingResult.c_str();
113 addLoggingClass (const string &name)
115 sgDebugClass classes = logbuf::get_log_classes();
116 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
117 if (name == log_class_mappings[i].name) {
118 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
122 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
127 * Set the logging classes.
130 setLoggingClasses (const char * c)
133 logbuf::set_log_classes(SG_NONE);
135 if (classes == "none") {
136 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
140 if (classes.empty() || classes == "all") { // default
141 logbuf::set_log_classes(SG_ALL);
142 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
143 << getLoggingClasses());
147 string rest = classes;
149 int sep = rest.find('|');
151 name = rest.substr(0, sep);
152 rest = rest.substr(sep+1);
153 addLoggingClass(name);
154 sep = rest.find('|');
156 addLoggingClass(rest);
157 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
158 << getLoggingClasses());
163 * Get the logging priority.
166 getLoggingPriority ()
168 switch (logbuf::get_log_priority()) {
180 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
181 << logbuf::get_log_priority());
188 * Set the logging priority.
191 setLoggingPriority (const char * p)
196 if (priority == "bulk") {
197 logbuf::set_log_priority(SG_BULK);
198 } else if (priority == "debug") {
199 logbuf::set_log_priority(SG_DEBUG);
200 } else if (priority.empty() || priority == "info") { // default
201 logbuf::set_log_priority(SG_INFO);
202 } else if (priority == "warn") {
203 logbuf::set_log_priority(SG_WARN);
204 } else if (priority == "alert") {
205 logbuf::set_log_priority(SG_ALERT);
207 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
209 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
214 * Return the current frozen state.
224 * Set the current frozen state.
231 // Stop sound on a pause
232 SGSoundMgr *smgr = globals->get_soundmgr();
233 if ( smgr != NULL ) {
236 } else if (fgGetBool("/sim/sound/working")) {
241 // Pause the particle system
242 simgear::Particles::setFrozen(f);
247 * Return the number of milliseconds elapsed since simulation started.
250 getElapsedTime_sec ()
252 return globals->get_sim_time_sec();
257 * Return the current Zulu time.
262 static char buf[64]; // FIXME
263 struct tm * t = globals->get_time_params()->getGmt();
264 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
265 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
266 t->tm_hour, t->tm_min, t->tm_sec);
272 * Set the current Zulu time.
275 setDateString (const char * date_string)
277 static const SGPropertyNode *cur_time_override
278 = fgGetNode("/sim/time/cur-time-override", true);
280 SGTime * st = globals->get_time_params();
281 struct tm * current_time = st->getGmt();
284 // Scan for basic ISO format
285 // YYYY-MM-DDTHH:MM:SS
286 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
287 &(new_time.tm_year), &(new_time.tm_mon),
288 &(new_time.tm_mday), &(new_time.tm_hour),
289 &(new_time.tm_min), &(new_time.tm_sec));
291 // Be pretty picky about this, so
292 // that strange things don't happen
293 // if the save file has been edited
296 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
297 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
301 // OK, it looks like we got six
302 // values, one way or another.
303 new_time.tm_year -= 1900;
304 new_time.tm_mon -= 1;
306 // Now, tell flight gear to use
307 // the new time. This was far
308 // too difficult, by the way.
310 mktime(&new_time) - mktime(current_time) + globals->get_warp();
311 double lon = current_aircraft.fdm_state->get_Longitude();
312 double lat = current_aircraft.fdm_state->get_Latitude();
313 globals->set_warp(warp);
314 st->update(lon, lat, cur_time_override->getLongValue(), warp);
318 * Return the GMT as a string.
324 struct tm *t = globals->get_time_params()->getGmt();
325 snprintf(buf, 16, "%.2d:%.2d:%.2d",
326 t->tm_hour, t->tm_min, t->tm_sec);
331 * Return the magnetic variation
336 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
341 * Return the magnetic dip
346 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
351 * Return the current heading in degrees.
357 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
358 if (magheading < 0) magheading += 360;
365 return globals->get_warp();
371 globals->set_warp(warp);
377 return globals->get_warp_delta();
381 setWarpDelta (long delta)
383 globals->set_warp_delta(delta);
393 setWindingCCW (bool state)
397 glFrontFace ( GL_CCW );
399 glFrontFace ( GL_CW );
405 return fdm_data_logging;
409 setFDMDataLogging (bool state)
411 // kludge; no getter or setter available
412 if (state != fdm_data_logging) {
413 fgToggleFDMdataLogging();
414 fdm_data_logging = state;
419 getLongitudeString ()
421 static SGConstPropertyNode_ptr n = fgGetNode("/position/longitude-deg", true);
422 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
424 double d = n->getDoubleValue();
425 int format = f->getIntValue();
426 char c = d < 0.0 ? 'W' : 'E';
429 snprintf(buf, 32, "%3.6f%c", d, c);
431 } else if (format == 1) {
432 // dd mm.mmm' (DMM-Format) -- uses a round-off factor tailored to the
433 // required precision of the minutes field (three decimal places),
434 // preventing minute values of 60.
435 double deg = fabs(d) + 5.0E-4 / 60.0;
436 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
437 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
440 // mm'ss.s'' (DMS-Format) -- uses a round-off factor tailored to the
441 // required precision of the seconds field (one decimal place),
442 // preventing second values of 60.
443 double deg = fabs(d) + 0.05 / 3600.0;
444 double min = (deg - int(deg)) * 60.0;
445 double sec = (min - int(min)) * 60.0 - 0.049;
446 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
447 int(min), fabs(sec), c);
456 static SGConstPropertyNode_ptr n = fgGetNode("/position/latitude-deg", true);
457 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
459 double d = n->getDoubleValue();
460 int format = f->getIntValue();
461 char c = d < 0.0 ? 'S' : 'N';
464 snprintf(buf, 32, "%3.6f%c", d, c);
466 } else if (format == 1) {
467 double deg = fabs(d) + 5.0E-4 / 60.0;
468 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
469 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
472 double deg = fabs(d) + 0.05 / 3600.0;
473 double min = (deg - int(deg)) * 60.0;
474 double sec = (min - int(min)) * 60.0 - 0.049;
475 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
476 int(min), fabs(sec), c);
485 ////////////////////////////////////////////////////////////////////////
486 // Tie the properties.
487 ////////////////////////////////////////////////////////////////////////
489 FGProperties::FGProperties ()
493 FGProperties::~FGProperties ()
498 FGProperties::init ()
503 FGProperties::bind ()
506 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
507 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
508 fgTie("/sim/freeze/master", getFreeze, setFreeze);
510 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
511 fgTie("/sim/time/gmt", getDateString, setDateString);
512 fgSetArchivable("/sim/time/gmt");
513 fgTie("/sim/time/gmt-string", getGMTString);
516 fgTie("/position/latitude-string", getLatitudeString);
517 fgTie("/position/longitude-string", getLongitudeString);
520 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
522 fgTie("/environment/magnetic-variation-deg", getMagVar);
523 fgTie("/environment/magnetic-dip-deg", getMagDip);
525 fgTie("/sim/time/warp", getWarp, setWarp, false);
526 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
528 // Misc. Temporary junk.
529 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
530 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
534 FGProperties::unbind ()
537 fgUntie("/sim/logging/priority");
538 fgUntie("/sim/logging/classes");
539 fgUntie("/sim/freeze/master");
541 fgUntie("/sim/time/elapsed-sec");
542 fgUntie("/sim/time/gmt");
543 fgUntie("/sim/time/gmt-string");
545 fgUntie("/position/latitude-string");
546 fgUntie("/position/longitude-string");
549 fgUntie("/orientation/heading-magnetic-deg");
552 fgUntie("/environment/magnetic-variation-deg");
553 fgUntie("/environment/magnetic-dip-deg");
555 fgUntie("/sim/time/warp");
556 fgUntie("/sim/time/warp-delta");
558 // Misc. Temporary junk.
559 fgUntie("/sim/temp/winding-ccw");
560 fgUntie("/sim/temp/full-screen");
561 fgUntie("/sim/temp/fdm-data-logging");
565 FGProperties::update (double dt)
567 static SGPropertyNode_ptr offset = fgGetNode("/sim/time/local-offset", true);
568 offset->setIntValue(globals->get_time_params()->get_local_offset());
572 static SGPropertyNode_ptr uyear = fgGetNode("/sim/time/utc/year", true);
573 static SGPropertyNode_ptr umonth = fgGetNode("/sim/time/utc/month", true);
574 static SGPropertyNode_ptr uday = fgGetNode("/sim/time/utc/day", true);
575 static SGPropertyNode_ptr uhour = fgGetNode("/sim/time/utc/hour", true);
576 static SGPropertyNode_ptr umin = fgGetNode("/sim/time/utc/minute", true);
577 static SGPropertyNode_ptr usec = fgGetNode("/sim/time/utc/second", true);
578 static SGPropertyNode_ptr uwday = fgGetNode("/sim/time/utc/weekday", true);
579 static SGPropertyNode_ptr udsec = fgGetNode("/sim/time/utc/day-seconds", true);
581 struct tm *u = globals->get_time_params()->getGmt();
582 uyear->setIntValue(u->tm_year + 1900);
583 umonth->setIntValue(u->tm_mon + 1);
584 uday->setIntValue(u->tm_mday);
585 uhour->setIntValue(u->tm_hour);
586 umin->setIntValue(u->tm_min);
587 usec->setIntValue(u->tm_sec);
588 uwday->setIntValue(u->tm_wday);
590 udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
593 // real local date/time
594 static SGPropertyNode_ptr ryear = fgGetNode("/sim/time/real/year", true);
595 static SGPropertyNode_ptr rmonth = fgGetNode("/sim/time/real/month", true);
596 static SGPropertyNode_ptr rday = fgGetNode("/sim/time/real/day", true);
597 static SGPropertyNode_ptr rhour = fgGetNode("/sim/time/real/hour", true);
598 static SGPropertyNode_ptr rmin = fgGetNode("/sim/time/real/minute", true);
599 static SGPropertyNode_ptr rsec = fgGetNode("/sim/time/real/second", true);
600 static SGPropertyNode_ptr rwday = fgGetNode("/sim/time/real/weekday", true);
602 time_t real = time(0);
603 struct tm *r = localtime(&real);
604 ryear->setIntValue(r->tm_year + 1900);
605 rmonth->setIntValue(r->tm_mon + 1);
606 rday->setIntValue(r->tm_mday);
607 rhour->setIntValue(r->tm_hour);
608 rmin->setIntValue(r->tm_min);
609 rsec->setIntValue(r->tm_sec);
610 rwday->setIntValue(r->tm_wday);
615 ////////////////////////////////////////////////////////////////////////
617 ////////////////////////////////////////////////////////////////////////
621 * Save the current state of the simulator to a stream.
624 fgSaveFlight (std::ostream &output, bool write_all)
627 fgSetBool("/sim/presets/onground", false);
628 fgSetArchivable("/sim/presets/onground");
629 fgSetBool("/sim/presets/trim", false);
630 fgSetArchivable("/sim/presets/trim");
631 fgSetString("/sim/presets/speed-set", "UVW");
632 fgSetArchivable("/sim/presets/speed-set");
635 writeProperties(output, globals->get_props(), write_all);
636 } catch (const sg_exception &e) {
637 guiErrorMessage("Error saving flight: ", e);
645 * Restore the current state of the simulator from a stream.
648 fgLoadFlight (std::istream &input)
650 SGPropertyNode props;
652 readProperties(input, &props);
653 } catch (const sg_exception &e) {
654 guiErrorMessage("Error reading saved flight: ", e);
658 fgSetBool("/sim/presets/onground", false);
659 fgSetBool("/sim/presets/trim", false);
660 fgSetString("/sim/presets/speed-set", "UVW");
662 copyProperties(&props, globals->get_props());
663 // When loading a flight, make it the
664 // new initial state.
665 globals->saveInitialState();
671 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
675 SGPath loadpath(globals->get_fg_root());
676 loadpath.append(path);
677 fullpath = loadpath.str();
683 readProperties(fullpath, props, default_mode);
684 } catch (const sg_exception &e) {
685 guiErrorMessage("Error reading properties: ", e);
693 ////////////////////////////////////////////////////////////////////////
694 // Property convenience functions.
695 ////////////////////////////////////////////////////////////////////////
698 fgGetNode (const char * path, bool create)
700 return globals->get_props()->getNode(path, create);
704 fgGetNode (const char * path, int index, bool create)
706 return globals->get_props()->getNode(path, index, create);
710 fgHasNode (const char * path)
712 return (fgGetNode(path, false) != 0);
716 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
718 fgGetNode(path, true)->addChangeListener(listener);
722 fgAddChangeListener (SGPropertyChangeListener * listener,
723 const char * path, int index)
725 fgGetNode(path, index, true)->addChangeListener(listener);
729 fgGetBool (const char * name, bool defaultValue)
731 return globals->get_props()->getBoolValue(name, defaultValue);
735 fgGetInt (const char * name, int defaultValue)
737 return globals->get_props()->getIntValue(name, defaultValue);
741 fgGetLong (const char * name, long defaultValue)
743 return globals->get_props()->getLongValue(name, defaultValue);
747 fgGetFloat (const char * name, float defaultValue)
749 return globals->get_props()->getFloatValue(name, defaultValue);
753 fgGetDouble (const char * name, double defaultValue)
755 return globals->get_props()->getDoubleValue(name, defaultValue);
759 fgGetString (const char * name, const char * defaultValue)
761 return globals->get_props()->getStringValue(name, defaultValue);
765 fgSetBool (const char * name, bool val)
767 return globals->get_props()->setBoolValue(name, val);
771 fgSetInt (const char * name, int val)
773 return globals->get_props()->setIntValue(name, val);
777 fgSetLong (const char * name, long val)
779 return globals->get_props()->setLongValue(name, val);
783 fgSetFloat (const char * name, float val)
785 return globals->get_props()->setFloatValue(name, val);
789 fgSetDouble (const char * name, double val)
791 return globals->get_props()->setDoubleValue(name, val);
795 fgSetString (const char * name, const char * val)
797 return globals->get_props()->setStringValue(name, val);
801 fgSetArchivable (const char * name, bool state)
803 SGPropertyNode * node = globals->get_props()->getNode(name);
805 SG_LOG(SG_GENERAL, SG_DEBUG,
806 "Attempt to set archive flag for non-existant property "
809 node->setAttribute(SGPropertyNode::ARCHIVE, state);
813 fgSetReadable (const char * name, bool state)
815 SGPropertyNode * node = globals->get_props()->getNode(name);
817 SG_LOG(SG_GENERAL, SG_DEBUG,
818 "Attempt to set read flag for non-existant property "
821 node->setAttribute(SGPropertyNode::READ, state);
825 fgSetWritable (const char * name, bool state)
827 SGPropertyNode * node = globals->get_props()->getNode(name);
829 SG_LOG(SG_GENERAL, SG_DEBUG,
830 "Attempt to set write flag for non-existant property "
833 node->setAttribute(SGPropertyNode::WRITE, state);
837 fgUntie (const char * name)
839 if (!globals->get_props()->untie(name))
840 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
844 // end of fg_props.cxx