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/textures", true);
189 fgTie( "/sim/rendering/filtering", SGGetTextureFilter, SGSetTextureFilter, false);
190 fgSetInt("/sim/rendering/filtering", 1);
191 fgSetBool("/sim/rendering/wireframe", false);
192 fgSetBool("/sim/rendering/horizon-effect", false);
193 fgSetBool("/sim/rendering/enhanced-lighting", false);
194 fgSetBool("/sim/rendering/distance-attenuation", false);
195 fgSetBool("/sim/rendering/specular-highlight", true);
196 fgSetString("/sim/rendering/materials-file", "materials.xml");
197 fgSetInt("/sim/startup/xsize", 1024);
198 fgSetInt("/sim/startup/ysize", 768);
199 fgSetInt("/sim/rendering/bits-per-pixel", 32);
200 fgSetString("/sim/view-mode", "pilot");
201 fgSetDouble("/sim/current-view/heading-offset-deg", 0);
204 fgSetString("/sim/startup/units", "feet");
205 fgSetString("/sim/hud/frame-stat-type", "tris");
208 fgSetInt("/sim/startup/time-offset", 0);
209 fgSetString("/sim/startup/time-offset-type", "system-offset");
210 fgSetLong("/sim/time/cur-time-override", 0);
213 fgSetBool("/sim/freeze/master", false);
214 fgSetBool("/sim/freeze/position", false);
215 fgSetBool("/sim/freeze/clock", false);
216 fgSetBool("/sim/freeze/fuel", false);
218 fgSetString("/sim/multiplay/callsign", "callsign");
219 fgSetString("/sim/multiplay/rxhost", "");
220 fgSetString("/sim/multiplay/txhost", "");
221 fgSetInt("/sim/multiplay/rxport", 0);
222 fgSetInt("/sim/multiplay/txport", 0);
224 SGPropertyNode* v = globals->get_props()->getNode("/sim/version", true);
225 v->setValueReadOnly("flightgear", FLIGHTGEAR_VERSION);
226 v->setValueReadOnly("simgear", SG_STRINGIZE(SIMGEAR_VERSION));
227 v->setValueReadOnly("openscenegraph", osgGetVersion());
228 v->setValueReadOnly("openscenegraph-thread-safe-reference-counting",
229 osg::Referenced::getThreadSafeReferenceCounting());
230 v->setValueReadOnly("revision", REVISION);
231 v->setValueReadOnly("build-number", HUDSON_BUILD_NUMBER);
232 v->setValueReadOnly("build-id", HUDSON_BUILD_ID);
233 v->setValueReadOnly("hla-support", bool(FG_HAVE_HLA));
235 char* envp = ::getenv( "http_proxy" );
237 fgSetupProxy( envp );
240 ///////////////////////////////////////////////////////////////////////////////
241 // helper object to implement the --show-aircraft command.
242 // resides here so we can share the fgFindAircraftInDir template above,
243 // and hence ensure this command lists exectly the same aircraft as the normal
245 class ShowAircraft : public AircraftDirVistorBase
250 _minStatus = getNumMaturity(fgGetString("/sim/aircraft-min-status", "all"));
254 void show(const SGPath& path)
258 simgear::requestConsole(); // ensure console is shown on Windows
260 std::sort(_aircraft.begin(), _aircraft.end(), ciLessLibC());
261 cout << "Available aircraft:" << endl;
262 for ( unsigned int i = 0; i < _aircraft.size(); i++ ) {
263 cout << _aircraft[i] << endl;
268 virtual VisitResult visit(const SGPath& path)
272 readProperties(path.str(), &root);
273 } catch (sg_exception& ) {
274 return VISIT_CONTINUE;
279 descStr += path.file();
280 // trim common suffix from file names
281 int nPos = descStr.rfind("-set.xml");
282 if (nPos == (int)(descStr.size() - 8)) {
283 descStr.resize(nPos);
286 SGPropertyNode *node = root.getNode("sim");
288 SGPropertyNode* desc = node->getNode("description");
289 // if a status tag is found, read it in
290 if (node->hasValue("status")) {
291 maturity = getNumMaturity(node->getStringValue("status"));
295 if (descStr.size() <= 27+3) {
296 descStr.append(29+3-descStr.size(), ' ');
299 descStr.append( 32, ' ');
301 descStr += desc->getStringValue();
303 } // of have 'sim' node
305 if (maturity >= _minStatus) {
306 _aircraft.push_back(descStr);
309 return VISIT_CONTINUE;
313 int getNumMaturity(const char * str)
315 // changes should also be reflected in $FG_ROOT/data/options.xml &
316 // $FG_ROOT/data/Translations/string-default.xml
317 const char* levels[] = {"alpha","beta","early-production","production"};
319 if (!strcmp(str, "all")) {
323 for (size_t i=0; i<(sizeof(levels)/sizeof(levels[0]));i++)
324 if (strcmp(str,levels[i])==0)
330 // recommended in Meyers, Effective STL when internationalization and embedded
331 // NULLs aren't an issue. Much faster than the STL or Boost lex versions.
332 struct ciLessLibC : public std::binary_function<string, string, bool>
334 bool operator()(const std::string &lhs, const std::string &rhs) const
336 return strcasecmp(lhs.c_str(), rhs.c_str()) < 0 ? 1 : 0;
341 string_list _aircraft;
345 * Search in the current directory, and in on directory deeper
346 * for <aircraft>-set.xml configuration files and show the aircaft name
347 * and the contents of the<description> tag in a sorted manner.
349 * @parampath the directory to search for configuration files
351 void fgShowAircraft(const SGPath &path)
357 cout << "Hit a key to continue..." << endl;
364 parse_wind (const string &wind, double * min_hdg, double * max_hdg,
365 double * speed, double * gust)
367 string::size_type pos = wind.find('@');
368 if (pos == string::npos)
370 string dir = wind.substr(0, pos);
371 string spd = wind.substr(pos+1);
373 if (pos == string::npos) {
374 *min_hdg = *max_hdg = atof(dir.c_str());
376 *min_hdg = atof(dir.substr(0,pos).c_str());
377 *max_hdg = atof(dir.substr(pos+1).c_str());
380 if (pos == string::npos) {
381 *speed = *gust = atof(spd.c_str());
383 *speed = atof(spd.substr(0,pos).c_str());
384 *gust = atof(spd.substr(pos+1).c_str());
390 parseIntValue(char** ppParserPos, int* pValue,int min, int max, const char* field, const char* argument)
392 if ( !strlen(*ppParserPos) )
398 while ( isdigit((*ppParserPos)[0]) && (i<255) )
400 num[i] = (*ppParserPos)[0];
406 switch ((*ppParserPos)[0])
414 SG_LOG(SG_GENERAL, SG_ALERT, "Illegal character in time string for " << field << ": '" <<
415 (*ppParserPos)[0] << "'.");
416 // invalid field - skip rest of string to avoid further errors
417 while ((*ppParserPos)[0])
425 int value = atoi(num);
426 if ((value < min)||(value > max))
428 SG_LOG(SG_GENERAL, SG_ALERT, "Invalid " << field << " in '" << argument <<
429 "'. Valid range is " << min << "-" << max << ".");
439 // parse a time string ([+/-]%f[:%f[:%f]]) into hours
441 parse_time(const string& time_in) {
442 char *time_str, num[256];
443 double hours, minutes, seconds;
448 time_str = (char *)time_in.c_str();
450 // printf("parse_time(): %s\n", time_str);
453 if ( strlen(time_str) ) {
454 if ( time_str[0] == '+' ) {
457 } else if ( time_str[0] == '-' ) {
462 // printf("sign = %d\n", sign);
465 if ( strlen(time_str) ) {
467 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
468 num[i] = time_str[0];
472 if ( time_str[0] == ':' ) {
477 // printf("hours = %.2lf\n", hours);
483 if ( strlen(time_str) ) {
485 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
486 num[i] = time_str[0];
490 if ( time_str[0] == ':' ) {
495 // printf("minutes = %.2lf\n", minutes);
497 result += minutes / 60.0;
501 if ( strlen(time_str) ) {
503 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
504 num[i] = time_str[0];
510 // printf("seconds = %.2lf\n", seconds);
512 result += seconds / 3600.0;
515 SG_LOG( SG_GENERAL, SG_INFO, " parse_time() = " << sign * result );
517 return(sign * result);
520 // parse a date string (yyyy:mm:dd:hh:mm:ss) into a time_t (seconds)
522 parse_date( const string& date, const char* timeType)
524 struct tm gmt,*pCurrentTime;
525 int year,month,day,hour,minute,second;
526 char *argument, *date_str;
529 CurrentTime.update(SGGeod(),0,0);
531 // FIXME This should obtain system/aircraft/GMT time depending on timeType
532 pCurrentTime = CurrentTime.getGmt();
534 // initialize all fields with current time
535 year = pCurrentTime->tm_year + 1900;
536 month = pCurrentTime->tm_mon + 1;
537 day = pCurrentTime->tm_mday;
538 hour = pCurrentTime->tm_hour;
539 minute = pCurrentTime->tm_min;
540 second = pCurrentTime->tm_sec;
542 argument = (char *)date.c_str();
545 // start with parsing year
546 if (!strlen(date_str) ||
547 !parseIntValue(&date_str,&year,0,9999,"year",argument))
554 SG_LOG(SG_GENERAL, SG_ALERT, "Invalid year '" << year << "'. Use 1970 or later.");
558 parseIntValue(&date_str, &month, 1, 12, "month", argument);
559 parseIntValue(&date_str, &day, 1, 31, "day", argument);
560 parseIntValue(&date_str, &hour, 0, 23, "hour", argument);
561 parseIntValue(&date_str, &minute, 0, 59, "minute", argument);
562 parseIntValue(&date_str, &second, 0, 59, "second", argument);
568 gmt.tm_mon = month - 1;
569 gmt.tm_year = year -1900;
570 gmt.tm_isdst = 0; // ignore daylight savings time for the moment
572 time_t theTime = sgTimeGetGMT( gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
573 gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
575 SG_LOG(SG_GENERAL, SG_INFO, "Configuring startup time to " << ctime(&theTime));
581 // parse angle in the form of [+/-]ddd:mm:ss into degrees
583 parse_degree( const string& degree_str) {
584 double result = parse_time( degree_str );
586 // printf("Degree = %.4f\n", result);
592 // parse time offset string into seconds
594 parse_time_offset( const string& time_str) {
597 // printf("time offset = %s\n", time_str);
600 result = (int)rint(parse_time(time_str) * 3600.0);
602 result = (int)(parse_time(time_str) * 3600.0);
605 // printf("parse_time_offset(): %d\n", result);
611 // Parse --fov=x.xx type option
613 parse_fov( const string& arg ) {
614 double fov = atof(arg);
616 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
617 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
619 fgSetDouble("/sim/view[0]/config/default-field-of-view-deg", fov);
621 // printf("parse_fov(): result = %.4f\n", fov);
627 // Parse I/O channel option
629 // Format is "--protocol=medium,direction,hz,medium_options,..."
631 // protocol = { native, nmea, garmin, AV400, AV400Sim, fgfs, rul, pve, etc. }
632 // medium = { serial, socket, file, etc. }
633 // direction = { in, out, bi }
634 // hz = number of times to process channel per second (floating
635 // point values are ok.
637 // Serial example "--nmea=serial,dir,hz,device,baud" where
639 // device = OS device name of serial line to be open()'ed
640 // baud = {300, 1200, 2400, ..., 230400}
642 // Socket exacmple "--native=socket,dir,hz,machine,port,style" where
644 // machine = machine name or ip address if client (leave empty if server)
645 // port = port, leave empty to let system choose
646 // style = tcp or udp
648 // File example "--garmin=file,dir,hz,filename" where
650 // filename = file system file name
653 add_channel( const string& type, const string& channel_str ) {
654 // This check is neccessary to prevent fgviewer from segfaulting when given
655 // weird options. (It doesn't run the full initailization)
656 if(!globals->get_channel_options_list())
658 SG_LOG(SG_GENERAL, SG_ALERT, "Option " << type << "=" << channel_str
662 SG_LOG(SG_GENERAL, SG_INFO, "Channel string = " << channel_str );
663 globals->get_channel_options_list()->push_back( type + "," + channel_str );
670 fgSetString("/sim/presets/airport-id", "");
671 fgSetString("/sim/presets/vor-id", "");
672 fgSetString("/sim/presets/ndb-id", "");
673 fgSetString("/sim/presets/carrier", "");
674 fgSetString("/sim/presets/parkpos", "");
675 fgSetString("/sim/presets/fix", "");
679 fgOptVOR( const char * arg )
682 fgSetString("/sim/presets/vor-id", arg);
683 return FG_OPTIONS_OK;
687 fgOptNDB( const char * arg )
690 fgSetString("/sim/presets/ndb-id", arg);
691 return FG_OPTIONS_OK;
695 fgOptCarrier( const char * arg )
698 fgSetString("/sim/presets/carrier", arg);
699 return FG_OPTIONS_OK;
703 fgOptParkpos( const char * arg )
705 fgSetString("/sim/presets/parkpos", arg);
706 return FG_OPTIONS_OK;
710 fgOptFIX( const char * arg )
713 fgSetString("/sim/presets/fix", arg);
714 return FG_OPTIONS_OK;
718 fgOptLon( const char *arg )
721 fgSetDouble("/sim/presets/longitude-deg", parse_degree( arg ));
722 fgSetDouble("/position/longitude-deg", parse_degree( arg ));
723 return FG_OPTIONS_OK;
727 fgOptLat( const char *arg )
730 fgSetDouble("/sim/presets/latitude-deg", parse_degree( arg ));
731 fgSetDouble("/position/latitude-deg", parse_degree( arg ));
732 return FG_OPTIONS_OK;
736 fgOptAltitude( const char *arg )
738 fgSetBool("/sim/presets/onground", false);
739 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
740 fgSetDouble("/sim/presets/altitude-ft", atof( arg ));
742 fgSetDouble("/sim/presets/altitude-ft",
743 atof( arg ) * SG_METER_TO_FEET);
744 return FG_OPTIONS_OK;
748 fgOptUBody( const char *arg )
750 fgSetString("/sim/presets/speed-set", "UVW");
751 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
752 fgSetDouble("/sim/presets/uBody-fps", atof( arg ));
754 fgSetDouble("/sim/presets/uBody-fps",
755 atof( arg ) * SG_METER_TO_FEET);
756 return FG_OPTIONS_OK;
760 fgOptVBody( const char *arg )
762 fgSetString("/sim/presets/speed-set", "UVW");
763 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
764 fgSetDouble("/sim/presets/vBody-fps", atof( arg ));
766 fgSetDouble("/sim/presets/vBody-fps",
767 atof( arg ) * SG_METER_TO_FEET);
768 return FG_OPTIONS_OK;
772 fgOptWBody( const char *arg )
774 fgSetString("/sim/presets/speed-set", "UVW");
775 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
776 fgSetDouble("/sim/presets/wBody-fps", atof(arg));
778 fgSetDouble("/sim/presets/wBody-fps",
779 atof(arg) * SG_METER_TO_FEET);
780 return FG_OPTIONS_OK;
784 fgOptVNorth( const char *arg )
786 fgSetString("/sim/presets/speed-set", "NED");
787 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
788 fgSetDouble("/sim/presets/speed-north-fps", atof( arg ));
790 fgSetDouble("/sim/presets/speed-north-fps",
791 atof( arg ) * SG_METER_TO_FEET);
792 return FG_OPTIONS_OK;
796 fgOptVEast( const char *arg )
798 fgSetString("/sim/presets/speed-set", "NED");
799 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
800 fgSetDouble("/sim/presets/speed-east-fps", atof(arg));
802 fgSetDouble("/sim/presets/speed-east-fps",
803 atof(arg) * SG_METER_TO_FEET);
804 return FG_OPTIONS_OK;
808 fgOptVDown( const char *arg )
810 fgSetString("/sim/presets/speed-set", "NED");
811 if ( !strcmp(fgGetString("/sim/startup/units"), "feet") )
812 fgSetDouble("/sim/presets/speed-down-fps", atof(arg));
814 fgSetDouble("/sim/presets/speed-down-fps",
815 atof(arg) * SG_METER_TO_FEET);
816 return FG_OPTIONS_OK;
820 fgOptVc( const char *arg )
822 // fgSetString("/sim/presets/speed-set", "knots");
823 // fgSetDouble("/velocities/airspeed-kt", atof(arg.substr(5)));
824 fgSetString("/sim/presets/speed-set", "knots");
825 fgSetDouble("/sim/presets/airspeed-kt", atof(arg));
826 return FG_OPTIONS_OK;
830 fgOptMach( const char *arg )
832 fgSetString("/sim/presets/speed-set", "mach");
833 fgSetDouble("/sim/presets/mach", atof(arg));
834 return FG_OPTIONS_OK;
838 fgOptRoc( const char *arg )
840 fgSetDouble("/sim/presets/vertical-speed-fps", atof(arg)/60);
841 return FG_OPTIONS_OK;
845 fgOptFgScenery( const char *arg )
847 globals->append_fg_scenery(arg);
848 return FG_OPTIONS_OK;
852 fgOptFov( const char *arg )
855 return FG_OPTIONS_OK;
859 fgOptGeometry( const char *arg )
861 bool geometry_ok = true;
862 int xsize = 0, ysize = 0;
863 string geometry = arg;
864 string::size_type i = geometry.find('x');
866 if (i != string::npos) {
867 xsize = atoi(geometry.substr(0, i));
868 ysize = atoi(geometry.substr(i+1));
873 if ( xsize <= 0 || ysize <= 0 ) {
879 if ( !geometry_ok ) {
880 SG_LOG( SG_GENERAL, SG_ALERT, "Unknown geometry: " << geometry );
881 SG_LOG( SG_GENERAL, SG_ALERT,
882 "Setting geometry to " << xsize << 'x' << ysize << '\n');
884 SG_LOG( SG_GENERAL, SG_INFO,
885 "Setting geometry to " << xsize << 'x' << ysize << '\n');
886 fgSetInt("/sim/startup/xsize", xsize);
887 fgSetInt("/sim/startup/ysize", ysize);
889 return FG_OPTIONS_OK;
893 fgOptBpp( const char *arg )
895 string bits_per_pix = arg;
896 if ( bits_per_pix == "16" ) {
897 fgSetInt("/sim/rendering/bits-per-pixel", 16);
898 } else if ( bits_per_pix == "24" ) {
899 fgSetInt("/sim/rendering/bits-per-pixel", 24);
900 } else if ( bits_per_pix == "32" ) {
901 fgSetInt("/sim/rendering/bits-per-pixel", 32);
903 SG_LOG(SG_GENERAL, SG_ALERT, "Unsupported bpp " << bits_per_pix);
905 return FG_OPTIONS_OK;
909 fgOptTimeOffset( const char *arg )
911 fgSetLong("/sim/startup/time-offset",
912 parse_time_offset( arg ));
913 fgSetString("/sim/startup/time-offset-type", "system-offset");
914 return FG_OPTIONS_OK;
918 fgOptStartDateSys( const char *arg )
920 long int theTime = parse_date( arg, "system" );
923 fgSetLong("/sim/startup/time-offset", theTime);
924 fgSetString("/sim/startup/time-offset-type", "system");
926 return FG_OPTIONS_OK;
930 fgOptStartDateLat( const char *arg )
932 long int theTime = parse_date( arg, "latitude" );
935 fgSetLong("/sim/startup/time-offset", theTime);
936 fgSetString("/sim/startup/time-offset-type", "latitude");
938 return FG_OPTIONS_OK;
942 fgOptStartDateGmt( const char *arg )
944 long int theTime = parse_date( arg, "gmt" );
947 fgSetLong("/sim/startup/time-offset", theTime);
948 fgSetString("/sim/startup/time-offset-type", "gmt");
950 return FG_OPTIONS_OK;
954 fgSetupProxy( const char *arg )
956 string options = simgear::strutils::strip( arg );
957 string host, port, auth;
958 string::size_type pos;
960 // this is NURLP - NURLP is not an url parser
961 if( simgear::strutils::starts_with( options, "http://" ) )
962 options = options.substr( 7 );
963 if( simgear::strutils::ends_with( options, "/" ) )
964 options = options.substr( 0, options.length() - 1 );
966 host = port = auth = "";
967 if ((pos = options.find("@")) != string::npos)
968 auth = options.substr(0, pos++);
972 host = options.substr(pos, options.size());
973 if ((pos = host.find(":")) != string::npos) {
974 port = host.substr(++pos, host.size());
975 host.erase(--pos, host.size());
978 fgSetString("/sim/presets/proxy/host", host.c_str());
979 fgSetString("/sim/presets/proxy/port", port.c_str());
980 fgSetString("/sim/presets/proxy/authentication", auth.c_str());
982 return FG_OPTIONS_OK;
986 fgOptTraceRead( const char *arg )
989 SG_LOG(SG_GENERAL, SG_INFO, "Tracing reads for property " << name);
990 fgGetNode(name.c_str(), true)
991 ->setAttribute(SGPropertyNode::TRACE_READ, true);
992 return FG_OPTIONS_OK;
996 fgOptLogLevel( const char *arg )
998 fgSetString("/sim/logging/priority", arg);
999 setLoggingPriority(arg);
1001 return FG_OPTIONS_OK;
1005 fgOptLogClasses( const char *arg )
1007 fgSetString("/sim/logging/classes", arg);
1008 setLoggingClasses (arg);
1010 return FG_OPTIONS_OK;
1014 fgOptTraceWrite( const char *arg )
1017 SG_LOG(SG_GENERAL, SG_INFO, "Tracing writes for property " << name);
1018 fgGetNode(name.c_str(), true)
1019 ->setAttribute(SGPropertyNode::TRACE_WRITE, true);
1020 return FG_OPTIONS_OK;
1024 fgOptViewOffset( const char *arg )
1026 // $$$ begin - added VS Renganathan, 14 Oct 2K
1027 // for multi-window outside window imagery
1028 string woffset = arg;
1029 double default_view_offset = 0.0;
1030 if ( woffset == "LEFT" ) {
1031 default_view_offset = SGD_PI * 0.25;
1032 } else if ( woffset == "RIGHT" ) {
1033 default_view_offset = SGD_PI * 1.75;
1034 } else if ( woffset == "CENTER" ) {
1035 default_view_offset = 0.00;
1037 default_view_offset = atof( woffset.c_str() ) * SGD_DEGREES_TO_RADIANS;
1039 /* apparently not used (CLO, 11 Jun 2002)
1040 FGViewer *pilot_view =
1041 (FGViewer *)globals->get_viewmgr()->get_view( 0 ); */
1042 // this will work without calls to the viewer...
1043 fgSetDouble( "/sim/current-view/heading-offset-deg",
1044 default_view_offset * SGD_RADIANS_TO_DEGREES );
1045 // $$$ end - added VS Renganathan, 14 Oct 2K
1046 return FG_OPTIONS_OK;
1050 fgOptVisibilityMeters( const char *arg )
1052 Environment::Presets::VisibilitySingleton::instance()->preset( atof( arg ) );
1053 return FG_OPTIONS_OK;
1057 fgOptVisibilityMiles( const char *arg )
1059 Environment::Presets::VisibilitySingleton::instance()->preset( atof( arg ) * 5280.0 * SG_FEET_TO_METER );
1060 return FG_OPTIONS_OK;
1064 fgOptRandomWind( const char *arg )
1066 double min_hdg = sg_random() * 360.0;
1067 double max_hdg = min_hdg + (20 - sqrt(sg_random() * 400));
1068 double speed = sg_random() * sg_random() * 40;
1069 double gust = speed + (10 - sqrt(sg_random() * 100));
1070 Environment::Presets::WindSingleton::instance()->preset(min_hdg, max_hdg, speed, gust);
1071 return FG_OPTIONS_OK;
1075 fgOptWind( const char *arg )
1077 double min_hdg = 0.0, max_hdg = 0.0, speed = 0.0, gust = 0.0;
1078 if (!parse_wind( arg, &min_hdg, &max_hdg, &speed, &gust)) {
1079 SG_LOG( SG_GENERAL, SG_ALERT, "bad wind value " << arg );
1080 return FG_OPTIONS_ERROR;
1082 Environment::Presets::WindSingleton::instance()->preset(min_hdg, max_hdg, speed, gust);
1083 return FG_OPTIONS_OK;
1087 fgOptTurbulence( const char *arg )
1089 Environment::Presets::TurbulenceSingleton::instance()->preset( atof(arg) );
1090 return FG_OPTIONS_OK;
1094 fgOptCeiling( const char *arg )
1096 double elevation, thickness;
1098 string::size_type pos = spec.find(':');
1099 if (pos == string::npos) {
1100 elevation = atof(spec.c_str());
1103 elevation = atof(spec.substr(0, pos).c_str());
1104 thickness = atof(spec.substr(pos + 1).c_str());
1106 Environment::Presets::CeilingSingleton::instance()->preset( elevation, thickness );
1107 return FG_OPTIONS_OK;
1111 fgOptWp( const char *arg )
1113 string_list *waypoints = globals->get_initial_waypoints();
1115 waypoints = new string_list;
1116 globals->set_initial_waypoints(waypoints);
1118 waypoints->push_back(arg);
1119 return FG_OPTIONS_OK;
1123 fgOptConfig( const char *arg )
1127 readProperties(file, globals->get_props());
1128 } catch (const sg_exception &e) {
1129 string message = "Error loading config file: ";
1130 message += e.getFormattedMessage() + e.getOrigin();
1131 SG_LOG(SG_INPUT, SG_ALERT, message);
1132 return FG_OPTIONS_ERROR;
1134 return FG_OPTIONS_OK;
1138 parse_colon (const string &s, double * val1, double * val2)
1140 string::size_type pos = s.find(':');
1141 if (pos == string::npos) {
1145 *val1 = atof(s.substr(0, pos).c_str());
1146 *val2 = atof(s.substr(pos+1).c_str());
1153 fgOptFailure( const char * arg )
1157 fgSetBool("/systems/pitot/serviceable", false);
1158 } else if (a == "static") {
1159 fgSetBool("/systems/static/serviceable", false);
1160 } else if (a == "vacuum") {
1161 fgSetBool("/systems/vacuum/serviceable", false);
1162 } else if (a == "electrical") {
1163 fgSetBool("/systems/electrical/serviceable", false);
1165 SG_LOG(SG_INPUT, SG_ALERT, "Unknown failure mode: " << a);
1166 return FG_OPTIONS_ERROR;
1169 return FG_OPTIONS_OK;
1174 fgOptNAV1( const char * arg )
1176 double radial, freq;
1177 if (parse_colon(arg, &radial, &freq))
1178 fgSetDouble("/instrumentation/nav[0]/radials/selected-deg", radial);
1179 fgSetDouble("/instrumentation/nav[0]/frequencies/selected-mhz", freq);
1180 return FG_OPTIONS_OK;
1184 fgOptNAV2( const char * arg )
1186 double radial, freq;
1187 if (parse_colon(arg, &radial, &freq))
1188 fgSetDouble("/instrumentation/nav[1]/radials/selected-deg", radial);
1189 fgSetDouble("/instrumentation/nav[1]/frequencies/selected-mhz", freq);
1190 return FG_OPTIONS_OK;
1194 fgOptADF1( const char * arg )
1197 if (parse_colon(arg, &rot, &freq))
1198 fgSetDouble("/instrumentation/adf[0]/rotation-deg", rot);
1199 fgSetDouble("/instrumentation/adf[0]/frequencies/selected-khz", freq);
1200 return FG_OPTIONS_OK;
1204 fgOptADF2( const char * arg )
1207 if (parse_colon(arg, &rot, &freq))
1208 fgSetDouble("/instrumentation/adf[1]/rotation-deg", rot);
1209 fgSetDouble("/instrumentation/adf[1]/frequencies/selected-khz", freq);
1210 return FG_OPTIONS_OK;
1214 fgOptDME( const char *arg )
1217 if (opt == "nav1") {
1218 fgSetInt("/instrumentation/dme/switch-position", 1);
1219 fgSetString("/instrumentation/dme/frequencies/source",
1220 "/instrumentation/nav[0]/frequencies/selected-mhz");
1221 } else if (opt == "nav2") {
1222 fgSetInt("/instrumentation/dme/switch-position", 3);
1223 fgSetString("/instrumentation/dme/frequencies/source",
1224 "/instrumentation/nav[1]/frequencies/selected-mhz");
1226 double frequency = atof(arg);
1229 SG_LOG(SG_INPUT, SG_ALERT, "Invalid DME frequency: '" << arg << "'.");
1230 return FG_OPTIONS_ERROR;
1232 fgSetInt("/instrumentation/dme/switch-position", 2);
1233 fgSetString("/instrumentation/dme/frequencies/source",
1234 "/instrumentation/dme/frequencies/selected-mhz");
1235 fgSetDouble("/instrumentation/dme/frequencies/selected-mhz", frequency);
1237 return FG_OPTIONS_OK;
1241 fgOptLivery( const char *arg )
1244 string livery_path = "livery/" + opt;
1245 fgSetString("/sim/model/texture-path", livery_path.c_str() );
1246 return FG_OPTIONS_OK;
1250 fgOptScenario( const char *arg )
1252 SGPropertyNode_ptr ai_node = fgGetNode( "/sim/ai", true );
1253 vector<SGPropertyNode_ptr> scenarii = ai_node->getChildren( "scenario" );
1255 for ( size_t i = 0; i < scenarii.size(); ++i ) {
1256 int ind = scenarii[i]->getIndex();
1257 if ( index < ind ) {
1261 SGPropertyNode_ptr scenario = ai_node->getNode( "scenario", index + 1, true );
1262 scenario->setStringValue( arg );
1263 return FG_OPTIONS_OK;
1267 fgOptRunway( const char *arg )
1269 fgSetString("/sim/presets/runway", arg );
1270 fgSetBool("/sim/presets/runway-requested", true );
1271 return FG_OPTIONS_OK;
1275 fgOptParking( const char *arg )
1277 cerr << "Processing argument " << arg << endl;
1278 fgSetString("/sim/presets/parking", arg );
1279 fgSetBool ("/sim/presets/parking-requested", true );
1280 return FG_OPTIONS_OK;
1284 fgOptVersion( const char *arg )
1286 cerr << "FlightGear version: " << FLIGHTGEAR_VERSION << endl;
1287 cerr << "Revision: " << REVISION << endl;
1288 cerr << "Build-Id: " << HUDSON_BUILD_ID << endl;
1289 cerr << "FG_ROOT=" << globals->get_fg_root() << endl;
1290 cerr << "FG_HOME=" << globals->get_fg_home() << endl;
1291 cerr << "FG_SCENERY=";
1294 string_list scn = globals->get_fg_scenery();
1295 for (string_list::const_iterator it = scn.begin(); it != scn.end(); it++)
1297 if (didsome) cerr << ":";
1302 cerr << "SimGear version: " << SG_STRINGIZE(SIMGEAR_VERSION) << endl;
1303 cerr << "PLIB version: " << PLIB_VERSION << endl;
1304 return FG_OPTIONS_EXIT;
1308 fgOptCallSign(const char * arg)
1312 strncpy(callsign,arg,10);
1314 for (i=0;callsign[i];i++)
1316 char c = callsign[i];
1317 if (c >= 'A' && c <= 'Z') continue;
1318 if (c >= 'a' && c <= 'z') continue;
1319 if (c >= '0' && c <= '9') continue;
1320 if (c == '-' || c == '_') continue;
1321 // convert any other illegal characters
1324 fgSetString("sim/multiplay/callsign", callsign );
1325 return FG_OPTIONS_OK;
1329 fgOptIgnoreAutosave(const char* arg)
1331 fgSetBool("/sim/startup/ignore-autosave", true);
1332 // don't overwrite autosave on exit
1333 fgSetBool("/sim/startup/save-on-exit", false);
1334 return FG_OPTIONS_OK;
1337 // Set a property for the --prop: option. Syntax: --prop:[<type>:]<name>=<value>
1338 // <type> can be "double" etc. but also only the first letter "d".
1339 // Examples: --prop:alpha=1 --prop:bool:beta=true --prop:d:gamma=0.123
1341 fgOptSetProperty(const char* raw)
1344 string::size_type pos = arg.find('=');
1345 if (pos == arg.npos || pos == 0 || pos + 1 == arg.size())
1346 return FG_OPTIONS_ERROR;
1348 string name = arg.substr(0, pos);
1349 string value = arg.substr(pos + 1);
1351 pos = name.find(':');
1353 if (pos != name.npos && pos != 0 && pos + 1 != name.size()) {
1354 type = name.substr(0, pos);
1355 name = name.substr(pos + 1);
1357 SGPropertyNode *n = fgGetNode(name.c_str(), true);
1359 bool writable = n->getAttribute(SGPropertyNode::WRITE);
1361 n->setAttribute(SGPropertyNode::WRITE, true);
1365 ret = n->setUnspecifiedValue(value.c_str());
1366 else if (type == "s" || type == "string")
1367 ret = n->setStringValue(value.c_str());
1368 else if (type == "d" || type == "double")
1369 ret = n->setDoubleValue(strtod(value.c_str(), 0));
1370 else if (type == "f" || type == "float")
1371 ret = n->setFloatValue(atof(value.c_str()));
1372 else if (type == "l" || type == "long")
1373 ret = n->setLongValue(strtol(value.c_str(), 0, 0));
1374 else if (type == "i" || type == "int")
1375 ret = n->setIntValue(atoi(value.c_str()));
1376 else if (type == "b" || type == "bool")
1377 ret = n->setBoolValue(value == "true" || atoi(value.c_str()) != 0);
1380 n->setAttribute(SGPropertyNode::WRITE, false);
1381 return ret ? FG_OPTIONS_OK : FG_OPTIONS_ERROR;
1387 option has_param type property b_param s_param func
1390 option : name of the option
1391 has_param : option is --name=value if true or --name if false
1392 type : OPTION_BOOL - property is a boolean
1393 OPTION_STRING - property is a string
1394 OPTION_DOUBLE - property is a double
1395 OPTION_INT - property is an integer
1396 OPTION_CHANNEL - name of option is the name of a channel
1397 OPTION_FUNC - the option trigger a function
1398 b_param : if type==OPTION_BOOL,
1399 value set to the property (has_param is false for boolean)
1400 s_param : if type==OPTION_STRING,
1401 value set to the property if has_param is false
1402 func : function called if type==OPTION_FUNC. if has_param is true,
1403 the value is passed to the function as a string, otherwise,
1406 For OPTION_DOUBLE and OPTION_INT, the parameter value is converted into a
1407 double or an integer and set to the property.
1409 For OPTION_CHANNEL, add_channel is called with the parameter value as the
1413 enum OptionType { OPTION_BOOL = 0, OPTION_STRING, OPTION_DOUBLE, OPTION_INT, OPTION_CHANNEL, OPTION_FUNC, OPTION_IGNORE };
1414 const int OPTION_MULTI = 1 << 17;
1420 const char *property;
1422 const char *s_param;
1423 int (*func)( const char * );
1424 } fgOptionArray[] = {
1426 {"language", true, OPTION_IGNORE, "", false, "", 0 },
1427 {"console", false, OPTION_IGNORE, "", false, "", 0 },
1428 {"disable-rembrandt", false, OPTION_BOOL, "/sim/rendering/rembrandt/enabled", false, "", 0 },
1429 {"enable-rembrandt", false, OPTION_BOOL, "/sim/rendering/rembrandt/enabled", true, "", 0 },
1430 {"renderer", true, OPTION_STRING, "/sim/rendering/rembrandt/renderer", false, "", 0 },
1431 {"disable-splash-screen", false, OPTION_BOOL, "/sim/startup/splash-screen", false, "", 0 },
1432 {"enable-splash-screen", false, OPTION_BOOL, "/sim/startup/splash-screen", true, "", 0 },
1433 {"disable-mouse-pointer", false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "disabled", 0 },
1434 {"enable-mouse-pointer", false, OPTION_STRING, "/sim/startup/mouse-pointer", false, "enabled", 0 },
1435 {"disable-random-objects", false, OPTION_BOOL, "/sim/rendering/random-objects", false, "", 0 },
1436 {"enable-random-objects", false, OPTION_BOOL, "/sim/rendering/random-objects", true, "", 0 },
1437 {"disable-random-vegetation", false, OPTION_BOOL, "/sim/rendering/random-vegetation", false, "", 0 },
1438 {"enable-random-vegetation", false, OPTION_BOOL, "/sim/rendering/random-vegetation", true, "", 0 },
1439 {"disable-random-buildings", false, OPTION_BOOL, "/sim/rendering/random-buildings", false, "", 0 },
1440 {"enable-random-buildings", false, OPTION_BOOL, "/sim/rendering/random-buildings", true, "", 0 },
1441 {"disable-real-weather-fetch", false, OPTION_BOOL, "/environment/realwx/enabled", false, "", 0 },
1442 {"enable-real-weather-fetch", false, OPTION_BOOL, "/environment/realwx/enabled", true, "", 0 },
1443 {"metar", true, OPTION_STRING, "/environment/metar/data", false, "", 0 },
1444 {"disable-ai-models", false, OPTION_BOOL, "/sim/ai/enabled", false, "", 0 },
1445 {"enable-ai-models", false, OPTION_BOOL, "/sim/ai/enabled", true, "", 0 },
1446 {"disable-ai-traffic", false, OPTION_BOOL, "/sim/traffic-manager/enabled", false, "", 0 },
1447 {"enable-ai-traffic", false, OPTION_BOOL, "/sim/traffic-manager/enabled", true, "", 0 },
1448 {"disable-freeze", false, OPTION_BOOL, "/sim/freeze/master", false, "", 0 },
1449 {"enable-freeze", false, OPTION_BOOL, "/sim/freeze/master", true, "", 0 },
1450 {"disable-fuel-freeze", false, OPTION_BOOL, "/sim/freeze/fuel", false, "", 0 },
1451 {"enable-fuel-freeze", false, OPTION_BOOL, "/sim/freeze/fuel", true, "", 0 },
1452 {"disable-clock-freeze", false, OPTION_BOOL, "/sim/freeze/clock", false, "", 0 },
1453 {"enable-clock-freeze", false, OPTION_BOOL, "/sim/freeze/clock", true, "", 0 },
1454 {"disable-hud-3d", false, OPTION_BOOL, "/sim/hud/enable3d[1]", false, "", 0 },
1455 {"enable-hud-3d", false, OPTION_BOOL, "/sim/hud/enable3d[1]", true, "", 0 },
1456 {"disable-anti-alias-hud", false, OPTION_BOOL, "/sim/hud/color/antialiased", false, "", 0 },
1457 {"enable-anti-alias-hud", false, OPTION_BOOL, "/sim/hud/color/antialiased", true, "", 0 },
1458 {"control", true, OPTION_STRING, "/sim/control-mode", false, "", 0 },
1459 {"disable-auto-coordination", false, OPTION_BOOL, "/controls/flight/auto-coordination", false, "", 0 },
1460 {"enable-auto-coordination", false, OPTION_BOOL, "/controls/flight/auto-coordination", true, "", 0 },
1461 {"browser-app", true, OPTION_STRING, "/sim/startup/browser-app", false, "", 0 },
1462 {"disable-hud", false, OPTION_BOOL, "/sim/hud/visibility[1]", false, "", 0 },
1463 {"enable-hud", false, OPTION_BOOL, "/sim/hud/visibility[1]", true, "", 0 },
1464 {"disable-panel", false, OPTION_BOOL, "/sim/panel/visibility", false, "", 0 },
1465 {"enable-panel", false, OPTION_BOOL, "/sim/panel/visibility", true, "", 0 },
1466 {"disable-sound", false, OPTION_BOOL, "/sim/sound/working", false, "", 0 },
1467 {"enable-sound", false, OPTION_BOOL, "/sim/sound/working", true, "", 0 },
1468 {"sound-device", true, OPTION_STRING, "/sim/sound/device-name", false, "", 0 },
1469 {"airport", true, OPTION_STRING, "/sim/presets/airport-id", false, "", 0 },
1470 {"runway", true, OPTION_FUNC, "", false, "", fgOptRunway },
1471 {"vor", true, OPTION_FUNC, "", false, "", fgOptVOR },
1472 {"vor-frequency", true, OPTION_DOUBLE, "/sim/presets/vor-freq", false, "", fgOptVOR },
1473 {"ndb", true, OPTION_FUNC, "", false, "", fgOptNDB },
1474 {"ndb-frequency", true, OPTION_DOUBLE, "/sim/presets/ndb-freq", false, "", fgOptVOR },
1475 {"carrier", true, OPTION_FUNC, "", false, "", fgOptCarrier },
1476 {"parkpos", true, OPTION_FUNC, "", false, "", fgOptParkpos },
1477 {"fix", true, OPTION_FUNC, "", false, "", fgOptFIX },
1478 {"offset-distance", true, OPTION_DOUBLE, "/sim/presets/offset-distance-nm", false, "", 0 },
1479 {"offset-azimuth", true, OPTION_DOUBLE, "/sim/presets/offset-azimuth-deg", false, "", 0 },
1480 {"lon", true, OPTION_FUNC, "", false, "", fgOptLon },
1481 {"lat", true, OPTION_FUNC, "", false, "", fgOptLat },
1482 {"altitude", true, OPTION_FUNC, "", false, "", fgOptAltitude },
1483 {"uBody", true, OPTION_FUNC, "", false, "", fgOptUBody },
1484 {"vBody", true, OPTION_FUNC, "", false, "", fgOptVBody },
1485 {"wBody", true, OPTION_FUNC, "", false, "", fgOptWBody },
1486 {"vNorth", true, OPTION_FUNC, "", false, "", fgOptVNorth },
1487 {"vEast", true, OPTION_FUNC, "", false, "", fgOptVEast },
1488 {"vDown", true, OPTION_FUNC, "", false, "", fgOptVDown },
1489 {"vc", true, OPTION_FUNC, "", false, "", fgOptVc },
1490 {"mach", true, OPTION_FUNC, "", false, "", fgOptMach },
1491 {"heading", true, OPTION_DOUBLE, "/sim/presets/heading-deg", false, "", 0 },
1492 {"roll", true, OPTION_DOUBLE, "/sim/presets/roll-deg", false, "", 0 },
1493 {"pitch", true, OPTION_DOUBLE, "/sim/presets/pitch-deg", false, "", 0 },
1494 {"glideslope", true, OPTION_DOUBLE, "/sim/presets/glideslope-deg", false, "", 0 },
1495 {"roc", true, OPTION_FUNC, "", false, "", fgOptRoc },
1496 {"fg-root", true, OPTION_IGNORE, "", false, "", 0 },
1497 {"fg-scenery", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptFgScenery },
1498 {"fg-aircraft", true, OPTION_IGNORE | OPTION_MULTI, "", false, "", 0 },
1499 {"fdm", true, OPTION_STRING, "/sim/flight-model", false, "", 0 },
1500 {"aero", true, OPTION_STRING, "/sim/aero", false, "", 0 },
1501 {"aircraft-dir", true, OPTION_IGNORE, "", false, "", 0 },
1502 {"model-hz", true, OPTION_INT, "/sim/model-hz", false, "", 0 },
1503 {"max-fps", true, OPTION_DOUBLE, "/sim/frame-rate-throttle-hz", false, "", 0 },
1504 {"speed", true, OPTION_DOUBLE, "/sim/speed-up", false, "", 0 },
1505 {"trim", false, OPTION_BOOL, "/sim/presets/trim", true, "", 0 },
1506 {"notrim", false, OPTION_BOOL, "/sim/presets/trim", false, "", 0 },
1507 {"on-ground", false, OPTION_BOOL, "/sim/presets/onground", true, "", 0 },
1508 {"in-air", false, OPTION_BOOL, "/sim/presets/onground", false, "", 0 },
1509 {"fog-disable", false, OPTION_STRING, "/sim/rendering/fog", false, "disabled", 0 },
1510 {"fog-fastest", false, OPTION_STRING, "/sim/rendering/fog", false, "fastest", 0 },
1511 {"fog-nicest", false, OPTION_STRING, "/sim/rendering/fog", false, "nicest", 0 },
1512 {"disable-horizon-effect", false, OPTION_BOOL, "/sim/rendering/horizon-effect", false, "", 0 },
1513 {"enable-horizon-effect", false, OPTION_BOOL, "/sim/rendering/horizon-effect", true, "", 0 },
1514 {"disable-enhanced-lighting", false, OPTION_BOOL, "/sim/rendering/enhanced-lighting", false, "", 0 },
1515 {"enable-enhanced-lighting", false, OPTION_BOOL, "/sim/rendering/enhanced-lighting", true, "", 0 },
1516 {"disable-distance-attenuation", false, OPTION_BOOL, "/sim/rendering/distance-attenuation", false, "", 0 },
1517 {"enable-distance-attenuation", false, OPTION_BOOL, "/sim/rendering/distance-attenuation", true, "", 0 },
1518 {"disable-specular-highlight", false, OPTION_BOOL, "/sim/rendering/specular-highlight", false, "", 0 },
1519 {"enable-specular-highlight", false, OPTION_BOOL, "/sim/rendering/specular-highlight", true, "", 0 },
1520 {"disable-clouds", false, OPTION_BOOL, "/environment/clouds/status", false, "", 0 },
1521 {"enable-clouds", false, OPTION_BOOL, "/environment/clouds/status", true, "", 0 },
1522 {"disable-clouds3d", false, OPTION_BOOL, "/sim/rendering/clouds3d-enable", false, "", 0 },
1523 {"enable-clouds3d", false, OPTION_BOOL, "/sim/rendering/clouds3d-enable", true, "", 0 },
1524 {"fov", true, OPTION_FUNC, "", false, "", fgOptFov },
1525 {"aspect-ratio-multiplier", true, OPTION_DOUBLE, "/sim/current-view/aspect-ratio-multiplier", false, "", 0 },
1526 {"disable-fullscreen", false, OPTION_BOOL, "/sim/startup/fullscreen", false, "", 0 },
1527 {"enable-fullscreen", false, OPTION_BOOL, "/sim/startup/fullscreen", true, "", 0 },
1528 {"disable-save-on-exit", false, OPTION_BOOL, "/sim/startup/save-on-exit", false, "", 0 },
1529 {"enable-save-on-exit", false, OPTION_BOOL, "/sim/startup/save-on-exit", true, "", 0 },
1530 {"read-only", false, OPTION_BOOL, "/sim/fghome-readonly", true, "", 0 },
1531 {"ignore-autosave", false, OPTION_FUNC, "", false, "", fgOptIgnoreAutosave },
1532 {"restore-defaults", false, OPTION_BOOL, "/sim/startup/restore-defaults", true, "", 0 },
1533 {"shading-flat", false, OPTION_BOOL, "/sim/rendering/shading", false, "", 0 },
1534 {"shading-smooth", false, OPTION_BOOL, "/sim/rendering/shading", true, "", 0 },
1535 {"disable-textures", false, OPTION_BOOL, "/sim/rendering/textures", false, "", 0 },
1536 {"enable-textures", false, OPTION_BOOL, "/sim/rendering/textures", true, "", 0 },
1537 {"texture-filtering", false, OPTION_INT, "/sim/rendering/filtering", 1, "", 0 },
1538 {"disable-wireframe", false, OPTION_BOOL, "/sim/rendering/wireframe", false, "", 0 },
1539 {"enable-wireframe", false, OPTION_BOOL, "/sim/rendering/wireframe", true, "", 0 },
1540 {"materials-file", true, OPTION_STRING, "/sim/rendering/materials-file", false, "", 0 },
1541 {"disable-terrasync", false, OPTION_BOOL, "/sim/terrasync/enabled", false, "", 0 },
1542 {"enable-terrasync", false, OPTION_BOOL, "/sim/terrasync/enabled", true, "", 0 },
1543 {"terrasync-dir", true, OPTION_STRING, "/sim/terrasync/scenery-dir", false, "", 0 },
1544 {"geometry", true, OPTION_FUNC, "", false, "", fgOptGeometry },
1545 {"bpp", true, OPTION_FUNC, "", false, "", fgOptBpp },
1546 {"units-feet", false, OPTION_STRING, "/sim/startup/units", false, "feet", 0 },
1547 {"units-meters", false, OPTION_STRING, "/sim/startup/units", false, "meters", 0 },
1548 {"timeofday", true, OPTION_STRING, "/sim/startup/time-offset-type", false, "noon", 0 },
1549 {"season", true, OPTION_STRING, "/sim/startup/season", false, "summer", 0 },
1550 {"time-offset", true, OPTION_FUNC, "", false, "", fgOptTimeOffset },
1551 {"time-match-real", false, OPTION_STRING, "/sim/startup/time-offset-type", false, "system-offset", 0 },
1552 {"time-match-local", false, OPTION_STRING, "/sim/startup/time-offset-type", false, "latitude-offset", 0 },
1553 {"start-date-sys", true, OPTION_FUNC, "", false, "", fgOptStartDateSys },
1554 {"start-date-lat", true, OPTION_FUNC, "", false, "", fgOptStartDateLat },
1555 {"start-date-gmt", true, OPTION_FUNC, "", false, "", fgOptStartDateGmt },
1556 {"hud-tris", false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "tris", 0 },
1557 {"hud-culled", false, OPTION_STRING, "/sim/hud/frame-stat-type", false, "culled", 0 },
1558 {"atcsim", true, OPTION_CHANNEL, "", false, "dummy", 0 },
1559 {"atlas", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1560 {"httpd", true, OPTION_CHANNEL, "", false, "", 0 },
1561 {"jpg-httpd", true, OPTION_CHANNEL, "", false, "", 0 },
1562 {"native", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1563 {"native-ctrls", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1564 {"native-fdm", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1565 {"native-gui", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1566 {"opengc", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1567 {"AV400", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1568 {"AV400Sim", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1569 {"AV400WSimA", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1570 {"AV400WSimB", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1571 {"garmin", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1572 {"igc", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1573 {"nmea", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1574 {"generic", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1575 {"props", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1576 {"telnet", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1577 {"pve", true, OPTION_CHANNEL, "", false, "", 0 },
1578 {"ray", true, OPTION_CHANNEL, "", false, "", 0 },
1579 {"rul", true, OPTION_CHANNEL, "", false, "", 0 },
1580 {"joyclient", true, OPTION_CHANNEL, "", false, "", 0 },
1581 {"jsclient", true, OPTION_CHANNEL, "", false, "", 0 },
1582 {"proxy", true, OPTION_FUNC, "", false, "", fgSetupProxy },
1583 {"callsign", true, OPTION_FUNC, "", false, "", fgOptCallSign},
1584 {"multiplay", true, OPTION_CHANNEL | OPTION_MULTI, "", false, "", 0 },
1586 {"hla", true, OPTION_CHANNEL, "", false, "", 0 },
1587 {"hla-local", true, OPTION_CHANNEL, "", false, "", 0 },
1589 {"trace-read", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptTraceRead },
1590 {"trace-write", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptTraceWrite },
1591 {"log-level", true, OPTION_FUNC, "", false, "", fgOptLogLevel },
1592 {"log-class", true, OPTION_FUNC, "", false, "", fgOptLogClasses },
1593 {"view-offset", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptViewOffset },
1594 {"visibility", true, OPTION_FUNC, "", false, "", fgOptVisibilityMeters },
1595 {"visibility-miles", true, OPTION_FUNC, "", false, "", fgOptVisibilityMiles },
1596 {"random-wind", false, OPTION_FUNC, "", false, "", fgOptRandomWind },
1597 {"wind", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptWind },
1598 {"turbulence", true, OPTION_FUNC, "", false, "", fgOptTurbulence },
1599 {"ceiling", true, OPTION_FUNC, "", false, "", fgOptCeiling },
1600 {"wp", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptWp },
1601 {"flight-plan", true, OPTION_STRING, "/autopilot/route-manager/file-path", false, "", NULL },
1602 {"config", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptConfig },
1603 {"aircraft", true, OPTION_STRING, "/sim/aircraft", false, "", 0 },
1604 {"vehicle", true, OPTION_STRING, "/sim/aircraft", false, "", 0 },
1605 {"failure", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptFailure },
1607 {"enable-fgcom", false, OPTION_BOOL, "/sim/fgcom/enabled", true, "", 0 },
1608 {"disable-fgcom", false, OPTION_BOOL, "/sim/fgcom/enabled", false, "", 0 },
1610 {"com1", true, OPTION_DOUBLE, "/instrumentation/comm[0]/frequencies/selected-mhz", false, "", 0 },
1611 {"com2", true, OPTION_DOUBLE, "/instrumentation/comm[1]/frequencies/selected-mhz", false, "", 0 },
1612 {"nav1", true, OPTION_FUNC, "", false, "", fgOptNAV1 },
1613 {"nav2", true, OPTION_FUNC, "", false, "", fgOptNAV2 },
1614 {"adf", /*legacy*/ true, OPTION_FUNC, "", false, "", fgOptADF1 },
1615 {"adf1", true, OPTION_FUNC, "", false, "", fgOptADF1 },
1616 {"adf2", true, OPTION_FUNC, "", false, "", fgOptADF2 },
1617 {"dme", true, OPTION_FUNC, "", false, "", fgOptDME },
1618 {"min-status", true, OPTION_STRING, "/sim/aircraft-min-status", false, "all", 0 },
1619 {"livery", true, OPTION_FUNC, "", false, "", fgOptLivery },
1620 {"ai-scenario", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptScenario },
1621 {"parking-id", true, OPTION_FUNC, "", false, "", fgOptParking },
1622 {"version", false, OPTION_FUNC, "", false, "", fgOptVersion },
1623 {"enable-fpe", false, OPTION_IGNORE, "", false, "", 0},
1624 {"fgviewer", false, OPTION_IGNORE, "", false, "", 0},
1625 {"no-default-config", false, OPTION_IGNORE, "", false, "", 0},
1626 {"prop", true, OPTION_FUNC | OPTION_MULTI, "", false, "", fgOptSetProperty},
1631 namespace flightgear
1635 * internal storage of a value->option binding
1640 OptionValue(OptionDesc* d, const string& v) :
1648 typedef std::vector<OptionValue> OptionValueVec;
1649 typedef std::map<string, OptionDesc*> OptionDescDict;
1651 class Options::OptionsPrivate
1655 OptionValueVec::const_iterator findValue(const string& key) const
1657 OptionValueVec::const_iterator it = values.begin();
1658 for (; it != values.end(); ++it) {
1660 continue; // ignore markers
1663 if (it->desc->option == key) {
1666 } // of set values iteration
1668 return it; // not found
1671 OptionDesc* findOption(const string& key) const
1673 OptionDescDict::const_iterator it = options.find(key);
1674 if (it == options.end()) {
1681 int processOption(OptionDesc* desc, const string& arg_value)
1684 return FG_OPTIONS_OK; // tolerate marker options
1687 switch ( desc->type & 0xffff ) {
1689 fgSetBool( desc->property, desc->b_param );
1692 if ( desc->has_param && !arg_value.empty() ) {
1693 fgSetString( desc->property, arg_value.c_str() );
1694 } else if ( !desc->has_param && arg_value.empty() ) {
1695 fgSetString( desc->property, desc->s_param );
1696 } else if ( desc->has_param ) {
1697 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1698 return FG_OPTIONS_ERROR;
1700 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" );
1701 return FG_OPTIONS_ERROR;
1705 if ( !arg_value.empty() ) {
1706 fgSetDouble( desc->property, atof( arg_value ) );
1708 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1709 return FG_OPTIONS_ERROR;
1713 if ( !arg_value.empty() ) {
1714 fgSetInt( desc->property, atoi( arg_value ) );
1716 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1717 return FG_OPTIONS_ERROR;
1720 case OPTION_CHANNEL:
1721 // XXX return value of add_channel should be checked?
1722 if ( desc->has_param && !arg_value.empty() ) {
1723 add_channel( desc->option, arg_value );
1724 } else if ( !desc->has_param && arg_value.empty() ) {
1725 add_channel( desc->option, desc->s_param );
1726 } else if ( desc->has_param ) {
1727 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1728 return FG_OPTIONS_ERROR;
1730 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" );
1731 return FG_OPTIONS_ERROR;
1735 if ( desc->has_param && !arg_value.empty() ) {
1736 return desc->func( arg_value.c_str() );
1737 } else if ( !desc->has_param && arg_value.empty() ) {
1738 return desc->func( desc->s_param );
1739 } else if ( desc->has_param ) {
1740 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' needs a parameter" );
1741 return FG_OPTIONS_ERROR;
1743 SG_LOG( SG_GENERAL, SG_ALERT, "Option '" << desc->option << "' does not have a parameter" );
1744 return FG_OPTIONS_ERROR;
1752 return FG_OPTIONS_OK;
1756 * insert a marker value into the values vector. This is necessary
1757 * when processing options, to ensure the correct ordering, where we scan
1758 * for marker values in reverse, and then forwards within each group.
1760 void insertGroupMarker()
1762 values.push_back(OptionValue(NULL, "-"));
1766 * given a current iterator into the values, find the preceeding group marker,
1767 * or return the beginning of the value vector.
1769 OptionValueVec::const_iterator rfindGroup(OptionValueVec::const_iterator pos) const
1771 while (--pos != values.begin()) {
1772 if (pos->desc == NULL) {
1773 return pos; // found a marker, we're done
1783 shouldLoadDefaultConfig;
1785 OptionDescDict options;
1786 OptionValueVec values;
1787 simgear::PathList propertyFiles;
1790 Options* Options::sharedInstance()
1792 if (shared_instance == NULL) {
1793 shared_instance = new Options;
1796 return shared_instance;
1799 Options::Options() :
1800 p(new OptionsPrivate())
1802 p->showHelp = false;
1804 p->showAircraft = false;
1805 p->shouldLoadDefaultConfig = true;
1808 OptionDesc *desc = &fgOptionArray[ 0 ];
1809 while ( desc->option != 0 ) {
1810 p->options[ desc->option ] = desc;
1819 void Options::init(int argc, char **argv, const SGPath& appDataPath)
1821 // first, process the command line
1822 bool inOptions = true;
1823 for (int i=1; i<argc; ++i) {
1824 if (inOptions && (argv[i][0] == '-')) {
1825 if (strcmp(argv[i], "--") == 0) { // end of options delimiter
1830 int result = parseOption(argv[i]);
1831 processArgResult(result);
1833 // XML properties file
1836 SG_LOG(SG_GENERAL, SG_ALERT, "config file not found:" << f.str());
1840 p->propertyFiles.push_back(f);
1842 } // of arguments iteration
1843 p->insertGroupMarker(); // command line is one group
1845 // establish log-level before anything else - otherwise it is not possible
1846 // to show extra (debug/info/warning) messages for the start-up phase.
1847 fgOptLogLevel(valueForOption("log-level", "alert").c_str());
1849 if (!p->shouldLoadDefaultConfig) {
1854 // then config files
1856 std::string homedir;
1857 if (getenv("HOME")) {
1858 homedir = getenv("HOME");
1861 if( !homedir.empty() && !hostname.empty() ) {
1862 // Check for ~/.fgfsrc.hostname
1863 config.set(homedir);
1864 config.append(".fgfsrc");
1865 config.concat( "." );
1866 config.concat( hostname );
1870 // Check for ~/.fgfsrc
1871 if( !homedir.empty() ) {
1872 config.set(homedir);
1873 config.append(".fgfsrc");
1877 // check for a config file in app data
1878 SGPath appDataConfig(appDataPath);
1879 appDataConfig.append("fgfsrc");
1880 if (appDataConfig.exists()) {
1881 readConfig(appDataConfig);
1887 // system.fgfsrc handling
1888 if( ! hostname.empty() ) {
1889 config.set(globals->get_fg_root());
1890 config.append( "system.fgfsrc" );
1891 config.concat( "." );
1892 config.concat( hostname );
1896 config.set(globals->get_fg_root());
1897 config.append( "system.fgfsrc" );
1901 void Options::initAircraft()
1903 BOOST_FOREACH(const string& paths, valuesForOption("fg-aircraft")) {
1904 globals->append_aircraft_paths(paths);
1907 const char* envp = ::getenv("FG_AIRCRAFT");
1909 globals->append_aircraft_paths(envp);
1913 if (isOptionSet("aircraft")) {
1914 aircraft = valueForOption("aircraft");
1915 } else if (isOptionSet("vehicle")) {
1916 aircraft = valueForOption("vehicle");
1919 if (!aircraft.empty()) {
1920 SG_LOG(SG_INPUT, SG_INFO, "aircraft = " << aircraft );
1921 fgSetString("/sim/aircraft", aircraft.c_str() );
1923 SG_LOG(SG_INPUT, SG_INFO, "No user specified aircraft, using default" );
1926 // persist across reset
1927 SGPropertyNode* aircraftProp = fgGetNode("/sim/aircraft", true);
1928 aircraftProp->setAttribute(SGPropertyNode::PRESERVE, true);
1930 if (p->showAircraft) {
1931 fgOptLogLevel( "alert" );
1932 SGPath path( globals->get_fg_root() );
1933 path.append("Aircraft");
1934 fgShowAircraft(path);
1938 if (isOptionSet("aircraft-dir")) {
1939 // set this now, so it's available in FindAndCacheAircraft
1940 fgSetString("/sim/aircraft-dir", valueForOption("aircraft-dir"));
1944 void Options::processArgResult(int result)
1946 if ((result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR))
1948 else if (result == FG_OPTIONS_VERBOSE_HELP)
1950 else if (result == FG_OPTIONS_SHOW_AIRCRAFT) {
1951 p->showAircraft = true;
1952 } else if (result == FG_OPTIONS_NO_DEFAULT_CONFIG) {
1953 p->shouldLoadDefaultConfig = false;
1954 } else if (result == FG_OPTIONS_SHOW_SOUND_DEVICES) {
1958 string vendor = smgr.get_vendor();
1959 string renderer = smgr.get_renderer();
1960 cout << renderer << " provided by " << vendor << endl;
1961 cout << endl << "No. Device" << endl;
1963 vector <const char*>devices = smgr.get_available_devices();
1964 for (vector <const char*>::size_type i=0; i<devices.size(); i++) {
1965 cout << i << ". \"" << devices[i] << "\"" << endl;
1970 } else if (result == FG_OPTIONS_EXIT) {
1975 void Options::readConfig(const SGPath& path)
1977 sg_gzifstream in( path.str() );
1978 if ( !in.is_open() ) {
1982 SG_LOG( SG_GENERAL, SG_INFO, "Processing config file: " << path.str() );
1985 while ( ! in.eof() ) {
1987 getline( in, line, '\n' );
1989 // catch extraneous (DOS) line ending character
1991 for (i = line.length(); i > 0; i--)
1992 if (line[i - 1] > 32)
1994 line = line.substr( 0, i );
1996 if ( parseOption( line ) == FG_OPTIONS_ERROR ) {
1997 cerr << endl << "Config file parse error: " << path.str() << " '"
1998 << line << "'" << endl;
2004 p->insertGroupMarker(); // each config file is a group
2007 int Options::parseOption(const string& s)
2009 if ((s == "--help") || (s=="-h")) {
2010 return FG_OPTIONS_HELP;
2011 } else if ( (s == "--verbose") || (s == "-v") ) {
2012 // verbose help/usage request
2013 return FG_OPTIONS_VERBOSE_HELP;
2014 } else if ((s == "--console") || (s == "-c")) {
2015 simgear::requestConsole();
2016 return FG_OPTIONS_OK;
2017 } else if (s.find("-psn") == 0) {
2018 // on Mac, when launched from the GUI, we are passed the ProcessSerialNumber
2019 // as an argument (and no others). Silently ignore the argument here.
2020 return FG_OPTIONS_OK;
2021 } else if ( s.find( "--show-aircraft") == 0) {
2022 return(FG_OPTIONS_SHOW_AIRCRAFT);
2023 } else if ( s.find( "--show-sound-devices") == 0) {
2024 return(FG_OPTIONS_SHOW_SOUND_DEVICES);
2025 } else if ( s.find( "--no-default-config") == 0) {
2026 return FG_OPTIONS_NO_DEFAULT_CONFIG;
2027 } else if ( s.find( "--prop:") == 0) {
2028 // property setting has a slightly different syntax, so fudge things
2029 OptionDesc* desc = p->findOption("prop");
2030 if (s.find("=", 7) == string::npos) { // no equals token
2031 SG_LOG(SG_GENERAL, SG_ALERT, "malformed property option:" << s);
2032 return FG_OPTIONS_ERROR;
2035 p->values.push_back(OptionValue(desc, s.substr(7)));
2036 return FG_OPTIONS_OK;
2037 } else if ( s.find( "--" ) == 0 ) {
2038 size_t eqPos = s.find( '=' );
2040 if (eqPos == string::npos) {
2043 key = s.substr( 2, eqPos - 2 );
2044 value = s.substr( eqPos + 1);
2047 return addOption(key, value);
2049 flightgear::modalMessageBox("Unknown option", "Unknown command-line option: " + s);
2050 return FG_OPTIONS_ERROR;
2054 int Options::addOption(const string &key, const string &value)
2056 OptionDesc* desc = p->findOption(key);
2058 flightgear::modalMessageBox("Unknown option", "Unknown command-line option: " + key);
2059 return FG_OPTIONS_ERROR;
2062 if (!(desc->type & OPTION_MULTI)) {
2063 OptionValueVec::const_iterator it = p->findValue(key);
2064 if (it != p->values.end()) {
2065 SG_LOG(SG_GENERAL, SG_WARN, "multiple values forbidden for option:" << key << ", ignoring:" << value);
2066 return FG_OPTIONS_OK;
2070 p->values.push_back(OptionValue(desc, value));
2071 return FG_OPTIONS_OK;
2074 bool Options::isOptionSet(const string &key) const
2076 OptionValueVec::const_iterator it = p->findValue(key);
2077 return (it != p->values.end());
2080 string Options::valueForOption(const string& key, const string& defValue) const
2082 OptionValueVec::const_iterator it = p->findValue(key);
2083 if (it == p->values.end()) {
2090 string_list Options::valuesForOption(const std::string& key) const
2093 OptionValueVec::const_iterator it = p->values.begin();
2094 for (; it != p->values.end(); ++it) {
2096 continue; // ignore marker values
2099 if (it->desc->option == key) {
2100 result.push_back(it->value);
2108 static string defaultTerrasyncDir()
2110 #if defined(SG_WINDOWS)
2111 SGPath p(SGPath::documents());
2112 p.append("FlightGear");
2114 SGPath p(globals->get_fg_home());
2116 p.append("TerraSync");
2121 OptionResult Options::processOptions()
2123 // establish locale before showing help (this selects the default locale,
2124 // when no explicit option was set)
2125 globals->get_locale()->selectLanguage(valueForOption("language").c_str());
2127 // now FG_ROOT is setup, process various command line options that bail us
2128 // out quickly, but rely on aircraft / root settings
2131 return FG_OPTIONS_EXIT;
2134 // processing order is complicated. We must process groups LIFO, but the
2135 // values *within* each group in FIFO order, to retain consistency with
2136 // older versions of FG, and existing user configs.
2137 // in practice this means system.fgfsrc must be *processed* before
2138 // .fgfsrc, which must be processed before the command line args, and so on.
2139 OptionValueVec::const_iterator groupEnd = p->values.end();
2141 while (groupEnd != p->values.begin()) {
2142 OptionValueVec::const_iterator groupBegin = p->rfindGroup(groupEnd);
2143 // run over the group in FIFO order
2144 OptionValueVec::const_iterator it;
2145 for (it = groupBegin; it != groupEnd; ++it) {
2146 int result = p->processOption(it->desc, it->value);
2149 case FG_OPTIONS_ERROR:
2151 return FG_OPTIONS_ERROR;
2153 case FG_OPTIONS_EXIT:
2154 return FG_OPTIONS_EXIT;
2161 groupEnd = groupBegin;
2164 BOOST_FOREACH(const SGPath& file, p->propertyFiles) {
2165 if (!file.exists()) {
2166 SG_LOG(SG_GENERAL, SG_ALERT, "config file not found:" << file.str());
2170 SG_LOG(SG_GENERAL, SG_INFO,
2171 "Reading command-line property file " << file.str());
2172 readProperties(file.str(), globals->get_props());
2175 // now options are process, do supplemental fixup
2176 const char *envp = ::getenv( "FG_SCENERY" );
2178 globals->append_fg_scenery(envp);
2181 // terrasync directory fixup
2182 string terrasyncDir = fgGetString("/sim/terrasync/scenery-dir");
2183 if (terrasyncDir.empty()) {
2184 terrasyncDir = defaultTerrasyncDir();
2185 // auto-save it for next time
2187 SG_LOG(SG_GENERAL, SG_INFO,
2188 "Using default TerraSync: " << terrasyncDir);
2189 fgSetString("/sim/terrasync/scenery-dir", terrasyncDir);
2192 SGPath p(terrasyncDir);
2194 // following is necessary to ensure NavDataCache sees stable scenery paths from
2195 // terrasync. Ensure the Terrain and Objects subdirs exist immediately, rather
2196 // than waiting for the first tiles to be scheduled.
2197 simgear::Dir terrainDir(SGPath(p, "Terrain")),
2198 objectsDir(SGPath(p, "Objects"));
2199 if (!terrainDir.exists()) {
2200 terrainDir.create(0755);
2203 if (!objectsDir.exists()) {
2204 objectsDir.create(0755);
2206 if (fgGetBool("/sim/terrasync/enabled")) {
2207 const string_list& scenery_paths(globals->get_fg_scenery());
2208 if (std::find(scenery_paths.begin(), scenery_paths.end(), terrasyncDir) == scenery_paths.end()) {
2209 // terrasync dir is not in the scenery paths, add it
2210 globals->append_fg_scenery(terrasyncDir);
2214 if (globals->get_fg_scenery().empty()) {
2215 // no scenery paths set *at all*, use the data in FG_ROOT
2216 SGPath root(globals->get_fg_root());
2217 root.append("Scenery");
2218 globals->append_fg_scenery(root.str());
2221 return FG_OPTIONS_OK;
2224 void Options::showUsage() const
2226 fgOptLogLevel( "alert" );
2228 FGLocale *locale = globals->get_locale();
2229 SGPropertyNode options_root;
2231 simgear::requestConsole(); // ensure console is shown on Windows
2235 fgLoadProps("options.xml", &options_root);
2236 } catch (const sg_exception &) {
2237 cout << "Unable to read the help file." << endl;
2238 cout << "Make sure the file options.xml is located in the FlightGear base directory," << endl;
2239 cout << "and the location of the base directory is specified by setting $FG_ROOT or" << endl;
2240 cout << "by adding --fg-root=path as a program argument." << endl;
2245 SGPropertyNode *options = options_root.getNode("options");
2247 SG_LOG( SG_GENERAL, SG_ALERT,
2248 "Error reading options.xml: <options> directive not found." );
2252 if (!locale->loadResource("options"))
2254 cout << "Unable to read the language resource." << endl;
2258 const char* usage = locale->getLocalizedString(options->getStringValue("usage"), "options");
2260 cout << usage << endl;
2263 vector<SGPropertyNode_ptr>section = options->getChildren("section");
2264 for (unsigned int j = 0; j < section.size(); j++) {
2267 vector<SGPropertyNode_ptr>option = section[j]->getChildren("option");
2268 for (unsigned int k = 0; k < option.size(); k++) {
2270 SGPropertyNode *name = option[k]->getNode("name");
2271 SGPropertyNode *short_name = option[k]->getNode("short");
2272 SGPropertyNode *key = option[k]->getNode("key");
2273 SGPropertyNode *arg = option[k]->getNode("arg");
2274 bool brief = option[k]->getNode("brief") != 0;
2276 if ((brief || p->verbose) && name) {
2277 string tmp = name->getStringValue();
2281 tmp.append(key->getStringValue());
2285 tmp.append(arg->getStringValue());
2289 tmp.append(short_name->getStringValue());
2292 if (tmp.size() <= 25) {
2295 msg.append( 27-tmp.size(), ' ');
2299 msg.append(32, ' ');
2301 // There may be more than one <description> tag associated
2304 vector<SGPropertyNode_ptr> desc;
2305 desc = option[k]->getChildren("description");
2306 if (! desc.empty()) {
2307 for ( unsigned int l = 0; l < desc.size(); l++) {
2308 string t = desc[l]->getStringValue();
2310 // There may be more than one translation line.
2311 vector<SGPropertyNode_ptr>trans_desc = locale->getLocalizedStrings(t.c_str(),"options");
2312 for ( unsigned int m = 0; m < trans_desc.size(); m++ ) {
2313 string t_str = trans_desc[m]->getStringValue();
2315 if ((m > 0) || ((l > 0) && m == 0)) {
2316 msg.append( 32, ' ');
2319 // If the string is too large to fit on the screen,
2320 // then split it up in several pieces.
2322 while ( t_str.size() > 47 ) {
2324 string::size_type m = t_str.rfind(' ', 47);
2325 msg += t_str.substr(0, m) + '\n';
2326 msg.append( 32, ' ');
2328 t_str.erase(t_str.begin(), t_str.begin() + m + 1);
2330 msg += t_str + '\n';
2337 const char* name = locale->getLocalizedString(section[j]->getStringValue("name"),"options");
2338 if (!msg.empty() && name) {
2339 cout << endl << name << ":" << endl;
2345 if ( !p->verbose ) {
2346 const char* verbose_help = locale->getLocalizedString(options->getStringValue("verbose-help"),"options");
2348 cout << endl << verbose_help << endl;
2351 std::cout << "Hit a key to continue..." << std::endl;
2356 #if defined(__CYGWIN__)
2357 string Options::platformDefaultRoot() const
2362 #elif defined(SG_WINDOWS)
2363 string Options::platformDefaultRoot() const
2367 #elif defined(SG_MAC)
2368 // platformDefaultRoot defined in CocoaHelpers.mm
2370 string Options::platformDefaultRoot() const
2376 void Options::setupRoot()
2379 if (isOptionSet("fg-root")) {
2380 root = valueForOption("fg-root"); // easy!
2382 // Next check if fg-root is set as an env variable
2383 char *envp = ::getenv( "FG_ROOT" );
2384 if ( envp != NULL ) {
2387 root = platformDefaultRoot();
2391 SG_LOG(SG_INPUT, SG_INFO, "fg_root = " << root );
2392 globals->set_fg_root(root);
2395 static char required_version[] = FLIGHTGEAR_VERSION;
2396 string base_version = fgBasePackageVersion();
2397 if (base_version.empty()) {
2398 flightgear::fatalMessageBox("Base package not found",
2399 "Required data files not found, check your installation.",
2400 "Looking for base-package files at: '" + root + "'");
2405 if (base_version != required_version) {
2406 // tell the operator how to use this application
2408 flightgear::fatalMessageBox("Base package version mismatch",
2409 "Version check failed: please check your installation.",
2410 "Found data files for version '" + base_version +
2411 "' at '" + globals->get_fg_root() + "', version '"
2412 + required_version + "' is required.");
2418 bool Options::shouldLoadDefaultConfig() const
2420 return p->shouldLoadDefaultConfig;
2423 } // of namespace flightgear