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_ALERT, "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)
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_INFO, "Logging priority is " << getLoggingPriority());
218 * Return the current frozen state.
228 * Set the current frozen state.
235 // Stop sound on a pause
236 SGSoundMgr *s = globals->get_soundmgr();
248 * Return the current aircraft directory (UIUC) as a string.
253 return aircraft_dir.c_str();
258 * Set the current aircraft directory (UIUC).
261 setAircraftDir (const char * dir)
268 * Return the number of milliseconds elapsed since simulation started.
271 getElapsedTime_sec ()
273 return globals->get_sim_time_sec();
278 * Return the current Zulu time.
283 static char buf[64]; // FIXME
284 struct tm * t = globals->get_time_params()->getGmt();
285 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
286 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
287 t->tm_hour, t->tm_min, t->tm_sec);
293 * Set the current Zulu time.
296 setDateString (const char * date_string)
298 static const SGPropertyNode *cur_time_override
299 = fgGetNode("/sim/time/cur-time-override", true);
301 SGTime * st = globals->get_time_params();
302 struct tm * current_time = st->getGmt();
305 // Scan for basic ISO format
306 // YYYY-MM-DDTHH:MM:SS
307 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
308 &(new_time.tm_year), &(new_time.tm_mon),
309 &(new_time.tm_mday), &(new_time.tm_hour),
310 &(new_time.tm_min), &(new_time.tm_sec));
312 // Be pretty picky about this, so
313 // that strange things don't happen
314 // if the save file has been edited
317 SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
318 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
322 // OK, it looks like we got six
323 // values, one way or another.
324 new_time.tm_year -= 1900;
325 new_time.tm_mon -= 1;
327 // Now, tell flight gear to use
328 // the new time. This was far
329 // too difficult, by the way.
331 mktime(&new_time) - mktime(current_time) + globals->get_warp();
332 double lon = current_aircraft.fdm_state->get_Longitude();
333 double lat = current_aircraft.fdm_state->get_Latitude();
334 globals->set_warp(warp);
335 st->update(lon, lat, cur_time_override->getLongValue(), warp);
339 * Return the GMT as a string.
344 static char buf[16]; // FIXME
345 struct tm *t = globals->get_time_params()->getGmt();
346 sprintf(buf, " %.2d:%.2d:%.2d",
347 t->tm_hour, t->tm_min, t->tm_sec);
348 // cout << t << " " << buf << endl;
354 * Return the magnetic variation
359 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
364 * Return the magnetic dip
369 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
374 * Return the current heading in degrees.
380 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
381 if (magheading < 0) magheading += 360;
389 * Get the current visibility (meters).
394 return WeatherDatabase->getWeatherVisibility();
399 * Set the current visibility (meters).
402 setVisibility (double visibility)
404 WeatherDatabase->setWeatherVisibility(visibility);
408 * Get the current wind north velocity (feet/second).
413 return current_aircraft.fdm_state->get_V_north_airmass();
418 * Set the current wind north velocity (feet/second).
421 setWindNorth (double speed)
423 current_aircraft.fdm_state
424 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
429 * Get the current wind east velocity (feet/second).
434 return current_aircraft.fdm_state->get_V_east_airmass();
439 * Set the current wind east velocity (feet/second).
442 setWindEast (double speed)
444 SG_LOG(SG_GENERAL, SG_INFO,, "Set wind-east to " << speed );
445 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
452 * Get the current wind down velocity (feet/second).
457 return current_aircraft.fdm_state->get_V_down_airmass();
462 * Set the current wind down velocity (feet/second).
465 setWindDown (double speed)
467 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
472 #endif // FG_WEATHERCM
477 return globals->get_warp();
483 globals->set_warp(warp);
489 return globals->get_warp_delta();
493 setWarpDelta (long delta)
495 globals->set_warp_delta(delta);
505 setWindingCCW (bool state)
509 glFrontFace ( GL_CCW );
511 glFrontFace ( GL_CW );
517 #if defined(FX) && !defined(WIN32)
518 return globals->get_fullscreen();
525 setFullScreen (bool state)
527 #if defined(FX) && !defined(WIN32)
528 globals->set_fullscreen(state);
529 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
530 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
538 return fdm_data_logging;
542 setFDMDataLogging (bool state)
544 // kludge; no getter or setter available
545 if (state != fdm_data_logging) {
546 fgToggleFDMdataLogging();
547 fdm_data_logging = state;
552 ////////////////////////////////////////////////////////////////////////
553 // Tie the properties.
554 ////////////////////////////////////////////////////////////////////////
559 SG_LOG(SG_GENERAL, SG_DEBUG, "start of fgInitProps()" );
561 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
562 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
563 fgTie("/sim/freeze/master", getFreeze, setFreeze);
564 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
566 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
567 fgTie("/sim/time/gmt", getDateString, setDateString);
568 fgSetArchivable("/sim/time/gmt");
569 fgTie("/sim/time/gmt-string", getGMTString);
572 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
576 fgTie("/environment/visibility-m", getVisibility, setVisibility);
577 fgSetArchivable("/environment/visibility-m");
578 fgTie("/environment/wind-from-north-fps", getWindNorth, setWindNorth);
579 fgSetArchivable("/environment/wind-from-north-fps");
580 fgTie("/environment/wind-from-east-fps", getWindEast, setWindEast);
581 fgSetArchivable("/environment/wind-from-east-fps");
582 fgTie("/environment/wind-from-down-fps", getWindDown, setWindDown);
583 fgSetArchivable("/environment/wind-from-down-fps");
586 fgTie("/environment/magnetic-variation-deg", getMagVar);
587 fgTie("/environment/magnetic-dip-deg", getMagDip);
589 fgTie("/sim/time/warp", getWarp, setWarp, false);
590 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
592 // Misc. Temporary junk.
593 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
594 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
595 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
597 SG_LOG(SG_GENERAL, SG_DEBUG, "end of fgInitProps()" );
608 ////////////////////////////////////////////////////////////////////////
610 ////////////////////////////////////////////////////////////////////////
614 * Save the current state of the simulator to a stream.
617 fgSaveFlight (ostream &output, bool write_all)
620 fgSetBool("/sim/presets/onground", false);
621 fgSetArchivable("/sim/presets/onground");
622 fgSetBool("/sim/presets/trim", false);
623 fgSetArchivable("/sim/presets/trim");
624 fgSetString("/sim/presets/speed-set", "UVW");
625 fgSetArchivable("/sim/presets/speed-set");
628 writeProperties(output, globals->get_props(), write_all);
629 } catch (const sg_exception &e) {
630 guiErrorMessage("Error saving flight: ", e);
638 * Restore the current state of the simulator from a stream.
641 fgLoadFlight (istream &input)
643 SGPropertyNode props;
645 readProperties(input, &props);
646 } catch (const sg_exception &e) {
647 guiErrorMessage("Error reading saved flight: ", e);
651 fgSetBool("/sim/presets/onground", false);
652 fgSetBool("/sim/presets/trim", false);
653 fgSetString("/sim/presets/speed-set", "UVW");
655 copyProperties(&props, globals->get_props());
656 // When loading a flight, make it the
657 // new initial state.
658 globals->saveInitialState();
664 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
668 SGPath loadpath(globals->get_fg_root());
669 loadpath.append(path);
670 fullpath = loadpath.str();
676 readProperties(fullpath, props);
677 } catch (const sg_exception &e) {
678 guiErrorMessage("Error reading properties: ", e);
686 ////////////////////////////////////////////////////////////////////////
687 // Property convenience functions.
688 ////////////////////////////////////////////////////////////////////////
691 fgGetNode (const char * path, bool create)
693 return globals->get_props()->getNode(path, create);
697 fgGetNode (const char * path, int index, bool create)
699 return globals->get_props()->getNode(path, index, create);
703 fgHasNode (const char * path)
705 return (fgGetNode(path, false) != 0);
709 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
711 fgGetNode(path, true)->addChangeListener(listener);
715 fgAddChangeListener (SGPropertyChangeListener * listener,
716 const char * path, int index)
718 fgGetNode(path, index, true)->addChangeListener(listener);
722 fgGetBool (const char * name, bool defaultValue)
724 return globals->get_props()->getBoolValue(name, defaultValue);
728 fgGetInt (const char * name, int defaultValue)
730 return globals->get_props()->getIntValue(name, defaultValue);
734 fgGetLong (const char * name, long defaultValue)
736 return globals->get_props()->getLongValue(name, defaultValue);
740 fgGetFloat (const char * name, float defaultValue)
742 return globals->get_props()->getFloatValue(name, defaultValue);
746 fgGetDouble (const char * name, double defaultValue)
748 return globals->get_props()->getDoubleValue(name, defaultValue);
752 fgGetString (const char * name, const char * defaultValue)
754 return globals->get_props()->getStringValue(name, defaultValue);
758 fgSetBool (const char * name, bool val)
760 return globals->get_props()->setBoolValue(name, val);
764 fgSetInt (const char * name, int val)
766 return globals->get_props()->setIntValue(name, val);
770 fgSetLong (const char * name, long val)
772 return globals->get_props()->setLongValue(name, val);
776 fgSetFloat (const char * name, float val)
778 return globals->get_props()->setFloatValue(name, val);
782 fgSetDouble (const char * name, double val)
784 return globals->get_props()->setDoubleValue(name, val);
788 fgSetString (const char * name, const char * val)
790 return globals->get_props()->setStringValue(name, val);
794 fgSetArchivable (const char * name, bool state)
796 SGPropertyNode * node = globals->get_props()->getNode(name);
798 SG_LOG(SG_GENERAL, SG_ALERT,
799 "Attempt to set archive flag for non-existant property "
802 node->setAttribute(SGPropertyNode::ARCHIVE, state);
806 fgSetReadable (const char * name, bool state)
808 SGPropertyNode * node = globals->get_props()->getNode(name);
810 SG_LOG(SG_GENERAL, SG_ALERT,
811 "Attempt to set read flag for non-existant property "
814 node->setAttribute(SGPropertyNode::READ, state);
818 fgSetWritable (const char * name, bool state)
820 SGPropertyNode * node = globals->get_props()->getNode(name);
822 SG_LOG(SG_GENERAL, SG_ALERT,
823 "Attempt to set write flag for non-existant property "
826 node->setAttribute(SGPropertyNode::WRITE, state);
830 fgUntie (const char * name)
832 if (!globals->get_props()->untie(name))
833 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
837 // end of fg_props.cxx