]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
Fix shared library build for metar executable
[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 <simgear/structure/commands.hxx>
31 #include <simgear/misc/sg_path.hxx>
32 #include <simgear/misc/sg_dir.hxx>
33 #include <simgear/timing/sg_time.hxx>
34 #include <simgear/ephemeris/ephemeris.hxx>
35 #include <simgear/magvar/magvar.hxx>
36 #include <simgear/scene/material/matlib.hxx>
37 #include <simgear/structure/subsystem_mgr.hxx>
38 #include <simgear/structure/event_mgr.hxx>
39 #include <simgear/sound/soundmgr_openal.hxx>
40 #include <simgear/misc/ResourceManager.hxx>
41 #include <simgear/props/propertyObject.hxx>
42 #include <simgear/props/props_io.hxx>
43 #include <simgear/scene/model/placement.hxx>
44
45 #include <Aircraft/controls.hxx>
46 #include <Airports/runways.hxx>
47 #include <ATCDCL/ATCmgr.hxx>
48 #include <Autopilot/route_mgr.hxx>
49 #include <Cockpit/panel.hxx>
50 #include <GUI/FGFontCache.hxx>
51 #include <Model/acmodel.hxx>
52 #include <Model/modelmgr.hxx>
53 #include <MultiPlayer/multiplaymgr.hxx>
54 #include <Scenery/scenery.hxx>
55 #include <Scenery/tilemgr.hxx>
56 #include <Navaids/navlist.hxx>
57
58 #include "globals.hxx"
59 #include "renderer.hxx"
60 #include "viewmgr.hxx"
61
62 #include "fg_props.hxx"
63 #include "fg_io.hxx"
64
65 class AircraftResourceProvider : public simgear::ResourceProvider
66 {
67 public:
68   AircraftResourceProvider() :
69     simgear::ResourceProvider(simgear::ResourceManager::PRIORITY_HIGH)
70   {
71   }
72   
73   virtual SGPath resolve(const std::string& aResource, SGPath&) const
74   {
75     string_list pieces(sgPathBranchSplit(aResource));
76     if ((pieces.size() < 3) || (pieces.front() != "Aircraft")) {
77       return SGPath(); // not an Aircraft path
78     }
79     
80   // test against the aircraft-dir property
81     const char* aircraftDir = fgGetString("/sim/aircraft-dir");
82     string_list aircraftDirPieces(sgPathBranchSplit(aircraftDir));
83     if (!aircraftDirPieces.empty() && (aircraftDirPieces.back() == pieces[1])) {
84         // current aircraft-dir matches resource aircraft
85         SGPath r(aircraftDir);
86         for (unsigned int i=2; i<pieces.size(); ++i) {
87           r.append(pieces[i]);
88         }
89         
90         if (r.exists()) {
91           SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " via /sim/aircraft-dir: " << r.str());
92           return r;
93         }
94     }
95   
96   // try each aircraft dir in turn
97     std::string res(aResource, 9); // resource path with 'Aircraft/' removed
98     const string_list& dirs(globals->get_aircraft_paths());
99     string_list::const_iterator it = dirs.begin();
100     for (; it != dirs.end(); ++it) {
101       SGPath p(*it, res);
102       if (p.exists()) {
103         SG_LOG(SG_IO, SG_DEBUG, "found path:" << aResource << " in aircraft dir: " << *it);
104         return p;
105       }
106     } // of aircraft path iteration
107     
108     return SGPath(); // not found
109   }
110 };
111
112 ////////////////////////////////////////////////////////////////////////
113 // Implementation of FGGlobals.
114 ////////////////////////////////////////////////////////////////////////
115
116 // global global :-)
117 FGGlobals *globals;
118
119
120 // Constructor
121 FGGlobals::FGGlobals() :
122     props( new SGPropertyNode ),
123     initial_state( NULL ),
124     locale( NULL ),
125     renderer( new FGRenderer ),
126     subsystem_mgr( new SGSubsystemMgr ),
127     event_mgr( new SGEventMgr ),
128     sim_time_sec( 0.0 ),
129     fg_root( "" ),
130     time_params( NULL ),
131     ephem( NULL ),
132     mag( NULL ),
133     matlib( NULL ),
134     route_mgr( NULL ),
135     current_panel( NULL ),
136     ATC_mgr( NULL ),
137     controls( NULL ),
138     viewmgr( NULL ),
139     commands( SGCommandMgr::instance() ),
140     acmodel( NULL ),
141     model_mgr( NULL ),
142     channel_options_list( NULL ),
143     initial_waypoints( NULL ),
144     scenery( NULL ),
145     tile_mgr( NULL ),
146     fontcache ( new FGFontCache ),
147     navlist( NULL ),
148     loclist( NULL ),
149     gslist( NULL ),
150     dmelist( NULL ),
151     tacanlist( NULL ),
152     carrierlist( NULL ),
153     channellist( NULL )
154 {
155   simgear::ResourceManager::instance()->addProvider(new AircraftResourceProvider());
156   simgear::PropertyObjectBase::setDefaultRoot(props);
157 }
158
159
160 // Destructor
161 FGGlobals::~FGGlobals() 
162 {    
163 // The AIModels manager performs a number of actions upon
164     // Shutdown that implicitly assume that other subsystems
165     // are still operational (Due to the dynamic allocation and
166     // deallocation of AIModel objects. To ensure we can safely
167     // shut down all subsystems, make sure we take down the 
168     // AIModels system first.
169     SGSubsystem* ai = subsystem_mgr->remove("ai-model");
170     if (ai) {
171         ai->unbind();
172         delete ai;
173     }
174     
175     subsystem_mgr->shutdown();
176     subsystem_mgr->unbind();
177     delete subsystem_mgr;
178     
179     delete renderer;
180     renderer = NULL;
181     
182     delete time_params;
183     delete mag;
184     delete matlib;
185     delete route_mgr;
186     delete current_panel;
187
188     delete ATC_mgr;
189
190     if (controls)
191     {
192         controls->unbind();
193         delete controls;
194     }
195
196     delete channel_options_list;
197     delete initial_waypoints;
198     delete scenery;
199     delete fontcache;
200
201     delete navlist;
202     delete loclist;
203     delete gslist;
204     delete dmelist;
205     delete tacanlist;
206     delete carrierlist;
207     delete channellist;
208 }
209
210
211 // set the fg_root path
212 void FGGlobals::set_fg_root (const string &root) {
213     fg_root = root;
214
215     // append /data to root if it exists
216     SGPath tmp( fg_root );
217     tmp.append( "data" );
218     tmp.append( "version" );
219     if ( tmp.exists() ) {
220         fgGetNode("BAD_FG_ROOT", true)->setStringValue(fg_root);
221         fg_root += "/data";
222         fgGetNode("GOOD_FG_ROOT", true)->setStringValue(fg_root);
223         SG_LOG(SG_GENERAL, SG_ALERT, "***\n***\n*** Warning: changing bad FG_ROOT/--fg-root to '"
224                 << fg_root << "'\n***\n***");
225     }
226
227     // remove /sim/fg-root before writing to prevent hijacking
228     SGPropertyNode *n = fgGetNode("/sim", true);
229     n->removeChild("fg-root", 0, false);
230     n = n->getChild("fg-root", 0, true);
231     n->setStringValue(fg_root.c_str());
232     n->setAttribute(SGPropertyNode::WRITE, false);
233     
234     simgear::ResourceManager::instance()->addBasePath(fg_root, 
235       simgear::ResourceManager::PRIORITY_DEFAULT);
236 }
237
238 void FGGlobals::append_fg_scenery (const string &paths)
239 {
240 //    fg_scenery.clear();
241     SGPropertyNode* sim = fgGetNode("/sim", true);
242
243   // find first unused fg-scenery property in /sim
244     int propIndex = 0;
245     while (sim->getChild("fg-scenery", propIndex) != NULL) {
246       ++propIndex; 
247     }
248   
249     BOOST_FOREACH(const SGPath& path, sgPathSplit( paths )) {
250         if (!path.exists()) {
251           SG_LOG(SG_GENERAL, SG_WARN, "scenery path not found:" << path.str());
252           continue;
253         }
254
255       // check for duplicates
256       string_list::const_iterator ex = std::find(fg_scenery.begin(), fg_scenery.end(), path.str());
257       if (ex != fg_scenery.end()) {
258         SG_LOG(SG_GENERAL, SG_INFO, "skipping duplicate add of scenery path:" << path.str());
259         continue;
260       }
261       
262         simgear::Dir dir(path);
263         SGPath terrainDir(dir.file("Terrain"));
264         SGPath objectsDir(dir.file("Objects"));
265         
266       // this code used to add *either* the base dir, OR add the 
267       // Terrain and Objects subdirs, but the conditional logic was commented
268       // out, such that all three dirs are added. Unfortunately there's
269       // no information as to why the change was made.
270         fg_scenery.push_back(path.str());
271         
272         if (terrainDir.exists()) {
273           fg_scenery.push_back(terrainDir.str());
274         }
275         
276         if (objectsDir.exists()) {
277           fg_scenery.push_back(objectsDir.str());
278         }
279         
280         // insert a marker for FGTileEntry::load(), so that
281         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
282         // "B/Terrain", "B/Objects", ""]
283         fg_scenery.push_back("");
284         
285       // make scenery dirs available to Nasal
286         SGPropertyNode* n = sim->getChild("fg-scenery", propIndex++, true);
287         n->setStringValue(path.str());
288         n->setAttribute(SGPropertyNode::WRITE, false);
289     } // of path list iteration
290 }
291
292 void FGGlobals::append_aircraft_path(const std::string& path)
293 {
294   SGPath dirPath(path);
295   if (!dirPath.exists()) {
296     SG_LOG(SG_GENERAL, SG_WARN, "aircraft path not found:" << path);
297     return;
298   }
299   
300   unsigned int index = fg_aircraft_dirs.size();  
301   fg_aircraft_dirs.push_back(path);
302   
303 // make aircraft dirs available to Nasal
304   SGPropertyNode* sim = fgGetNode("/sim", true);
305   sim->removeChild("fg-aircraft", index, false);
306   SGPropertyNode* n = sim->getChild("fg-aircraft", index, true);
307   n->setStringValue(path);
308   n->setAttribute(SGPropertyNode::WRITE, false);
309 }
310
311 void FGGlobals::append_aircraft_paths(const std::string& path)
312 {
313   string_list paths = sgPathSplit(path);
314   for (unsigned int p = 0; p<paths.size(); ++p) {
315     append_aircraft_path(paths[p]);
316   }
317 }
318
319 SGPath FGGlobals::resolve_aircraft_path(const std::string& branch) const
320 {
321   return simgear::ResourceManager::instance()->findPath(branch);
322 }
323
324 SGPath FGGlobals::resolve_maybe_aircraft_path(const std::string& branch) const
325 {
326   return simgear::ResourceManager::instance()->findPath(branch);
327 }
328
329 FGRenderer *
330 FGGlobals::get_renderer () const
331 {
332    return renderer;
333 }
334
335 SGSubsystemMgr *
336 FGGlobals::get_subsystem_mgr () const
337 {
338     return subsystem_mgr;
339 }
340
341 SGSubsystem *
342 FGGlobals::get_subsystem (const char * name)
343 {
344     return subsystem_mgr->get_subsystem(name);
345 }
346
347 void
348 FGGlobals::add_subsystem (const char * name,
349                           SGSubsystem * subsystem,
350                           SGSubsystemMgr::GroupType type,
351                           double min_time_sec)
352 {
353     subsystem_mgr->add(name, subsystem, type, min_time_sec);
354 }
355
356 SGSoundMgr *
357 FGGlobals::get_soundmgr () const
358 {
359     if (subsystem_mgr)
360         return (SGSoundMgr*) subsystem_mgr->get_subsystem("sound");
361
362     return NULL;
363 }
364
365 SGEventMgr *
366 FGGlobals::get_event_mgr () const
367 {
368     return event_mgr;
369 }
370
371 const SGGeod &
372 FGGlobals::get_aircraft_position() const
373 {
374     if( acmodel != NULL ) {
375         SGModelPlacement * mp = acmodel->get3DModel();
376         if( mp != NULL )
377             return mp->getPosition();
378     }
379     throw sg_exception("Can't get aircraft position", "FGGlobals::get_aircraft_position()" );
380 }
381
382 SGVec3d
383 FGGlobals::get_aircraft_positon_cart() const
384 {
385     return SGVec3d::fromGeod(get_aircraft_position());
386 }
387
388
389 // Save the current state as the initial state.
390 void
391 FGGlobals::saveInitialState ()
392 {
393   initial_state = new SGPropertyNode();
394
395   // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
396   int checked  = SGPropertyNode::READ+SGPropertyNode::WRITE+
397                  SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
398   int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
399   if (!copyProperties(props, initial_state, expected, checked))
400     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
401     
402   // delete various properties from the initial state, since we want to
403   // preserve their values even if doing a restore
404   // => Properties should now use the PRESERVE flag to protect their values
405   // on sim-reset. Remove some specific properties for backward compatibility.
406   SGPropertyNode* sim = initial_state->getChild("sim");
407   SGPropertyNode* cameraGroupNode = sim->getNode("rendering/camera-group");
408   if (cameraGroupNode) {
409     cameraGroupNode->removeChild("camera");
410     cameraGroupNode->removeChild("gui");
411   }
412 }
413
414
415 // Restore the saved initial state, if any
416 void
417 FGGlobals::restoreInitialState ()
418 {
419     if ( initial_state == 0 ) {
420         SG_LOG(SG_GENERAL, SG_ALERT,
421                "No initial state available to restore!!!");
422         return;
423     }
424     // copy properties which are READ/WRITEable - but not USERARCHIVEd or PRESERVEd
425     int checked  = SGPropertyNode::READ+SGPropertyNode::WRITE+
426                    SGPropertyNode::USERARCHIVE+SGPropertyNode::PRESERVE;
427     int expected = SGPropertyNode::READ+SGPropertyNode::WRITE;
428     if ( copyProperties(initial_state, props, expected, checked)) {
429         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
430     } else {
431         SG_LOG( SG_GENERAL, SG_INFO,
432                 "Some errors restoring initial state (read-only props?)" );
433     }
434
435 }
436
437 FGViewer *
438 FGGlobals::get_current_view () const
439 {
440   return viewmgr->get_current_view();
441 }
442
443 long int FGGlobals::get_warp() const
444 {
445   return fgGetInt("/sim/time/warp");
446 }
447
448 void FGGlobals::set_warp( long int w )
449 {
450   fgSetInt("/sim/time/warp", w);
451 }
452
453 long int FGGlobals::get_warp_delta() const
454 {
455   return fgGetInt("/sim/time/warp-delta");
456 }
457
458 void FGGlobals::set_warp_delta( long int d )
459 {
460   fgSetInt("/sim/time/warp-delta", d);
461 }
462     
463 // end of globals.cxx