]> git.mxchange.org Git - flightgear.git/blob - src/Main/options.cxx
Miscellaneous MSVC porting fixes by Christian Mayer.
[flightgear.git] / src / 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 <simgear/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 <simgear/constants.h>
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/fgstream.hxx>
44 #include <simgear/misc/props.hxx>
45 #include <simgear/timing/sg_time.hxx>
46
47 #include <Include/general.hxx>
48 #include <Airports/simple.hxx>
49 #include <Cockpit/cockpit.hxx>
50 #include <FDM/flight.hxx>
51 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
52 #ifdef FG_NETWORK_OLK
53 #  include <NetworkOLK/network.h>
54 #endif
55
56 #include "fg_init.hxx"
57 #include "globals.hxx"
58 #include "options.hxx"
59
60 FG_USING_STD(string);
61 FG_USING_NAMESPACE(std);
62
63 // from main.cxx
64 extern void fgReshape( int width, int height );
65
66 inline double
67 atof( const string& str )
68 {
69
70 #ifdef __MWERKS__ 
71     // -dw- if ::atof is called, then we get an infinite loop
72     return std::atof( str.c_str() );
73 #else
74     return ::atof( str.c_str() );
75 #endif
76 }
77
78 inline int
79 atoi( const string& str )
80 {
81 #ifdef __MWERKS__ 
82     // -dw- if ::atoi is called, then we get an infinite loop
83     return std::atoi( str.c_str() );
84 #else
85     return ::atoi( str.c_str() );
86 #endif
87 }
88
89
90 // Defined the shared options class here
91 // FGOptions current_options;
92
93
94 // Constructor
95 FGOptions::FGOptions() :
96     // starting longitude in degrees (west = -)
97     // starting latitude in degrees (south = -)
98
99     // Default initial position is Globe, AZ (P13)
100     lon(-110.6642444),
101     lat(  33.3528917),
102
103     // North of the city of Globe
104     // lon(-110.7),
105     // lat(  33.4),
106
107     // North of the city of Globe
108     // lon(-110.742578),
109     // lat(  33.507122),
110
111     // Near where I used to live in Globe, AZ
112     // lon(-110.766000),
113     // lat(  33.377778),
114
115     // 10125 Jewell St. NE
116     // lon(-93.15),
117     // lat( 45.15),
118
119     // Near KHSP (Hot Springs, VA)
120     // lon(-79.8338964 + 0.01),
121     // lat( 37.9514564 + 0.008),
122
123     // (SEZ) SEDONA airport
124     // lon(-111.7884614 + 0.01),
125     // lat(  34.8486289 - 0.015),
126
127     // Jim Brennon's Kingmont Observatory
128     // lon(-121.1131667),
129     // lat(  38.8293917),
130
131     // Huaras, Peru (S09d 31.871'  W077d 31.498')
132     // lon(-77.5249667),
133     // lat( -9.5311833),
134  
135     // Eclipse Watching w73.5 n10 (approx) 18:00 UT
136     // lon(-73.5),
137     // lat( 10.0),
138
139     // Timms Hill (WI)
140     // lon(-90.1953055556),
141     // lat( 45.4511388889),
142
143     // starting altitude in meters (this will be reset to ground level
144     // if it is lower than the terrain
145     altitude(-9999.0),
146
147     // Initial Orientation
148     heading(270.0),      // heading (yaw) angle in degress (Psi)
149     roll(0.0),           // roll angle in degrees (Phi)
150     pitch(0.424),        // pitch angle in degrees (Theta)
151
152     // Initialize current options velocities to 0.0
153     speedset(FG_VC),
154     uBody(0.0), vBody(0.0), wBody(0.0), 
155     vNorth(0.0),vEast(0.0), vDown(0.0),
156     vkcas(0.0), mach(0.0),
157
158     // Miscellaneous
159     game_mode(0),
160     splash_screen(1),
161     intro_music(1),
162     mouse_pointer(0),
163     control_mode(FG_JOYSTICK),
164     auto_coordination(FG_AUTO_COORD_NOT_SPECIFIED),
165
166     // Features
167     hud_status(0),
168     panel_status(1),
169     sound(1),
170     anti_alias_hud(0),
171
172     // Flight Model options
173     flight_model( FGInterface::FG_LARCSIM ),
174     aircraft( "c172" ),
175     model_hz( NEW_DEFAULT_MODEL_HZ ),
176     speed_up( 1 ),
177     trim(0),
178
179     // Rendering options
180     fog(FG_FOG_NICEST),  // nicest
181     clouds(true),
182     clouds_asl(5000*FEET_TO_METER),
183     fullscreen(0),
184     shading(1),
185     skyblend(1),
186     textures(1),
187     wireframe(0),
188     xsize(800),
189     ysize(600),
190     bpp(16),
191     view_mode(FG_VIEW_PILOT),
192     default_view_offset(0),
193     visibility(-1.0),
194
195     // HUD options
196     units(FG_UNITS_FEET),
197     tris_or_culled(0),
198         
199     // Time options
200     time_offset(0),
201
202     network_olk(false)
203 {
204     // set initial values/defaults
205     time_offset_type = FG_TIME_SYS_OFFSET;
206     char* envp = ::getenv( "FG_ROOT" );
207
208     if ( envp != NULL ) {
209         // fg_root could be anywhere, so default to environmental
210         // variable $FG_ROOT if it is set.
211         fg_root = envp;
212     } else {
213         // Otherwise, default to a random compiled-in location if
214         // $FG_ROOT is not set.  This can still be overridden from the
215         // command line or a config file.
216
217 #if defined( WIN32 )
218         fg_root = "\\FlightGear";
219 #elif defined( macintosh )
220         fg_root = "";
221 #else
222         fg_root = PKGLIBDIR;
223 #endif
224     }
225
226     // set a possibly independent location for scenery data
227     envp = ::getenv( "FG_SCENERY" );
228
229     if ( envp != NULL ) {
230         // fg_root could be anywhere, so default to environmental
231         // variable $FG_ROOT if it is set.
232         fg_scenery = envp;
233     } else {
234         // Otherwise, default to Scenery being in $FG_ROOT/Scenery
235         fg_scenery = "";
236     }
237
238     airport_id = "KPAO";        // default airport id
239     net_id = "Johnney";         // default pilot's name
240
241     // initialize port config string list
242     channel_options_list.clear();
243 }
244
245 void 
246 FGOptions::toggle_panel() {
247     
248     bool freeze = globals->get_freeze();
249
250     if( !freeze )
251         globals->set_freeze(true);
252     
253     if( panel_status ) {
254         panel_status = false;
255         if ( current_panel != NULL )
256           current_panel->setVisibility(false);
257     } else {
258         panel_status = true;
259         if ( current_panel != NULL )
260           current_panel->setVisibility(true);
261     }
262
263     fgReshape( xsize, ysize);
264
265     if( !freeze )
266         globals->set_freeze( false );
267 }
268
269 double
270 FGOptions::parse_time(const string& time_in) {
271     char *time_str, num[256];
272     double hours, minutes, seconds;
273     double result = 0.0;
274     int sign = 1;
275     int i;
276
277     time_str = (char *)time_in.c_str();
278
279     // printf("parse_time(): %s\n", time_str);
280
281     // check for sign
282     if ( strlen(time_str) ) {
283         if ( time_str[0] == '+' ) {
284             sign = 1;
285             time_str++;
286         } else if ( time_str[0] == '-' ) {
287             sign = -1;
288             time_str++;
289         }
290     }
291     // printf("sign = %d\n", sign);
292
293     // get hours
294     if ( strlen(time_str) ) {
295         i = 0;
296         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
297             num[i] = time_str[0];
298             time_str++;
299             i++;
300         }
301         if ( time_str[0] == ':' ) {
302             time_str++;
303         }
304         num[i] = '\0';
305         hours = atof(num);
306         // printf("hours = %.2lf\n", hours);
307
308         result += hours;
309     }
310
311     // get minutes
312     if ( strlen(time_str) ) {
313         i = 0;
314         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
315             num[i] = time_str[0];
316             time_str++;
317             i++;
318         }
319         if ( time_str[0] == ':' ) {
320             time_str++;
321         }
322         num[i] = '\0';
323         minutes = atof(num);
324         // printf("minutes = %.2lf\n", minutes);
325
326         result += minutes / 60.0;
327     }
328
329     // get seconds
330     if ( strlen(time_str) ) {
331         i = 0;
332         while ( (time_str[0] != ':') && (time_str[0] != '\0') ) {
333             num[i] = time_str[0];
334             time_str++;
335             i++;
336         }
337         num[i] = '\0';
338         seconds = atof(num);
339         // printf("seconds = %.2lf\n", seconds);
340
341         result += seconds / 3600.0;
342     }
343
344     return(sign * result);
345 }
346
347
348 long int FGOptions::parse_date( const string& date)
349 {
350     struct tm gmt;
351     char * date_str, num[256];
352     int i;
353     // initialize to zero
354     gmt.tm_sec = 0;
355     gmt.tm_min = 0;
356     gmt.tm_hour = 0;
357     gmt.tm_mday = 0;
358     gmt.tm_mon = 0;
359     gmt.tm_year = 0;
360     gmt.tm_isdst = 0; // ignore daylight savings time for the moment
361     date_str = (char *)date.c_str();
362     // get year
363     if ( strlen(date_str) ) {
364         i = 0;
365         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
366             num[i] = date_str[0];
367             date_str++;
368             i++;
369         }
370         if ( date_str[0] == ':' ) {
371             date_str++;
372         }
373         num[i] = '\0';
374         gmt.tm_year = atoi(num) - 1900;
375     }
376     // get month
377     if ( strlen(date_str) ) {
378         i = 0;
379         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
380             num[i] = date_str[0];
381             date_str++;
382             i++;
383         }
384         if ( date_str[0] == ':' ) {
385             date_str++;
386         }
387         num[i] = '\0';
388         gmt.tm_mon = atoi(num) -1;
389     }
390     // get day
391     if ( strlen(date_str) ) {
392         i = 0;
393         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
394             num[i] = date_str[0];
395             date_str++;
396             i++;
397         }
398         if ( date_str[0] == ':' ) {
399             date_str++;
400         }
401         num[i] = '\0';
402         gmt.tm_mday = atoi(num);
403     }
404     // get hour
405     if ( strlen(date_str) ) {
406         i = 0;
407         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
408             num[i] = date_str[0];
409             date_str++;
410             i++;
411         }
412         if ( date_str[0] == ':' ) {
413             date_str++;
414         }
415         num[i] = '\0';
416         gmt.tm_hour = atoi(num);
417     }
418     // get minute
419     if ( strlen(date_str) ) {
420         i = 0;
421         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
422             num[i] = date_str[0];
423             date_str++;
424             i++;
425         }
426         if ( date_str[0] == ':' ) {
427             date_str++;
428         }
429         num[i] = '\0';
430         gmt.tm_min = atoi(num);
431     }
432     // get second
433     if ( strlen(date_str) ) {
434         i = 0;
435         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
436             num[i] = date_str[0];
437             date_str++;
438             i++;
439         }
440         if ( date_str[0] == ':' ) {
441             date_str++;
442         }
443         num[i] = '\0';
444         gmt.tm_sec = atoi(num);
445     }
446     time_t theTime = sgTimeGetGMT( gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
447                                    gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
448     //printf ("Date is %s\n", ctime(&theTime));
449     //printf ("in seconds that is %d\n", theTime);
450     //exit(1);
451     return (theTime);
452 }
453
454
455 // parse degree in the form of [+/-]hhh:mm:ss
456 void FGOptions::parse_control( const string& mode ) {
457     if ( mode == "joystick" ) {
458         control_mode = FG_JOYSTICK;
459     } else if ( mode == "mouse" ) {
460         control_mode = FG_MOUSE;
461     } else {
462         control_mode = FG_KEYBOARD;
463     }
464 }
465
466
467 /// parse degree in the form of [+/-]hhh:mm:ss
468 double
469 FGOptions::parse_degree( const string& degree_str) {
470     double result = parse_time( degree_str );
471
472     // printf("Degree = %.4f\n", result);
473
474     return(result);
475 }
476
477
478 // parse time offset command line option
479 int
480 FGOptions::parse_time_offset( const string& time_str) {
481     int result;
482
483     // printf("time offset = %s\n", time_str);
484
485 #ifdef HAVE_RINT
486     result = (int)rint(parse_time(time_str) * 3600.0);
487 #else
488     result = (int)(parse_time(time_str) * 3600.0);
489 #endif
490
491     // printf("parse_time_offset(): %d\n", result);
492
493     return( result );
494 }
495
496
497 // Parse --fdm=abcdefg type option 
498 int
499 FGOptions::parse_fdm( const string& fm ) {
500     // cout << "fdm = " << fm << endl;
501
502     if ( fm == "ada" ) {
503         return FGInterface::FG_ADA;
504     } else if ( fm == "balloon" ) {
505         return FGInterface::FG_BALLOONSIM;
506     } else if ( fm == "external" ) {
507         return FGInterface::FG_EXTERNAL;
508     } else if ( fm == "jsb" ) {
509         return FGInterface::FG_JSBSIM;
510     } else if ( (fm == "larcsim") || (fm == "LaRCsim") ) {
511         return FGInterface::FG_LARCSIM;
512     } else if ( fm == "magic" ) {
513         return FGInterface::FG_MAGICCARPET;
514     } else {
515         FG_LOG( FG_GENERAL, FG_ALERT, "Unknown fdm = " << fm );
516         exit(-1);
517     }
518
519     // we'll never get here, but it makes the compiler happy.
520     return -1;
521 }
522
523
524 // Parse --fov=x.xx type option 
525 double
526 FGOptions::parse_fov( const string& arg ) {
527     double fov = atof(arg);
528
529     if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
530     if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
531
532     globals->get_current_view()->set_fov( fov );
533
534     // printf("parse_fov(): result = %.4f\n", fov);
535
536     return fov;
537 }
538
539
540 // Parse I/O channel option
541 //
542 // Format is "--protocol=medium,direction,hz,medium_options,..."
543 //
544 //   protocol = { native, nmea, garmin, fgfs, rul, pve, etc. }
545 //   medium = { serial, socket, file, etc. }
546 //   direction = { in, out, bi }
547 //   hz = number of times to process channel per second (floating
548 //        point values are ok.
549 //
550 // Serial example "--nmea=serial,dir,hz,device,baud" where
551 // 
552 //  device = OS device name of serial line to be open()'ed
553 //  baud = {300, 1200, 2400, ..., 230400}
554 //
555 // Socket exacmple "--native=socket,dir,hz,machine,port,style" where
556 // 
557 //  machine = machine name or ip address if client (leave empty if server)
558 //  port = port, leave empty to let system choose
559 //  style = tcp or udp
560 //
561 // File example "--garmin=file,dir,hz,filename" where
562 // 
563 //  filename = file system file name
564
565 bool 
566 FGOptions::parse_channel( const string& type, const string& channel_str ) {
567     // cout << "Channel string = " << channel_str << endl;
568
569     channel_options_list.push_back( type + "," + channel_str );
570
571     return true;
572 }
573
574
575 // Parse --wp=ID[,alt]
576 bool FGOptions::parse_wp( const string& arg ) {
577     string id, alt_str;
578     double alt = 0.0;
579
580     int pos = arg.find( "@" );
581     if ( pos != string::npos ) {
582         id = arg.substr( 0, pos );
583         alt_str = arg.substr( pos + 1 );
584         // cout << "id str = " << id << "  alt str = " << alt_str << endl;
585         alt = atof( alt_str.c_str() );
586         if ( units == FG_UNITS_FEET ) {
587             alt *= FEET_TO_METER;
588         }
589     } else {
590         id = arg;
591     }
592
593     FGAirport a;
594     if ( fgFindAirportID( id, &a ) ) {
595         SGWayPoint wp( a.longitude, a.latitude, alt, SGWayPoint::WGS84, id );
596         globals->get_route()->add_waypoint( wp );
597
598         return true;
599     } else {
600         return false;
601     }
602 }
603
604
605 // Parse --flight-plan=[file]
606 bool FGOptions::parse_flightplan(const string& arg)
607 {
608     fg_gzifstream infile(arg.c_str());
609     if (!infile) {
610         return false;
611     }
612     while ( true ) {
613         string line;
614 #ifdef GETLINE_NEEDS_TERMINATOR
615         getline( infile, line, '\n' );
616 #elif defined( macintosh )
617         getline( infile, line, '\r' );
618 #else
619         getline( infile, line );
620 #endif
621         if ( infile.eof() ) {
622             break;
623         }
624         parse_wp(line);
625     }
626
627     return true;
628 }
629
630
631 // Parse a single option
632 int FGOptions::parse_option( const string& arg ) {
633     // General Options
634     if ( (arg == "--help") || (arg == "-h") ) {
635         // help/usage request
636         return(FG_OPTIONS_HELP);
637     } else if ( arg == "--disable-game-mode") {
638         game_mode = false;
639     } else if ( arg == "--enable-game-mode" ) {
640         game_mode = true;
641     } else if ( arg == "--disable-splash-screen" ) {
642         splash_screen = false;
643     } else if ( arg == "--enable-splash-screen" ) {
644         splash_screen = true;
645     } else if ( arg == "--disable-intro-music" ) {
646         intro_music = false;
647     } else if ( arg == "--enable-intro-music" ) {
648         intro_music = true;
649     } else if ( arg == "--disable-mouse-pointer" ) {
650         mouse_pointer = 1;
651     } else if ( arg == "--enable-mouse-pointer" ) {
652         mouse_pointer = 2;
653     } else if ( arg == "--disable-freeze" ) {
654         globals->set_freeze( false );
655     } else if ( arg == "--enable-freeze" ) {
656         globals->set_freeze( true );
657     } else if ( arg == "--disable-anti-alias-hud" ) {
658         anti_alias_hud = false; 
659     } else if ( arg == "--enable-anti-alias-hud" ) {
660         anti_alias_hud = true;  
661     } else if ( arg.find( "--control=") != string::npos ) {
662         parse_control( arg.substr(10) );
663     } else if ( arg == "--disable-auto-coordination" ) {
664         auto_coordination = FG_AUTO_COORD_DISABLED;     
665     } else if ( arg == "--enable-auto-coordination" ) {
666         auto_coordination = FG_AUTO_COORD_ENABLED;      
667     } else if ( arg == "--disable-hud" ) {
668         hud_status = false;     
669         globals->get_props()->setBoolValue("/sim/hud/visibility", false);
670     } else if ( arg == "--enable-hud" ) {
671         hud_status = true;
672         globals->get_props()->setBoolValue("/sim/hud/visibility", true);
673     } else if ( arg == "--disable-panel" ) {
674         panel_status = false;
675         globals->get_props()->setBoolValue("/sim/panel/visibility", false);
676         if ( current_panel != NULL )
677           current_panel->setVisibility(false);
678     } else if ( arg == "--enable-panel" ) {
679         panel_status = true;
680         globals->get_props()->setBoolValue("/sim/panel/visibility", true);
681         if ( current_panel != NULL )
682             current_panel->setVisibility(true);
683     } else if ( arg == "--disable-sound" ) {
684         sound = false;
685     } else if ( arg == "--enable-sound" ) {
686         sound = true;
687     } else if ( arg.find( "--airport-id=") != string::npos ) {
688         airport_id = arg.substr( 13 );
689         globals->get_props()->setStringValue("/position/airport-id", airport_id);
690     } else if ( arg.find( "--lon=" ) != string::npos ) {
691         lon = parse_degree( arg.substr(6) );
692         airport_id = "";
693         globals->get_props()->setDoubleValue("/position/longitude", lon);
694         globals->get_props()->setStringValue("/position/airport-id", airport_id);
695     } else if ( arg.find( "--lat=" ) != string::npos ) {
696         lat = parse_degree( arg.substr(6) );
697         airport_id = "";
698         globals->get_props()->setDoubleValue("/position/latitude", lat);
699         globals->get_props()->setStringValue("/position/airport-id", airport_id);
700     } else if ( arg.find( "--altitude=" ) != string::npos ) {
701         if ( units == FG_UNITS_FEET ) {
702             altitude = atof( arg.substr(11) ) * FEET_TO_METER;
703         } else {
704             altitude = atof( arg.substr(11) );
705         }
706         globals->get_props()->setDoubleValue("/position/altitude", altitude);
707     } else if ( arg.find( "--uBody=" ) != string::npos ) {
708         speedset = FG_VTUVW;
709         if ( units == FG_UNITS_FEET ) {
710             uBody = atof( arg.substr(8) );
711         } else {
712             uBody = atof( arg.substr(8) ) * FEET_TO_METER;
713         }
714         //globals->get_props()->setDoubleValue("/velocities/speed-north", uBody);
715     } else if ( arg.find( "--vBody=" ) != string::npos ) {
716         speedset = FG_VTUVW;
717         if ( units == FG_UNITS_FEET ) {
718             vBody = atof( arg.substr(8) );
719         } else {
720             vBody = atof( arg.substr(8) ) * FEET_TO_METER;
721         }
722         //globals->get_props()->setDoubleValue("/velocities/speed-east", vBody);
723     } else if ( arg.find( "--wBody=" ) != string::npos ) {
724         speedset = FG_VTUVW;
725         if ( units == FG_UNITS_FEET ) {
726             wBody = atof( arg.substr(8) );
727         } else {
728             wBody = atof( arg.substr(8) ) * FEET_TO_METER;
729         }
730     } else if ( arg.find( "--vNorth=" ) != string::npos ) {
731         speedset = FG_VTNED;
732         if ( units == FG_UNITS_FEET ) {
733             vNorth = atof( arg.substr(9) );
734         } else {
735             vNorth = atof( arg.substr(9) ) * FEET_TO_METER;
736         }
737     } else if ( arg.find( "--vEast=" ) != string::npos ) {
738         speedset = FG_VTNED;
739         if ( units == FG_UNITS_FEET ) {
740             vEast = atof( arg.substr(8) );
741         } else {
742             vEast = atof( arg.substr(8) ) * FEET_TO_METER;
743         }
744     } else if ( arg.find( "--vDown=" ) != string::npos ) {
745         speedset = FG_VTNED;
746         if ( units == FG_UNITS_FEET ) {
747             vDown = atof( arg.substr(8) );
748         } else {
749             vDown = atof( arg.substr(8) ) * FEET_TO_METER;
750         }
751     } else if ( arg.find( "--vc=" ) != string::npos) {
752         speedset = FG_VC;
753         vkcas=atof( arg.substr(5) );
754         cout << "Got vc: " << vkcas << endl;
755         globals->get_props()->setDoubleValue("/velocities/airspeed", vkcas);
756     } else if ( arg.find( "--mach=" ) != string::npos) {
757         speedset = FG_MACH;
758         mach=atof( arg.substr(7) );
759     } else if ( arg.find( "--heading=" ) != string::npos ) {
760         heading = atof( arg.substr(10) );
761         globals->get_props()->setDoubleValue("/orientation/heading", heading);
762     } else if ( arg.find( "--roll=" ) != string::npos ) {
763         roll = atof( arg.substr(7) );
764         globals->get_props()->setDoubleValue("/orientation/roll", roll);
765     } else if ( arg.find( "--pitch=" ) != string::npos ) {
766         pitch = atof( arg.substr(8) );
767         globals->get_props()->setDoubleValue("/orientation/pitch", pitch);
768     } else if ( arg.find( "--fg-root=" ) != string::npos ) {
769         fg_root = arg.substr( 10 );
770     } else if ( arg.find( "--fg-scenery=" ) != string::npos ) {
771         fg_scenery = arg.substr( 13 );
772     } else if ( arg.find( "--fdm=" ) != string::npos ) {
773         flight_model = parse_fdm( arg.substr(6) );
774         globals->get_props()->setIntValue("/sim/flight-model", flight_model);
775     if((flight_model == FGInterface::FG_JSBSIM) && (get_trim_mode() == 0)) {
776         set_trim_mode(1);
777     } else {
778         set_trim_mode(0);
779     }        
780     } else if ( arg.find( "--aircraft=" ) != string::npos ) {
781         aircraft = arg.substr(11);
782         globals->get_props()->setStringValue("/sim/aircraft", aircraft);
783     } else if ( arg.find( "--aircraft-dir=" ) != string::npos ) {
784         aircraft_dir =  arg.substr(15); //  (UIUC)
785     } else if ( arg.find( "--model-hz=" ) != string::npos ) {
786         model_hz = atoi( arg.substr(11) );
787     } else if ( arg.find( "--speed=" ) != string::npos ) {
788         speed_up = atoi( arg.substr(8) );
789     } else if ( arg.find( "--notrim") != string::npos) {
790         trim=-1;
791     } else if ( arg == "--fog-disable" ) {
792         fog = FG_FOG_DISABLED;  
793     } else if ( arg == "--fog-fastest" ) {
794         fog = FG_FOG_FASTEST;   
795     } else if ( arg == "--fog-nicest" ) {
796         fog = FG_FOG_NICEST;    
797     } else if ( arg == "--disable-clouds" ) {
798         clouds = false; 
799     } else if ( arg == "--enable-clouds" ) {
800         clouds = true;  
801     } else if ( arg.find( "--clouds-asl=" ) != string::npos ) {
802         if ( units == FG_UNITS_FEET ) {
803             clouds_asl = atof( arg.substr(13) ) * FEET_TO_METER;
804         } else {
805             clouds_asl = atof( arg.substr(13) );
806         }
807     } else if ( arg.find( "--fov=" ) != string::npos ) {
808         parse_fov( arg.substr(6) );
809     } else if ( arg == "--disable-fullscreen" ) {
810         fullscreen = false;     
811     } else if ( arg== "--enable-fullscreen") {
812         fullscreen = true;      
813     } else if ( arg == "--shading-flat") {
814         shading = 0;    
815     } else if ( arg == "--shading-smooth") {
816         shading = 1;    
817     } else if ( arg == "--disable-skyblend") {
818         skyblend = false;       
819     } else if ( arg== "--enable-skyblend" ) {
820         skyblend = true;        
821     } else if ( arg == "--disable-textures" ) {
822         textures = false;       
823     } else if ( arg == "--enable-textures" ) {
824         textures = true;
825     } else if ( arg == "--disable-wireframe" ) {
826         wireframe = false;      
827     } else if ( arg == "--enable-wireframe" ) {
828         wireframe = true;
829     } else if ( arg.find( "--geometry=" ) != string::npos ) {
830         bool geometry_ok = true;
831         string geometry = arg.substr( 11 );
832         string::size_type i = geometry.find('x');
833
834         if (i != string::npos) {
835             xsize = atoi(geometry.substr(0, i));
836             ysize = atoi(geometry.substr(i+1));
837             // cout << "Geometry is " << xsize << 'x' << ysize << '\n';
838         } else {
839             geometry_ok = false;
840         }
841
842         if ( xsize <= 0 || ysize <= 0 ) {
843             xsize = 640;
844             ysize = 480;
845             geometry_ok = false;
846         }
847
848         if ( !geometry_ok ) {
849             FG_LOG( FG_GENERAL, FG_ALERT, "Unknown geometry: " << geometry );
850             FG_LOG( FG_GENERAL, FG_ALERT,
851                     "Setting geometry to " << xsize << 'x' << ysize << '\n');
852         }
853     } else if ( arg.find( "--bpp=" ) != string::npos ) {
854         string bits_per_pix = arg.substr( 6 );
855         if ( bits_per_pix == "16" ) {
856             bpp = 16;
857         } else if ( bits_per_pix == "24" ) {
858             bpp = 24;
859         } else if ( bits_per_pix == "32" ) {
860             bpp = 32;
861         }
862     } else if ( arg == "--units-feet" ) {
863         units = FG_UNITS_FEET;  
864     } else if ( arg == "--units-meters" ) {
865         units = FG_UNITS_METERS;        
866     } else if ( arg.find( "--time-offset" ) != string::npos ) {
867         time_offset = parse_time_offset( (arg.substr(14)) );
868         //time_offset_type = FG_TIME_SYS_OFFSET;
869     } else if ( arg.find( "--time-match-real") != string::npos ) {
870       //time_offset = parse_time_offset(arg.substr(18));
871         time_offset_type = FG_TIME_SYS_OFFSET;
872     } else if ( arg.find( "--time-match-local") != string::npos ) {
873       //time_offset = parse_time_offset(arg.substr(18));
874         time_offset_type = FG_TIME_LAT_OFFSET;
875     } else if ( arg.find( "--start-date-sys=") != string::npos ) {
876         time_offset = parse_date( (arg.substr(17)) );
877         time_offset_type = FG_TIME_SYS_ABSOLUTE;
878     } else if ( arg.find( "--start-date-lat=") != string::npos ) {
879         time_offset = parse_date( (arg.substr(17)) );
880         time_offset_type = FG_TIME_LAT_ABSOLUTE;
881     } else if ( arg.find( "--start-date-gmt=") != string::npos ) {
882         time_offset = parse_date( (arg.substr(17)) );
883         time_offset_type = FG_TIME_GMT_ABSOLUTE;
884
885     } else if ( arg == "--hud-tris" ) {
886         tris_or_culled = 0;     
887     } else if ( arg == "--hud-culled" ) {
888         tris_or_culled = 1;
889     } else if ( arg.find( "--native=" ) != string::npos ) {
890         parse_channel( "native", arg.substr(9) );
891     } else if ( arg.find( "--garmin=" ) != string::npos ) {
892         parse_channel( "garmin", arg.substr(9) );
893     } else if ( arg.find( "--nmea=" ) != string::npos ) {
894         parse_channel( "nmea", arg.substr(7) );
895     } else if ( arg.find( "--props=" ) != string::npos ) {
896         parse_channel( "props", arg.substr(8) );
897     } else if ( arg.find( "--pve=" ) != string::npos ) {
898         parse_channel( "pve", arg.substr(6) );
899     } else if ( arg.find( "--ray=" ) != string::npos ) {
900         parse_channel( "ray", arg.substr(6) );
901     } else if ( arg.find( "--rul=" ) != string::npos ) {
902         parse_channel( "rul", arg.substr(6) );
903     } else if ( arg.find( "--joyclient=" ) != string::npos ) {
904         parse_channel( "joyclient", arg.substr(12) );
905 #ifdef FG_NETWORK_OLK
906     } else if ( arg == "--disable-network-olk" ) {
907         network_olk = false;    
908     } else if ( arg== "--enable-network-olk") {
909         network_olk = true;     
910     } else if ( arg == "--net-hud" ) {
911         net_hud_display = 1;    
912     } else if ( arg.find( "--net-id=") != string::npos ) {
913         net_id = arg.substr( 9 );
914 #endif
915     } else if ( arg.find( "--prop:" ) == 0 ) {
916         string assign = arg.substr(7);
917         int pos = assign.find('=');
918         if (pos == arg.npos || pos == 0) {
919             FG_LOG(FG_GENERAL, FG_ALERT, "Bad property assignment: " << arg);
920             return FG_OPTIONS_ERROR;
921         }
922         string name = assign.substr(0, pos);
923         string value = assign.substr(pos + 1);
924         globals->get_props()->setStringValue(name.c_str(), value);
925         FG_LOG(FG_GENERAL, FG_INFO, "Setting default value of property "
926                << name << " to \"" << value << '"');
927     // $$$ begin - added VS Renganathan, 14 Oct 2K
928     // for multi-window outside window imagery
929     } else if ( arg.find( "--view-offset=" ) != string::npos ) {
930         string woffset = arg.substr( 14 );
931         if ( woffset == "LEFT" ) {
932                default_view_offset = FG_PI * 0.25;
933         } else if ( woffset == "RIGHT" ) {
934             default_view_offset = FG_PI * 1.75;
935         } else if ( woffset == "CENTER" ) {
936             default_view_offset = 0.00;
937         } else {
938             default_view_offset = atof( woffset.c_str() ) * DEG_TO_RAD;
939         }
940         FGViewerRPH *pilot_view =
941             (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
942         pilot_view->set_view_offset( default_view_offset );
943         pilot_view->set_goal_view_offset( default_view_offset );
944     // $$$ end - added VS Renganathan, 14 Oct 2K
945     } else if ( arg.find( "--visibility=" ) != string::npos ) {
946         visibility = atof( arg.substr( 13 ) );
947         globals->get_props()->setDoubleValue("/environment/visibility",
948                                              visibility);
949     } else if ( arg.find( "--visibility-miles=" ) != string::npos ) {
950         visibility = atof( arg.substr( 19 ) ) * 5280.0 * FEET_TO_METER;
951         globals->get_props()->setDoubleValue("/environment/visibility",
952                                              visibility);
953     } else if ( arg.find( "--wind=" ) == 0 ) {
954         string val = arg.substr(7);
955         int pos = val.find('@');
956         if (pos == string::npos) {
957           FG_LOG(FG_GENERAL, FG_ALERT, "bad wind value " << val);
958           return FG_OPTIONS_ERROR;
959         }
960         double dir = atof(val.substr(0,pos).c_str());
961         double speed = atof(val.substr(pos+1).c_str());
962         FG_LOG(FG_GENERAL, FG_INFO, "WIND: " << dir << '@' << 
963                speed << " knots" << endl);
964                                 // convert to fps
965         speed *= NM_TO_METER * METER_TO_FEET * (1.0/3600);
966         dir += 180;
967         if (dir >= 360)
968           dir -= 360;
969         dir *= DEG_TO_RAD;
970         globals->get_props()->setDoubleValue("/environment/wind-north",
971                                              speed * cos(dir));
972         globals->get_props()->setDoubleValue("/environment/wind-east",
973                                              speed * sin(dir));
974     } else if ( arg.find( "--wp=" ) != string::npos ) {
975         parse_wp( arg.substr( 5 ) );
976     } else if ( arg.find( "--flight-plan=") != string::npos) {
977         parse_flightplan ( arg.substr (14) );
978     } else {
979         FG_LOG( FG_GENERAL, FG_ALERT, "Unknown option '" << arg << "'" );
980         return FG_OPTIONS_ERROR;
981     }
982     
983     return FG_OPTIONS_OK;
984 }
985
986
987 // Scan the command line options for an fg_root definition and set
988 // just that.
989 int FGOptions::scan_command_line_for_root( int argc, char **argv ) {
990     int i = 1;
991
992     FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
993
994     while ( i < argc ) {
995         FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
996
997         string arg = argv[i];
998         if ( arg.find( "--fg-root=" ) != string::npos ) {
999             fg_root = arg.substr( 10 );
1000         }
1001
1002         i++;
1003     }
1004     
1005     return FG_OPTIONS_OK;
1006 }
1007
1008
1009 // Scan the config file for an fg_root definition and set just that.
1010 int FGOptions::scan_config_file_for_root( const string& path ) {
1011     fg_gzifstream in( path );
1012     if ( !in.is_open() )
1013         return(FG_OPTIONS_ERROR);
1014
1015     FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
1016
1017     in >> skipcomment;
1018 #ifndef __MWERKS__
1019     while ( ! in.eof() ) {
1020 #else
1021     char c = '\0';
1022     while ( in.get(c) && c != '\0' ) {
1023         in.putback(c);
1024 #endif
1025         string line;
1026
1027 #ifdef GETLINE_NEEDS_TERMINATOR
1028         getline( in, line, '\n' );
1029 #elif defined( macintosh )
1030         getline( in, line, '\r' );
1031 #else
1032         getline( in, line );
1033 #endif
1034
1035         if ( line.find( "--fg-root=" ) != string::npos ) {
1036             fg_root = line.substr( 10 );
1037         }
1038
1039         in >> skipcomment;
1040     }
1041
1042     return FG_OPTIONS_OK;
1043 }
1044
1045
1046 // Parse the command line options
1047 int FGOptions::parse_command_line( int argc, char **argv ) {
1048     int i = 1;
1049     int result;
1050
1051     FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
1052
1053     while ( i < argc ) {
1054         FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
1055
1056         result = parse_option(argv[i]);
1057         if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
1058             return(result);
1059         }
1060
1061         i++;
1062     }
1063     
1064     return FG_OPTIONS_OK;
1065 }
1066
1067
1068 // Parse config file options
1069 int FGOptions::parse_config_file( const string& path ) {
1070     fg_gzifstream in( path );
1071     if ( !in.is_open() )
1072         return(FG_OPTIONS_ERROR);
1073
1074     FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
1075
1076     in >> skipcomment;
1077 #ifndef __MWERKS__
1078     while ( ! in.eof() ) {
1079 #else
1080     char c = '\0';
1081     while ( in.get(c) && c != '\0' ) {
1082         in.putback(c);
1083 #endif
1084         string line;
1085
1086 #ifdef GETLINE_NEEDS_TERMINATOR
1087         getline( in, line, '\n' );
1088 #elif defined( macintosh )
1089         getline( in, line, '\r' );
1090 #else
1091         getline( in, line );
1092 #endif
1093
1094         if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
1095             FG_LOG( FG_GENERAL, FG_ALERT, 
1096                     "Config file parse error: " << path << " '" 
1097                     << line << "'" );
1098             exit(-1);
1099         }
1100         in >> skipcomment;
1101     }
1102
1103     return FG_OPTIONS_OK;
1104 }
1105
1106
1107 // Print usage message
1108 void FGOptions::usage ( void ) {
1109     cout << "Usage: fg [ options ... ]" << endl;
1110     cout << endl;
1111
1112     cout << "General Options:" << endl;
1113     cout << "\t--help -h:  print usage" << endl;
1114     cout << "\t--fg-root=path:  specify the root path for all the data files"
1115          << endl;
1116     cout << "\t--fg-scenery=path:  specify the base path for all the scenery"
1117          << " data." << endl
1118          << "\t\tdefaults to $FG_ROOT/Scenery" << endl;
1119     cout << "\t--disable-game-mode:  disable full-screen game mode" << endl;
1120     cout << "\t--enable-game-mode:  enable full-screen game mode" << endl;
1121     cout << "\t--disable-splash-screen:  disable splash screen" << endl;
1122     cout << "\t--enable-splash-screen:  enable splash screen" << endl;
1123     cout << "\t--disable-intro-music:  disable introduction music" << endl;
1124     cout << "\t--enable-intro-music:  enable introduction music" << endl;
1125     cout << "\t--disable-mouse-pointer:  disable extra mouse pointer" << endl;
1126     cout << "\t--enable-mouse-pointer:  enable extra mouse pointer (i.e. for"
1127          << endl;
1128     cout << "\t\tfull screen voodoo/voodoo-II based cards.)" << endl;
1129     cout << "\t--disable-freeze:  start out in an running state" << endl;
1130     cout << "\t--enable-freeze:  start out in a frozen state" << endl;
1131     cout << "\t--control=mode:  primary control mode " 
1132          << "(joystick, keyboard, mouse)" << endl;
1133     cout << endl;
1134
1135     cout << "Features:" << endl;
1136     cout << "\t--disable-hud:  disable heads up display" << endl;
1137     cout << "\t--enable-hud:  enable heads up display" << endl;
1138     cout << "\t--disable-panel:  disable instrument panel" << endl;
1139     cout << "\t--enable-panel:  enable instrumetn panel" << endl;
1140     cout << "\t--disable-sound:  disable sound effects" << endl;
1141     cout << "\t--enable-sound:  enable sound effects" << endl;
1142     cout << "\t--disable-anti-alias-hud:  disable anti aliased hud" << endl;
1143     cout << "\t--enable-anti-alias-hud:  enable anti aliased hud" << endl;
1144     cout << endl;
1145  
1146     cout << "Flight Model:" << endl;
1147     cout << "\t--fdm=abcd:  selects the core flight model code." << endl;
1148     cout << "\t\tcan be one of jsb, larcsim, magic, external, balloon, or ada"
1149          << endl;
1150     cout << "\t--aircraft=abcd:  aircraft model to load" << endl;
1151     cout << "\t--model-hz=n:  run the FDM this rate (iterations per second)" 
1152          << endl;
1153     cout << "\t--speed=n:  run the FDM this much faster than real time" << endl;
1154     cout << "\t--notrim:  Do NOT attempt to trim the model when initializing JSBsim" << endl;
1155     cout << "\t--wind=DIR@SPEED: specify wind coming from DIR (degrees) at SPEED (knots)" << endl;
1156     cout << endl;
1157
1158     //(UIUC)
1159     cout <<"Aircraft model directory:" << endl;
1160     cout <<"\t--aircraft-dir=<path> path is relative to the path of the executable" << endl;
1161     cout << endl;
1162
1163     cout << "Initial Position and Orientation:" << endl;
1164     cout << "\t--airport-id=ABCD:  specify starting postion by airport id" 
1165          << endl;
1166     cout << "\t--lon=degrees:  starting longitude in degrees (west = -)" 
1167          << endl;
1168     cout << "\t--lat=degrees:  starting latitude in degrees (south = -)"
1169          << endl;
1170     cout << "\t--altitude=feet:  starting altitude in feet" << endl;
1171     cout << "\t\t(unless --units-meters specified" << endl;
1172     cout << "\t--heading=degrees:  heading (yaw) angle in degress (Psi)"
1173          << endl;
1174     cout << "\t--roll=degrees:  roll angle in degrees (Phi)" << endl;
1175     cout << "\t--pitch=degrees:  pitch angle in degrees (Theta)" << endl;
1176     cout << "\t--uBody=feet per second:  velocity along the body X axis"
1177          << endl;
1178     cout << "\t--vBody=feet per second:  velocity along the body Y axis"
1179          << endl;
1180     cout << "\t--wBody=feet per second:  velocity along the body Z axis"
1181          << endl;
1182     cout << "\t\t(unless --units-meters specified" << endl;
1183     cout << "\t--vc= initial airspeed in knots (--fdm=jsb only)" << endl;
1184     cout << "\t--mach= initial mach number (--fdm=jsb only)" << endl;
1185     cout << endl;
1186
1187     cout << "Rendering Options:" << endl;
1188     cout << "\t--fog-disable:  disable fog/haze" << endl;
1189     cout << "\t--fog-fastest:  enable fastest fog/haze" << endl;
1190     cout << "\t--fog-nicest:  enable nicest fog/haze" << endl;
1191     cout << "\t--enable-clouds:  enable demo cloud layer" << endl;
1192     cout << "\t--disable-clouds:  disable demo cloud layer" << endl;
1193     cout << "\t--clouds-asl=xxx:  specify altitude of cloud layer above sea level" << endl;
1194     cout << "\t--fov=xx.x:  specify initial field of view angle in degrees"
1195          << endl;
1196     cout << "\t--disable-fullscreen:  disable fullscreen mode" << endl;
1197     cout << "\t--enable-fullscreen:  enable fullscreen mode" << endl;
1198     cout << "\t--shading-flat:  enable flat shading" << endl;
1199     cout << "\t--shading-smooth:  enable smooth shading" << endl;
1200     cout << "\t--disable-skyblend:  disable sky blending" << endl;
1201     cout << "\t--enable-skyblend:  enable sky blending" << endl;
1202     cout << "\t--disable-textures:  disable textures" << endl;
1203     cout << "\t--enable-textures:  enable textures" << endl;
1204     cout << "\t--disable-wireframe:  disable wireframe drawing mode" << endl;
1205     cout << "\t--enable-wireframe:  enable wireframe drawing mode" << endl;
1206     cout << "\t--geometry=WWWxHHH:  window geometry: 640x480, 800x600, etc."
1207          << endl;
1208     cout << "\t--view-offset=xxx:  set the default forward view direction"
1209          << endl;
1210     cout << "\t\tas an offset from straight ahead.  Allowable values are"
1211          << endl;
1212     cout << "\t\tLEFT, RIGHT, CENTER, or a specific number of degrees" << endl;
1213     cout << "\t--visibility=xxx:  specify initial visibility in meters" << endl;
1214     cout << "\t--visibility-miles=xxx:  specify initial visibility in miles"
1215          << endl;
1216     cout << endl;
1217
1218     cout << "Scenery Options:" << endl;
1219     cout << "\t--tile-radius=n:  specify tile radius, must be 1 - 4" << endl;
1220     cout << endl;
1221
1222     cout << "Hud Options:" << endl;
1223     cout << "\t--units-feet:  Hud displays units in feet" << endl;
1224     cout << "\t--units-meters:  Hud displays units in meters" << endl;
1225     cout << "\t--hud-tris:  Hud displays number of triangles rendered" << endl;
1226     cout << "\t--hud-culled:  Hud displays percentage of triangles culled"
1227          << endl;
1228     cout << endl;
1229         
1230     cout << "Time Options:" << endl;
1231     cout << "\t--time-offset=[+-]hh:mm:ss: add this time offset" << endl;
1232     cout << "\t--time-match-real: Synchronize real-world and FlightGear" << endl
1233          << "\t\ttime. Can be used in combination with --time-offset." << endl;
1234     cout << "\t--time-match-local:Synchronize local real-world and " << endl
1235          << "\t\tFlightGear time" << endl;   
1236     cout << "\t--start-date-sys=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1237          << "\t\tdate/time. Uses your system time " << endl;
1238     cout << "\t--start-date-gmt=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1239          << "\t\tdate/time. Uses Greenwich Mean Time" << endl;
1240     cout << "\t--start-date-lat=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1241          << "\t\tdate/time. Uses Local Aircraft Time" << endl;
1242 #ifdef FG_NETWORK_OLK
1243     cout << endl;
1244
1245     cout << "Network Options:" << endl;
1246     cout << "\t--enable-network-olk:  enable Multipilot mode" << endl;
1247     cout << "\t--disable-network-olk:  disable Multipilot mode (default)" << endl;
1248     cout << "\t--net-hud:  Hud displays network info" << endl;
1249     cout << "\t--net-id=name:  specify your own callsign" << endl;
1250 #endif
1251
1252     cout << endl;
1253     cout << "Route/Way Point Options:" << endl;
1254     cout << "\t--wp=ID[@alt]:  specify a waypoint for the GC autopilot" << endl;
1255     cout << "\t\tYou can specify multiple waypoints (a route) with multiple"
1256          << endl;
1257     cout << "\t\tinstances of --wp=" << endl;
1258     cout << "\t--flight-plan=[file]: Read all waypoints from [file]" <<endl;
1259 }
1260
1261
1262 // Destructor
1263 FGOptions::~FGOptions( void ) {
1264 }