]> git.mxchange.org Git - flightgear.git/blob - src/Main/options.cxx
669e1640ef21a18a0fd2a3018390012f340cd8f2
[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 #include <simgear/compiler.h>
29
30 /* normans fix */
31 #if defined(FX) && defined(XMESA)
32 bool global_fullscreen = true;
33 #endif
34
35 #include <math.h>            // rint()
36 #include <stdio.h>
37 #include <stdlib.h>          // atof(), atoi()
38 #include <string.h>
39
40 #include STL_STRING
41
42 #include <simgear/misc/fgstream.hxx>
43
44 // #include <Include/general.hxx>
45 // #include <Airports/simple.hxx>
46 // #include <Cockpit/cockpit.hxx>
47 // #include <FDM/flight.hxx>
48 // #include <FDM/UIUCModel/uiuc_aircraftdir.h>
49 #ifdef FG_NETWORK_OLK
50 #  include <NetworkOLK/network.h>
51 #endif
52
53 #include "globals.hxx"
54 #include "fg_init.hxx"
55 #include "fg_props.hxx"
56 #include "options.hxx"
57
58 SG_USING_STD(string);
59 SG_USING_NAMESPACE(std);
60
61
62 #define NEW_DEFAULT_MODEL_HZ 120
63
64 enum
65 {
66     FG_OPTIONS_OK = 0,
67     FG_OPTIONS_HELP = 1,
68     FG_OPTIONS_ERROR = 2
69 };
70
71 static double
72 atof( const string& str )
73 {
74
75 #ifdef __MWERKS__ 
76     // -dw- if ::atof is called, then we get an infinite loop
77     return std::atof( str.c_str() );
78 #else
79     return ::atof( str.c_str() );
80 #endif
81 }
82
83 static int
84 atoi( const string& str )
85 {
86 #ifdef __MWERKS__ 
87     // -dw- if ::atoi is called, then we get an infinite loop
88     return std::atoi( str.c_str() );
89 #else
90     return ::atoi( str.c_str() );
91 #endif
92 }
93
94
95 /**
96  * Set a few fail-safe default property values.
97  *
98  * These should all be set in $FG_ROOT/preferences.xml, but just
99  * in case, we provide some initial sane values here. This method 
100  * should be invoked *before* reading any init files.
101  */
102 void
103 fgSetDefaults ()
104 {
105     // set a possibly independent location for scenery data
106     char *envp = ::getenv( "FG_SCENERY" );
107
108     if ( envp != NULL ) {
109         // fg_root could be anywhere, so default to environmental
110         // variable $FG_ROOT if it is set.
111         globals->set_fg_scenery(envp);
112     } else {
113         // Otherwise, default to Scenery being in $FG_ROOT/Scenery
114         globals->set_fg_scenery("");
115     }
116                                 // Position (Globe, AZ)
117     fgSetDouble("/position/longitude", -110.6642444);
118     fgSetDouble("/position/latitude", 33.3528917);
119     fgSetDouble("/position/altitude", -9999.0);
120
121                                 // Orientation
122     fgSetDouble("/orientation/heading", 270);
123     fgSetDouble("/orientation/roll", 0);
124     fgSetDouble("/orientation/pitch", 0.424);
125
126                                 // Velocities
127     fgSetString("/sim/startup/speed-set", "knots");
128     fgSetDouble("/velocities/uBody", 0.0);
129     fgSetDouble("/velocities/vBody", 0.0);
130     fgSetDouble("/velocities/wBody", 0.0);
131     fgSetDouble("/velocities/speed-north", 0.0);
132     fgSetDouble("/velocities/speed-east", 0.0);
133     fgSetDouble("/velocities/speed-down", 0.0);
134     fgSetDouble("/velocities/airspeed", 0.0);
135     fgSetDouble("/velocities/mach", 0.0);
136
137                                 // Miscellaneous
138     fgSetBool("/sim/startup/game-mode", false);
139     fgSetBool("/sim/startup/splash-screen", true);
140     fgSetBool("/sim/startup/intro-music", true);
141     // we want mouse-pointer to have an undefined value if nothing is
142     // specified so we can do the right thing for voodoo-1/2 cards.
143     // fgSetString("/sim/startup/mouse-pointer", "disabled");
144     fgSetString("/sim/control-mode", "joystick");
145     fgSetBool("/sim/auto-coordination", false);
146
147                                 // Features
148     fgSetBool("/sim/hud/visibility", false);
149     fgSetBool("/sim/panel/visibility", true);
150     fgSetBool("/sim/sound", true);
151     fgSetBool("/sim/hud/antialiased", false);
152
153                                 // Flight Model options
154     fgSetString("/sim/flight-model", "larcsim");
155     fgSetString("/sim/aircraft", "c172");
156     fgSetInt("/sim/model-hz", NEW_DEFAULT_MODEL_HZ);
157     fgSetInt("/sim/speed-up", 1);
158     fgSetBool("/sim/startup/trim", false);
159     fgSetBool("/sim/startup/onground", true);
160
161                                 // Rendering options
162     fgSetString("/sim/rendering/fog", "nicest");
163     fgSetBool("/environment/clouds/status", true);
164     fgSetDouble("/environment/clouds/altitude", 5000);
165     fgSetBool("/sim/startup/fullscreen", false);
166     fgSetBool("/sim/rendering/shading", true);
167     fgSetBool("/sim/rendering/skyblend", true);
168     fgSetBool("/sim/rendering/textures", true);
169     fgSetBool("/sim/rendering/wireframe", false);
170     fgSetInt("/sim/startup/xsize", 800);
171     fgSetInt("/sim/startup/ysize", 600);
172     fgSetInt("/sim/rendering/bits-per-pixel", 16);
173     fgSetString("/sim/view-mode", "pilot");
174     fgSetDouble("/sim/startup/view-offset", 0);
175     fgSetDouble("/environment/visibility", 20000);
176
177                                 // HUD options
178     fgSetString("/sim/startup/units", "feet");
179     fgSetString("/sim/hud/frame-stat-type", "tris");
180         
181                                 // Time options
182     fgSetInt("/sim/startup/time-offset", 0);
183     fgSetString("/sim/startup/time-offset-type", "system-offset");
184
185     fgSetBool("/sim/networking/network-olk", false);
186     fgSetString("/sim/networking/call-sign", "Johnny");
187 }
188
189
190 // parse a time string ([+/-]%f[:%f[:%f]]) into hours
191 static double
192 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 a date string (yyyy:mm:dd:hh:mm:ss) into a time_t (seconds)
271 static long int 
272 parse_date( const string& date)
273 {
274     struct tm gmt;
275     char * date_str, num[256];
276     int i;
277     // initialize to zero
278     gmt.tm_sec = 0;
279     gmt.tm_min = 0;
280     gmt.tm_hour = 0;
281     gmt.tm_mday = 0;
282     gmt.tm_mon = 0;
283     gmt.tm_year = 0;
284     gmt.tm_isdst = 0; // ignore daylight savings time for the moment
285     date_str = (char *)date.c_str();
286     // get year
287     if ( strlen(date_str) ) {
288         i = 0;
289         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
290             num[i] = date_str[0];
291             date_str++;
292             i++;
293         }
294         if ( date_str[0] == ':' ) {
295             date_str++;
296         }
297         num[i] = '\0';
298         gmt.tm_year = atoi(num) - 1900;
299     }
300     // get month
301     if ( strlen(date_str) ) {
302         i = 0;
303         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
304             num[i] = date_str[0];
305             date_str++;
306             i++;
307         }
308         if ( date_str[0] == ':' ) {
309             date_str++;
310         }
311         num[i] = '\0';
312         gmt.tm_mon = atoi(num) -1;
313     }
314     // get day
315     if ( strlen(date_str) ) {
316         i = 0;
317         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
318             num[i] = date_str[0];
319             date_str++;
320             i++;
321         }
322         if ( date_str[0] == ':' ) {
323             date_str++;
324         }
325         num[i] = '\0';
326         gmt.tm_mday = atoi(num);
327     }
328     // get hour
329     if ( strlen(date_str) ) {
330         i = 0;
331         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
332             num[i] = date_str[0];
333             date_str++;
334             i++;
335         }
336         if ( date_str[0] == ':' ) {
337             date_str++;
338         }
339         num[i] = '\0';
340         gmt.tm_hour = atoi(num);
341     }
342     // get minute
343     if ( strlen(date_str) ) {
344         i = 0;
345         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
346             num[i] = date_str[0];
347             date_str++;
348             i++;
349         }
350         if ( date_str[0] == ':' ) {
351             date_str++;
352         }
353         num[i] = '\0';
354         gmt.tm_min = atoi(num);
355     }
356     // get second
357     if ( strlen(date_str) ) {
358         i = 0;
359         while ( (date_str[0] != ':') && (date_str[0] != '\0') ) {
360             num[i] = date_str[0];
361             date_str++;
362             i++;
363         }
364         if ( date_str[0] == ':' ) {
365             date_str++;
366         }
367         num[i] = '\0';
368         gmt.tm_sec = atoi(num);
369     }
370     time_t theTime = sgTimeGetGMT( gmt.tm_year, gmt.tm_mon, gmt.tm_mday,
371                                    gmt.tm_hour, gmt.tm_min, gmt.tm_sec );
372     //printf ("Date is %s\n", ctime(&theTime));
373     //printf ("in seconds that is %d\n", theTime);
374     //exit(1);
375     return (theTime);
376 }
377
378
379 // parse angle in the form of [+/-]ddd:mm:ss into degrees
380 static double
381 parse_degree( const string& degree_str) {
382     double result = parse_time( degree_str );
383
384     // printf("Degree = %.4f\n", result);
385
386     return(result);
387 }
388
389
390 // parse time offset string into seconds
391 static int
392 parse_time_offset( const string& time_str) {
393     int result;
394
395     // printf("time offset = %s\n", time_str);
396
397 #ifdef HAVE_RINT
398     result = (int)rint(parse_time(time_str) * 3600.0);
399 #else
400     result = (int)(parse_time(time_str) * 3600.0);
401 #endif
402
403     // printf("parse_time_offset(): %d\n", result);
404
405     return( result );
406 }
407
408
409 // Parse --fov=x.xx type option 
410 static double
411 parse_fov( const string& arg ) {
412     double fov = atof(arg);
413
414     if ( fov < FG_FOV_MIN ) { fov = FG_FOV_MIN; }
415     if ( fov > FG_FOV_MAX ) { fov = FG_FOV_MAX; }
416
417     fgSetDouble("/sim/field-of-view", fov);
418
419     // printf("parse_fov(): result = %.4f\n", fov);
420
421     return fov;
422 }
423
424
425 // Parse I/O channel option
426 //
427 // Format is "--protocol=medium,direction,hz,medium_options,..."
428 //
429 //   protocol = { native, nmea, garmin, fgfs, rul, pve, etc. }
430 //   medium = { serial, socket, file, etc. }
431 //   direction = { in, out, bi }
432 //   hz = number of times to process channel per second (floating
433 //        point values are ok.
434 //
435 // Serial example "--nmea=serial,dir,hz,device,baud" where
436 // 
437 //  device = OS device name of serial line to be open()'ed
438 //  baud = {300, 1200, 2400, ..., 230400}
439 //
440 // Socket exacmple "--native=socket,dir,hz,machine,port,style" where
441 // 
442 //  machine = machine name or ip address if client (leave empty if server)
443 //  port = port, leave empty to let system choose
444 //  style = tcp or udp
445 //
446 // File example "--garmin=file,dir,hz,filename" where
447 // 
448 //  filename = file system file name
449
450 static bool 
451 parse_channel( const string& type, const string& channel_str ) {
452     // cout << "Channel string = " << channel_str << endl;
453
454     globals->get_channel_options_list()->push_back( type + "," + channel_str );
455
456     return true;
457 }
458
459
460 // Parse --wp=ID[@alt]
461 static bool 
462 parse_wp( const string& arg ) {
463     string id, alt_str;
464     double alt = 0.0;
465
466     int pos = arg.find( "@" );
467     if ( pos != string::npos ) {
468         id = arg.substr( 0, pos );
469         alt_str = arg.substr( pos + 1 );
470         // cout << "id str = " << id << "  alt str = " << alt_str << endl;
471         alt = atof( alt_str.c_str() );
472         if ( fgGetString("/sim/startup/units") == "feet" ) {
473             alt *= FEET_TO_METER;
474         }
475     } else {
476         id = arg;
477     }
478
479     FGAirport a;
480     if ( fgFindAirportID( id, &a ) ) {
481         SGWayPoint wp( a.longitude, a.latitude, alt, SGWayPoint::WGS84, id );
482         globals->get_route()->add_waypoint( wp );
483
484         return true;
485     } else {
486         return false;
487     }
488 }
489
490
491 // Parse --flight-plan=[file]
492 static bool 
493 parse_flightplan(const string& arg)
494 {
495     fg_gzifstream infile(arg.c_str());
496     if ( !infile.is_open() ) {
497         return false;
498     }
499     while ( true ) {
500         string line;
501 #ifdef GETLINE_NEEDS_TERMINATOR
502         getline( infile, line, '\n' );
503 #elif defined( macintosh )
504         getline( infile, line, '\r' );
505 #else
506         getline( infile, line );
507 #endif
508         if ( infile.eof() ) {
509             break;
510         }
511         parse_wp(line);
512     }
513
514     return true;
515 }
516
517
518 // Parse a single option
519 static int 
520 parse_option (const string& arg) 
521 {
522     // General Options
523     if ( (arg == "--help") || (arg == "-h") ) {
524         // help/usage request
525         return(FG_OPTIONS_HELP);
526     } else if ( arg == "--disable-game-mode") {
527         fgSetBool("/sim/startup/game-mode", false);
528     } else if ( arg == "--enable-game-mode" ) {
529         fgSetBool("/sim/startup/game-mode", true);
530     } else if ( arg == "--disable-splash-screen" ) {
531         fgSetBool("/sim/startup/splash-screen", false); 
532     } else if ( arg == "--enable-splash-screen" ) {
533         fgSetBool("/sim/startup/splash-screen", true);
534     } else if ( arg == "--disable-intro-music" ) {
535         fgSetBool("/sim/startup/intro-music", false);
536     } else if ( arg == "--enable-intro-music" ) {
537         fgSetBool("/sim/startup/intro-music", true);
538     } else if ( arg == "--disable-mouse-pointer" ) {
539         fgSetString("/sim/startup/mouse-pointer", "disabled");
540     } else if ( arg == "--enable-mouse-pointer" ) {
541         fgSetString("/sim/startup/mouse-pointer", "enabled");
542     } else if ( arg == "--disable-freeze" ) {
543         globals->set_freeze(false);
544     } else if ( arg == "--enable-freeze" ) {
545         globals->set_freeze(true);
546     } else if ( arg == "--disable-anti-alias-hud" ) {
547         fgSetBool("/sim/hud/antialiased", false);
548     } else if ( arg == "--enable-anti-alias-hud" ) {
549         fgSetBool("/sim/hud/antialiased", true);
550     } else if ( arg.find( "--control=") == 0 ) {
551         fgSetString("/sim/control-mode", arg.substr(10));
552     } else if ( arg == "--disable-auto-coordination" ) {
553         fgSetBool("/sim/auto-coordination", false);
554     } else if ( arg == "--enable-auto-coordination" ) {
555         fgSetBool("/sim/auto-coordination", true);
556     } else if ( arg == "--disable-hud" ) {
557         fgSetBool("/sim/hud/visibility", false);
558     } else if ( arg == "--enable-hud" ) {
559         fgSetBool("/sim/hud/visibility", true);
560     } else if ( arg == "--disable-panel" ) {
561         fgSetBool("/sim/panel/visibility", false);
562     } else if ( arg == "--enable-panel" ) {
563         fgSetBool("/sim/panel/visibility", true);
564     } else if ( arg == "--disable-sound" ) {
565         fgSetBool("/sim/sound", false);
566     } else if ( arg == "--enable-sound" ) {
567         fgSetBool("/sim/sound", true);
568     } else if ( arg.find( "--airport-id=") == 0 ) {
569                                 // NB: changed property name!!!
570         fgSetString("/sim/startup/airport-id", arg.substr(13));
571     } else if ( arg.find( "--offset-distance=") == 0 ) {
572         fgSetDouble("/sim/startup/offset-distance", atof(arg.substr(18)));
573     } else if ( arg.find( "--offset-azimuth=") == 0 ) {
574         fgSetDouble("/sim/startup/offset-azimuth", atof(arg.substr(17))); 
575     } else if ( arg.find( "--lon=" ) == 0 ) {
576         fgSetDouble("/position/longitude",
577                               parse_degree(arg.substr(6)));
578         fgSetString("/sim/startup/airport-id", "");
579     } else if ( arg.find( "--lat=" ) == 0 ) {
580         fgSetDouble("/position/latitude",
581                               parse_degree(arg.substr(6)));
582         fgSetString("/sim/startup/airport-id", "");
583     } else if ( arg.find( "--altitude=" ) == 0 ) {
584         fgSetBool("/sim/startup/onground", false);
585         if ( fgGetString("/sim/startup/units") == "feet" )
586             fgSetDouble("/position/altitude", atof(arg.substr(11)));
587         else
588             fgSetDouble("/position/altitude",
589                         atof(arg.substr(11)) * METER_TO_FEET);
590     } else if ( arg.find( "--uBody=" ) == 0 ) {
591         fgSetString("/sim/startup/speed-set", "UVW");
592         if ( fgGetString("/sim/startup/units") == "feet" )
593           fgSetDouble("/velocities/uBody", atof(arg.substr(8)));
594         else
595           fgSetDouble("/velocities/uBody",
596                                atof(arg.substr(8)) * METER_TO_FEET);
597     } else if ( arg.find( "--vBody=" ) == 0 ) {
598         fgSetString("/sim/startup/speed-set", "UVW");
599         if ( fgGetString("/sim/startup/units") == "feet" )
600           fgSetDouble("/velocities/vBody", atof(arg.substr(8)));
601         else
602           fgSetDouble("/velocities/vBody",
603                                atof(arg.substr(8)) * METER_TO_FEET);
604     } else if ( arg.find( "--wBody=" ) == 0 ) {
605         fgSetString("/sim/startup/speed-set", "UVW");
606         if ( fgGetString("/sim/startup/units") == "feet" )
607           fgSetDouble("/velocities/wBody", atof(arg.substr(8)));
608         else
609           fgSetDouble("/velocities/wBody",
610                                atof(arg.substr(8)) * METER_TO_FEET);
611     } else if ( arg.find( "--vNorth=" ) == 0 ) {
612         fgSetString("/sim/startup/speed-set", "NED");
613         if ( fgGetString("/sim/startup/units") == "feet" )
614           fgSetDouble("/velocities/speed-north", atof(arg.substr(9)));
615         else
616           fgSetDouble("/velocities/speed-north",
617                                atof(arg.substr(9)) * METER_TO_FEET);
618     } else if ( arg.find( "--vEast=" ) == 0 ) {
619         fgSetString("/sim/startup/speed-set", "NED");
620         if ( fgGetString("/sim/startup/units") == "feet" )
621           fgSetDouble("/velocities/speed-east", atof(arg.substr(8)));
622         else
623           fgSetDouble("/velocities/speed-east",
624                                atof(arg.substr(8)) * METER_TO_FEET);
625     } else if ( arg.find( "--vDown=" ) == 0 ) {
626         fgSetString("/sim/startup/speed-set", "NED");
627         if ( fgGetString("/sim/startup/units") == "feet" )
628           fgSetDouble("/velocities/speed-down", atof(arg.substr(8)));
629         else
630           fgSetDouble("/velocities/speed-down",
631                                atof(arg.substr(8)) * METER_TO_FEET);
632     } else if ( arg.find( "--vc=" ) == 0) {
633         fgSetString("/sim/startup/speed-set", "knots");
634         fgSetDouble("/velocities/airspeed", atof(arg.substr(5)));
635     } else if ( arg.find( "--mach=" ) == 0) {
636         fgSetString("/sim/startup/speed-set", "mach");
637         fgSetDouble("/velocities/mach", atof(arg.substr(7)));
638     } else if ( arg.find( "--heading=" ) == 0 ) {
639         fgSetDouble("/orientation/heading", atof(arg.substr(10)));
640     } else if ( arg.find( "--roll=" ) == 0 ) {
641         fgSetDouble("/orientation/roll", atof(arg.substr(7)));
642     } else if ( arg.find( "--pitch=" ) == 0 ) {
643         fgSetDouble("/orientation/pitch", atof(arg.substr(8)));
644     } else if ( arg.find( "--fg-root=" ) == 0 ) {
645         globals->set_fg_root(arg.substr( 10 ));
646     } else if ( arg.find( "--fg-scenery=" ) == 0 ) {
647         globals->set_fg_scenery(arg.substr( 13 ));
648     } else if ( arg.find( "--fdm=" ) == 0 ) {
649         fgSetString("/sim/flight-model", arg.substr(6));
650     } else if ( arg.find( "--aircraft=" ) == 0 ) {
651         fgSetString("/sim/aircraft", arg.substr(11));
652     } else if ( arg.find( "--aircraft-dir=" ) == 0 ) {
653         fgSetString("/sim/aircraft-dir", arg.substr(15));
654     } else if ( arg.find( "--model-hz=" ) == 0 ) {
655         fgSetInt("/sim/model-hz", atoi(arg.substr(11)));
656     } else if ( arg.find( "--speed=" ) == 0 ) {
657         fgSetInt("/sim/speed-up", atoi(arg.substr(8)));
658     } else if ( arg.find( "--trim") == 0) {
659         fgSetBool("/sim/startup/trim", true);
660     } else if ( arg.find( "--notrim") == 0) {
661         fgSetBool("/sim/startup/trim", false);
662     } else if ( arg.find( "--on-ground") == 0) {
663         fgSetBool("/sim/startup/onground", true);
664     } else if ( arg.find( "--in-air") == 0) {
665         fgSetBool("/sim/startup/onground", false);
666     } else if ( arg == "--fog-disable" ) {
667         fgSetString("/sim/rendering/fog", "disabled");
668     } else if ( arg == "--fog-fastest" ) {
669         fgSetString("/sim/rendering/fog", "fastest");
670     } else if ( arg == "--fog-nicest" ) {
671         fgSetString("/sim/fog", "nicest");
672     } else if ( arg == "--disable-clouds" ) {
673         fgSetBool("/environment/clouds/status", false);
674     } else if ( arg == "--enable-clouds" ) {
675         fgSetBool("/environment/clouds/status", true);
676     } else if ( arg.find( "--clouds-asl=" ) == 0 ) {
677                                 // FIXME: check units
678         if ( fgGetString("/sim/startup/units") == "feet" )
679           fgSetDouble("/environment/clouds/altitude",
680                                 atof(arg.substr(13)) * FEET_TO_METER);
681         else
682           fgSetDouble("/environment/clouds/altitude",
683                                 atof(arg.substr(13)));
684     } else if ( arg.find( "--fov=" ) == 0 ) {
685         parse_fov( arg.substr(6) );
686     } else if ( arg == "--disable-fullscreen" ) {
687         fgSetBool("/sim/startup/fullscreen", false);
688     } else if ( arg== "--enable-fullscreen") {
689         fgSetBool("/sim/startup/fullscreen", true);
690     } else if ( arg == "--shading-flat") {
691         fgSetBool("/sim/rendering/shading", false);
692     } else if ( arg == "--shading-smooth") {
693         fgSetBool("/sim/rendering/shading", true);
694     } else if ( arg == "--disable-skyblend") {
695         fgSetBool("/sim/rendering/skyblend", false);
696     } else if ( arg== "--enable-skyblend" ) {
697         fgSetBool("/sim/rendering/skyblend", true);
698     } else if ( arg == "--disable-textures" ) {
699         fgSetBool("/sim/rendering/textures", false);
700     } else if ( arg == "--enable-textures" ) {
701         fgSetBool("/sim/rendering/textures", true);
702     } else if ( arg == "--disable-wireframe" ) {
703         fgSetBool("/sim/rendering/wireframe", false);
704     } else if ( arg == "--enable-wireframe" ) {
705         fgSetBool("/sim/rendering/wireframe", true);
706     } else if ( arg.find( "--geometry=" ) == 0 ) {
707         bool geometry_ok = true;
708         int xsize = 0, ysize = 0;
709         string geometry = arg.substr( 11 );
710         string::size_type i = geometry.find('x');
711
712         if (i != string::npos) {
713             xsize = atoi(geometry.substr(0, i));
714             ysize = atoi(geometry.substr(i+1));
715         } else {
716             geometry_ok = false;
717         }
718
719         if ( xsize <= 0 || ysize <= 0 ) {
720             xsize = 640;
721             ysize = 480;
722             geometry_ok = false;
723         }
724
725         if ( !geometry_ok ) {
726             FG_LOG( FG_GENERAL, FG_ALERT, "Unknown geometry: " << geometry );
727             FG_LOG( FG_GENERAL, FG_ALERT,
728                     "Setting geometry to " << xsize << 'x' << ysize << '\n');
729         } else {
730           FG_LOG( FG_GENERAL, FG_INFO,
731                   "Setting geometry to " << xsize << 'x' << ysize << '\n');
732           fgSetInt("/sim/startup/xsize", xsize);
733           fgSetInt("/sim/startup/ysize", ysize);
734         }
735     } else if ( arg.find( "--bpp=" ) == 0 ) {
736         string bits_per_pix = arg.substr( 6 );
737         if ( bits_per_pix == "16" ) {
738             fgSetInt("/sim/rendering/bits-per-pixel", 16);
739         } else if ( bits_per_pix == "24" ) {
740             fgSetInt("/sim/rendering/bits-per-pixel", 24);
741         } else if ( bits_per_pix == "32" ) {
742             fgSetInt("/sim/rendering/bits-per-pixel", 32);
743         } else {
744           FG_LOG(FG_GENERAL, FG_ALERT, "Unsupported bpp " << bits_per_pix);
745         }
746     } else if ( arg == "--units-feet" ) {
747         fgSetString("/sim/startup/units", "feet");
748     } else if ( arg == "--units-meters" ) {
749         fgSetString("/sim/startup/units", "meters");
750     } else if ( arg.find( "--time-offset" ) == 0 ) {
751         fgSetInt("/sim/startup/time-offset",
752                            parse_time_offset( (arg.substr(14)) ));
753     } else if ( arg.find( "--time-match-real") == 0 ) {
754         fgSetString("/sim/startup/time-offset-type",
755                               "system-offset");
756     } else if ( arg.find( "--time-match-local") == 0 ) {
757         fgSetString("/sim/startup/time-offset-type",
758                               "latitude-offset");
759     } else if ( arg.find( "--start-date-sys=") == 0 ) {
760         fgSetInt("/sim/startup/time-offset",
761                            parse_date((arg.substr(17))));
762         fgSetString("/sim/startup/time-offset-type", "system");
763     } else if ( arg.find( "--start-date-lat=") == 0 ) {
764         fgSetInt("/sim/startup/time-offset",
765                            parse_date((arg.substr(17))));
766         fgSetString("/sim/startup/time-offset-type",
767                            "latitude");
768     } else if ( arg.find( "--start-date-gmt=") == 0 ) {
769         fgSetInt("/sim/startup/time-offset",
770                            parse_date((arg.substr(17))));
771         fgSetString("/sim/startup/time-offset-type", "gmt");
772     } else if ( arg == "--hud-tris" ) {
773         fgSetString("/sim/hud/frame-stat-type", "tris");
774     } else if ( arg == "--hud-culled" ) {
775         fgSetString("/sim/hud/frame-stat-type", "culled");
776     } else if ( arg.find( "--atlas=" ) == 0 ) {
777         parse_channel( "atlas", arg.substr(8) );
778     } else if ( arg.find( "--native=" ) == 0 ) {
779         parse_channel( "native", arg.substr(9) );
780     } else if ( arg.find( "--garmin=" ) == 0 ) {
781         parse_channel( "garmin", arg.substr(9) );
782     } else if ( arg.find( "--nmea=" ) == 0 ) {
783         parse_channel( "nmea", arg.substr(7) );
784     } else if ( arg.find( "--props=" ) == 0 ) {
785         parse_channel( "props", arg.substr(8) );
786     } else if ( arg.find( "--pve=" ) == 0 ) {
787         parse_channel( "pve", arg.substr(6) );
788     } else if ( arg.find( "--ray=" ) == 0 ) {
789         parse_channel( "ray", arg.substr(6) );
790     } else if ( arg.find( "--rul=" ) == 0 ) {
791         parse_channel( "rul", arg.substr(6) );
792     } else if ( arg.find( "--joyclient=" ) == 0 ) {
793         parse_channel( "joyclient", arg.substr(12) );
794 #ifdef FG_NETWORK_OLK
795     } else if ( arg == "--disable-network-olk" ) {
796         fgSetBool("/sim/networking/olk", false);
797     } else if ( arg== "--enable-network-olk") {
798         fgSetBool("/sim/networking/olk", true);
799     } else if ( arg == "--net-hud" ) {
800         fgSetBool("/sim/hud/net-display", true);
801         net_hud_display = 1;    // FIXME
802     } else if ( arg.find( "--net-id=") == 0 ) {
803         fgSetString("sim/networking/call-sign", arg.substr(9));
804 #endif
805     } else if ( arg.find( "--prop:" ) == 0 ) {
806         string assign = arg.substr(7);
807         int pos = assign.find('=');
808         if (pos == arg.npos || pos == 0) {
809             FG_LOG(FG_GENERAL, FG_ALERT, "Bad property assignment: " << arg);
810             return FG_OPTIONS_ERROR;
811         }
812         string name = assign.substr(0, pos);
813         string value = assign.substr(pos + 1);
814         fgSetString(name.c_str(), value);
815         // FG_LOG(FG_GENERAL, FG_INFO, "Setting default value of property "
816         //        << name << " to \"" << value << '"');
817     // $$$ begin - added VS Renganathan, 14 Oct 2K
818     // for multi-window outside window imagery
819     } else if ( arg.find( "--view-offset=" ) == 0 ) {
820         string woffset = arg.substr( 14 );
821         double default_view_offset = 0.0;
822         if ( woffset == "LEFT" ) {
823                default_view_offset = SGD_PI * 0.25;
824         } else if ( woffset == "RIGHT" ) {
825             default_view_offset = SGD_PI * 1.75;
826         } else if ( woffset == "CENTER" ) {
827             default_view_offset = 0.00;
828         } else {
829             default_view_offset = atof( woffset.c_str() ) * SGD_DEGREES_TO_RADIANS;
830         }
831         FGViewerRPH *pilot_view =
832             (FGViewerRPH *)globals->get_viewmgr()->get_view( 0 );
833         pilot_view->set_view_offset( default_view_offset );
834         pilot_view->set_goal_view_offset( default_view_offset );
835         fgSetDouble("/sim/startup/view-offset", default_view_offset);
836     // $$$ end - added VS Renganathan, 14 Oct 2K
837     } else if ( arg.find( "--visibility=" ) == 0 ) {
838         fgSetDouble("/environment/visibility", atof(arg.substr(13)));
839     } else if ( arg.find( "--visibility-miles=" ) == 0 ) {
840         double visibility = atof(arg.substr(19)) * 5280.0 * FEET_TO_METER;
841         fgSetDouble("/environment/visibility", visibility);
842     } else if ( arg.find( "--wind=" ) == 0 ) {
843         string val = arg.substr(7);
844         int pos = val.find('@');
845         if (pos == string::npos) {
846           FG_LOG(FG_GENERAL, FG_ALERT, "bad wind value " << val);
847           return FG_OPTIONS_ERROR;
848         }
849         double dir = atof(val.substr(0,pos).c_str());
850         double speed = atof(val.substr(pos+1).c_str());
851         FG_LOG(FG_GENERAL, FG_INFO, "WIND: " << dir << '@' << 
852                speed << " knots" << endl);
853                                 // convert to fps
854         speed *= NM_TO_METER * METER_TO_FEET * (1.0/3600);
855         dir += 180;
856         if (dir >= 360)
857           dir -= 360;
858         dir *= SGD_DEGREES_TO_RADIANS;
859         fgSetDouble("/environment/wind-north",
860                                              speed * cos(dir));
861         fgSetDouble("/environment/wind-east",
862                                              speed * sin(dir));
863     } else if ( arg.find( "--wp=" ) == 0 ) {
864         parse_wp( arg.substr( 5 ) );
865     } else if ( arg.find( "--flight-plan=") == 0) {
866         parse_flightplan ( arg.substr (14) );
867     } else if ( arg.find( "--config=" ) == 0 ) {
868         string file = arg.substr(9);
869         if (!readProperties(file, globals->get_props())) {
870           FG_LOG(FG_IO, FG_ALERT,
871                  "--config: failed to read properties from " << file);
872           return FG_OPTIONS_ERROR;
873         }
874     } else {
875         FG_LOG( FG_GENERAL, FG_ALERT, "Unknown option '" << arg << "'" );
876         return FG_OPTIONS_ERROR;
877     }
878     
879     return FG_OPTIONS_OK;
880 }
881
882
883 // Scan the command line options for an fg_root definition and set
884 // just that.
885 string
886 fgScanForRoot (int argc, char **argv) 
887 {
888     int i = 1;
889
890     FG_LOG(FG_GENERAL, FG_INFO, "Scanning for root: command line");
891
892     while ( i < argc ) {
893         FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
894
895         string arg = argv[i];
896         if ( arg.find( "--fg-root=" ) == 0 ) {
897             return arg.substr( 10 );
898         }
899
900         i++;
901     }
902
903     return "";
904 }
905
906
907 // Scan the config file for an fg_root definition and set just that.
908 string
909 fgScanForRoot (const string& path)
910 {
911     fg_gzifstream in( path );
912     if ( !in.is_open() )
913       return "";
914
915     FG_LOG( FG_GENERAL, FG_INFO, "Scanning for root: " << path );
916
917     in >> skipcomment;
918 #ifndef __MWERKS__
919     while ( ! in.eof() ) {
920 #else
921     char c = '\0';
922     while ( in.get(c) && c != '\0' ) {
923         in.putback(c);
924 #endif
925         string line;
926
927 #ifdef GETLINE_NEEDS_TERMINATOR
928         getline( in, line, '\n' );
929 #elif defined( macintosh )
930         getline( in, line, '\r' );
931 #else
932         getline( in, line );
933 #endif
934
935         if ( line.find( "--fg-root=" ) == 0 ) {
936             return line.substr( 10 );
937         }
938
939         in >> skipcomment;
940     }
941
942     return "";
943 }
944
945
946 // Parse the command line options
947 void
948 fgParseOptions (int argc, char **argv) {
949     int i = 1;
950     int result;
951
952     FG_LOG(FG_GENERAL, FG_INFO, "Processing command line arguments");
953
954     while ( i < argc ) {
955         FG_LOG( FG_GENERAL, FG_DEBUG, "argv[" << i << "] = " << argv[i] );
956
957         result = parse_option(argv[i]);
958         if ( (result == FG_OPTIONS_HELP) || (result == FG_OPTIONS_ERROR) ) {
959             fgUsage();
960             exit(-1);
961         }
962
963         i++;
964     }
965 }
966
967
968 // Parse config file options
969 void
970 fgParseOptions (const string& path) {
971     fg_gzifstream in( path );
972     if ( !in.is_open() )
973       return;
974
975     FG_LOG( FG_GENERAL, FG_INFO, "Processing config file: " << path );
976
977     in >> skipcomment;
978 #ifndef __MWERKS__
979     while ( ! in.eof() ) {
980 #else
981     char c = '\0';
982     while ( in.get(c) && c != '\0' ) {
983         in.putback(c);
984 #endif
985         string line;
986
987 #ifdef GETLINE_NEEDS_TERMINATOR
988         getline( in, line, '\n' );
989 #elif defined( macintosh )
990         getline( in, line, '\r' );
991 #else
992         getline( in, line );
993 #endif
994
995         if ( parse_option( line ) == FG_OPTIONS_ERROR ) {
996             FG_LOG( FG_GENERAL, FG_ALERT, 
997                     "Config file parse error: " << path << " '" 
998                     << line << "'" );
999             fgUsage();
1000             exit(-1);
1001         }
1002         in >> skipcomment;
1003     }
1004 }
1005
1006
1007 // Print usage message
1008 void 
1009 fgUsage ()
1010 {
1011     cout << "Usage: fgfs [ options ... ]" << endl;
1012     cout << endl;
1013
1014     cout << "General Options:" << endl;
1015     cout << "\t--help -h:  print usage" << endl;
1016     cout << "\t--fg-root=path:  specify the root path for all the data files"
1017          << endl;
1018     cout << "\t--fg-scenery=path:  specify the base path for all the scenery"
1019          << " data." << endl
1020          << "\t\tdefaults to $FG_ROOT/Scenery" << endl;
1021     cout << "\t--disable-game-mode:  disable full-screen game mode" << endl;
1022     cout << "\t--enable-game-mode:  enable full-screen game mode" << endl;
1023     cout << "\t--disable-splash-screen:  disable splash screen" << endl;
1024     cout << "\t--enable-splash-screen:  enable splash screen" << endl;
1025     cout << "\t--disable-intro-music:  disable introduction music" << endl;
1026     cout << "\t--enable-intro-music:  enable introduction music" << endl;
1027     cout << "\t--disable-mouse-pointer:  disable extra mouse pointer" << endl;
1028     cout << "\t--enable-mouse-pointer:  enable extra mouse pointer (i.e. for"
1029          << endl;
1030     cout << "\t\tfull screen voodoo/voodoo-II based cards.)" << endl;
1031     cout << "\t--disable-freeze:  start out in an running state" << endl;
1032     cout << "\t--enable-freeze:  start out in a frozen state" << endl;
1033     cout << "\t--control=mode:  primary control mode " 
1034          << "(joystick, keyboard, mouse)" << endl;
1035     cout << "\t--prop:name=value:  set property <name> to <value>" << endl;
1036     cout << "\t--file=path:  load additional properties from path" << endl;
1037     cout << endl;
1038
1039     cout << "Features:" << endl;
1040     cout << "\t--disable-hud:  disable heads up display" << endl;
1041     cout << "\t--enable-hud:  enable heads up display" << endl;
1042     cout << "\t--disable-panel:  disable instrument panel" << endl;
1043     cout << "\t--enable-panel:  enable instrumetn panel" << endl;
1044     cout << "\t--disable-sound:  disable sound effects" << endl;
1045     cout << "\t--enable-sound:  enable sound effects" << endl;
1046     cout << "\t--disable-anti-alias-hud:  disable anti aliased hud" << endl;
1047     cout << "\t--enable-anti-alias-hud:  enable anti aliased hud" << endl;
1048     cout << endl;
1049  
1050     cout << "Flight Model:" << endl;
1051     cout << "\t--fdm=abcd:  selects the core flight model code." << endl;
1052     cout << "\t\tcan be one of jsb, larcsim, magic, external, balloon, or ada"
1053          << endl;
1054     cout << "\t--aircraft=abcd:  aircraft model to load" << endl;
1055     cout << "\t--model-hz=n:  run the FDM this rate (iterations per second)" 
1056          << endl;
1057     cout << "\t--speed=n:  run the FDM this much faster than real time" << endl;
1058     cout << "\t--notrim:  Do NOT attempt to trim the model when initializing JSBsim" << endl;
1059     cout << "\t--on-ground:  Start up at ground level (default)" << endl;
1060     cout << "\t--in-air:  Start up in air (implied by specifying an initial"
1061          << " altitude above ground level." << endl;
1062     cout << "\t--wind=DIR@SPEED: specify wind coming from DIR (degrees) at SPEED (knots)" << endl;
1063     cout << endl;
1064
1065     //(UIUC)
1066     cout <<"Aircraft model directory:" << endl;
1067     cout <<"\t--aircraft-dir=<path> path is relative to the path of the executable" << endl;
1068     cout << endl;
1069
1070     cout << "Initial Position and Orientation:" << endl;
1071     cout << "\t--airport-id=ABCD:  specify starting postion by airport id" 
1072          << endl;
1073     cout << "\t--offset-distance:  specify distance to threshhold"
1074          << " (NM)" << endl; 
1075     cout << "\t--offset-azimuth:  specify heading to threshhold (deg) " 
1076          << endl;    
1077     cout << "\t--lon=degrees:  starting longitude in degrees (west = -)" 
1078          << endl;
1079     cout << "\t--lat=degrees:  starting latitude in degrees (south = -)"
1080          << endl;
1081     cout << "\t--altitude=feet:  starting altitude in feet" << endl;
1082     cout << "\t\t(unless --units-meters specified" << endl;
1083     cout << "\t--heading=degrees:  heading (yaw) angle in degress (Psi)"
1084          << endl;
1085     cout << "\t--roll=degrees:  roll angle in degrees (Phi)" << endl;
1086     cout << "\t--pitch=degrees:  pitch angle in degrees (Theta)" << endl;
1087     cout << "\t--uBody=feet per second:  velocity along the body X axis"
1088          << endl;
1089     cout << "\t--vBody=feet per second:  velocity along the body Y axis"
1090          << endl;
1091     cout << "\t--wBody=feet per second:  velocity along the body Z axis"
1092          << endl;
1093     cout << "\t\t(unless --units-meters specified" << endl;
1094     cout << "\t--vc= initial airspeed in knots (--fdm=jsb only)" << endl;
1095     cout << "\t--mach= initial mach number (--fdm=jsb only)" << endl;
1096     cout << endl;
1097
1098     cout << "Rendering Options:" << endl;
1099     cout << "\t--fog-disable:  disable fog/haze" << endl;
1100     cout << "\t--fog-fastest:  enable fastest fog/haze" << endl;
1101     cout << "\t--fog-nicest:  enable nicest fog/haze" << endl;
1102     cout << "\t--enable-clouds:  enable demo cloud layer" << endl;
1103     cout << "\t--disable-clouds:  disable demo cloud layer" << endl;
1104     cout << "\t--clouds-asl=xxx:  specify altitude of cloud layer above sea level" << endl;
1105     cout << "\t--fov=xx.x:  specify initial field of view angle in degrees"
1106          << endl;
1107     cout << "\t--disable-fullscreen:  disable fullscreen mode" << endl;
1108     cout << "\t--enable-fullscreen:  enable fullscreen mode" << endl;
1109     cout << "\t--shading-flat:  enable flat shading" << endl;
1110     cout << "\t--shading-smooth:  enable smooth shading" << endl;
1111     cout << "\t--disable-skyblend:  disable sky blending" << endl;
1112     cout << "\t--enable-skyblend:  enable sky blending" << endl;
1113     cout << "\t--disable-textures:  disable textures" << endl;
1114     cout << "\t--enable-textures:  enable textures" << endl;
1115     cout << "\t--disable-wireframe:  disable wireframe drawing mode" << endl;
1116     cout << "\t--enable-wireframe:  enable wireframe drawing mode" << endl;
1117     cout << "\t--geometry=WWWxHHH:  window geometry: 640x480, 800x600, etc."
1118          << endl;
1119     cout << "\t--view-offset=xxx:  set the default forward view direction"
1120          << endl;
1121     cout << "\t\tas an offset from straight ahead.  Allowable values are"
1122          << endl;
1123     cout << "\t\tLEFT, RIGHT, CENTER, or a specific number of degrees" << endl;
1124     cout << "\t--visibility=xxx:  specify initial visibility in meters" << endl;
1125     cout << "\t--visibility-miles=xxx:  specify initial visibility in miles"
1126          << endl;
1127     cout << endl;
1128
1129     cout << "Scenery Options:" << endl;
1130     cout << "\t--tile-radius=n:  specify tile radius, must be 1 - 4" << endl;
1131     cout << endl;
1132
1133     cout << "Hud Options:" << endl;
1134     cout << "\t--units-feet:  Hud displays units in feet" << endl;
1135     cout << "\t--units-meters:  Hud displays units in meters" << endl;
1136     cout << "\t--hud-tris:  Hud displays number of triangles rendered" << endl;
1137     cout << "\t--hud-culled:  Hud displays percentage of triangles culled"
1138          << endl;
1139     cout << endl;
1140         
1141     cout << "Time Options:" << endl;
1142     cout << "\t--time-offset=[+-]hh:mm:ss: add this time offset" << endl;
1143     cout << "\t--time-match-real: Synchronize real-world and FlightGear" << endl
1144          << "\t\ttime. Can be used in combination with --time-offset." << endl;
1145     cout << "\t--time-match-local:Synchronize local real-world and " << endl
1146          << "\t\tFlightGear time" << endl;   
1147     cout << "\t--start-date-sys=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1148          << "\t\tdate/time. Uses your system time " << endl;
1149     cout << "\t--start-date-gmt=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1150          << "\t\tdate/time. Uses Greenwich Mean Time" << endl;
1151     cout << "\t--start-date-lat=yyyy:mm:dd:hh:mm:ss: specify a starting" << endl
1152          << "\t\tdate/time. Uses Local Aircraft Time" << endl;
1153 #ifdef FG_NETWORK_OLK
1154     cout << endl;
1155
1156     cout << "Network Options:" << endl;
1157     cout << "\t--enable-network-olk:  enable Multipilot mode" << endl;
1158     cout << "\t--disable-network-olk:  disable Multipilot mode (default)" << endl;
1159     cout << "\t--net-hud:  Hud displays network info" << endl;
1160     cout << "\t--net-id=name:  specify your own callsign" << endl;
1161 #endif
1162
1163     cout << endl;
1164     cout << "Route/Way Point Options:" << endl;
1165     cout << "\t--wp=ID[@alt]:  specify a waypoint for the GC autopilot" << endl;
1166     cout << "\t\tYou can specify multiple waypoints (a route) with multiple"
1167          << endl;
1168     cout << "\t\tinstances of --wp=" << endl;
1169     cout << "\t--flight-plan=[file]: Read all waypoints from [file]" <<endl;
1170 }