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>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/magvar/magvar.hxx>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/misc/sg_path.hxx>
33 #include <simgear/scene/material/matlib.hxx>
34 #include <simgear/sound/soundmgr_openal.hxx>
38 #include <ATC/ATCdisplay.hxx>
39 #include <Aircraft/aircraft.hxx>
40 #include <Time/tmp.hxx>
41 #include <Environment/environment.hxx>
45 #include "globals.hxx"
46 #include "fg_props.hxx"
48 SG_USING_STD(istream);
49 SG_USING_STD(ostream);
51 static bool winding_ccw = true; // FIXME: temporary
53 static bool fdm_data_logging = false; // FIXME: temporary
55 static bool frozen = false; // FIXME: temporary
59 ////////////////////////////////////////////////////////////////////////
60 // Default property bindings (not yet handled by any module).
61 ////////////////////////////////////////////////////////////////////////
63 struct LogClassMapping {
66 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
69 LogClassMapping log_class_mappings [] = {
70 LogClassMapping(SG_NONE, "none"),
71 LogClassMapping(SG_TERRAIN, "terrain"),
72 LogClassMapping(SG_ASTRO, "astro"),
73 LogClassMapping(SG_FLIGHT, "flight"),
74 LogClassMapping(SG_INPUT, "input"),
75 LogClassMapping(SG_GL, "gl"),
76 LogClassMapping(SG_VIEW, "view"),
77 LogClassMapping(SG_COCKPIT, "cockpit"),
78 LogClassMapping(SG_GENERAL, "general"),
79 LogClassMapping(SG_MATH, "math"),
80 LogClassMapping(SG_EVENT, "event"),
81 LogClassMapping(SG_AIRCRAFT, "aircraft"),
82 LogClassMapping(SG_AUTOPILOT, "autopilot"),
83 LogClassMapping(SG_IO, "io"),
84 LogClassMapping(SG_CLIPPER, "clipper"),
85 LogClassMapping(SG_NETWORK, "network"),
86 LogClassMapping(SG_INSTR, "instrumentation"),
87 LogClassMapping(SG_SYSTEMS, "systems"),
88 LogClassMapping(SG_UNDEFD, "")
93 * Get the logging classes.
98 sgDebugClass classes = logbuf::get_log_classes();
99 static string result = ""; // FIXME
100 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
101 if ((classes&log_class_mappings[i].c) > 0) {
104 result += log_class_mappings[i].name;
107 return result.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 *s = globals->get_soundmgr();
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.
319 static char buf[16]; // FIXME
320 struct tm *t = globals->get_time_params()->getGmt();
321 sprintf(buf, " %.2d:%.2d:%.2d",
322 t->tm_hour, t->tm_min, t->tm_sec);
323 // cout << t << " " << buf << endl;
328 * Return the magnetic variation
333 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
338 * Return the magnetic dip
343 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
348 * Return the current heading in degrees.
354 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
355 if (magheading < 0) magheading += 360;
362 return globals->get_warp();
368 globals->set_warp(warp);
374 return globals->get_warp_delta();
378 setWarpDelta (long delta)
380 globals->set_warp_delta(delta);
390 setWindingCCW (bool state)
394 glFrontFace ( GL_CCW );
396 glFrontFace ( GL_CW );
402 #if defined(FX) && !defined(WIN32)
403 return globals->get_fullscreen();
410 setFullScreen (bool state)
412 #if defined(FX) && !defined(WIN32)
413 globals->set_fullscreen(state);
414 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
415 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
423 return fdm_data_logging;
427 setFDMDataLogging (bool state)
429 // kludge; no getter or setter available
430 if (state != fdm_data_logging) {
431 fgToggleFDMdataLogging();
432 fdm_data_logging = state;
437 ////////////////////////////////////////////////////////////////////////
438 // Tie the properties.
439 ////////////////////////////////////////////////////////////////////////
441 FGProperties::FGProperties ()
445 FGProperties::~FGProperties ()
450 FGProperties::init ()
455 FGProperties::bind ()
458 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
459 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
460 fgTie("/sim/freeze/master", getFreeze, setFreeze);
462 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
463 fgTie("/sim/time/gmt", getDateString, setDateString);
464 fgSetArchivable("/sim/time/gmt");
465 fgTie("/sim/time/gmt-string", getGMTString);
468 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
470 fgTie("/environment/magnetic-variation-deg", getMagVar);
471 fgTie("/environment/magnetic-dip-deg", getMagDip);
473 fgTie("/sim/time/warp", getWarp, setWarp, false);
474 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
476 // Misc. Temporary junk.
477 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
478 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
479 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
483 FGProperties::unbind ()
486 fgUntie("/sim/logging/priority");
487 fgUntie("/sim/logging/classes");
488 fgUntie("/sim/freeze/master");
490 fgUntie("/sim/time/elapsed-sec");
491 fgUntie("/sim/time/gmt");
492 fgUntie("/sim/time/gmt-string");
495 fgUntie("/orientation/heading-magnetic-deg");
498 fgUntie("/environment/magnetic-variation-deg");
499 fgUntie("/environment/magnetic-dip-deg");
501 fgUntie("/sim/time/warp");
502 fgUntie("/sim/time/warp-delta");
504 // Misc. Temporary junk.
505 fgUntie("/sim/temp/winding-ccw");
506 fgUntie("/sim/temp/full-screen");
507 fgUntie("/sim/temp/fdm-data-logging");
511 FGProperties::update (double dt)
514 struct tm *t = globals->get_time_params()->getGmt();
516 fgSetInt("/sim/time/utc/year", t->tm_year + 1900);
517 fgSetInt("/sim/time/utc/month", t->tm_mon + 1);
518 fgSetInt("/sim/time/utc/day", t->tm_mday);
519 fgSetInt("/sim/time/utc/hour", t->tm_hour);
520 fgSetInt("/sim/time/utc/minute", t->tm_min);
521 fgSetInt("/sim/time/utc/second", t->tm_sec);
523 fgSetDouble("/sim/time/utc/day-seconds",
528 fgSetInt("/sim/time/local-offset",
529 globals->get_time_params()->get_local_offset());
534 ////////////////////////////////////////////////////////////////////////
536 ////////////////////////////////////////////////////////////////////////
540 * Save the current state of the simulator to a stream.
543 fgSaveFlight (ostream &output, bool write_all)
546 fgSetBool("/sim/presets/onground", false);
547 fgSetArchivable("/sim/presets/onground");
548 fgSetBool("/sim/presets/trim", false);
549 fgSetArchivable("/sim/presets/trim");
550 fgSetString("/sim/presets/speed-set", "UVW");
551 fgSetArchivable("/sim/presets/speed-set");
554 writeProperties(output, globals->get_props(), write_all);
555 } catch (const sg_exception &e) {
556 guiErrorMessage("Error saving flight: ", e);
564 * Restore the current state of the simulator from a stream.
567 fgLoadFlight (istream &input)
569 SGPropertyNode props;
571 readProperties(input, &props);
572 } catch (const sg_exception &e) {
573 guiErrorMessage("Error reading saved flight: ", e);
577 fgSetBool("/sim/presets/onground", false);
578 fgSetBool("/sim/presets/trim", false);
579 fgSetString("/sim/presets/speed-set", "UVW");
581 copyProperties(&props, globals->get_props());
582 // When loading a flight, make it the
583 // new initial state.
584 globals->saveInitialState();
590 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
594 SGPath loadpath(globals->get_fg_root());
595 loadpath.append(path);
596 fullpath = loadpath.str();
602 readProperties(fullpath, props, default_mode);
603 } catch (const sg_exception &e) {
604 guiErrorMessage("Error reading properties: ", e);
612 ////////////////////////////////////////////////////////////////////////
613 // Property convenience functions.
614 ////////////////////////////////////////////////////////////////////////
617 fgGetNode (const char * path, bool create)
619 return globals->get_props()->getNode(path, create);
623 fgGetNode (const char * path, int index, bool create)
625 return globals->get_props()->getNode(path, index, create);
629 fgHasNode (const char * path)
631 return (fgGetNode(path, false) != 0);
635 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
637 fgGetNode(path, true)->addChangeListener(listener);
641 fgAddChangeListener (SGPropertyChangeListener * listener,
642 const char * path, int index)
644 fgGetNode(path, index, true)->addChangeListener(listener);
648 fgGetBool (const char * name, bool defaultValue)
650 return globals->get_props()->getBoolValue(name, defaultValue);
654 fgGetInt (const char * name, int defaultValue)
656 return globals->get_props()->getIntValue(name, defaultValue);
660 fgGetLong (const char * name, long defaultValue)
662 return globals->get_props()->getLongValue(name, defaultValue);
666 fgGetFloat (const char * name, float defaultValue)
668 return globals->get_props()->getFloatValue(name, defaultValue);
672 fgGetDouble (const char * name, double defaultValue)
674 return globals->get_props()->getDoubleValue(name, defaultValue);
678 fgGetString (const char * name, const char * defaultValue)
680 return globals->get_props()->getStringValue(name, defaultValue);
684 fgSetBool (const char * name, bool val)
686 return globals->get_props()->setBoolValue(name, val);
690 fgSetInt (const char * name, int val)
692 return globals->get_props()->setIntValue(name, val);
696 fgSetLong (const char * name, long val)
698 return globals->get_props()->setLongValue(name, val);
702 fgSetFloat (const char * name, float val)
704 return globals->get_props()->setFloatValue(name, val);
708 fgSetDouble (const char * name, double val)
710 return globals->get_props()->setDoubleValue(name, val);
714 fgSetString (const char * name, const char * val)
716 return globals->get_props()->setStringValue(name, val);
720 fgSetArchivable (const char * name, bool state)
722 SGPropertyNode * node = globals->get_props()->getNode(name);
724 SG_LOG(SG_GENERAL, SG_DEBUG,
725 "Attempt to set archive flag for non-existant property "
728 node->setAttribute(SGPropertyNode::ARCHIVE, state);
732 fgSetReadable (const char * name, bool state)
734 SGPropertyNode * node = globals->get_props()->getNode(name);
736 SG_LOG(SG_GENERAL, SG_DEBUG,
737 "Attempt to set read flag for non-existant property "
740 node->setAttribute(SGPropertyNode::READ, state);
744 fgSetWritable (const char * name, bool state)
746 SGPropertyNode * node = globals->get_props()->getNode(name);
748 SG_LOG(SG_GENERAL, SG_DEBUG,
749 "Attempt to set write flag for non-existant property "
752 node->setAttribute(SGPropertyNode::WRITE, state);
756 fgUntie (const char * name)
758 if (!globals->get_props()->untie(name))
759 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
763 // end of fg_props.cxx