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., 675 Mass Ave, Cambridge, MA 02139, USA.
24 # include <simgear/compiler.h>
27 #include <simgear/structure/exception.hxx>
28 #include <simgear/magvar/magvar.hxx>
29 #include <simgear/timing/sg_time.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/scene/material/matlib.hxx>
32 #include <simgear/sound/soundmgr_openal.hxx>
36 #include <ATC/ATCdisplay.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Time/tmp.hxx>
39 #include <Environment/environment.hxx>
43 #include "globals.hxx"
44 #include "fg_props.hxx"
46 SG_USING_STD(istream);
47 SG_USING_STD(ostream);
49 static bool winding_ccw = true; // FIXME: temporary
51 static bool fdm_data_logging = false; // FIXME: temporary
53 static bool frozen = false; // FIXME: temporary
57 ////////////////////////////////////////////////////////////////////////
58 // Default property bindings (not yet handled by any module).
59 ////////////////////////////////////////////////////////////////////////
61 struct LogClassMapping {
64 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
67 LogClassMapping log_class_mappings [] = {
68 LogClassMapping(SG_NONE, "none"),
69 LogClassMapping(SG_TERRAIN, "terrain"),
70 LogClassMapping(SG_ASTRO, "astro"),
71 LogClassMapping(SG_FLIGHT, "flight"),
72 LogClassMapping(SG_INPUT, "input"),
73 LogClassMapping(SG_GL, "gl"),
74 LogClassMapping(SG_VIEW, "view"),
75 LogClassMapping(SG_COCKPIT, "cockpit"),
76 LogClassMapping(SG_GENERAL, "general"),
77 LogClassMapping(SG_MATH, "math"),
78 LogClassMapping(SG_EVENT, "event"),
79 LogClassMapping(SG_AIRCRAFT, "aircraft"),
80 LogClassMapping(SG_AUTOPILOT, "autopilot"),
81 LogClassMapping(SG_IO, "io"),
82 LogClassMapping(SG_CLIPPER, "clipper"),
83 LogClassMapping(SG_NETWORK, "network"),
84 LogClassMapping(SG_INSTR, "instrumentation"),
85 LogClassMapping(SG_SYSTEMS, "systems"),
86 LogClassMapping(SG_UNDEFD, "")
91 * Get the logging classes.
96 sgDebugClass classes = logbuf::get_log_classes();
97 static string result = ""; // FIXME
98 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
99 if ((classes&log_class_mappings[i].c) > 0) {
102 result += log_class_mappings[i].name;
105 return result.c_str();
110 addLoggingClass (const string &name)
112 sgDebugClass classes = logbuf::get_log_classes();
113 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
114 if (name == log_class_mappings[i].name) {
115 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
119 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
124 * Set the logging classes.
127 setLoggingClasses (const char * c)
130 logbuf::set_log_classes(SG_NONE);
132 if (classes == "none") {
133 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
137 if (classes.empty() || classes == "all") { // default
138 logbuf::set_log_classes(SG_ALL);
139 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
140 << getLoggingClasses());
144 string rest = classes;
146 int sep = rest.find('|');
148 name = rest.substr(0, sep);
149 rest = rest.substr(sep+1);
150 addLoggingClass(name);
151 sep = rest.find('|');
153 addLoggingClass(rest);
154 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
155 << getLoggingClasses());
160 * Get the logging priority.
163 getLoggingPriority ()
165 switch (logbuf::get_log_priority()) {
177 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
178 << logbuf::get_log_priority());
185 * Set the logging priority.
188 setLoggingPriority (const char * p)
193 if (priority == "bulk") {
194 logbuf::set_log_priority(SG_BULK);
195 } else if (priority == "debug") {
196 logbuf::set_log_priority(SG_DEBUG);
197 } else if (priority.empty() || priority == "info") { // default
198 logbuf::set_log_priority(SG_INFO);
199 } else if (priority == "warn") {
200 logbuf::set_log_priority(SG_WARN);
201 } else if (priority == "alert") {
202 logbuf::set_log_priority(SG_ALERT);
204 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
206 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
211 * Return the current frozen state.
221 * Set the current frozen state.
228 // Stop sound on a pause
229 SGSoundMgr *s = globals->get_soundmgr();
241 * Return the number of milliseconds elapsed since simulation started.
244 getElapsedTime_sec ()
246 return globals->get_sim_time_sec();
251 * Return the current Zulu time.
256 static char buf[64]; // FIXME
257 struct tm * t = globals->get_time_params()->getGmt();
258 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
259 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
260 t->tm_hour, t->tm_min, t->tm_sec);
266 * Set the current Zulu time.
269 setDateString (const char * date_string)
271 static const SGPropertyNode *cur_time_override
272 = fgGetNode("/sim/time/cur-time-override", true);
274 SGTime * st = globals->get_time_params();
275 struct tm * current_time = st->getGmt();
278 // Scan for basic ISO format
279 // YYYY-MM-DDTHH:MM:SS
280 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
281 &(new_time.tm_year), &(new_time.tm_mon),
282 &(new_time.tm_mday), &(new_time.tm_hour),
283 &(new_time.tm_min), &(new_time.tm_sec));
285 // Be pretty picky about this, so
286 // that strange things don't happen
287 // if the save file has been edited
290 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
291 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
295 // OK, it looks like we got six
296 // values, one way or another.
297 new_time.tm_year -= 1900;
298 new_time.tm_mon -= 1;
300 // Now, tell flight gear to use
301 // the new time. This was far
302 // too difficult, by the way.
304 mktime(&new_time) - mktime(current_time) + globals->get_warp();
305 double lon = current_aircraft.fdm_state->get_Longitude();
306 double lat = current_aircraft.fdm_state->get_Latitude();
307 globals->set_warp(warp);
308 st->update(lon, lat, cur_time_override->getLongValue(), warp);
312 * Return the GMT as a string.
317 static char buf[16]; // FIXME
318 struct tm *t = globals->get_time_params()->getGmt();
319 sprintf(buf, " %.2d:%.2d:%.2d",
320 t->tm_hour, t->tm_min, t->tm_sec);
321 // cout << t << " " << buf << endl;
326 * Return the magnetic variation
331 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
336 * Return the magnetic dip
341 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
346 * Return the current heading in degrees.
352 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
353 if (magheading < 0) magheading += 360;
360 return globals->get_warp();
366 globals->set_warp(warp);
372 return globals->get_warp_delta();
376 setWarpDelta (long delta)
378 globals->set_warp_delta(delta);
388 setWindingCCW (bool state)
392 glFrontFace ( GL_CCW );
394 glFrontFace ( GL_CW );
400 #if defined(FX) && !defined(WIN32)
401 return globals->get_fullscreen();
408 setFullScreen (bool state)
410 #if defined(FX) && !defined(WIN32)
411 globals->set_fullscreen(state);
412 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
413 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
421 return fdm_data_logging;
425 setFDMDataLogging (bool state)
427 // kludge; no getter or setter available
428 if (state != fdm_data_logging) {
429 fgToggleFDMdataLogging();
430 fdm_data_logging = state;
435 ////////////////////////////////////////////////////////////////////////
436 // Tie the properties.
437 ////////////////////////////////////////////////////////////////////////
439 FGProperties::FGProperties ()
443 FGProperties::~FGProperties ()
448 FGProperties::init ()
453 FGProperties::bind ()
456 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
457 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
458 fgTie("/sim/freeze/master", getFreeze, setFreeze);
460 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
461 fgTie("/sim/time/gmt", getDateString, setDateString);
462 fgSetArchivable("/sim/time/gmt");
463 fgTie("/sim/time/gmt-string", getGMTString);
466 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
468 fgTie("/environment/magnetic-variation-deg", getMagVar);
469 fgTie("/environment/magnetic-dip-deg", getMagDip);
471 fgTie("/sim/time/warp", getWarp, setWarp, false);
472 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
474 // Misc. Temporary junk.
475 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
476 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
477 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
481 FGProperties::unbind ()
484 fgUntie("/sim/logging/priority");
485 fgUntie("/sim/logging/classes");
486 fgUntie("/sim/freeze/master");
488 fgUntie("/sim/time/elapsed-sec");
489 fgUntie("/sim/time/gmt");
490 fgUntie("/sim/time/gmt-string");
493 fgUntie("/orientation/heading-magnetic-deg");
496 fgUntie("/environment/magnetic-variation-deg");
497 fgUntie("/environment/magnetic-dip-deg");
499 fgUntie("/sim/time/warp");
500 fgUntie("/sim/time/warp-delta");
502 // Misc. Temporary junk.
503 fgUntie("/sim/temp/winding-ccw");
504 fgUntie("/sim/temp/full-screen");
505 fgUntie("/sim/temp/fdm-data-logging");
509 FGProperties::update (double dt)
512 struct tm *t = globals->get_time_params()->getGmt();
514 fgSetInt("/sim/time/utc/year", t->tm_year + 1900);
515 fgSetInt("/sim/time/utc/month", t->tm_mon + 1);
516 fgSetInt("/sim/time/utc/day", t->tm_mday);
517 fgSetInt("/sim/time/utc/hour", t->tm_hour);
518 fgSetInt("/sim/time/utc/minute", t->tm_min);
519 fgSetInt("/sim/time/utc/second", t->tm_sec);
521 fgSetDouble("/sim/time/utc/day-seconds",
526 fgSetInt("/sim/time/local-offset",
527 globals->get_time_params()->get_local_offset());
532 ////////////////////////////////////////////////////////////////////////
534 ////////////////////////////////////////////////////////////////////////
538 * Save the current state of the simulator to a stream.
541 fgSaveFlight (ostream &output, bool write_all)
544 fgSetBool("/sim/presets/onground", false);
545 fgSetArchivable("/sim/presets/onground");
546 fgSetBool("/sim/presets/trim", false);
547 fgSetArchivable("/sim/presets/trim");
548 fgSetString("/sim/presets/speed-set", "UVW");
549 fgSetArchivable("/sim/presets/speed-set");
552 writeProperties(output, globals->get_props(), write_all);
553 } catch (const sg_exception &e) {
554 guiErrorMessage("Error saving flight: ", e);
562 * Restore the current state of the simulator from a stream.
565 fgLoadFlight (istream &input)
567 SGPropertyNode props;
569 readProperties(input, &props);
570 } catch (const sg_exception &e) {
571 guiErrorMessage("Error reading saved flight: ", e);
575 fgSetBool("/sim/presets/onground", false);
576 fgSetBool("/sim/presets/trim", false);
577 fgSetString("/sim/presets/speed-set", "UVW");
579 copyProperties(&props, globals->get_props());
580 // When loading a flight, make it the
581 // new initial state.
582 globals->saveInitialState();
588 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root, int default_mode)
592 SGPath loadpath(globals->get_fg_root());
593 loadpath.append(path);
594 fullpath = loadpath.str();
600 readProperties(fullpath, props, default_mode);
601 } catch (const sg_exception &e) {
602 guiErrorMessage("Error reading properties: ", e);
610 ////////////////////////////////////////////////////////////////////////
611 // Property convenience functions.
612 ////////////////////////////////////////////////////////////////////////
615 fgGetNode (const char * path, bool create)
617 return globals->get_props()->getNode(path, create);
621 fgGetNode (const char * path, int index, bool create)
623 return globals->get_props()->getNode(path, index, create);
627 fgHasNode (const char * path)
629 return (fgGetNode(path, false) != 0);
633 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
635 fgGetNode(path, true)->addChangeListener(listener);
639 fgAddChangeListener (SGPropertyChangeListener * listener,
640 const char * path, int index)
642 fgGetNode(path, index, true)->addChangeListener(listener);
646 fgGetBool (const char * name, bool defaultValue)
648 return globals->get_props()->getBoolValue(name, defaultValue);
652 fgGetInt (const char * name, int defaultValue)
654 return globals->get_props()->getIntValue(name, defaultValue);
658 fgGetLong (const char * name, long defaultValue)
660 return globals->get_props()->getLongValue(name, defaultValue);
664 fgGetFloat (const char * name, float defaultValue)
666 return globals->get_props()->getFloatValue(name, defaultValue);
670 fgGetDouble (const char * name, double defaultValue)
672 return globals->get_props()->getDoubleValue(name, defaultValue);
676 fgGetString (const char * name, const char * defaultValue)
678 return globals->get_props()->getStringValue(name, defaultValue);
682 fgSetBool (const char * name, bool val)
684 return globals->get_props()->setBoolValue(name, val);
688 fgSetInt (const char * name, int val)
690 return globals->get_props()->setIntValue(name, val);
694 fgSetLong (const char * name, long val)
696 return globals->get_props()->setLongValue(name, val);
700 fgSetFloat (const char * name, float val)
702 return globals->get_props()->setFloatValue(name, val);
706 fgSetDouble (const char * name, double val)
708 return globals->get_props()->setDoubleValue(name, val);
712 fgSetString (const char * name, const char * val)
714 return globals->get_props()->setStringValue(name, val);
718 fgSetArchivable (const char * name, bool state)
720 SGPropertyNode * node = globals->get_props()->getNode(name);
722 SG_LOG(SG_GENERAL, SG_DEBUG,
723 "Attempt to set archive flag for non-existant property "
726 node->setAttribute(SGPropertyNode::ARCHIVE, state);
730 fgSetReadable (const char * name, bool state)
732 SGPropertyNode * node = globals->get_props()->getNode(name);
734 SG_LOG(SG_GENERAL, SG_DEBUG,
735 "Attempt to set read flag for non-existant property "
738 node->setAttribute(SGPropertyNode::READ, state);
742 fgSetWritable (const char * name, bool state)
744 SGPropertyNode * node = globals->get_props()->getNode(name);
746 SG_LOG(SG_GENERAL, SG_DEBUG,
747 "Attempt to set write flag for non-existant property "
750 node->setAttribute(SGPropertyNode::WRITE, state);
754 fgUntie (const char * name)
756 if (!globals->get_props()->untie(name))
757 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
761 // end of fg_props.cxx