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/structure/event_mgr.hxx>
23 #include <simgear/sound/soundmgr_openal.hxx>
24 #include <simgear/timing/sg_time.hxx>
26 #include <Cockpit/panel.hxx>
27 #include <Cockpit/panel_io.hxx>
28 #include <FDM/flight.hxx>
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>
41 #include "fg_init.hxx"
44 #include "fg_commands.hxx"
45 #include "fg_props.hxx"
46 #include "globals.hxx"
49 #include "viewmgr.hxx"
51 #include <Main/viewer.hxx>
52 #include <Environment/presets.hxx>
60 ////////////////////////////////////////////////////////////////////////
61 // Static helper functions.
62 ////////////////////////////////////////////////////////////////////////
65 static inline SGPropertyNode *
66 get_prop (const SGPropertyNode * arg)
68 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
71 static inline SGPropertyNode *
72 get_prop2 (const SGPropertyNode * arg)
74 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
79 * Get a double value and split it as required.
82 split_value (double full_value, const char * mask,
83 double * unmodifiable, double * modifiable)
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;
92 if (strcmp("all", mask))
93 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
94 << " assuming 'all'");
96 *modifiable = full_value;
102 * Clamp or wrap a value as specified.
105 limit_value (double * value, const SGPropertyNode * arg)
107 const SGPropertyNode * min_node = arg->getChild("min");
108 const SGPropertyNode * max_node = arg->getChild("max");
110 bool wrap = arg->getBoolValue("wrap");
112 if (min_node == 0 || max_node == 0)
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;
126 // plain circular wrapping
127 SG_NORMALIZE_RANGE(*value, min_val, max_val);
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();
138 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
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());
152 return !strcmp(value1->getStringValue(), value2->getStringValue());
158 ////////////////////////////////////////////////////////////////////////
159 // Command implementations.
160 ////////////////////////////////////////////////////////////////////////
164 * Built-in command: do nothing.
167 do_null (const SGPropertyNode * arg)
173 * Built-in command: run a Nasal script.
176 do_nasal (const SGPropertyNode * arg)
178 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
182 * Built-in command: exit FlightGear.
184 * status: the exit status to return to the operating system (defaults to 0)
187 do_exit (const SGPropertyNode * arg)
189 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
190 fgSetBool("/sim/signals/exit", true);
192 if (fgGetBool("/sim/startup/save-on-exit")) {
194 char* envp = ::getenv( "APPDATA" );
195 if ( envp != NULL ) {
196 SGPath config( envp );
197 config.append( "flightgear.org" );
199 if ( homedir != NULL ) {
200 SGPath config( homedir );
201 config.append( ".fgfs" );
203 config.append( "autosave.xml" );
204 config.create_dir( 0700 );
205 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
207 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
208 } catch (const sg_exception &e) {
209 guiErrorMessage("Error writing autosave.xml: ", e);
212 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
216 fgOSExit(arg->getIntValue("status", 0));
222 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
225 do_reset (const SGPropertyNode * arg)
227 fgReInitSubsystems();
233 * Built-in command: reinitialize one or more subsystems.
235 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
236 * none is specified, reinitialize all of them.
239 do_reinit (const SGPropertyNode * arg)
243 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
244 if (subsystems.size() == 0) {
245 globals->get_subsystem_mgr()->reinit();
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) {
252 SG_LOG( SG_GENERAL, SG_ALERT,
253 "Subsystem " << name << " not found" );
260 globals->get_event_mgr()->reinit();
267 // these routines look useful ??? but are never used in the code ???
271 * Built-in command: suspend one or more subsystems.
273 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
276 do_suspend (const SGPropertyNode * arg)
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) {
286 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
288 subsystem->suspend();
295 * Built-in command: suspend one or more subsystems.
297 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
300 do_resume (const SGPropertyNode * arg)
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) {
310 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
321 do_pause (const SGPropertyNode * arg)
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);
332 * Built-in command: load flight.
334 * file (optional): the name of the file to load (relative to current
335 * directory). Defaults to "fgfs.sav"
338 do_load (const SGPropertyNode * arg)
340 string file = arg->getStringValue("file", "fgfs.sav");
341 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
344 if (!fgValidatePath(file.c_str(), false)) {
345 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
346 "(unauthorized access)");
350 ifstream input(file.c_str());
351 if (input.good() && fgLoadFlight(input)) {
353 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
356 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
363 * Built-in command: save flight.
365 * file (optional): the name of the file to save (relative to the
366 * current directory). Defaults to "fgfs.sav".
369 do_save (const SGPropertyNode * arg)
371 string file = arg->getStringValue("file", "fgfs.sav");
372 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
375 if (!fgValidatePath(file.c_str(), false)) {
376 SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
377 "(unauthorized access)");
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)) {
386 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
389 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
396 * Built-in command: (re)load the panel.
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".
403 do_panel_load (const SGPropertyNode * arg)
406 arg->getStringValue("path", fgGetString("/sim/panel/path"));
407 if (panel_path.empty()) {
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);
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();
427 * Built-in command: pass a mouse click to the panel.
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.
435 do_panel_mouse_click (const SGPropertyNode * arg)
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"));
449 * Built-in command: (re)load preferences.
451 * path (optional): the file name to load the panel from (relative
452 * to FG_ROOT). Defaults to "preferences.xml".
455 do_preferences_load (const SGPropertyNode * arg)
458 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
459 globals->get_props());
460 } catch (const sg_exception &e) {
461 guiErrorMessage("Error reading global preferences: ", e);
464 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
471 globals->get_current_view()->setHeadingOffset_deg(0.0);
472 globals->get_viewmgr()->next_view();
478 globals->get_current_view()->setHeadingOffset_deg(0.0);
479 globals->get_viewmgr()->prev_view();
483 * Built-in command: cycle view.
486 do_view_cycle (const SGPropertyNode * arg)
488 globals->get_current_view()->setHeadingOffset_deg(0.0);
489 globals->get_viewmgr()->next_view();
494 * Built-in command: capture screen.
497 do_screen_capture (const SGPropertyNode * arg)
499 return fgDumpSnapShot();
503 do_reload_shaders (const SGPropertyNode*)
505 simgear::reload_shaders();
510 do_dump_scene_graph (const SGPropertyNode*)
517 do_dump_terrain_branch (const SGPropertyNode*)
519 fgDumpTerrainBranch();
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);
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 );
534 do_print_visible_scene_info(const SGPropertyNode*)
536 fgPrintVisibleSceneInfoCommand();
541 * Built-in command: hires capture screen.
544 do_hires_screen_capture (const SGPropertyNode * arg)
552 * Reload the tile cache.
555 do_tile_cache_reload (const SGPropertyNode * arg)
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");
562 fgSetBool("/sim/freeze/master", true);
565 globals->get_subsystem("tile-manager")->reinit();
568 fgSetBool("/sim/freeze/master", false);
575 These do_set_(some-environment-parameters) are deprecated and no longer
576 useful/functional - Torsten Dreyer, January 2011
578 * Set the sea level outside air temperature and assigning that to all
579 * boundary and aloft environment layers.
582 do_set_sea_level_degc ( double temp_sea_level_degc)
584 SGPropertyNode *node, *child;
587 node = fgGetNode( "/environment/config/boundary" );
588 if ( node != NULL ) {
590 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
591 child->setDoubleValue( "temperature-sea-level-degc",
592 temp_sea_level_degc );
598 node = fgGetNode( "/environment/config/aloft" );
599 if ( node != NULL ) {
601 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
602 child->setDoubleValue( "temperature-sea-level-degc",
603 temp_sea_level_degc );
612 do_set_sea_level_degc (const SGPropertyNode * arg)
614 return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
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.
624 do_set_oat_degc (const SGPropertyNode * arg)
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");
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());
641 * Set the sea level outside air dewpoint and assigning that to all
642 * boundary and aloft environment layers.
645 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
648 SGPropertyNode *node, *child;
651 node = fgGetNode( "/environment/config/boundary" );
652 if ( node != NULL ) {
654 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
655 child->setDoubleValue( "dewpoint-sea-level-degc",
656 dewpoint_sea_level_degc );
662 node = fgGetNode( "/environment/config/aloft" );
663 if ( node != NULL ) {
665 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
666 child->setDoubleValue( "dewpoint-sea-level-degc",
667 dewpoint_sea_level_degc );
676 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
678 return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
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.
687 do_set_dewpoint_degc (const SGPropertyNode * arg)
689 double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
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");
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());
706 * Built-in command: toggle a bool property value.
708 * property: The name of the property to toggle.
711 do_property_toggle (const SGPropertyNode * arg)
713 SGPropertyNode * prop = get_prop(arg);
714 return prop->setBoolValue(!prop->getBoolValue());
719 * Built-in command: assign a value to a property.
721 * property: the name of the property to assign.
722 * value: the value to assign; or
723 * property[1]: the property to copy from.
726 do_property_assign (const SGPropertyNode * arg)
728 SGPropertyNode * prop = get_prop(arg);
729 const SGPropertyNode * prop2 = get_prop2(arg);
730 const SGPropertyNode * value = arg->getNode("value");
733 return prop->setUnspecifiedValue(value->getStringValue());
735 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);
937 * Built-in command: reinit the data logging system based on the
938 * current contents of the /logger tree.
941 do_data_logging_commit (const SGPropertyNode * arg)
943 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
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:
953 * name: the name of the GUI dialog for future reference.
956 do_dialog_new (const SGPropertyNode * arg)
958 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
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
967 gui->newDialog((SGPropertyNode*)arg);
972 * Built-in command: Show an XML-configured dialog.
974 * dialog-name: the name of the GUI dialog to display.
977 do_dialog_show (const SGPropertyNode * arg)
979 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
980 gui->showDialog(arg->getStringValue("dialog-name"));
986 * Built-in Command: Hide the active XML-configured dialog.
989 do_dialog_close (const SGPropertyNode * arg)
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();
999 * Update a value in the active XML-configured dialog.
1001 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1004 do_dialog_update (const SGPropertyNode * arg)
1006 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1008 if (arg->hasValue("dialog-name"))
1009 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1011 dialog = gui->getActiveDialog();
1014 dialog->updateValues(arg->getStringValue("object-name"));
1023 * Apply a value in the active XML-configured dialog.
1025 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1028 do_dialog_apply (const SGPropertyNode * arg)
1030 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1032 if (arg->hasValue("dialog-name"))
1033 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1035 dialog = gui->getActiveDialog();
1038 dialog->applyValues(arg->getStringValue("object-name"));
1047 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1051 do_gui_redraw (const SGPropertyNode * arg)
1053 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1060 * Adds model to the scenery. The path to the added branch (/models/model[*])
1061 * is returned in property "property".
1064 do_add_model (const SGPropertyNode * arg)
1066 SGPropertyNode * model = fgGetNode("models", true);
1067 for (int i = 0;; i++) {
1070 if (!model->getChild("model", i, false)) {
1071 model = model->getChild("model", i, true);
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());
1087 * Set mouse cursor coordinates and cursor shape.
1090 do_set_cursor (const SGPropertyNode * arg)
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());
1102 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1103 if (cursor->getType() != simgear::props::NONE)
1104 fgSetMouseCursor(cursor->getIntValue());
1106 cursor->setIntValue(fgGetMouseCursor());
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
1118 do_play_audio_sample (const SGPropertyNode * arg)
1120 string path = arg->getStringValue("path");
1121 string file = arg->getStringValue("file");
1122 float volume = arg->getFloatValue("volume");
1123 // cout << "playing " << path << " / " << file << endl;
1125 static FGSampleQueue *queue = 0;
1127 SGSoundMgr *smgr = globals->get_soundmgr();
1128 queue = new FGSampleQueue(smgr, "chatter");
1129 queue->tie_to_listener();
1132 SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1133 msg->set_volume( volume );
1138 } catch (const sg_io_exception&) {
1139 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1140 "failed to load" << path << '/' << file);
1146 * Built-in command: commit presets (read from in /sim/presets/)
1149 do_presets_commit (const SGPropertyNode * arg)
1151 if (fgGetBool("/sim/initialized", false)) {
1152 fgReInitSubsystems();
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.
1164 * Built-in command: set log level (0 ... 7)
1167 do_log_level (const SGPropertyNode * arg)
1169 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1175 * Built-in command: replay the FDR buffer
1178 do_replay (const SGPropertyNode * arg)
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 );
1186 // cout << "start = " << r->get_start_time()
1187 // << " end = " << r->get_end_time() << endl;
1194 do_decrease_visibility (const SGPropertyNode * arg)
1196 Environment::Presets::VisibilitySingleton::instance()->adjust( 0.9 );
1201 do_increase_visibility (const SGPropertyNode * arg)
1203 Environment::Presets::VisibilitySingleton::instance()->adjust( 1.1 );
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.
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.
1220 do_load_xml_to_proptree(const SGPropertyNode * arg)
1222 SGPath file(arg->getStringValue("filename"));
1223 if (file.str().empty())
1226 if (file.extension() != "xml")
1227 file.concat(".xml");
1229 std::string icao = arg->getStringValue("icao");
1231 if (file.isRelative()) {
1232 file = globals->resolve_maybe_aircraft_path(file.str());
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);
1242 if (!fgValidatePath(file.c_str(), false)) {
1243 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1244 "(unauthorized access)");
1248 SGPropertyNode *targetnode;
1249 if (arg->hasValue("targetnode"))
1250 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1252 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1255 readProperties(file.c_str(), targetnode, true);
1256 } catch (const sg_exception &e) {
1257 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
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
1270 * @param filename a string to hold the complete path & filename of the (new)
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.
1279 do_save_xml_from_proptree(const SGPropertyNode * arg)
1281 SGPath file(arg->getStringValue("filename"));
1282 if (file.str().empty())
1285 if (file.extension() != "xml")
1286 file.concat(".xml");
1288 if (!fgValidatePath(file.c_str(), true)) {
1289 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1290 "(unauthorized access)");
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");
1303 writeProperties (file.c_str(), sourcenode, true);
1304 } catch (const sg_exception &e) {
1305 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1313 do_press_cockpit_button (const SGPropertyNode *arg)
1315 const char *prefix = arg->getStringValue("prefix");
1317 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1320 string prop = string(prefix) + "-button";
1323 if (arg->getBoolValue("latching"))
1324 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1328 fgSetDouble(prop.c_str(), value);
1329 fgSetBool(arg->getStringValue("discrete"), value > 0);
1335 do_release_cockpit_button (const SGPropertyNode *arg)
1337 const char *prefix = arg->getStringValue("prefix");
1339 if (arg->getBoolValue("guarded")) {
1340 string prop = string(prefix) + "-guard";
1341 if (fgGetDouble(prop.c_str()) < 1) {
1342 fgSetDouble(prop.c_str(), 1);
1347 if (! arg->getBoolValue("latching")) {
1348 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1349 fgSetBool(arg->getStringValue("discrete"), false);
1355 static SGGeod commandSearchPos(const SGPropertyNode* arg)
1357 if (arg->hasChild("longitude-deg") && arg->hasChild("latitude-deg")) {
1358 return SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
1359 arg->getDoubleValue("latitude-deg"));
1362 // use current viewer/aircraft position
1363 return SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
1364 fgGetDouble("/position/latitude-deg"));
1368 do_comm_search(const SGPropertyNode* arg)
1370 SGGeod pos = commandSearchPos(arg);
1371 int khz = static_cast<int>(arg->getDoubleValue("frequency-mhz") * 100.0 + 0.25);
1373 flightgear::CommStation* sta = flightgear::CommStation::findByFreq(khz, pos, NULL);
1378 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1379 sta->createBinding(result);
1384 do_nav_search(const SGPropertyNode* arg)
1386 SGGeod pos = commandSearchPos(arg);
1387 double mhz = arg->getDoubleValue("frequency-mhz");
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();
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);
1407 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1408 nav->createBinding(result);
1412 ////////////////////////////////////////////////////////////////////////
1414 ////////////////////////////////////////////////////////////////////////
1418 * Table of built-in commands.
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(...).
1425 SGCommandMgr::command_t command;
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 },
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 },
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 },
1472 { "decrease-visibility", do_decrease_visibility },
1473 { "increase-visibility", do_increase_visibility },
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 },
1484 { "find-navaid", do_nav_search },
1485 { "find-comm", do_comm_search },
1487 { 0, 0 } // zero-terminated
1492 * Initialize the default built-in commands.
1494 * Other commands may be added by other parts of the application.
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);
1506 typedef bool (*dummy)();
1507 fgTie( "/command/view/next", dummy(0), do_view_next );
1508 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1511 // end of fg_commands.cxx