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