1 // options.cxx -- class to handle command line options
3 // Written by Curtis Olson, started April 1998.
5 // Copyright (C) 1998 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <simgear/compiler.h>
29 #include <simgear/structure/exception.hxx>
30 #include <simgear/debug/logstream.hxx>
31 #include <simgear/timing/sg_time.hxx>
32 #include <simgear/misc/sg_dir.hxx>
34 #include <boost/foreach.hpp>
36 #include <math.h> // rint()
38 #include <stdlib.h> // atof(), atoi()
39 #include <string.h> // strcmp()
45 #include <simgear/math/sg_random.h>
46 #include <simgear/props/props_io.hxx>
47 #include <simgear/misc/sgstream.hxx>
48 #include <simgear/misc/sg_path.hxx>
49 #include <simgear/scene/material/mat.hxx>
50 #include <simgear/sound/soundmgr_openal.hxx>
51 #include <simgear/misc/strutils.hxx>
52 #include <Autopilot/route_mgr.hxx>
55 #include <GUI/MessageBox.hxx>
57 #include <Main/locale.hxx>
58 #include "globals.hxx"
59 #include "fg_init.hxx"
60 #include "fg_props.hxx"
61 #include "options.hxx"
65 #include <Viewer/viewer.hxx>
66 #include <Viewer/viewmgr.hxx>
67 #include <Environment/presets.hxx>
68 #include "AircraftDirVisitorBase.hxx"
70 #include <osg/Version>
72 #if defined( HAVE_VERSION_H ) && HAVE_VERSION_H
73 # include <Include/version.h>
74 # include <simgear/version.h>
76 # include <Include/no_version.h>
87 using namespace flightgear;
89 #define NEW_DEFAULT_MODEL_HZ 120
91 static flightgear::Options* shared_instance = NULL;
94 atof( const string& str )
96 return ::atof( str.c_str() );
100 atoi( const string& str )
102 return ::atoi( str.c_str() );
105 static int fgSetupProxy( const char *arg );
108 * Set a few fail-safe default property values.
110 * These should all be set in $FG_ROOT/preferences.xml, but just
111 * in case, we provide some initial sane values here. This method
112 * should be invoked *before* reading any init files.
114 void fgSetDefaults ()
117 // Position (deliberately out of range)
118 fgSetDouble("/position/longitude-deg", 9999.0);
119 fgSetDouble("/position/latitude-deg", 9999.0);
120 fgSetDouble("/position/altitude-ft", -9999.0);
123 fgSetDouble("/orientation/heading-deg", 9999.0);
124 fgSetDouble("/orientation/roll-deg", 0.0);
125 fgSetDouble("/orientation/pitch-deg", 0.424);
128 fgSetDouble("/velocities/uBody-fps", 0.0);
129 fgSetDouble("/velocities/vBody-fps", 0.0);
130 fgSetDouble("/velocities/wBody-fps", 0.0);
131 fgSetDouble("/velocities/speed-north-fps", 0.0);
132 fgSetDouble("/velocities/speed-east-fps", 0.0);
133 fgSetDouble("/velocities/speed-down-fps", 0.0);
134 fgSetDouble("/velocities/airspeed-kt", 0.0);
135 fgSetDouble("/velocities/mach", 0.0);
138 fgSetDouble("/sim/presets/longitude-deg", 9999.0);
139 fgSetDouble("/sim/presets/latitude-deg", 9999.0);
140 fgSetDouble("/sim/presets/altitude-ft", -9999.0);
142 fgSetDouble("/sim/presets/heading-deg", 9999.0);
143 fgSetDouble("/sim/presets/roll-deg", 0.0);
144 fgSetDouble("/sim/presets/pitch-deg", 0.424);
146 fgSetString("/sim/presets/speed-set", "knots");
147 fgSetDouble("/sim/presets/airspeed-kt", 0.0);
148 fgSetDouble("/sim/presets/mach", 0.0);
149 fgSetDouble("/sim/presets/uBody-fps", 0.0);
150 fgSetDouble("/sim/presets/vBody-fps", 0.0);
151 fgSetDouble("/sim/presets/wBody-fps", 0.0);
152 fgSetDouble("/sim/presets/speed-north-fps", 0.0);
153 fgSetDouble("/sim/presets/speed-east-fps", 0.0);
154 fgSetDouble("/sim/presets/speed-down-fps", 0.0);
156 fgSetBool("/sim/presets/onground", true);
157 fgSetBool("/sim/presets/trim", false);
160 fgSetBool("/sim/startup/splash-screen", true);
161 // we want mouse-pointer to have an undefined value if nothing is
162 // specified so we can do the right thing for voodoo-1/2 cards.
163 // fgSetString("/sim/startup/mouse-pointer", "disabled");
164 fgSetString("/sim/control-mode", "joystick");
165 fgSetBool("/controls/flight/auto-coordination", false);
166 fgSetString("/sim/logging/priority", "alert");
169 fgSetBool("/sim/hud/color/antialiased", false);
170 fgSetBool("/sim/hud/enable3d[1]", true);
171 fgSetBool("/sim/hud/visibility[1]", false);
172 fgSetBool("/sim/panel/visibility", true);
173 fgSetBool("/sim/sound/enabled", true);
174 fgSetBool("/sim/sound/working", true);
175 fgSetBool("/sim/fgcom/enabled", false);
177 // Flight Model options
178 fgSetString("/sim/flight-model", "jsb");
179 fgSetString("/sim/aero", "c172");
180 fgSetInt("/sim/model-hz", NEW_DEFAULT_MODEL_HZ);
181 fgSetDouble("/sim/speed-up", 1.0);
184 fgSetString("/sim/rendering/fog", "nicest");
185 fgSetBool("/environment/clouds/status", true);
186 fgSetBool("/sim/startup/fullscreen", false);
187 fgSetBool("/sim/rendering/shading", true);
188 fgSetBool("/sim/rendering/skyblend", true);
189 fgSetBool("/sim/rendering/textures", true);
190 fgTie( "/sim/rendering/filtering", SGGetTextureFilter, SGSetTextureFilter, false);
191 fgSetInt("/sim/rendering/filtering", 1);
192 fgSetBool("/sim/rendering/wireframe", false);
193 fgSetBool("/sim/rendering/horizon-effect", false);
194 fgSetBool("/sim/rendering/enhanced-lighting", false);
195 fgSetBool("/sim/rendering/distance-attenuation", false);
196 fgSetBool("/sim/rendering/specular-highlight", true);
197 fgSetString("/sim/rendering/materials-file", "materials.xml");
198 fgSetInt("/sim/startup/xsize", 1024);
199 fgSetInt("/sim/startup/ysize", 768);
200 fgSetInt("/sim/rendering/bits-per-pixel", 32);
201 fgSetString("/sim/view-mode", "pilot");
202 fgSetDouble("/sim/current-view/heading-offset-deg", 0);
205 fgSetString("/sim/startup/units", "feet");
206 fgSetString("/sim/hud/frame-stat-type", "tris");
209 fgSetInt("/sim/startup/time-offset", 0);
210 fgSetString("/sim/startup/time-offset-type", "system-offset");
211 fgSetLong("/sim/time/cur-time-override", 0);
214 fgSetBool("/sim/freeze/master", false);
215 fgSetBool("/sim/freeze/position", false);
216 fgSetBool("/sim/freeze/clock", false);
217 fgSetBool("/sim/freeze/fuel", false);
219 fgSetString("/sim/multiplay/callsign", "callsign");
220 fgSetString("/sim/multiplay/rxhost", "");
221 fgSetString("/sim/multiplay/txhost", "");
222 fgSetInt("/sim/multiplay/rxport", 0);
223 fgSetInt("/sim/multiplay/txport", 0);
225 SGPropertyNode* v = globals->get_props()->getNode("/sim/version", true);
226 v->setValueReadOnly("flightgear", FLIGHTGEAR_VERSION);
227 v->setValueReadOnly("simgear", SG_STRINGIZE(SIMGEAR_VERSION));
228 v->setValueReadOnly("openscenegraph", osgGetVersion());
229 v->setValueReadOnly("openscenegraph-thread-safe-reference-counting",
230 osg::Referenced::getThreadSafeReferenceCounting());
231 v->setValueReadOnly("revision", REVISION);
232 v->setValueReadOnly("build-number", HUDSON_BUILD_NUMBER);
233 v->setValueReadOnly("build-id", HUDSON_BUILD_ID);
234 v->setValueReadOnly("hla-support", bool(FG_HAVE_HLA));
236 char* envp = ::getenv( "http_proxy" );
238 fgSetupProxy( envp );
241 ///////////////////////////////////////////////////////////////////////////////
242 // helper object to implement the --show-aircraft command.
243 // resides here so we can share the fgFindAircraftInDir template above,
244 // and hence ensure this command lists exectly the same aircraft as the normal
246 class ShowAircraft : public AircraftDirVistorBase
251 _minStatus = getNumMaturity(fgGetString("/sim/aircraft-min-status", "all"));
255 void show(const SGPath& path)
259 simgear::requestConsole(); // ensure console is shown on Windows
261 std::sort(_aircraft.begin(), _aircraft.end(), ciLessLibC());
262 cout << "Available aircraft:" << endl;
263 for ( unsigned int i = 0; i < _aircraft.size(); i++ ) {
264 cout << _aircraft[i] << endl;
269 virtual VisitResult visit(const SGPath& path)
273 readProperties(path.str(), &root);
274 } catch (sg_exception& ) {
275 return VISIT_CONTINUE;
280 descStr += path.file();
281 // trim common suffix from file names
282 int nPos = descStr.rfind("-set.xml");
283 if (nPos == (int)(descStr.size() - 8)) {
284 descStr.resize(nPos);
287 SGPropertyNode *node = root.getNode("sim");
289 SGPropertyNode* desc = node->getNode("description");
290 // if a status tag is found, read it in
291 if (node->hasValue("status")) {
292 maturity = getNumMaturity(node->getStringValue("status"));
296 if (descStr.size() <= 27+3) {
297 descStr.append(29+3-descStr.size(), ' ');
300 descStr.append( 32, ' ');
302 descStr += desc->getStringValue();
304 } // of have 'sim' node
306 if (maturity >= _minStatus) {
307 _aircraft.push_back(descStr);
310 return VISIT_CONTINUE;
314 int getNumMaturity(const char * str)
316 // changes should also be reflected in $FG_ROOT/data/options.xml &
317 // $FG_ROOT/data/Translations/string-default.xml
318 const char* levels[] = {"alpha","beta","early-production","production"};
320 if (!strcmp(str, "all")) {
324 for (size_t i=0; i<(sizeof(levels)/sizeof(levels[0]));i++)
325 if (strcmp(str,levels[i])==0)
331 // recommended in Meyers, Effective STL when internationalization and embedded
332 // NULLs aren't an issue. Much faster than the STL or Boost lex versions.
333 struct ciLessLibC : public std::binary_function<string, string, bool>
335 bool operator()(const std::string &lhs, const std::string &rhs) const
337 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0 ? 1 : 0;
342 string_list _aircraft;
346 * Search in the current directory, and in on directory deeper
347 * for <aircraft>-set.xml configuration files and show the aircaft name
348 * and the contents of the<description> tag in a sorted manner.
350 * @parampath the directory to search for configuration files
352 void fgShowAircraft(const SGPath &path)
358 cout << "Hit a key to continue..." << endl;
365 parse_wind (const string &wind, double * min_hdg, double * max_hdg,
366 double * speed, double * gust)
368 string::size_type pos = wind.find('@');
369 if (pos == string::npos)
371 string dir = wind.substr(0, pos);
372 string spd = wind.substr(pos+1);
374 if (pos == string::npos) {
375 *min_hdg = *max_hdg = atof(dir.c_str());
377 *min_hdg = atof(dir.substr(0,pos).c_str());
378 *max_hdg = atof(dir.substr(pos+1).c_str());
381 if (pos == string::npos) {
382 *speed = *gust = atof(spd.c_str());
384 *speed = atof(spd.substr(0,pos).c_str());
385 *gust = atof(spd.substr(pos+1).c_str());
391 parseIntValue(char** ppParserPos, int* pValue,int min, int max, const char* field, const char* argument)
393 if ( !strlen(*ppParserPos) )
399 while ( isdigit((*ppParserPos)[0]) && (i<255) )
401 num[i] = (*ppParserPos)[0];
407 switch ((*ppParserPos)[0])
415 SG_LOG(SG_GENERAL, SG_ALERT, "Illegal character in time string for " << field << ": '" <<
416 (*ppParserPos)[0] << "'.");
417 // invalid field - skip rest of string to avoid further errors
418 while ((*ppParserPos)[0])
426 int value = atoi(num);
427 if ((value < min)||(value > max))
429 SG_LOG(SG_GENERAL, SG_ALERT, "Invalid " << field << " in '" << argument <<
430 "'. Valid range is " << min << "-" << max << ".");
440 // parse a time string ([+/-]%f[:%f[:%f]]) into hours
442 parse_time(const string& time_in) {
443 char *time_str, num[256];
444 double hours, minutes, seconds;
449 time_str = (char *)time_in.c_str();
451 // printf("parse_time(): %s\n", time_str);
454 if ( strlen(time_str) ) {
455 if ( time_str[0] == '+' ) {
458 } else if ( time_str[0] == '-' ) {
463 // printf("sign = %d\n", sign);
466 if ( strlen(time_str) ) {
468 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
469 num[i] = time_str[0];
473 if ( time_str[0] == ':' ) {
478 // printf("hours = %.2lf\n", hours);
484 if ( strlen(time_str) ) {
486 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
487 num[i] = time_str[0];
491 if ( time_str[0] == ':' ) {
496 // printf("minutes = %.2lf\n", minutes);
498 result += minutes / 60.0;
502 if ( strlen(time_str) ) {
504 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
505 num[i] = time_str[0];
511 // printf("seconds = %.2lf\n", seconds);
513 result += seconds / 3600.0;
516 SG_LOG( SG_GENERAL, SG_INFO, " parse_time() = " << sign * result );
518 return(sign * result);
521 // parse a date string (yyyy:mm:dd:hh:mm:ss) into a time_t (seconds)
523 parse_date( const string& date, const char* timeType)
525 struct tm gmt,*pCurrentTime;
526 int year,month,day,hour,minute,second;
527 char *argument, *date_str;
530 CurrentTime.update(SGGeod(),0,0);
532 // FIXME This should obtain system/aircraft/GMT time depending on timeType
533 pCurrentTime = CurrentTime.getGmt();
535 // initialize all fields with current time
536 year = pCurrentTime->tm_year + 1900;
537 month = pCurrentTime->tm_mon + 1;
538 day = pCurrentTime->tm_mday;
539 hour = pCurrentTime->tm_hour;
540 minute = pCurrentTime->tm_min;
541 second = pCurrentTime->tm_sec;
543 argument = (char *)date.c_str();
546 // start with parsing year
547 if (!strlen(date_str) ||
548 !parseIntValue(&date_str,&year,0,9999,"year",argument))
555 SG_LOG(SG_GENERAL, SG_ALERT, "Invalid year '" << year << "'. Use 1970 or later.");
559 parseIntValue(&date_str, &month, 1, 12, "month", argument);
560 parseIntValue(&date_str, &day, 1, 31, "day", argument);
561 parseIntValue(&date_str, &hour, 0, 23, "hour", argument);
562 parseIntValue(&date_str, &minute, 0, 59, "minute", argument);
563 parseIntValue(&date_str, &second, 0, 59, "second", argument);
569 gmt.tm_mon = month - 1;
570 gmt.tm_year = year -1900;
571 gmt.tm_isdst = 0; // ignore daylight savings time for the moment
573 time_t theTime = sgTimeGetGMT( gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
574 gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
576 SG_LOG(SG_GENERAL, SG_INFO, "Configuring startup time to " << ctime(&theTime));
582 // parse angle in the form of [+/-]ddd:mm:ss into degrees
584 parse_degree( const string& degree_str) {
585 double result = parse_time( degree_str );
587 // printf("Degree = %.4f\n", result);
593 // parse time offset string into seconds
595 parse_time_offset( const string& time_str) {
598 // printf("time offset = %s\n", time_str);
601 result = (int)rint(parse_time(time_str) * 3600.0);
603 result = (int)(parse_time(time_str) * 3600.0);
606 // printf("parse_time_offset(): %d\n", result);
612 // Parse --fov=x.xx type option
614 parse_fov( const string& arg ) {
615 double fov = atof(arg);
617 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
618 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
620 fgSetDouble("/sim/view[0]/config/default-field-of-view-deg", fov);
622 // printf("parse_fov(): result = %.4f\n", fov);
628 // Parse I/O channel option
630 // Format is "--protocol=medium,direction,hz,medium_options,..."
632 // protocol = { native, nmea, garmin, AV400, AV400Sim, fgfs, rul, pve, etc. }
633 // medium = { serial, socket, file, etc. }
634 // direction = { in, out, bi }
635 // hz = number of times to process channel per second (floating
636 // point values are ok.
638 // Serial example "--nmea=serial,dir,hz,device,baud" where
640 // device = OS device name of serial line to be open()'ed
641 // baud = {300, 1200, 2400, ..., 230400}
643 // Socket exacmple "--native=socket,dir,hz,machine,port,style" where
645 // machine = machine name or ip address if client (leave empty if server)
646 // port = port, leave empty to let system choose
647 // style = tcp or udp
649 // File example "--garmin=file,dir,hz,filename" where
651 // filename = file system file name
654 add_channel( const string& type, const string& channel_str ) {
655 // This check is neccessary to prevent fgviewer from segfaulting when given
656 // weird options. (It doesn't run the full initailization)
657 if(!globals->get_channel_options_list())
659 SG_LOG(SG_GENERAL, SG_ALERT, "Option " << type << "=" << channel_str
663 SG_LOG(SG_GENERAL, SG_INFO, "Channel string = " << channel_str );
664 globals->get_channel_options_list()->push_back( type + "," + channel_str );
671 fgSetString("/sim/presets/airport-id", "");
672 fgSetString("/sim/presets/vor-id", "");
673 fgSetString("/sim/presets/ndb-id", "");
674 fgSetString("/sim/presets/carrier", "");
675 fgSetString("/sim/presets/parkpos", "");
676 fgSetString("/sim/presets/fix", "");
680 fgOptVOR( const char * arg )
683 fgSetString("/sim/presets/vor-id", arg);
684 return FG_OPTIONS_OK;
688 fgOptNDB( const char * arg )
691 fgSetString("/sim/presets/ndb-id", arg);
692 return FG_OPTIONS_OK;
696 fgOptCarrier( const char * arg )
699 fgSetString("/sim/presets/carrier", arg);
700 return FG_OPTIONS_OK;
704 fgOptParkpos( const char * arg )
706 fgSetString("/sim/presets/parkpos", arg);
707 return FG_OPTIONS_OK;
711 fgOptFIX( const char * arg )
714 fgSetString("/sim/presets/fix", arg);
715 return FG_OPTIONS_OK;
719 fgOptLon( const char *arg )
722 fgSetDouble("/sim/presets/longitude-deg", parse_degree( arg ));
723 fgSetDouble("/position/longitude-deg", parse_degree( arg ));
724 return FG_OPTIONS_OK;
728 fgOptLat( const char *arg )
731 fgSetDouble("/sim/presets/latitude-deg", parse_degree( arg ));
732 fgSetDouble("/position/latitude-deg", parse_degree( arg ));
733 return FG_OPTIONS_OK;
737 fgOptAltitude( const char *arg )
739 fgSetBool("/sim/presets/onground", false);
740 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
741 fgSetDouble("/sim/presets/altitude-ft", atof( arg ));
743 fgSetDouble("/sim/presets/altitude-ft",
744 atof( arg ) * SG_METER_TO_FEET);
745 return FG_OPTIONS_OK;
749 fgOptUBody( const char *arg )
751 fgSetString("/sim/presets/speed-set", "UVW");
752 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
753 fgSetDouble("/sim/presets/uBody-fps", atof( arg ));
755 fgSetDouble("/sim/presets/uBody-fps",
756 atof( arg ) * SG_METER_TO_FEET);
757 return FG_OPTIONS_OK;
761 fgOptVBody( const char *arg )
763 fgSetString("/sim/presets/speed-set", "UVW");
764 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
765 fgSetDouble("/sim/presets/vBody-fps", atof( arg ));
767 fgSetDouble("/sim/presets/vBody-fps",
768 atof( arg ) * SG_METER_TO_FEET);
769 return FG_OPTIONS_OK;
773 fgOptWBody( const char *arg )
775 fgSetString("/sim/presets/speed-set", "UVW");
776 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
777 fgSetDouble("/sim/presets/wBody-fps", atof(arg));
779 fgSetDouble("/sim/presets/wBody-fps",
780 atof(arg) * SG_METER_TO_FEET);
781 return FG_OPTIONS_OK;
785 fgOptVNorth( const char *arg )
787 fgSetString("/sim/presets/speed-set", "NED");
788 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
789 fgSetDouble("/sim/presets/speed-north-fps", atof( arg ));
791 fgSetDouble("/sim/presets/speed-north-fps",
792 atof( arg ) * SG_METER_TO_FEET);
793 return FG_OPTIONS_OK;
797 fgOptVEast( const char *arg )
799 fgSetString("/sim/presets/speed-set", "NED");
800 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
801 fgSetDouble("/sim/presets/speed-east-fps", atof(arg));
803 fgSetDouble("/sim/presets/speed-east-fps",
804 atof(arg) * SG_METER_TO_FEET);
805 return FG_OPTIONS_OK;
809 fgOptVDown( const char *arg )
811 fgSetString("/sim/presets/speed-set", "NED");
812 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
813 fgSetDouble("/sim/presets/speed-down-fps", atof(arg));
815 fgSetDouble("/sim/presets/speed-down-fps",
816 atof(arg) * SG_METER_TO_FEET);
817 return FG_OPTIONS_OK;
821 fgOptVc( const char *arg )
823 // fgSetString("/sim/presets/speed-set", "knots");
824 // fgSetDouble("/velocities/airspeed-kt", atof(arg.substr(5)));
825 fgSetString("/sim/presets/speed-set", "knots");
826 fgSetDouble("/sim/presets/airspeed-kt", atof(arg));
827 return FG_OPTIONS_OK;
831 fgOptMach( const char *arg )
833 fgSetString("/sim/presets/speed-set", "mach");
834 fgSetDouble("/sim/presets/mach", atof(arg));
835 return FG_OPTIONS_OK;
839 fgOptRoc( const char *arg )
841 fgSetDouble("/sim/presets/vertical-speed-fps", atof(arg)/60);
842 return FG_OPTIONS_OK;
846 fgOptFgScenery( const char *arg )
848 globals->append_fg_scenery(arg);
849 return FG_OPTIONS_OK;
853 fgOptFov( const char *arg )
856 return FG_OPTIONS_OK;
860 fgOptGeometry( const char *arg )
862 bool geometry_ok = true;
863 int xsize = 0, ysize = 0;
864 string geometry = arg;
865 string::size_type i = geometry.find('x');
867 if (i != string::npos) {
868 xsize = atoi(geometry.substr(0, i));
869 ysize = atoi(geometry.substr(i+1));
874 if ( xsize <= 0 || ysize <= 0 ) {
880 if ( !geometry_ok ) {
881 SG_LOG( SG_GENERAL, SG_ALERT, "Unknown geometry: " << geometry );
882 SG_LOG( SG_GENERAL, SG_ALERT,
883 "Setting geometry to " << xsize << 'x' << ysize << '\n');
885 SG_LOG( SG_GENERAL, SG_INFO,
886 "Setting geometry to " << xsize << 'x' << ysize << '\n');
887 fgSetInt("/sim/startup/xsize", xsize);
888 fgSetInt("/sim/startup/ysize", ysize);
890 return FG_OPTIONS_OK;
894 fgOptBpp( const char *arg )
896 string bits_per_pix = arg;
897 if ( bits_per_pix == "16" ) {
898 fgSetInt("/sim/rendering/bits-per-pixel", 16);
899 } else if ( bits_per_pix == "24" ) {
900 fgSetInt("/sim/rendering/bits-per-pixel", 24);
901 } else if ( bits_per_pix == "32" ) {
902 fgSetInt("/sim/rendering/bits-per-pixel", 32);
904 SG_LOG(SG_GENERAL, SG_ALERT, "Unsupported bpp " << bits_per_pix);
906 return FG_OPTIONS_OK;
910 fgOptTimeOffset( const char *arg )
912 fgSetLong("/sim/startup/time-offset",
913 parse_time_offset( arg ));
914 fgSetString("/sim/startup/time-offset-type", "system-offset");
915 return FG_OPTIONS_OK;
919 fgOptStartDateSys( const char *arg )
921 long int theTime = parse_date( arg, "system" );
924 fgSetLong("/sim/startup/time-offset", theTime);
925 fgSetString("/sim/startup/time-offset-type", "system");
927 return FG_OPTIONS_OK;
931 fgOptStartDateLat( const char *arg )
933 long int theTime = parse_date( arg, "latitude" );
936 fgSetLong("/sim/startup/time-offset", theTime);
937 fgSetString("/sim/startup/time-offset-type", "latitude");
939 return FG_OPTIONS_OK;
943 fgOptStartDateGmt( const char *arg )
945 long int theTime = parse_date( arg, "gmt" );
948 fgSetLong("/sim/startup/time-offset", theTime);
949 fgSetString("/sim/startup/time-offset-type", "gmt");
951 return FG_OPTIONS_OK;
955 fgSetupProxy( const char *arg )
957 string options = simgear::strutils::strip( arg );
958 string host, port, auth;
959 string::size_type pos;
961 // this is NURLP - NURLP is not an url parser
962 if( simgear::strutils::starts_with( options, "http://" ) )
963 options = options.substr( 7 );
964 if( simgear::strutils::ends_with( options, "/" ) )
965 options = options.substr( 0, options.length() - 1 );
967 host = port = auth = "";
968 if ((pos = options.find("@")) != string::npos)
969 auth = options.substr(0, pos++);
973 host = options.substr(pos, options.size());
974 if ((pos = host.find(":")) != string::npos) {
975 port = host.substr(++pos, host.size());
976 host.erase(--pos, host.size());
979 fgSetString("/sim/presets/proxy/host", host.c_str());
980 fgSetString("/sim/presets/proxy/port", port.c_str());
981 fgSetString("/sim/presets/proxy/authentication", auth.c_str());
983 return FG_OPTIONS_OK;
987 fgOptTraceRead( const char *arg )
990 SG_LOG(SG_GENERAL, SG_INFO, "Tracing reads for property " << name);
991 fgGetNode(name.c_str(), true)
992 ->setAttribute(SGPropertyNode::TRACE_READ, true);
993 return FG_OPTIONS_OK;
997 fgOptLogLevel( const char *arg )
999 fgSetString("/sim/logging/priority", arg);
1000 setLoggingPriority(arg);
1002 return FG_OPTIONS_OK;
1006 fgOptLogClasses( const char *arg )
1008 fgSetString("/sim/logging/classes", arg);
1009 setLoggingClasses (arg);
1011 return FG_OPTIONS_OK;
1015 fgOptTraceWrite( const char *arg )
1018 SG_LOG(SG_GENERAL, SG_INFO, "Tracing writes for property " << name);
1019 fgGetNode(name.c_str(), true)
1020 ->setAttribute(SGPropertyNode::TRACE_WRITE, true);
1021 return FG_OPTIONS_OK;
1025 fgOptViewOffset( const char *arg )
1027 // $$$ begin - added VS Renganathan, 14 Oct 2K
1028 // for multi-window outside window imagery
1029 string woffset = arg;
1030 double default_view_offset = 0.0;
1031 if ( woffset == "LEFT" ) {
1032 default_view_offset = SGD_PI * 0.25;
1033 } else if ( woffset == "RIGHT" ) {
1034 default_view_offset = SGD_PI * 1.75;
1035 } else if ( woffset == "CENTER" ) {
1036 default_view_offset = 0.00;
1038 default_view_offset = atof( woffset.c_str() ) * SGD_DEGREES_TO_RADIANS;
1040 /* apparently not used (CLO, 11 Jun 2002)
1041 FGViewer *pilot_view =
1042 (FGViewer *)globals->get_viewmgr()->get_view( 0 ); */
1043 // this will work without calls to the viewer...
1044 fgSetDouble( "/sim/current-view/heading-offset-deg",
1045 default_view_offset * SGD_RADIANS_TO_DEGREES );
1046 // $$$ end - added VS Renganathan, 14 Oct 2K
1047 return FG_OPTIONS_OK;
1051 fgOptVisibilityMeters( const char *arg )
1053 Environment::Presets::VisibilitySingleton::instance()->preset( atof( arg ) );
1054 return FG_OPTIONS_OK;
1058 fgOptVisibilityMiles( const char *arg )
1060 Environment::Presets::VisibilitySingleton::instance()->preset( atof( arg ) * 5280.0 * SG_FEET_TO_METER );
1061 return FG_OPTIONS_OK;
1065 fgOptRandomWind( const char *arg )
1067 double min_hdg = sg_random() * 360.0;
1068 double max_hdg = min_hdg + (20 - sqrt(sg_random() * 400));
1069 double speed = sg_random() * sg_random() * 40;
1070 double gust = speed + (10 - sqrt(sg_random() * 100));
1071 Environment::Presets::WindSingleton::instance()->preset(min_hdg, max_hdg, speed, gust);
1072 return FG_OPTIONS_OK;
1076 fgOptWind( const char *arg )
1078 double min_hdg = 0.0, max_hdg = 0.0, speed = 0.0, gust = 0.0;
1079 if (!parse_wind( arg, &min_hdg, &max_hdg, &speed, &gust)) {
1080 SG_LOG( SG_GENERAL, SG_ALERT, "bad wind value " << arg );
1081 return FG_OPTIONS_ERROR;
1083 Environment::Presets::WindSingleton::instance()->preset(min_hdg, max_hdg, speed, gust);
1084 return FG_OPTIONS_OK;
1088 fgOptTurbulence( const char *arg )
1090 Environment::Presets::TurbulenceSingleton::instance()->preset( atof(arg) );
1091 return FG_OPTIONS_OK;
1095 fgOptCeiling( const char *arg )
1097 double elevation, thickness;
1099 string::size_type pos = spec.find(':');
1100 if (pos == string::npos) {
1101 elevation = atof(spec.c_str());
1104 elevation = atof(spec.substr(0, pos).c_str());
1105 thickness = atof(spec.substr(pos + 1).c_str());
1107 Environment::Presets::CeilingSingleton::instance()->preset( elevation, thickness );
1108 return FG_OPTIONS_OK;
1112 fgOptWp( const char *arg )
1114 string_list *waypoints = globals->get_initial_waypoints();
1116 waypoints = new string_list;
1117 globals->set_initial_waypoints(waypoints);
1119 waypoints->push_back(arg);
1120 return FG_OPTIONS_OK;
1124 fgOptConfig( const char *arg )
1128 readProperties(file, globals->get_props());
1129 } catch (const sg_exception &e) {
1130 string message = "Error loading config file: ";
1131 message += e.getFormattedMessage() + e.getOrigin();
1132 SG_LOG(SG_INPUT, SG_ALERT, message);
1133 return FG_OPTIONS_ERROR;
1135 return FG_OPTIONS_OK;
1139 parse_colon (const string &s, double * val1, double * val2)
1141 string::size_type pos = s.find(':');
1142 if (pos == string::npos) {
1146 *val1 = atof(s.substr(0, pos).c_str());
1147 *val2 = atof(s.substr(pos+1).c_str());
1154 fgOptFailure( const char * arg )
1158 fgSetBool("/systems/pitot/serviceable", false);
1159 } else if (a == "static") {
1160 fgSetBool("/systems/static/serviceable", false);
1161 } else if (a == "vacuum") {
1162 fgSetBool("/systems/vacuum/serviceable", false);
1163 } else if (a == "electrical") {
1164 fgSetBool("/systems/electrical/serviceable", false);
1166 SG_LOG(SG_INPUT, SG_ALERT, "Unknown failure mode: " << a);
1167 return FG_OPTIONS_ERROR;
1170 return FG_OPTIONS_OK;
1175 fgOptNAV1( const char * arg )
1177 double radial, freq;
1178 if (parse_colon(arg, &radial, &freq))
1179 fgSetDouble("/instrumentation/nav[0]/radials/selected-deg", radial);
1180 fgSetDouble("/instrumentation/nav[0]/frequencies/selected-mhz", freq);
1181 return FG_OPTIONS_OK;
1185 fgOptNAV2( const char * arg )
1187 double radial, freq;
1188 if (parse_colon(arg, &radial, &freq))
1189 fgSetDouble("/instrumentation/nav[1]/radials/selected-deg", radial);
1190 fgSetDouble("/instrumentation/nav[1]/frequencies/selected-mhz", freq);
1191 return FG_OPTIONS_OK;
1195 fgOptADF1( const char * arg )
1198 if (parse_colon(arg, &rot, &freq))
1199 fgSetDouble("/instrumentation/adf[0]/rotation-deg", rot);
1200 fgSetDouble("/instrumentation/adf[0]/frequencies/selected-khz", freq);
1201 return FG_OPTIONS_OK;
1205 fgOptADF2( const char * arg )
1208 if (parse_colon(arg, &rot, &freq))
1209 fgSetDouble("/instrumentation/adf[1]/rotation-deg", rot);
1210 fgSetDouble("/instrumentation/adf[1]/frequencies/selected-khz", freq);
1211 return FG_OPTIONS_OK;
1215 fgOptDME( const char *arg )
1218 if (opt == "nav1") {
1219 fgSetInt("/instrumentation/dme/switch-position", 1);
1220 fgSetString("/instrumentation/dme/frequencies/source",
1221 "/instrumentation/nav[0]/frequencies/selected-mhz");
1222 } else if (opt == "nav2") {
1223 fgSetInt("/instrumentation/dme/switch-position", 3);
1224 fgSetString("/instrumentation/dme/frequencies/source",
1225 "/instrumentation/nav[1]/frequencies/selected-mhz");
1227 double frequency = atof(arg);
1230 SG_LOG(SG_INPUT, SG_ALERT, "Invalid DME frequency: '" << arg << "'.");
1231 return FG_OPTIONS_ERROR;
1233 fgSetInt("/instrumentation/dme/switch-position", 2);
1234 fgSetString("/instrumentation/dme/frequencies/source",
1235 "/instrumentation/dme/frequencies/selected-mhz");
1236 fgSetDouble("/instrumentation/dme/frequencies/selected-mhz", frequency);
1238 return FG_OPTIONS_OK;
1242 fgOptLivery( const char *arg )
1245 string livery_path = "livery/" + opt;
1246 fgSetString("/sim/model/texture-path", livery_path.c_str() );
1247 return FG_OPTIONS_OK;
1251 fgOptScenario( const char *arg )
1253 SGPropertyNode_ptr ai_node = fgGetNode( "/sim/ai", true );
1254 vector<SGPropertyNode_ptr> scenarii = ai_node->getChildren( "scenario" );
1256 for ( size_t i = 0; i < scenarii.size(); ++i ) {
1257 int ind = scenarii[i]->getIndex();
1258 if ( index < ind ) {
1262 SGPropertyNode_ptr scenario = ai_node->getNode( "scenario", index + 1, true );
1263 scenario->setStringValue( arg );
1264 return FG_OPTIONS_OK;
1268 fgOptRunway( const char *arg )
1270 fgSetString("/sim/presets/runway", arg );
1271 fgSetBool("/sim/presets/runway-requested", true );
1272 return FG_OPTIONS_OK;
1276 fgOptParking( const char *arg )
1278 cerr << "Processing argument " << arg << endl;
1279 fgSetString("/sim/presets/parking", arg );
1280 fgSetBool ("/sim/presets/parking-requested", true );
1281 return FG_OPTIONS_OK;
1285 fgOptVersion( const char *arg )
1287 cerr << "FlightGear version: " << FLIGHTGEAR_VERSION << endl;
1288 cerr << "Revision: " << REVISION << endl;
1289 cerr << "Build-Id: " << HUDSON_BUILD_ID << endl;
1290 cerr << "FG_ROOT=" << globals->get_fg_root() << endl;
1291 cerr << "FG_HOME=" << globals->get_fg_home() << endl;
1292 cerr << "FG_SCENERY=";
1295 string_list scn = globals->get_fg_scenery();
1296 for (string_list::const_iterator it = scn.begin(); it != scn.end(); it++)
1298 if (didsome) cerr << ":";
1303 cerr << "SimGear version: " << SG_STRINGIZE(SIMGEAR_VERSION) << endl;
1304 cerr << "PLIB version: " << PLIB_VERSION << endl;
1305 return FG_OPTIONS_EXIT;
1309 fgOptCallSign(const char * arg)
1313 strncpy(callsign,arg,10);
1315 for (i=0;callsign[i];i++)
1317 char c = callsign[i];
1318 if (c >= 'A' && c <= 'Z') continue;
1319 if (c >= 'a' && c <= 'z') continue;
1320 if (c >= '0' && c <= '9') continue;
1321 if (c == '-' || c == '_') continue;
1322 // convert any other illegal characters
1325 fgSetString("sim/multiplay/callsign", callsign );
1326 return FG_OPTIONS_OK;
1330 fgOptIgnoreAutosave(const char* arg)
1332 fgSetBool("/sim/startup/ignore-autosave", true);
1333 // don't overwrite autosave on exit
1334 fgSetBool("/sim/startup/save-on-exit", false);
1335 return FG_OPTIONS_OK;
1338 // Set a property for the --prop: option. Syntax: --prop:[<type>:]<name>=<value>
1339 // <type> can be "double" etc. but also only the first letter "d".
1340 // Examples: --prop:alpha=1 --prop:bool:beta=true --prop:d:gamma=0.123
1342 fgOptSetProperty(const char* raw)
1345 string::size_type pos = arg.find('=');
1346 if (pos == arg.npos || pos == 0 || pos + 1 == arg.size())
1347 return FG_OPTIONS_ERROR;
1349 string name = arg.substr(0, pos);
1350 string value = arg.substr(pos + 1);
1352 pos = name.find(':');
1354 if (pos != name.npos && pos != 0 && pos + 1 != name.size()) {
1355 type = name.substr(0, pos);
1356 name = name.substr(pos + 1);
1358 SGPropertyNode *n = fgGetNode(name.c_str(), true);
1360 bool writable = n->getAttribute(SGPropertyNode::WRITE);
1362 n->setAttribute(SGPropertyNode::WRITE, true);
1366 ret = n->setUnspecifiedValue(value.c_str());
1367 else if (type == "s" || type == "string")
1368 ret = n->setStringValue(value.c_str());
1369 else if (type == "d" || type == "double")
1370 ret = n->setDoubleValue(strtod(value.c_str(), 0));
1371 else if (type == "f" || type == "float")
1372 ret = n->setFloatValue(atof(value.c_str()));
1373 else if (type == "l" || type == "long")
1374 ret = n->setLongValue(strtol(value.c_str(), 0, 0));
1375 else if (type == "i" || type == "int")
1376 ret = n->setIntValue(atoi(value.c_str()));
1377 else if (type == "b" || type == "bool")
1378 ret = n->setBoolValue(value == "true" || atoi(value.c_str()) != 0);
1381 n->setAttribute(SGPropertyNode::WRITE, false);
1382 return ret ? FG_OPTIONS_OK : FG_OPTIONS_ERROR;
1388 option has_param type property b_param s_param func
1391 option : name of the option
1392 has_param : option is --name=value if true or --name if false
1393 type : OPTION_BOOL - property is a boolean
1394 OPTION_STRING - property is a string
1395 OPTION_DOUBLE - property is a double
1396 OPTION_INT - property is an integer
1397 OPTION_CHANNEL - name of option is the name of a channel
1398 OPTION_FUNC - the option trigger a function
1399 b_param : if type==OPTION_BOOL,
1400 value set to the property (has_param is false for boolean)
1401 s_param : if type==OPTION_STRING,
1402 value set to the property if has_param is false
1403 func : function called if type==OPTION_FUNC. if has_param is true,
1404 the value is passed to the function as a string, otherwise,
1407 For OPTION_DOUBLE and OPTION_INT, the parameter value is converted into a
1408 double or an integer and set to the property.
1410 For OPTION_CHANNEL, add_channel is called with the parameter value as the
1414 enum OptionType { OPTION_BOOL = 0, OPTION_STRING, OPTION_DOUBLE, OPTION_INT, OPTION_CHANNEL, OPTION_FUNC, OPTION_IGNORE };
1415 const int OPTION_MULTI = 1 << 17;
1421 const char *property;
1423 const char *s_param;
1424 int (*func)( const char * );
1425 } fgOptionArray[] = {
1427 {"language", true, OPTION_IGNORE, "", false, "", 0 },
1428 {"console", false, OPTION_IGNORE, "", false, "", 0 },
1429 {"disable-rembrandt", false, OPTION_BOOL, "/sim/rendering/rembrandt/enabled", false, "", 0 },
1430 {"enable-rembrandt", false, OPTION_BOOL, "/sim/rendering/rembrandt/enabled", true, "", 0 },
1431 {"renderer", true, OPTION_STRING, "/sim/rendering/rembrandt/renderer", false, "", 0 },
1432 {"disable-splash-screen", false, OPTION_BOOL, "/sim/startup/splash-screen", false, "", 0 },
1433 {"enable-splash-screen", false, OPTION_BOOL, "/sim/startup/splash-screen", true, "", 0 },
1434 {"disable-mouse-pointer", false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "disabled", 0 },
1435 {"enable-mouse-pointer", false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "enabled", 0 },
1436 {"disable-random-objects", false, OPTION_BOOL, "/sim/rendering/random-objects", false, "", 0 },
1437 {"enable-random-objects", false, OPTION_BOOL, "/sim/rendering/random-objects", true, "", 0 },
1438 {"disable-random-vegetation", false, OPTION_BOOL, "/sim/rendering/random-vegetation", false, "", 0 },
1439 {"enable-random-vegetation", false, OPTION_BOOL, "/sim/rendering/random-vegetation", true, "", 0 },
1440 {"disable-random-buildings", false, OPTION_BOOL, "/sim/rendering/random-buildings", false, "", 0 },
1441 {"enable-random-buildings", false, OPTION_BOOL, "/sim/rendering/random-buildings", true, "", 0 },
1442 {"disable-real-weather-fetch", false, OPTION_BOOL, "/environment/realwx/enabled", false, "", 0 },
1443 {"enable-real-weather-fetch", false, OPTION_BOOL, "/environment/realwx/enabled", true, "", 0 },
1444 {"metar", true, OPTION_STRING, "/environment/metar/data", false, "", 0 },
1445 {"disable-ai-models", false, OPTION_BOOL, "/sim/ai/enabled", false, "", 0 },
1446 {"enable-ai-models", false, OPTION_BOOL, "/sim/ai/enabled", true, "", 0 },
1447 {"disable-ai-traffic", false, OPTION_BOOL, "/sim/traffic-manager/enabled", false, "", 0 },
1448 {"enable-ai-traffic", false, OPTION_BOOL, "/sim/traffic-manager/enabled", true, "", 0 },
1449 {"disable-freeze", false, OPTION_BOOL, "/sim/freeze/master", false, "", 0 },
1450 {"enable-freeze", false, OPTION_BOOL, "/sim/freeze/master", true, "", 0 },
1451 {"disable-fuel-freeze", false, OPTION_BOOL, "/sim/freeze/fuel", false, "", 0 },
1452 {"enable-fuel-freeze", false, OPTION_BOOL, "/sim/freeze/fuel", true, "", 0 },
1453 {"disable-clock-freeze", false, OPTION_BOOL, "/sim/freeze/clock", false, "", 0 },
1454 {"enable-clock-freeze", false, OPTION_BOOL, "/sim/freeze/clock", true, "", 0 },
1455 {"disable-hud-3d", false, OPTION_BOOL, "/sim/hud/enable3d[1]", false, "", 0 },
1456 {"enable-hud-3d", false, OPTION_BOOL, "/sim/hud/enable3d[1]", true, "", 0 },
1457 {"disable-anti-alias-hud", false, OPTION_BOOL, "/sim/hud/color/antialiased", false, "", 0 },
1458 {"enable-anti-alias-hud", false, OPTION_BOOL, "/sim/hud/color/antialiased", true, "", 0 },
1459 {"control", true, OPTION_STRING, "/sim/control-mode", false, "", 0 },
1460 {"disable-auto-coordination", false, OPTION_BOOL, "/controls/flight/auto-coordination", false, "", 0 },
1461 {"enable-auto-coordination", false, OPTION_BOOL, "/controls/flight/auto-coordination", true, "", 0 },
1462 {"browser-app", true, OPTION_STRING, "/sim/startup/browser-app", false, "", 0 },
1463 {"disable-hud", false, OPTION_BOOL, "/sim/hud/visibility[1]", false, "", 0 },
1464 {"enable-hud", false, OPTION_BOOL, "/sim/hud/visibility[1]", true, "", 0 },
1465 {"disable-panel", false, OPTION_BOOL, "/sim/panel/visibility", false, "", 0 },
1466 {"enable-panel", false, OPTION_BOOL, "/sim/panel/visibility", true, "", 0 },
1467 {"disable-sound", false, OPTION_BOOL, "/sim/sound/working", false, "", 0 },
1468 {"enable-sound", false, OPTION_BOOL, "/sim/sound/working", true, "", 0 },
1469 {"sound-device", true, OPTION_STRING, "/sim/sound/device-name", false, "", 0 },
1470 {"airport", true, OPTION_STRING, "/sim/presets/airport-id", false, "", 0 },
1471 {"runway", true, OPTION_FUNC, "", false, "", fgOptRunway },
1472 {"vor", true, OPTION_FUNC, "", false, "", fgOptVOR },
1473 {"vor-frequency", true, OPTION_DOUBLE, "/sim/presets/vor-freq", false, "", fgOptVOR },
1474 {"ndb", true, OPTION_FUNC, "", false, "", fgOptNDB },
1475 {"ndb-frequency", true, OPTION_DOUBLE, "/sim/presets/ndb-freq", false, "", fgOptVOR },
1476 {"carrier", true, OPTION_FUNC, "", false, "", fgOptCarrier },
1477 {"parkpos", true, OPTION_FUNC, "", false, "", fgOptParkpos },
1478 {"fix", true, OPTION_FUNC, "", false, "", fgOptFIX },
1479 {"offset-distance", true, OPTION_DOUBLE, "/sim/presets/offset-distance-nm", false, "", 0 },
1480 {"offset-azimuth", true, OPTION_DOUBLE, "/sim/presets/offset-azimuth-deg", false, "", 0 },
1481 {"lon", true, OPTION_FUNC, "", false, "", fgOptLon },
1482 {"lat", true, OPTION_FUNC, "", false, "", fgOptLat },
1483 {"altitude", true, OPTION_FUNC, "", false, "", fgOptAltitude },
1484 {"uBody", true, OPTION_FUNC, "", false, "", fgOptUBody },
1485 {"vBody", true, OPTION_FUNC, "", false, "", fgOptVBody },
1486 {"wBody", true, OPTION_FUNC, "", false, "", fgOptWBody },
1487 {"vNorth", true, OPTION_FUNC, "", false, "", fgOptVNorth },
1488 {"vEast", true, OPTION_FUNC, "", false, "", fgOptVEast },
1489 {"vDown", true, OPTION_FUNC, "", false, "", fgOptVDown },
1490 {"vc", true, OPTION_FUNC, "", false, "", fgOptVc },
1491 {"mach", true, OPTION_FUNC, "", false, "", fgOptMach },
1492 {"heading", true, OPTION_DOUBLE, "/sim/presets/heading-deg", false, "", 0 },
1493 {"roll", true, OPTION_DOUBLE, "/sim/presets/roll-deg", false, "", 0 },
1494 {"pitch", true, OPTION_DOUBLE, "/sim/presets/pitch-deg", false, "", 0 },
1495 {"glideslope", true, OPTION_DOUBLE, "/sim/presets/glideslope-deg", false, "", 0 },
1496 {"roc", true, OPTION_FUNC, "", false, "", fgOptRoc },
1497 {"fg-root", true, OPTION_IGNORE, "", false, "", 0 },
1498 {"fg-scenery", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptFgScenery },
1499 {"fg-aircraft", true, OPTION_IGNORE | OPTION_MULTI, "", false, "", 0 },
1500 {"fdm", true, OPTION_STRING, "/sim/flight-model", false, "", 0 },
1501 {"aero", true, OPTION_STRING, "/sim/aero", false, "", 0 },
1502 {"aircraft-dir", true, OPTION_IGNORE, "", false, "", 0 },
1503 {"model-hz", true, OPTION_INT, "/sim/model-hz", false, "", 0 },
1504 {"max-fps", true, OPTION_DOUBLE, "/sim/frame-rate-throttle-hz", false, "", 0 },
1505 {"speed", true, OPTION_DOUBLE, "/sim/speed-up", false, "", 0 },
1506 {"trim", false, OPTION_BOOL, "/sim/presets/trim", true, "", 0 },
1507 {"notrim", false, OPTION_BOOL, "/sim/presets/trim", false, "", 0 },
1508 {"on-ground", false, OPTION_BOOL, "/sim/presets/onground", true, "", 0 },
1509 {"in-air", false, OPTION_BOOL, "/sim/presets/onground", false, "", 0 },
1510 {"fog-disable", false, OPTION_STRING, "/sim/rendering/fog", false, "disabled", 0 },
1511 {"fog-fastest", false, OPTION_STRING, "/sim/rendering/fog", false, "fastest", 0 },
1512 {"fog-nicest", false, OPTION_STRING, "/sim/rendering/fog", false, "nicest", 0 },
1513 {"disable-horizon-effect", false, OPTION_BOOL, "/sim/rendering/horizon-effect", false, "", 0 },
1514 {"enable-horizon-effect", false, OPTION_BOOL, "/sim/rendering/horizon-effect", true, "", 0 },
1515 {"disable-enhanced-lighting", false, OPTION_BOOL, "/sim/rendering/enhanced-lighting", false, "", 0 },
1516 {"enable-enhanced-lighting", false, OPTION_BOOL, "/sim/rendering/enhanced-lighting", true, "", 0 },
1517 {"disable-distance-attenuation", false, OPTION_BOOL, "/sim/rendering/distance-attenuation", false, "", 0 },
1518 {"enable-distance-attenuation", false, OPTION_BOOL, "/sim/rendering/distance-attenuation", true, "", 0 },
1519 {"disable-specular-highlight", false, OPTION_BOOL, "/sim/rendering/specular-highlight", false, "", 0 },
1520 {"enable-specular-highlight", false, OPTION_BOOL, "/sim/rendering/specular-highlight", true, "", 0 },
1521 {"disable-clouds", false, OPTION_BOOL, "/environment/clouds/status", false, "", 0 },
1522 {"enable-clouds", false, OPTION_BOOL, "/environment/clouds/status", true, "", 0 },
1523 {"disable-clouds3d", false, OPTION_BOOL, "/sim/rendering/clouds3d-enable", false, "", 0 },
1524 {"enable-clouds3d", false, OPTION_BOOL, "/sim/rendering/clouds3d-enable", true, "", 0 },
1525 {"fov", true, OPTION_FUNC, "", false, "", fgOptFov },
1526 {"aspect-ratio-multiplier", true, OPTION_DOUBLE, "/sim/current-view/aspect-ratio-multiplier", false, "", 0 },
1527 {"disable-fullscreen", false, OPTION_BOOL, "/sim/startup/fullscreen", false, "", 0 },
1528 {"enable-fullscreen", false, OPTION_BOOL, "/sim/startup/fullscreen", true, "", 0 },
1529 {"disable-save-on-exit", false, OPTION_BOOL, "/sim/startup/save-on-exit", false, "", 0 },
1530 {"enable-save-on-exit", false, OPTION_BOOL, "/sim/startup/save-on-exit", true, "", 0 },
1531 {"read-only", false, OPTION_BOOL, "/sim/fghome-readonly", true, "", 0 },
1532 {"ignore-autosave", false, OPTION_FUNC, "", false, "", fgOptIgnoreAutosave },
1533 {"restore-defaults", false, OPTION_BOOL, "/sim/startup/restore-defaults", true, "", 0 },
1534 {"shading-flat", false, OPTION_BOOL, "/sim/rendering/shading", false, "", 0 },
1535 {"shading-smooth", false, OPTION_BOOL, "/sim/rendering/shading", true, "", 0 },
1536 {"disable-skyblend", false, OPTION_BOOL, "/sim/rendering/skyblend", false, "", 0 },
1537 {"enable-skyblend", false, OPTION_BOOL, "/sim/rendering/skyblend", true, "", 0 },
1538 {"disable-textures", false, OPTION_BOOL, "/sim/rendering/textures", false, "", 0 },
1539 {"enable-textures", false, OPTION_BOOL, "/sim/rendering/textures", true, "", 0 },
1540 {"texture-filtering", false, OPTION_INT, "/sim/rendering/filtering", 1, "", 0 },
1541 {"disable-wireframe", false, OPTION_BOOL, "/sim/rendering/wireframe", false, "", 0 },
1542 {"enable-wireframe", false, OPTION_BOOL, "/sim/rendering/wireframe", true, "", 0 },
1543 {"materials-file", true, OPTION_STRING, "/sim/rendering/materials-file", false, "", 0 },
1544 {"disable-terrasync", false, OPTION_BOOL, "/sim/terrasync/enabled", false, "", 0 },
1545 {"enable-terrasync", false, OPTION_BOOL, "/sim/terrasync/enabled", true, "", 0 },
1546 {"terrasync-dir", true, OPTION_STRING, "/sim/terrasync/scenery-dir", false, "", 0 },
1547 {"geometry", true, OPTION_FUNC, "", false, "", fgOptGeometry },
1548 {"bpp", true, OPTION_FUNC, "", false, "", fgOptBpp },
1549 {"units-feet", false, OPTION_STRING, "/sim/startup/units", false, "feet", 0 },
1550 {"units-meters", false, OPTION_STRING, "/sim/startup/units", false, "meters", 0 },
1551 {"timeofday", true, OPTION_STRING, "/sim/startup/time-offset-type", false, "noon", 0 },
1552 {"season", true, OPTION_STRING, "/sim/startup/season", false, "summer", 0 },
1553 {"time-offset", true, OPTION_FUNC, "", false, "", fgOptTimeOffset },
1554 {"time-match-real", false, OPTION_STRING, "/sim/startup/time-offset-type", false, "system-offset", 0 },
1555 {"time-match-local", false, OPTION_STRING, "/sim/startup/time-offset-type", false, "latitude-offset", 0 },
1556 {"start-date-sys", true, OPTION_FUNC, "", false, "", fgOptStartDateSys },
1557 {"start-date-lat", true, OPTION_FUNC, "", false, "", fgOptStartDateLat },
1558 {"start-date-gmt", true, OPTION_FUNC, "", false, "", fgOptStartDateGmt },
1559 {"hud-tris", false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "tris", 0 },
1560 {"hud-culled", false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "culled", 0 },
1561 {"atcsim", true, OPTION_CHANNEL, "", false, "dummy", 0 },
1562 {"atlas", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1563 {"httpd", true, OPTION_CHANNEL, "", false, "", 0 },
1564 #ifdef FG_JPEG_SERVER
1565 {"jpg-httpd", true, OPTION_CHANNEL, "", false, "", 0 },
1567 {"native", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1568 {"native-ctrls", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1569 {"native-fdm", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1570 {"native-gui", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1571 {"opengc", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1572 {"AV400", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1573 {"AV400Sim", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1574 {"AV400WSimA", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1575 {"AV400WSimB", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1576 {"garmin", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1577 {"igc", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1578 {"nmea", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1579 {"generic", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1580 {"props", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1581 {"telnet", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1582 {"pve", true, OPTION_CHANNEL, "", false, "", 0 },
1583 {"ray", true, OPTION_CHANNEL, "", false, "", 0 },
1584 {"rul", true, OPTION_CHANNEL, "", false, "", 0 },
1585 {"joyclient", true, OPTION_CHANNEL, "", false, "", 0 },
1586 {"jsclient", true, OPTION_CHANNEL, "", false, "", 0 },
1587 {"proxy", true, OPTION_FUNC, "", false, "", fgSetupProxy },
1588 {"callsign", true, OPTION_FUNC, "", false, "", fgOptCallSign},
1589 {"multiplay", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1591 {"hla", true, OPTION_CHANNEL, "", false, "", 0 },
1592 {"hla-local", true, OPTION_CHANNEL, "", false, "", 0 },
1594 {"trace-read", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptTraceRead },
1595 {"trace-write", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptTraceWrite },
1596 {"log-level", true, OPTION_FUNC, "", false, "", fgOptLogLevel },
1597 {"log-class", true, OPTION_FUNC, "", false, "", fgOptLogClasses },
1598 {"view-offset", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptViewOffset },
1599 {"visibility", true, OPTION_FUNC, "", false, "", fgOptVisibilityMeters },
1600 {"visibility-miles", true, OPTION_FUNC, "", false, "", fgOptVisibilityMiles },
1601 {"random-wind", false, OPTION_FUNC, "", false, "", fgOptRandomWind },
1602 {"wind", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptWind },
1603 {"turbulence", true, OPTION_FUNC, "", false, "", fgOptTurbulence },
1604 {"ceiling", true, OPTION_FUNC, "", false, "", fgOptCeiling },
1605 {"wp", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptWp },
1606 {"flight-plan", true, OPTION_STRING, "/autopilot/route-manager/file-path", false, "", NULL },
1607 {"config", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptConfig },
1608 {"aircraft", true, OPTION_STRING, "/sim/aircraft", false, "", 0 },
1609 {"vehicle", true, OPTION_STRING, "/sim/aircraft", false, "", 0 },
1610 {"failure", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptFailure },
1612 {"enable-fgcom", false, OPTION_BOOL, "/sim/fgcom/enabled", true, "", 0 },
1613 {"disable-fgcom", false, OPTION_BOOL, "/sim/fgcom/enabled", false, "", 0 },
1615 {"com1", true, OPTION_DOUBLE, "/instrumentation/comm[0]/frequencies/selected-mhz", false, "", 0 },
1616 {"com2", true, OPTION_DOUBLE, "/instrumentation/comm[1]/frequencies/selected-mhz", false, "", 0 },
1617 {"nav1", true, OPTION_FUNC, "", false, "", fgOptNAV1 },
1618 {"nav2", true, OPTION_FUNC, "", false, "", fgOptNAV2 },
1619 {"adf", /*legacy*/ true, OPTION_FUNC, "", false, "", fgOptADF1 },
1620 {"adf1", true, OPTION_FUNC, "", false, "", fgOptADF1 },
1621 {"adf2", true, OPTION_FUNC, "", false, "", fgOptADF2 },
1622 {"dme", true, OPTION_FUNC, "", false, "", fgOptDME },
1623 {"min-status", true, OPTION_STRING, "/sim/aircraft-min-status", false, "all", 0 },
1624 {"livery", true, OPTION_FUNC, "", false, "", fgOptLivery },
1625 {"ai-scenario", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptScenario },
1626 {"parking-id", true, OPTION_FUNC, "", false, "", fgOptParking },
1627 {"version", false, OPTION_FUNC, "", false, "", fgOptVersion },
1628 {"enable-fpe", false, OPTION_IGNORE, "", false, "", 0},
1629 {"fgviewer", false, OPTION_IGNORE, "", false, "", 0},
1630 {"no-default-config", false, OPTION_IGNORE, "", false, "", 0},
1631 {"prop", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptSetProperty},
1636 namespace flightgear
1640 * internal storage of a value->option binding
1645 OptionValue(OptionDesc* d, const string& v) :
1653 typedef std::vector<OptionValue> OptionValueVec;
1654 typedef std::map<string, OptionDesc*> OptionDescDict;
1656 class Options::OptionsPrivate
1660 OptionValueVec::const_iterator findValue(const string& key) const
1662 OptionValueVec::const_iterator it = values.begin();
1663 for (; it != values.end(); ++it) {
1665 continue; // ignore markers
1668 if (it->desc->option == key) {
1671 } // of set values iteration
1673 return it; // not found
1676 OptionDesc* findOption(const string& key) const
1678 OptionDescDict::const_iterator it = options.find(key);
1679 if (it == options.end()) {
1686 int processOption(OptionDesc* desc, const string& arg_value)
1689 return FG_OPTIONS_OK; // tolerate marker options
1692 switch ( desc->type & 0xffff ) {
1694 fgSetBool( desc->property, desc->b_param );
1697 if ( desc->has_param && !arg_value.empty() ) {
1698 fgSetString( desc->property, arg_value.c_str() );
1699 } else if ( !desc->has_param && arg_value.empty() ) {
1700 fgSetString( desc->property, desc->s_param );
1701 } else if ( desc->has_param ) {
1702 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1703 return FG_OPTIONS_ERROR;
1705 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" );
1706 return FG_OPTIONS_ERROR;
1710 if ( !arg_value.empty() ) {
1711 fgSetDouble( desc->property, atof( arg_value ) );
1713 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1714 return FG_OPTIONS_ERROR;
1718 if ( !arg_value.empty() ) {
1719 fgSetInt( desc->property, atoi( arg_value ) );
1721 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1722 return FG_OPTIONS_ERROR;
1725 case OPTION_CHANNEL:
1726 // XXX return value of add_channel should be checked?
1727 if ( desc->has_param && !arg_value.empty() ) {
1728 add_channel( desc->option, arg_value );
1729 } else if ( !desc->has_param && arg_value.empty() ) {
1730 add_channel( desc->option, desc->s_param );
1731 } else if ( desc->has_param ) {
1732 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1733 return FG_OPTIONS_ERROR;
1735 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" );
1736 return FG_OPTIONS_ERROR;
1740 if ( desc->has_param && !arg_value.empty() ) {
1741 return desc->func( arg_value.c_str() );
1742 } else if ( !desc->has_param && arg_value.empty() ) {
1743 return desc->func( desc->s_param );
1744 } else if ( desc->has_param ) {
1745 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1746 return FG_OPTIONS_ERROR;
1748 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" );
1749 return FG_OPTIONS_ERROR;
1757 return FG_OPTIONS_OK;
1761 * insert a marker value into the values vector. This is necessary
1762 * when processing options, to ensure the correct ordering, where we scan
1763 * for marker values in reverse, and then forwards within each group.
1765 void insertGroupMarker()
1767 values.push_back(OptionValue(NULL, "-"));
1771 * given a current iterator into the values, find the preceeding group marker,
1772 * or return the beginning of the value vector.
1774 OptionValueVec::const_iterator rfindGroup(OptionValueVec::const_iterator pos) const
1776 while (--pos != values.begin()) {
1777 if (pos->desc == NULL) {
1778 return pos; // found a marker, we're done
1788 shouldLoadDefaultConfig;
1790 OptionDescDict options;
1791 OptionValueVec values;
1792 simgear::PathList propertyFiles;
1795 Options* Options::sharedInstance()
1797 if (shared_instance == NULL) {
1798 shared_instance = new Options;
1801 return shared_instance;
1804 Options::Options() :
1805 p(new OptionsPrivate())
1807 p->showHelp = false;
1809 p->showAircraft = false;
1810 p->shouldLoadDefaultConfig = true;
1813 OptionDesc *desc = &fgOptionArray[ 0 ];
1814 while ( desc->option != 0 ) {
1815 p->options[ desc->option ] = desc;
1824 void Options::init(int argc, char **argv, const SGPath& appDataPath)
1826 // first, process the command line
1827 bool inOptions = true;
1828 for (int i=1; i<argc; ++i) {
1829 if (inOptions && (argv[i][0] == '-')) {
1830 if (strcmp(argv[i], "--") == 0) { // end of options delimiter
1835 int result = parseOption(argv[i]);
1836 processArgResult(result);
1838 // XML properties file
1841 SG_LOG(SG_GENERAL, SG_ALERT, "config file not found:" << f.str());
1845 p->propertyFiles.push_back(f);
1847 } // of arguments iteration
1848 p->insertGroupMarker(); // command line is one group
1850 // establish log-level before anything else - otherwise it is not possible
1851 // to show extra (debug/info/warning) messages for the start-up phase.
1852 fgOptLogLevel(valueForOption("log-level", "alert").c_str());
1854 if (!p->shouldLoadDefaultConfig) {
1859 // then config files
1861 std::string homedir;
1862 if (getenv("HOME")) {
1863 homedir = getenv("HOME");
1866 if( !homedir.empty() && !hostname.empty() ) {
1867 // Check for ~/.fgfsrc.hostname
1868 config.set(homedir);
1869 config.append(".fgfsrc");
1870 config.concat( "." );
1871 config.concat( hostname );
1875 // Check for ~/.fgfsrc
1876 if( !homedir.empty() ) {
1877 config.set(homedir);
1878 config.append(".fgfsrc");
1882 // check for a config file in app data
1883 SGPath appDataConfig(appDataPath);
1884 appDataConfig.append("fgfsrc");
1885 if (appDataConfig.exists()) {
1886 readConfig(appDataConfig);
1892 // system.fgfsrc handling
1893 if( ! hostname.empty() ) {
1894 config.set(globals->get_fg_root());
1895 config.append( "system.fgfsrc" );
1896 config.concat( "." );
1897 config.concat( hostname );
1901 config.set(globals->get_fg_root());
1902 config.append( "system.fgfsrc" );
1906 void Options::initAircraft()
1908 BOOST_FOREACH(const string& paths, valuesForOption("fg-aircraft")) {
1909 globals->append_aircraft_paths(paths);
1912 const char* envp = ::getenv("FG_AIRCRAFT");
1914 globals->append_aircraft_paths(envp);
1918 if (isOptionSet("aircraft")) {
1919 aircraft = valueForOption("aircraft");
1920 } else if (isOptionSet("vehicle")) {
1921 aircraft = valueForOption("vehicle");
1924 if (!aircraft.empty()) {
1925 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
1926 fgSetString("/sim/aircraft", aircraft.c_str() );
1928 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
1931 if (p->showAircraft) {
1932 fgOptLogLevel( "alert" );
1933 SGPath path( globals->get_fg_root() );
1934 path.append("Aircraft");
1935 fgShowAircraft(path);
1939 if (isOptionSet("aircraft-dir")) {
1940 // set this now, so it's available in FindAndCacheAircraft
1941 fgSetString("/sim/aircraft-dir", valueForOption("aircraft-dir"));
1945 void Options::processArgResult(int result)
1947 if ((result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR))
1949 else if (result == FG_OPTIONS_VERBOSE_HELP)
1951 else if (result == FG_OPTIONS_SHOW_AIRCRAFT) {
1952 p->showAircraft = true;
1953 } else if (result == FG_OPTIONS_NO_DEFAULT_CONFIG) {
1954 p->shouldLoadDefaultConfig = false;
1955 } else if (result == FG_OPTIONS_SHOW_SOUND_DEVICES) {
1959 string vendor = smgr.get_vendor();
1960 string renderer = smgr.get_renderer();
1961 cout << renderer << " provided by " << vendor << endl;
1962 cout << endl << "No. Device" << endl;
1964 vector <const char*>devices = smgr.get_available_devices();
1965 for (vector <const char*>::size_type i=0; i<devices.size(); i++) {
1966 cout << i << ". \"" << devices[i] << "\"" << endl;
1971 } else if (result == FG_OPTIONS_EXIT) {
1976 void Options::readConfig(const SGPath& path)
1978 sg_gzifstream in( path.str() );
1979 if ( !in.is_open() ) {
1983 SG_LOG( SG_GENERAL, SG_INFO, "Processing config file: " << path.str() );
1986 while ( ! in.eof() ) {
1988 getline( in, line, '\n' );
1990 // catch extraneous (DOS) line ending character
1992 for (i = line.length(); i > 0; i--)
1993 if (line[i - 1] > 32)
1995 line = line.substr( 0, i );
1997 if ( parseOption( line ) == FG_OPTIONS_ERROR ) {
1998 cerr << endl << "Config file parse error: " << path.str() << " '"
1999 << line << "'" << endl;
2005 p->insertGroupMarker(); // each config file is a group
2008 int Options::parseOption(const string& s)
2010 if ((s == "--help") || (s=="-h")) {
2011 return FG_OPTIONS_HELP;
2012 } else if ( (s == "--verbose") || (s == "-v") ) {
2013 // verbose help/usage request
2014 return FG_OPTIONS_VERBOSE_HELP;
2015 } else if ((s == "--console") || (s == "-c")) {
2016 simgear::requestConsole();
2017 return FG_OPTIONS_OK;
2018 } else if (s.find("-psn") == 0) {
2019 // on Mac, when launched from the GUI, we are passed the ProcessSerialNumber
2020 // as an argument (and no others). Silently ignore the argument here.
2021 return FG_OPTIONS_OK;
2022 } else if ( s.find( "--show-aircraft") == 0) {
2023 return(FG_OPTIONS_SHOW_AIRCRAFT);
2024 } else if ( s.find( "--show-sound-devices") == 0) {
2025 return(FG_OPTIONS_SHOW_SOUND_DEVICES);
2026 } else if ( s.find( "--no-default-config") == 0) {
2027 return FG_OPTIONS_NO_DEFAULT_CONFIG;
2028 } else if ( s.find( "--prop:") == 0) {
2029 // property setting has a slightly different syntax, so fudge things
2030 OptionDesc* desc = p->findOption("prop");
2031 if (s.find("=", 7) == string::npos) { // no equals token
2032 SG_LOG(SG_GENERAL, SG_ALERT, "malformed property option:" << s);
2033 return FG_OPTIONS_ERROR;
2036 p->values.push_back(OptionValue(desc, s.substr(7)));
2037 return FG_OPTIONS_OK;
2038 } else if ( s.find( "--" ) == 0 ) {
2039 size_t eqPos = s.find( '=' );
2041 if (eqPos == string::npos) {
2044 key = s.substr( 2, eqPos - 2 );
2045 value = s.substr( eqPos + 1);
2048 return addOption(key, value);
2050 SG_LOG(SG_GENERAL, SG_ALERT, "unknown option:" << s);
2051 return FG_OPTIONS_ERROR;
2055 int Options::addOption(const string &key, const string &value)
2057 OptionDesc* desc = p->findOption(key);
2059 SG_LOG(SG_GENERAL, SG_ALERT, "unknown option:" << key);
2060 return FG_OPTIONS_ERROR;
2063 if (!(desc->type & OPTION_MULTI)) {
2064 OptionValueVec::const_iterator it = p->findValue(key);
2065 if (it != p->values.end()) {
2066 SG_LOG(SG_GENERAL, SG_INFO, "multiple values forbidden for option:" << key << ", ignoring:" << value);
2067 return FG_OPTIONS_OK;
2071 p->values.push_back(OptionValue(desc, value));
2072 return FG_OPTIONS_OK;
2075 bool Options::isOptionSet(const string &key) const
2077 OptionValueVec::const_iterator it = p->findValue(key);
2078 return (it != p->values.end());
2081 string Options::valueForOption(const string& key, const string& defValue) const
2083 OptionValueVec::const_iterator it = p->findValue(key);
2084 if (it == p->values.end()) {
2091 string_list Options::valuesForOption(const std::string& key) const
2094 OptionValueVec::const_iterator it = p->values.begin();
2095 for (; it != p->values.end(); ++it) {
2097 continue; // ignore marker values
2100 if (it->desc->option == key) {
2101 result.push_back(it->value);
2108 OptionResult Options::processOptions()
2110 // establish locale before showing help (this selects the default locale,
2111 // when no explicit option was set)
2112 globals->get_locale()->selectLanguage(valueForOption("language").c_str());
2114 // now FG_ROOT is setup, process various command line options that bail us
2115 // out quickly, but rely on aircraft / root settings
2118 return FG_OPTIONS_EXIT;
2121 // processing order is complicated. We must process groups LIFO, but the
2122 // values *within* each group in FIFO order, to retain consistency with
2123 // older versions of FG, and existing user configs.
2124 // in practice this means system.fgfsrc must be *processed* before
2125 // .fgfsrc, which must be processed before the command line args, and so on.
2126 OptionValueVec::const_iterator groupEnd = p->values.end();
2128 while (groupEnd != p->values.begin()) {
2129 OptionValueVec::const_iterator groupBegin = p->rfindGroup(groupEnd);
2130 // run over the group in FIFO order
2131 OptionValueVec::const_iterator it;
2132 for (it = groupBegin; it != groupEnd; ++it) {
2133 int result = p->processOption(it->desc, it->value);
2136 case FG_OPTIONS_ERROR:
2138 return FG_OPTIONS_ERROR;
2140 case FG_OPTIONS_EXIT:
2141 return FG_OPTIONS_EXIT;
2148 groupEnd = groupBegin;
2151 BOOST_FOREACH(const SGPath& file, p->propertyFiles) {
2152 if (!file.exists()) {
2153 SG_LOG(SG_GENERAL, SG_ALERT, "config file not found:" << file.str());
2157 SG_LOG(SG_GENERAL, SG_INFO,
2158 "Reading command-line property file " << file.str());
2159 readProperties(file.str(), globals->get_props());
2162 // now options are process, do supplemental fixup
2163 const char *envp = ::getenv( "FG_SCENERY" );
2165 globals->append_fg_scenery(envp);
2168 // terrasync directory fixup
2169 if (fgGetBool("/sim/terrasync/enabled")) {
2170 string terrasyncDir = fgGetString("/sim/terrasync/scenery-dir");
2171 if (terrasyncDir.empty()) {
2172 SGPath p(globals->get_fg_home());
2173 p.append("TerraSync");
2174 terrasyncDir = p.str();
2175 SG_LOG(SG_GENERAL, SG_INFO,
2176 "Using default TerraSync dir: " << terrasyncDir);
2177 fgSetString("/sim/terrasync/scenery-dir", terrasyncDir);
2180 SGPath p(terrasyncDir);
2181 // following is necessary to ensure NavDataCache sees stable scenery paths from
2182 // terrasync. Ensure the Terrain and Objects subdirs exist immediately, rather
2183 // than waiting for the first tiles to be scheduled.
2184 simgear::Dir terrainDir(SGPath(p, "Terrain")),
2185 objectsDir(SGPath(p, "Objects"));
2186 if (!terrainDir.exists()) {
2187 terrainDir.create(0755);
2190 if (!objectsDir.exists()) {
2191 objectsDir.create(0755);
2194 const string_list& scenery_paths(globals->get_fg_scenery());
2195 if (std::find(scenery_paths.begin(), scenery_paths.end(), terrasyncDir) == scenery_paths.end()) {
2196 // terrasync dir is not in the scenery paths, add it
2197 globals->append_fg_scenery(terrasyncDir);
2201 if (globals->get_fg_scenery().empty()) {
2202 // no scenery paths set *at all*, use the data in FG_ROOT
2203 SGPath root(globals->get_fg_root());
2204 root.append("Scenery");
2205 globals->append_fg_scenery(root.str());
2208 return FG_OPTIONS_OK;
2211 void Options::showUsage() const
2213 fgOptLogLevel( "alert" );
2215 FGLocale *locale = globals->get_locale();
2216 SGPropertyNode options_root;
2218 simgear::requestConsole(); // ensure console is shown on Windows
2222 fgLoadProps("options.xml", &options_root);
2223 } catch (const sg_exception &) {
2224 cout << "Unable to read the help file." << endl;
2225 cout << "Make sure the file options.xml is located in the FlightGear base directory," << endl;
2226 cout << "and the location of the base directory is specified by setting $FG_ROOT or" << endl;
2227 cout << "by adding --fg-root=path as a program argument." << endl;
2232 SGPropertyNode *options = options_root.getNode("options");
2234 SG_LOG( SG_GENERAL, SG_ALERT,
2235 "Error reading options.xml: <options> directive not found." );
2239 if (!locale->loadResource("options"))
2241 cout << "Unable to read the language resource." << endl;
2245 const char* usage = locale->getLocalizedString(options->getStringValue("usage"), "options");
2247 cout << usage << endl;
2250 vector<SGPropertyNode_ptr>section = options->getChildren("section");
2251 for (unsigned int j = 0; j < section.size(); j++) {
2254 vector<SGPropertyNode_ptr>option = section[j]->getChildren("option");
2255 for (unsigned int k = 0; k < option.size(); k++) {
2257 SGPropertyNode *name = option[k]->getNode("name");
2258 SGPropertyNode *short_name = option[k]->getNode("short");
2259 SGPropertyNode *key = option[k]->getNode("key");
2260 SGPropertyNode *arg = option[k]->getNode("arg");
2261 bool brief = option[k]->getNode("brief") != 0;
2263 if ((brief || p->verbose) && name) {
2264 string tmp = name->getStringValue();
2268 tmp.append(key->getStringValue());
2272 tmp.append(arg->getStringValue());
2276 tmp.append(short_name->getStringValue());
2279 if (tmp.size() <= 25) {
2282 msg.append( 27-tmp.size(), ' ');
2286 msg.append(32, ' ');
2288 // There may be more than one <description> tag associated
2291 vector<SGPropertyNode_ptr> desc;
2292 desc = option[k]->getChildren("description");
2293 if (! desc.empty()) {
2294 for ( unsigned int l = 0; l < desc.size(); l++) {
2295 string t = desc[l]->getStringValue();
2297 // There may be more than one translation line.
2298 vector<SGPropertyNode_ptr>trans_desc = locale->getLocalizedStrings(t.c_str(),"options");
2299 for ( unsigned int m = 0; m < trans_desc.size(); m++ ) {
2300 string t_str = trans_desc[m]->getStringValue();
2302 if ((m > 0) || ((l > 0) && m == 0)) {
2303 msg.append( 32, ' ');
2306 // If the string is too large to fit on the screen,
2307 // then split it up in several pieces.
2309 while ( t_str.size() > 47 ) {
2311 string::size_type m = t_str.rfind(' ', 47);
2312 msg += t_str.substr(0, m) + '\n';
2313 msg.append( 32, ' ');
2315 t_str.erase(t_str.begin(), t_str.begin() + m + 1);
2317 msg += t_str + '\n';
2324 const char* name = locale->getLocalizedString(section[j]->getStringValue("name"),"options");
2325 if (!msg.empty() && name) {
2326 cout << endl << name << ":" << endl;
2332 if ( !p->verbose ) {
2333 const char* verbose_help = locale->getLocalizedString(options->getStringValue("verbose-help"),"options");
2335 cout << endl << verbose_help << endl;
2338 std::cout << "Hit a key to continue..." << std::endl;
2343 #if defined(__CYGWIN__)
2344 string Options::platformDefaultRoot() const
2349 #elif defined(SG_WINDOWS)
2350 string Options::platformDefaultRoot() const
2354 #elif defined(SG_MAC)
2355 // platformDefaultRoot defined in CocoaHelpers.mm
2357 string Options::platformDefaultRoot() const
2363 void Options::setupRoot()
2366 if (isOptionSet("fg-root")) {
2367 root = valueForOption("fg-root"); // easy!
2369 // Next check if fg-root is set as an env variable
2370 char *envp = ::getenv( "FG_ROOT" );
2371 if ( envp != NULL ) {
2374 root = platformDefaultRoot();
2378 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
2379 globals->set_fg_root(root);
2382 static char required_version[] = FLIGHTGEAR_VERSION;
2383 string base_version = fgBasePackageVersion();
2384 if (base_version.empty()) {
2385 flightgear::fatalMessageBox("Base package not found",
2386 "Required data files not found, check your installation.",
2387 "Looking for base-package files at: '" + root + "'");
2392 if (base_version != required_version) {
2393 // tell the operator how to use this application
2395 flightgear::fatalMessageBox("Base package version mismatch",
2396 "Version check failed: please check your installation.",
2397 "Found data files for version '" + base_version +
2398 "' at '" + globals->get_fg_root() + "', version '"
2399 + required_version + "' is required.");
2405 bool Options::shouldLoadDefaultConfig() const
2407 return p->shouldLoadDefaultConfig;
2410 } // of namespace flightgear