1 // globals.cxx -- Global state that needs to be shared among the sim modules
3 // Written by Curtis Olson, started July 2000.
5 // Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt
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 Foundation,
19 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 #include <simgear/structure/commands.hxx>
28 #include <simgear/misc/sg_path.hxx>
29 #include <simgear/misc/sg_dir.hxx>
30 #include <simgear/timing/sg_time.hxx>
31 #include <simgear/ephemeris/ephemeris.hxx>
32 #include <simgear/magvar/magvar.hxx>
33 #include <simgear/scene/material/matlib.hxx>
34 #include <simgear/structure/subsystem_mgr.hxx>
35 #include <simgear/structure/event_mgr.hxx>
36 #include <simgear/sound/soundmgr_openal.hxx>
37 #include <simgear/misc/ResourceManager.hxx>
39 #include <Aircraft/controls.hxx>
40 #include <Airports/runways.hxx>
41 #include <ATCDCL/ATCmgr.hxx>
42 #include <Autopilot/route_mgr.hxx>
43 #include <Cockpit/panel.hxx>
44 #include <GUI/new_gui.hxx>
45 #include <Model/acmodel.hxx>
46 #include <Model/modelmgr.hxx>
47 #include <MultiPlayer/multiplaymgr.hxx>
48 #include <Navaids/awynet.hxx>
49 #include <Scenery/scenery.hxx>
50 #include <Scenery/tilemgr.hxx>
51 #include <Navaids/navlist.hxx>
53 #include "globals.hxx"
54 #include "renderer.hxx"
55 #include "viewmgr.hxx"
57 #include "fg_props.hxx"
60 \fclass AircraftResourceProvider : public simgear::ResourceProvider
63 AircraftResourceProvider() :
64 simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
68 virtual SGPath resolve(const std::string& aResource, SGPath&) const
70 string_list pieces(sgPathBranchSplit(aResource));
71 if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
72 return SGPath(); // not an Aircraft path
75 // test against the aircraft-dir property
76 const char* aircraftDir = fgGetString("/sim/aircraft-dir");
77 string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
78 if (aircraftDirPieces.empty() || (aircraftDirPieces.back() != pieces[1])) {
79 return SGPath(); // current aircraft-dir does not match resource aircraft
82 SGPath r(aircraftDir);
83 for (unsigned int i=2; i<pieces.size(); ++i) {
88 SG_LOG(SG_IO, SG_INFO, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
92 // try each aircaft dir in turn
93 std::string res(aResource, 9); // resource path with 'Aircraft/' removed
94 const string_list& dirs(globals->get_aircraft_paths());
95 string_list::const_iterator it = dirs.begin();
96 for (; it != dirs.end(); ++it) {
99 SG_LOG(SG_IO, SG_INFO, "found path:" << aResource << " in aircraft dir: " << r.str());
102 } // of aircraft path iteration
104 return SGPath(); // not found
108 ////////////////////////////////////////////////////////////////////////
109 // Implementation of FGGlobals.
110 ////////////////////////////////////////////////////////////////////////
117 FGGlobals::FGGlobals() :
118 props( new SGPropertyNode ),
119 initial_state( NULL ),
121 renderer( new FGRenderer ),
122 subsystem_mgr( new SGSubsystemMgr ),
123 event_mgr( new SGEventMgr ),
124 soundmgr( new SGSoundMgr ),
134 current_panel( NULL ),
138 commands( SGCommandMgr::instance() ),
141 channel_options_list( NULL ),
142 initial_waypoints( NULL ),
145 fontcache ( new FGFontCache ),
154 multiplayer_mgr( NULL )
156 simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
161 FGGlobals::~FGGlobals()
164 // The AIModels manager performs a number of actions upon
165 // Shutdown that implicitly assume that other subsystems
166 // are still operational (Due to the dynamic allocation and
167 // deallocation of AIModel objects. To ensure we can safely
168 // shut down all subsystems, make sure we take down the
169 // AIModels system first.
170 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
171 // FGInput (FGInputEvent) and FGDialog calls get_subsystem() in their destructors,
172 // which is not safe since some subsystem are already deleted but can be referred.
173 // So these subsystems must be deleted prior to deleting subsystem_mgr unless
174 // ~SGSubsystemGroup and SGSubsystemMgr::get_subsystem are changed not to refer to
175 // deleted subsystems.
176 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("input");
177 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("gui");
178 subsystem_mgr->unbind();
179 delete subsystem_mgr;
185 delete current_panel;
194 delete channel_options_list;
195 delete initial_waypoints;
207 delete multiplayer_mgr;
214 // set the fg_root path
215 void FGGlobals::set_fg_root (const string &root) {
218 // append /data to root if it exists
219 SGPath tmp( fg_root );
220 tmp.append( "data" );
221 tmp.append( "version" );
222 if ( ulFileExists( tmp.c_str() ) ) {
223 fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
225 fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
226 SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
227 << fg_root << "'\n***\n***");
230 // remove /sim/fg-root before writing to prevent hijacking
231 SGPropertyNode *n = fgGetNode("/sim", true);
232 n->removeChild("fg-root", 0, false);
233 n = n->getChild("fg-root", 0, true);
234 n->setStringValue(fg_root.c_str());
235 n->setAttribute(SGPropertyNode::WRITE, false);
237 simgear::ResourceManager::instance()->addBasePath(fg_root,
238 simgear::ResourceManager::PRIORITY_DEFAULT);
241 void FGGlobals::set_fg_scenery (const string &scenery)
244 if (scenery.empty()) {
246 s.append( "Scenery" );
250 string_list path_list = sgPathSplit( s.str() );
253 for (unsigned i = 0; i < path_list.size(); i++) {
254 SGPath path(path_list[i]);
255 if (!path.exists()) {
256 SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << path.str());
260 simgear::Dir dir(path);
261 SGPath terrainDir(dir.file("Terrain"));
262 SGPath objectsDir(dir.file("Objects"));
264 // this code used to add *either* the base dir, OR add the
265 // Terrain and Objects subdirs, but the conditional logic was commented
266 // out, such that all three dirs are added. Unfortunately there's
267 // no information as to why the change was made.
268 fg_scenery.push_back(path.str());
270 if (terrainDir.exists()) {
271 fg_scenery.push_back(terrainDir.str());
274 if (objectsDir.exists()) {
275 fg_scenery.push_back(objectsDir.str());
278 // insert a marker for FGTileEntry::load(), so that
279 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
280 // "B/Terrain", "B/Objects", ""]
281 fg_scenery.push_back("");
282 } // of path list iteration
285 void FGGlobals::append_aircraft_path(const std::string& path)
287 SGPath dirPath(path);
288 if (!dirPath.exists()) {
289 SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
293 unsigned int index = fg_aircraft_dirs.size();
294 fg_aircraft_dirs.push_back(path);
296 // make aircraft dirs available to Nasal
297 SGPropertyNode* sim = fgGetNode("/sim", true);
298 sim->removeChild("fg-aircraft", index, false);
299 SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
300 n->setStringValue(path);
301 n->setAttribute(SGPropertyNode::WRITE, false);
304 void FGGlobals::append_aircraft_paths(const std::string& path)
306 string_list paths = sgPathSplit(path);
307 for (unsigned int p = 0; p<paths.size(); ++p) {
308 append_aircraft_path(paths[p]);
312 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
314 return simgear::ResourceManager::instance()->findPath(branch);
317 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
319 return simgear::ResourceManager::instance()->findPath(branch);
323 FGGlobals::get_renderer () const
329 FGGlobals::get_subsystem_mgr () const
331 return subsystem_mgr;
335 FGGlobals::get_subsystem (const char * name)
337 return subsystem_mgr->get_subsystem(name);
341 FGGlobals::add_subsystem (const char * name,
342 SGSubsystem * subsystem,
343 SGSubsystemMgr::GroupType type,
346 subsystem_mgr->add(name, subsystem, type, min_time_sec);
350 FGGlobals::get_soundmgr () const
356 FGGlobals::get_event_mgr () const
362 // Save the current state as the initial state.
364 FGGlobals::saveInitialState ()
366 initial_state = new SGPropertyNode();
368 if (!copyProperties(props, initial_state))
369 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
371 // delete various properties from the initial state, since we want to
372 // preserve their values even if doing a restore
374 SGPropertyNode* sim = initial_state->getChild("sim");
375 sim->removeChild("presets");
376 SGPropertyNode* simStartup = sim->getChild("startup");
377 simStartup->removeChild("xsize");
378 simStartup->removeChild("ysize");
380 SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
381 if (cameraGroupNode) {
382 cameraGroupNode->removeChild("camera");
383 cameraGroupNode->removeChild("gui");
388 // Restore the saved initial state, if any
390 FGGlobals::restoreInitialState ()
392 if ( initial_state == 0 ) {
393 SG_LOG(SG_GENERAL, SG_ALERT,
394 "No initial state available to restore!!!");
398 if ( copyProperties(initial_state, props) ) {
399 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
401 SG_LOG( SG_GENERAL, SG_INFO,
402 "Some errors restoring initial state (read-only props?)" );
408 FGGlobals::get_current_view () const
410 return viewmgr->get_current_view();
413 // end of globals.cxx