]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
e21e5b59127c05ca913e9671e22ef9b6e7e2880d
[flightgear.git] / src / Main / globals.cxx
1 // globals.cxx -- Global state that needs to be shared among the sim modules
2 //
3 // Written by Curtis Olson, started July 2000.
4 //
5 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
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.
11 //
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.
16 //
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 Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <boost/foreach.hpp>
28 #include <algorithm>
29
30 #include <osgViewer/Viewer>
31 #include <osgDB/Registry>
32
33 #include <simgear/structure/commands.hxx>
34 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/misc/sg_dir.hxx>
36 #include <simgear/timing/sg_time.hxx>
37 #include <simgear/ephemeris/ephemeris.hxx>
38 #include <simgear/scene/material/matlib.hxx>
39 #include <simgear/structure/subsystem_mgr.hxx>
40 #include <simgear/structure/event_mgr.hxx>
41 #include <simgear/sound/soundmgr_openal.hxx>
42 #include <simgear/misc/ResourceManager.hxx>
43 #include <simgear/props/propertyObject.hxx>
44 #include <simgear/props/props_io.hxx>
45 #include <simgear/scene/model/modellib.hxx>
46
47 #include <Aircraft/controls.hxx>
48 #include <Airports/runways.hxx>
49 #include <ATCDCL/ATISmgr.hxx>
50 #include <Autopilot/route_mgr.hxx>
51 #include <GUI/FGFontCache.hxx>
52 #include <GUI/gui.h>
53 #include <MultiPlayer/multiplaymgr.hxx>
54 #include <Scenery/scenery.hxx>
55 #include <Scenery/tilemgr.hxx>
56 #include <Navaids/navlist.hxx>
57 #include <Viewer/renderer.hxx>
58 #include <Viewer/viewmgr.hxx>
59 #include <Sound/sample_queue.hxx>
60
61 #include "globals.hxx"
62 #include "locale.hxx"
63
64 #include "fg_props.hxx"
65 #include "fg_io.hxx"
66
67 class AircraftResourceProvider : public simgear::ResourceProvider
68 {
69 public:
70   AircraftResourceProvider() :
71     simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
72   {
73   }
74   
75   virtual SGPath resolve(const std::string& aResource, SGPath&) const
76   {
77     string_list pieces(sgPathBranchSplit(aResource));
78     if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
79       return SGPath(); // not an Aircraft path
80     }
81     
82   // test against the aircraft-dir property
83     const char* aircraftDir = fgGetString("/sim/aircraft-dir");
84     string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
85     if (!aircraftDirPieces.empty() && (aircraftDirPieces.back() == pieces[1])) {
86         // current aircraft-dir matches resource aircraft
87         SGPath r(aircraftDir);
88         for (unsigned int i=2; i<pieces.size(); ++i) {
89           r.append(pieces[i]);
90         }
91         
92         if (r.exists()) {
93           return r;
94         }
95     }
96   
97   // try each aircraft dir in turn
98     std::string res(aResource, 9); // resource path with 'Aircraft/' removed
99     const string_list& dirs(globals->get_aircraft_paths());
100     string_list::const_iterator it = dirs.begin();
101     for (; it != dirs.end(); ++it) {
102       SGPath p(*it, res);
103       if (p.exists()) {
104         return p;
105       }
106     } // of aircraft path iteration
107     
108     return SGPath(); // not found
109   }
110 };
111
112 class CurrentAircraftDirProvider : public simgear::ResourceProvider
113 {
114 public:
115   CurrentAircraftDirProvider() :
116     simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
117   {
118   }
119   
120   virtual SGPath resolve(const std::string& aResource, SGPath&) const
121   {
122     const char* aircraftDir = fgGetString("/sim/aircraft-dir");
123     SGPath p(aircraftDir);
124     p.append(aResource);
125     return p.exists() ? p : SGPath();
126   }
127 };
128
129 ////////////////////////////////////////////////////////////////////////
130 // Implementation of FGGlobals.
131 ////////////////////////////////////////////////////////////////////////
132
133 // global global :-)
134 FGGlobals *globals = NULL;
135
136
137 // Constructor
138 FGGlobals::FGGlobals() :
139     initial_state( NULL ),
140     renderer( new FGRenderer ),
141     subsystem_mgr( new SGSubsystemMgr ),
142     event_mgr( new SGEventMgr ),
143     sim_time_sec( 0.0 ),
144     fg_root( "" ),
145     fg_home( "" ),
146     time_params( NULL ),
147     ephem( NULL ),
148     route_mgr( NULL ),
149     ATIS_mgr( NULL ),
150     controls( NULL ),
151     viewmgr( NULL ),
152     commands( SGCommandMgr::instance() ),
153     channel_options_list( NULL ),
154     initial_waypoints( NULL ),
155     fontcache ( new FGFontCache ),
156     channellist( NULL ),
157     haveUserSettings(false),
158     _chatter_queue(NULL)
159 {
160     SGPropertyNode* root = new SGPropertyNode;
161     props = SGPropertyNode_ptr(root);
162     locale = new FGLocale(props);
163     
164     simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider);
165     simgear::ResourceManager::instance()->addProvider(new CurrentAircraftDirProvider);
166     initProperties();
167 }
168
169 void FGGlobals::initProperties()
170 {
171     simgear::PropertyObjectBase::setDefaultRoot(props);
172     
173     positionLon = props->getNode("position/longitude-deg", true);
174     positionLat = props->getNode("position/latitude-deg", true);
175     positionAlt = props->getNode("position/altitude-ft", true);
176     
177     viewLon = props->getNode("sim/current-view/viewer-lon-deg", true);
178     viewLat = props->getNode("sim/current-view/viewer-lat-deg", true);
179     viewAlt = props->getNode("sim/current-view/viewer-elev-ft", true);
180     
181     orientPitch = props->getNode("orientation/pitch-deg", true);
182     orientHeading = props->getNode("orientation/heading-deg", true);
183     orientRoll = props->getNode("orientation/roll-deg", true);
184
185 }
186
187 // Destructor
188 FGGlobals::~FGGlobals() 
189 {
190     // save user settings (unless already saved)
191     saveUserSettings();
192
193     // The AIModels manager performs a number of actions upon
194     // Shutdown that implicitly assume that other subsystems
195     // are still operational (Due to the dynamic allocation and
196     // deallocation of AIModel objects. To ensure we can safely
197     // shut down all subsystems, make sure we take down the 
198     // AIModels system first.
199     SGSubsystemRef ai = subsystem_mgr->get_subsystem("ai-model");
200     if (ai) {
201         subsystem_mgr->remove("ai-model");
202         ai->unbind();
203         ai.clear(); // ensure AI is deleted now, not at end of this method
204     }
205     
206     subsystem_mgr->shutdown();
207     subsystem_mgr->unbind();    
208
209     subsystem_mgr->remove("aircraft-model");
210     subsystem_mgr->remove("tile-manager");
211     subsystem_mgr->remove("model-manager");
212     _tile_mgr.clear();
213
214     osg::ref_ptr<osgViewer::Viewer> vw(renderer->getViewer());
215     if (vw) {
216         // https://code.google.com/p/flightgear-bugs/issues/detail?id=1291
217         // explicitly stop trheading before we delete the renderer or
218         // viewMgr (which ultimately holds refs to the CameraGroup, and
219         // GraphicsContext)
220         vw->stopThreading();
221     }
222     
223     // don't cancel the pager until after shutdown, since AIModels (and
224     // potentially others) can queue delete requests on the pager.
225     if (vw && vw->getDatabasePager()) {
226         vw->getDatabasePager()->cancel();
227         vw->getDatabasePager()->clear();
228     }
229     
230     osgDB::Registry::instance()->clearObjectCache();
231     
232     // renderer touches subsystems during its destruction
233     set_renderer(NULL);
234     _scenery.clear();
235     _chatter_queue.clear();
236     
237     delete subsystem_mgr;
238     subsystem_mgr = NULL; // important so ::get_subsystem returns NULL
239     vw = 0; // don't delete the viewer until now
240
241     delete time_params;
242     set_matlib(NULL);
243     delete route_mgr;
244     delete ATIS_mgr;
245     delete channel_options_list;
246     delete initial_waypoints;
247     delete fontcache;
248     delete channellist;
249
250     simgear::PropertyObjectBase::setDefaultRoot(NULL);
251     simgear::SGModelLib::resetPropertyRoot();
252     
253     delete locale;
254     locale = NULL;
255     
256     cleanupListeners();
257     
258     props.clear();
259     
260     delete commands;
261 }
262
263 // set the fg_root path
264 void FGGlobals::set_fg_root (const std::string &root) {
265     SGPath tmp(root);
266     fg_root = tmp.realpath();
267
268     // append /data to root if it exists
269     tmp.append( "data" );
270     tmp.append( "version" );
271     if ( tmp.exists() ) {
272         fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
273         fg_root += "/data";
274         fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
275         SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
276                 << fg_root << "'\n***\n***");
277     }
278
279     // remove /sim/fg-root before writing to prevent hijacking
280     SGPropertyNode *n = fgGetNode("/sim", true);
281     n->removeChild("fg-root", 0, false);
282     n = n->getChild("fg-root", 0, true);
283     n->setStringValue(fg_root.c_str());
284     n->setAttribute(SGPropertyNode::WRITE, false);
285     
286     simgear::ResourceManager::instance()->addBasePath(fg_root, 
287       simgear::ResourceManager::PRIORITY_DEFAULT);
288 }
289
290 // set the fg_home path
291 void FGGlobals::set_fg_home (const std::string &home) {
292     SGPath tmp(home);
293     fg_home = tmp.realpath();
294 }
295
296 PathList FGGlobals::get_data_paths() const
297 {
298     PathList r(additional_data_paths);
299     r.push_back(SGPath(fg_root));
300     return r;
301 }
302
303 PathList FGGlobals::get_data_paths(const std::string& suffix) const
304 {
305     PathList r;
306     BOOST_FOREACH(SGPath p, get_data_paths()) {
307         p.append(suffix);
308         if (p.exists()) {
309             r.push_back(p);
310         }
311     }
312
313     return r;
314 }
315
316 void FGGlobals::append_data_path(const SGPath& path)
317 {
318     if (!path.exists()) {
319         SG_LOG(SG_GENERAL, SG_WARN, "adding non-existant data path:" << path);
320     }
321     
322     additional_data_paths.push_back(path);
323 }
324
325 SGPath FGGlobals::find_data_dir(const std::string& pathSuffix) const
326 {
327     BOOST_FOREACH(SGPath p, additional_data_paths) {
328         p.append(pathSuffix);
329         if (p.exists()) {
330             return p;
331         }
332     }
333     
334     SGPath rootPath(fg_root);
335     rootPath.append(pathSuffix);
336     if (rootPath.exists()) {
337         return rootPath;
338     }
339     
340     SG_LOG(SG_GENERAL, SG_WARN, "dir not found in any data path:" << pathSuffix);
341     return SGPath();
342 }
343
344 void FGGlobals::append_fg_scenery (const std::string &paths)
345 {
346 //    fg_scenery.clear();
347     SGPropertyNode* sim = fgGetNode("/sim", true);
348
349   // find first unused fg-scenery property in /sim
350     int propIndex = 0;
351     while (sim->getChild("fg-scenery", propIndex) != NULL) {
352       ++propIndex; 
353     }
354   
355     BOOST_FOREACH(const SGPath& path, sgPathSplit( paths )) {
356         SGPath abspath(path.realpath());
357         if (!abspath.exists()) {
358           SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << abspath.str());
359           continue;
360         }
361
362       // check for duplicates
363       string_list::const_iterator ex = std::find(fg_scenery.begin(), fg_scenery.end(), abspath.str());
364       if (ex != fg_scenery.end()) {
365         SG_LOG(SG_GENERAL, SG_INFO, "skipping duplicate add of scenery path:" << abspath.str());
366         continue;
367       }
368       
369         simgear::Dir dir(abspath);
370         SGPath terrainDir(dir.file("Terrain"));
371         SGPath objectsDir(dir.file("Objects"));
372         
373       // this code used to add *either* the base dir, OR add the 
374       // Terrain and Objects subdirs, but the conditional logic was commented
375       // out, such that all three dirs are added. Unfortunately there's
376       // no information as to why the change was made.
377         fg_scenery.push_back(abspath.str());
378         
379         if (terrainDir.exists()) {
380           fg_scenery.push_back(terrainDir.str());
381         }
382         
383         if (objectsDir.exists()) {
384           fg_scenery.push_back(objectsDir.str());
385         }
386         
387         // insert a marker for FGTileEntry::load(), so that
388         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
389         // "B/Terrain", "B/Objects", ""]
390         fg_scenery.push_back("");
391         
392       // make scenery dirs available to Nasal
393         SGPropertyNode* n = sim->getChild("fg-scenery", propIndex++, true);
394         n->setStringValue(abspath.str());
395         n->setAttribute(SGPropertyNode::WRITE, false);
396     } // of path list iteration
397 }
398
399 void FGGlobals::append_aircraft_path(const std::string& path)
400 {
401   SGPath dirPath(path);
402   if (!dirPath.exists()) {
403     SG_LOG(SG_GENERAL, SG_ALERT, "aircraft path not found:" << path);
404     return;
405   }
406     
407   SGPath acSubdir(dirPath);
408   acSubdir.append("Aircraft");
409   if (!acSubdir.exists()) {
410       if (dirPath.file() == "Aircraft") {
411           dirPath = dirPath.dir();
412           SG_LOG(SG_GENERAL, SG_WARN, "Specified an aircraft-dir path ending in 'Aircraft':" << path
413                  << ", will instead use parent directory:" << dirPath);
414       } else {
415           SG_LOG(SG_GENERAL, SG_ALERT, "Aircraft-dir path '" << path <<
416              "' does not contain an 'Aircraft' subdirectory, cross-aircraft paths will not resolve correctly.");
417       }
418   }
419     
420   std::string abspath = dirPath.realpath();
421   
422   unsigned int index = fg_aircraft_dirs.size();  
423   fg_aircraft_dirs.push_back(abspath);
424   
425 // make aircraft dirs available to Nasal
426   SGPropertyNode* sim = fgGetNode("/sim", true);
427   sim->removeChild("fg-aircraft", index, false);
428   SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
429   n->setStringValue(abspath);
430   n->setAttribute(SGPropertyNode::WRITE, false);
431 }
432
433 void FGGlobals::append_aircraft_paths(const std::string& path)
434 {
435   string_list paths = sgPathSplit(path);
436   for (unsigned int p = 0; p<paths.size(); ++p) {
437     append_aircraft_path(paths[p]);
438   }
439 }
440
441 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
442 {
443   return simgear::ResourceManager::instance()->findPath(branch);
444 }
445
446 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
447 {
448   return simgear::ResourceManager::instance()->findPath(branch);
449 }
450
451 SGPath FGGlobals::resolve_resource_path(const std::string& branch) const
452 {
453   return simgear::ResourceManager::instance()
454     ->findPath(branch, SGPath(fgGetString("/sim/aircraft-dir")));
455 }
456
457 FGRenderer *
458 FGGlobals::get_renderer () const
459 {
460    return renderer;
461 }
462
463 void FGGlobals::set_renderer(FGRenderer *render)
464 {
465     if (render == renderer) {
466         return;
467     }
468     
469     delete renderer;
470     renderer = render;
471 }
472
473 SGSubsystemMgr *
474 FGGlobals::get_subsystem_mgr () const
475 {
476     return subsystem_mgr;
477 }
478
479 SGSubsystem *
480 FGGlobals::get_subsystem (const char * name)
481 {
482     if (!subsystem_mgr) {
483         return NULL;
484     }
485     
486     return subsystem_mgr->get_subsystem(name);
487 }
488
489 void
490 FGGlobals::add_subsystem (const char * name,
491                           SGSubsystem * subsystem,
492                           SGSubsystemMgr::GroupType type,
493                           double min_time_sec)
494 {
495     subsystem_mgr->add(name, subsystem, type, min_time_sec);
496 }
497
498 SGSoundMgr *
499 FGGlobals::get_soundmgr () const
500 {
501     if (subsystem_mgr)
502         return (SGSoundMgr*) subsystem_mgr->get_subsystem("sound");
503
504     return NULL;
505 }
506
507 SGEventMgr *
508 FGGlobals::get_event_mgr () const
509 {
510     return event_mgr;
511 }
512
513 SGGeod
514 FGGlobals::get_aircraft_position() const
515 {
516   return SGGeod::fromDegFt(positionLon->getDoubleValue(),
517                            positionLat->getDoubleValue(),
518                            positionAlt->getDoubleValue());
519 }
520
521 SGVec3d
522 FGGlobals::get_aircraft_position_cart() const
523 {
524     return SGVec3d::fromGeod(get_aircraft_position());
525 }
526
527 void FGGlobals::get_aircraft_orientation(double& heading, double& pitch, double& roll)
528 {
529   heading = orientHeading->getDoubleValue();
530   pitch = orientPitch->getDoubleValue();
531   roll = orientRoll->getDoubleValue();
532 }
533
534 SGGeod
535 FGGlobals::get_view_position() const
536 {
537   return SGGeod::fromDegFt(viewLon->getDoubleValue(),
538                            viewLat->getDoubleValue(),
539                            viewAlt->getDoubleValue());
540 }
541
542 SGVec3d
543 FGGlobals::get_view_position_cart() const
544 {
545   return SGVec3d::fromGeod(get_view_position());
546 }
547
548 static void treeDumpRefCounts(int depth, SGPropertyNode* nd)
549 {
550     for (int i=0; i<nd->nChildren(); ++i) {
551         SGPropertyNode* cp = nd->getChild(i);
552         if (SGReferenced::count(cp) > 1) {
553             SG_LOG(SG_GENERAL, SG_INFO, "\t" << cp->getPath() << " refcount:" << SGReferenced::count(cp));
554         }
555         
556         treeDumpRefCounts(depth + 1, cp);
557     }
558 }
559
560 static void treeClearAliases(SGPropertyNode* nd)
561 {
562     if (nd->isAlias()) {
563         nd->unalias();
564     }
565     
566     for (int i=0; i<nd->nChildren(); ++i) {
567         SGPropertyNode* cp = nd->getChild(i);
568         treeClearAliases(cp);
569     }
570 }
571
572 void
573 FGGlobals::resetPropertyRoot()
574 {
575     delete locale;
576     
577     cleanupListeners();
578     
579     // we don't strictly need to clear these (they will be reset when we
580     // initProperties again), but trying to reduce false-positives when dumping
581     // ref-counts.
582     positionLon.clear();
583     positionLat.clear();
584     positionAlt.clear();
585     viewLon.clear();
586     viewLat.clear();
587     viewAlt.clear();
588     orientPitch.clear();
589     orientHeading.clear();
590     orientRoll.clear();
591     
592     // clear aliases so ref-counts are accurate when dumped
593     treeClearAliases(props);
594     
595     SG_LOG(SG_GENERAL, SG_INFO, "root props refcount:" << props.getNumRefs());
596     treeDumpRefCounts(0, props);
597
598     //BaseStackSnapshot::dumpAll(std::cout);
599     
600     props = new SGPropertyNode;
601     initProperties();
602     locale = new FGLocale(props);
603     
604     // remove /sim/fg-root before writing to prevent hijacking
605     SGPropertyNode *n = props->getNode("/sim", true);
606     n->removeChild("fg-root", 0, false);
607     n = n->getChild("fg-root", 0, true);
608     n->setStringValue(fg_root.c_str());
609     n->setAttribute(SGPropertyNode::WRITE, false);
610 }
611
612 // Save the current state as the initial state.
613 void
614 FGGlobals::saveInitialState ()
615 {
616   initial_state = new SGPropertyNode();
617
618   // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
619   int checked  = SGPropertyNode::READ+SGPropertyNode::WRITE+
620                  SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
621   int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
622   if (!copyProperties(props, initial_state, expected, checked))
623     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
624     
625   // delete various properties from the initial state, since we want to
626   // preserve their values even if doing a restore
627   // => Properties should now use the PRESERVE flag to protect their values
628   // on sim-reset. Remove some specific properties for backward compatibility.
629   SGPropertyNode* sim = initial_state->getChild("sim");
630   SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
631   if (cameraGroupNode) {
632     cameraGroupNode->removeChild("camera");
633     cameraGroupNode->removeChild("gui");
634   }
635 }
636
637 static std::string autosaveName()
638 {
639     std::ostringstream os;
640     string_list versionParts = simgear::strutils::split(VERSION, ".");
641     if (versionParts.size() < 2) {
642         return "autosave.xml";
643     }
644     
645     os << "autosave_" << versionParts[0] << "_" << versionParts[1] << ".xml";
646     return os.str();
647 }
648
649 // Restore the saved initial state, if any
650 void
651 FGGlobals::restoreInitialState ()
652 {
653     if ( initial_state == 0 ) {
654         SG_LOG(SG_GENERAL, SG_ALERT,
655                "No initial state available to restore!!!");
656         return;
657     }
658     // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
659     int checked  = SGPropertyNode::READ+SGPropertyNode::WRITE+
660                    SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
661     int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
662     if ( copyProperties(initial_state, props, expected, checked)) {
663         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
664     } else {
665         SG_LOG( SG_GENERAL, SG_INFO,
666                 "Some errors restoring initial state (read-only props?)" );
667     }
668
669 }
670
671 // Load user settings from autosave.xml
672 void
673 FGGlobals::loadUserSettings(const SGPath& dataPath)
674 {
675     // remember that we have (tried) to load any existing autsave.xml
676     haveUserSettings = true;
677
678     SGPath autosaveFile = simgear::Dir(dataPath).file(autosaveName());
679     SGPropertyNode autosave;
680     if (autosaveFile.exists()) {
681       SG_LOG(SG_INPUT, SG_INFO, "Reading user settings from " << autosaveFile.str());
682       try {
683           readProperties(autosaveFile.str(), &autosave, SGPropertyNode::USERARCHIVE);
684       } catch (sg_exception& e) {
685           SG_LOG(SG_INPUT, SG_WARN, "failed to read user settings:" << e.getMessage()
686             << "(from " << e.getOrigin() << ")");
687       }
688     }
689     copyProperties(&autosave, globals->get_props());
690 }
691
692 // Save user settings in autosave.xml
693 void
694 FGGlobals::saveUserSettings()
695 {
696     // only save settings when we have (tried) to load the previous
697     // settings (otherwise user data was lost)
698     if (!haveUserSettings)
699         return;
700
701     if (fgGetBool("/sim/startup/save-on-exit")) {
702       // don't save settings more than once on shutdown
703       haveUserSettings = false;
704
705       SGPath autosaveFile(globals->get_fg_home());
706       autosaveFile.append(autosaveName());
707       autosaveFile.create_dir( 0700 );
708       SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << autosaveFile.str());
709       try {
710         writeProperties(autosaveFile.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
711       } catch (const sg_exception &e) {
712         guiErrorMessage("Error writing autosave:", e);
713       }
714       SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
715     }
716 }
717
718 FGViewer *
719 FGGlobals::get_current_view () const
720 {
721   return viewmgr->get_current_view();
722 }
723
724 long int FGGlobals::get_warp() const
725 {
726   return fgGetInt("/sim/time/warp");
727 }
728
729 void FGGlobals::set_warp( long int w )
730 {
731   fgSetInt("/sim/time/warp", w);
732 }
733
734 long int FGGlobals::get_warp_delta() const
735 {
736   return fgGetInt("/sim/time/warp-delta");
737 }
738
739 void FGGlobals::set_warp_delta( long int d )
740 {
741   fgSetInt("/sim/time/warp-delta", d);
742 }
743
744 FGScenery* FGGlobals::get_scenery () const
745 {
746     return _scenery.get();
747 }
748
749 void FGGlobals::set_scenery ( FGScenery *s )
750 {
751     _scenery = s;
752 }
753
754 FGTileMgr* FGGlobals::get_tile_mgr () const
755 {
756     return _tile_mgr.get();
757 }
758
759 void FGGlobals::set_tile_mgr ( FGTileMgr *t )
760 {
761     _tile_mgr = t;
762 }
763
764 void FGGlobals::set_matlib( SGMaterialLib *m )
765 {
766     matlib = m;
767 }
768
769 FGSampleQueue* FGGlobals::get_chatter_queue() const
770 {
771     return _chatter_queue;
772 }
773
774 void FGGlobals::set_chatter_queue(FGSampleQueue* queue)
775 {
776     _chatter_queue = queue;
777 }
778
779 void FGGlobals::addListenerToCleanup(SGPropertyChangeListener* l)
780 {
781     _listeners_to_cleanup.push_back(l);
782 }
783
784 void FGGlobals::cleanupListeners()
785 {
786     SGPropertyChangeListenerVec::iterator i = _listeners_to_cleanup.begin();
787     for (; i != _listeners_to_cleanup.end(); ++i) {
788         delete *i;
789     }
790     _listeners_to_cleanup.clear();
791 }
792
793 // end of globals.cxx