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"
42 static bool winding_ccw = true; // FIXME: temporary
44 static bool frozen = false; // FIXME: temporary
48 ////////////////////////////////////////////////////////////////////////
49 // Default property bindings (not yet handled by any module).
50 ////////////////////////////////////////////////////////////////////////
52 struct LogClassMapping {
55 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
58 LogClassMapping log_class_mappings [] = {
59 LogClassMapping(SG_NONE, "none"),
60 LogClassMapping(SG_TERRAIN, "terrain"),
61 LogClassMapping(SG_ASTRO, "astro"),
62 LogClassMapping(SG_FLIGHT, "flight"),
63 LogClassMapping(SG_INPUT, "input"),
64 LogClassMapping(SG_GL, "gl"),
65 LogClassMapping(SG_VIEW, "view"),
66 LogClassMapping(SG_COCKPIT, "cockpit"),
67 LogClassMapping(SG_GENERAL, "general"),
68 LogClassMapping(SG_MATH, "math"),
69 LogClassMapping(SG_EVENT, "event"),
70 LogClassMapping(SG_AIRCRAFT, "aircraft"),
71 LogClassMapping(SG_AUTOPILOT, "autopilot"),
72 LogClassMapping(SG_IO, "io"),
73 LogClassMapping(SG_CLIPPER, "clipper"),
74 LogClassMapping(SG_NETWORK, "network"),
75 LogClassMapping(SG_INSTR, "instrumentation"),
76 LogClassMapping(SG_ATC, "atc"),
77 LogClassMapping(SG_NASAL, "nasal"),
78 LogClassMapping(SG_SYSTEMS, "systems"),
79 LogClassMapping(SG_AI, "ai"),
80 LogClassMapping(SG_ENVIRONMENT, "environment"),
81 LogClassMapping(SG_SOUND, "sound"),
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 string::size_type sep = rest.find('|');
150 if (sep == string::npos)
151 sep = rest.find(',');
152 while (sep != string::npos) {
153 name = rest.substr(0, sep);
154 rest = rest.substr(sep+1);
155 addLoggingClass(name);
156 sep = rest.find('|');
157 if (sep == string::npos)
158 sep = rest.find(',');
160 addLoggingClass(rest);
161 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
162 << getLoggingClasses());
167 * Get the logging priority.
170 getLoggingPriority ()
172 switch (logbuf::get_log_priority()) {
184 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
185 << logbuf::get_log_priority());
192 * Set the logging priority.
195 setLoggingPriority (const char * p)
200 if (priority == "bulk") {
201 logbuf::set_log_priority(SG_BULK);
202 } else if (priority == "debug") {
203 logbuf::set_log_priority(SG_DEBUG);
204 } else if (priority.empty() || priority == "info") { // default
205 logbuf::set_log_priority(SG_INFO);
206 } else if (priority == "warn") {
207 logbuf::set_log_priority(SG_WARN);
208 } else if (priority == "alert") {
209 logbuf::set_log_priority(SG_ALERT);
211 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
213 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
218 * Return the current frozen state.
228 * Set the current frozen state.
235 // Stop sound on a pause
236 SGSoundMgr *smgr = globals->get_soundmgr();
237 if ( smgr != NULL ) {
240 } else if (fgGetBool("/sim/sound/working")) {
245 // Pause the particle system
246 simgear::Particles::setFrozen(f);
251 * Return the number of milliseconds elapsed since simulation started.
254 getElapsedTime_sec ()
256 return globals->get_sim_time_sec();
261 * Return the current Zulu time.
266 static char buf[64]; // FIXME
268 SGTime * st = globals->get_time_params();
274 struct tm * t = st->getGmt();
275 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
276 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
277 t->tm_hour, t->tm_min, t->tm_sec);
283 * Set the current Zulu time.
286 setDateString (const char * date_string)
288 SGTime * st = globals->get_time_params();
289 struct tm * current_time = st->getGmt();
292 // Scan for basic ISO format
293 // YYYY-MM-DDTHH:MM:SS
294 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
295 &(new_time.tm_year), &(new_time.tm_mon),
296 &(new_time.tm_mday), &(new_time.tm_hour),
297 &(new_time.tm_min), &(new_time.tm_sec));
299 // Be pretty picky about this, so
300 // that strange things don't happen
301 // if the save file has been edited
304 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
305 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
309 // OK, it looks like we got six
310 // values, one way or another.
311 new_time.tm_year -= 1900;
312 new_time.tm_mon -= 1;
313 // Now, tell flight gear to use
314 // the new time. This was far
315 // too difficult, by the way.
317 mktime(&new_time) - mktime(current_time) + globals->get_warp();
319 fgSetInt("/sim/time/warp", warp);
323 * Return the GMT as a string.
329 SGTime * st = globals->get_time_params();
335 struct tm *t = st->getGmt();
336 snprintf(buf, 16, "%.2d:%.2d:%.2d",
337 t->tm_hour, t->tm_min, t->tm_sec);
342 * Return the current heading in degrees.
347 double magheading = fgGetDouble("/orientation/heading-deg") -
348 fgGetDouble("/environment/magnetic-variation-deg");
349 return SGMiscd::normalizePeriodic(0, 360, magheading );
353 * Return the current track in degrees.
358 double magtrack = fgGetDouble("/orientation/track-deg") -
359 fgGetDouble("/environment/magnetic-variation-deg");
360 return SGMiscd::normalizePeriodic(0, 360, magtrack );
370 setWindingCCW (bool state)
374 glFrontFace ( GL_CCW );
376 glFrontFace ( GL_CW );
380 getLongitudeString ()
382 static SGConstPropertyNode_ptr n = fgGetNode("/position/longitude-deg", true);
383 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
385 double d = n->getDoubleValue();
386 int format = f->getIntValue();
387 char c = d < 0.0 ? 'W' : 'E';
390 snprintf(buf, 32, "%3.6f%c", d, c);
392 } else if (format == 1) {
393 // dd mm.mmm' (DMM-Format) -- uses a round-off factor tailored to the
394 // required precision of the minutes field (three decimal places),
395 // preventing minute values of 60.
396 double deg = fabs(d) + 5.0E-4 / 60.0;
397 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
398 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
401 // mm'ss.s'' (DMS-Format) -- uses a round-off factor tailored to the
402 // required precision of the seconds field (one decimal place),
403 // preventing second values of 60.
404 double deg = fabs(d) + 0.05 / 3600.0;
405 double min = (deg - int(deg)) * 60.0;
406 double sec = (min - int(min)) * 60.0 - 0.049;
407 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
408 int(min), fabs(sec), c);
417 static SGConstPropertyNode_ptr n = fgGetNode("/position/latitude-deg", true);
418 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
420 double d = n->getDoubleValue();
421 int format = f->getIntValue();
422 char c = d < 0.0 ? 'S' : 'N';
425 snprintf(buf, 32, "%3.6f%c", d, c);
427 } else if (format == 1) {
428 double deg = fabs(d) + 5.0E-4 / 60.0;
429 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
430 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
433 double deg = fabs(d) + 0.05 / 3600.0;
434 double min = (deg - int(deg)) * 60.0;
435 double sec = (min - int(min)) * 60.0 - 0.049;
436 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
437 int(min), fabs(sec), c);
446 ////////////////////////////////////////////////////////////////////////
447 // Tie the properties.
448 ////////////////////////////////////////////////////////////////////////
450 FGProperties::FGProperties ()
454 FGProperties::~FGProperties ()
459 FGProperties::init ()
464 FGProperties::bind ()
466 _tiedProperties.setRoot(globals->get_props());
469 _tiedProperties.Tie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
470 _tiedProperties.Tie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
471 _tiedProperties.Tie("/sim/freeze/master", getFreeze, setFreeze);
473 _tiedProperties.Tie("/sim/time/elapsed-sec", getElapsedTime_sec);
474 _tiedProperties.Tie("/sim/time/gmt", getDateString, setDateString);
475 fgSetArchivable("/sim/time/gmt");
476 _tiedProperties.Tie("/sim/time/gmt-string", getGMTString);
479 _tiedProperties.Tie("/position/latitude-string", getLatitudeString);
480 _tiedProperties.Tie("/position/longitude-string", getLongitudeString);
483 _tiedProperties.Tie("/orientation/heading-magnetic-deg", getHeadingMag);
484 _tiedProperties.Tie("/orientation/track-magnetic-deg", getTrackMag);
486 // Misc. Temporary junk.
487 _tiedProperties.Tie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
491 FGProperties::unbind ()
493 _tiedProperties.Untie();
497 FGProperties::update (double dt)
499 static SGPropertyNode_ptr offset = fgGetNode("/sim/time/local-offset", true);
500 offset->setIntValue(globals->get_time_params()->get_local_offset());
503 static SGPropertyNode_ptr uyear = fgGetNode("/sim/time/utc/year", true);
504 static SGPropertyNode_ptr umonth = fgGetNode("/sim/time/utc/month", true);
505 static SGPropertyNode_ptr uday = fgGetNode("/sim/time/utc/day", true);
506 static SGPropertyNode_ptr uhour = fgGetNode("/sim/time/utc/hour", true);
507 static SGPropertyNode_ptr umin = fgGetNode("/sim/time/utc/minute", true);
508 static SGPropertyNode_ptr usec = fgGetNode("/sim/time/utc/second", true);
509 static SGPropertyNode_ptr uwday = fgGetNode("/sim/time/utc/weekday", true);
510 static SGPropertyNode_ptr udsec = fgGetNode("/sim/time/utc/day-seconds", true);
512 struct tm *u = globals->get_time_params()->getGmt();
513 uyear->setIntValue(u->tm_year + 1900);
514 umonth->setIntValue(u->tm_mon + 1);
515 uday->setIntValue(u->tm_mday);
516 uhour->setIntValue(u->tm_hour);
517 umin->setIntValue(u->tm_min);
518 usec->setIntValue(u->tm_sec);
519 uwday->setIntValue(u->tm_wday);
521 udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
524 // real local date/time
525 static SGPropertyNode_ptr ryear = fgGetNode("/sim/time/real/year", true);
526 static SGPropertyNode_ptr rmonth = fgGetNode("/sim/time/real/month", true);
527 static SGPropertyNode_ptr rday = fgGetNode("/sim/time/real/day", true);
528 static SGPropertyNode_ptr rhour = fgGetNode("/sim/time/real/hour", true);
529 static SGPropertyNode_ptr rmin = fgGetNode("/sim/time/real/minute", true);
530 static SGPropertyNode_ptr rsec = fgGetNode("/sim/time/real/second", true);
531 static SGPropertyNode_ptr rwday = fgGetNode("/sim/time/real/weekday", true);
533 time_t real = time(0);
534 struct tm *r = localtime(&real);
535 ryear->setIntValue(r->tm_year + 1900);
536 rmonth->setIntValue(r->tm_mon + 1);
537 rday->setIntValue(r->tm_mday);
538 rhour->setIntValue(r->tm_hour);
539 rmin->setIntValue(r->tm_min);
540 rsec->setIntValue(r->tm_sec);
541 rwday->setIntValue(r->tm_wday);
546 ////////////////////////////////////////////////////////////////////////
548 ////////////////////////////////////////////////////////////////////////
552 * Save the current state of the simulator to a stream.
555 fgSaveFlight (std::ostream &output, bool write_all)
558 fgSetBool("/sim/presets/onground", false);
559 fgSetArchivable("/sim/presets/onground");
560 fgSetBool("/sim/presets/trim", false);
561 fgSetArchivable("/sim/presets/trim");
562 fgSetString("/sim/presets/speed-set", "UVW");
563 fgSetArchivable("/sim/presets/speed-set");
566 writeProperties(output, globals->get_props(), write_all);
567 } catch (const sg_exception &e) {
568 guiErrorMessage("Error saving flight: ", e);
576 * Restore the current state of the simulator from a stream.
579 fgLoadFlight (std::istream &input)
581 SGPropertyNode props;
583 readProperties(input, &props);
584 } catch (const sg_exception &e) {
585 guiErrorMessage("Error reading saved flight: ", e);
589 fgSetBool("/sim/presets/onground", false);
590 fgSetBool("/sim/presets/trim", false);
591 fgSetString("/sim/presets/speed-set", "UVW");
593 copyProperties(&props, globals->get_props());
594 // When loading a flight, make it the
595 // new initial state.
596 globals->saveInitialState();
602 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
606 SGPath loadpath(globals->get_fg_root());
607 loadpath.append(path);
608 fullpath = loadpath.str();
614 readProperties(fullpath, props, default_mode);
615 } catch (const sg_exception &e) {
616 guiErrorMessage("Error reading properties: ", e);
624 ////////////////////////////////////////////////////////////////////////
625 // Property convenience functions.
626 ////////////////////////////////////////////////////////////////////////
629 fgGetNode (const char * path, bool create)
631 return globals->get_props()->getNode(path, create);
635 fgGetNode (const char * path, int index, bool create)
637 return globals->get_props()->getNode(path, index, create);
641 fgHasNode (const char * path)
643 return (fgGetNode(path, false) != 0);
647 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
649 fgGetNode(path, true)->addChangeListener(listener);
653 fgAddChangeListener (SGPropertyChangeListener * listener,
654 const char * path, int index)
656 fgGetNode(path, index, true)->addChangeListener(listener);
660 fgGetBool (const char * name, bool defaultValue)
662 return globals->get_props()->getBoolValue(name, defaultValue);
666 fgGetInt (const char * name, int defaultValue)
668 return globals->get_props()->getIntValue(name, defaultValue);
672 fgGetLong (const char * name, long defaultValue)
674 return globals->get_props()->getLongValue(name, defaultValue);
678 fgGetFloat (const char * name, float defaultValue)
680 return globals->get_props()->getFloatValue(name, defaultValue);
684 fgGetDouble (const char * name, double defaultValue)
686 return globals->get_props()->getDoubleValue(name, defaultValue);
690 fgGetString (const char * name, const char * defaultValue)
692 return globals->get_props()->getStringValue(name, defaultValue);
696 fgSetBool (const char * name, bool val)
698 return globals->get_props()->setBoolValue(name, val);
702 fgSetInt (const char * name, int val)
704 return globals->get_props()->setIntValue(name, val);
708 fgSetLong (const char * name, long val)
710 return globals->get_props()->setLongValue(name, val);
714 fgSetFloat (const char * name, float val)
716 return globals->get_props()->setFloatValue(name, val);
720 fgSetDouble (const char * name, double val)
722 return globals->get_props()->setDoubleValue(name, val);
726 fgSetString (const char * name, const char * val)
728 return globals->get_props()->setStringValue(name, val);
732 fgSetArchivable (const char * name, bool state)
734 SGPropertyNode * node = globals->get_props()->getNode(name);
736 SG_LOG(SG_GENERAL, SG_DEBUG,
737 "Attempt to set archive flag for non-existant property "
740 node->setAttribute(SGPropertyNode::ARCHIVE, state);
744 fgSetReadable (const char * name, bool state)
746 SGPropertyNode * node = globals->get_props()->getNode(name);
748 SG_LOG(SG_GENERAL, SG_DEBUG,
749 "Attempt to set read flag for non-existant property "
752 node->setAttribute(SGPropertyNode::READ, state);
756 fgSetWritable (const char * name, bool state)
758 SGPropertyNode * node = globals->get_props()->getNode(name);
760 SG_LOG(SG_GENERAL, SG_DEBUG,
761 "Attempt to set write flag for non-existant property "
764 node->setAttribute(SGPropertyNode::WRITE, state);
768 fgUntie(const char * name)
770 SGPropertyNode* node = globals->get_props()->getNode(name);
772 SG_LOG(SG_GENERAL, SG_WARN, "fgUntie: unknown property " << name);
776 if (!node->isTied()) {
780 if (!node->untie()) {
781 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
786 // end of fg_props.cxx