]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
b4b07ddcea9963b238e728c2e0423efe8b449f90
[flightgear.git] / src / Main / fg_commands.cxx
1 // fg_commands.cxx - internal FGFS commands.
2
3 #ifdef HAVE_CONFIG_H
4 #  include "config.h"
5 #endif
6
7 #include <string.h>             // strcmp()
8
9 #include <simgear/compiler.h>
10
11 #include STL_STRING
12 #include STL_FSTREAM
13
14 #include <simgear/sg_inlines.h>
15 #include <simgear/debug/logstream.hxx>
16 #include <simgear/math/sg_random.h>
17 #include <simgear/scene/material/mat.hxx>
18 #include <simgear/structure/exception.hxx>
19 #include <simgear/structure/commands.hxx>
20 #include <simgear/props/props.hxx>
21
22 #include <Cockpit/panel.hxx>
23 #include <Cockpit/panel_io.hxx>
24 #include <Cockpit/hud.hxx>
25 #include <Environment/environment.hxx>
26 #include <FDM/flight.hxx>
27 #include <GUI/gui.h>
28 #include <GUI/new_gui.hxx>
29 #include <GUI/dialog.hxx>
30 #include <Aircraft/replay.hxx>
31 #include <Scenery/tilemgr.hxx>
32 #include <Scenery/scenery.hxx>
33 #include <Scripting/NasalSys.hxx>
34 #include <Sound/fg_fx.hxx>
35 #include <Time/sunsolver.hxx>
36 #include <Time/tmp.hxx>
37
38 #include "fg_init.hxx"
39 #include "fg_io.hxx"
40 #include "fg_commands.hxx"
41 #include "fg_props.hxx"
42 #include "globals.hxx"
43 #include "logger.hxx"
44 #include "util.hxx"
45 #include "viewmgr.hxx"
46 #include "main.hxx"
47
48 SG_USING_STD(string);
49 SG_USING_STD(ifstream);
50 SG_USING_STD(ofstream);
51
52
53 \f
54 ////////////////////////////////////////////////////////////////////////
55 // Static helper functions.
56 ////////////////////////////////////////////////////////////////////////
57
58
59 static inline SGPropertyNode *
60 get_prop (const SGPropertyNode * arg)
61 {
62     return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
63 }
64
65 static inline SGPropertyNode *
66 get_prop2 (const SGPropertyNode * arg)
67 {
68     return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
69 }
70
71
72 /**
73  * Get a double value and split it as required.
74  */
75 static void
76 split_value (double full_value, const char * mask,
77              double * unmodifiable, double * modifiable)
78 {
79     if (!strcmp("integer", mask)) {
80         *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
81         *unmodifiable = full_value - *modifiable;
82     } else if (!strcmp("decimal", mask)) {
83         *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
84         *modifiable = full_value - *unmodifiable;
85     } else {
86         if (strcmp("all", mask))
87             SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
88                    << " assuming 'all'");
89         *unmodifiable = 0;
90         *modifiable = full_value;
91     }
92 }
93
94
95 /**
96  * Clamp or wrap a value as specified.
97  */
98 static void
99 limit_value (double * value, const SGPropertyNode * arg)
100 {
101     const SGPropertyNode * min_node = arg->getChild("min");
102     const SGPropertyNode * max_node = arg->getChild("max");
103
104     bool wrap = arg->getBoolValue("wrap");
105
106     if (min_node == 0 || max_node == 0)
107         wrap = false;
108   
109     if (wrap) {                 // wrap such that min <= x < max
110         double min_val = min_node->getDoubleValue();
111         double max_val = max_node->getDoubleValue();
112         double resolution = arg->getDoubleValue("resolution");
113         if (resolution > 0.0) {
114             // snap to (min + N*resolution), taking special care to handle imprecision
115             int n = (int)floor((*value - min_val) / resolution + 0.5);
116             int steps = (int)floor((max_val - min_val) / resolution + 0.5);
117             SG_NORMALIZE_RANGE(n, 0, steps);
118             *value = min_val + resolution * n;
119         } else {
120             // plain circular wrapping
121             SG_NORMALIZE_RANGE(*value, min_val, max_val);
122         }
123     } else {                    // clamp such that min <= x <= max
124         if ((min_node != 0) && (*value < min_node->getDoubleValue()))
125             *value = min_node->getDoubleValue();
126         else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
127             *value = max_node->getDoubleValue();
128     }
129 }
130
131 static bool
132 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
133 {
134     switch (value1->getType()) {
135     case SGPropertyNode::BOOL:
136         return (value1->getBoolValue() == value2->getBoolValue());
137     case SGPropertyNode::INT:
138         return (value1->getIntValue() == value2->getIntValue());
139     case SGPropertyNode::LONG:
140         return (value1->getLongValue() == value2->getLongValue());
141     case SGPropertyNode::FLOAT:
142         return (value1->getFloatValue() == value2->getFloatValue());
143     case SGPropertyNode::DOUBLE:
144         return (value1->getDoubleValue() == value2->getDoubleValue());
145     default:
146         return !strcmp(value1->getStringValue(), value2->getStringValue());
147     }
148 }
149
150
151 \f
152 ////////////////////////////////////////////////////////////////////////
153 // Command implementations.
154 ////////////////////////////////////////////////////////////////////////
155
156
157 /**
158  * Built-in command: do nothing.
159  */
160 static bool
161 do_null (const SGPropertyNode * arg)
162 {
163   return true;
164 }
165
166 /**
167  * Built-in command: run a Nasal script.
168  */
169 static bool
170 do_nasal (const SGPropertyNode * arg)
171 {
172     return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
173 }
174
175 /**
176  * Built-in command: exit FlightGear.
177  *
178  * status: the exit status to return to the operating system (defaults to 0)
179  */
180 static bool
181 do_exit (const SGPropertyNode * arg)
182 {
183     SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
184     fgSetBool("/sim/signals/exit", true);
185
186     if (fgGetBool("/sim/startup/save-on-exit")) {
187 #ifdef _MSC_VER
188         char* envp = ::getenv( "APPDATA" );
189         if ( envp != NULL ) {
190             SGPath config( envp );
191             config.append( "flightgear.org" );
192 #else
193         if ( homedir != NULL ) {
194             SGPath config( homedir );
195             config.append( ".fgfs" );
196 #endif
197             config.append( "autosave.xml" );
198             config.create_dir( 0700 );
199             SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
200             try {
201                 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
202             } catch (const sg_exception &e) {
203                 guiErrorMessage("Error writing autosave.xml: ", e);
204             }
205
206             SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
207         }
208     }
209     fgExit(arg->getIntValue("status", 0));
210     return true;
211 }
212
213
214 /**
215  * Reset FlightGear (Shift-Escape or Menu->File->Reset)
216  */
217 static bool
218 do_reset (const SGPropertyNode * arg)
219 {
220     reInit();
221     return true;
222 }
223
224
225 /**
226  * Built-in command: reinitialize one or more subsystems.
227  *
228  * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
229  * none is specified, reinitialize all of them.
230  */
231 static bool
232 do_reinit (const SGPropertyNode * arg)
233 {
234     bool result = true;
235
236     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
237     if (subsystems.size() == 0) {
238         globals->get_subsystem_mgr()->reinit();
239     } else {
240         for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
241             const char * name = subsystems[i]->getStringValue();
242             SGSubsystem * subsystem = globals->get_subsystem(name);
243             if (subsystem == 0) {
244                 result = false;
245                 SG_LOG( SG_GENERAL, SG_ALERT,
246                         "Subsystem " << name << "not found" );
247             } else {
248                 subsystem->reinit();
249             }
250         }
251     }
252
253     globals->get_event_mgr()->reinit();
254
255     return result;
256 }
257
258 #if 0
259   //
260   // these routines look useful ??? but are never used in the code ???
261   //
262
263 /**
264  * Built-in command: suspend one or more subsystems.
265  *
266  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
267  */
268 static bool
269 do_suspend (const SGPropertyNode * arg)
270 {
271     bool result = true;
272
273     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
274     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
275         const char * name = subsystems[i]->getStringValue();
276         SGSubsystem * subsystem = globals->get_subsystem(name);
277         if (subsystem == 0) {
278             result = false;
279             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
280         } else {
281             subsystem->suspend();
282         }
283     }
284     return result;
285 }
286
287 /**
288  * Built-in command: suspend one or more subsystems.
289  *
290  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
291  */
292 static bool
293 do_resume (const SGPropertyNode * arg)
294 {
295     bool result = true;
296
297     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
298     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
299         const char * name = subsystems[i]->getStringValue();
300         SGSubsystem * subsystem = globals->get_subsystem(name);
301         if (subsystem == 0) {
302             result = false;
303             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
304         } else {
305             subsystem->resume();
306         }
307     }
308     return result;
309 }
310
311 #endif
312
313
314 /**
315  * Built-in command: load flight.
316  *
317  * file (optional): the name of the file to load (relative to current
318  *   directory).  Defaults to "fgfs.sav"
319  */
320 static bool
321 do_load (const SGPropertyNode * arg)
322 {
323     const string &file = arg->getStringValue("file", "fgfs.sav");
324
325     if (!fgValidatePath(file.c_str(), false)) {
326         SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
327                 "(unauthorized access)");
328         return false;
329     }
330
331     ifstream input(file.c_str());
332     if (input.good() && fgLoadFlight(input)) {
333         input.close();
334         SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
335         return true;
336     } else {
337         SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
338         return false;
339     }
340 }
341
342
343 /**
344  * Built-in command: save flight.
345  *
346  * file (optional): the name of the file to save (relative to the
347  * current directory).  Defaults to "fgfs.sav".
348  */
349 static bool
350 do_save (const SGPropertyNode * arg)
351 {
352     const string &file = arg->getStringValue("file", "fgfs.sav");
353
354     if (!fgValidatePath(file.c_str(), false)) {
355         SG_LOG(SG_IO, SG_ALERT, "save: reading '" << file << "' denied "
356                 "(unauthorized access)");
357         return false;
358     }
359
360     bool write_all = arg->getBoolValue("write-all", false);
361     SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
362     ofstream output(file.c_str());
363     if (output.good() && fgSaveFlight(output, write_all)) {
364         output.close();
365         SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
366         return true;
367     } else {
368         SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
369         return false;
370     }
371 }
372
373
374 /**
375  * Built-in command: (re)load the panel.
376  *
377  * path (optional): the file name to load the panel from 
378  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
379  * and if that's unspecified, to "Panels/Default/default.xml".
380  */
381 static bool
382 do_panel_load (const SGPropertyNode * arg)
383 {
384   string panel_path =
385     arg->getStringValue("path",
386                         fgGetString("/sim/panel/path",
387                                     "Panels/Default/default.xml"));
388   FGPanel * new_panel = fgReadPanel(panel_path);
389   if (new_panel == 0) {
390     SG_LOG(SG_INPUT, SG_ALERT,
391            "Error reading new panel from " << panel_path);
392     return false;
393   }
394   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
395   globals->get_current_panel()->unbind();
396   delete globals->get_current_panel();
397   globals->set_current_panel( new_panel );
398   globals->get_current_panel()->bind();
399   return true;
400 }
401
402
403 /**
404  * Built-in command: pass a mouse click to the panel.
405  *
406  * button: the mouse button number, zero-based.
407  * is-down: true if the button is down, false if it is up.
408  * x-pos: the x position of the mouse click.
409  * y-pos: the y position of the mouse click.
410  */
411 static bool
412 do_panel_mouse_click (const SGPropertyNode * arg)
413 {
414   if (globals->get_current_panel() != 0)
415     return globals->get_current_panel()
416       ->doMouseAction(arg->getIntValue("button"),
417                       arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
418                       arg->getIntValue("x-pos"),
419                       arg->getIntValue("y-pos"));
420   else
421     return false;
422 }
423
424
425 /**
426  * Built-in command: (re)load preferences.
427  *
428  * path (optional): the file name to load the panel from (relative
429  * to FG_ROOT). Defaults to "preferences.xml".
430  */
431 static bool
432 do_preferences_load (const SGPropertyNode * arg)
433 {
434   try {
435     fgLoadProps(arg->getStringValue("path", "preferences.xml"),
436                 globals->get_props());
437   } catch (const sg_exception &e) {
438     guiErrorMessage("Error reading global preferences: ", e);
439     return false;
440   }
441   SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
442   return true;
443 }
444
445
446 static void
447 fix_hud_visibility()
448 {
449   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
450       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
451       if ( globals->get_viewmgr()->get_current() == 1 ) {
452           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
453       }
454   }
455 }
456
457 static void
458 do_view_next( bool )
459 {
460     globals->get_current_view()->setHeadingOffset_deg(0.0);
461     globals->get_viewmgr()->next_view();
462     fix_hud_visibility();
463 }
464
465 static void
466 do_view_prev( bool )
467 {
468     globals->get_current_view()->setHeadingOffset_deg(0.0);
469     globals->get_viewmgr()->prev_view();
470     fix_hud_visibility();
471 }
472
473 /**
474  * Built-in command: cycle view.
475  */
476 static bool
477 do_view_cycle (const SGPropertyNode * arg)
478 {
479   globals->get_current_view()->setHeadingOffset_deg(0.0);
480   globals->get_viewmgr()->next_view();
481   fix_hud_visibility();
482   return true;
483 }
484
485 /**
486  * Built-in command: capture screen.
487  */
488 static bool
489 do_screen_capture (const SGPropertyNode * arg)
490 {
491   return fgDumpSnapShot();
492 }
493
494 static bool
495 do_dump_scene_graph (const SGPropertyNode*)
496 {
497     fgDumpSceneGraph();
498     return true;
499 }
500
501 static bool
502 do_dump_terrain_branch (const SGPropertyNode*)
503 {
504     fgDumpTerrainBranch();
505
506     double lon_deg = fgGetDouble("/position/longitude-deg");
507     double lat_deg = fgGetDouble("/position/latitude-deg");
508     SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
509     SGVec3d zero = SGVec3d::fromGeod(geodPos);
510
511     SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
512     SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
513     SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
514
515     return true;
516 }
517
518 /**
519  * Built-in command: hires capture screen.
520  */
521 static bool
522 do_hires_screen_capture (const SGPropertyNode * arg)
523 {
524   fgHiResDump();
525   return true;
526 }
527
528
529 /**
530  * Reload the tile cache.
531  */
532 static bool
533 do_tile_cache_reload (const SGPropertyNode * arg)
534 {
535     static const SGPropertyNode *master_freeze
536         = fgGetNode("/sim/freeze/master");
537     bool freeze = master_freeze->getBoolValue();
538     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
539     if ( !freeze ) {
540         fgSetBool("/sim/freeze/master", true);
541     }
542     if ( globals->get_tile_mgr()->init() ) {
543         // Load the local scenery data
544         double visibility_meters = fgGetDouble("/environment/visibility-m");
545         globals->get_tile_mgr()->update( visibility_meters );
546     } else {
547         SG_LOG( SG_GENERAL, SG_ALERT, 
548                 "Error in Tile Manager initialization!" );
549         exit(-1);
550     }
551     if ( !freeze ) {
552         fgSetBool("/sim/freeze/master", false);
553     }
554     return true;
555 }
556
557
558 /**
559  * Set the sea level outside air temperature and assigning that to all
560  * boundary and aloft environment layers.
561  */
562 static bool
563 do_set_sea_level_degc (const SGPropertyNode * arg)
564 {
565     double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
566
567     SGPropertyNode *node, *child;
568
569     // boundary layers
570     node = fgGetNode( "/environment/config/boundary" );
571     if ( node != NULL ) {
572       int i = 0;
573       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
574         child->setDoubleValue( "temperature-sea-level-degc",
575                                temp_sea_level_degc );
576         ++i;
577       }
578     }
579
580     // aloft layers
581     node = fgGetNode( "/environment/config/aloft" );
582     if ( node != NULL ) {
583       int i = 0;
584       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
585         child->setDoubleValue( "temperature-sea-level-degc",
586                                temp_sea_level_degc );
587         ++i;
588       }
589     }
590
591     return true;
592 }
593
594
595 /**
596  * Set the outside air temperature at the "current" altitude by first
597  * calculating the corresponding sea level temp, and assigning that to
598  * all boundary and aloft environment layers.
599  */
600 static bool
601 do_set_oat_degc (const SGPropertyNode * arg)
602 {
603     const string &temp_str = arg->getStringValue("temp-degc", "15.0");
604
605     // check for an altitude specified in the arguments, otherwise use
606     // current aircraft altitude.
607     const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
608     if ( altitude_ft == NULL ) {
609         altitude_ft = fgGetNode("/position/altitude-ft");
610     }
611
612     FGEnvironment dummy;        // instantiate a dummy so we can leech a method
613     dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
614     dummy.set_temperature_degc( atof( temp_str.c_str() ) );
615     double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
616
617     //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
618     //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
619     //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
620  
621     SGPropertyNode *node, *child;
622
623     // boundary layers
624     node = fgGetNode( "/environment/config/boundary" );
625     if ( node != NULL ) {
626       int i = 0;
627       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
628         child->setDoubleValue( "temperature-sea-level-degc",
629                                temp_sea_level_degc );
630         ++i;
631       }
632     }
633
634     // aloft layers
635     node = fgGetNode( "/environment/config/aloft" );
636     if ( node != NULL ) {
637       int i = 0;
638       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
639         child->setDoubleValue( "temperature-sea-level-degc",
640                                temp_sea_level_degc );
641         ++i;
642       }
643     }
644
645     return true;
646 }
647
648 /**
649  * Set the sea level outside air dewpoint and assigning that to all
650  * boundary and aloft environment layers.
651  */
652 static bool
653 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
654 {
655     double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
656
657     SGPropertyNode *node, *child;
658
659     // boundary layers
660     node = fgGetNode( "/environment/config/boundary" );
661     if ( node != NULL ) {
662       int i = 0;
663       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
664         child->setDoubleValue( "dewpoint-sea-level-degc",
665                                dewpoint_sea_level_degc );
666         ++i;
667       }
668     }
669
670     // aloft layers
671     node = fgGetNode( "/environment/config/aloft" );
672     if ( node != NULL ) {
673       int i = 0;
674       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
675         child->setDoubleValue( "dewpoint-sea-level-degc",
676                                dewpoint_sea_level_degc );
677         ++i;
678       }
679     }
680
681     return true;
682 }
683
684
685 /**
686  * Set the outside air dewpoint at the "current" altitude by first
687  * calculating the corresponding sea level dewpoint, and assigning
688  * that to all boundary and aloft environment layers.
689  */
690 static bool
691 do_set_dewpoint_degc (const SGPropertyNode * arg)
692 {
693     const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
694
695     // check for an altitude specified in the arguments, otherwise use
696     // current aircraft altitude.
697     const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
698     if ( altitude_ft == NULL ) {
699         altitude_ft = fgGetNode("/position/altitude-ft");
700     }
701
702     FGEnvironment dummy;        // instantiate a dummy so we can leech a method
703     dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
704     dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
705     double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
706
707     //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
708     //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
709     //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
710  
711     SGPropertyNode *node, *child;
712
713     // boundary layers
714     node = fgGetNode( "/environment/config/boundary" );
715     if ( node != NULL ) {
716       int i = 0;
717       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
718         child->setDoubleValue( "dewpoint-sea-level-degc",
719                                dewpoint_sea_level_degc );
720         ++i;
721       }
722     }
723
724     // aloft layers
725     node = fgGetNode( "/environment/config/aloft" );
726     if ( node != NULL ) {
727       int i = 0;
728       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
729         child->setDoubleValue( "dewpoint-sea-level-degc",
730                                dewpoint_sea_level_degc );
731         ++i;
732       }
733     }
734
735     return true;
736 }
737
738 /**
739  * Update the lighting manually.
740  */
741 static bool
742 do_timeofday (const SGPropertyNode * arg)
743 {
744     const string &offset_type = arg->getStringValue("timeofday", "noon");
745
746     static const SGPropertyNode *longitude
747         = fgGetNode("/position/longitude-deg");
748     static const SGPropertyNode *latitude
749         = fgGetNode("/position/latitude-deg");
750     static const SGPropertyNode *cur_time_override
751         = fgGetNode("/sim/time/cur-time-override", true);
752
753     int orig_warp = globals->get_warp();
754     SGTime *t = globals->get_time_params();
755     time_t cur_time = t->get_cur_time();
756     // cout << "cur_time = " << cur_time << endl;
757     // cout << "orig_warp = " << orig_warp << endl;
758
759     int warp = 0;
760     if ( offset_type == "real" ) {
761         warp = -orig_warp;
762     } else if ( offset_type == "dawn" ) {
763         warp = fgTimeSecondsUntilSunAngle( cur_time,
764                                            longitude->getDoubleValue()
765                                              * SGD_DEGREES_TO_RADIANS,
766                                            latitude->getDoubleValue()
767                                              * SGD_DEGREES_TO_RADIANS,
768                                            90.0, true ); 
769     } else if ( offset_type == "morning" ) {
770         warp = fgTimeSecondsUntilSunAngle( cur_time,
771                                            longitude->getDoubleValue()
772                                              * SGD_DEGREES_TO_RADIANS,
773                                            latitude->getDoubleValue()
774                                              * SGD_DEGREES_TO_RADIANS,
775                                            75.0, true ); 
776     } else if ( offset_type == "noon" ) {
777         warp = fgTimeSecondsUntilSunAngle( cur_time,
778                                            longitude->getDoubleValue()
779                                              * SGD_DEGREES_TO_RADIANS,
780                                            latitude->getDoubleValue()
781                                              * SGD_DEGREES_TO_RADIANS,
782                                            0.0, true ); 
783     } else if ( offset_type == "afternoon" ) {
784         warp = fgTimeSecondsUntilSunAngle( cur_time,
785                                            longitude->getDoubleValue()
786                                              * SGD_DEGREES_TO_RADIANS,
787                                            latitude->getDoubleValue()
788                                              * SGD_DEGREES_TO_RADIANS,
789                                            60.0, false ); 
790      } else if ( offset_type == "dusk" ) {
791         warp = fgTimeSecondsUntilSunAngle( cur_time,
792                                            longitude->getDoubleValue()
793                                              * SGD_DEGREES_TO_RADIANS,
794                                            latitude->getDoubleValue()
795                                              * SGD_DEGREES_TO_RADIANS,
796                                            90.0, false ); 
797      } else if ( offset_type == "evening" ) {
798         warp = fgTimeSecondsUntilSunAngle( cur_time,
799                                            longitude->getDoubleValue()
800                                              * SGD_DEGREES_TO_RADIANS,
801                                            latitude->getDoubleValue()
802                                              * SGD_DEGREES_TO_RADIANS,
803                                            100.0, false ); 
804     } else if ( offset_type == "midnight" ) {
805         warp = fgTimeSecondsUntilSunAngle( cur_time,
806                                            longitude->getDoubleValue()
807                                              * SGD_DEGREES_TO_RADIANS,
808                                            latitude->getDoubleValue()
809                                              * SGD_DEGREES_TO_RADIANS,
810                                            180.0, false ); 
811     }
812     // cout << "warp = " << warp << endl;
813     globals->set_warp( orig_warp + warp );
814
815     t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
816                latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
817                cur_time_override->getLongValue(),
818                globals->get_warp() );
819
820     return true;
821 }
822
823
824 /**
825  * Built-in command: toggle a bool property value.
826  *
827  * property: The name of the property to toggle.
828  */
829 static bool
830 do_property_toggle (const SGPropertyNode * arg)
831 {
832   SGPropertyNode * prop = get_prop(arg);
833   return prop->setBoolValue(!prop->getBoolValue());
834 }
835
836
837 /**
838  * Built-in command: assign a value to a property.
839  *
840  * property: the name of the property to assign.
841  * value: the value to assign; or
842  * property[1]: the property to copy from.
843  */
844 static bool
845 do_property_assign (const SGPropertyNode * arg)
846 {
847   SGPropertyNode * prop = get_prop(arg);
848   const SGPropertyNode * prop2 = get_prop2(arg);
849   const SGPropertyNode * value = arg->getNode("value");
850
851   if (value != 0)
852       return prop->setUnspecifiedValue(value->getStringValue());
853   else if (prop2)
854       return prop->setUnspecifiedValue(prop2->getStringValue());
855   else
856       return false;
857 }
858
859
860 /**
861  * Built-in command: increment or decrement a property value.
862  *
863  * If the 'step' argument is present, it will be used; otherwise,
864  * the command uses 'offset' and 'factor', usually from the mouse.
865  *
866  * property: the name of the property to increment or decrement.
867  * step: the amount of the increment or decrement (default: 0).
868  * offset: offset from the current setting (used for the mouse; multiplied 
869  *         by factor)
870  * factor: scaling amount for the offset (defaults to 1).
871  * min: the minimum allowed value (default: no minimum).
872  * max: the maximum allowed value (default: no maximum).
873  * mask: 'integer' to apply only to the left of the decimal point, 
874  *       'decimal' to apply only to the right of the decimal point,
875  *       or 'all' to apply to the whole number (the default).
876  * wrap: true if the value should be wrapped when it passes min or max;
877  *       both min and max must be present for this to work (default:
878  *       false).
879  */
880 static bool
881 do_property_adjust (const SGPropertyNode * arg)
882 {
883   SGPropertyNode * prop = get_prop(arg);
884
885   double amount = 0;
886   if (arg->hasValue("step"))
887       amount = arg->getDoubleValue("step");
888   else
889       amount = (arg->getDoubleValue("factor", 1)
890                 * arg->getDoubleValue("offset"));
891           
892   double unmodifiable, modifiable;
893   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
894               &unmodifiable, &modifiable);
895   modifiable += amount;
896   limit_value(&modifiable, arg);
897
898   prop->setDoubleValue(unmodifiable + modifiable);
899
900   return true;
901 }
902
903
904 /**
905  * Built-in command: multiply a property value.
906  *
907  * property: the name of the property to multiply.
908  * factor: the amount by which to multiply.
909  * min: the minimum allowed value (default: no minimum).
910  * max: the maximum allowed value (default: no maximum).
911  * mask: 'integer' to apply only to the left of the decimal point, 
912  *       'decimal' to apply only to the right of the decimal point,
913  *       or 'all' to apply to the whole number (the default).
914  * wrap: true if the value should be wrapped when it passes min or max;
915  *       both min and max must be present for this to work (default:
916  *       false).
917  */
918 static bool
919 do_property_multiply (const SGPropertyNode * arg)
920 {
921   SGPropertyNode * prop = get_prop(arg);
922   double factor = arg->getDoubleValue("factor", 1);
923
924   double unmodifiable, modifiable;
925   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
926               &unmodifiable, &modifiable);
927   modifiable *= factor;
928   limit_value(&modifiable, arg);
929
930   prop->setDoubleValue(unmodifiable + modifiable);
931
932   return true;
933 }
934
935
936 /**
937  * Built-in command: swap two property values.
938  *
939  * property[0]: the name of the first property.
940  * property[1]: the name of the second property.
941  */
942 static bool
943 do_property_swap (const SGPropertyNode * arg)
944 {
945   SGPropertyNode * prop1 = get_prop(arg);
946   SGPropertyNode * prop2 = get_prop2(arg);
947
948                                 // FIXME: inefficient
949   const string & tmp = prop1->getStringValue();
950   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
951           prop2->setUnspecifiedValue(tmp.c_str()));
952 }
953
954
955 /**
956  * Built-in command: Set a property to an axis or other moving input.
957  *
958  * property: the name of the property to set.
959  * setting: the current input setting, usually between -1.0 and 1.0.
960  * offset: the offset to shift by, before applying the factor.
961  * factor: the factor to multiply by (use negative to reverse).
962  */
963 static bool
964 do_property_scale (const SGPropertyNode * arg)
965 {
966   SGPropertyNode * prop = get_prop(arg);
967   double setting = arg->getDoubleValue("setting");
968   double offset = arg->getDoubleValue("offset", 0.0);
969   double factor = arg->getDoubleValue("factor", 1.0);
970   bool squared = arg->getBoolValue("squared", false);
971   int power = arg->getIntValue("power", (squared ? 2 : 1));
972
973   int sign = (setting < 0 ? -1 : 1);
974
975   switch (power) {
976   case 1:
977       break;
978   case 2:
979       setting = setting * setting * sign;
980       break;
981   case 3:
982       setting = setting * setting * setting;
983       break;
984   case 4:
985       setting = setting * setting * setting * setting * sign;
986       break;
987   default:
988       setting =  pow(setting, power);
989       if ((power % 2) == 0)
990           setting *= sign;
991       break;
992   }
993
994   return prop->setDoubleValue((setting + offset) * factor);
995 }
996
997
998 /**
999  * Built-in command: cycle a property through a set of values.
1000  *
1001  * If the current value isn't in the list, the cycle will
1002  * (re)start from the beginning.
1003  *
1004  * property: the name of the property to cycle.
1005  * value[*]: the list of values to cycle through.
1006  */
1007 static bool
1008 do_property_cycle (const SGPropertyNode * arg)
1009 {
1010     SGPropertyNode * prop = get_prop(arg);
1011     vector<SGPropertyNode_ptr> values = arg->getChildren("value");
1012     int selection = -1;
1013     int nSelections = values.size();
1014
1015     if (nSelections < 1) {
1016         SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
1017         return false;
1018     }
1019
1020                                 // Try to find the current selection
1021     for (int i = 0; i < nSelections; i++) {
1022         if (compare_values(prop, values[i])) {
1023             selection = i + 1;
1024             break;
1025         }
1026     }
1027
1028                                 // Default or wrap to the first selection
1029     if (selection < 0 || selection >= nSelections)
1030         selection = 0;
1031
1032     prop->setUnspecifiedValue(values[selection]->getStringValue());
1033     return true;
1034 }
1035
1036
1037 /**
1038  * Built-in command: randomize a numeric property value.
1039  *
1040  * property: the name of the property value to randomize.
1041  * min: the minimum allowed value.
1042  * max: the maximum allowed value.
1043  */
1044 static bool
1045 do_property_randomize (const SGPropertyNode * arg)
1046 {
1047     SGPropertyNode * prop = get_prop(arg);
1048     double min = arg->getDoubleValue("min", DBL_MIN);
1049     double max = arg->getDoubleValue("max", DBL_MAX);
1050     prop->setDoubleValue(sg_random() * (max - min) + min);
1051     return true;
1052 }
1053
1054
1055 /**
1056  * Built-in command: reinit the data logging system based on the
1057  * current contents of the /logger tree.
1058  */
1059 static bool
1060 do_data_logging_commit (const SGPropertyNode * arg)
1061 {
1062     FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1063     log->reinit();
1064     return true;
1065 }
1066
1067 /**
1068  * Built-in command: Add a dialog to the GUI system.  Does *not*
1069  * display the dialog.  The property node should have the same format
1070  * as a dialog XML configuration.  It must include:
1071  *
1072  * name: the name of the GUI dialog for future reference.
1073  */
1074 static bool
1075 do_dialog_new (const SGPropertyNode * arg)
1076 {
1077     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1078
1079     // Note the casting away of const: this is *real*.  Doing a
1080     // "dialog-apply" command later on will mutate this property node.
1081     // I'm not convinced that this isn't the Right Thing though; it
1082     // allows client to create a node, pass it to dialog-new, and get
1083     // the values back from the dialog by reading the same node.
1084     // Perhaps command arguments are not as "const" as they would
1085     // seem?
1086     gui->newDialog((SGPropertyNode*)arg);
1087     return true;
1088 }
1089
1090 /**
1091  * Built-in command: Show an XML-configured dialog.
1092  *
1093  * dialog-name: the name of the GUI dialog to display.
1094  */
1095 static bool
1096 do_dialog_show (const SGPropertyNode * arg)
1097 {
1098     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1099     gui->showDialog(arg->getStringValue("dialog-name"));
1100     return true;
1101 }
1102
1103
1104 /**
1105  * Built-in Command: Hide the active XML-configured dialog.
1106  */
1107 static bool
1108 do_dialog_close (const SGPropertyNode * arg)
1109 {
1110     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1111     if(arg->hasValue("dialog-name"))
1112         return gui->closeDialog(arg->getStringValue("dialog-name"));
1113     return gui->closeActiveDialog();
1114 }
1115
1116
1117 /**
1118  * Update a value in the active XML-configured dialog.
1119  *
1120  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1121  */
1122 static bool
1123 do_dialog_update (const SGPropertyNode * arg)
1124 {
1125     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1126     FGDialog * dialog;
1127     if (arg->hasValue("dialog-name"))
1128         dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1129     else
1130         dialog = gui->getActiveDialog();
1131
1132     if (dialog != 0) {
1133         dialog->updateValues(arg->getStringValue("object-name"));
1134         return true;
1135     } else {
1136         return false;
1137     }
1138 }
1139
1140
1141 /**
1142  * Apply a value in the active XML-configured dialog.
1143  *
1144  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1145  */
1146 static bool
1147 do_dialog_apply (const SGPropertyNode * arg)
1148 {
1149     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1150     FGDialog * dialog;
1151     if (arg->hasValue("dialog-name"))
1152         dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1153     else
1154         dialog = gui->getActiveDialog();
1155
1156     if (dialog != 0) {
1157         dialog->applyValues(arg->getStringValue("object-name"));
1158         return true;
1159     } else {
1160         return false;
1161     }
1162 }
1163
1164
1165 /**
1166  * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1167  * unlike reinit().
1168  */
1169 static bool
1170 do_gui_redraw (const SGPropertyNode * arg)
1171 {
1172     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1173     gui->redraw();
1174     return true;
1175 }
1176
1177
1178 /**
1179  * Set mouse cursor coordinates and cursor shape.
1180  */
1181 static bool
1182 do_set_cursor (const SGPropertyNode * arg)
1183 {
1184     if (arg->hasValue("x") || arg->hasValue("y")) {
1185         SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1186         SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1187         int x = arg->getIntValue("x", mx->getIntValue());
1188         int y = arg->getIntValue("y", my->getIntValue());
1189         fgWarpMouse(x, y);
1190         mx->setIntValue(x);
1191         my->setIntValue(y);
1192     }
1193
1194     SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1195     if (cursor->getType() != SGPropertyNode::NONE)
1196         fgSetMouseCursor(cursor->getIntValue());
1197
1198     cursor->setIntValue(fgGetMouseCursor());
1199     return true;
1200 }
1201
1202
1203 /**
1204  * Built-in command: play an audio message (i.e. a wav file) This is
1205  * fire and forget.  Call this once per message and it will get dumped
1206  * into a queue.  Messages are played sequentially so they do not
1207  * overlap.
1208  */
1209 static bool
1210 do_play_audio_sample (const SGPropertyNode * arg)
1211 {
1212     FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1213     string path = arg->getStringValue("path");
1214     string file = arg->getStringValue("file");
1215     double volume = arg->getDoubleValue("volume");
1216     // cout << "playing " << path << " / " << file << endl;
1217     try {
1218         fx->play_message( path, file, volume );
1219         return true;
1220
1221     } catch (const sg_io_exception& e) {
1222         SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1223                 "failed to load" << path << '/' << file);
1224         return false;
1225     }
1226 }
1227
1228 /**
1229  * Built-in command: commit presets (read from in /sim/presets/)
1230  */
1231 static bool
1232 do_presets_commit (const SGPropertyNode * arg)
1233 {
1234     // unbind the current fdm state so property changes
1235     // don't get lost when we subsequently delete this fdm
1236     // and create a new one.
1237     cur_fdm_state->unbind();
1238
1239     // set position from presets
1240     fgInitPosition();
1241
1242     fgReInitSubsystems();
1243
1244     globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1245
1246 #if 0
1247     if ( ! fgGetBool("/sim/presets/onground") ) {
1248         fgSetBool( "/sim/freeze/master", true );
1249         fgSetBool( "/sim/freeze/clock", true );
1250     }
1251 #endif
1252
1253     return true;
1254 }
1255
1256 /**
1257  * Built-in command: set log level (0 ... 7)
1258  */
1259 static bool
1260 do_log_level (const SGPropertyNode * arg)
1261 {
1262    sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1263
1264    return true;
1265 }
1266
1267 /**
1268  * Built-in command: replay the FDR buffer
1269  */
1270 static bool
1271 do_replay (const SGPropertyNode * arg)
1272 {
1273     // freeze the master fdm
1274     fgSetInt( "/sim/freeze/replay-state", 1 );
1275
1276     FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1277
1278     fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1279     fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1280     double duration = fgGetDouble( "/sim/replay/duration" );
1281     if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1282         fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1283     } else {
1284         fgSetDouble( "/sim/replay/time", r->get_start_time() );
1285     }
1286
1287     // cout << "start = " << r->get_start_time()
1288     //      << "  end = " << r->get_end_time() << endl;
1289
1290     return true;
1291 }
1292
1293
1294 static bool
1295 do_decrease_visibility (const SGPropertyNode * arg)
1296 {
1297     double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1298     fgSetDouble("/environment/visibility-m", new_value);
1299     fgDefaultWeatherValue("visibility-m", new_value);
1300     globals->get_subsystem("environment")->reinit();
1301
1302     return true;
1303 }
1304  
1305 static bool
1306 do_increase_visibility (const SGPropertyNode * arg)
1307 {
1308     double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1309     fgSetDouble("/environment/visibility-m", new_value);
1310     fgDefaultWeatherValue("visibility-m", new_value);
1311     globals->get_subsystem("environment")->reinit();
1312
1313     return true;
1314 }
1315
1316 static bool
1317 do_hud_init(const SGPropertyNode *)
1318 {
1319     fgHUDInit(0); // minimal HUD
1320     return true;
1321 }
1322
1323 static bool
1324 do_hud_init2(const SGPropertyNode *)
1325 {
1326     fgHUDInit2(0);  // normal HUD
1327     return true;
1328 }
1329
1330
1331 /**
1332  * An fgcommand to allow loading of xml files via nasal,
1333  * the xml file's structure will be made available within
1334  * a property tree node defined under argument "targetnode",
1335  * or in the given argument tree under "data" otherwise.
1336  *
1337  * @param filename a string to hold the complete path & filename of an XML file
1338  * @param targetnode a string pointing to a location within the property tree
1339  * where to store the parsed XML file. If <targetnode> is undefined, then the
1340  * file contents are stored under a node <data> in the argument tree.
1341  */
1342
1343 static bool
1344 do_load_xml_to_proptree(const SGPropertyNode * arg)
1345 {
1346     SGPath file(arg->getStringValue("filename"));
1347     if (file.str().empty())
1348         return false;
1349
1350     if (file.extension() != "xml")
1351         file.concat(".xml");
1352
1353     if (!fgValidatePath(file.c_str(), false)) {
1354         SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1355                 "(unauthorized access)");
1356         return false;
1357     }
1358
1359     SGPropertyNode *targetnode;
1360     if (arg->hasValue("targetnode"))
1361         targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1362     else
1363         targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1364
1365     try {
1366         readProperties(file.c_str(), targetnode, true);
1367     } catch (const sg_exception &e) {
1368         SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1369         return false;
1370     }
1371
1372     return true;
1373 }
1374
1375
1376 /**
1377  * An fgcommand to allow saving of xml files via nasal,
1378  * the file's structure will be determined based on what's
1379  * encountered in the passed (source) property tree node
1380  *
1381  * @param filename a string to hold the complete path & filename of the (new)
1382  * XML file
1383  * @param sourcenode a string pointing to a location within the property tree
1384  * where to find the nodes that should be written recursively into an XML file
1385  * @param data if no sourcenode is given, then the file contents are taken from
1386  * the argument tree's "data" node.
1387  */
1388
1389 static bool
1390 do_save_xml_from_proptree(const SGPropertyNode * arg)
1391 {
1392     SGPath file(arg->getStringValue("filename"));
1393     if (file.str().empty())
1394         return false;
1395
1396     if (file.extension() != "xml")
1397         file.concat(".xml");
1398
1399     if (!fgValidatePath(file.c_str(), true)) {
1400         SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1401                 "(unauthorized access)");
1402         return false;
1403     }
1404
1405     SGPropertyNode *sourcenode;
1406     if (arg->hasValue("sourcenode"))
1407         sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1408     else if (arg->getNode("data", false))
1409         sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1410     else
1411         return false;
1412
1413     try {
1414         writeProperties (file.c_str(), sourcenode, true);
1415     } catch (const sg_exception &e) {
1416         SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1417         return false;
1418     }
1419
1420     return true;
1421 }
1422
1423 static bool
1424 do_press_cockpit_button (const SGPropertyNode *arg)
1425 {
1426   const char *prefix = arg->getStringValue("prefix");
1427
1428   if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1429     return true;
1430
1431   string prop = string(prefix) + "-button";
1432   double value;
1433
1434   if (arg->getBoolValue("latching"))
1435     value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1436   else
1437     value = 1;
1438
1439   fgSetDouble(prop.c_str(), value);
1440   fgSetBool(arg->getStringValue("discrete"), value > 0);
1441
1442   return true;
1443 }
1444
1445 static bool
1446 do_release_cockpit_button (const SGPropertyNode *arg)
1447 {
1448   const char *prefix = arg->getStringValue("prefix");
1449
1450   if (arg->getBoolValue("guarded")) {
1451     string prop = string(prefix) + "-guard";
1452     if (fgGetDouble(prop.c_str()) < 1) {
1453       fgSetDouble(prop.c_str(), 1);
1454       return true;
1455     }
1456   }
1457
1458   if (! arg->getBoolValue("latching")) {
1459     fgSetDouble((string(prefix) + "-button").c_str(), 0);
1460     fgSetBool(arg->getStringValue("discrete"), false);
1461   }
1462
1463   return true;
1464 }
1465
1466
1467 ////////////////////////////////////////////////////////////////////////
1468 // Command setup.
1469 ////////////////////////////////////////////////////////////////////////
1470
1471
1472 /**
1473  * Table of built-in commands.
1474  *
1475  * New commands do not have to be added here; any module in the application
1476  * can add a new command using globals->get_commands()->addCommand(...).
1477  */
1478 static struct {
1479   const char * name;
1480   SGCommandMgr::command_t command;
1481 } built_ins [] = {
1482     { "null", do_null },
1483     { "nasal", do_nasal },
1484     { "exit", do_exit },
1485     { "reset", do_reset },
1486     { "reinit", do_reinit },
1487     { "suspend", do_reinit },
1488     { "resume", do_reinit },
1489     { "load", do_load },
1490     { "save", do_save },
1491     { "panel-load", do_panel_load },
1492     { "panel-mouse-click", do_panel_mouse_click },
1493     { "preferences-load", do_preferences_load },
1494     { "view-cycle", do_view_cycle },
1495     { "screen-capture", do_screen_capture },
1496     { "hires-screen-capture", do_hires_screen_capture },
1497     { "tile-cache-reload", do_tile_cache_reload },
1498     { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1499     { "set-outside-air-temp-degc", do_set_oat_degc },
1500     { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1501     { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1502     { "timeofday", do_timeofday },
1503     { "property-toggle", do_property_toggle },
1504     { "property-assign", do_property_assign },
1505     { "property-adjust", do_property_adjust },
1506     { "property-multiply", do_property_multiply },
1507     { "property-swap", do_property_swap },
1508     { "property-scale", do_property_scale },
1509     { "property-cycle", do_property_cycle },
1510     { "property-randomize", do_property_randomize },
1511     { "data-logging-commit", do_data_logging_commit },
1512     { "dialog-new", do_dialog_new },
1513     { "dialog-show", do_dialog_show },
1514     { "dialog-close", do_dialog_close },
1515     { "dialog-update", do_dialog_update },
1516     { "dialog-apply", do_dialog_apply },
1517     { "gui-redraw", do_gui_redraw },
1518     { "set-cursor", do_set_cursor },
1519     { "play-audio-sample", do_play_audio_sample },
1520     { "presets-commit", do_presets_commit },
1521     { "log-level", do_log_level },
1522     { "replay", do_replay },
1523     { "decrease-visibility", do_decrease_visibility },
1524     { "increase-visibility", do_increase_visibility },
1525     { "hud-init", do_hud_init },
1526     { "hud-init2", do_hud_init2 },
1527     { "loadxml", do_load_xml_to_proptree},
1528     { "savexml", do_save_xml_from_proptree },
1529     { "press-cockpit-button", do_press_cockpit_button },
1530     { "release-cockpit-button", do_release_cockpit_button },
1531     { "dump-scenegraph", do_dump_scene_graph },
1532     { "dump-terrainbranch", do_dump_terrain_branch },
1533     { 0, 0 }                    // zero-terminated
1534 };
1535
1536
1537 /**
1538  * Initialize the default built-in commands.
1539  *
1540  * Other commands may be added by other parts of the application.
1541  */
1542 void
1543 fgInitCommands ()
1544 {
1545   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1546   for (int i = 0; built_ins[i].name != 0; i++) {
1547     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
1548     globals->get_commands()->addCommand(built_ins[i].name,
1549                                         built_ins[i].command);
1550   }
1551
1552   typedef bool (*dummy)();
1553   fgTie( "/command/view/next", dummy(0), do_view_next );
1554   fgTie( "/command/view/prev", dummy(0), do_view_prev );
1555 }
1556
1557 // end of fg_commands.cxx