]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.hxx
Progress towards testing
[flightgear.git] / src / Main / globals.hxx
1 // globals.hxx -- 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
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifndef _GLOBALS_HXX
25 #define _GLOBALS_HXX
26
27 #include <simgear/compiler.h>
28 #include <simgear/props/props.hxx>
29 #include <simgear/structure/subsystem_mgr.hxx>
30 #include <simgear/misc/sg_path.hxx>
31
32 #include <vector>
33 #include <string>
34 #include <memory>
35
36 typedef std::vector<std::string> string_list;
37 typedef std::vector<SGPath> PathList;
38
39 // Forward declarations
40
41 // This file is included, directly or indirectly, almost everywhere in
42 // FlightGear, so if any of its dependencies changes, most of the sim
43 // has to be recompiled.  Using these forward declarations helps us to
44 // avoid including a lot of header files (and thus, a lot of
45 // dependencies).  Since Most of the methods simply set or return
46 // pointers, we don't need to know anything about the class details
47 // anyway.
48
49 class SGEphemeris;
50 class SGCommandMgr;
51 class SGMaterialLib;
52 class SGPropertyNode;
53 class SGTime;
54 class SGEventMgr;
55 class SGSubsystemMgr;
56 class SGSubsystem;
57 class SGSoundMgr;
58
59 class FGControls;
60 class FGTACANList;
61 class FGLocale;
62 class FGRouteMgr;
63 class FGScenery;
64 class FGTileMgr;
65 class FGViewMgr;
66 class FGViewer;
67 class FGRenderer;
68 class FGFontCache;
69 class FGSampleQueue;
70
71 namespace simgear { namespace pkg {
72   class Root;
73 }}
74
75 /**
76  * Bucket for subsystem pointers representing the sim's state.
77  */
78
79 class FGGlobals
80 {
81
82 private:
83
84     // properties, destroy last
85     SGPropertyNode_ptr props;
86
87     // localization
88     FGLocale* locale;
89
90     FGRenderer *renderer;
91     SGSubsystemMgr *subsystem_mgr;
92     SGEventMgr *event_mgr;
93
94     // Number of milliseconds elapsed since the start of the program.
95     double sim_time_sec;
96
97     // Root of FlightGear data tree
98     std::string fg_root;
99
100     /**
101      * locations to search for (non-scenery) data. 
102      */
103     PathList additional_data_paths;
104     
105     // Users home directory for data
106     std::string fg_home;
107
108     // Roots of FlightGear scenery tree
109     string_list fg_scenery;
110     string_list secure_fg_scenery;
111
112     std::string browser;
113
114     // Time structure
115     SGTime *time_params;
116
117     // Sky structures
118     SGEphemeris *ephem;
119
120     // Material properties library
121     SGSharedPtr<SGMaterialLib> matlib;
122
123     // viewer manager
124     FGViewMgr *viewmgr;
125
126     SGCommandMgr *commands;
127
128     // list of serial port-like configurations
129     string_list *channel_options_list;
130
131     // A list of initial waypoints that are read from the command line
132     // and or flight-plan file during initialization
133     string_list *initial_waypoints;
134
135     // FlightGear scenery manager
136     SGSharedPtr<FGScenery> _scenery;
137
138     // Tile manager
139     SGSharedPtr<FGTileMgr> _tile_mgr;
140
141     FGFontCache *fontcache;
142
143     // Navigational Aids
144     FGTACANList *channellist;
145
146     /// roots of Aircraft trees
147     string_list fg_aircraft_dirs;
148     SGPath catalog_aircraft_dir;
149
150     bool haveUserSettings;
151
152     SGPropertyNode_ptr positionLon, positionLat, positionAlt;
153     SGPropertyNode_ptr viewLon, viewLat, viewAlt;
154     SGPropertyNode_ptr orientHeading, orientPitch, orientRoll;
155     
156     /**
157      * helper to initialise standard properties on a new property tree
158      */
159     void initProperties();
160     
161     SGSharedPtr<FGSampleQueue> _chatter_queue;
162     
163     void cleanupListeners();
164     
165     typedef std::vector<SGPropertyChangeListener*> SGPropertyChangeListenerVec;
166     SGPropertyChangeListenerVec _listeners_to_cleanup;
167   
168     SGSharedPtr<simgear::pkg::Root> _packageRoot;
169 public:
170
171     FGGlobals();
172     virtual ~FGGlobals();
173
174     virtual FGRenderer *get_renderer () const;
175     void set_renderer(FGRenderer* render);
176     
177     SGSubsystemMgr *get_subsystem_mgr () const;
178
179     SGSubsystem *get_subsystem (const char * name) const;
180
181     template<class T>
182     T* get_subsystem() const
183     {
184         return dynamic_cast<T*>(get_subsystem(T::subsystemName()));
185     }
186
187
188     void add_subsystem (const char * name,
189                                 SGSubsystem * subsystem,
190                                 SGSubsystemMgr::GroupType
191                                 type = SGSubsystemMgr::GENERAL,
192                                 double min_time_sec = 0);
193
194     template<class T>
195     T* add_new_subsystem (SGSubsystemMgr::GroupType
196                                 type = SGSubsystemMgr::GENERAL,
197                                 double min_time_sec = 0)
198     {
199         T* sub = new T;
200         add_subsystem(T::subsystemName(), sub, type, min_time_sec);
201         return sub;
202     }
203
204     SGEventMgr *get_event_mgr () const;
205
206     SGSoundMgr *get_soundmgr () const;
207
208     inline double get_sim_time_sec () const { return sim_time_sec; }
209     inline void inc_sim_time_sec (double dt) { sim_time_sec += dt; }
210     inline void set_sim_time_sec (double t) { sim_time_sec = t; }
211
212     inline const std::string &get_fg_root () const { return fg_root; }
213     void set_fg_root (const std::string &root);
214
215     /**
216      * Get list of data locations. fg_root is always the final item in the
217      * result.
218      */
219     PathList get_data_paths() const;
220     
221     /**
222      * Get data locations which contain the file path suffix. Eg pass ing
223      * 'AI/Traffic' to get all data paths which define <path>/AI/Traffic subdir
224      */
225     PathList get_data_paths(const std::string& suffix) const;
226     
227     void append_data_path(const SGPath& path);
228     
229     /**
230      * Given a path suffix (eg 'Textures' or 'AI/Traffic'), find the
231      * first data directory which defines it.
232      */
233     SGPath find_data_dir(const std::string& pathSuffix) const;
234     
235     inline const std::string &get_fg_home () const { return fg_home; }
236     void set_fg_home (const std::string &home);
237
238     inline const string_list &get_fg_scenery () const { return fg_scenery; }
239     inline const string_list &get_secure_fg_scenery () const { return secure_fg_scenery; }
240     /**
241      * Add a scenery directory
242      *
243      * secure = allow Nasal to read this directory; to avoid
244      * can-read-any-file security holes, do NOT set this on directories
245      * obtained from the property tree (e.g. /sim/terrasync/scenery-dir)
246      * or other Nasal-writable places
247      */ 
248     void append_fg_scenery (const std::string &scenery, bool secure = false);
249
250     void clear_fg_scenery();
251
252     /**
253      * specify a path we'll prepend to the aircraft paths list if non-empty.
254      * This is used with packaged aircraft, to ensure their catalog (and hence,
255      * dependency packages) are found correctly.
256      */
257     void set_catalog_aircraft_path(const SGPath& path);
258
259     string_list get_aircraft_paths() const;
260
261     void append_aircraft_path(const std::string& path);
262     void append_aircraft_paths(const std::string& path);
263     
264     /**
265      * Given a path to an aircraft-related resource file, resolve it
266      * against the appropriate root. This means looking at the location
267      * defined by /sim/aircraft-dir, and then aircraft_path in turn,
268      * finishing with fg_root/Aircraft.
269      *
270      * if the path could not be resolved, an empty path is returned.
271      */
272     SGPath resolve_aircraft_path(const std::string& branch) const;
273     
274     /**
275      * Same as above, but test for non 'Aircraft/' branch paths, and
276      * always resolve them against fg_root.
277      */
278     SGPath resolve_maybe_aircraft_path(const std::string& branch) const;
279     
280     /**
281      * Search in the following directories:
282      *
283      *  1. Root directory of current aircraft (defined by /sim/aircraft-dir)
284      *  2. All aircraft directories if branch starts with Aircraft/
285      *  3. fg_data directory
286      */
287     SGPath resolve_resource_path(const std::string& branch) const;
288
289     inline const std::string &get_browser () const { return browser; }
290     void set_browser (const std::string &b) { browser = b; }
291
292     long int get_warp() const;
293     void set_warp( long int w );
294
295     long int get_warp_delta() const;
296     void set_warp_delta( long int d );
297
298     inline SGTime *get_time_params() const { return time_params; }
299     inline void set_time_params( SGTime *t ) { time_params = t; }
300
301     inline SGEphemeris *get_ephem() const { return ephem; }
302     inline void set_ephem( SGEphemeris *e ) { ephem = e; }
303
304     inline SGMaterialLib *get_matlib() const { return matlib; }
305     void set_matlib( SGMaterialLib *m );
306
307     inline FGViewMgr *get_viewmgr() const { return viewmgr; }
308     inline void set_viewmgr( FGViewMgr *vm ) { viewmgr = vm; }
309     FGViewer *get_current_view() const;
310
311     inline SGPropertyNode *get_props () { return props; }
312
313     /**
314      * @brief reset the property tree to new, empty tree. Ensure all
315      * subsystems are shutdown and unbound before call this.
316      */
317     void resetPropertyRoot();
318     
319     inline FGLocale* get_locale () { return locale; }
320
321     inline SGCommandMgr *get_commands () { return commands; }
322
323     SGGeod get_aircraft_position() const;
324
325     SGVec3d get_aircraft_position_cart() const;
326
327     void get_aircraft_orientation(double& heading, double& pitch, double& roll);
328   
329     SGGeod get_view_position() const;
330   
331     SGVec3d get_view_position_cart() const;
332   
333     inline string_list *get_channel_options_list () {
334         return channel_options_list;
335     }
336     inline void set_channel_options_list( string_list *l ) {
337         channel_options_list = l;
338     }
339
340     inline string_list *get_initial_waypoints () {
341         return initial_waypoints;
342     }
343   
344     inline void set_initial_waypoints (string_list *list) {
345         initial_waypoints = list;
346     }
347
348     FGControls *get_controls() const;
349
350     FGScenery * get_scenery () const;
351     void set_scenery ( FGScenery *s );
352
353     FGTileMgr * get_tile_mgr () const;
354     void set_tile_mgr ( FGTileMgr *t );
355
356     inline FGFontCache *get_fontcache() const { return fontcache; }
357   
358     inline FGTACANList *get_channellist() const { return channellist; }
359     inline void set_channellist( FGTACANList *c ) { channellist = c; }
360
361     /**
362      * Load user settings from autosave.xml
363      */
364     void loadUserSettings(const SGPath& datapath);
365
366     /**
367      * Save user settings in autosave.xml
368      */
369     void saveUserSettings();
370     
371     FGSampleQueue* get_chatter_queue() const;
372     void set_chatter_queue(FGSampleQueue* queue);
373     
374     void addListenerToCleanup(SGPropertyChangeListener* l);
375   
376     simgear::pkg::Root* packageRoot();
377     void setPackageRoot(const SGSharedPtr<simgear::pkg::Root>& p);
378 };
379
380
381 extern FGGlobals *globals;
382
383
384 #endif // _GLOBALS_HXX