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