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 <Include/fg_constants.h>
42 #include <Include/general.hxx>
43 #include <Cockpit/cockpit.hxx>
44 #include <Debug/logstream.hxx>
45 #include <FDM/flight.hxx>
46 #include <Misc/fgstream.hxx>
48 # include <Network/network.h>
50 #include <Time/fg_time.hxx>
52 #include "options.hxx"
53 #include "fg_serial.hxx"
56 FG_USING_NAMESPACE(std);
59 extern void fgReshape( int width, int height );
62 atof( const string& str )
66 // -dw- if ::atof is called, then we get an infinite loop
67 return std::atof( str.c_str() );
69 return ::atof( str.c_str() );
74 atoi( const string& str )
77 // -dw- if ::atoi is called, then we get an infinite loop
78 return std::atoi( str.c_str() );
80 return ::atoi( str.c_str() );
84 // Defined the shared options class here
85 fgOPTIONS current_options;
89 fgOPTIONS::fgOPTIONS() :
90 // starting longitude in degrees (west = -)
91 // starting latitude in degrees (south = -)
93 // Default initial position is Globe, AZ (P13)
97 // North of the city of Globe
101 // North of the city of Globe
105 // Near where I used to live in Globe, AZ
109 // 10125 Jewell St. NE
113 // Near KHSP (Hot Springs, VA)
114 // lon(-79.8338964 + 0.01),
115 // lat( 37.9514564 + 0.008),
117 // (SEZ) SEDONA airport
118 // lon(-111.7884614 + 0.01),
119 // lat( 34.8486289 - 0.015),
121 // Jim Brennon's Kingmont Observatory
122 // lon(-121.1131667),
125 // Huaras, Peru (S09d 31.871' W077d 31.498')
129 // Eclipse Watching w73.5 n10 (approx) 18:00 UT
134 // lon(-90.1953055556),
135 // lat( 45.4511388889),
137 // starting altitude in meters (this will be reset to ground level
138 // if it is lower than the terrain
141 // Initial Orientation
142 heading(270.0), // heading (yaw) angle in degress (Psi)
143 roll(0.0), // roll angle in degrees (Phi)
144 pitch(0.424), // pitch angle in degrees (Theta)
146 // Initialize current options velocities to 0.0
147 uBody(0.0), vBody(0.0), wBody(0.0),
155 control_mode(FG_JOYSTICK),
162 // Flight Model options
163 flight_model( FGInterface::FG_LARCSIM ),
167 fog(FG_FOG_NICEST), // nicest
181 units(FG_UNITS_FEET),
190 // set initial values/defaults
191 char* envp = ::getenv( "FG_ROOT" );
193 if ( envp != NULL ) {
194 // fg_root could be anywhere, so default to environmental
195 // variable $FG_ROOT if it is set.
198 // Otherwise, default to a random compiled in location if
199 // $FG_ROOT is not set. This can still be overridden from the
200 // command line or a config file.
203 fg_root = "\\FlightGear";
204 #elif defined( MACOS )
211 airport_id = ""; // default airport id
212 net_id = "Johnney"; // default pilot's name
214 // initialize port config string list
215 port_options_list.erase ( port_options_list.begin(),
216 port_options_list.end() );
220 fgOPTIONS::toggle_panel() {
222 FGTime *t = FGTime::cur_time_params;
224 int toggle_pause = t->getPause();
227 t->togglePauseMode();
230 panel_status = false;
234 if ( panel_status ) {
235 if( FGPanel::OurPanel == 0)
239 fov *= (1.0 / 0.4232);
241 fgReshape( xsize, ysize);
244 t->togglePauseMode();
248 fgOPTIONS::parse_time(const string& time_in) {
249 char *time_str, num[256];
250 double hours, minutes, seconds;
255 time_str = (char *)time_in.c_str();
257 // printf("parse_time(): %s\n", time_str);
260 if ( strlen(time_str) ) {
261 if ( time_str[0] == '+' ) {
264 } else if ( time_str[0] == '-' ) {
269 // printf("sign = %d\n", sign);
272 if ( strlen(time_str) ) {
274 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
275 num[i] = time_str[0];
279 if ( time_str[0] == ':' ) {
284 // printf("hours = %.2lf\n", hours);
290 if ( strlen(time_str) ) {
292 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
293 num[i] = time_str[0];
297 if ( time_str[0] == ':' ) {
302 // printf("minutes = %.2lf\n", minutes);
304 result += minutes / 60.0;
308 if ( strlen(time_str) ) {
310 while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
311 num[i] = time_str[0];
317 // printf("seconds = %.2lf\n", seconds);
319 result += seconds / 3600.0;
322 return(sign * result);
326 long int fgOPTIONS::parse_date( const string& date)
329 char * date_str, num[256];
331 // initialize to zero
338 gmt.tm_isdst = 0; // ignore daylight savingtime for the moment
339 date_str = (char *)date.c_str();
341 if ( strlen(date_str) ) {
343 while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
344 num[i] = date_str[0];
348 if ( date_str[0] == ':' ) {
352 gmt.tm_year = atoi(num) - 1900;
355 if ( strlen(date_str) ) {
357 while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
358 num[i] = date_str[0];
362 if ( date_str[0] == ':' ) {
366 gmt.tm_mon = atoi(num) -1;
369 if ( strlen(date_str) ) {
371 while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
372 num[i] = date_str[0];
376 if ( date_str[0] == ':' ) {
380 gmt.tm_mday = atoi(num);
383 if ( strlen(date_str) ) {
385 while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
386 num[i] = date_str[0];
390 if ( date_str[0] == ':' ) {
394 gmt.tm_hour = atoi(num);
397 if ( strlen(date_str) ) {
399 while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
400 num[i] = date_str[0];
404 if ( date_str[0] == ':' ) {
408 gmt.tm_min = atoi(num);
411 if ( strlen(date_str) ) {
413 while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
414 num[i] = date_str[0];
418 if ( date_str[0] == ':' ) {
422 gmt.tm_sec = atoi(num);
424 time_t theTime = FGTime::cur_time_params->get_gmt(gmt.tm_year,
430 //printf ("Date is %s\n", ctime(&theTime));
431 //printf ("in seconds that is %d\n", theTime);
437 // parse degree in the form of [+/-]hhh:mm:ss
438 void fgOPTIONS::parse_control( const string& mode ) {
439 if ( mode == "joystick" ) {
440 control_mode = FG_JOYSTICK;
441 } else if ( mode == "mouse" ) {
442 control_mode = FG_MOUSE;
444 control_mode = FG_KEYBOARD;
449 /// parse degree in the form of [+/-]hhh:mm:ss
451 fgOPTIONS::parse_degree( const string& degree_str) {
452 double result = parse_time( degree_str );
454 // printf("Degree = %.4f\n", result);
460 // parse time offset command line option
462 fgOPTIONS::parse_time_offset( const string& time_str) {
465 // printf("time offset = %s\n", time_str);
468 result = (int)rint(parse_time(time_str) * 3600.0);
470 result = (int)(parse_time(time_str) * 3600.0);
473 // printf("parse_time_offset(): %d\n", result);
479 // Parse --tile-diameter=n type option
482 fgOPTIONS::parse_tile_radius( const string& arg ) {
483 int radius = atoi( arg );
485 if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
486 if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
488 // printf("parse_tile_radius(): radius = %d\n", radius);
494 // Parse --fdm=abcdefg type option
496 fgOPTIONS::parse_fdm( const string& fm ) {
497 // printf("fdm = %s\n", fm);
499 if ( fm == "slew" ) {
500 return FGInterface::FG_SLEW;
501 } else if ( fm == "jsb" ) {
502 return FGInterface::FG_JSBSIM;
503 } else if ( (fm == "larcsim") || (fm == "LaRCsim") ) {
504 return FGInterface::FG_LARCSIM;
505 } else if ( fm == "external" ) {
506 return FGInterface::FG_EXTERNAL;
508 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown fdm = " << fm );
512 // we'll never get here, but it makes the compiler happy.
517 // Parse --fov=x.xx type option
519 fgOPTIONS::parse_fov( const string& arg ) {
520 double fov = atof(arg);
522 if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
523 if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
525 // printf("parse_fov(): result = %.4f\n", fov);
531 // Parse serial port option --serial=/dev/ttyS1,nmea,4800,out
533 // Format is "--serial=device,format,baud,direction" where
535 // device = OS device name to be open()'ed
536 // format = {nmea, garmin,fgfs,rul}
537 // baud = {300, 1200, 2400, ..., 230400}
538 // direction = {in, out, bi}
541 fgOPTIONS::parse_serial( const string& serial_str ) {
542 string::size_type pos;
544 // cout << "Serial string = " << serial_str << endl;
546 // a flailing attempt to see if the port config string has a
547 // chance at being valid
548 pos = serial_str.find(",");
549 if ( pos == string::npos ) {
550 FG_LOG( FG_GENERAL, FG_ALERT,
551 "Malformed serial port configure string" );
555 port_options_list.push_back( serial_str );
561 // Parse a single option
562 int fgOPTIONS::parse_option( const string& arg ) {
564 if ( (arg == "--help") || (arg == "-h") ) {
565 // help/usage request
566 return(FG_OPTIONS_HELP);
567 } else if ( arg == "--disable-game-mode") {
569 } else if ( arg == "--enable-game-mode" ) {
571 } else if ( arg == "--disable-splash-screen" ) {
572 splash_screen = false;
573 } else if ( arg == "--enable-splash-screen" ) {
574 splash_screen = true;
575 } else if ( arg == "--disable-intro-music" ) {
577 } else if ( arg == "--enable-intro-music" ) {
579 } else if ( arg == "--disable-mouse-pointer" ) {
581 } else if ( arg == "--enable-mouse-pointer" ) {
583 } else if ( arg == "--disable-pause" ) {
585 } else if ( arg == "--enable-pause" ) {
587 } else if ( arg.find( "--control=") != string::npos ) {
588 parse_control( arg.substr(10) );
589 } else if ( arg == "--disable-hud" ) {
591 } else if ( arg == "--enable-hud" ) {
593 } else if ( arg == "--disable-panel" ) {
594 panel_status = false;
595 } else if ( arg == "--enable-panel" ) {
598 } else if ( arg == "--disable-sound" ) {
600 } else if ( arg == "--enable-sound" ) {
602 } else if ( arg.find( "--airport-id=") != string::npos ) {
603 airport_id = arg.substr( 13 );
604 } else if ( arg.find( "--lon=" ) != string::npos ) {
605 lon = parse_degree( arg.substr(6) );
606 } else if ( arg.find( "--lat=" ) != string::npos ) {
607 lat = parse_degree( arg.substr(6) );
608 } else if ( arg.find( "--altitude=" ) != string::npos ) {
609 if ( units == FG_UNITS_FEET ) {
610 altitude = atof( arg.substr(11) ) * FEET_TO_METER;
612 altitude = atof( arg.substr(11) );
614 } else if ( arg.find( "--uBody=" ) != string::npos ) {
615 if ( units == FG_UNITS_FEET ) {
616 uBody = atof( arg.substr(8) ) * FEET_TO_METER;
618 uBody = atof( arg.substr(8) );
620 } else if ( arg.find( "--vBody=" ) != string::npos ) {
621 if ( units == FG_UNITS_FEET ) {
622 vBody = atof( arg.substr(8) ) * FEET_TO_METER;
624 vBody = atof( arg.substr(8) );
626 } else if ( arg.find( "--wBody=" ) != string::npos ) {
627 if ( units == FG_UNITS_FEET ) {
628 wBody = atof( arg.substr(8) ) * FEET_TO_METER;
630 wBody = atof( arg.substr(8) );
632 } else if ( arg.find( "--heading=" ) != string::npos ) {
633 heading = atof( arg.substr(10) );
634 } else if ( arg.find( "--roll=" ) != string::npos ) {
635 roll = atof( arg.substr(7) );
636 } else if ( arg.find( "--pitch=" ) != string::npos ) {
637 pitch = atof( arg.substr(8) );
638 } else if ( arg.find( "--fg-root=" ) != string::npos ) {
639 fg_root = arg.substr( 10 );
640 } else if ( arg.find( "--fdm=" ) != string::npos ) {
641 flight_model = parse_fdm( arg.substr(6) );
642 } else if ( arg.find( "--speed=" ) != string::npos ) {
643 speed_up = atoi( arg.substr(8) );
644 } else if ( arg == "--fog-disable" ) {
645 fog = FG_FOG_DISABLED;
646 } else if ( arg == "--fog-fastest" ) {
647 fog = FG_FOG_FASTEST;
648 } else if ( arg == "--fog-nicest" ) {
650 } else if ( arg.find( "--fov=" ) != string::npos ) {
651 fov = parse_fov( arg.substr(6) );
652 } else if ( arg == "--disable-fullscreen" ) {
654 } else if ( arg== "--enable-fullscreen") {
656 } else if ( arg == "--shading-flat") {
658 } else if ( arg == "--shading-smooth") {
660 } else if ( arg == "--disable-skyblend") {
662 } else if ( arg== "--enable-skyblend" ) {
664 } else if ( arg == "--disable-textures" ) {
666 } else if ( arg == "--enable-textures" ) {
668 } else if ( arg == "--disable-wireframe" ) {
670 } else if ( arg == "--enable-wireframe" ) {
672 } else if ( arg.find( "--geometry=" ) != string::npos ) {
673 string geometry = arg.substr( 11 );
674 if ( geometry == "640x480" ) {
677 } else if ( geometry == "800x600" ) {
680 } else if ( geometry == "1024x768" ) {
684 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown geometry: " << geometry );
687 } else if ( arg == "--units-feet" ) {
688 units = FG_UNITS_FEET;
689 } else if ( arg == "--units-meters" ) {
690 units = FG_UNITS_METERS;
691 } else if ( arg.find( "--tile-radius=" ) != string::npos ) {
692 tile_radius = parse_tile_radius( arg.substr(14) );
693 tile_diameter = tile_radius * 2 + 1;
694 } else if ( arg.find( "--time-offset=" ) != string::npos ) {
695 time_offset = parse_time_offset( (arg.substr(14)) );
696 } else if (arg.find( "--start-date-gmt=") != string::npos ) {
697 start_gst = parse_date( (arg.substr(17)) );
698 } else if (arg.find( "--start-data-lst=") != string::npos ) {
699 start_lst = parse_date( (arg.substr(17)) );
700 } else if ( arg == "--hud-tris" ) {
702 } else if ( arg == "--hud-culled" ) {
704 } else if ( arg.find( "--serial=" ) != string::npos ) {
705 parse_serial( arg.substr(9) );
706 #ifdef FG_NETWORK_OLK
707 } else if ( arg == "--net-hud" ) {
709 } else if ( arg.find( "--net-id=") != string::npos ) {
710 net_id = arg.substr( 9 );
713 FG_LOG( FG_GENERAL, FG_ALERT, "Unknown option '" << arg << "'" );
714 return FG_OPTIONS_ERROR;
717 return FG_OPTIONS_OK;
721 // Parse the command line options
722 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
726 FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
729 FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
731 result = parse_option(argv[i]);
732 if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
739 return(FG_OPTIONS_OK);
743 // Parse config file options
744 int fgOPTIONS::parse_config_file( const string& path ) {
745 fg_gzifstream in( path );
747 return(FG_OPTIONS_ERROR);
749 FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
753 while ( ! in.eof() ) {
756 while ( in.get(c) && c != '\0' ) {
761 #ifdef GETLINE_NEEDS_TERMINATOR
762 getline( in, line, '\n' );
767 if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
768 FG_LOG( FG_GENERAL, FG_ALERT,
769 "Config file parse error: " << path << " '"
776 return FG_OPTIONS_OK;
780 // Print usage message
781 void fgOPTIONS::usage ( void ) {
782 printf("Usage: fg [ options ... ]\n");
785 printf("General Options:\n");
786 printf("\t--help -h: print usage\n");
787 printf("\t--fg-root=path: specify the root path for all the data files\n");
788 printf("\t--disable-game-mode: disable full-screen game mode\n");
789 printf("\t--enable-game-mode: enable full-screen game mode\n");
790 printf("\t--disable-splash-screen: disable splash screen\n");
791 printf("\t--enable-splash-screen: enable splash screen\n");
792 printf("\t--disable-intro-music: disable introduction music\n");
793 printf("\t--enable-intro-music: enable introduction music\n");
794 printf("\t--disable-mouse-pointer: disable extra mouse pointer\n");
795 printf("\t--enable-mouse-pointer: enable extra mouse pointer (i.e. for\n");
796 printf("\t\tfull screen voodoo/voodoo-II based cards.)\n");
797 printf("\t--disable-pause: start out in an active state\n");
798 printf("\t--enable-pause: start out in a paused state\n");
799 printf("\t--control=mode: primary control mode (joystick, keyboard, mouse)\n");
802 printf("Features:\n");
803 printf("\t--disable-hud: disable heads up display\n");
804 printf("\t--enable-hud: enable heads up display\n");
805 printf("\t--disable-panel: disable instrument panel\n");
806 printf("\t--enable-panel: enable instrumetn panel\n");
807 printf("\t--disable-sound: disable sound effects\n");
808 printf("\t--enable-sound: enable sound effects\n");
811 printf("Flight Model:\n");
812 printf("\t--fdm=abcd: one of slew, jsb, larcsim, or external\n");
813 printf("\t--speed=n: run the FDM this much faster than real time\n");
816 printf("Initial Position and Orientation:\n");
817 printf("\t--airport-id=ABCD: specify starting postion by airport id\n");
818 printf("\t--lon=degrees: starting longitude in degrees (west = -)\n");
819 printf("\t--lat=degrees: starting latitude in degrees (south = -)\n");
820 printf("\t--altitude=feet: starting altitude in feet\n");
821 printf("\t\t(unless --units-meters specified\n");
822 printf("\t--heading=degrees: heading (yaw) angle in degress (Psi)\n");
823 printf("\t--roll=degrees: roll angle in degrees (Phi)\n");
824 printf("\t--pitch=degrees: pitch angle in degrees (Theta)\n");
825 printf("\t--uBody=feet per second: velocity along the body X axis\n");
826 printf("\t--vBody=feet per second: velocity along the body Y axis\n");
827 printf("\t--wBody=feet per second: velocity along the body Z axis\n");
828 printf("\t\t(unless --units-meters specified\n");
831 printf("Rendering Options:\n");
832 printf("\t--fog-disable: disable fog/haze\n");
833 printf("\t--fog-fastest: enable fastest fog/haze\n");
834 printf("\t--fog-nicest: enable nicest fog/haze\n");
835 printf("\t--fov=xx.x: specify initial field of view angle in degrees\n");
836 printf("\t--disable-fullscreen: disable fullscreen mode\n");
837 printf("\t--enable-fullscreen: enable fullscreen mode\n");
838 printf("\t--shading-flat: enable flat shading\n");
839 printf("\t--shading-smooth: enable smooth shading\n");
840 printf("\t--disable-skyblend: disable sky blending\n");
841 printf("\t--enable-skyblend: enable sky blending\n");
842 printf("\t--disable-textures: disable textures\n");
843 printf("\t--enable-textures: enable textures\n");
844 printf("\t--disable-wireframe: disable wireframe drawing mode\n");
845 printf("\t--enable-wireframe: enable wireframe drawing mode\n");
846 printf("\t--geometry=WWWxHHH: window geometry: 640x480, 800x600, etc.\n");
849 printf("Scenery Options:\n");
850 printf("\t--tile-radius=n: specify tile radius, must be 1 - 4\n");
853 printf("Hud Options:\n");
854 printf("\t--units-feet: Hud displays units in feet\n");
855 printf("\t--units-meters: Hud displays units in meters\n");
856 printf("\t--hud-tris: Hud displays number of triangles rendered\n");
857 printf("\t--hud-culled: Hud displays percentage of triangles culled\n");
860 printf("Time Options:\n");
861 printf("\t--time-offset=[+-]hh:mm:ss: offset local time by this amount\n");
862 printf("\t--start-date-gmt=yyyy:mm:dd:hh:mm:ss: specify a starting date/time. Time is Greenwich Mean Time\n");
863 printf("\t--start-date-lst=yyyy:mm:dd:hh:mm:ss: specify a starting date/time. Uses local sidereal time\n");
864 #ifdef FG_NETWORK_OLK
867 printf("Network Options:\n");
868 printf("\t--net-hud: Hud displays network info\n");
869 printf("\t--net-id=name: specify your own callsign\n");
875 fgOPTIONS::~fgOPTIONS( void ) {