1 // options.cxx -- class to handle command line options
3 // Written by Curtis Olson, started April 1998.
5 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #if defined(FX) && defined(XMESA)
29 bool global_fullscreen = true;
32 #include <Include/compiler.h>
34 #include <math.h> // rint()
36 #include <stdlib.h> // atof(), atoi()
41 #include <Debug/logstream.hxx>
42 #include <Misc/fgstream.hxx>
43 #include <FDM/flight.hxx>
44 #include <Include/fg_constants.h>
45 #include <Main/options.hxx>
47 #include "fg_serial.hxx"
50 FG_USING_NAMESPACE(std);
54 atof( const string& str )
56 return ::atof( str.c_str() );
60 atoi( const string& str )
62 return ::atoi( str.c_str() );
65 // Defined the shared options class here
66 fgOPTIONS current_options;
70 fgOPTIONS::fgOPTIONS() :
71 // starting longitude in degrees (west = -)
72 // starting latitude in degrees (south = -)
74 // Default initial position is Globe, AZ (P13)
78 // North of the city of Globe
82 // North of the city of Globe
86 // Near where I used to live in Globe, AZ
90 // 10125 Jewell St. NE
94 // Near KHSP (Hot Springs, VA)
95 // lon(-79.8338964 + 0.01),
96 // lat( 37.9514564 + 0.008),
98 // (SEZ) SEDONA airport
99 // lon(-111.7884614 + 0.01),
100 // lat( 34.8486289 - 0.015),
102 // Jim Brennon's Kingmont Observatory
103 // lon(-121.1131667),
106 // Huaras, Peru (S09d 31.871' W077d 31.498')
110 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
115 // lon(-90.1953055556),
116 // lat( 45.4511388889),
118 // starting altitude in meters (this will be reset to ground level
119 // if it is lower than the terrain
122 // Initial Orientation
123 heading(270.0), // heading (yaw) angle in degress (Psi)
124 roll(0.0), // roll angle in degrees (Phi)
125 pitch(0.424), // pitch angle in degrees (Theta)
139 // Flight Model options
140 flight_model(FGInterface::FG_LARCSIM),
143 fog(FG_FOG_NICEST), // nicest
157 units(FG_UNITS_FEET),
164 // set initial values/defaults
165 char* envp = ::getenv( "FG_ROOT" );
167 if ( envp != NULL ) {
168 // fg_root could be anywhere, so default to environmental
169 // variable $FG_ROOT if it is set.
172 // Otherwise, default to a random compiled in location if
173 // $FG_ROOT is not set. This can still be overridden from the
174 // command line or a config file.
177 fg_root = "\\FlightGear";
179 fg_root = "/usr/local/lib/FlightGear";
183 airport_id = ""; // default airport id
185 // initialize port config string list
186 port_options_list.erase ( port_options_list.begin(),
187 port_options_list.end() );
192 fgOPTIONS::parse_time(const string& time_in) {
193 char *time_str, num[256];
194 double hours, minutes, seconds;
199 time_str = (char *)time_in.c_str();
201 // printf("parse_time(): %s\n", time_str);
204 if ( strlen(time_str) ) {
205 if ( time_str[0] == '+' ) {
208 } else if ( time_str[0] == '-' ) {
213 // printf("sign = %d\n", sign);
216 if ( strlen(time_str) ) {
218 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
219 num[i] = time_str[0];
223 if ( time_str[0] == ':' ) {
228 // printf("hours = %.2lf\n", hours);
234 if ( strlen(time_str) ) {
236 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
237 num[i] = time_str[0];
241 if ( time_str[0] == ':' ) {
246 // printf("minutes = %.2lf\n", minutes);
248 result += minutes / 60.0;
252 if ( strlen(time_str) ) {
254 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
255 num[i] = time_str[0];
261 // printf("seconds = %.2lf\n", seconds);
263 result += seconds / 3600.0;
266 return(sign * result);
270 // parse degree in the form of [+/-]hhh:mm:ss
272 fgOPTIONS::parse_degree( const string& degree_str) {
273 double result = parse_time( degree_str );
275 // printf("Degree = %.4f\n", result);
281 // parse time offset command line option
283 fgOPTIONS::parse_time_offset( const string& time_str) {
286 // printf("time offset = %s\n", time_str);
289 result = (int)rint(parse_time(time_str) * 3600.0);
291 result = (int)(parse_time(time_str) * 3600.0);
294 // printf("parse_time_offset(): %d\n", result);
300 // Parse --tile-diameter=n type option
303 fgOPTIONS::parse_tile_radius( const string& arg ) {
304 int radius = atoi( arg );
306 if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
307 if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
309 // printf("parse_tile_radius(): radius = %d\n", radius);
315 // Parse --fdm=abcdefg type option
317 fgOPTIONS::parse_fdm( const string& fm ) {
318 // printf("fdm = %s\n", fm);
320 if ( fm == "slew" ) {
321 return FGInterface::FG_SLEW;
322 } else if ( fm == "jsb" ) {
323 return FGInterface::FG_JSBSIM;
324 } else if ( (fm == "larcsim") || (fm == "LaRCsim") ) {
325 return FGInterface::FG_LARCSIM;
326 } else if ( fm == "external" ) {
327 return FGInterface::FG_EXTERNAL;
329 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown fdm = " << fm );
333 // we'll never get here, but it makes the compiler happy.
338 // Parse --fov=x.xx type option
340 fgOPTIONS::parse_fov( const string& arg ) {
341 double fov = atof(arg);
343 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
344 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
346 // printf("parse_fov(): result = %.4f\n", fov);
352 // Parse serial port option --serial=/dev/ttyS1,nmea,4800,out
354 // Format is "--serial=device,format,baud,direction" where
356 // device = OS device name to be open()'ed
357 // format = {nmea, fgfs}
358 // baud = {300, 1200, 2400, ..., 230400}
359 // direction = {in, out, bi}
362 fgOPTIONS::parse_serial( const string& serial_str ) {
363 string::size_type pos;
365 // cout << "Serial string = " << serial_str << endl;
367 // a flailing attempt to see if the port config string has a
368 // chance at being valid
369 pos = serial_str.find(",");
370 if ( pos == string::npos ) {
371 FG_LOG( FG_GENERAL, FG_ALERT,
372 "Malformed serial port configure string" );
376 port_options_list.push_back( serial_str );
382 // Parse a single option
383 int fgOPTIONS::parse_option( const string& arg ) {
385 if ( (arg == "--help") || (arg == "-h") ) {
386 // help/usage request
387 return(FG_OPTIONS_HELP);
388 } else if ( arg == "--disable-game-mode") {
390 } else if ( arg == "--enable-game-mode" ) {
392 } else if ( arg == "--disable-splash-screen" ) {
393 splash_screen = false;
394 } else if ( arg == "--enable-splash-screen" ) {
395 splash_screen = true;
396 } else if ( arg == "--disable-intro-music" ) {
398 } else if ( arg == "--enable-intro-music" ) {
400 } else if ( arg == "--disable-mouse-pointer" ) {
402 } else if ( arg == "--enable-mouse-pointer" ) {
404 } else if ( arg == "--disable-pause" ) {
406 } else if ( arg == "--enable-pause" ) {
408 } else if ( arg == "--disable-hud" ) {
410 } else if ( arg == "--enable-hud" ) {
412 } else if ( arg == "--disable-panel" ) {
413 panel_status = false;
414 } else if ( arg == "--enable-panel" ) {
417 } else if ( arg == "--disable-sound" ) {
419 } else if ( arg == "--enable-sound" ) {
421 } else if ( arg.find( "--airport-id=") != string::npos ) {
422 airport_id = arg.substr( 13 );
423 } else if ( arg.find( "--lon=" ) != string::npos ) {
424 lon = parse_degree( arg.substr(6) );
425 } else if ( arg.find( "--lat=" ) != string::npos ) {
426 lat = parse_degree( arg.substr(6) );
427 } else if ( arg.find( "--altitude=" ) != string::npos ) {
428 if ( units == FG_UNITS_FEET ) {
429 altitude = atof( arg.substr(11) ) * FEET_TO_METER;
431 altitude = atof( arg.substr(11) );
433 } else if ( arg.find( "--heading=" ) != string::npos ) {
434 heading = atof( arg.substr(10) );
435 } else if ( arg.find( "--roll=" ) != string::npos ) {
436 roll = atof( arg.substr(7) );
437 } else if ( arg.find( "--pitch=" ) != string::npos ) {
438 pitch = atof( arg.substr(8) );
439 } else if ( arg.find( "--fg-root=" ) != string::npos ) {
440 fg_root = arg.substr( 10 );
441 } else if ( arg.find( "--fdm=" ) != string::npos ) {
442 flight_model = parse_fdm( arg.substr(6) );
443 } else if ( arg == "--fog-disable" ) {
444 fog = FG_FOG_DISABLED;
445 } else if ( arg == "--fog-fastest" ) {
446 fog = FG_FOG_FASTEST;
447 } else if ( arg == "--fog-nicest" ) {
449 } else if ( arg.find( "--fov=" ) != string::npos ) {
450 fov = parse_fov( arg.substr(6) );
451 } else if ( arg == "--disable-fullscreen" ) {
453 } else if ( arg== "--enable-fullscreen") {
455 } else if ( arg == "--shading-flat") {
457 } else if ( arg == "--shading-smooth") {
459 } else if ( arg == "--disable-skyblend") {
461 } else if ( arg== "--enable-skyblend" ) {
463 } else if ( arg == "--disable-textures" ) {
465 } else if ( arg == "--enable-textures" ) {
467 } else if ( arg == "--disable-wireframe" ) {
469 } else if ( arg == "--enable-wireframe" ) {
471 } else if ( arg.find( "--geometry=" ) != string::npos ) {
472 string geometry = arg.substr( 11 );
473 if ( geometry == "640x480" ) {
476 } else if ( geometry == "800x600" ) {
479 } else if ( geometry == "1024x768" ) {
483 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown geometry: " << geometry );
486 } else if ( arg == "--units-feet" ) {
487 units = FG_UNITS_FEET;
488 } else if ( arg == "--units-meters" ) {
489 units = FG_UNITS_METERS;
490 } else if ( arg.find( "--tile-radius=" ) != string::npos ) {
491 tile_radius = parse_tile_radius( arg.substr(14) );
492 tile_diameter = tile_radius * 2 + 1;
493 } else if ( arg.find( "--time-offset=" ) != string::npos ) {
494 time_offset = parse_time_offset( (arg.substr(14)) );
495 } else if ( arg == "--hud-tris" ) {
497 } else if ( arg == "--hud-culled" ) {
499 } else if ( arg.find( "--serial=" ) != string::npos ) {
500 parse_serial( arg.substr(9) );
502 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown option '" << arg << "'" );
503 return FG_OPTIONS_ERROR;
506 return FG_OPTIONS_OK;
510 // Parse the command line options
511 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
515 FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
518 FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
520 result = parse_option(argv[i]);
521 if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
528 return(FG_OPTIONS_OK);
532 // Parse config file options
533 int fgOPTIONS::parse_config_file( const string& path ) {
534 fg_gzifstream in( path );
536 return(FG_OPTIONS_ERROR);
538 FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
546 if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
547 FG_LOG( FG_GENERAL, FG_ALERT,
548 "Config file parse error: " << path << " '"
555 return FG_OPTIONS_OK;
559 // Print usage message
560 void fgOPTIONS::usage ( void ) {
561 printf("Usage: fg [ options ... ]\n");
564 printf("General Options:\n");
565 printf("\t--help -h: print usage\n");
566 printf("\t--fg-root=path: specify the root path for all the data files\n");
567 printf("\t--disable-game-mode: disable full-screen game mode\n");
568 printf("\t--enable-game-mode: enable full-screen game mode\n");
569 printf("\t--disable-splash-screen: disable splash screen\n");
570 printf("\t--enable-splash-screen: enable splash screen\n");
571 printf("\t--disable-intro-music: disable introduction music\n");
572 printf("\t--enable-intro-music: enable introduction music\n");
573 printf("\t--disable-mouse-pointer: disable extra mouse pointer\n");
574 printf("\t--enable-mouse-pointer: enable extra mouse pointer (i.e. for\n");
575 printf("\t\tfull screen voodoo/voodoo-II based cards.)\n");
576 printf("\t--disable-pause: start out in an active state\n");
577 printf("\t--enable-pause: start out in a paused state\n");
580 printf("Features:\n");
581 printf("\t--disable-hud: disable heads up display\n");
582 printf("\t--enable-hud: enable heads up display\n");
583 printf("\t--disable-panel: disable instrument panel\n");
584 printf("\t--enable-panel: enable instrumetn panel\n");
585 printf("\t--disable-sound: disable sound effects\n");
586 printf("\t--enable-sound: enable sound effects\n");
589 printf("Flight Model:\n");
590 printf("\t--fdm=abcd: one of slew, jsb, larcsim, or external\n");
593 printf("Initial Position and Orientation:\n");
594 printf("\t--airport-id=ABCD: specify starting postion by airport id\n");
595 printf("\t--lon=degrees: starting longitude in degrees (west = -)\n");
596 printf("\t--lat=degrees: starting latitude in degrees (south = -)\n");
597 printf("\t--altitude=feet: starting altitude in feet\n");
598 printf("\t\t(unless --units-meters specified\n");
599 printf("\t--heading=degrees: heading (yaw) angle in degress (Psi)\n");
600 printf("\t--roll=degrees: roll angle in degrees (Phi)\n");
601 printf("\t--pitch=degrees: pitch angle in degrees (Theta)\n");
604 printf("Rendering Options:\n");
605 printf("\t--fog-disable: disable fog/haze\n");
606 printf("\t--fog-fastest: enable fastest fog/haze\n");
607 printf("\t--fog-nicest: enable nicest fog/haze\n");
608 printf("\t--fov=xx.x: specify initial field of view angle in degrees\n");
609 printf("\t--disable-fullscreen: disable fullscreen mode\n");
610 printf("\t--enable-fullscreen: enable fullscreen mode\n");
611 printf("\t--shading-flat: enable flat shading\n");
612 printf("\t--shading-smooth: enable smooth shading\n");
613 printf("\t--disable-skyblend: disable sky blending\n");
614 printf("\t--enable-skyblend: enable sky blending\n");
615 printf("\t--disable-textures: disable textures\n");
616 printf("\t--enable-textures: enable textures\n");
617 printf("\t--disable-wireframe: disable wireframe drawing mode\n");
618 printf("\t--enable-wireframe: enable wireframe drawing mode\n");
619 printf("\t--geometry=WWWxHHH: window geometry: 640x480, 800x600, etc.\n");
622 printf("Scenery Options:\n");
623 printf("\t--tile-radius=n: specify tile radius, must be 1 - 4\n");
626 printf("Hud Options:\n");
627 printf("\t--units-feet: Hud displays units in feet\n");
628 printf("\t--units-meters: Hud displays units in meters\n");
629 printf("\t--hud-tris: Hud displays number of triangles rendered\n");
630 printf("\t--hud-culled: Hud displays percentage of triangles culled\n");
633 printf("Time Options:\n");
634 printf("\t--time-offset=[+-]hh:mm:ss: offset local time by this amount\n");
639 fgOPTIONS::~fgOPTIONS( void ) {