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