]> git.mxchange.org Git - flightgear.git/blob - Simulator/Main/options.cxx
Removed in-src cvs logs.
[flightgear.git] / Simulator / Main / options.cxx
1 // options.cxx -- class to handle command line options
2 //
3 // Written by Curtis Olson, started April 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #if defined(FX) && defined(XMESA)
29 bool global_fullscreen = true;
30 #endif
31
32 #include <Include/compiler.h>
33
34 #include <math.h>            // rint()
35 #include <stdio.h>
36 #include <stdlib.h>          // atof(), atoi()
37 #include <string.h>
38
39 #include STL_STRING
40
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>
46
47 #include "fg_serial.hxx"
48
49 FG_USING_STD(string);
50 FG_USING_NAMESPACE(std);
51
52
53 inline double
54 atof( const string& str )
55 {
56     return ::atof( str.c_str() );
57 }
58
59 inline int
60 atoi( const string& str )
61 {
62     return ::atoi( str.c_str() );
63 }
64
65 // Defined the shared options class here
66 fgOPTIONS current_options;
67
68
69 // Constructor
70 fgOPTIONS::fgOPTIONS() :
71     // starting longitude in degrees (west = -)
72     // starting latitude in degrees (south = -)
73
74     // Default initial position is Globe, AZ (P13)
75     lon(-110.6642444),
76     lat(  33.3528917),
77
78     // North of the city of Globe
79     // lon(-110.7),
80     // lat(  33.4),
81
82     // North of the city of Globe
83     // lon(-110.742578),
84     // lat(  33.507122),
85
86     // Near where I used to live in Globe, AZ
87     // lon(-110.766000),
88     // lat(  33.377778),
89
90     // 10125 Jewell St. NE
91     // lon(-93.15),
92     // lat( 45.15),
93
94     // Near KHSP (Hot Springs, VA)
95     // lon(-79.8338964 + 0.01),
96     // lat( 37.9514564 + 0.008),
97
98     // (SEZ) SEDONA airport
99     // lon(-111.7884614 + 0.01),
100     // lat(  34.8486289 - 0.015),
101
102     // Jim Brennon's Kingmont Observatory
103     // lon(-121.1131667),
104     // lat(  38.8293917),
105
106     // Huaras, Peru (S09d 31.871'  W077d 31.498')
107     // lon(-77.5249667),
108     // lat( -9.5311833),
109  
110     // Eclipse Watching w73.5 n10 (approx) 18:00 UT
111     // lon(-73.5),
112     // lat( 10.0),
113
114     // Timms Hill (WI)
115     // lon(-90.1953055556),
116     // lat( 45.4511388889),
117
118     // starting altitude in meters (this will be reset to ground level
119     // if it is lower than the terrain
120     altitude(-9999.0),
121
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)
126
127     // Miscellaneous
128     game_mode(0),
129     splash_screen(1),
130     intro_music(1),
131     mouse_pointer(0),
132     pause(0),
133
134     // Features
135     hud_status(1),
136     panel_status(0),
137     sound(1),
138
139     // Flight Model options
140     flight_model(FGInterface::FG_LARCSIM),
141
142     // Rendering options
143     fog(FG_FOG_NICEST),  // nicest
144     fov(55.0),
145     fullscreen(0),
146     shading(1),
147     skyblend(1),
148     textures(1),
149     wireframe(0),
150     xsize(640),
151     ysize(480),
152
153     // Scenery options
154     tile_diameter(5),
155
156     // HUD options
157     units(FG_UNITS_FEET),
158     tris_or_culled(0),
159         
160     // Time options
161     time_offset(0)
162
163 {
164     // set initial values/defaults
165     char* envp = ::getenv( "FG_ROOT" );
166
167     if ( envp != NULL ) {
168         // fg_root could be anywhere, so default to environmental
169         // variable $FG_ROOT if it is set.
170         fg_root = envp;
171     } else {
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.
175
176 #if defined(WIN32)
177         fg_root = "\\FlightGear";
178 #else
179         fg_root = "/usr/local/lib/FlightGear";
180 #endif
181     }
182
183     airport_id = "";  // default airport id
184
185     // initialize port config string list
186     port_options_list.erase ( port_options_list.begin(), 
187                               port_options_list.end() );
188 }
189
190
191 double
192 fgOPTIONS::parse_time(const string& time_in) {
193     char *time_str, num[256];
194     double hours, minutes, seconds;
195     double result = 0.0;
196     int sign = 1;
197     int i;
198
199     time_str = (char *)time_in.c_str();
200
201     // printf("parse_time(): %s\n", time_str);
202
203     // check for sign
204     if ( strlen(time_str) ) {
205         if ( time_str[0] == '+' ) {
206             sign = 1;
207             time_str++;
208         } else if ( time_str[0] == '-' ) {
209             sign = -1;
210             time_str++;
211         }
212     }
213     // printf("sign = %d\n", sign);
214
215     // get hours
216     if ( strlen(time_str) ) {
217         i = 0;
218         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
219             num[i] = time_str[0];
220             time_str++;
221             i++;
222         }
223         if ( time_str[0] == ':' ) {
224             time_str++;
225         }
226         num[i] = '\0';
227         hours = atof(num);
228         // printf("hours = %.2lf\n", hours);
229
230         result += hours;
231     }
232
233     // get minutes
234     if ( strlen(time_str) ) {
235         i = 0;
236         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
237             num[i] = time_str[0];
238             time_str++;
239             i++;
240         }
241         if ( time_str[0] == ':' ) {
242             time_str++;
243         }
244         num[i] = '\0';
245         minutes = atof(num);
246         // printf("minutes = %.2lf\n", minutes);
247
248         result += minutes / 60.0;
249     }
250
251     // get seconds
252     if ( strlen(time_str) ) {
253         i = 0;
254         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
255             num[i] = time_str[0];
256             time_str++;
257             i++;
258         }
259         num[i] = '\0';
260         seconds = atof(num);
261         // printf("seconds = %.2lf\n", seconds);
262
263         result += seconds / 3600.0;
264     }
265
266     return(sign * result);
267 }
268
269
270 // parse degree in the form of [+/-]hhh:mm:ss
271 double
272 fgOPTIONS::parse_degree( const string& degree_str) {
273     double result = parse_time( degree_str );
274
275     // printf("Degree = %.4f\n", result);
276
277     return(result);
278 }
279
280
281 // parse time offset command line option
282 int
283 fgOPTIONS::parse_time_offset( const string& time_str) {
284     int result;
285
286     // printf("time offset = %s\n", time_str);
287
288 #ifdef HAVE_RINT
289     result = (int)rint(parse_time(time_str) * 3600.0);
290 #else
291     result = (int)(parse_time(time_str) * 3600.0);
292 #endif
293
294     // printf("parse_time_offset(): %d\n", result);
295
296     return( result );
297 }
298
299
300 // Parse --tile-diameter=n type option 
301
302 int
303 fgOPTIONS::parse_tile_radius( const string& arg ) {
304     int radius = atoi( arg );
305
306     if ( radius < FG_RADIUS_MIN ) { radius = FG_RADIUS_MIN; }
307     if ( radius > FG_RADIUS_MAX ) { radius = FG_RADIUS_MAX; }
308
309     // printf("parse_tile_radius(): radius = %d\n", radius);
310
311     return(radius);
312 }
313
314
315 // Parse --fdm=abcdefg type option 
316 int
317 fgOPTIONS::parse_fdm( const string& fm ) {
318     // printf("fdm = %s\n", fm);
319
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;
328     } else {
329         FG_LOG( FG_GENERAL, FG_ALERT, "Unknown fdm = " << fm );
330         exit(-1);
331     }
332
333     // we'll never get here, but it makes the compiler happy.
334     return -1;
335 }
336
337
338 // Parse --fov=x.xx type option 
339 double
340 fgOPTIONS::parse_fov( const string& arg ) {
341     double fov = atof(arg);
342
343     if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
344     if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
345
346     // printf("parse_fov(): result = %.4f\n", fov);
347
348     return(fov);
349 }
350
351
352 // Parse serial port option --serial=/dev/ttyS1,nmea,4800,out
353 //
354 // Format is "--serial=device,format,baud,direction" where
355 // 
356 //  device = OS device name to be open()'ed
357 //  format = {nmea, fgfs}
358 //  baud = {300, 1200, 2400, ..., 230400}
359 //  direction = {in, out, bi}
360
361 bool 
362 fgOPTIONS::parse_serial( const string& serial_str ) {
363     string::size_type pos;
364
365     // cout << "Serial string = " << serial_str << endl;
366
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" );
373         return false;
374     }
375     
376     port_options_list.push_back( serial_str );
377
378     return true;
379 }
380
381
382 // Parse a single option
383 int fgOPTIONS::parse_option( const string& arg ) {
384     // General Options
385     if ( (arg == "--help") || (arg == "-h") ) {
386         // help/usage request
387         return(FG_OPTIONS_HELP);
388     } else if ( arg == "--disable-game-mode") {
389         game_mode = false;
390     } else if ( arg == "--enable-game-mode" ) {
391         game_mode = true;
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" ) {
397         intro_music = false;
398     } else if ( arg == "--enable-intro-music" ) {
399         intro_music = true;
400     } else if ( arg == "--disable-mouse-pointer" ) {
401         mouse_pointer = 1;
402     } else if ( arg == "--enable-mouse-pointer" ) {
403         mouse_pointer = 2;
404     } else if ( arg == "--disable-pause" ) {
405         pause = false;  
406     } else if ( arg == "--enable-pause" ) {
407         pause = true;   
408     } else if ( arg == "--disable-hud" ) {
409         hud_status = false;     
410     } else if ( arg == "--enable-hud" ) {
411         hud_status = true;      
412     } else if ( arg == "--disable-panel" ) {
413         panel_status = false;
414     } else if ( arg == "--enable-panel" ) {
415         panel_status = true;
416         fov *= 0.4232;
417     } else if ( arg == "--disable-sound" ) {
418         sound = false;
419     } else if ( arg == "--enable-sound" ) {
420         sound = true;
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;
430         } else {
431             altitude = atof( arg.substr(11) );
432         }
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" ) {
448         fog = FG_FOG_NICEST;    
449     } else if ( arg.find( "--fov=" ) != string::npos ) {
450         fov = parse_fov( arg.substr(6) );
451     } else if ( arg == "--disable-fullscreen" ) {
452         fullscreen = false;     
453     } else if ( arg== "--enable-fullscreen") {
454         fullscreen = true;      
455     } else if ( arg == "--shading-flat") {
456         shading = 0;    
457     } else if ( arg == "--shading-smooth") {
458         shading = 1;    
459     } else if ( arg == "--disable-skyblend") {
460         skyblend = false;       
461     } else if ( arg== "--enable-skyblend" ) {
462         skyblend = true;        
463     } else if ( arg == "--disable-textures" ) {
464         textures = false;       
465     } else if ( arg == "--enable-textures" ) {
466         textures = true;
467     } else if ( arg == "--disable-wireframe" ) {
468         wireframe = false;      
469     } else if ( arg == "--enable-wireframe" ) {
470         wireframe = true;
471     } else if ( arg.find( "--geometry=" ) != string::npos ) {
472         string geometry = arg.substr( 11 );
473         if ( geometry == "640x480" ) {
474             xsize = 640;
475             ysize = 480;
476         } else if ( geometry == "800x600" ) {
477             xsize = 800;
478             ysize = 600;
479         } else if ( geometry == "1024x768" ) {
480             xsize = 1024;
481             ysize = 768;
482         } else {
483             FG_LOG( FG_GENERAL, FG_ALERT, "Unknown geometry: " << geometry );
484             exit(-1);
485         }
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" ) {
496         tris_or_culled = 0;     
497     } else if ( arg == "--hud-culled" ) {
498         tris_or_culled = 1;
499     } else if ( arg.find( "--serial=" ) != string::npos ) {
500         parse_serial( arg.substr(9) );
501     } else {
502         FG_LOG( FG_GENERAL, FG_ALERT, "Unknown option '" << arg << "'" );
503         return FG_OPTIONS_ERROR;
504     }
505     
506     return FG_OPTIONS_OK;
507 }
508
509
510 // Parse the command line options
511 int fgOPTIONS::parse_command_line( int argc, char **argv ) {
512     int i = 1;
513     int result;
514
515     FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
516
517     while ( i < argc ) {
518         FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
519
520         result = parse_option(argv[i]);
521         if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
522             return(result);
523         }
524
525         i++;
526     }
527     
528     return(FG_OPTIONS_OK);
529 }
530
531
532 // Parse config file options
533 int fgOPTIONS::parse_config_file( const string& path ) {
534     fg_gzifstream in( path );
535     if ( !in )
536         return(FG_OPTIONS_ERROR);
537
538     FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
539
540     in >> skipcomment;
541     while ( !in.eof() )
542     {
543         string line;
544         getline( in, line );
545
546         if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
547             FG_LOG( FG_GENERAL, FG_ALERT, 
548                     "Config file parse error: " << path << " '" 
549                     << line << "'" );
550             exit(-1);
551         }
552         in >> skipcomment;
553     }
554
555     return FG_OPTIONS_OK;
556 }
557
558
559 // Print usage message
560 void fgOPTIONS::usage ( void ) {
561     printf("Usage: fg [ options ... ]\n");
562     printf("\n");
563
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");
578     printf("\n");
579
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");
587     printf("\n");
588  
589     printf("Flight Model:\n");
590     printf("\t--fdm=abcd:  one of slew, jsb, larcsim, or external\n");
591     printf("\n");
592
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");
602     printf("\n");
603
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");
620     printf("\n");
621
622     printf("Scenery Options:\n");
623     printf("\t--tile-radius=n:  specify tile radius, must be 1 - 4\n");
624     printf("\n");
625
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");
631     printf("\n");
632         
633     printf("Time Options:\n");
634     printf("\t--time-offset=[+-]hh:mm:ss:  offset local time by this amount\n");
635 }
636
637
638 // Destructor
639 fgOPTIONS::~fgOPTIONS( void ) {
640 }
641
642