1 // fg_commands.cxx - internal FGFS commands.
7 #include <string.h> // strcmp()
9 #include <simgear/compiler.h>
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>
28 #include <Cockpit/panel.hxx>
29 #include <Cockpit/panel_io.hxx>
30 #include <FDM/flight.hxx>
32 #include <GUI/new_gui.hxx>
33 #include <GUI/dialog.hxx>
34 #include <Aircraft/replay.hxx>
35 #include <Scenery/scenery.hxx>
36 #include <Scripting/NasalSys.hxx>
37 #include <Sound/sample_queue.hxx>
38 #include <Airports/xmlloader.hxx>
39 #include <ATC/CommStation.hxx>
40 #include <Navaids/navrecord.hxx>
41 #include <Navaids/navlist.hxx>
43 #include "fg_init.hxx"
46 #include "fg_commands.hxx"
47 #include "fg_props.hxx"
48 #include "globals.hxx"
51 #include "viewmgr.hxx"
53 #include <Main/viewer.hxx>
54 #include <Environment/presets.hxx>
56 #include <boost/scoped_array.hpp>
64 ////////////////////////////////////////////////////////////////////////
65 // Static helper functions.
66 ////////////////////////////////////////////////////////////////////////
69 static inline SGPropertyNode *
70 get_prop (const SGPropertyNode * arg)
72 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
75 static inline SGPropertyNode *
76 get_prop2 (const SGPropertyNode * arg)
78 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
83 * Get a double value and split it as required.
86 split_value (double full_value, const char * mask,
87 double * unmodifiable, double * modifiable)
89 if (!strcmp("integer", mask)) {
90 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
91 *unmodifiable = full_value - *modifiable;
92 } else if (!strcmp("decimal", mask)) {
93 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
94 *modifiable = full_value - *unmodifiable;
96 if (strcmp("all", mask))
97 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
98 << " assuming 'all'");
100 *modifiable = full_value;
106 * Clamp or wrap a value as specified.
109 limit_value (double * value, const SGPropertyNode * arg)
111 const SGPropertyNode * min_node = arg->getChild("min");
112 const SGPropertyNode * max_node = arg->getChild("max");
114 bool wrap = arg->getBoolValue("wrap");
116 if (min_node == 0 || max_node == 0)
119 if (wrap) { // wrap such that min <= x < max
120 double min_val = min_node->getDoubleValue();
121 double max_val = max_node->getDoubleValue();
122 double resolution = arg->getDoubleValue("resolution");
123 if (resolution > 0.0) {
124 // snap to (min + N*resolution), taking special care to handle imprecision
125 int n = (int)floor((*value - min_val) / resolution + 0.5);
126 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
127 SG_NORMALIZE_RANGE(n, 0, steps);
128 *value = min_val + resolution * n;
130 // plain circular wrapping
131 SG_NORMALIZE_RANGE(*value, min_val, max_val);
133 } else { // clamp such that min <= x <= max
134 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
135 *value = min_node->getDoubleValue();
136 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
137 *value = max_node->getDoubleValue();
142 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
144 switch (value1->getType()) {
145 case simgear::props::BOOL:
146 return (value1->getBoolValue() == value2->getBoolValue());
147 case simgear::props::INT:
148 return (value1->getIntValue() == value2->getIntValue());
149 case simgear::props::LONG:
150 return (value1->getLongValue() == value2->getLongValue());
151 case simgear::props::FLOAT:
152 return (value1->getFloatValue() == value2->getFloatValue());
153 case simgear::props::DOUBLE:
154 return (value1->getDoubleValue() == value2->getDoubleValue());
156 return !strcmp(value1->getStringValue(), value2->getStringValue());
162 ////////////////////////////////////////////////////////////////////////
163 // Command implementations.
164 ////////////////////////////////////////////////////////////////////////
168 * Built-in command: do nothing.
171 do_null (const SGPropertyNode * arg)
177 * Built-in command: run a Nasal script.
180 do_nasal (const SGPropertyNode * arg)
182 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
186 * Built-in command: exit FlightGear.
188 * status: the exit status to return to the operating system (defaults to 0)
191 do_exit (const SGPropertyNode * arg)
193 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
194 fgSetBool("/sim/signals/exit", true);
195 globals->saveUserSettings();
196 fgOSExit(arg->getIntValue("status", 0));
202 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
205 do_reset (const SGPropertyNode * arg)
207 fgReInitSubsystems();
213 * Built-in command: reinitialize one or more subsystems.
215 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
216 * none is specified, reinitialize all of them.
219 do_reinit (const SGPropertyNode * arg)
223 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
224 if (subsystems.size() == 0) {
225 globals->get_subsystem_mgr()->reinit();
227 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
228 const char * name = subsystems[i]->getStringValue();
229 SGSubsystem * subsystem = globals->get_subsystem(name);
230 if (subsystem == 0) {
232 SG_LOG( SG_GENERAL, SG_ALERT,
233 "Subsystem " << name << " not found" );
240 globals->get_event_mgr()->reinit();
247 // these routines look useful ??? but are never used in the code ???
251 * Built-in command: suspend one or more subsystems.
253 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
256 do_suspend (const SGPropertyNode * arg)
260 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
261 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
262 const char * name = subsystems[i]->getStringValue();
263 SGSubsystem * subsystem = globals->get_subsystem(name);
264 if (subsystem == 0) {
266 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
268 subsystem->suspend();
275 * Built-in command: suspend one or more subsystems.
277 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
280 do_resume (const SGPropertyNode * arg)
284 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
285 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
286 const char * name = subsystems[i]->getStringValue();
287 SGSubsystem * subsystem = globals->get_subsystem(name);
288 if (subsystem == 0) {
290 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
301 * Built-in command: replay the FDR buffer
304 do_replay (const SGPropertyNode * arg)
306 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
311 * Built-in command: pause/unpause the sim
314 do_pause (const SGPropertyNode * arg)
316 bool paused = fgGetBool("/sim/freeze/master",true) || fgGetBool("/sim/freeze/clock",true);
317 if (paused && (fgGetInt("/sim/freeze/replay-state",0)>0))
323 fgSetBool("/sim/freeze/master",!paused);
324 fgSetBool("/sim/freeze/clock",!paused);
330 * Built-in command: load flight.
332 * file (optional): the name of the file to load (relative to current
333 * directory). Defaults to "fgfs.sav"
336 do_load (const SGPropertyNode * arg)
338 string file = arg->getStringValue("file", "fgfs.sav");
339 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
342 if (!fgValidatePath(file.c_str(), false)) {
343 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
344 "(unauthorized access)");
348 ifstream input(file.c_str());
349 if (input.good() && fgLoadFlight(input)) {
351 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
354 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
361 * Built-in command: save flight.
363 * file (optional): the name of the file to save (relative to the
364 * current directory). Defaults to "fgfs.sav".
367 do_save (const SGPropertyNode * arg)
369 string file = arg->getStringValue("file", "fgfs.sav");
370 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
373 if (!fgValidatePath(file.c_str(), false)) {
374 SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
375 "(unauthorized access)");
379 bool write_all = arg->getBoolValue("write-all", false);
380 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
381 ofstream output(file.c_str());
382 if (output.good() && fgSaveFlight(output, write_all)) {
384 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
387 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
394 * Built-in command: (re)load the panel.
396 * path (optional): the file name to load the panel from
397 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
398 * and if that's unspecified, to "Panels/Default/default.xml".
401 do_panel_load (const SGPropertyNode * arg)
404 arg->getStringValue("path", fgGetString("/sim/panel/path"));
405 if (panel_path.empty()) {
409 FGPanel * new_panel = fgReadPanel(panel_path);
410 if (new_panel == 0) {
411 SG_LOG(SG_INPUT, SG_ALERT,
412 "Error reading new panel from " << panel_path);
415 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
416 globals->get_current_panel()->unbind();
417 globals->set_current_panel( new_panel );
418 globals->get_current_panel()->bind();
424 * Built-in command: pass a mouse click to the panel.
426 * button: the mouse button number, zero-based.
427 * is-down: true if the button is down, false if it is up.
428 * x-pos: the x position of the mouse click.
429 * y-pos: the y position of the mouse click.
432 do_panel_mouse_click (const SGPropertyNode * arg)
434 if (globals->get_current_panel() != 0)
435 return globals->get_current_panel()
436 ->doMouseAction(arg->getIntValue("button"),
437 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
438 arg->getIntValue("x-pos"),
439 arg->getIntValue("y-pos"));
446 * Built-in command: (re)load preferences.
448 * path (optional): the file name to load the panel from (relative
449 * to FG_ROOT). Defaults to "preferences.xml".
452 do_preferences_load (const SGPropertyNode * arg)
455 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
456 globals->get_props());
457 } catch (const sg_exception &e) {
458 guiErrorMessage("Error reading global preferences: ", e);
461 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
468 globals->get_current_view()->setHeadingOffset_deg(0.0);
469 globals->get_viewmgr()->next_view();
475 globals->get_current_view()->setHeadingOffset_deg(0.0);
476 globals->get_viewmgr()->prev_view();
480 * Built-in command: cycle view.
483 do_view_cycle (const SGPropertyNode * arg)
485 globals->get_current_view()->setHeadingOffset_deg(0.0);
486 globals->get_viewmgr()->next_view();
491 * Built-in command: capture screen.
494 do_screen_capture (const SGPropertyNode * arg)
496 return fgDumpSnapShot();
500 do_reload_shaders (const SGPropertyNode*)
502 simgear::reload_shaders();
507 do_dump_scene_graph (const SGPropertyNode*)
514 do_dump_terrain_branch (const SGPropertyNode*)
516 fgDumpTerrainBranch();
518 double lon_deg = fgGetDouble("/position/longitude-deg");
519 double lat_deg = fgGetDouble("/position/latitude-deg");
520 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
521 SGVec3d zero = SGVec3d::fromGeod(geodPos);
523 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
524 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
525 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
531 do_print_visible_scene_info(const SGPropertyNode*)
533 fgPrintVisibleSceneInfoCommand();
538 * Built-in command: hires capture screen.
541 do_hires_screen_capture (const SGPropertyNode * arg)
549 * Reload the tile cache.
552 do_tile_cache_reload (const SGPropertyNode * arg)
554 static const SGPropertyNode *master_freeze
555 = fgGetNode("/sim/freeze/master");
556 bool freeze = master_freeze->getBoolValue();
557 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
559 fgSetBool("/sim/freeze/master", true);
562 globals->get_subsystem("tile-manager")->reinit();
565 fgSetBool("/sim/freeze/master", false);
572 These do_set_(some-environment-parameters) are deprecated and no longer
573 useful/functional - Torsten Dreyer, January 2011
575 * Set the sea level outside air temperature and assigning that to all
576 * boundary and aloft environment layers.
579 do_set_sea_level_degc ( double temp_sea_level_degc)
581 SGPropertyNode *node, *child;
584 node = fgGetNode( "/environment/config/boundary" );
585 if ( node != NULL ) {
587 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
588 child->setDoubleValue( "temperature-sea-level-degc",
589 temp_sea_level_degc );
595 node = fgGetNode( "/environment/config/aloft" );
596 if ( node != NULL ) {
598 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
599 child->setDoubleValue( "temperature-sea-level-degc",
600 temp_sea_level_degc );
609 do_set_sea_level_degc (const SGPropertyNode * arg)
611 return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
616 * Set the outside air temperature at the "current" altitude by first
617 * calculating the corresponding sea level temp, and assigning that to
618 * all boundary and aloft environment layers.
621 do_set_oat_degc (const SGPropertyNode * arg)
623 double oat_degc = arg->getDoubleValue("temp-degc", 15.0);
624 // check for an altitude specified in the arguments, otherwise use
625 // current aircraft altitude.
626 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
627 if ( altitude_ft == NULL ) {
628 altitude_ft = fgGetNode("/position/altitude-ft");
631 FGEnvironment dummy; // instantiate a dummy so we can leech a method
632 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
633 dummy.set_temperature_degc( oat_degc );
634 return do_set_sea_level_degc( dummy.get_temperature_sea_level_degc());
638 * Set the sea level outside air dewpoint and assigning that to all
639 * boundary and aloft environment layers.
642 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
645 SGPropertyNode *node, *child;
648 node = fgGetNode( "/environment/config/boundary" );
649 if ( node != NULL ) {
651 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
652 child->setDoubleValue( "dewpoint-sea-level-degc",
653 dewpoint_sea_level_degc );
659 node = fgGetNode( "/environment/config/aloft" );
660 if ( node != NULL ) {
662 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
663 child->setDoubleValue( "dewpoint-sea-level-degc",
664 dewpoint_sea_level_degc );
673 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
675 return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
679 * Set the outside air dewpoint at the "current" altitude by first
680 * calculating the corresponding sea level dewpoint, and assigning
681 * that to all boundary and aloft environment layers.
684 do_set_dewpoint_degc (const SGPropertyNode * arg)
686 double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
688 // check for an altitude specified in the arguments, otherwise use
689 // current aircraft altitude.
690 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
691 if ( altitude_ft == NULL ) {
692 altitude_ft = fgGetNode("/position/altitude-ft");
695 FGEnvironment dummy; // instantiate a dummy so we can leech a method
696 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
697 dummy.set_dewpoint_degc( dewpoint_degc );
698 return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
703 * Built-in command: toggle a bool property value.
705 * property: The name of the property to toggle.
708 do_property_toggle (const SGPropertyNode * arg)
710 SGPropertyNode * prop = get_prop(arg);
711 return prop->setBoolValue(!prop->getBoolValue());
716 * Built-in command: assign a value to a property.
718 * property: the name of the property to assign.
719 * value: the value to assign; or
720 * property[1]: the property to copy from.
723 do_property_assign (const SGPropertyNode * arg)
725 SGPropertyNode * prop = get_prop(arg);
726 const SGPropertyNode * value = arg->getNode("value");
729 return prop->setUnspecifiedValue(value->getStringValue());
732 const SGPropertyNode * prop2 = get_prop2(arg);
734 return prop->setUnspecifiedValue(prop2->getStringValue());
742 * Built-in command: increment or decrement a property value.
744 * If the 'step' argument is present, it will be used; otherwise,
745 * the command uses 'offset' and 'factor', usually from the mouse.
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
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:
762 do_property_adjust (const SGPropertyNode * arg)
764 SGPropertyNode * prop = get_prop(arg);
767 if (arg->hasValue("step"))
768 amount = arg->getDoubleValue("step");
770 amount = (arg->getDoubleValue("factor", 1)
771 * arg->getDoubleValue("offset"));
773 double unmodifiable, modifiable;
774 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
775 &unmodifiable, &modifiable);
776 modifiable += amount;
777 limit_value(&modifiable, arg);
779 prop->setDoubleValue(unmodifiable + modifiable);
786 * Built-in command: multiply a property value.
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:
800 do_property_multiply (const SGPropertyNode * arg)
802 SGPropertyNode * prop = get_prop(arg);
803 double factor = arg->getDoubleValue("factor", 1);
805 double unmodifiable, modifiable;
806 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
807 &unmodifiable, &modifiable);
808 modifiable *= factor;
809 limit_value(&modifiable, arg);
811 prop->setDoubleValue(unmodifiable + modifiable);
818 * Built-in command: swap two property values.
820 * property[0]: the name of the first property.
821 * property[1]: the name of the second property.
824 do_property_swap (const SGPropertyNode * arg)
826 SGPropertyNode * prop1 = get_prop(arg);
827 SGPropertyNode * prop2 = get_prop2(arg);
829 // FIXME: inefficient
830 const string & tmp = prop1->getStringValue();
831 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
832 prop2->setUnspecifiedValue(tmp.c_str()));
837 * Built-in command: Set a property to an axis or other moving input.
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).
845 do_property_scale (const SGPropertyNode * arg)
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));
854 int sign = (setting < 0 ? -1 : 1);
860 setting = setting * setting * sign;
863 setting = setting * setting * setting;
866 setting = setting * setting * setting * setting * sign;
869 setting = pow(setting, power);
870 if ((power % 2) == 0)
875 return prop->setDoubleValue((setting + offset) * factor);
880 * Built-in command: cycle a property through a set of values.
882 * If the current value isn't in the list, the cycle will
883 * (re)start from the beginning.
885 * property: the name of the property to cycle.
886 * value[*]: the list of values to cycle through.
889 do_property_cycle (const SGPropertyNode * arg)
891 SGPropertyNode * prop = get_prop(arg);
892 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
894 int nSelections = values.size();
896 if (nSelections < 1) {
897 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
901 // Try to find the current selection
902 for (int i = 0; i < nSelections; i++) {
903 if (compare_values(prop, values[i])) {
909 // Default or wrap to the first selection
910 if (selection < 0 || selection >= nSelections)
913 prop->setUnspecifiedValue(values[selection]->getStringValue());
919 * Built-in command: randomize a numeric property value.
921 * property: the name of the property value to randomize.
922 * min: the minimum allowed value.
923 * max: the maximum allowed value.
926 do_property_randomize (const SGPropertyNode * arg)
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);
936 * Built-in command: interpolate a property value over time
938 * property: the name of the property value to interpolate.
939 * value[0..n] any number of constant values to interpolate
940 * time[0..n] time between each value, number of time elements must
941 * match those of value elements
943 * property[1..n] any number of target values taken from named properties
944 * time[0..n] time between each value, number of time elements must
945 * match those of property elements minus one
948 do_property_interpolate (const SGPropertyNode * arg)
950 SGPropertyNode * prop = get_prop(arg);
952 simgear::PropertyList valueNodes = arg->getChildren( "value" );
953 simgear::PropertyList timeNodes = arg->getChildren( "time" );
955 boost::scoped_array<double> value;
956 boost::scoped_array<double> time;
958 if( valueNodes.size() > 0 ) {
960 if( timeNodes.size() != valueNodes.size() )
963 value.reset( new double[valueNodes.size()] );
964 for( simgear::PropertyList::size_type n = 0; n < valueNodes.size(); n++ ) {
965 value[n] = valueNodes[n]->getDoubleValue();
968 valueNodes = arg->getChildren("property");
969 // must have one more property node
970 if( valueNodes.size() - 1 != timeNodes.size() )
973 value.reset( new double[valueNodes.size()-1] );
974 for( simgear::PropertyList::size_type n = 0; n < valueNodes.size()-1; n++ ) {
975 value[n] = fgGetNode(valueNodes[n+1]->getStringValue(), "/null")->getDoubleValue();
980 time.reset( new double[timeNodes.size()] );
981 for( simgear::PropertyList::size_type n = 0; n < timeNodes.size(); n++ ) {
982 time[n] = timeNodes[n]->getDoubleValue();
985 ((SGInterpolator*)globals->get_subsystem_mgr()
986 ->get_group(SGSubsystemMgr::INIT)->get_subsystem("interpolator"))
987 ->interpolate(prop, timeNodes.size(), value.get(), time.get() );
993 * Built-in command: reinit the data logging system based on the
994 * current contents of the /logger tree.
997 do_data_logging_commit (const SGPropertyNode * arg)
999 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1005 * Built-in command: Add a dialog to the GUI system. Does *not*
1006 * display the dialog. The property node should have the same format
1007 * as a dialog XML configuration. It must include:
1009 * name: the name of the GUI dialog for future reference.
1012 do_dialog_new (const SGPropertyNode * arg)
1014 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1016 // Note the casting away of const: this is *real*. Doing a
1017 // "dialog-apply" command later on will mutate this property node.
1018 // I'm not convinced that this isn't the Right Thing though; it
1019 // allows client to create a node, pass it to dialog-new, and get
1020 // the values back from the dialog by reading the same node.
1021 // Perhaps command arguments are not as "const" as they would
1023 gui->newDialog((SGPropertyNode*)arg);
1028 * Built-in command: Show an XML-configured dialog.
1030 * dialog-name: the name of the GUI dialog to display.
1033 do_dialog_show (const SGPropertyNode * arg)
1035 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1036 gui->showDialog(arg->getStringValue("dialog-name"));
1042 * Built-in Command: Hide the active XML-configured dialog.
1045 do_dialog_close (const SGPropertyNode * arg)
1047 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1048 if(arg->hasValue("dialog-name"))
1049 return gui->closeDialog(arg->getStringValue("dialog-name"));
1050 return gui->closeActiveDialog();
1055 * Update a value in the active XML-configured dialog.
1057 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1060 do_dialog_update (const SGPropertyNode * arg)
1062 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1064 if (arg->hasValue("dialog-name"))
1065 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1067 dialog = gui->getActiveDialog();
1070 dialog->updateValues(arg->getStringValue("object-name"));
1078 do_open_browser (const SGPropertyNode * arg)
1081 if (arg->hasValue("path"))
1082 path = arg->getStringValue("path");
1084 if (arg->hasValue("url"))
1085 path = arg->getStringValue("url");
1089 return openBrowser(path);
1093 * Apply a value in the active XML-configured dialog.
1095 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1098 do_dialog_apply (const SGPropertyNode * arg)
1100 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1102 if (arg->hasValue("dialog-name"))
1103 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1105 dialog = gui->getActiveDialog();
1108 dialog->applyValues(arg->getStringValue("object-name"));
1117 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1121 do_gui_redraw (const SGPropertyNode * arg)
1123 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1130 * Adds model to the scenery. The path to the added branch (/models/model[*])
1131 * is returned in property "property".
1134 do_add_model (const SGPropertyNode * arg)
1136 SGPropertyNode * model = fgGetNode("models", true);
1138 for (i = 0; model->hasChild("model",i); i++);
1139 model = model->getChild("model", i, true);
1140 copyProperties(arg, model);
1141 if (model->hasValue("elevation-m"))
1142 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1143 * SG_METER_TO_FEET);
1144 model->getNode("load", true);
1145 model->removeChildren("load");
1146 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1152 * Set mouse cursor coordinates and cursor shape.
1155 do_set_cursor (const SGPropertyNode * arg)
1157 if (arg->hasValue("x") || arg->hasValue("y")) {
1158 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1159 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1160 int x = arg->getIntValue("x", mx->getIntValue());
1161 int y = arg->getIntValue("y", my->getIntValue());
1167 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1168 if (cursor->getType() != simgear::props::NONE)
1169 fgSetMouseCursor(cursor->getIntValue());
1171 cursor->setIntValue(fgGetMouseCursor());
1177 * Built-in command: play an audio message (i.e. a wav file) This is
1178 * fire and forget. Call this once per message and it will get dumped
1179 * into a queue. Messages are played sequentially so they do not
1183 do_play_audio_sample (const SGPropertyNode * arg)
1185 string path = arg->getStringValue("path");
1186 string file = arg->getStringValue("file");
1187 float volume = arg->getFloatValue("volume");
1188 // cout << "playing " << path << " / " << file << endl;
1190 static FGSampleQueue *queue = 0;
1192 SGSoundMgr *smgr = globals->get_soundmgr();
1193 queue = new FGSampleQueue(smgr, "chatter");
1194 queue->tie_to_listener();
1197 SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1198 msg->set_volume( volume );
1203 } catch (const sg_io_exception&) {
1204 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1205 "failed to load" << path << '/' << file);
1211 * Built-in command: commit presets (read from in /sim/presets/)
1214 do_presets_commit (const SGPropertyNode * arg)
1216 if (fgGetBool("/sim/initialized", false)) {
1217 fgReInitSubsystems();
1219 // Nasal can trigger this during initial init, which confuses
1220 // the logic in ReInitSubsystems, since initial state has not been
1221 // saved at that time. Short-circuit everything here.
1229 * Built-in command: set log level (0 ... 7)
1232 do_log_level (const SGPropertyNode * arg)
1234 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1241 do_decrease_visibility (const SGPropertyNode * arg)
1243 Environment::Presets::VisibilitySingleton::instance()->adjust( 0.9 );
1248 do_increase_visibility (const SGPropertyNode * arg)
1250 Environment::Presets::VisibilitySingleton::instance()->adjust( 1.1 );
1255 * An fgcommand to allow loading of xml files via nasal,
1256 * the xml file's structure will be made available within
1257 * a property tree node defined under argument "targetnode",
1258 * or in the given argument tree under "data" otherwise.
1260 * @param filename a string to hold the complete path & filename of an XML file
1261 * @param targetnode a string pointing to a location within the property tree
1262 * where to store the parsed XML file. If <targetnode> is undefined, then the
1263 * file contents are stored under a node <data> in the argument tree.
1267 do_load_xml_to_proptree(const SGPropertyNode * arg)
1269 SGPath file(arg->getStringValue("filename"));
1270 if (file.str().empty())
1273 if (file.extension() != "xml")
1274 file.concat(".xml");
1276 std::string icao = arg->getStringValue("icao");
1278 if (file.isRelative()) {
1279 SGPath absPath = globals->resolve_maybe_aircraft_path(file.str());
1280 if (!absPath.isNull())
1284 SG_LOG(SG_IO, SG_ALERT, "loadxml: Cannot find XML property file '"
1285 << file.str() << "'.");
1290 if (!XMLLoader::findAirportData(icao, file.str(), file)) {
1291 SG_LOG(SG_IO, SG_INFO, "loadxml: failed to find airport data for "
1292 << file.str() << " at ICAO:" << icao);
1297 if (!fgValidatePath(file.c_str(), false)) {
1298 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1299 "(unauthorized access)");
1303 SGPropertyNode *targetnode;
1304 if (arg->hasValue("targetnode"))
1305 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1307 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1310 readProperties(file.c_str(), targetnode, true);
1311 } catch (const sg_exception &e) {
1312 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1321 * An fgcommand to allow saving of xml files via nasal,
1322 * the file's structure will be determined based on what's
1323 * encountered in the passed (source) property tree node
1325 * @param filename a string to hold the complete path & filename of the (new)
1327 * @param sourcenode a string pointing to a location within the property tree
1328 * where to find the nodes that should be written recursively into an XML file
1329 * @param data if no sourcenode is given, then the file contents are taken from
1330 * the argument tree's "data" node.
1334 do_save_xml_from_proptree(const SGPropertyNode * arg)
1336 SGPath file(arg->getStringValue("filename"));
1337 if (file.str().empty())
1340 if (file.extension() != "xml")
1341 file.concat(".xml");
1343 if (!fgValidatePath(file.c_str(), true)) {
1344 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1345 "(unauthorized access)");
1349 SGPropertyNode *sourcenode;
1350 if (arg->hasValue("sourcenode"))
1351 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1352 else if (arg->getNode("data", false))
1353 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1358 writeProperties (file.c_str(), sourcenode, true);
1359 } catch (const sg_exception &e) {
1360 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1368 do_press_cockpit_button (const SGPropertyNode *arg)
1370 const char *prefix = arg->getStringValue("prefix");
1372 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1375 string prop = string(prefix) + "-button";
1378 if (arg->getBoolValue("latching"))
1379 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1383 fgSetDouble(prop.c_str(), value);
1384 fgSetBool(arg->getStringValue("discrete"), value > 0);
1390 do_release_cockpit_button (const SGPropertyNode *arg)
1392 const char *prefix = arg->getStringValue("prefix");
1394 if (arg->getBoolValue("guarded")) {
1395 string prop = string(prefix) + "-guard";
1396 if (fgGetDouble(prop.c_str()) < 1) {
1397 fgSetDouble(prop.c_str(), 1);
1402 if (! arg->getBoolValue("latching")) {
1403 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1404 fgSetBool(arg->getStringValue("discrete"), false);
1410 static SGGeod commandSearchPos(const SGPropertyNode* arg)
1412 if (arg->hasChild("longitude-deg") && arg->hasChild("latitude-deg")) {
1413 return SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
1414 arg->getDoubleValue("latitude-deg"));
1417 // use current viewer/aircraft position
1418 return SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
1419 fgGetDouble("/position/latitude-deg"));
1423 do_comm_search(const SGPropertyNode* arg)
1425 SGGeod pos = commandSearchPos(arg);
1426 int khz = static_cast<int>(arg->getDoubleValue("frequency-mhz") * 100.0 + 0.25);
1428 flightgear::CommStation* sta = flightgear::CommStation::findByFreq(khz, pos, NULL);
1433 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1434 sta->createBinding(result);
1439 do_nav_search(const SGPropertyNode* arg)
1441 SGGeod pos = commandSearchPos(arg);
1442 double mhz = arg->getDoubleValue("frequency-mhz");
1444 FGNavList* navList = globals->get_navlist();
1445 string type(arg->getStringValue("type", "vor"));
1446 if (type == "dme") {
1447 navList = globals->get_dmelist();
1448 } else if (type == "tacan") {
1449 navList = globals->get_tacanlist();
1452 FGNavRecord* nav = navList->findByFreq(mhz, pos);
1453 if (!nav && (type == "vor")) {
1454 // if we're searching VORs, look for localizers too
1455 nav = globals->get_loclist()->findByFreq(mhz, pos);
1462 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1463 nav->createBinding(result);
1467 ////////////////////////////////////////////////////////////////////////
1469 ////////////////////////////////////////////////////////////////////////
1473 * Table of built-in commands.
1475 * New commands do not have to be added here; any module in the application
1476 * can add a new command using globals->get_commands()->addCommand(...).
1480 SGCommandMgr::command_t command;
1482 { "null", do_null },
1483 { "nasal", do_nasal },
1484 { "exit", do_exit },
1485 { "reset", do_reset },
1486 { "reinit", do_reinit },
1487 { "suspend", do_reinit },
1488 { "resume", do_reinit },
1489 { "pause", do_pause },
1490 { "load", do_load },
1491 { "save", do_save },
1492 { "panel-load", do_panel_load },
1493 { "panel-mouse-click", do_panel_mouse_click },
1494 { "preferences-load", do_preferences_load },
1495 { "view-cycle", do_view_cycle },
1496 { "screen-capture", do_screen_capture },
1497 { "hires-screen-capture", do_hires_screen_capture },
1498 { "tile-cache-reload", do_tile_cache_reload },
1500 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1501 { "set-outside-air-temp-degc", do_set_oat_degc },
1502 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1503 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1505 { "property-toggle", do_property_toggle },
1506 { "property-assign", do_property_assign },
1507 { "property-adjust", do_property_adjust },
1508 { "property-multiply", do_property_multiply },
1509 { "property-swap", do_property_swap },
1510 { "property-scale", do_property_scale },
1511 { "property-cycle", do_property_cycle },
1512 { "property-randomize", do_property_randomize },
1513 { "property-interpolate", do_property_interpolate },
1514 { "data-logging-commit", do_data_logging_commit },
1515 { "dialog-new", do_dialog_new },
1516 { "dialog-show", do_dialog_show },
1517 { "dialog-close", do_dialog_close },
1518 { "dialog-update", do_dialog_update },
1519 { "dialog-apply", do_dialog_apply },
1520 { "open-browser", do_open_browser },
1521 { "gui-redraw", do_gui_redraw },
1522 { "add-model", do_add_model },
1523 { "set-cursor", do_set_cursor },
1524 { "play-audio-sample", do_play_audio_sample },
1525 { "presets-commit", do_presets_commit },
1526 { "log-level", do_log_level },
1527 { "replay", do_replay },
1529 { "decrease-visibility", do_decrease_visibility },
1530 { "increase-visibility", do_increase_visibility },
1532 { "loadxml", do_load_xml_to_proptree},
1533 { "savexml", do_save_xml_from_proptree },
1534 { "press-cockpit-button", do_press_cockpit_button },
1535 { "release-cockpit-button", do_release_cockpit_button },
1536 { "dump-scenegraph", do_dump_scene_graph },
1537 { "dump-terrainbranch", do_dump_terrain_branch },
1538 { "print-visible-scene", do_print_visible_scene_info },
1539 { "reload-shaders", do_reload_shaders },
1541 { "find-navaid", do_nav_search },
1542 { "find-comm", do_comm_search },
1544 { 0, 0 } // zero-terminated
1549 * Initialize the default built-in commands.
1551 * Other commands may be added by other parts of the application.
1556 SG_LOG(SG_GENERAL, SG_BULK, "Initializing basic built-in commands:");
1557 for (int i = 0; built_ins[i].name != 0; i++) {
1558 SG_LOG(SG_GENERAL, SG_BULK, " " << built_ins[i].name);
1559 globals->get_commands()->addCommand(built_ins[i].name,
1560 built_ins[i].command);
1563 typedef bool (*dummy)();
1564 fgTie( "/command/view/next", dummy(0), do_view_next );
1565 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1568 // end of fg_commands.cxx