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 delete globals->get_current_panel();
418 globals->set_current_panel( new_panel );
419 globals->get_current_panel()->bind();
425 * Built-in command: pass a mouse click to the panel.
427 * button: the mouse button number, zero-based.
428 * is-down: true if the button is down, false if it is up.
429 * x-pos: the x position of the mouse click.
430 * y-pos: the y position of the mouse click.
433 do_panel_mouse_click (const SGPropertyNode * arg)
435 if (globals->get_current_panel() != 0)
436 return globals->get_current_panel()
437 ->doMouseAction(arg->getIntValue("button"),
438 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
439 arg->getIntValue("x-pos"),
440 arg->getIntValue("y-pos"));
447 * Built-in command: (re)load preferences.
449 * path (optional): the file name to load the panel from (relative
450 * to FG_ROOT). Defaults to "preferences.xml".
453 do_preferences_load (const SGPropertyNode * arg)
456 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
457 globals->get_props());
458 } catch (const sg_exception &e) {
459 guiErrorMessage("Error reading global preferences: ", e);
462 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
469 globals->get_current_view()->setHeadingOffset_deg(0.0);
470 globals->get_viewmgr()->next_view();
476 globals->get_current_view()->setHeadingOffset_deg(0.0);
477 globals->get_viewmgr()->prev_view();
481 * Built-in command: cycle view.
484 do_view_cycle (const SGPropertyNode * arg)
486 globals->get_current_view()->setHeadingOffset_deg(0.0);
487 globals->get_viewmgr()->next_view();
492 * Built-in command: capture screen.
495 do_screen_capture (const SGPropertyNode * arg)
497 return fgDumpSnapShot();
501 do_reload_shaders (const SGPropertyNode*)
503 simgear::reload_shaders();
508 do_dump_scene_graph (const SGPropertyNode*)
515 do_dump_terrain_branch (const SGPropertyNode*)
517 fgDumpTerrainBranch();
519 double lon_deg = fgGetDouble("/position/longitude-deg");
520 double lat_deg = fgGetDouble("/position/latitude-deg");
521 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
522 SGVec3d zero = SGVec3d::fromGeod(geodPos);
524 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
525 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
526 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
532 do_print_visible_scene_info(const SGPropertyNode*)
534 fgPrintVisibleSceneInfoCommand();
539 * Built-in command: hires capture screen.
542 do_hires_screen_capture (const SGPropertyNode * arg)
550 * Reload the tile cache.
553 do_tile_cache_reload (const SGPropertyNode * arg)
555 static const SGPropertyNode *master_freeze
556 = fgGetNode("/sim/freeze/master");
557 bool freeze = master_freeze->getBoolValue();
558 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
560 fgSetBool("/sim/freeze/master", true);
563 globals->get_subsystem("tile-manager")->reinit();
566 fgSetBool("/sim/freeze/master", false);
573 These do_set_(some-environment-parameters) are deprecated and no longer
574 useful/functional - Torsten Dreyer, January 2011
576 * Set the sea level outside air temperature and assigning that to all
577 * boundary and aloft environment layers.
580 do_set_sea_level_degc ( double temp_sea_level_degc)
582 SGPropertyNode *node, *child;
585 node = fgGetNode( "/environment/config/boundary" );
586 if ( node != NULL ) {
588 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
589 child->setDoubleValue( "temperature-sea-level-degc",
590 temp_sea_level_degc );
596 node = fgGetNode( "/environment/config/aloft" );
597 if ( node != NULL ) {
599 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
600 child->setDoubleValue( "temperature-sea-level-degc",
601 temp_sea_level_degc );
610 do_set_sea_level_degc (const SGPropertyNode * arg)
612 return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
617 * Set the outside air temperature at the "current" altitude by first
618 * calculating the corresponding sea level temp, and assigning that to
619 * all boundary and aloft environment layers.
622 do_set_oat_degc (const SGPropertyNode * arg)
624 double oat_degc = arg->getDoubleValue("temp-degc", 15.0);
625 // check for an altitude specified in the arguments, otherwise use
626 // current aircraft altitude.
627 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
628 if ( altitude_ft == NULL ) {
629 altitude_ft = fgGetNode("/position/altitude-ft");
632 FGEnvironment dummy; // instantiate a dummy so we can leech a method
633 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
634 dummy.set_temperature_degc( oat_degc );
635 return do_set_sea_level_degc( dummy.get_temperature_sea_level_degc());
639 * Set the sea level outside air dewpoint and assigning that to all
640 * boundary and aloft environment layers.
643 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
646 SGPropertyNode *node, *child;
649 node = fgGetNode( "/environment/config/boundary" );
650 if ( node != NULL ) {
652 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
653 child->setDoubleValue( "dewpoint-sea-level-degc",
654 dewpoint_sea_level_degc );
660 node = fgGetNode( "/environment/config/aloft" );
661 if ( node != NULL ) {
663 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
664 child->setDoubleValue( "dewpoint-sea-level-degc",
665 dewpoint_sea_level_degc );
674 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
676 return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
680 * Set the outside air dewpoint at the "current" altitude by first
681 * calculating the corresponding sea level dewpoint, and assigning
682 * that to all boundary and aloft environment layers.
685 do_set_dewpoint_degc (const SGPropertyNode * arg)
687 double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
689 // check for an altitude specified in the arguments, otherwise use
690 // current aircraft altitude.
691 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
692 if ( altitude_ft == NULL ) {
693 altitude_ft = fgGetNode("/position/altitude-ft");
696 FGEnvironment dummy; // instantiate a dummy so we can leech a method
697 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
698 dummy.set_dewpoint_degc( dewpoint_degc );
699 return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
704 * Built-in command: toggle a bool property value.
706 * property: The name of the property to toggle.
709 do_property_toggle (const SGPropertyNode * arg)
711 SGPropertyNode * prop = get_prop(arg);
712 return prop->setBoolValue(!prop->getBoolValue());
717 * Built-in command: assign a value to a property.
719 * property: the name of the property to assign.
720 * value: the value to assign; or
721 * property[1]: the property to copy from.
724 do_property_assign (const SGPropertyNode * arg)
726 SGPropertyNode * prop = get_prop(arg);
727 const SGPropertyNode * value = arg->getNode("value");
730 return prop->setUnspecifiedValue(value->getStringValue());
733 const SGPropertyNode * prop2 = get_prop2(arg);
735 return prop->setUnspecifiedValue(prop2->getStringValue());
743 * Built-in command: increment or decrement a property value.
745 * If the 'step' argument is present, it will be used; otherwise,
746 * the command uses 'offset' and 'factor', usually from the mouse.
748 * property: the name of the property to increment or decrement.
749 * step: the amount of the increment or decrement (default: 0).
750 * offset: offset from the current setting (used for the mouse; multiplied
752 * factor: scaling amount for the offset (defaults to 1).
753 * min: the minimum allowed value (default: no minimum).
754 * max: the maximum allowed value (default: no maximum).
755 * mask: 'integer' to apply only to the left of the decimal point,
756 * 'decimal' to apply only to the right of the decimal point,
757 * or 'all' to apply to the whole number (the default).
758 * wrap: true if the value should be wrapped when it passes min or max;
759 * both min and max must be present for this to work (default:
763 do_property_adjust (const SGPropertyNode * arg)
765 SGPropertyNode * prop = get_prop(arg);
768 if (arg->hasValue("step"))
769 amount = arg->getDoubleValue("step");
771 amount = (arg->getDoubleValue("factor", 1)
772 * arg->getDoubleValue("offset"));
774 double unmodifiable, modifiable;
775 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
776 &unmodifiable, &modifiable);
777 modifiable += amount;
778 limit_value(&modifiable, arg);
780 prop->setDoubleValue(unmodifiable + modifiable);
787 * Built-in command: multiply a property value.
789 * property: the name of the property to multiply.
790 * factor: the amount by which to multiply.
791 * min: the minimum allowed value (default: no minimum).
792 * max: the maximum allowed value (default: no maximum).
793 * mask: 'integer' to apply only to the left of the decimal point,
794 * 'decimal' to apply only to the right of the decimal point,
795 * or 'all' to apply to the whole number (the default).
796 * wrap: true if the value should be wrapped when it passes min or max;
797 * both min and max must be present for this to work (default:
801 do_property_multiply (const SGPropertyNode * arg)
803 SGPropertyNode * prop = get_prop(arg);
804 double factor = arg->getDoubleValue("factor", 1);
806 double unmodifiable, modifiable;
807 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
808 &unmodifiable, &modifiable);
809 modifiable *= factor;
810 limit_value(&modifiable, arg);
812 prop->setDoubleValue(unmodifiable + modifiable);
819 * Built-in command: swap two property values.
821 * property[0]: the name of the first property.
822 * property[1]: the name of the second property.
825 do_property_swap (const SGPropertyNode * arg)
827 SGPropertyNode * prop1 = get_prop(arg);
828 SGPropertyNode * prop2 = get_prop2(arg);
830 // FIXME: inefficient
831 const string & tmp = prop1->getStringValue();
832 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
833 prop2->setUnspecifiedValue(tmp.c_str()));
838 * Built-in command: Set a property to an axis or other moving input.
840 * property: the name of the property to set.
841 * setting: the current input setting, usually between -1.0 and 1.0.
842 * offset: the offset to shift by, before applying the factor.
843 * factor: the factor to multiply by (use negative to reverse).
846 do_property_scale (const SGPropertyNode * arg)
848 SGPropertyNode * prop = get_prop(arg);
849 double setting = arg->getDoubleValue("setting");
850 double offset = arg->getDoubleValue("offset", 0.0);
851 double factor = arg->getDoubleValue("factor", 1.0);
852 bool squared = arg->getBoolValue("squared", false);
853 int power = arg->getIntValue("power", (squared ? 2 : 1));
855 int sign = (setting < 0 ? -1 : 1);
861 setting = setting * setting * sign;
864 setting = setting * setting * setting;
867 setting = setting * setting * setting * setting * sign;
870 setting = pow(setting, power);
871 if ((power % 2) == 0)
876 return prop->setDoubleValue((setting + offset) * factor);
881 * Built-in command: cycle a property through a set of values.
883 * If the current value isn't in the list, the cycle will
884 * (re)start from the beginning.
886 * property: the name of the property to cycle.
887 * value[*]: the list of values to cycle through.
890 do_property_cycle (const SGPropertyNode * arg)
892 SGPropertyNode * prop = get_prop(arg);
893 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
895 int nSelections = values.size();
897 if (nSelections < 1) {
898 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
902 // Try to find the current selection
903 for (int i = 0; i < nSelections; i++) {
904 if (compare_values(prop, values[i])) {
910 // Default or wrap to the first selection
911 if (selection < 0 || selection >= nSelections)
914 prop->setUnspecifiedValue(values[selection]->getStringValue());
920 * Built-in command: randomize a numeric property value.
922 * property: the name of the property value to randomize.
923 * min: the minimum allowed value.
924 * max: the maximum allowed value.
927 do_property_randomize (const SGPropertyNode * arg)
929 SGPropertyNode * prop = get_prop(arg);
930 double min = arg->getDoubleValue("min", DBL_MIN);
931 double max = arg->getDoubleValue("max", DBL_MAX);
932 prop->setDoubleValue(sg_random() * (max - min) + min);
937 * Built-in command: interpolate a property value over time
939 * property: the name of the property value to interpolate.
940 * value[0..n] any number of constant values to interpolate
941 * time[0..n] time between each value, number of time elements must
942 * match those of value elements
944 * property[1..n] any number of target values taken from named properties
945 * time[0..n] time between each value, number of time elements must
946 * match those of property elements minus one
949 do_property_interpolate (const SGPropertyNode * arg)
951 SGPropertyNode * prop = get_prop(arg);
953 simgear::PropertyList valueNodes = arg->getChildren( "value" );
954 simgear::PropertyList timeNodes = arg->getChildren( "time" );
956 boost::scoped_array<double> value;
957 boost::scoped_array<double> time;
959 if( valueNodes.size() > 0 ) {
961 if( timeNodes.size() != valueNodes.size() )
964 value.reset( new double[valueNodes.size()] );
965 for( simgear::PropertyList::size_type n = 0; n < valueNodes.size(); n++ ) {
966 value[n] = valueNodes[n]->getDoubleValue();
969 valueNodes = arg->getChildren("property");
970 // must have one more property node
971 if( valueNodes.size() - 1 != timeNodes.size() )
974 value.reset( new double[valueNodes.size()-1] );
975 for( simgear::PropertyList::size_type n = 0; n < valueNodes.size()-1; n++ ) {
976 value[n] = fgGetNode(valueNodes[n+1]->getStringValue(), "/null")->getDoubleValue();
981 time.reset( new double[timeNodes.size()] );
982 for( simgear::PropertyList::size_type n = 0; n < timeNodes.size(); n++ ) {
983 time[n] = timeNodes[n]->getDoubleValue();
986 ((SGInterpolator*)globals->get_subsystem_mgr()
987 ->get_group(SGSubsystemMgr::INIT)->get_subsystem("interpolator"))
988 ->interpolate(prop, timeNodes.size(), value.get(), time.get() );
994 * Built-in command: reinit the data logging system based on the
995 * current contents of the /logger tree.
998 do_data_logging_commit (const SGPropertyNode * arg)
1000 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1006 * Built-in command: Add a dialog to the GUI system. Does *not*
1007 * display the dialog. The property node should have the same format
1008 * as a dialog XML configuration. It must include:
1010 * name: the name of the GUI dialog for future reference.
1013 do_dialog_new (const SGPropertyNode * arg)
1015 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1017 // Note the casting away of const: this is *real*. Doing a
1018 // "dialog-apply" command later on will mutate this property node.
1019 // I'm not convinced that this isn't the Right Thing though; it
1020 // allows client to create a node, pass it to dialog-new, and get
1021 // the values back from the dialog by reading the same node.
1022 // Perhaps command arguments are not as "const" as they would
1024 gui->newDialog((SGPropertyNode*)arg);
1029 * Built-in command: Show an XML-configured dialog.
1031 * dialog-name: the name of the GUI dialog to display.
1034 do_dialog_show (const SGPropertyNode * arg)
1036 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1037 gui->showDialog(arg->getStringValue("dialog-name"));
1043 * Built-in Command: Hide the active XML-configured dialog.
1046 do_dialog_close (const SGPropertyNode * arg)
1048 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1049 if(arg->hasValue("dialog-name"))
1050 return gui->closeDialog(arg->getStringValue("dialog-name"));
1051 return gui->closeActiveDialog();
1056 * Update a value in the active XML-configured dialog.
1058 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1061 do_dialog_update (const SGPropertyNode * arg)
1063 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1065 if (arg->hasValue("dialog-name"))
1066 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1068 dialog = gui->getActiveDialog();
1071 dialog->updateValues(arg->getStringValue("object-name"));
1079 do_open_browser (const SGPropertyNode * arg)
1082 if (arg->hasValue("path"))
1083 path = arg->getStringValue("path");
1085 if (arg->hasValue("url"))
1086 path = arg->getStringValue("url");
1090 return openBrowser(path);
1094 * Apply a value in the active XML-configured dialog.
1096 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1099 do_dialog_apply (const SGPropertyNode * arg)
1101 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1103 if (arg->hasValue("dialog-name"))
1104 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1106 dialog = gui->getActiveDialog();
1109 dialog->applyValues(arg->getStringValue("object-name"));
1118 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1122 do_gui_redraw (const SGPropertyNode * arg)
1124 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1131 * Adds model to the scenery. The path to the added branch (/models/model[*])
1132 * is returned in property "property".
1135 do_add_model (const SGPropertyNode * arg)
1137 SGPropertyNode * model = fgGetNode("models", true);
1139 for (i = 0; model->hasChild("model",i); i++);
1140 model = model->getChild("model", i, true);
1141 copyProperties(arg, model);
1142 if (model->hasValue("elevation-m"))
1143 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1144 * SG_METER_TO_FEET);
1145 model->getNode("load", true);
1146 model->removeChildren("load");
1147 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1153 * Set mouse cursor coordinates and cursor shape.
1156 do_set_cursor (const SGPropertyNode * arg)
1158 if (arg->hasValue("x") || arg->hasValue("y")) {
1159 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1160 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1161 int x = arg->getIntValue("x", mx->getIntValue());
1162 int y = arg->getIntValue("y", my->getIntValue());
1168 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1169 if (cursor->getType() != simgear::props::NONE)
1170 fgSetMouseCursor(cursor->getIntValue());
1172 cursor->setIntValue(fgGetMouseCursor());
1178 * Built-in command: play an audio message (i.e. a wav file) This is
1179 * fire and forget. Call this once per message and it will get dumped
1180 * into a queue. Messages are played sequentially so they do not
1184 do_play_audio_sample (const SGPropertyNode * arg)
1186 string path = arg->getStringValue("path");
1187 string file = arg->getStringValue("file");
1188 float volume = arg->getFloatValue("volume");
1189 // cout << "playing " << path << " / " << file << endl;
1191 static FGSampleQueue *queue = 0;
1193 SGSoundMgr *smgr = globals->get_soundmgr();
1194 queue = new FGSampleQueue(smgr, "chatter");
1195 queue->tie_to_listener();
1198 SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1199 msg->set_volume( volume );
1204 } catch (const sg_io_exception&) {
1205 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1206 "failed to load" << path << '/' << file);
1212 * Built-in command: commit presets (read from in /sim/presets/)
1215 do_presets_commit (const SGPropertyNode * arg)
1217 if (fgGetBool("/sim/initialized", false)) {
1218 fgReInitSubsystems();
1220 // Nasal can trigger this during initial init, which confuses
1221 // the logic in ReInitSubsystems, since initial state has not been
1222 // saved at that time. Short-circuit everything here.
1230 * Built-in command: set log level (0 ... 7)
1233 do_log_level (const SGPropertyNode * arg)
1235 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1242 do_decrease_visibility (const SGPropertyNode * arg)
1244 Environment::Presets::VisibilitySingleton::instance()->adjust( 0.9 );
1249 do_increase_visibility (const SGPropertyNode * arg)
1251 Environment::Presets::VisibilitySingleton::instance()->adjust( 1.1 );
1256 * An fgcommand to allow loading of xml files via nasal,
1257 * the xml file's structure will be made available within
1258 * a property tree node defined under argument "targetnode",
1259 * or in the given argument tree under "data" otherwise.
1261 * @param filename a string to hold the complete path & filename of an XML file
1262 * @param targetnode a string pointing to a location within the property tree
1263 * where to store the parsed XML file. If <targetnode> is undefined, then the
1264 * file contents are stored under a node <data> in the argument tree.
1268 do_load_xml_to_proptree(const SGPropertyNode * arg)
1270 SGPath file(arg->getStringValue("filename"));
1271 if (file.str().empty())
1274 if (file.extension() != "xml")
1275 file.concat(".xml");
1277 std::string icao = arg->getStringValue("icao");
1279 if (file.isRelative()) {
1280 SGPath absPath = globals->resolve_maybe_aircraft_path(file.str());
1281 if (!absPath.isNull())
1285 SG_LOG(SG_IO, SG_ALERT, "loadxml: Cannot find XML property file '"
1286 << file.str() << "'.");
1291 if (!XMLLoader::findAirportData(icao, file.str(), file)) {
1292 SG_LOG(SG_IO, SG_INFO, "loadxml: failed to find airport data for "
1293 << file.str() << " at ICAO:" << icao);
1298 if (!fgValidatePath(file.c_str(), false)) {
1299 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1300 "(unauthorized access)");
1304 SGPropertyNode *targetnode;
1305 if (arg->hasValue("targetnode"))
1306 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1308 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1311 readProperties(file.c_str(), targetnode, true);
1312 } catch (const sg_exception &e) {
1313 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1322 * An fgcommand to allow saving of xml files via nasal,
1323 * the file's structure will be determined based on what's
1324 * encountered in the passed (source) property tree node
1326 * @param filename a string to hold the complete path & filename of the (new)
1328 * @param sourcenode a string pointing to a location within the property tree
1329 * where to find the nodes that should be written recursively into an XML file
1330 * @param data if no sourcenode is given, then the file contents are taken from
1331 * the argument tree's "data" node.
1335 do_save_xml_from_proptree(const SGPropertyNode * arg)
1337 SGPath file(arg->getStringValue("filename"));
1338 if (file.str().empty())
1341 if (file.extension() != "xml")
1342 file.concat(".xml");
1344 if (!fgValidatePath(file.c_str(), true)) {
1345 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1346 "(unauthorized access)");
1350 SGPropertyNode *sourcenode;
1351 if (arg->hasValue("sourcenode"))
1352 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1353 else if (arg->getNode("data", false))
1354 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1359 writeProperties (file.c_str(), sourcenode, true);
1360 } catch (const sg_exception &e) {
1361 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1369 do_press_cockpit_button (const SGPropertyNode *arg)
1371 const char *prefix = arg->getStringValue("prefix");
1373 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1376 string prop = string(prefix) + "-button";
1379 if (arg->getBoolValue("latching"))
1380 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1384 fgSetDouble(prop.c_str(), value);
1385 fgSetBool(arg->getStringValue("discrete"), value > 0);
1391 do_release_cockpit_button (const SGPropertyNode *arg)
1393 const char *prefix = arg->getStringValue("prefix");
1395 if (arg->getBoolValue("guarded")) {
1396 string prop = string(prefix) + "-guard";
1397 if (fgGetDouble(prop.c_str()) < 1) {
1398 fgSetDouble(prop.c_str(), 1);
1403 if (! arg->getBoolValue("latching")) {
1404 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1405 fgSetBool(arg->getStringValue("discrete"), false);
1411 static SGGeod commandSearchPos(const SGPropertyNode* arg)
1413 if (arg->hasChild("longitude-deg") && arg->hasChild("latitude-deg")) {
1414 return SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
1415 arg->getDoubleValue("latitude-deg"));
1418 // use current viewer/aircraft position
1419 return SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
1420 fgGetDouble("/position/latitude-deg"));
1424 do_comm_search(const SGPropertyNode* arg)
1426 SGGeod pos = commandSearchPos(arg);
1427 int khz = static_cast<int>(arg->getDoubleValue("frequency-mhz") * 100.0 + 0.25);
1429 flightgear::CommStation* sta = flightgear::CommStation::findByFreq(khz, pos, NULL);
1434 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1435 sta->createBinding(result);
1440 do_nav_search(const SGPropertyNode* arg)
1442 SGGeod pos = commandSearchPos(arg);
1443 double mhz = arg->getDoubleValue("frequency-mhz");
1445 FGNavList* navList = globals->get_navlist();
1446 string type(arg->getStringValue("type", "vor"));
1447 if (type == "dme") {
1448 navList = globals->get_dmelist();
1449 } else if (type == "tacan") {
1450 navList = globals->get_tacanlist();
1453 FGNavRecord* nav = navList->findByFreq(mhz, pos);
1454 if (!nav && (type == "vor")) {
1455 // if we're searching VORs, look for localizers too
1456 nav = globals->get_loclist()->findByFreq(mhz, pos);
1463 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1464 nav->createBinding(result);
1468 ////////////////////////////////////////////////////////////////////////
1470 ////////////////////////////////////////////////////////////////////////
1474 * Table of built-in commands.
1476 * New commands do not have to be added here; any module in the application
1477 * can add a new command using globals->get_commands()->addCommand(...).
1481 SGCommandMgr::command_t command;
1483 { "null", do_null },
1484 { "nasal", do_nasal },
1485 { "exit", do_exit },
1486 { "reset", do_reset },
1487 { "reinit", do_reinit },
1488 { "suspend", do_reinit },
1489 { "resume", do_reinit },
1490 { "pause", do_pause },
1491 { "load", do_load },
1492 { "save", do_save },
1493 { "panel-load", do_panel_load },
1494 { "panel-mouse-click", do_panel_mouse_click },
1495 { "preferences-load", do_preferences_load },
1496 { "view-cycle", do_view_cycle },
1497 { "screen-capture", do_screen_capture },
1498 { "hires-screen-capture", do_hires_screen_capture },
1499 { "tile-cache-reload", do_tile_cache_reload },
1501 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1502 { "set-outside-air-temp-degc", do_set_oat_degc },
1503 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1504 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1506 { "property-toggle", do_property_toggle },
1507 { "property-assign", do_property_assign },
1508 { "property-adjust", do_property_adjust },
1509 { "property-multiply", do_property_multiply },
1510 { "property-swap", do_property_swap },
1511 { "property-scale", do_property_scale },
1512 { "property-cycle", do_property_cycle },
1513 { "property-randomize", do_property_randomize },
1514 { "property-interpolate", do_property_interpolate },
1515 { "data-logging-commit", do_data_logging_commit },
1516 { "dialog-new", do_dialog_new },
1517 { "dialog-show", do_dialog_show },
1518 { "dialog-close", do_dialog_close },
1519 { "dialog-update", do_dialog_update },
1520 { "dialog-apply", do_dialog_apply },
1521 { "open-browser", do_open_browser },
1522 { "gui-redraw", do_gui_redraw },
1523 { "add-model", do_add_model },
1524 { "set-cursor", do_set_cursor },
1525 { "play-audio-sample", do_play_audio_sample },
1526 { "presets-commit", do_presets_commit },
1527 { "log-level", do_log_level },
1528 { "replay", do_replay },
1530 { "decrease-visibility", do_decrease_visibility },
1531 { "increase-visibility", do_increase_visibility },
1533 { "loadxml", do_load_xml_to_proptree},
1534 { "savexml", do_save_xml_from_proptree },
1535 { "press-cockpit-button", do_press_cockpit_button },
1536 { "release-cockpit-button", do_release_cockpit_button },
1537 { "dump-scenegraph", do_dump_scene_graph },
1538 { "dump-terrainbranch", do_dump_terrain_branch },
1539 { "print-visible-scene", do_print_visible_scene_info },
1540 { "reload-shaders", do_reload_shaders },
1542 { "find-navaid", do_nav_search },
1543 { "find-comm", do_comm_search },
1545 { 0, 0 } // zero-terminated
1550 * Initialize the default built-in commands.
1552 * Other commands may be added by other parts of the application.
1557 SG_LOG(SG_GENERAL, SG_BULK, "Initializing basic built-in commands:");
1558 for (int i = 0; built_ins[i].name != 0; i++) {
1559 SG_LOG(SG_GENERAL, SG_BULK, " " << built_ins[i].name);
1560 globals->get_commands()->addCommand(built_ins[i].name,
1561 built_ins[i].command);
1564 typedef bool (*dummy)();
1565 fgTie( "/command/view/next", dummy(0), do_view_next );
1566 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1569 // end of fg_commands.cxx