]> git.mxchange.org Git - flightgear.git/blob - src/Main/globals.cxx
Additional checks to io arguments. This prevents fg from crashing on invalid
[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
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/sound/soundmgr_openal.hxx>
28 #include <simgear/structure/commands.hxx>
29 #include <simgear/misc/sg_path.hxx>
30
31 #include "globals.hxx"
32 #include "renderer.hxx"
33 #include "viewmgr.hxx"
34
35 #include "fg_props.hxx"
36 #include "fg_io.hxx"
37
38 \f
39 ////////////////////////////////////////////////////////////////////////
40 // Implementation of FGGlobals.
41 ////////////////////////////////////////////////////////////////////////
42
43 // global global :-)
44 FGGlobals *globals;
45
46
47 // Constructor
48 FGGlobals::FGGlobals() :
49     renderer( new FGRenderer ),
50     subsystem_mgr( new SGSubsystemMgr ),
51     event_mgr( new SGEventMgr ),
52     sim_time_sec( 0.0 ),
53     fg_root( "" ),
54 #if defined(FX) && defined(XMESA)
55     fullscreen( true ),
56 #endif
57     warp( 0 ),
58     warp_delta( 0 ),
59     time_params( NULL ),
60     ephem( NULL ),
61     mag( NULL ),
62     route_mgr( NULL ),
63     current_panel( NULL ),
64     soundmgr( NULL ),
65     airports( NULL ),
66     ATC_mgr( NULL ),
67     ATC_display( NULL ),
68     AI_mgr( NULL ),
69     controls( NULL ),
70     viewmgr( NULL ),
71     props( new SGPropertyNode ),
72     initial_state( NULL ),
73     locale( NULL ),
74     commands( new SGCommandMgr ),
75     model_lib( NULL ),
76     acmodel( NULL ),
77     model_mgr( NULL ),
78     channel_options_list( NULL ),
79     initial_waypoints( NULL ),
80     scenery( NULL ),
81     tile_mgr( NULL ),
82     io( new FGIO ),
83     navlist( NULL ),
84     loclist( NULL ),
85     gslist( NULL ),
86     dmelist( NULL ),
87     mkrlist( NULL ),
88     tacanlist( NULL ),
89     carrierlist( NULL ), 
90     fixlist( NULL )
91 {
92 }
93
94
95 // Destructor
96 FGGlobals::~FGGlobals() 
97 {
98     delete soundmgr;
99     delete subsystem_mgr;
100     delete event_mgr;
101     delete initial_state;
102     delete props;
103     delete commands;
104     delete io;
105     delete renderer;
106     delete initial_waypoints;
107 }
108
109
110 // set the fg_root path
111 void FGGlobals::set_fg_root (const string &root) {
112     fg_root = root;
113     
114     // append /data to root if it exists
115     SGPath tmp( fg_root );
116     tmp.append( "data" );
117     tmp.append( "version" );
118     if ( ulFileExists( tmp.c_str() ) ) {
119         fg_root += "/data";
120         }
121 }
122
123 void FGGlobals::set_fg_scenery (const string &scenery) {
124     SGPath s;
125     if (scenery.empty()) {
126         s.set( fg_root );
127         s.append( "Scenery" );
128     } else
129         s.set( scenery );
130
131     string_list path_list = sgPathSplit( s.str() );
132     fg_scenery.clear();
133
134     for (unsigned i = 0; i < path_list.size(); i++) {
135
136         ulDir *d = ulOpenDir( path_list[i].c_str() );
137         if (d == NULL)
138             continue;
139         ulCloseDir( d );
140
141         SGPath pt( path_list[i] ), po( path_list[i] );
142         pt.append("Terrain");
143         po.append("Objects");
144
145         ulDir *td = ulOpenDir( pt.c_str() );
146         ulDir *od = ulOpenDir( po.c_str() );
147
148         if (td == NULL && od == NULL)
149             fg_scenery.push_back( path_list[i] );
150         else {
151             if (td != NULL) {
152                 fg_scenery.push_back( pt.str() );
153                 ulCloseDir( td );
154             }
155             if (od != NULL) {
156                 fg_scenery.push_back( po.str() );
157                 ulCloseDir( od );
158             }
159         }
160         // insert a marker for FGTileEntry::load(), so that
161         // FG_SCENERY=A:B becomes list ["A/Terrain", "A/Objects", "",
162         // "B/Terrain", "B/Objects", ""]
163         fg_scenery.push_back("");
164     }
165 }
166
167
168 FGRenderer *
169 FGGlobals::get_renderer () const
170 {
171    return renderer;
172 }
173
174 SGSubsystemMgr *
175 FGGlobals::get_subsystem_mgr () const
176 {
177     return subsystem_mgr;
178 }
179
180 SGSubsystem *
181 FGGlobals::get_subsystem (const char * name)
182 {
183     return subsystem_mgr->get_subsystem(name);
184 }
185
186 void
187 FGGlobals::add_subsystem (const char * name,
188                           SGSubsystem * subsystem,
189                           SGSubsystemMgr::GroupType type,
190                           double min_time_sec)
191 {
192     subsystem_mgr->add(name, subsystem, type, min_time_sec);
193 }
194
195
196 SGEventMgr *
197 FGGlobals::get_event_mgr () const
198 {
199     return event_mgr;
200 }
201
202
203 // Save the current state as the initial state.
204 void
205 FGGlobals::saveInitialState ()
206 {
207   delete initial_state;
208   initial_state = new SGPropertyNode();
209
210   if (!copyProperties(props, initial_state))
211     SG_LOG(SG_GENERAL, SG_ALERT, "Error saving initial state");
212 }
213
214
215 // Restore the saved initial state, if any
216 void
217 FGGlobals::restoreInitialState ()
218 {
219     if ( initial_state == 0 ) {
220         SG_LOG(SG_GENERAL, SG_ALERT,
221                "No initial state available to restore!!!");
222         return;
223     }
224
225     SGPropertyNode *currentPresets = new SGPropertyNode;
226     SGPropertyNode *targetNode = fgGetNode( "/sim/presets" );
227
228     // stash the /sim/presets tree
229     if ( !copyProperties(targetNode, currentPresets) ) {
230         SG_LOG( SG_GENERAL, SG_ALERT, "Failed to save /sim/presets subtree" );
231     }
232     
233     if ( copyProperties(initial_state, props) ) {
234         SG_LOG( SG_GENERAL, SG_INFO, "Initial state restored successfully" );
235     } else {
236         SG_LOG( SG_GENERAL, SG_INFO,
237                 "Some errors restoring initial state (read-only props?)" );
238     }
239
240     // recover the /sim/presets tree
241     if ( !copyProperties(currentPresets, targetNode) ) {
242         SG_LOG( SG_GENERAL, SG_ALERT,
243                 "Failed to restore /sim/presets subtree" );
244     }
245
246    delete currentPresets;
247 }
248
249 FGViewer *
250 FGGlobals::get_current_view () const
251 {
252   return viewmgr->get_current_view();
253 }
254
255 // end of globals.cxx