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.hxx>
36 #include <ATC/ATCdisplay.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Time/tmp.hxx>
39 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
41 # include <WeatherCM/FGLocalWeatherDatabase.h>
43 # include <Environment/environment.hxx>
44 #endif // FG_WEATHERCM
48 #include "globals.hxx"
49 #include "fg_props.hxx"
51 SG_USING_STD(istream);
52 SG_USING_STD(ostream);
55 static double getWindNorth ();
56 static double getWindEast ();
57 static double getWindDown ();
58 #endif // FG_WEATHERCM
60 static bool winding_ccw = true; // FIXME: temporary
62 static bool fdm_data_logging = false; // FIXME: temporary
64 static bool frozen = false; // FIXME: temporary
68 ////////////////////////////////////////////////////////////////////////
69 // Default property bindings (not yet handled by any module).
70 ////////////////////////////////////////////////////////////////////////
72 struct LogClassMapping {
75 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
78 LogClassMapping log_class_mappings [] = {
79 LogClassMapping(SG_NONE, "none"),
80 LogClassMapping(SG_TERRAIN, "terrain"),
81 LogClassMapping(SG_ASTRO, "astro"),
82 LogClassMapping(SG_FLIGHT, "flight"),
83 LogClassMapping(SG_INPUT, "input"),
84 LogClassMapping(SG_GL, "gl"),
85 LogClassMapping(SG_VIEW, "view"),
86 LogClassMapping(SG_COCKPIT, "cockpit"),
87 LogClassMapping(SG_GENERAL, "general"),
88 LogClassMapping(SG_MATH, "math"),
89 LogClassMapping(SG_EVENT, "event"),
90 LogClassMapping(SG_AIRCRAFT, "aircraft"),
91 LogClassMapping(SG_AUTOPILOT, "autopilot"),
92 LogClassMapping(SG_IO, "io"),
93 LogClassMapping(SG_CLIPPER, "clipper"),
94 LogClassMapping(SG_NETWORK, "network"),
95 LogClassMapping(SG_UNDEFD, "")
100 * Get the logging classes.
105 sgDebugClass classes = logbuf::get_log_classes();
106 static string result = ""; // FIXME
107 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
108 if ((classes&log_class_mappings[i].c) > 0) {
111 result += log_class_mappings[i].name;
114 return result.c_str();
119 addLoggingClass (const string &name)
121 sgDebugClass classes = logbuf::get_log_classes();
122 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
123 if (name == log_class_mappings[i].name) {
124 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
128 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
133 * Set the logging classes.
136 setLoggingClasses (const char * c)
139 logbuf::set_log_classes(SG_NONE);
141 if (classes == "none") {
142 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
146 if (classes.empty() || classes == "all") { // default
147 logbuf::set_log_classes(SG_ALL);
148 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
149 << getLoggingClasses());
153 string rest = classes;
155 int sep = rest.find('|');
157 name = rest.substr(0, sep);
158 rest = rest.substr(sep+1);
159 addLoggingClass(name);
160 sep = rest.find('|');
162 addLoggingClass(rest);
163 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
164 << getLoggingClasses());
169 * Get the logging priority.
172 getLoggingPriority ()
174 switch (logbuf::get_log_priority()) {
186 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
187 << logbuf::get_log_priority());
194 * Set the logging priority.
197 setLoggingPriority (const char * p)
202 if (priority == "bulk") {
203 logbuf::set_log_priority(SG_BULK);
204 } else if (priority == "debug") {
205 logbuf::set_log_priority(SG_DEBUG);
206 } else if (priority.empty() || priority == "info") { // default
207 logbuf::set_log_priority(SG_INFO);
208 } else if (priority == "warn") {
209 logbuf::set_log_priority(SG_WARN);
210 } else if (priority == "alert") {
211 logbuf::set_log_priority(SG_ALERT);
213 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
215 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
220 * Return the current frozen state.
230 * Set the current frozen state.
237 // Stop sound on a pause
238 SGSoundMgr *s = globals->get_soundmgr();
250 * Return the current aircraft directory (UIUC) as a string.
255 return aircraft_dir.c_str();
260 * Set the current aircraft directory (UIUC).
263 setAircraftDir (const char * dir)
270 * Return the number of milliseconds elapsed since simulation started.
273 getElapsedTime_sec ()
275 return globals->get_sim_time_sec();
280 * Return the current Zulu time.
285 static char buf[64]; // FIXME
286 struct tm * t = globals->get_time_params()->getGmt();
287 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
288 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
289 t->tm_hour, t->tm_min, t->tm_sec);
295 * Set the current Zulu time.
298 setDateString (const char * date_string)
300 static const SGPropertyNode *cur_time_override
301 = fgGetNode("/sim/time/cur-time-override", true);
303 SGTime * st = globals->get_time_params();
304 struct tm * current_time = st->getGmt();
307 // Scan for basic ISO format
308 // YYYY-MM-DDTHH:MM:SS
309 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
310 &(new_time.tm_year), &(new_time.tm_mon),
311 &(new_time.tm_mday), &(new_time.tm_hour),
312 &(new_time.tm_min), &(new_time.tm_sec));
314 // Be pretty picky about this, so
315 // that strange things don't happen
316 // if the save file has been edited
319 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
320 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
324 // OK, it looks like we got six
325 // values, one way or another.
326 new_time.tm_year -= 1900;
327 new_time.tm_mon -= 1;
329 // Now, tell flight gear to use
330 // the new time. This was far
331 // too difficult, by the way.
333 mktime(&new_time) - mktime(current_time) + globals->get_warp();
334 double lon = current_aircraft.fdm_state->get_Longitude();
335 double lat = current_aircraft.fdm_state->get_Latitude();
336 globals->set_warp(warp);
337 st->update(lon, lat, cur_time_override->getLongValue(), warp);
341 * Return the GMT as a string.
346 static char buf[16]; // FIXME
347 struct tm *t = globals->get_time_params()->getGmt();
348 sprintf(buf, " %.2d:%.2d:%.2d",
349 t->tm_hour, t->tm_min, t->tm_sec);
350 // cout << t << " " << buf << endl;
355 * Return the magnetic variation
360 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
365 * Return the magnetic dip
370 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
375 * Return the current heading in degrees.
381 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
382 if (magheading < 0) magheading += 360;
390 * Get the current visibility (meters).
395 return WeatherDatabase->getWeatherVisibility();
400 * Set the current visibility (meters).
403 setVisibility (double visibility)
405 WeatherDatabase->setWeatherVisibility(visibility);
409 * Get the current wind north velocity (feet/second).
414 return current_aircraft.fdm_state->get_V_north_airmass();
419 * Set the current wind north velocity (feet/second).
422 setWindNorth (double speed)
424 current_aircraft.fdm_state
425 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
430 * Get the current wind east velocity (feet/second).
435 return current_aircraft.fdm_state->get_V_east_airmass();
440 * Set the current wind east velocity (feet/second).
443 setWindEast (double speed)
445 SG_LOG(SG_GENERAL, SG_INFO,, "Set wind-east to " << speed );
446 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
453 * Get the current wind down velocity (feet/second).
458 return current_aircraft.fdm_state->get_V_down_airmass();
463 * Set the current wind down velocity (feet/second).
466 setWindDown (double speed)
468 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
473 #endif // FG_WEATHERCM
478 return globals->get_warp();
484 globals->set_warp(warp);
490 return globals->get_warp_delta();
494 setWarpDelta (long delta)
496 globals->set_warp_delta(delta);
506 setWindingCCW (bool state)
510 glFrontFace ( GL_CCW );
512 glFrontFace ( GL_CW );
518 #if defined(FX) && !defined(WIN32)
519 return globals->get_fullscreen();
526 setFullScreen (bool state)
528 #if defined(FX) && !defined(WIN32)
529 globals->set_fullscreen(state);
530 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
531 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
539 return fdm_data_logging;
543 setFDMDataLogging (bool state)
545 // kludge; no getter or setter available
546 if (state != fdm_data_logging) {
547 fgToggleFDMdataLogging();
548 fdm_data_logging = state;
553 ////////////////////////////////////////////////////////////////////////
554 // Tie the properties.
555 ////////////////////////////////////////////////////////////////////////
557 FGProperties::FGProperties ()
561 FGProperties::~FGProperties ()
566 FGProperties::init ()
571 FGProperties::bind ()
574 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
575 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
576 fgTie("/sim/freeze/master", getFreeze, setFreeze);
577 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
579 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
580 fgTie("/sim/time/gmt", getDateString, setDateString);
581 fgSetArchivable("/sim/time/gmt");
582 fgTie("/sim/time/gmt-string", getGMTString);
585 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
589 fgTie("/environment/visibility-m", getVisibility, setVisibility);
590 fgSetArchivable("/environment/visibility-m");
591 fgTie("/environment/wind-from-north-fps", getWindNorth, setWindNorth);
592 fgSetArchivable("/environment/wind-from-north-fps");
593 fgTie("/environment/wind-from-east-fps", getWindEast, setWindEast);
594 fgSetArchivable("/environment/wind-from-east-fps");
595 fgTie("/environment/wind-from-down-fps", getWindDown, setWindDown);
596 fgSetArchivable("/environment/wind-from-down-fps");
599 fgTie("/environment/magnetic-variation-deg", getMagVar);
600 fgTie("/environment/magnetic-dip-deg", getMagDip);
602 fgTie("/sim/time/warp", getWarp, setWarp, false);
603 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
605 // Misc. Temporary junk.
606 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
607 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
608 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
612 FGProperties::unbind ()
615 fgUntie("/sim/logging/priority");
616 fgUntie("/sim/logging/classes");
617 fgUntie("/sim/freeze/master");
618 fgUntie("/sim/aircraft-dir");
620 fgUntie("/sim/time/elapsed-sec");
621 fgUntie("/sim/time/gmt");
622 fgUntie("/sim/time/gmt-string");
625 fgUntie("/orientation/heading-magnetic-deg");
629 fgUntie("/environment/visibility-m");
630 fgUntie("/environment/wind-from-north-fps");
631 fgUntie("/environment/wind-from-east-fps");
632 fgUntie("/environment/wind-from-down-fps");
635 fgUntie("/environment/magnetic-variation-deg");
636 fgUntie("/environment/magnetic-dip-deg");
638 fgUntie("/sim/time/warp");
639 fgUntie("/sim/time/warp-delta");
641 // Misc. Temporary junk.
642 fgUntie("/sim/temp/winding-ccw");
643 fgUntie("/sim/temp/full-screen");
644 fgUntie("/sim/temp/fdm-data-logging");
648 FGProperties::update (double dt)
651 struct tm * t = globals->get_time_params()->getGmt();
653 fgSetInt("/sim/time/utc/year", t->tm_year + 1900);
654 fgSetInt("/sim/time/utc/month", t->tm_mon + 1);
655 fgSetInt("/sim/time/utc/day", t->tm_mday);
656 fgSetInt("/sim/time/utc/hour", t->tm_hour);
657 fgSetInt("/sim/time/utc/minute", t->tm_min);
658 fgSetInt("/sim/time/utc/second", t->tm_sec);
660 fgSetDouble("/sim/time/utc/day-seconds",
668 ////////////////////////////////////////////////////////////////////////
670 ////////////////////////////////////////////////////////////////////////
674 * Save the current state of the simulator to a stream.
677 fgSaveFlight (ostream &output, bool write_all)
680 fgSetBool("/sim/presets/onground", false);
681 fgSetArchivable("/sim/presets/onground");
682 fgSetBool("/sim/presets/trim", false);
683 fgSetArchivable("/sim/presets/trim");
684 fgSetString("/sim/presets/speed-set", "UVW");
685 fgSetArchivable("/sim/presets/speed-set");
688 writeProperties(output, globals->get_props(), write_all);
689 } catch (const sg_exception &e) {
690 guiErrorMessage("Error saving flight: ", e);
698 * Restore the current state of the simulator from a stream.
701 fgLoadFlight (istream &input)
703 SGPropertyNode props;
705 readProperties(input, &props);
706 } catch (const sg_exception &e) {
707 guiErrorMessage("Error reading saved flight: ", e);
711 fgSetBool("/sim/presets/onground", false);
712 fgSetBool("/sim/presets/trim", false);
713 fgSetString("/sim/presets/speed-set", "UVW");
715 copyProperties(&props, globals->get_props());
716 // When loading a flight, make it the
717 // new initial state.
718 globals->saveInitialState();
724 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
728 SGPath loadpath(globals->get_fg_root());
729 loadpath.append(path);
730 fullpath = loadpath.str();
736 readProperties(fullpath, props);
737 } catch (const sg_exception &e) {
738 guiErrorMessage("Error reading properties: ", e);
746 ////////////////////////////////////////////////////////////////////////
747 // Property convenience functions.
748 ////////////////////////////////////////////////////////////////////////
751 fgGetNode (const char * path, bool create)
753 return globals->get_props()->getNode(path, create);
757 fgGetNode (const char * path, int index, bool create)
759 return globals->get_props()->getNode(path, index, create);
763 fgHasNode (const char * path)
765 return (fgGetNode(path, false) != 0);
769 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
771 fgGetNode(path, true)->addChangeListener(listener);
775 fgAddChangeListener (SGPropertyChangeListener * listener,
776 const char * path, int index)
778 fgGetNode(path, index, true)->addChangeListener(listener);
782 fgGetBool (const char * name, bool defaultValue)
784 return globals->get_props()->getBoolValue(name, defaultValue);
788 fgGetInt (const char * name, int defaultValue)
790 return globals->get_props()->getIntValue(name, defaultValue);
794 fgGetLong (const char * name, long defaultValue)
796 return globals->get_props()->getLongValue(name, defaultValue);
800 fgGetFloat (const char * name, float defaultValue)
802 return globals->get_props()->getFloatValue(name, defaultValue);
806 fgGetDouble (const char * name, double defaultValue)
808 return globals->get_props()->getDoubleValue(name, defaultValue);
812 fgGetString (const char * name, const char * defaultValue)
814 return globals->get_props()->getStringValue(name, defaultValue);
818 fgSetBool (const char * name, bool val)
820 return globals->get_props()->setBoolValue(name, val);
824 fgSetInt (const char * name, int val)
826 return globals->get_props()->setIntValue(name, val);
830 fgSetLong (const char * name, long val)
832 return globals->get_props()->setLongValue(name, val);
836 fgSetFloat (const char * name, float val)
838 return globals->get_props()->setFloatValue(name, val);
842 fgSetDouble (const char * name, double val)
844 return globals->get_props()->setDoubleValue(name, val);
848 fgSetString (const char * name, const char * val)
850 return globals->get_props()->setStringValue(name, val);
854 fgSetArchivable (const char * name, bool state)
856 SGPropertyNode * node = globals->get_props()->getNode(name);
858 SG_LOG(SG_GENERAL, SG_DEBUG,
859 "Attempt to set archive flag for non-existant property "
862 node->setAttribute(SGPropertyNode::ARCHIVE, state);
866 fgSetReadable (const char * name, bool state)
868 SGPropertyNode * node = globals->get_props()->getNode(name);
870 SG_LOG(SG_GENERAL, SG_DEBUG,
871 "Attempt to set read flag for non-existant property "
874 node->setAttribute(SGPropertyNode::READ, state);
878 fgSetWritable (const char * name, bool state)
880 SGPropertyNode * node = globals->get_props()->getNode(name);
882 SG_LOG(SG_GENERAL, SG_DEBUG,
883 "Attempt to set write flag for non-existant property "
886 node->setAttribute(SGPropertyNode::WRITE, state);
890 fgUntie (const char * name)
892 if (!globals->get_props()->untie(name))
893 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
897 // end of fg_props.cxx