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>
40 #include <Environment/environment.hxx>
44 #include "globals.hxx"
45 #include "fg_props.hxx"
47 SG_USING_STD(istream);
48 SG_USING_STD(ostream);
50 static bool winding_ccw = true; // FIXME: temporary
52 static bool fdm_data_logging = false; // FIXME: temporary
54 static bool frozen = false; // FIXME: temporary
58 ////////////////////////////////////////////////////////////////////////
59 // Default property bindings (not yet handled by any module).
60 ////////////////////////////////////////////////////////////////////////
62 struct LogClassMapping {
65 LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
68 LogClassMapping log_class_mappings [] = {
69 LogClassMapping(SG_NONE, "none"),
70 LogClassMapping(SG_TERRAIN, "terrain"),
71 LogClassMapping(SG_ASTRO, "astro"),
72 LogClassMapping(SG_FLIGHT, "flight"),
73 LogClassMapping(SG_INPUT, "input"),
74 LogClassMapping(SG_GL, "gl"),
75 LogClassMapping(SG_VIEW, "view"),
76 LogClassMapping(SG_COCKPIT, "cockpit"),
77 LogClassMapping(SG_GENERAL, "general"),
78 LogClassMapping(SG_MATH, "math"),
79 LogClassMapping(SG_EVENT, "event"),
80 LogClassMapping(SG_AIRCRAFT, "aircraft"),
81 LogClassMapping(SG_AUTOPILOT, "autopilot"),
82 LogClassMapping(SG_IO, "io"),
83 LogClassMapping(SG_CLIPPER, "clipper"),
84 LogClassMapping(SG_NETWORK, "network"),
85 LogClassMapping(SG_UNDEFD, "")
90 * Get the logging classes.
95 sgDebugClass classes = logbuf::get_log_classes();
96 static string result = ""; // FIXME
97 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
98 if ((classes&log_class_mappings[i].c) > 0) {
101 result += log_class_mappings[i].name;
104 return result.c_str();
109 addLoggingClass (const string &name)
111 sgDebugClass classes = logbuf::get_log_classes();
112 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
113 if (name == log_class_mappings[i].name) {
114 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
118 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
123 * Set the logging classes.
126 setLoggingClasses (const char * c)
129 logbuf::set_log_classes(SG_NONE);
131 if (classes == "none") {
132 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
136 if (classes.empty() || classes == "all") { // default
137 logbuf::set_log_classes(SG_ALL);
138 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
139 << getLoggingClasses());
143 string rest = classes;
145 int sep = rest.find('|');
147 name = rest.substr(0, sep);
148 rest = rest.substr(sep+1);
149 addLoggingClass(name);
150 sep = rest.find('|');
152 addLoggingClass(rest);
153 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
154 << getLoggingClasses());
159 * Get the logging priority.
162 getLoggingPriority ()
164 switch (logbuf::get_log_priority()) {
176 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
177 << logbuf::get_log_priority());
184 * Set the logging priority.
187 setLoggingPriority (const char * p)
192 if (priority == "bulk") {
193 logbuf::set_log_priority(SG_BULK);
194 } else if (priority == "debug") {
195 logbuf::set_log_priority(SG_DEBUG);
196 } else if (priority.empty() || priority == "info") { // default
197 logbuf::set_log_priority(SG_INFO);
198 } else if (priority == "warn") {
199 logbuf::set_log_priority(SG_WARN);
200 } else if (priority == "alert") {
201 logbuf::set_log_priority(SG_ALERT);
203 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
205 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
210 * Return the current frozen state.
220 * Set the current frozen state.
227 // Stop sound on a pause
228 SGSoundMgr *s = globals->get_soundmgr();
240 * Return the current aircraft directory (UIUC) as a string.
245 return aircraft_dir.c_str();
250 * Set the current aircraft directory (UIUC).
253 setAircraftDir (const char * dir)
260 * Return the number of milliseconds elapsed since simulation started.
263 getElapsedTime_sec ()
265 return globals->get_sim_time_sec();
270 * Return the current Zulu time.
275 static char buf[64]; // FIXME
276 struct tm * t = globals->get_time_params()->getGmt();
277 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
278 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
279 t->tm_hour, t->tm_min, t->tm_sec);
285 * Set the current Zulu time.
288 setDateString (const char * date_string)
290 static const SGPropertyNode *cur_time_override
291 = fgGetNode("/sim/time/cur-time-override", true);
293 SGTime * st = globals->get_time_params();
294 struct tm * current_time = st->getGmt();
297 // Scan for basic ISO format
298 // YYYY-MM-DDTHH:MM:SS
299 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
300 &(new_time.tm_year), &(new_time.tm_mon),
301 &(new_time.tm_mday), &(new_time.tm_hour),
302 &(new_time.tm_min), &(new_time.tm_sec));
304 // Be pretty picky about this, so
305 // that strange things don't happen
306 // if the save file has been edited
309 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
310 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
314 // OK, it looks like we got six
315 // values, one way or another.
316 new_time.tm_year -= 1900;
317 new_time.tm_mon -= 1;
319 // Now, tell flight gear to use
320 // the new time. This was far
321 // too difficult, by the way.
323 mktime(&new_time) - mktime(current_time) + globals->get_warp();
324 double lon = current_aircraft.fdm_state->get_Longitude();
325 double lat = current_aircraft.fdm_state->get_Latitude();
326 globals->set_warp(warp);
327 st->update(lon, lat, cur_time_override->getLongValue(), warp);
331 * Return the GMT as a string.
336 static char buf[16]; // FIXME
337 struct tm *t = globals->get_time_params()->getGmt();
338 sprintf(buf, " %.2d:%.2d:%.2d",
339 t->tm_hour, t->tm_min, t->tm_sec);
340 // cout << t << " " << buf << endl;
345 * Return the magnetic variation
350 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
355 * Return the magnetic dip
360 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
365 * Return the current heading in degrees.
371 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
372 if (magheading < 0) magheading += 360;
379 return globals->get_warp();
385 globals->set_warp(warp);
391 return globals->get_warp_delta();
395 setWarpDelta (long delta)
397 globals->set_warp_delta(delta);
407 setWindingCCW (bool state)
411 glFrontFace ( GL_CCW );
413 glFrontFace ( GL_CW );
419 #if defined(FX) && !defined(WIN32)
420 return globals->get_fullscreen();
427 setFullScreen (bool state)
429 #if defined(FX) && !defined(WIN32)
430 globals->set_fullscreen(state);
431 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
432 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
440 return fdm_data_logging;
444 setFDMDataLogging (bool state)
446 // kludge; no getter or setter available
447 if (state != fdm_data_logging) {
448 fgToggleFDMdataLogging();
449 fdm_data_logging = state;
454 ////////////////////////////////////////////////////////////////////////
455 // Tie the properties.
456 ////////////////////////////////////////////////////////////////////////
458 FGProperties::FGProperties ()
462 FGProperties::~FGProperties ()
467 FGProperties::init ()
472 FGProperties::bind ()
475 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
476 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
477 fgTie("/sim/freeze/master", getFreeze, setFreeze);
478 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
480 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
481 fgTie("/sim/time/gmt", getDateString, setDateString);
482 fgSetArchivable("/sim/time/gmt");
483 fgTie("/sim/time/gmt-string", getGMTString);
486 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
488 fgTie("/environment/magnetic-variation-deg", getMagVar);
489 fgTie("/environment/magnetic-dip-deg", getMagDip);
491 fgTie("/sim/time/warp", getWarp, setWarp, false);
492 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
494 // Misc. Temporary junk.
495 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
496 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
497 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
501 FGProperties::unbind ()
504 fgUntie("/sim/logging/priority");
505 fgUntie("/sim/logging/classes");
506 fgUntie("/sim/freeze/master");
507 fgUntie("/sim/aircraft-dir");
509 fgUntie("/sim/time/elapsed-sec");
510 fgUntie("/sim/time/gmt");
511 fgUntie("/sim/time/gmt-string");
514 fgUntie("/orientation/heading-magnetic-deg");
517 fgUntie("/environment/magnetic-variation-deg");
518 fgUntie("/environment/magnetic-dip-deg");
520 fgUntie("/sim/time/warp");
521 fgUntie("/sim/time/warp-delta");
523 // Misc. Temporary junk.
524 fgUntie("/sim/temp/winding-ccw");
525 fgUntie("/sim/temp/full-screen");
526 fgUntie("/sim/temp/fdm-data-logging");
530 FGProperties::update (double dt)
533 struct tm * t = globals->get_time_params()->getGmt();
535 fgSetInt("/sim/time/utc/year", t->tm_year + 1900);
536 fgSetInt("/sim/time/utc/month", t->tm_mon + 1);
537 fgSetInt("/sim/time/utc/day", t->tm_mday);
538 fgSetInt("/sim/time/utc/hour", t->tm_hour);
539 fgSetInt("/sim/time/utc/minute", t->tm_min);
540 fgSetInt("/sim/time/utc/second", t->tm_sec);
542 fgSetDouble("/sim/time/utc/day-seconds",
550 ////////////////////////////////////////////////////////////////////////
552 ////////////////////////////////////////////////////////////////////////
556 * Save the current state of the simulator to a stream.
559 fgSaveFlight (ostream &output, bool write_all)
562 fgSetBool("/sim/presets/onground", false);
563 fgSetArchivable("/sim/presets/onground");
564 fgSetBool("/sim/presets/trim", false);
565 fgSetArchivable("/sim/presets/trim");
566 fgSetString("/sim/presets/speed-set", "UVW");
567 fgSetArchivable("/sim/presets/speed-set");
570 writeProperties(output, globals->get_props(), write_all);
571 } catch (const sg_exception &e) {
572 guiErrorMessage("Error saving flight: ", e);
580 * Restore the current state of the simulator from a stream.
583 fgLoadFlight (istream &input)
585 SGPropertyNode props;
587 readProperties(input, &props);
588 } catch (const sg_exception &e) {
589 guiErrorMessage("Error reading saved flight: ", e);
593 fgSetBool("/sim/presets/onground", false);
594 fgSetBool("/sim/presets/trim", false);
595 fgSetString("/sim/presets/speed-set", "UVW");
597 copyProperties(&props, globals->get_props());
598 // When loading a flight, make it the
599 // new initial state.
600 globals->saveInitialState();
606 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
610 SGPath loadpath(globals->get_fg_root());
611 loadpath.append(path);
612 fullpath = loadpath.str();
618 readProperties(fullpath, props);
619 } catch (const sg_exception &e) {
620 guiErrorMessage("Error reading properties: ", e);
628 ////////////////////////////////////////////////////////////////////////
629 // Property convenience functions.
630 ////////////////////////////////////////////////////////////////////////
633 fgGetNode (const char * path, bool create)
635 return globals->get_props()->getNode(path, create);
639 fgGetNode (const char * path, int index, bool create)
641 return globals->get_props()->getNode(path, index, create);
645 fgHasNode (const char * path)
647 return (fgGetNode(path, false) != 0);
651 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
653 fgGetNode(path, true)->addChangeListener(listener);
657 fgAddChangeListener (SGPropertyChangeListener * listener,
658 const char * path, int index)
660 fgGetNode(path, index, true)->addChangeListener(listener);
664 fgGetBool (const char * name, bool defaultValue)
666 return globals->get_props()->getBoolValue(name, defaultValue);
670 fgGetInt (const char * name, int defaultValue)
672 return globals->get_props()->getIntValue(name, defaultValue);
676 fgGetLong (const char * name, long defaultValue)
678 return globals->get_props()->getLongValue(name, defaultValue);
682 fgGetFloat (const char * name, float defaultValue)
684 return globals->get_props()->getFloatValue(name, defaultValue);
688 fgGetDouble (const char * name, double defaultValue)
690 return globals->get_props()->getDoubleValue(name, defaultValue);
694 fgGetString (const char * name, const char * defaultValue)
696 return globals->get_props()->getStringValue(name, defaultValue);
700 fgSetBool (const char * name, bool val)
702 return globals->get_props()->setBoolValue(name, val);
706 fgSetInt (const char * name, int val)
708 return globals->get_props()->setIntValue(name, val);
712 fgSetLong (const char * name, long val)
714 return globals->get_props()->setLongValue(name, val);
718 fgSetFloat (const char * name, float val)
720 return globals->get_props()->setFloatValue(name, val);
724 fgSetDouble (const char * name, double val)
726 return globals->get_props()->setDoubleValue(name, val);
730 fgSetString (const char * name, const char * val)
732 return globals->get_props()->setStringValue(name, val);
736 fgSetArchivable (const char * name, bool state)
738 SGPropertyNode * node = globals->get_props()->getNode(name);
740 SG_LOG(SG_GENERAL, SG_DEBUG,
741 "Attempt to set archive flag for non-existant property "
744 node->setAttribute(SGPropertyNode::ARCHIVE, state);
748 fgSetReadable (const char * name, bool state)
750 SGPropertyNode * node = globals->get_props()->getNode(name);
752 SG_LOG(SG_GENERAL, SG_DEBUG,
753 "Attempt to set read flag for non-existant property "
756 node->setAttribute(SGPropertyNode::READ, state);
760 fgSetWritable (const char * name, bool state)
762 SGPropertyNode * node = globals->get_props()->getNode(name);
764 SG_LOG(SG_GENERAL, SG_DEBUG,
765 "Attempt to set write flag for non-existant property "
768 node->setAttribute(SGPropertyNode::WRITE, state);
772 fgUntie (const char * name)
774 if (!globals->get_props()->untie(name))
775 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
779 // end of fg_props.cxx