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