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