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