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 <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_INSTR, "instrumentation"),
86 LogClassMapping(SG_SYSTEMS, "systems"),
87 LogClassMapping(SG_UNDEFD, "")
92 * Get the logging classes.
97 sgDebugClass classes = logbuf::get_log_classes();
98 static string result = ""; // FIXME
99 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
100 if ((classes&log_class_mappings[i].c) > 0) {
103 result += log_class_mappings[i].name;
106 return result.c_str();
111 addLoggingClass (const string &name)
113 sgDebugClass classes = logbuf::get_log_classes();
114 for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
115 if (name == log_class_mappings[i].name) {
116 logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
120 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging class: " << name);
125 * Set the logging classes.
128 setLoggingClasses (const char * c)
131 logbuf::set_log_classes(SG_NONE);
133 if (classes == "none") {
134 SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
138 if (classes.empty() || classes == "all") { // default
139 logbuf::set_log_classes(SG_ALL);
140 SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
141 << getLoggingClasses());
145 string rest = classes;
147 int sep = rest.find('|');
149 name = rest.substr(0, sep);
150 rest = rest.substr(sep+1);
151 addLoggingClass(name);
152 sep = rest.find('|');
154 addLoggingClass(rest);
155 SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
156 << getLoggingClasses());
161 * Get the logging priority.
164 getLoggingPriority ()
166 switch (logbuf::get_log_priority()) {
178 SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
179 << logbuf::get_log_priority());
186 * Set the logging priority.
189 setLoggingPriority (const char * p)
194 if (priority == "bulk") {
195 logbuf::set_log_priority(SG_BULK);
196 } else if (priority == "debug") {
197 logbuf::set_log_priority(SG_DEBUG);
198 } else if (priority.empty() || priority == "info") { // default
199 logbuf::set_log_priority(SG_INFO);
200 } else if (priority == "warn") {
201 logbuf::set_log_priority(SG_WARN);
202 } else if (priority == "alert") {
203 logbuf::set_log_priority(SG_ALERT);
205 SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
207 SG_LOG(SG_GENERAL, SG_DEBUG, "Logging priority is " << getLoggingPriority());
212 * Return the current frozen state.
222 * Set the current frozen state.
229 // Stop sound on a pause
230 SGSoundMgr *s = globals->get_soundmgr();
242 * Return the current aircraft directory (UIUC) as a string.
247 return aircraft_dir.c_str();
252 * Set the current aircraft directory (UIUC).
255 setAircraftDir (const char * dir)
262 * Return the number of milliseconds elapsed since simulation started.
265 getElapsedTime_sec ()
267 return globals->get_sim_time_sec();
272 * Return the current Zulu time.
277 static char buf[64]; // FIXME
278 struct tm * t = globals->get_time_params()->getGmt();
279 sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
280 t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
281 t->tm_hour, t->tm_min, t->tm_sec);
287 * Set the current Zulu time.
290 setDateString (const char * date_string)
292 static const SGPropertyNode *cur_time_override
293 = fgGetNode("/sim/time/cur-time-override", true);
295 SGTime * st = globals->get_time_params();
296 struct tm * current_time = st->getGmt();
299 // Scan for basic ISO format
300 // YYYY-MM-DDTHH:MM:SS
301 int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
302 &(new_time.tm_year), &(new_time.tm_mon),
303 &(new_time.tm_mday), &(new_time.tm_hour),
304 &(new_time.tm_min), &(new_time.tm_sec));
306 // Be pretty picky about this, so
307 // that strange things don't happen
308 // if the save file has been edited
311 SG_LOG(SG_INPUT, SG_WARN, "Date/time string " << date_string
312 << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
316 // OK, it looks like we got six
317 // values, one way or another.
318 new_time.tm_year -= 1900;
319 new_time.tm_mon -= 1;
321 // Now, tell flight gear to use
322 // the new time. This was far
323 // too difficult, by the way.
325 mktime(&new_time) - mktime(current_time) + globals->get_warp();
326 double lon = current_aircraft.fdm_state->get_Longitude();
327 double lat = current_aircraft.fdm_state->get_Latitude();
328 globals->set_warp(warp);
329 st->update(lon, lat, cur_time_override->getLongValue(), warp);
333 * Return the GMT as a string.
338 static char buf[16]; // FIXME
339 struct tm *t = globals->get_time_params()->getGmt();
340 sprintf(buf, " %.2d:%.2d:%.2d",
341 t->tm_hour, t->tm_min, t->tm_sec);
342 // cout << t << " " << buf << endl;
347 * Return the magnetic variation
352 return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
357 * Return the magnetic dip
362 return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
367 * Return the current heading in degrees.
373 magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
374 if (magheading < 0) magheading += 360;
381 return globals->get_warp();
387 globals->set_warp(warp);
393 return globals->get_warp_delta();
397 setWarpDelta (long delta)
399 globals->set_warp_delta(delta);
409 setWindingCCW (bool state)
413 glFrontFace ( GL_CCW );
415 glFrontFace ( GL_CW );
421 #if defined(FX) && !defined(WIN32)
422 return globals->get_fullscreen();
429 setFullScreen (bool state)
431 #if defined(FX) && !defined(WIN32)
432 globals->set_fullscreen(state);
433 # if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
434 XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
442 return fdm_data_logging;
446 setFDMDataLogging (bool state)
448 // kludge; no getter or setter available
449 if (state != fdm_data_logging) {
450 fgToggleFDMdataLogging();
451 fdm_data_logging = state;
456 ////////////////////////////////////////////////////////////////////////
457 // Tie the properties.
458 ////////////////////////////////////////////////////////////////////////
460 FGProperties::FGProperties ()
464 FGProperties::~FGProperties ()
469 FGProperties::init ()
474 FGProperties::bind ()
477 fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
478 fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
479 fgTie("/sim/freeze/master", getFreeze, setFreeze);
480 fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
482 fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
483 fgTie("/sim/time/gmt", getDateString, setDateString);
484 fgSetArchivable("/sim/time/gmt");
485 fgTie("/sim/time/gmt-string", getGMTString);
488 fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
490 fgTie("/environment/magnetic-variation-deg", getMagVar);
491 fgTie("/environment/magnetic-dip-deg", getMagDip);
493 fgTie("/sim/time/warp", getWarp, setWarp, false);
494 fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
496 // Misc. Temporary junk.
497 fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
498 fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
499 fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
503 FGProperties::unbind ()
506 fgUntie("/sim/logging/priority");
507 fgUntie("/sim/logging/classes");
508 fgUntie("/sim/freeze/master");
509 fgUntie("/sim/aircraft-dir");
511 fgUntie("/sim/time/elapsed-sec");
512 fgUntie("/sim/time/gmt");
513 fgUntie("/sim/time/gmt-string");
516 fgUntie("/orientation/heading-magnetic-deg");
519 fgUntie("/environment/magnetic-variation-deg");
520 fgUntie("/environment/magnetic-dip-deg");
522 fgUntie("/sim/time/warp");
523 fgUntie("/sim/time/warp-delta");
525 // Misc. Temporary junk.
526 fgUntie("/sim/temp/winding-ccw");
527 fgUntie("/sim/temp/full-screen");
528 fgUntie("/sim/temp/fdm-data-logging");
532 FGProperties::update (double dt)
535 struct tm * t = globals->get_time_params()->getGmt();
537 fgSetInt("/sim/time/utc/year", t->tm_year + 1900);
538 fgSetInt("/sim/time/utc/month", t->tm_mon + 1);
539 fgSetInt("/sim/time/utc/day", t->tm_mday);
540 fgSetInt("/sim/time/utc/hour", t->tm_hour);
541 fgSetInt("/sim/time/utc/minute", t->tm_min);
542 fgSetInt("/sim/time/utc/second", t->tm_sec);
544 fgSetDouble("/sim/time/utc/day-seconds",
552 ////////////////////////////////////////////////////////////////////////
554 ////////////////////////////////////////////////////////////////////////
558 * Save the current state of the simulator to a stream.
561 fgSaveFlight (ostream &output, bool write_all)
564 fgSetBool("/sim/presets/onground", false);
565 fgSetArchivable("/sim/presets/onground");
566 fgSetBool("/sim/presets/trim", false);
567 fgSetArchivable("/sim/presets/trim");
568 fgSetString("/sim/presets/speed-set", "UVW");
569 fgSetArchivable("/sim/presets/speed-set");
572 writeProperties(output, globals->get_props(), write_all);
573 } catch (const sg_exception &e) {
574 guiErrorMessage("Error saving flight: ", e);
582 * Restore the current state of the simulator from a stream.
585 fgLoadFlight (istream &input)
587 SGPropertyNode props;
589 readProperties(input, &props);
590 } catch (const sg_exception &e) {
591 guiErrorMessage("Error reading saved flight: ", e);
595 fgSetBool("/sim/presets/onground", false);
596 fgSetBool("/sim/presets/trim", false);
597 fgSetString("/sim/presets/speed-set", "UVW");
599 copyProperties(&props, globals->get_props());
600 // When loading a flight, make it the
601 // new initial state.
602 globals->saveInitialState();
608 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
612 SGPath loadpath(globals->get_fg_root());
613 loadpath.append(path);
614 fullpath = loadpath.str();
620 readProperties(fullpath, props);
621 } catch (const sg_exception &e) {
622 guiErrorMessage("Error reading properties: ", e);
630 ////////////////////////////////////////////////////////////////////////
631 // Property convenience functions.
632 ////////////////////////////////////////////////////////////////////////
635 fgGetNode (const char * path, bool create)
637 return globals->get_props()->getNode(path, create);
641 fgGetNode (const char * path, int index, bool create)
643 return globals->get_props()->getNode(path, index, create);
647 fgHasNode (const char * path)
649 return (fgGetNode(path, false) != 0);
653 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
655 fgGetNode(path, true)->addChangeListener(listener);
659 fgAddChangeListener (SGPropertyChangeListener * listener,
660 const char * path, int index)
662 fgGetNode(path, index, true)->addChangeListener(listener);
666 fgGetBool (const char * name, bool defaultValue)
668 return globals->get_props()->getBoolValue(name, defaultValue);
672 fgGetInt (const char * name, int defaultValue)
674 return globals->get_props()->getIntValue(name, defaultValue);
678 fgGetLong (const char * name, long defaultValue)
680 return globals->get_props()->getLongValue(name, defaultValue);
684 fgGetFloat (const char * name, float defaultValue)
686 return globals->get_props()->getFloatValue(name, defaultValue);
690 fgGetDouble (const char * name, double defaultValue)
692 return globals->get_props()->getDoubleValue(name, defaultValue);
696 fgGetString (const char * name, const char * defaultValue)
698 return globals->get_props()->getStringValue(name, defaultValue);
702 fgSetBool (const char * name, bool val)
704 return globals->get_props()->setBoolValue(name, val);
708 fgSetInt (const char * name, int val)
710 return globals->get_props()->setIntValue(name, val);
714 fgSetLong (const char * name, long val)
716 return globals->get_props()->setLongValue(name, val);
720 fgSetFloat (const char * name, float val)
722 return globals->get_props()->setFloatValue(name, val);
726 fgSetDouble (const char * name, double val)
728 return globals->get_props()->setDoubleValue(name, val);
732 fgSetString (const char * name, const char * val)
734 return globals->get_props()->setStringValue(name, val);
738 fgSetArchivable (const char * name, bool state)
740 SGPropertyNode * node = globals->get_props()->getNode(name);
742 SG_LOG(SG_GENERAL, SG_DEBUG,
743 "Attempt to set archive flag for non-existant property "
746 node->setAttribute(SGPropertyNode::ARCHIVE, state);
750 fgSetReadable (const char * name, bool state)
752 SGPropertyNode * node = globals->get_props()->getNode(name);
754 SG_LOG(SG_GENERAL, SG_DEBUG,
755 "Attempt to set read flag for non-existant property "
758 node->setAttribute(SGPropertyNode::READ, state);
762 fgSetWritable (const char * name, bool state)
764 SGPropertyNode * node = globals->get_props()->getNode(name);
766 SG_LOG(SG_GENERAL, SG_DEBUG,
767 "Attempt to set write flag for non-existant property "
770 node->setAttribute(SGPropertyNode::WRITE, state);
774 fgUntie (const char * name)
776 if (!globals->get_props()->untie(name))
777 SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
781 // end of fg_props.cxx