| 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 FGViewMgr; |
| 63 | class FGRenderer; |
| 64 | |
| 65 | namespace simgear { namespace pkg { |
| 66 | class Root; |
| 67 | }} |
| 68 | |
| 69 | namespace flightgear |
| 70 | { |
| 71 | class View; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Bucket for subsystem pointers representing the sim's state. |
| 76 | */ |
| 77 | |
| 78 | class FGGlobals |
| 79 | { |
| 80 | |
| 81 | private: |
| 82 | |
| 83 | // properties, destroy last |
| 84 | SGPropertyNode_ptr props; |
| 85 | |
| 86 | // localization |
| 87 | FGLocale* locale; |
| 88 | |
| 89 | FGRenderer *renderer; |
| 90 | SGSubsystemMgr *subsystem_mgr; |
| 91 | SGEventMgr *event_mgr; |
| 92 | |
| 93 | // Number of milliseconds elapsed since the start of the program. |
| 94 | double sim_time_sec; |
| 95 | |
| 96 | // Root of FlightGear data tree |
| 97 | SGPath fg_root; |
| 98 | |
| 99 | /** |
| 100 | * locations to search for (non-scenery) data. |
| 101 | */ |
| 102 | PathList additional_data_paths; |
| 103 | |
| 104 | PathList _dataPathsAfterFGRoot; ///< paths with a /lower/ priority than FGRoot |
| 105 | |
| 106 | // Users home directory for data |
| 107 | SGPath fg_home; |
| 108 | |
| 109 | SGPath texture_cache_dir; |
| 110 | // Download directory |
| 111 | SGPath download_dir; |
| 112 | // Terrasync directory |
| 113 | SGPath terrasync_dir; |
| 114 | |
| 115 | // Roots of FlightGear scenery tree |
| 116 | PathList fg_scenery; |
| 117 | // Paths Nasal is allowed to read |
| 118 | PathList extra_read_allowed_paths; |
| 119 | |
| 120 | std::string browser; |
| 121 | |
| 122 | // Time structure |
| 123 | SGTime *time_params; |
| 124 | |
| 125 | // Material properties library |
| 126 | SGSharedPtr<SGMaterialLib> matlib; |
| 127 | |
| 128 | SGCommandMgr *commands; |
| 129 | |
| 130 | // list of serial port-like configurations |
| 131 | string_list *channel_options_list; |
| 132 | |
| 133 | // A list of initial waypoints that are read from the command line |
| 134 | // and or flight-plan file during initialization |
| 135 | string_list *initial_waypoints; |
| 136 | |
| 137 | // Navigational Aids |
| 138 | FGTACANList *channellist; |
| 139 | |
| 140 | /// roots of Aircraft trees |
| 141 | PathList fg_aircraft_dirs; |
| 142 | SGPath catalog_aircraft_dir; |
| 143 | |
| 144 | bool haveUserSettings; |
| 145 | |
| 146 | SGPropertyNode_ptr positionLon, positionLat, positionAlt; |
| 147 | SGPropertyNode_ptr viewLon, viewLat, viewAlt; |
| 148 | SGPropertyNode_ptr orientHeading, orientPitch, orientRoll; |
| 149 | |
| 150 | SGPropertyNode_ptr referenceOffsetX, referenceOffsetY, referenceOffsetZ; |
| 151 | |
| 152 | /** |
| 153 | * helper to initialise standard properties on a new property tree |
| 154 | */ |
| 155 | void initProperties(); |
| 156 | |
| 157 | void cleanupListeners(); |
| 158 | |
| 159 | typedef std::vector<SGPropertyChangeListener*> SGPropertyChangeListenerVec; |
| 160 | SGPropertyChangeListenerVec _listeners_to_cleanup; |
| 161 | |
| 162 | SGSharedPtr<simgear::pkg::Root> _packageRoot; |
| 163 | |
| 164 | SGSubsystem *get_subsystem (const char * name) const; |
| 165 | |
| 166 | public: |
| 167 | |
| 168 | FGGlobals(); |
| 169 | virtual ~FGGlobals(); |
| 170 | |
| 171 | virtual FGRenderer *get_renderer () const; |
| 172 | void set_renderer(FGRenderer* render); |
| 173 | |
| 174 | SGSubsystemMgr *get_subsystem_mgr () const; |
| 175 | |
| 176 | template<class T> |
| 177 | T* get_subsystem() const |
| 178 | { |
| 179 | return dynamic_cast<T*>(get_subsystem(T::staticSubsystemClassId())); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | SGEventMgr *get_event_mgr () const; |
| 184 | |
| 185 | inline double get_sim_time_sec () const { return sim_time_sec; } |
| 186 | inline void inc_sim_time_sec (double dt) { sim_time_sec += dt; } |
| 187 | inline void set_sim_time_sec (double t) { sim_time_sec = t; } |
| 188 | |
| 189 | const SGPath &get_fg_root () const { return fg_root; } |
| 190 | void set_fg_root(const SGPath&root); |
| 191 | void set_texture_cache_dir(const SGPath&textureCache); |
| 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, bool afterFGRoot = false); |
| 206 | |
| 207 | /** |
| 208 | * Given a path suffix (eg 'Textures' or 'AI/Traffic'), find the |
| 209 | * first data directory which defines it. |
| 210 | */ |
| 211 | SGPath findDataPath(const std::string& pathSuffix) const; |
| 212 | |
| 213 | const SGPath &get_fg_home () const { return fg_home; } |
| 214 | void set_fg_home (const SGPath &home); |
| 215 | |
| 216 | const SGPath &get_download_dir () const { return download_dir; } |
| 217 | // The 'path' argument to set_download_dir() must come from trustworthy |
| 218 | // code, because the method grants read permissions to Nasal code for many |
| 219 | // files beneath 'path'. In particular, don't call this method with a |
| 220 | // 'path' value taken from the property tree or any other Nasal-writable |
| 221 | // place. |
| 222 | void set_download_dir (const SGPath &path); |
| 223 | |
| 224 | const SGPath &get_texture_cache_dir() const { return texture_cache_dir; } |
| 225 | |
| 226 | const SGPath &get_terrasync_dir () const { return terrasync_dir; } |
| 227 | // The 'path' argument to set_terrasync_dir() must come from trustworthy |
| 228 | // code, because the method grants read permissions to Nasal code for all |
| 229 | // files beneath 'path'. In particular, don't call this method with a |
| 230 | // 'path' value taken from the property tree or any other Nasal-writable |
| 231 | // place. |
| 232 | void set_terrasync_dir (const SGPath &path); |
| 233 | |
| 234 | const PathList &get_fg_scenery () const { return fg_scenery; } |
| 235 | const PathList &get_extra_read_allowed_paths () const { return extra_read_allowed_paths; } |
| 236 | /** |
| 237 | * Add a scenery directory |
| 238 | * |
| 239 | * This also makes the path Nasal-readable: |
| 240 | * to avoid can-read-any-file security holes, do NOT call this on paths |
| 241 | * obtained from the property tree or other Nasal-writable places |
| 242 | */ |
| 243 | void append_fg_scenery (const SGPath &scenery); |
| 244 | |
| 245 | void append_fg_scenery (const PathList &scenery); |
| 246 | |
| 247 | void clear_fg_scenery(); |
| 248 | |
| 249 | /** |
| 250 | * Allow Nasal to read a path |
| 251 | * |
| 252 | * To avoid can-read-any-file security holes, do NOT call this on paths |
| 253 | * obtained from the property tree or other Nasal-writable places |
| 254 | * |
| 255 | * Only works during initial load (before fgInitAllowedPaths) |
| 256 | */ |
| 257 | void append_read_allowed_paths (const SGPath &path); |
| 258 | |
| 259 | /** |
| 260 | * specify a path we'll prepend to the aircraft paths list if non-empty. |
| 261 | * This is used with packaged aircraft, to ensure their catalog (and hence, |
| 262 | * dependency packages) are found correctly. |
| 263 | */ |
| 264 | void set_catalog_aircraft_path(const SGPath& path); |
| 265 | |
| 266 | PathList get_aircraft_paths() const; |
| 267 | /** |
| 268 | * Add an aircraft directory |
| 269 | * |
| 270 | * This also makes the path Nasal-readable: |
| 271 | * to avoid can-read-any-file security holes, do NOT call this on paths |
| 272 | * obtained from the property tree or other Nasal-writable places |
| 273 | */ |
| 274 | void append_aircraft_path(const SGPath& path); |
| 275 | void append_aircraft_paths(const PathList& path); |
| 276 | |
| 277 | /** |
| 278 | * Given a path to an aircraft-related resource file, resolve it |
| 279 | * against the appropriate root. This means looking at the location |
| 280 | * defined by /sim/aircraft-dir, and then aircraft_path in turn, |
| 281 | * finishing with fg_root/Aircraft. |
| 282 | * |
| 283 | * if the path could not be resolved, an empty path is returned. |
| 284 | */ |
| 285 | SGPath resolve_aircraft_path(const std::string& branch) const; |
| 286 | |
| 287 | /** |
| 288 | * Same as above, but test for non 'Aircraft/' branch paths, and |
| 289 | * always resolve them against fg_root. |
| 290 | */ |
| 291 | SGPath resolve_maybe_aircraft_path(const std::string& branch) const; |
| 292 | |
| 293 | /** |
| 294 | * Search in the following directories: |
| 295 | * |
| 296 | * 1. Root directory of current aircraft (defined by /sim/aircraft-dir) |
| 297 | * 2. All aircraft directories if branch starts with Aircraft/ |
| 298 | * 3. fg_data directory |
| 299 | */ |
| 300 | SGPath resolve_resource_path(const std::string& branch) const; |
| 301 | |
| 302 | inline const std::string &get_browser () const { return browser; } |
| 303 | void set_browser (const std::string &b) { browser = b; } |
| 304 | |
| 305 | long int get_warp() const; |
| 306 | void set_warp( long int w ); |
| 307 | |
| 308 | long int get_warp_delta() const; |
| 309 | void set_warp_delta( long int d ); |
| 310 | |
| 311 | inline SGTime *get_time_params() const { return time_params; } |
| 312 | inline void set_time_params( SGTime *t ) { time_params = t; } |
| 313 | |
| 314 | inline SGMaterialLib *get_matlib() const |
| 315 | { |
| 316 | return matlib; |
| 317 | } |
| 318 | void set_matlib( SGMaterialLib *m ); |
| 319 | |
| 320 | inline SGPropertyNode *get_props () { return props; } |
| 321 | |
| 322 | /** |
| 323 | * @brief reset the property tree to new, empty tree. Ensure all |
| 324 | * subsystems are shutdown and unbound before call this. |
| 325 | */ |
| 326 | void resetPropertyRoot(); |
| 327 | |
| 328 | inline FGLocale* get_locale () { return locale; } |
| 329 | |
| 330 | inline SGCommandMgr *get_commands () { return commands; } |
| 331 | |
| 332 | SGGeod get_aircraft_position() const; |
| 333 | |
| 334 | SGVec3d get_aircraft_position_cart() const; |
| 335 | |
| 336 | void get_aircraft_orientation(double& heading, double& pitch, double& roll); |
| 337 | |
| 338 | SGGeod get_view_position() const; |
| 339 | |
| 340 | SGVec3d get_view_position_cart() const; |
| 341 | |
| 342 | SGVec3d get_ownship_reference_position_cart() const; |
| 343 | |
| 344 | inline string_list *get_channel_options_list () { |
| 345 | return channel_options_list; |
| 346 | } |
| 347 | inline void set_channel_options_list( string_list *l ) { |
| 348 | channel_options_list = l; |
| 349 | } |
| 350 | |
| 351 | inline string_list *get_initial_waypoints () { |
| 352 | return initial_waypoints; |
| 353 | } |
| 354 | |
| 355 | inline void set_initial_waypoints (string_list *list) { |
| 356 | initial_waypoints = list; |
| 357 | } |
| 358 | |
| 359 | FGViewMgr *get_viewmgr() const; |
| 360 | flightgear::View *get_current_view() const; |
| 361 | |
| 362 | FGControls *get_controls() const; |
| 363 | |
| 364 | FGScenery * get_scenery () const; |
| 365 | |
| 366 | inline FGTACANList *get_channellist() const { return channellist; } |
| 367 | inline void set_channellist( FGTACANList *c ) { channellist = c; } |
| 368 | |
| 369 | /** |
| 370 | * Return an SGPath instance for the autosave file under 'userDataPath', |
| 371 | * which defaults to $FG_HOME. |
| 372 | */ |
| 373 | SGPath autosaveFilePath(SGPath userDataPath = SGPath()) const; |
| 374 | /** |
| 375 | * Load user settings from the autosave file under 'userDataPath', |
| 376 | * which defaults to $FG_HOME. |
| 377 | */ |
| 378 | void loadUserSettings(SGPath userDatapath = SGPath()); |
| 379 | |
| 380 | /** |
| 381 | * Save user settings to the autosave file under 'userDataPath', which |
| 382 | * defaults to $FG_HOME. |
| 383 | * |
| 384 | * When calling this method, make sure the value of 'userDataPath' is |
| 385 | * trustworthy. In particular, make sure it can't be influenced by Nasal |
| 386 | * code, not even indirectly via a Nasal-writable place such as the |
| 387 | * property tree. |
| 388 | * |
| 389 | * Note: the default value is safe---if not, it would be a bug. |
| 390 | */ |
| 391 | void saveUserSettings(SGPath userDatapath = SGPath()); |
| 392 | |
| 393 | void addListenerToCleanup(SGPropertyChangeListener* l); |
| 394 | |
| 395 | simgear::pkg::Root* packageRoot(); |
| 396 | void setPackageRoot(const SGSharedPtr<simgear::pkg::Root>& p); |
| 397 | |
| 398 | /** |
| 399 | * A runtime headless mode for running without a GUI. |
| 400 | */ |
| 401 | bool is_headless(); |
| 402 | void set_headless(bool mode); |
| 403 | }; |
| 404 | |
| 405 | |
| 406 | extern FGGlobals *globals; |
| 407 | |
| 408 | |
| 409 | #endif // _GLOBALS_HXX |