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/misc/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"
50 #include "fg_props.hxx"
52 SG_USING_STD(istream);
53 SG_USING_STD(ostream);
56 static double getWindNorth ();
57 static double getWindEast ();
58 static double getWindDown ();
59 #endif // FG_WEATHERCM
61 static bool winding_ccw = true; // FIXME: temporary
63 static bool fdm_data_logging = false; // FIXME: temporary
65 static bool frozen = false; // FIXME: temporary
69 ////////////////////////////////////////////////////////////////////////
70 // Default property bindings (not yet handled by any module).
71 ////////////////////////////////////////////////////////////////////////
73 struct LogClassMapping {
76 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
79 LogClassMapping log_class_mappings [] = {
80 LogClassMapping(SG_NONE, "none"),
81 LogClassMapping(SG_TERRAIN, "terrain"),
82 LogClassMapping(SG_ASTRO, "astro"),
83 LogClassMapping(SG_FLIGHT, "flight"),
84 LogClassMapping(SG_INPUT, "input"),
85 LogClassMapping(SG_GL, "gl"),
86 LogClassMapping(SG_VIEW, "view"),
87 LogClassMapping(SG_COCKPIT, "cockpit"),
88 LogClassMapping(SG_GENERAL, "general"),
89 LogClassMapping(SG_MATH, "math"),
90 LogClassMapping(SG_EVENT, "event"),
91 LogClassMapping(SG_AIRCRAFT, "aircraft"),
92 LogClassMapping(SG_AUTOPILOT, "autopilot"),
93 LogClassMapping(SG_IO, "io"),
94 LogClassMapping(SG_CLIPPER, "clipper"),
95 LogClassMapping(SG_NETWORK, "network"),
96 LogClassMapping(SG_UNDEFD, "")
101 * Get the logging classes.
106 sgDebugClass classes = logbuf::get_log_classes();
107 static string result = ""; // FIXME
108 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
109 if ((classes&log_class_mappings[i].c) > 0) {
112 result += log_class_mappings[i].name;
115 return result.c_str();
120 addLoggingClass (const string &name)
122 sgDebugClass classes = logbuf::get_log_classes();
123 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
124 if (name == log_class_mappings[i].name) {
125 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
129 SG_LOG(SG_GENERAL, SG_ALERT, "Unknown logging class: " << name);
134 * Set the logging classes.
137 setLoggingClasses (const char * c)
140 logbuf::set_log_classes(SG_NONE);
142 if (classes == "none") {
143 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
147 if (classes.empty() || classes == "all") { // default
148 logbuf::set_log_classes(SG_ALL);
149 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
150 << getLoggingClasses());
154 string rest = classes;
156 int sep = rest.find('|');
158 name = rest.substr(0, sep);
159 rest = rest.substr(sep+1);
160 addLoggingClass(name);
161 sep = rest.find('|');
163 addLoggingClass(rest);
164 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
165 << getLoggingClasses());
170 * Get the logging priority.
173 getLoggingPriority ()
175 switch (logbuf::get_log_priority()) {
187 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
188 << logbuf::get_log_priority());
195 * Set the logging priority.
198 setLoggingPriority (const char * p)
201 if (priority == "bulk") {
202 logbuf::set_log_priority(SG_BULK);
203 } else if (priority == "debug") {
204 logbuf::set_log_priority(SG_DEBUG);
205 } else if (priority.empty() || priority == "info") { // default
206 logbuf::set_log_priority(SG_INFO);
207 } else if (priority == "warn") {
208 logbuf::set_log_priority(SG_WARN);
209 } else if (priority == "alert") {
210 logbuf::set_log_priority(SG_ALERT);
212 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
214 SG_LOG(SG_GENERAL, SG_INFO, "Logging priority is " << getLoggingPriority());
219 * Return the current frozen state.
229 * Set the current frozen state.
236 // Stop sound on a pause
237 SGSoundMgr *s = globals->get_soundmgr();
249 * Return the current aircraft directory (UIUC) as a string.
254 return aircraft_dir.c_str();
259 * Set the current aircraft directory (UIUC).
262 setAircraftDir (const char * dir)
269 * Return the number of milliseconds elapsed since simulation started.
272 getElapsedTime_sec ()
274 return globals->get_sim_time_sec();
279 * Return the current Zulu time.
284 static char buf[64]; // FIXME
285 struct tm * t = globals->get_time_params()->getGmt();
286 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
287 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
288 t->tm_hour, t->tm_min, t->tm_sec);
294 * Set the current Zulu time.
297 setDateString (const char * date_string)
299 static const SGPropertyNode *cur_time_override
300 = fgGetNode("/sim/time/cur-time-override", true);
302 SGTime * st = globals->get_time_params();
303 struct tm * current_time = st->getGmt();
306 // Scan for basic ISO format
307 // YYYY-MM-DDTHH:MM:SS
308 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
309 &(new_time.tm_year), &(new_time.tm_mon),
310 &(new_time.tm_mday), &(new_time.tm_hour),
311 &(new_time.tm_min), &(new_time.tm_sec));
313 // Be pretty picky about this, so
314 // that strange things don't happen
315 // if the save file has been edited
318 SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
319 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
323 // OK, it looks like we got six
324 // values, one way or another.
325 new_time.tm_year -= 1900;
326 new_time.tm_mon -= 1;
328 // Now, tell flight gear to use
329 // the new time. This was far
330 // too difficult, by the way.
332 mktime(&new_time) - mktime(current_time) + globals->get_warp();
333 double lon = current_aircraft.fdm_state->get_Longitude();
334 double lat = current_aircraft.fdm_state->get_Latitude();
335 globals->set_warp(warp);
336 st->update(lon, lat, cur_time_override->getLongValue(), warp);
337 fgUpdateSkyAndLightingParams();
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;
356 * Return the magnetic variation
361 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
366 * Return the magnetic dip
371 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
376 * Return the current heading in degrees.
382 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
383 if (magheading < 0) magheading += 360;
391 * Get the current visibility (meters).
396 return WeatherDatabase->getWeatherVisibility();
401 * Set the current visibility (meters).
404 setVisibility (double visibility)
406 WeatherDatabase->setWeatherVisibility(visibility);
410 * Get the current wind north velocity (feet/second).
415 return current_aircraft.fdm_state->get_V_north_airmass();
420 * Set the current wind north velocity (feet/second).
423 setWindNorth (double speed)
425 current_aircraft.fdm_state
426 ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
431 * Get the current wind east velocity (feet/second).
436 return current_aircraft.fdm_state->get_V_east_airmass();
441 * Set the current wind east velocity (feet/second).
444 setWindEast (double speed)
446 SG_LOG(SG_GENERAL, SG_INFO,, "Set wind-east to " << speed );
447 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
454 * Get the current wind down velocity (feet/second).
459 return current_aircraft.fdm_state->get_V_down_airmass();
464 * Set the current wind down velocity (feet/second).
467 setWindDown (double speed)
469 current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
474 #endif // FG_WEATHERCM
479 return globals->get_warp();
485 globals->set_warp(warp);
491 return globals->get_warp_delta();
495 setWarpDelta (long delta)
497 globals->set_warp_delta(delta);
507 setWindingCCW (bool state)
511 glFrontFace ( GL_CCW );
513 glFrontFace ( GL_CW );
519 #if defined(FX) && !defined(WIN32)
520 return globals->get_fullscreen();
527 setFullScreen (bool state)
529 #if defined(FX) && !defined(WIN32)
530 globals->set_fullscreen(state);
531 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
532 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
540 return fdm_data_logging;
544 setFDMDataLogging (bool state)
546 // kludge; no getter or setter available
547 if (state != fdm_data_logging) {
548 fgToggleFDMdataLogging();
549 fdm_data_logging = state;
554 ////////////////////////////////////////////////////////////////////////
555 // Tie the properties.
556 ////////////////////////////////////////////////////////////////////////
561 SG_LOG(SG_GENERAL, SG_DEBUG, "start of fgInitProps()" );
563 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
564 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
565 fgTie("/sim/freeze/master", getFreeze, setFreeze);
566 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
568 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
569 fgTie("/sim/time/gmt", getDateString, setDateString);
570 fgSetArchivable("/sim/time/gmt");
571 fgTie("/sim/time/gmt-string", getGMTString);
574 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
578 fgTie("/environment/visibility-m", getVisibility, setVisibility);
579 fgSetArchivable("/environment/visibility-m");
580 fgTie("/environment/wind-from-north-fps", getWindNorth, setWindNorth);
581 fgSetArchivable("/environment/wind-from-north-fps");
582 fgTie("/environment/wind-from-east-fps", getWindEast, setWindEast);
583 fgSetArchivable("/environment/wind-from-east-fps");
584 fgTie("/environment/wind-from-down-fps", getWindDown, setWindDown);
585 fgSetArchivable("/environment/wind-from-down-fps");
588 fgTie("/environment/magnetic-variation-deg", getMagVar);
589 fgTie("/environment/magnetic-dip-deg", getMagDip);
591 fgTie("/sim/time/warp", getWarp, setWarp, false);
592 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
594 // Misc. Temporary junk.
595 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
596 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
597 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
599 SG_LOG(SG_GENERAL, SG_DEBUG, "end of fgInitProps()" );
610 ////////////////////////////////////////////////////////////////////////
612 ////////////////////////////////////////////////////////////////////////
616 * Save the current state of the simulator to a stream.
619 fgSaveFlight (ostream &output, bool write_all)
622 fgSetBool("/sim/presets/onground", false);
623 fgSetArchivable("/sim/presets/onground");
624 fgSetBool("/sim/presets/trim", false);
625 fgSetArchivable("/sim/presets/trim");
626 fgSetString("/sim/presets/speed-set", "UVW");
627 fgSetArchivable("/sim/presets/speed-set");
630 writeProperties(output, globals->get_props(), write_all);
631 } catch (const sg_exception &e) {
632 guiErrorMessage("Error saving flight: ", e);
640 * Restore the current state of the simulator from a stream.
643 fgLoadFlight (istream &input)
645 SGPropertyNode props;
647 readProperties(input, &props);
648 } catch (const sg_exception &e) {
649 guiErrorMessage("Error reading saved flight: ", e);
653 fgSetBool("/sim/presets/onground", false);
654 fgSetBool("/sim/presets/trim", false);
655 fgSetString("/sim/presets/speed-set", "UVW");
657 copyProperties(&props, globals->get_props());
658 // When loading a flight, make it the
659 // new initial state.
660 globals->saveInitialState();
666 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
670 SGPath loadpath(globals->get_fg_root());
671 loadpath.append(path);
672 fullpath = loadpath.str();
678 readProperties(fullpath, props);
679 } catch (const sg_exception &e) {
680 guiErrorMessage("Error reading properties: ", e);
688 ////////////////////////////////////////////////////////////////////////
689 // Property convenience functions.
690 ////////////////////////////////////////////////////////////////////////
693 fgGetNode (const char * path, bool create)
695 return globals->get_props()->getNode(path, create);
699 fgGetNode (const char * path, int index, bool create)
701 return globals->get_props()->getNode(path, index, create);
705 fgHasNode (const char * path)
707 return (fgGetNode(path, false) != 0);
711 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
713 fgGetNode(path, true)->addChangeListener(listener);
717 fgAddChangeListener (SGPropertyChangeListener * listener,
718 const char * path, int index)
720 fgGetNode(path, index, true)->addChangeListener(listener);
724 fgGetBool (const char * name, bool defaultValue)
726 return globals->get_props()->getBoolValue(name, defaultValue);
730 fgGetInt (const char * name, int defaultValue)
732 return globals->get_props()->getIntValue(name, defaultValue);
736 fgGetLong (const char * name, long defaultValue)
738 return globals->get_props()->getLongValue(name, defaultValue);
742 fgGetFloat (const char * name, float defaultValue)
744 return globals->get_props()->getFloatValue(name, defaultValue);
748 fgGetDouble (const char * name, double defaultValue)
750 return globals->get_props()->getDoubleValue(name, defaultValue);
754 fgGetString (const char * name, const char * defaultValue)
756 return globals->get_props()->getStringValue(name, defaultValue);
760 fgSetBool (const char * name, bool val)
762 return globals->get_props()->setBoolValue(name, val);
766 fgSetInt (const char * name, int val)
768 return globals->get_props()->setIntValue(name, val);
772 fgSetLong (const char * name, long val)
774 return globals->get_props()->setLongValue(name, val);
778 fgSetFloat (const char * name, float val)
780 return globals->get_props()->setFloatValue(name, val);
784 fgSetDouble (const char * name, double val)
786 return globals->get_props()->setDoubleValue(name, val);
790 fgSetString (const char * name, const char * val)
792 return globals->get_props()->setStringValue(name, val);
796 fgSetArchivable (const char * name, bool state)
798 SGPropertyNode * node = globals->get_props()->getNode(name);
800 SG_LOG(SG_GENERAL, SG_ALERT,
801 "Attempt to set archive flag for non-existant property "
804 node->setAttribute(SGPropertyNode::ARCHIVE, state);
808 fgSetReadable (const char * name, bool state)
810 SGPropertyNode * node = globals->get_props()->getNode(name);
812 SG_LOG(SG_GENERAL, SG_ALERT,
813 "Attempt to set read flag for non-existant property "
816 node->setAttribute(SGPropertyNode::READ, state);
820 fgSetWritable (const char * name, bool state)
822 SGPropertyNode * node = globals->get_props()->getNode(name);
824 SG_LOG(SG_GENERAL, SG_ALERT,
825 "Attempt to set write flag for non-existant property "
828 node->setAttribute(SGPropertyNode::WRITE, state);
832 fgUntie (const char * name)
834 if (!globals->get_props()->untie(name))
835 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
839 // end of fg_props.cxx