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>
38 #include <Aircraft/controls.hxx>
39 #include <Airports/runways.hxx>
40 #include <ATCDCL/ATCmgr.hxx>
41 #include <Autopilot/route_mgr.hxx>
42 #include <Cockpit/panel.hxx>
43 #include <GUI/new_gui.hxx>
44 #include <Model/acmodel.hxx>
45 #include <Model/modelmgr.hxx>
46 #include <MultiPlayer/multiplaymgr.hxx>
47 #include <Navaids/awynet.hxx>
48 #include <Scenery/scenery.hxx>
49 #include <Scenery/tilemgr.hxx>
50 #include <Navaids/navlist.hxx>
52 #include "globals.hxx"
53 #include "renderer.hxx"
54 #include "viewmgr.hxx"
56 #include "fg_props.hxx"
60 ////////////////////////////////////////////////////////////////////////
61 // Implementation of FGGlobals.
62 ////////////////////////////////////////////////////////////////////////
69 FGGlobals::FGGlobals() :
70 props( new SGPropertyNode ),
71 initial_state( NULL ),
73 renderer( new FGRenderer ),
74 subsystem_mgr( new SGSubsystemMgr ),
75 event_mgr( new SGEventMgr ),
76 soundmgr( new SGSoundMgr ),
86 current_panel( NULL ),
90 commands( SGCommandMgr::instance() ),
93 channel_options_list( NULL ),
94 initial_waypoints( NULL ),
97 fontcache ( new FGFontCache ),
106 multiplayer_mgr( NULL )
112 FGGlobals::~FGGlobals()
115 // The AIModels manager performs a number of actions upon
116 // Shutdown that implicitly assume that other subsystems
117 // are still operational (Due to the dynamic allocation and
118 // deallocation of AIModel objects. To ensure we can safely
119 // shut down all subsystems, make sure we take down the
120 // AIModels system first.
121 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("ai_model");
122 // FGInput (FGInputEvent) and FGDialog calls get_subsystem() in their destructors,
123 // which is not safe since some subsystem are already deleted but can be referred.
124 // So these subsystems must be deleted prior to deleting subsystem_mgr unless
125 // ~SGSubsystemGroup and SGSubsystemMgr::get_subsystem are changed not to refer to
126 // deleted subsystems.
127 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("input");
128 subsystem_mgr->get_group(SGSubsystemMgr::GENERAL)->remove_subsystem("gui");
129 subsystem_mgr->unbind();
130 delete subsystem_mgr;
136 delete current_panel;
145 delete channel_options_list;
146 delete initial_waypoints;
159 delete multiplayer_mgr;
166 // set the fg_root path
167 void FGGlobals::set_fg_root (const string &root) {
170 // append /data to root if it exists
171 SGPath tmp( fg_root );
172 tmp.append( "data" );
173 tmp.append( "version" );
174 if ( ulFileExists( tmp.c_str() ) ) {
175 fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
177 fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
178 SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
179 << fg_root << "'\n***\n***");
182 // remove /sim/fg-root before writing to prevent hijacking
183 SGPropertyNode *n = fgGetNode("/sim", true);
184 n->removeChild("fg-root", 0, false);
185 n = n->getChild("fg-root", 0, true);
186 n->setStringValue(fg_root.c_str());
187 n->setAttribute(SGPropertyNode::WRITE, false);
190 void FGGlobals::set_fg_scenery (const string &scenery)
193 if (scenery.empty()) {
195 s.append( "Scenery" );
199 string_list path_list = sgPathSplit( s.str() );
202 for (unsigned i = 0; i < path_list.size(); i++) {
203 SGPath path(path_list[i]);
204 if (!path.exists()) {
205 SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << path.str());
209 simgear::Dir dir(path);
210 SGPath terrainDir(dir.file("Terrain"));
211 SGPath objectsDir(dir.file("Objects"));
213 // this code used to add *either* the base dir, OR add the
214 // Terrain and Objects subdirs, but the conditional logic was commented
215 // out, such that all three dirs are added. Unfortunately there's
216 // no information as to why the change was made.
217 fg_scenery.push_back(path.str());
219 if (terrainDir.exists()) {
220 fg_scenery.push_back(terrainDir.str());
223 if (objectsDir.exists()) {
224 fg_scenery.push_back(objectsDir.str());
227 // insert a marker for FGTileEntry::load(), so that
228 // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
229 // "B/Terrain", "B/Objects", ""]
230 fg_scenery.push_back("");
231 } // of path list iteration
234 void FGGlobals::append_aircraft_path(const std::string& path)
236 SGPath dirPath(path);
237 if (!dirPath.exists()) {
238 SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
242 fg_aircraft_dirs.push_back(path);
245 void FGGlobals::append_aircraft_paths(const std::string& path)
247 string_list paths = sgPathSplit(path);
248 for (unsigned int p = 0; p<paths.size(); ++p) {
249 append_aircraft_path(paths[p]);
253 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
255 string_list pieces(sgPathBranchSplit(branch));
256 if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
257 SG_LOG(SG_AIRCRAFT, SG_ALERT, "resolve_aircraft_path: bad path:" << branch);
261 // check current aircraft dir first (takes precedence, allows Generics to be
263 const char* aircraftDir = fgGetString("/sim/aircraft-dir");
264 string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
265 if (!aircraftDirPieces.empty() && (aircraftDirPieces.back() == pieces[1])) {
266 SGPath r(aircraftDir);
268 for (unsigned int i=2; i<pieces.size(); ++i) {
273 std::cout << "using aircraft-dir for:" << r.str() << std::endl;
276 } // of using aircraft-dir case
278 // try each fg_aircraft_dirs in turn
279 for (unsigned int p=0; p<fg_aircraft_dirs.size(); ++p) {
280 SGPath r(fg_aircraft_dirs[p]);
283 std::cout << "using aircraft directory for:" << r.str() << std::endl;
286 } // of fg_aircraft_dirs iteration
288 // finally, try fg_root
292 std::cout << "using FG_ROOT for:" << r.str() << std::endl;
296 SG_LOG(SG_AIRCRAFT, SG_ALERT, "resolve_aircraft_path: failed to resolve:" << branch);
300 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
302 if (branch.find("Aircraft/") == 0) {
303 return resolve_aircraft_path(branch);
312 FGGlobals::get_renderer () const
318 FGGlobals::get_subsystem_mgr () const
320 return subsystem_mgr;
324 FGGlobals::get_subsystem (const char * name)
326 return subsystem_mgr->get_subsystem(name);
330 FGGlobals::add_subsystem (const char * name,
331 SGSubsystem * subsystem,
332 SGSubsystemMgr::GroupType type,
335 subsystem_mgr->add(name, subsystem, type, min_time_sec);
339 FGGlobals::get_soundmgr () const
345 FGGlobals::get_event_mgr () const
351 // Save the current state as the initial state.
353 FGGlobals::saveInitialState ()
355 initial_state = new SGPropertyNode();
357 if (!copyProperties(props, initial_state))
358 SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
360 // delete various properties from the initial state, since we want to
361 // preserve their values even if doing a restore
363 SGPropertyNode* sim = initial_state->getChild("sim");
364 sim->removeChild("presets");
365 SGPropertyNode* simStartup = sim->getChild("startup");
366 simStartup->removeChild("xsize");
367 simStartup->removeChild("ysize");
369 SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
370 if (cameraGroupNode) {
371 cameraGroupNode->removeChild("camera");
372 cameraGroupNode->removeChild("gui");
377 // Restore the saved initial state, if any
379 FGGlobals::restoreInitialState ()
381 if ( initial_state == 0 ) {
382 SG_LOG(SG_GENERAL, SG_ALERT,
383 "No initial state available to restore!!!");
387 if ( copyProperties(initial_state, props) ) {
388 SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
390 SG_LOG( SG_GENERAL, SG_INFO,
391 "Some errors restoring initial state (read-only props?)" );
397 FGGlobals::get_current_view () const
399 return viewmgr->get_current_view();
402 // end of globals.cxx