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/sound/soundmgr_openal.hxx>
36 #include <Aircraft/aircraft.hxx>
37 #include <FDM/flight.hxx>
40 #include "globals.hxx"
41 #include "fg_props.hxx"
44 static bool winding_ccw = true; // FIXME: temporary
46 static bool fdm_data_logging = false; // FIXME: temporary
48 static bool frozen = false; // FIXME: temporary
52 ////////////////////////////////////////////////////////////////////////
53 // Default property bindings (not yet handled by any module).
54 ////////////////////////////////////////////////////////////////////////
56 struct LogClassMapping {
59 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
62 LogClassMapping log_class_mappings [] = {
63 LogClassMapping(SG_NONE, "none"),
64 LogClassMapping(SG_TERRAIN, "terrain"),
65 LogClassMapping(SG_ASTRO, "astro"),
66 LogClassMapping(SG_FLIGHT, "flight"),
67 LogClassMapping(SG_INPUT, "input"),
68 LogClassMapping(SG_GL, "gl"),
69 LogClassMapping(SG_VIEW, "view"),
70 LogClassMapping(SG_COCKPIT, "cockpit"),
71 LogClassMapping(SG_GENERAL, "general"),
72 LogClassMapping(SG_MATH, "math"),
73 LogClassMapping(SG_EVENT, "event"),
74 LogClassMapping(SG_AIRCRAFT, "aircraft"),
75 LogClassMapping(SG_AUTOPILOT, "autopilot"),
76 LogClassMapping(SG_IO, "io"),
77 LogClassMapping(SG_CLIPPER, "clipper"),
78 LogClassMapping(SG_NETWORK, "network"),
79 LogClassMapping(SG_INSTR, "instrumentation"),
80 LogClassMapping(SG_SYSTEMS, "systems"),
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 = logbuf::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 = logbuf::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 logbuf::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 logbuf::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 logbuf::set_log_classes(SG_ALL);
141 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
142 << getLoggingClasses());
146 string rest = classes;
148 int sep = rest.find('|');
150 name = rest.substr(0, sep);
151 rest = rest.substr(sep+1);
152 addLoggingClass(name);
153 sep = rest.find('|');
155 addLoggingClass(rest);
156 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
157 << getLoggingClasses());
162 * Get the logging priority.
165 getLoggingPriority ()
167 switch (logbuf::get_log_priority()) {
179 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
180 << logbuf::get_log_priority());
187 * Set the logging priority.
190 setLoggingPriority (const char * p)
195 if (priority == "bulk") {
196 logbuf::set_log_priority(SG_BULK);
197 } else if (priority == "debug") {
198 logbuf::set_log_priority(SG_DEBUG);
199 } else if (priority.empty() || priority == "info") { // default
200 logbuf::set_log_priority(SG_INFO);
201 } else if (priority == "warn") {
202 logbuf::set_log_priority(SG_WARN);
203 } else if (priority == "alert") {
204 logbuf::set_log_priority(SG_ALERT);
206 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
208 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
213 * Return the current frozen state.
223 * Set the current frozen state.
230 // Stop sound on a pause
231 SGSoundMgr *smgr = globals->get_soundmgr();
232 if ( smgr != NULL ) {
235 } else if (fgGetBool("/sim/sound/working")) {
243 * Return the number of milliseconds elapsed since simulation started.
246 getElapsedTime_sec ()
248 return globals->get_sim_time_sec();
253 * Return the current Zulu time.
258 static char buf[64]; // FIXME
259 struct tm * t = globals->get_time_params()->getGmt();
260 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
261 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
262 t->tm_hour, t->tm_min, t->tm_sec);
268 * Set the current Zulu time.
271 setDateString (const char * date_string)
273 static const SGPropertyNode *cur_time_override
274 = fgGetNode("/sim/time/cur-time-override", true);
276 SGTime * st = globals->get_time_params();
277 struct tm * current_time = st->getGmt();
280 // Scan for basic ISO format
281 // YYYY-MM-DDTHH:MM:SS
282 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
283 &(new_time.tm_year), &(new_time.tm_mon),
284 &(new_time.tm_mday), &(new_time.tm_hour),
285 &(new_time.tm_min), &(new_time.tm_sec));
287 // Be pretty picky about this, so
288 // that strange things don't happen
289 // if the save file has been edited
292 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
293 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
297 // OK, it looks like we got six
298 // values, one way or another.
299 new_time.tm_year -= 1900;
300 new_time.tm_mon -= 1;
302 // Now, tell flight gear to use
303 // the new time. This was far
304 // too difficult, by the way.
306 mktime(&new_time) - mktime(current_time) + globals->get_warp();
307 double lon = current_aircraft.fdm_state->get_Longitude();
308 double lat = current_aircraft.fdm_state->get_Latitude();
309 globals->set_warp(warp);
310 st->update(lon, lat, cur_time_override->getLongValue(), warp);
314 * Return the GMT as a string.
320 struct tm *t = globals->get_time_params()->getGmt();
321 snprintf(buf, 16, "%.2d:%.2d:%.2d",
322 t->tm_hour, t->tm_min, t->tm_sec);
327 * Return the magnetic variation
332 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
337 * Return the magnetic dip
342 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
347 * Return the current heading in degrees.
353 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
354 if (magheading < 0) magheading += 360;
361 return globals->get_warp();
367 globals->set_warp(warp);
373 return globals->get_warp_delta();
377 setWarpDelta (long delta)
379 globals->set_warp_delta(delta);
389 setWindingCCW (bool state)
393 glFrontFace ( GL_CCW );
395 glFrontFace ( GL_CW );
401 return fdm_data_logging;
405 setFDMDataLogging (bool state)
407 // kludge; no getter or setter available
408 if (state != fdm_data_logging) {
409 fgToggleFDMdataLogging();
410 fdm_data_logging = state;
415 getLongitudeString ()
417 static SGConstPropertyNode_ptr n = fgGetNode("/position/longitude-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 ? 'W' : 'E';
425 snprintf(buf, 32, "%3.6f%c", d, c);
427 } else if (format == 1) {
428 // dd mm.mmm' (DMM-Format) -- uses a round-off factor tailored to the
429 // required precision of the minutes field (three decimal places),
430 // preventing minute values of 60.
431 double deg = fabs(d) + 5.0E-4 / 60.0;
432 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
433 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
436 // mm'ss.s'' (DMS-Format) -- uses a round-off factor tailored to the
437 // required precision of the seconds field (one decimal place),
438 // preventing second values of 60.
439 double deg = fabs(d) + 0.05 / 3600.0;
440 double min = (deg - int(deg)) * 60.0;
441 double sec = (min - int(min)) * 60.0 - 0.049;
442 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
443 int(min), fabs(sec), c);
452 static SGConstPropertyNode_ptr n = fgGetNode("/position/latitude-deg", true);
453 static SGConstPropertyNode_ptr f = fgGetNode("/sim/lon-lat-format", true);
455 double d = n->getDoubleValue();
456 int format = f->getIntValue();
457 char c = d < 0.0 ? 'S' : 'N';
460 snprintf(buf, 32, "%3.6f%c", d, c);
462 } else if (format == 1) {
463 double deg = fabs(d) + 5.0E-4 / 60.0;
464 double min = fabs(deg - int(deg)) * 60.0 - 4.999E-4;
465 snprintf(buf, 32, "%d*%06.3f%c", int(d < 0.0 ? -deg : deg), min, c);
468 double deg = fabs(d) + 0.05 / 3600.0;
469 double min = (deg - int(deg)) * 60.0;
470 double sec = (min - int(min)) * 60.0 - 0.049;
471 snprintf(buf, 32, "%d*%02d %04.1f%c", int(d < 0.0 ? -deg : deg),
472 int(min), fabs(sec), c);
481 ////////////////////////////////////////////////////////////////////////
482 // Tie the properties.
483 ////////////////////////////////////////////////////////////////////////
485 FGProperties::FGProperties ()
489 FGProperties::~FGProperties ()
494 FGProperties::init ()
499 FGProperties::bind ()
502 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
503 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
504 fgTie("/sim/freeze/master", getFreeze, setFreeze);
506 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
507 fgTie("/sim/time/gmt", getDateString, setDateString);
508 fgSetArchivable("/sim/time/gmt");
509 fgTie("/sim/time/gmt-string", getGMTString);
512 fgTie("/position/latitude-string", getLatitudeString);
513 fgTie("/position/longitude-string", getLongitudeString);
516 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
518 fgTie("/environment/magnetic-variation-deg", getMagVar);
519 fgTie("/environment/magnetic-dip-deg", getMagDip);
521 fgTie("/sim/time/warp", getWarp, setWarp, false);
522 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
524 // Misc. Temporary junk.
525 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
526 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
530 FGProperties::unbind ()
533 fgUntie("/sim/logging/priority");
534 fgUntie("/sim/logging/classes");
535 fgUntie("/sim/freeze/master");
537 fgUntie("/sim/time/elapsed-sec");
538 fgUntie("/sim/time/gmt");
539 fgUntie("/sim/time/gmt-string");
541 fgUntie("/position/latitude-string");
542 fgUntie("/position/longitude-string");
545 fgUntie("/orientation/heading-magnetic-deg");
548 fgUntie("/environment/magnetic-variation-deg");
549 fgUntie("/environment/magnetic-dip-deg");
551 fgUntie("/sim/time/warp");
552 fgUntie("/sim/time/warp-delta");
554 // Misc. Temporary junk.
555 fgUntie("/sim/temp/winding-ccw");
556 fgUntie("/sim/temp/full-screen");
557 fgUntie("/sim/temp/fdm-data-logging");
561 FGProperties::update (double dt)
563 static SGPropertyNode_ptr offset = fgGetNode("/sim/time/local-offset", true);
564 offset->setIntValue(globals->get_time_params()->get_local_offset());
568 static SGPropertyNode_ptr uyear = fgGetNode("/sim/time/utc/year", true);
569 static SGPropertyNode_ptr umonth = fgGetNode("/sim/time/utc/month", true);
570 static SGPropertyNode_ptr uday = fgGetNode("/sim/time/utc/day", true);
571 static SGPropertyNode_ptr uhour = fgGetNode("/sim/time/utc/hour", true);
572 static SGPropertyNode_ptr umin = fgGetNode("/sim/time/utc/minute", true);
573 static SGPropertyNode_ptr usec = fgGetNode("/sim/time/utc/second", true);
574 static SGPropertyNode_ptr uwday = fgGetNode("/sim/time/utc/weekday", true);
575 static SGPropertyNode_ptr udsec = fgGetNode("/sim/time/utc/day-seconds", true);
577 struct tm *u = globals->get_time_params()->getGmt();
578 uyear->setIntValue(u->tm_year + 1900);
579 umonth->setIntValue(u->tm_mon + 1);
580 uday->setIntValue(u->tm_mday);
581 uhour->setIntValue(u->tm_hour);
582 umin->setIntValue(u->tm_min);
583 usec->setIntValue(u->tm_sec);
584 uwday->setIntValue(u->tm_wday);
586 udsec->setIntValue(u->tm_hour * 3600 + u->tm_min * 60 + u->tm_sec);
589 // real local date/time
590 static SGPropertyNode_ptr ryear = fgGetNode("/sim/time/real/year", true);
591 static SGPropertyNode_ptr rmonth = fgGetNode("/sim/time/real/month", true);
592 static SGPropertyNode_ptr rday = fgGetNode("/sim/time/real/day", true);
593 static SGPropertyNode_ptr rhour = fgGetNode("/sim/time/real/hour", true);
594 static SGPropertyNode_ptr rmin = fgGetNode("/sim/time/real/minute", true);
595 static SGPropertyNode_ptr rsec = fgGetNode("/sim/time/real/second", true);
596 static SGPropertyNode_ptr rwday = fgGetNode("/sim/time/real/weekday", true);
598 time_t real = time(0);
599 struct tm *r = localtime(&real);
600 ryear->setIntValue(r->tm_year + 1900);
601 rmonth->setIntValue(r->tm_mon + 1);
602 rday->setIntValue(r->tm_mday);
603 rhour->setIntValue(r->tm_hour);
604 rmin->setIntValue(r->tm_min);
605 rsec->setIntValue(r->tm_sec);
606 rwday->setIntValue(r->tm_wday);
611 ////////////////////////////////////////////////////////////////////////
613 ////////////////////////////////////////////////////////////////////////
617 * Save the current state of the simulator to a stream.
620 fgSaveFlight (std::ostream &output, bool write_all)
623 fgSetBool("/sim/presets/onground", false);
624 fgSetArchivable("/sim/presets/onground");
625 fgSetBool("/sim/presets/trim", false);
626 fgSetArchivable("/sim/presets/trim");
627 fgSetString("/sim/presets/speed-set", "UVW");
628 fgSetArchivable("/sim/presets/speed-set");
631 writeProperties(output, globals->get_props(), write_all);
632 } catch (const sg_exception &e) {
633 guiErrorMessage("Error saving flight: ", e);
641 * Restore the current state of the simulator from a stream.
644 fgLoadFlight (std::istream &input)
646 SGPropertyNode props;
648 readProperties(input, &props);
649 } catch (const sg_exception &e) {
650 guiErrorMessage("Error reading saved flight: ", e);
654 fgSetBool("/sim/presets/onground", false);
655 fgSetBool("/sim/presets/trim", false);
656 fgSetString("/sim/presets/speed-set", "UVW");
658 copyProperties(&props, globals->get_props());
659 // When loading a flight, make it the
660 // new initial state.
661 globals->saveInitialState();
667 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
671 SGPath loadpath(globals->get_fg_root());
672 loadpath.append(path);
673 fullpath = loadpath.str();
679 readProperties(fullpath, props, default_mode);
680 } catch (const sg_exception &e) {
681 guiErrorMessage("Error reading properties: ", e);
689 ////////////////////////////////////////////////////////////////////////
690 // Property convenience functions.
691 ////////////////////////////////////////////////////////////////////////
694 fgGetNode (const char * path, bool create)
696 return globals->get_props()->getNode(path, create);
700 fgGetNode (const char * path, int index, bool create)
702 return globals->get_props()->getNode(path, index, create);
706 fgHasNode (const char * path)
708 return (fgGetNode(path, false) != 0);
712 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
714 fgGetNode(path, true)->addChangeListener(listener);
718 fgAddChangeListener (SGPropertyChangeListener * listener,
719 const char * path, int index)
721 fgGetNode(path, index, true)->addChangeListener(listener);
725 fgGetBool (const char * name, bool defaultValue)
727 return globals->get_props()->getBoolValue(name, defaultValue);
731 fgGetInt (const char * name, int defaultValue)
733 return globals->get_props()->getIntValue(name, defaultValue);
737 fgGetLong (const char * name, long defaultValue)
739 return globals->get_props()->getLongValue(name, defaultValue);
743 fgGetFloat (const char * name, float defaultValue)
745 return globals->get_props()->getFloatValue(name, defaultValue);
749 fgGetDouble (const char * name, double defaultValue)
751 return globals->get_props()->getDoubleValue(name, defaultValue);
755 fgGetString (const char * name, const char * defaultValue)
757 return globals->get_props()->getStringValue(name, defaultValue);
761 fgSetBool (const char * name, bool val)
763 return globals->get_props()->setBoolValue(name, val);
767 fgSetInt (const char * name, int val)
769 return globals->get_props()->setIntValue(name, val);
773 fgSetLong (const char * name, long val)
775 return globals->get_props()->setLongValue(name, val);
779 fgSetFloat (const char * name, float val)
781 return globals->get_props()->setFloatValue(name, val);
785 fgSetDouble (const char * name, double val)
787 return globals->get_props()->setDoubleValue(name, val);
791 fgSetString (const char * name, const char * val)
793 return globals->get_props()->setStringValue(name, val);
797 fgSetArchivable (const char * name, bool state)
799 SGPropertyNode * node = globals->get_props()->getNode(name);
801 SG_LOG(SG_GENERAL, SG_DEBUG,
802 "Attempt to set archive flag for non-existant property "
805 node->setAttribute(SGPropertyNode::ARCHIVE, state);
809 fgSetReadable (const char * name, bool state)
811 SGPropertyNode * node = globals->get_props()->getNode(name);
813 SG_LOG(SG_GENERAL, SG_DEBUG,
814 "Attempt to set read flag for non-existant property "
817 node->setAttribute(SGPropertyNode::READ, state);
821 fgSetWritable (const char * name, bool state)
823 SGPropertyNode * node = globals->get_props()->getNode(name);
825 SG_LOG(SG_GENERAL, SG_DEBUG,
826 "Attempt to set write flag for non-existant property "
829 node->setAttribute(SGPropertyNode::WRITE, state);
833 fgUntie (const char * name)
835 if (!globals->get_props()->untie(name))
836 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
840 // end of fg_props.cxx