2 // options.cxx -- class to handle command line options
4 // Written by Curtis Olson, started April 1998.
6 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 // (Log is kept at end of this file)
30 #include <math.h> // rint()
32 #include <stdlib.h> // atof(), atoi()
36 #include <Debug/logstream.hxx>
37 #include <Flight/flight.hxx>
38 #include <Include/fg_constants.h>
39 #include <Main/options.hxx>
40 #include <Misc/fgstream.hxx>
42 #include "fg_serial.hxx"
46 atof( const string& str )
48 return ::atof( str.c_str() );
52 atoi( const string& str )
54 return ::atoi( str.c_str() );
57 // Defined the shared options class here
58 fgOPTIONS current_options;
62 fgOPTIONS::fgOPTIONS() :
63 // starting longitude in degrees (west = -)
64 // starting latitude in degrees (south = -)
66 // Default initial position is Globe, AZ (P13)
70 // North of the city of Globe
74 // North of the city of Globe
78 // Near where I used to live in Globe, AZ
82 // 10125 Jewell St. NE
86 // Near KHSP (Hot Springs, VA)
87 // lon(-79.8338964 + 0.01),
88 // lat( 37.9514564 + 0.008),
90 // (SEZ) SEDONA airport
91 // lon(-111.7884614 + 0.01),
92 // lat( 34.8486289 - 0.015),
94 // Jim Brennon's Kingmont Observatory
98 // Huaras, Peru (S09d 31.871' W077d 31.498')
102 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
107 // lon(-90.1953055556),
108 // lat( 45.4511388889),
110 // starting altitude in meters (this will be reset to ground level
111 // if it is lower than the terrain
114 // Initial Orientation
115 heading(270.0), // heading (yaw) angle in degress (Psi)
116 roll(0.0), // roll angle in degrees (Phi)
117 pitch(0.424), // pitch angle in degrees (Theta)
131 // Flight Model options
132 flight_model(FGState::FG_LARCSIM),
135 fog(FG_FOG_NICEST), // nicest
149 units(FG_UNITS_FEET),
156 // set initial values/defaults
157 char* envp = ::getenv( "FG_ROOT" );
159 if ( envp != NULL ) {
160 // fg_root could be anywhere, so default to environmental
161 // variable $FG_ROOT if it is set.
164 // Otherwise, default to a random compiled in location if
165 // $FG_ROOT is not set. This can still be overridden from the
166 // command line or a config file.
169 fg_root = "\\FlightGear";
171 fg_root = "/usr/local/lib/FlightGear";
175 airport_id = ""; // default airport id
177 // initialize port config string list
178 port_options_list.erase ( port_options_list.begin(),
179 port_options_list.end() );
184 fgOPTIONS::parse_time(const string& time_in) {
185 char *time_str, num[256];
186 double hours, minutes, seconds;
191 time_str = (char *)time_in.c_str();
193 // printf("parse_time(): %s\n", time_str);
196 if ( strlen(time_str) ) {
197 if ( time_str[0] == '+' ) {
200 } else if ( time_str[0] == '-' ) {
205 // printf("sign = %d\n", sign);
208 if ( strlen(time_str) ) {
210 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
211 num[i] = time_str[0];
215 if ( time_str[0] == ':' ) {
220 // printf("hours = %.2lf\n", hours);
226 if ( strlen(time_str) ) {
228 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
229 num[i] = time_str[0];
233 if ( time_str[0] == ':' ) {
238 // printf("minutes = %.2lf\n", minutes);
240 result += minutes / 60.0;
244 if ( strlen(time_str) ) {
246 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
247 num[i] = time_str[0];
253 // printf("seconds = %.2lf\n", seconds);
255 result += seconds / 3600.0;
258 return(sign * result);
262 // parse degree in the form of [+/-]hhh:mm:ss
264 fgOPTIONS::parse_degree( const string& degree_str) {
265 double result = parse_time( degree_str );
267 // printf("Degree = %.4f\n", result);
273 // parse time offset command line option
275 fgOPTIONS::parse_time_offset( const string& time_str) {
278 // printf("time offset = %s\n", time_str);
281 result = (int)rint(parse_time(time_str) * 3600.0);
283 result = (int)(parse_time(time_str) * 3600.0);
286 // printf("parse_time_offset(): %d\n", result);
292 // Parse --tile-diameter=n type option
295 fgOPTIONS::parse_tile_radius( const string& arg ) {
296 int radius = atoi( arg );
298 if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
299 if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
301 // printf("parse_tile_radius(): radius = %d\n", radius);
307 // Parse --flightmode=abcdefg type option
309 fgOPTIONS::parse_flight_model( const string& fm ) {
310 // printf("flight model = %s\n", fm);
312 if ( fm == "slew" ) {
313 return FGState::FG_SLEW;
314 } else if ( (fm == "larcsim") || (fm == "LaRCsim") ) {
315 return FGState::FG_LARCSIM;
316 } else if ( fm == "external" ) {
317 return FGState::FG_EXTERNAL;
319 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown flight model = " << fm );
323 // we'll never get here, but it makes the compiler happy.
328 // Parse --fov=x.xx type option
330 fgOPTIONS::parse_fov( const string& arg ) {
331 double fov = atof(arg);
333 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
334 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
336 // printf("parse_fov(): result = %.4f\n", fov);
342 // Parse serial port option --serial=/dev/ttyS1,nmea,4800,out
344 // Format is "--serial=device,format,baud,direction" where
346 // device = OS device name to be open()'ed
347 // format = {nmea, fgfs}
348 // baud = {300, 1200, 2400, ..., 230400}
349 // direction = {in, out, bi}
352 fgOPTIONS::parse_serial( const string& serial_str ) {
353 string::size_type pos;
355 // cout << "Serial string = " << serial_str << endl;
357 // a flailing attempt to see if the port config string has a
358 // chance at being valid
359 pos = serial_str.find(",");
360 if ( pos == string::npos ) {
361 FG_LOG( FG_GENERAL, FG_ALERT,
362 "Malformed serial port configure string" );
366 port_options_list.push_back( serial_str );
372 // Parse a single option
373 int fgOPTIONS::parse_option( const string& arg ) {
375 if ( (arg == "--help") || (arg == "-h") ) {
376 // help/usage request
377 return(FG_OPTIONS_HELP);
378 } else if ( arg == "--disable-game-mode") {
380 } else if ( arg == "--enable-game-mode" ) {
382 } else if ( arg == "--disable-splash-screen" ) {
383 splash_screen = false;
384 } else if ( arg == "--enable-splash-screen" ) {
385 splash_screen = true;
386 } else if ( arg == "--disable-intro-music" ) {
388 } else if ( arg == "--enable-intro-music" ) {
390 } else if ( arg == "--disable-mouse-pointer" ) {
392 } else if ( arg == "--enable-mouse-pointer" ) {
394 } else if ( arg == "--disable-pause" ) {
396 } else if ( arg == "--enable-pause" ) {
398 } else if ( arg == "--disable-hud" ) {
400 } else if ( arg == "--enable-hud" ) {
402 } else if ( arg == "--disable-panel" ) {
403 panel_status = false;
404 } else if ( arg == "--enable-panel" ) {
406 } else if ( arg == "--disable-sound" ) {
408 } else if ( arg == "--enable-sound" ) {
410 } else if ( arg.find( "--airport-id=") != string::npos ) {
411 airport_id = arg.substr( 13 );
412 } else if ( arg.find( "--lon=" ) != string::npos ) {
413 lon = parse_degree( arg.substr(6) );
414 } else if ( arg.find( "--lat=" ) != string::npos ) {
415 lat = parse_degree( arg.substr(6) );
416 } else if ( arg.find( "--altitude=" ) != string::npos ) {
417 if ( units == FG_UNITS_FEET ) {
418 altitude = atof( arg.substr(11) ) * FEET_TO_METER;
420 altitude = atof( arg.substr(11) );
422 } else if ( arg.find( "--heading=" ) != string::npos ) {
423 heading = atof( arg.substr(10) );
424 } else if ( arg.find( "--roll=" ) != string::npos ) {
425 roll = atof( arg.substr(7) );
426 } else if ( arg.find( "--pitch=" ) != string::npos ) {
427 pitch = atof( arg.substr(8) );
428 } else if ( arg.find( "--fg-root=" ) != string::npos ) {
429 fg_root = arg.substr( 10 );
430 } else if ( arg.find( "--flight-model=" ) != string::npos ) {
431 flight_model = parse_flight_model( arg.substr(15) );
432 } else if ( arg == "--fog-disable" ) {
433 fog = FG_FOG_DISABLED;
434 } else if ( arg == "--fog-fastest" ) {
435 fog = FG_FOG_FASTEST;
436 } else if ( arg == "--fog-nicest" ) {
438 } else if ( arg.find( "--fov=" ) != string::npos ) {
439 fov = parse_fov( arg.substr(6) );
440 } else if ( arg == "--disable-fullscreen" ) {
442 } else if ( arg== "--enable-fullscreen") {
444 } else if ( arg == "--shading-flat") {
446 } else if ( arg == "--shading-smooth") {
448 } else if ( arg == "--disable-skyblend") {
450 } else if ( arg== "--enable-skyblend" ) {
452 } else if ( arg == "--disable-textures" ) {
454 } else if ( arg == "--enable-textures" ) {
456 } else if ( arg == "--disable-wireframe" ) {
458 } else if ( arg == "--enable-wireframe" ) {
460 } else if ( arg.find( "--geometry=" ) != string::npos ) {
461 string geometry = arg.substr( 11 );
462 if ( geometry == "640x480" ) {
465 } else if ( geometry == "800x600" ) {
468 } else if ( geometry == "1024x768" ) {
472 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown geometry: " << geometry );
475 } else if ( arg == "--units-feet" ) {
476 units = FG_UNITS_FEET;
477 } else if ( arg == "--units-meters" ) {
478 units = FG_UNITS_METERS;
479 } else if ( arg.find( "--tile-radius=" ) != string::npos ) {
480 tile_radius = parse_tile_radius( arg.substr(14) );
481 tile_diameter = tile_radius * 2 + 1;
482 } else if ( arg.find( "--time-offset=" ) != string::npos ) {
483 time_offset = parse_time_offset( (arg.substr(14)) );
484 } else if ( arg == "--hud-tris" ) {
486 } else if ( arg == "--hud-culled" ) {
488 } else if ( arg.find( "--serial=" ) != string::npos ) {
489 parse_serial( arg.substr(9) );
491 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown option '" << arg << "'" );
492 return FG_OPTIONS_ERROR;
495 return FG_OPTIONS_OK;
499 // Parse the command line options
500 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
504 FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
507 FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
509 result = parse_option(argv[i]);
510 if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
517 return(FG_OPTIONS_OK);
521 // Parse config file options
522 int fgOPTIONS::parse_config_file( const string& path ) {
523 fg_gzifstream in( path );
525 return(FG_OPTIONS_ERROR);
527 FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
535 if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
536 FG_LOG( FG_GENERAL, FG_ALERT,
537 "Config file parse error: " << path << " '"
544 return FG_OPTIONS_OK;
548 // Print usage message
549 void fgOPTIONS::usage ( void ) {
550 printf("Usage: fg [ options ... ]\n");
553 printf("General Options:\n");
554 printf("\t--help -h: print usage\n");
555 printf("\t--fg-root=path: specify the root path for all the data files\n");
556 printf("\t--disable-game-mode: disable full-screen game mode\n");
557 printf("\t--enable-game-mode: enable full-screen game mode\n");
558 printf("\t--disable-splash-screen: disable splash screen\n");
559 printf("\t--enable-splash-screen: enable splash screen\n");
560 printf("\t--disable-intro-music: disable introduction music\n");
561 printf("\t--enable-intro-music: enable introduction music\n");
562 printf("\t--disable-mouse-pointer: disable extra mouse pointer\n");
563 printf("\t--enable-mouse-pointer: enable extra mouse pointer (i.e. for\n");
564 printf("\t\tfull screen voodoo/voodoo-II based cards.)\n");
565 printf("\t--disable-pause: start out in an active state\n");
566 printf("\t--enable-pause: start out in a paused state\n");
569 printf("Features:\n");
570 printf("\t--disable-hud: disable heads up display\n");
571 printf("\t--enable-hud: enable heads up display\n");
572 printf("\t--disable-panel: disable instrument panel\n");
573 printf("\t--enable-panel: enable instrumetn panel\n");
574 printf("\t--disable-sound: disable sound effects\n");
575 printf("\t--enable-sound: enable sound effects\n");
578 printf("Flight Model:\n");
579 printf("\t--flight-mode=abcd: one of slew, larcsim, or external\n");
582 printf("Initial Position and Orientation:\n");
583 printf("\t--airport-id=ABCD: specify starting postion by airport id\n");
584 printf("\t--lon=degrees: starting longitude in degrees (west = -)\n");
585 printf("\t--lat=degrees: starting latitude in degrees (south = -)\n");
586 printf("\t--altitude=feet: starting altitude in feet\n");
587 printf("\t\t(unless --units-meters specified\n");
588 printf("\t--heading=degrees: heading (yaw) angle in degress (Psi)\n");
589 printf("\t--roll=degrees: roll angle in degrees (Phi)\n");
590 printf("\t--pitch=degrees: pitch angle in degrees (Theta)\n");
593 printf("Rendering Options:\n");
594 printf("\t--fog-disable: disable fog/haze\n");
595 printf("\t--fog-fastest: enable fastest fog/haze\n");
596 printf("\t--fog-nicest: enable nicest fog/haze\n");
597 printf("\t--fov=xx.x: specify initial field of view angle in degrees\n");
598 printf("\t--disable-fullscreen: disable fullscreen mode\n");
599 printf("\t--enable-fullscreen: enable fullscreen mode\n");
600 printf("\t--shading-flat: enable flat shading\n");
601 printf("\t--shading-smooth: enable smooth shading\n");
602 printf("\t--disable-skyblend: disable sky blending\n");
603 printf("\t--enable-skyblend: enable sky blending\n");
604 printf("\t--disable-textures: disable textures\n");
605 printf("\t--enable-textures: enable textures\n");
606 printf("\t--disable-wireframe: disable wireframe drawing mode\n");
607 printf("\t--enable-wireframe: enable wireframe drawing mode\n");
608 printf("\t--geometry=WWWxHHH: window geometry: 640x480, 800x600, etc.\n");
611 printf("Scenery Options:\n");
612 printf("\t--tile-radius=n: specify tile radius, must be 1 - 4\n");
615 printf("Hud Options:\n");
616 printf("\t--units-feet: Hud displays units in feet\n");
617 printf("\t--units-meters: Hud displays units in meters\n");
618 printf("\t--hud-tris: Hud displays number of triangles rendered\n");
619 printf("\t--hud-culled: Hud displays percentage of triangles culled\n");
621 printf("Time Options:\n");
622 printf("\t--time-offset=[+-]hh:mm:ss: offset local time by this amount\n");
627 fgOPTIONS::~fgOPTIONS( void ) {
632 // Revision 1.35 1998/12/06 14:52:57 curt
633 // Fixed a problem with the initial starting altitude. "v->abs_view_pos" wasn't
634 // being calculated correctly at the beginning causing the first terrain
635 // intersection to fail, returning a ground altitude of zero, causing the plane
636 // to free fall for one frame, until the ground altitude was corrected, but now
637 // being under the ground we got a big bounce and the plane always ended up
640 // Revision 1.34 1998/12/05 15:54:22 curt
641 // Renamed class fgFLIGHT to class FGState as per request by JSB.
643 // Revision 1.33 1998/12/04 01:30:44 curt
644 // Added support for the External flight model.
646 // Revision 1.32 1998/11/25 01:34:00 curt
647 // Support for an arbitrary number of serial ports.
649 // Revision 1.31 1998/11/23 21:49:04 curt
650 // Borland portability tweaks.
652 // Revision 1.30 1998/11/16 14:00:02 curt
653 // Added pow() macro bug work around.
654 // Added support for starting FGFS at various resolutions.
655 // Added some initial serial port support.
656 // Specify default log levels in main().
658 // Revision 1.29 1998/11/06 21:18:12 curt
659 // Converted to new logstream debugging facility. This allows release
660 // builds with no messages at all (and no performance impact) by using
661 // the -DFG_NDEBUG flag.
663 // Revision 1.28 1998/11/06 14:47:03 curt
664 // Changes to track Bernie's updates to fgstream.
666 // Revision 1.27 1998/11/02 23:04:04 curt
667 // HUD units now display in feet by default with meters being a command line
670 // Revision 1.26 1998/10/17 01:34:24 curt
673 // Revision 1.25 1998/09/15 02:09:27 curt
674 // Include/fg_callback.hxx
675 // Moved code inline to stop g++ 2.7 from complaining.
677 // Simulator/Time/event.[ch]xx
678 // Changed return type of fgEVENT::printStat(). void caused g++ 2.7 to
679 // complain bitterly.
681 // Minor bugfix and changes.
683 // Simulator/Main/GLUTmain.cxx
684 // Added missing type to idle_state definition - eliminates a warning.
686 // Simulator/Main/fg_init.cxx
687 // Changes to airport lookup.
689 // Simulator/Main/options.cxx
690 // Uses fg_gzifstream when loading config file.
692 // Revision 1.24 1998/09/08 15:04:33 curt
693 // Optimizations by Norman Vine.
695 // Revision 1.23 1998/08/27 17:02:07 curt
696 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
697 // - use strings for fg_root and airport_id and added methods to return
699 // - inlined all access methods,
700 // - made the parsing functions private methods,
701 // - deleted some unused functions.
702 // - propogated some of these changes out a bit further.
704 // Revision 1.22 1998/08/24 20:11:13 curt
705 // Added i/I to toggle full vs. minimal HUD.
706 // Added a --hud-tris vs --hud-culled option.
707 // Moved options accessor funtions to options.hxx.
709 // Revision 1.21 1998/08/20 15:10:34 curt
710 // Added GameGLUT support.
712 // Revision 1.20 1998/07/30 23:48:28 curt
713 // Output position & orientation when pausing.
714 // Eliminated libtool use.
715 // Added options to specify initial position and orientation.
716 // Changed default fov to 55 degrees.
717 // Added command line option to start in paused or unpaused state.
719 // Revision 1.19 1998/07/27 18:41:25 curt
720 // Added a pause command "p"
721 // Fixed some initialization order problems between pui and glut.
722 // Added an --enable/disable-sound option.
724 // Revision 1.18 1998/07/22 01:27:03 curt
725 // Strip out \r when parsing config file in case we are on a windoze system.
727 // Revision 1.17 1998/07/16 17:33:38 curt
728 // "H" / "h" now control hud brightness as well with off being one of the
730 // Better checking for xmesa/fx 3dfx fullscreen/window support for deciding
731 // whether or not to build in the feature.
732 // Translucent menu support.
733 // HAVE_AUDIO_SUPPORT -> ENABLE_AUDIO_SUPPORT
734 // Use fork() / wait() for playing mp3 init music in background under unix.
735 // Changed default tile diameter to 5.
737 // Revision 1.16 1998/07/13 21:01:39 curt
738 // Wrote access functions for current fgOPTIONS.
740 // Revision 1.15 1998/07/06 21:34:19 curt
741 // Added an enable/disable splash screen option.
742 // Added an enable/disable intro music option.
743 // Added an enable/disable instrument panel option.
744 // Added an enable/disable mouse pointer option.
745 // Added using namespace std for compilers that support this.
747 // Revision 1.14 1998/07/04 00:52:26 curt
748 // Add my own version of gluLookAt() (which is nearly identical to the
749 // Mesa/glu version.) But, by calculating the Model View matrix our selves
750 // we can save this matrix without having to read it back in from the video
751 // card. This hopefully allows us to save a few cpu cycles when rendering
752 // out the fragments because we can just use glLoadMatrixd() with the
753 // precalculated matrix for each tile rather than doing a push(), translate(),
754 // pop() for every fragment.
756 // Panel status defaults to off for now until it gets a bit more developed.
758 // Extract OpenGL driver info on initialization.
760 // Revision 1.13 1998/06/27 16:54:34 curt
761 // Replaced "extern displayInstruments" with a entry in fgOPTIONS.
762 // Don't change the view port when displaying the panel.
764 // Revision 1.12 1998/06/17 21:35:13 curt
765 // Refined conditional audio support compilation.
766 // Moved texture parameter setup calls to ../Scenery/materials.cxx
767 // #include <string.h> before various STL includes.
768 // Make HUD default state be enabled.
770 // Revision 1.11 1998/06/13 00:40:33 curt
771 // Tweaked fog command line options.
773 // Revision 1.10 1998/05/16 13:08:36 curt
774 // C++ - ified views.[ch]xx
775 // Shuffled some additional view parameters into the fgVIEW class.
776 // Changed tile-radius to tile-diameter because it is a much better
778 // Added a WORLD_TO_EYE transformation to views.cxx. This allows us
779 // to transform world space to eye space for view frustum culling.
781 // Revision 1.9 1998/05/13 18:29:59 curt
782 // Added a keyboard binding to dynamically adjust field of view.
783 // Added a command line option to specify fov.
784 // Adjusted terrain color.
785 // Root path info moved to fgOPTIONS.
786 // Added ability to parse options out of a config file.
788 // Revision 1.8 1998/05/07 23:14:16 curt
789 // Added "D" key binding to set autopilot heading.
790 // Made frame rate calculation average out over last 10 frames.
791 // Borland C++ floating point exception workaround.
792 // Added a --tile-radius=n option.
794 // Revision 1.7 1998/05/06 03:16:25 curt
795 // Added an averaged global frame rate counter.
796 // Added an option to control tile radius.
798 // Revision 1.6 1998/05/03 00:47:32 curt
799 // Added an option to enable/disable full-screen mode.
801 // Revision 1.5 1998/04/30 12:34:19 curt
802 // Added command line rendering options:
803 // enable/disable fog/haze
804 // specify smooth/flat shading
805 // disable sky blending and just use a solid color
806 // enable wireframe drawing mode
808 // Revision 1.4 1998/04/28 01:20:22 curt
809 // Type-ified fgTIME and fgVIEW.
810 // Added a command line option to disable textures.
812 // Revision 1.3 1998/04/26 05:01:19 curt
813 // Added an rint() / HAVE_RINT check.
815 // Revision 1.2 1998/04/25 15:11:12 curt
816 // Added an command line option to set starting position based on airport ID.
818 // Revision 1.1 1998/04/24 00:49:21 curt
819 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
820 // Trying out some different option parsing code.
821 // Some code reorganization.