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>
27 #include <Cockpit/panel.hxx>
28 #include <Cockpit/panel_io.hxx>
29 #include <FDM/flight.hxx>
31 #include <GUI/new_gui.hxx>
32 #include <GUI/dialog.hxx>
33 #include <Aircraft/replay.hxx>
34 #include <Scenery/scenery.hxx>
35 #include <Scripting/NasalSys.hxx>
36 #include <Sound/sample_queue.hxx>
37 #include <Airports/xmlloader.hxx>
38 #include <ATC/CommStation.hxx>
39 #include <Navaids/navrecord.hxx>
40 #include <Navaids/navlist.hxx>
42 #include "fg_init.hxx"
45 #include "fg_commands.hxx"
46 #include "fg_props.hxx"
47 #include "globals.hxx"
50 #include "viewmgr.hxx"
52 #include <Main/viewer.hxx>
53 #include <Environment/presets.hxx>
61 ////////////////////////////////////////////////////////////////////////
62 // Static helper functions.
63 ////////////////////////////////////////////////////////////////////////
66 static inline SGPropertyNode *
67 get_prop (const SGPropertyNode * arg)
69 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
72 static inline SGPropertyNode *
73 get_prop2 (const SGPropertyNode * arg)
75 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
80 * Get a double value and split it as required.
83 split_value (double full_value, const char * mask,
84 double * unmodifiable, double * modifiable)
86 if (!strcmp("integer", mask)) {
87 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
88 *unmodifiable = full_value - *modifiable;
89 } else if (!strcmp("decimal", mask)) {
90 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
91 *modifiable = full_value - *unmodifiable;
93 if (strcmp("all", mask))
94 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
95 << " assuming 'all'");
97 *modifiable = full_value;
103 * Clamp or wrap a value as specified.
106 limit_value (double * value, const SGPropertyNode * arg)
108 const SGPropertyNode * min_node = arg->getChild("min");
109 const SGPropertyNode * max_node = arg->getChild("max");
111 bool wrap = arg->getBoolValue("wrap");
113 if (min_node == 0 || max_node == 0)
116 if (wrap) { // wrap such that min <= x < max
117 double min_val = min_node->getDoubleValue();
118 double max_val = max_node->getDoubleValue();
119 double resolution = arg->getDoubleValue("resolution");
120 if (resolution > 0.0) {
121 // snap to (min + N*resolution), taking special care to handle imprecision
122 int n = (int)floor((*value - min_val) / resolution + 0.5);
123 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
124 SG_NORMALIZE_RANGE(n, 0, steps);
125 *value = min_val + resolution * n;
127 // plain circular wrapping
128 SG_NORMALIZE_RANGE(*value, min_val, max_val);
130 } else { // clamp such that min <= x <= max
131 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
132 *value = min_node->getDoubleValue();
133 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
134 *value = max_node->getDoubleValue();
139 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
141 switch (value1->getType()) {
142 case simgear::props::BOOL:
143 return (value1->getBoolValue() == value2->getBoolValue());
144 case simgear::props::INT:
145 return (value1->getIntValue() == value2->getIntValue());
146 case simgear::props::LONG:
147 return (value1->getLongValue() == value2->getLongValue());
148 case simgear::props::FLOAT:
149 return (value1->getFloatValue() == value2->getFloatValue());
150 case simgear::props::DOUBLE:
151 return (value1->getDoubleValue() == value2->getDoubleValue());
153 return !strcmp(value1->getStringValue(), value2->getStringValue());
159 ////////////////////////////////////////////////////////////////////////
160 // Command implementations.
161 ////////////////////////////////////////////////////////////////////////
165 * Built-in command: do nothing.
168 do_null (const SGPropertyNode * arg)
174 * Built-in command: run a Nasal script.
177 do_nasal (const SGPropertyNode * arg)
179 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
183 * Built-in command: exit FlightGear.
185 * status: the exit status to return to the operating system (defaults to 0)
188 do_exit (const SGPropertyNode * arg)
190 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
191 fgSetBool("/sim/signals/exit", true);
192 globals->saveUserSettings();
193 fgOSExit(arg->getIntValue("status", 0));
199 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
202 do_reset (const SGPropertyNode * arg)
204 fgReInitSubsystems();
210 * Built-in command: reinitialize one or more subsystems.
212 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
213 * none is specified, reinitialize all of them.
216 do_reinit (const SGPropertyNode * arg)
220 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
221 if (subsystems.size() == 0) {
222 globals->get_subsystem_mgr()->reinit();
224 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
225 const char * name = subsystems[i]->getStringValue();
226 SGSubsystem * subsystem = globals->get_subsystem(name);
227 if (subsystem == 0) {
229 SG_LOG( SG_GENERAL, SG_ALERT,
230 "Subsystem " << name << " not found" );
237 globals->get_event_mgr()->reinit();
244 // these routines look useful ??? but are never used in the code ???
248 * Built-in command: suspend one or more subsystems.
250 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
253 do_suspend (const SGPropertyNode * arg)
257 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
258 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
259 const char * name = subsystems[i]->getStringValue();
260 SGSubsystem * subsystem = globals->get_subsystem(name);
261 if (subsystem == 0) {
263 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
265 subsystem->suspend();
272 * Built-in command: suspend one or more subsystems.
274 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
277 do_resume (const SGPropertyNode * arg)
281 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
282 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
283 const char * name = subsystems[i]->getStringValue();
284 SGSubsystem * subsystem = globals->get_subsystem(name);
285 if (subsystem == 0) {
287 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
298 * Built-in command: replay the FDR buffer
301 do_replay (const SGPropertyNode * arg)
303 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
308 * Built-in command: pause/unpause the sim
311 do_pause (const SGPropertyNode * arg)
313 bool paused = fgGetBool("/sim/freeze/master",true) || fgGetBool("/sim/freeze/clock",true);
314 if (paused && (fgGetInt("/sim/freeze/replay-state",0)>0))
320 fgSetBool("/sim/freeze/master",!paused);
321 fgSetBool("/sim/freeze/clock",!paused);
327 * Built-in command: load flight.
329 * file (optional): the name of the file to load (relative to current
330 * directory). Defaults to "fgfs.sav"
333 do_load (const SGPropertyNode * arg)
335 string file = arg->getStringValue("file", "fgfs.sav");
336 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
339 if (!fgValidatePath(file.c_str(), false)) {
340 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
341 "(unauthorized access)");
345 ifstream input(file.c_str());
346 if (input.good() && fgLoadFlight(input)) {
348 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
351 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
358 * Built-in command: save flight.
360 * file (optional): the name of the file to save (relative to the
361 * current directory). Defaults to "fgfs.sav".
364 do_save (const SGPropertyNode * arg)
366 string file = arg->getStringValue("file", "fgfs.sav");
367 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
370 if (!fgValidatePath(file.c_str(), false)) {
371 SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
372 "(unauthorized access)");
376 bool write_all = arg->getBoolValue("write-all", false);
377 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
378 ofstream output(file.c_str());
379 if (output.good() && fgSaveFlight(output, write_all)) {
381 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
384 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
391 * Built-in command: (re)load the panel.
393 * path (optional): the file name to load the panel from
394 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
395 * and if that's unspecified, to "Panels/Default/default.xml".
398 do_panel_load (const SGPropertyNode * arg)
401 arg->getStringValue("path", fgGetString("/sim/panel/path"));
402 if (panel_path.empty()) {
406 FGPanel * new_panel = fgReadPanel(panel_path);
407 if (new_panel == 0) {
408 SG_LOG(SG_INPUT, SG_ALERT,
409 "Error reading new panel from " << panel_path);
412 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
413 globals->get_current_panel()->unbind();
414 delete globals->get_current_panel();
415 globals->set_current_panel( new_panel );
416 globals->get_current_panel()->bind();
422 * Built-in command: pass a mouse click to the panel.
424 * button: the mouse button number, zero-based.
425 * is-down: true if the button is down, false if it is up.
426 * x-pos: the x position of the mouse click.
427 * y-pos: the y position of the mouse click.
430 do_panel_mouse_click (const SGPropertyNode * arg)
432 if (globals->get_current_panel() != 0)
433 return globals->get_current_panel()
434 ->doMouseAction(arg->getIntValue("button"),
435 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
436 arg->getIntValue("x-pos"),
437 arg->getIntValue("y-pos"));
444 * Built-in command: (re)load preferences.
446 * path (optional): the file name to load the panel from (relative
447 * to FG_ROOT). Defaults to "preferences.xml".
450 do_preferences_load (const SGPropertyNode * arg)
453 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
454 globals->get_props());
455 } catch (const sg_exception &e) {
456 guiErrorMessage("Error reading global preferences: ", e);
459 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
466 globals->get_current_view()->setHeadingOffset_deg(0.0);
467 globals->get_viewmgr()->next_view();
473 globals->get_current_view()->setHeadingOffset_deg(0.0);
474 globals->get_viewmgr()->prev_view();
478 * Built-in command: cycle view.
481 do_view_cycle (const SGPropertyNode * arg)
483 globals->get_current_view()->setHeadingOffset_deg(0.0);
484 globals->get_viewmgr()->next_view();
489 * Built-in command: capture screen.
492 do_screen_capture (const SGPropertyNode * arg)
494 return fgDumpSnapShot();
498 do_reload_shaders (const SGPropertyNode*)
500 simgear::reload_shaders();
505 do_dump_scene_graph (const SGPropertyNode*)
512 do_dump_terrain_branch (const SGPropertyNode*)
514 fgDumpTerrainBranch();
516 double lon_deg = fgGetDouble("/position/longitude-deg");
517 double lat_deg = fgGetDouble("/position/latitude-deg");
518 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
519 SGVec3d zero = SGVec3d::fromGeod(geodPos);
521 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
522 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
523 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
529 do_print_visible_scene_info(const SGPropertyNode*)
531 fgPrintVisibleSceneInfoCommand();
536 * Built-in command: hires capture screen.
539 do_hires_screen_capture (const SGPropertyNode * arg)
547 * Reload the tile cache.
550 do_tile_cache_reload (const SGPropertyNode * arg)
552 static const SGPropertyNode *master_freeze
553 = fgGetNode("/sim/freeze/master");
554 bool freeze = master_freeze->getBoolValue();
555 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
557 fgSetBool("/sim/freeze/master", true);
560 globals->get_subsystem("tile-manager")->reinit();
563 fgSetBool("/sim/freeze/master", false);
570 These do_set_(some-environment-parameters) are deprecated and no longer
571 useful/functional - Torsten Dreyer, January 2011
573 * Set the sea level outside air temperature and assigning that to all
574 * boundary and aloft environment layers.
577 do_set_sea_level_degc ( double temp_sea_level_degc)
579 SGPropertyNode *node, *child;
582 node = fgGetNode( "/environment/config/boundary" );
583 if ( node != NULL ) {
585 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
586 child->setDoubleValue( "temperature-sea-level-degc",
587 temp_sea_level_degc );
593 node = fgGetNode( "/environment/config/aloft" );
594 if ( node != NULL ) {
596 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
597 child->setDoubleValue( "temperature-sea-level-degc",
598 temp_sea_level_degc );
607 do_set_sea_level_degc (const SGPropertyNode * arg)
609 return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
614 * Set the outside air temperature at the "current" altitude by first
615 * calculating the corresponding sea level temp, and assigning that to
616 * all boundary and aloft environment layers.
619 do_set_oat_degc (const SGPropertyNode * arg)
621 double oat_degc = arg->getDoubleValue("temp-degc", 15.0);
622 // check for an altitude specified in the arguments, otherwise use
623 // current aircraft altitude.
624 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
625 if ( altitude_ft == NULL ) {
626 altitude_ft = fgGetNode("/position/altitude-ft");
629 FGEnvironment dummy; // instantiate a dummy so we can leech a method
630 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
631 dummy.set_temperature_degc( oat_degc );
632 return do_set_sea_level_degc( dummy.get_temperature_sea_level_degc());
636 * Set the sea level outside air dewpoint and assigning that to all
637 * boundary and aloft environment layers.
640 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
643 SGPropertyNode *node, *child;
646 node = fgGetNode( "/environment/config/boundary" );
647 if ( node != NULL ) {
649 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
650 child->setDoubleValue( "dewpoint-sea-level-degc",
651 dewpoint_sea_level_degc );
657 node = fgGetNode( "/environment/config/aloft" );
658 if ( node != NULL ) {
660 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
661 child->setDoubleValue( "dewpoint-sea-level-degc",
662 dewpoint_sea_level_degc );
671 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
673 return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
677 * Set the outside air dewpoint at the "current" altitude by first
678 * calculating the corresponding sea level dewpoint, and assigning
679 * that to all boundary and aloft environment layers.
682 do_set_dewpoint_degc (const SGPropertyNode * arg)
684 double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
686 // check for an altitude specified in the arguments, otherwise use
687 // current aircraft altitude.
688 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
689 if ( altitude_ft == NULL ) {
690 altitude_ft = fgGetNode("/position/altitude-ft");
693 FGEnvironment dummy; // instantiate a dummy so we can leech a method
694 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
695 dummy.set_dewpoint_degc( dewpoint_degc );
696 return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
701 * Built-in command: toggle a bool property value.
703 * property: The name of the property to toggle.
706 do_property_toggle (const SGPropertyNode * arg)
708 SGPropertyNode * prop = get_prop(arg);
709 return prop->setBoolValue(!prop->getBoolValue());
714 * Built-in command: assign a value to a property.
716 * property: the name of the property to assign.
717 * value: the value to assign; or
718 * property[1]: the property to copy from.
721 do_property_assign (const SGPropertyNode * arg)
723 SGPropertyNode * prop = get_prop(arg);
724 const SGPropertyNode * value = arg->getNode("value");
727 return prop->setUnspecifiedValue(value->getStringValue());
730 const SGPropertyNode * prop2 = get_prop2(arg);
732 return prop->setUnspecifiedValue(prop2->getStringValue());
740 * Built-in command: increment or decrement a property value.
742 * If the 'step' argument is present, it will be used; otherwise,
743 * the command uses 'offset' and 'factor', usually from the mouse.
745 * property: the name of the property to increment or decrement.
746 * step: the amount of the increment or decrement (default: 0).
747 * offset: offset from the current setting (used for the mouse; multiplied
749 * factor: scaling amount for the offset (defaults to 1).
750 * min: the minimum allowed value (default: no minimum).
751 * max: the maximum allowed value (default: no maximum).
752 * mask: 'integer' to apply only to the left of the decimal point,
753 * 'decimal' to apply only to the right of the decimal point,
754 * or 'all' to apply to the whole number (the default).
755 * wrap: true if the value should be wrapped when it passes min or max;
756 * both min and max must be present for this to work (default:
760 do_property_adjust (const SGPropertyNode * arg)
762 SGPropertyNode * prop = get_prop(arg);
765 if (arg->hasValue("step"))
766 amount = arg->getDoubleValue("step");
768 amount = (arg->getDoubleValue("factor", 1)
769 * arg->getDoubleValue("offset"));
771 double unmodifiable, modifiable;
772 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
773 &unmodifiable, &modifiable);
774 modifiable += amount;
775 limit_value(&modifiable, arg);
777 prop->setDoubleValue(unmodifiable + modifiable);
784 * Built-in command: multiply a property value.
786 * property: the name of the property to multiply.
787 * factor: the amount by which to multiply.
788 * min: the minimum allowed value (default: no minimum).
789 * max: the maximum allowed value (default: no maximum).
790 * mask: 'integer' to apply only to the left of the decimal point,
791 * 'decimal' to apply only to the right of the decimal point,
792 * or 'all' to apply to the whole number (the default).
793 * wrap: true if the value should be wrapped when it passes min or max;
794 * both min and max must be present for this to work (default:
798 do_property_multiply (const SGPropertyNode * arg)
800 SGPropertyNode * prop = get_prop(arg);
801 double factor = arg->getDoubleValue("factor", 1);
803 double unmodifiable, modifiable;
804 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
805 &unmodifiable, &modifiable);
806 modifiable *= factor;
807 limit_value(&modifiable, arg);
809 prop->setDoubleValue(unmodifiable + modifiable);
816 * Built-in command: swap two property values.
818 * property[0]: the name of the first property.
819 * property[1]: the name of the second property.
822 do_property_swap (const SGPropertyNode * arg)
824 SGPropertyNode * prop1 = get_prop(arg);
825 SGPropertyNode * prop2 = get_prop2(arg);
827 // FIXME: inefficient
828 const string & tmp = prop1->getStringValue();
829 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
830 prop2->setUnspecifiedValue(tmp.c_str()));
835 * Built-in command: Set a property to an axis or other moving input.
837 * property: the name of the property to set.
838 * setting: the current input setting, usually between -1.0 and 1.0.
839 * offset: the offset to shift by, before applying the factor.
840 * factor: the factor to multiply by (use negative to reverse).
843 do_property_scale (const SGPropertyNode * arg)
845 SGPropertyNode * prop = get_prop(arg);
846 double setting = arg->getDoubleValue("setting");
847 double offset = arg->getDoubleValue("offset", 0.0);
848 double factor = arg->getDoubleValue("factor", 1.0);
849 bool squared = arg->getBoolValue("squared", false);
850 int power = arg->getIntValue("power", (squared ? 2 : 1));
852 int sign = (setting < 0 ? -1 : 1);
858 setting = setting * setting * sign;
861 setting = setting * setting * setting;
864 setting = setting * setting * setting * setting * sign;
867 setting = pow(setting, power);
868 if ((power % 2) == 0)
873 return prop->setDoubleValue((setting + offset) * factor);
878 * Built-in command: cycle a property through a set of values.
880 * If the current value isn't in the list, the cycle will
881 * (re)start from the beginning.
883 * property: the name of the property to cycle.
884 * value[*]: the list of values to cycle through.
887 do_property_cycle (const SGPropertyNode * arg)
889 SGPropertyNode * prop = get_prop(arg);
890 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
892 int nSelections = values.size();
894 if (nSelections < 1) {
895 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
899 // Try to find the current selection
900 for (int i = 0; i < nSelections; i++) {
901 if (compare_values(prop, values[i])) {
907 // Default or wrap to the first selection
908 if (selection < 0 || selection >= nSelections)
911 prop->setUnspecifiedValue(values[selection]->getStringValue());
917 * Built-in command: randomize a numeric property value.
919 * property: the name of the property value to randomize.
920 * min: the minimum allowed value.
921 * max: the maximum allowed value.
924 do_property_randomize (const SGPropertyNode * arg)
926 SGPropertyNode * prop = get_prop(arg);
927 double min = arg->getDoubleValue("min", DBL_MIN);
928 double max = arg->getDoubleValue("max", DBL_MAX);
929 prop->setDoubleValue(sg_random() * (max - min) + min);
935 * Built-in command: reinit the data logging system based on the
936 * current contents of the /logger tree.
939 do_data_logging_commit (const SGPropertyNode * arg)
941 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
947 * Built-in command: Add a dialog to the GUI system. Does *not*
948 * display the dialog. The property node should have the same format
949 * as a dialog XML configuration. It must include:
951 * name: the name of the GUI dialog for future reference.
954 do_dialog_new (const SGPropertyNode * arg)
956 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
958 // Note the casting away of const: this is *real*. Doing a
959 // "dialog-apply" command later on will mutate this property node.
960 // I'm not convinced that this isn't the Right Thing though; it
961 // allows client to create a node, pass it to dialog-new, and get
962 // the values back from the dialog by reading the same node.
963 // Perhaps command arguments are not as "const" as they would
965 gui->newDialog((SGPropertyNode*)arg);
970 * Built-in command: Show an XML-configured dialog.
972 * dialog-name: the name of the GUI dialog to display.
975 do_dialog_show (const SGPropertyNode * arg)
977 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
978 gui->showDialog(arg->getStringValue("dialog-name"));
984 * Built-in Command: Hide the active XML-configured dialog.
987 do_dialog_close (const SGPropertyNode * arg)
989 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
990 if(arg->hasValue("dialog-name"))
991 return gui->closeDialog(arg->getStringValue("dialog-name"));
992 return gui->closeActiveDialog();
997 * Update a value in the active XML-configured dialog.
999 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1002 do_dialog_update (const SGPropertyNode * arg)
1004 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1006 if (arg->hasValue("dialog-name"))
1007 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1009 dialog = gui->getActiveDialog();
1012 dialog->updateValues(arg->getStringValue("object-name"));
1020 do_open_browser (const SGPropertyNode * arg)
1023 if (arg->hasValue("path"))
1024 path = arg->getStringValue("path");
1026 if (arg->hasValue("url"))
1027 path = arg->getStringValue("url");
1031 return openBrowser(path);
1035 * Apply a value in the active XML-configured dialog.
1037 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1040 do_dialog_apply (const SGPropertyNode * arg)
1042 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1044 if (arg->hasValue("dialog-name"))
1045 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1047 dialog = gui->getActiveDialog();
1050 dialog->applyValues(arg->getStringValue("object-name"));
1059 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1063 do_gui_redraw (const SGPropertyNode * arg)
1065 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1072 * Adds model to the scenery. The path to the added branch (/models/model[*])
1073 * is returned in property "property".
1076 do_add_model (const SGPropertyNode * arg)
1078 SGPropertyNode * model = fgGetNode("models", true);
1080 for (i = 0; model->hasChild("model",i); i++);
1081 model = model->getChild("model", i, true);
1082 copyProperties(arg, model);
1083 if (model->hasValue("elevation-m"))
1084 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1085 * SG_METER_TO_FEET);
1086 model->getNode("load", true);
1087 model->removeChildren("load");
1088 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1094 * Set mouse cursor coordinates and cursor shape.
1097 do_set_cursor (const SGPropertyNode * arg)
1099 if (arg->hasValue("x") || arg->hasValue("y")) {
1100 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1101 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1102 int x = arg->getIntValue("x", mx->getIntValue());
1103 int y = arg->getIntValue("y", my->getIntValue());
1109 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1110 if (cursor->getType() != simgear::props::NONE)
1111 fgSetMouseCursor(cursor->getIntValue());
1113 cursor->setIntValue(fgGetMouseCursor());
1119 * Built-in command: play an audio message (i.e. a wav file) This is
1120 * fire and forget. Call this once per message and it will get dumped
1121 * into a queue. Messages are played sequentially so they do not
1125 do_play_audio_sample (const SGPropertyNode * arg)
1127 string path = arg->getStringValue("path");
1128 string file = arg->getStringValue("file");
1129 float volume = arg->getFloatValue("volume");
1130 // cout << "playing " << path << " / " << file << endl;
1132 static FGSampleQueue *queue = 0;
1134 SGSoundMgr *smgr = globals->get_soundmgr();
1135 queue = new FGSampleQueue(smgr, "chatter");
1136 queue->tie_to_listener();
1139 SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1140 msg->set_volume( volume );
1145 } catch (const sg_io_exception&) {
1146 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1147 "failed to load" << path << '/' << file);
1153 * Built-in command: commit presets (read from in /sim/presets/)
1156 do_presets_commit (const SGPropertyNode * arg)
1158 if (fgGetBool("/sim/initialized", false)) {
1159 fgReInitSubsystems();
1161 // Nasal can trigger this during initial init, which confuses
1162 // the logic in ReInitSubsystems, since initial state has not been
1163 // saved at that time. Short-circuit everything here.
1171 * Built-in command: set log level (0 ... 7)
1174 do_log_level (const SGPropertyNode * arg)
1176 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1183 do_decrease_visibility (const SGPropertyNode * arg)
1185 Environment::Presets::VisibilitySingleton::instance()->adjust( 0.9 );
1190 do_increase_visibility (const SGPropertyNode * arg)
1192 Environment::Presets::VisibilitySingleton::instance()->adjust( 1.1 );
1197 * An fgcommand to allow loading of xml files via nasal,
1198 * the xml file's structure will be made available within
1199 * a property tree node defined under argument "targetnode",
1200 * or in the given argument tree under "data" otherwise.
1202 * @param filename a string to hold the complete path & filename of an XML file
1203 * @param targetnode a string pointing to a location within the property tree
1204 * where to store the parsed XML file. If <targetnode> is undefined, then the
1205 * file contents are stored under a node <data> in the argument tree.
1209 do_load_xml_to_proptree(const SGPropertyNode * arg)
1211 SGPath file(arg->getStringValue("filename"));
1212 if (file.str().empty())
1215 if (file.extension() != "xml")
1216 file.concat(".xml");
1218 std::string icao = arg->getStringValue("icao");
1220 if (file.isRelative()) {
1221 SGPath absPath = globals->resolve_maybe_aircraft_path(file.str());
1222 if (!absPath.isNull())
1226 SG_LOG(SG_IO, SG_ALERT, "loadxml: Cannot find XML property file '"
1227 << file.str() << "'.");
1232 if (!XMLLoader::findAirportData(icao, file.str(), file)) {
1233 SG_LOG(SG_IO, SG_INFO, "loadxml: failed to find airport data for "
1234 << file.str() << " at ICAO:" << icao);
1239 if (!fgValidatePath(file.c_str(), false)) {
1240 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1241 "(unauthorized access)");
1245 SGPropertyNode *targetnode;
1246 if (arg->hasValue("targetnode"))
1247 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1249 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1252 readProperties(file.c_str(), targetnode, true);
1253 } catch (const sg_exception &e) {
1254 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1263 * An fgcommand to allow saving of xml files via nasal,
1264 * the file's structure will be determined based on what's
1265 * encountered in the passed (source) property tree node
1267 * @param filename a string to hold the complete path & filename of the (new)
1269 * @param sourcenode a string pointing to a location within the property tree
1270 * where to find the nodes that should be written recursively into an XML file
1271 * @param data if no sourcenode is given, then the file contents are taken from
1272 * the argument tree's "data" node.
1276 do_save_xml_from_proptree(const SGPropertyNode * arg)
1278 SGPath file(arg->getStringValue("filename"));
1279 if (file.str().empty())
1282 if (file.extension() != "xml")
1283 file.concat(".xml");
1285 if (!fgValidatePath(file.c_str(), true)) {
1286 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1287 "(unauthorized access)");
1291 SGPropertyNode *sourcenode;
1292 if (arg->hasValue("sourcenode"))
1293 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1294 else if (arg->getNode("data", false))
1295 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1300 writeProperties (file.c_str(), sourcenode, true);
1301 } catch (const sg_exception &e) {
1302 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1310 do_press_cockpit_button (const SGPropertyNode *arg)
1312 const char *prefix = arg->getStringValue("prefix");
1314 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1317 string prop = string(prefix) + "-button";
1320 if (arg->getBoolValue("latching"))
1321 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1325 fgSetDouble(prop.c_str(), value);
1326 fgSetBool(arg->getStringValue("discrete"), value > 0);
1332 do_release_cockpit_button (const SGPropertyNode *arg)
1334 const char *prefix = arg->getStringValue("prefix");
1336 if (arg->getBoolValue("guarded")) {
1337 string prop = string(prefix) + "-guard";
1338 if (fgGetDouble(prop.c_str()) < 1) {
1339 fgSetDouble(prop.c_str(), 1);
1344 if (! arg->getBoolValue("latching")) {
1345 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1346 fgSetBool(arg->getStringValue("discrete"), false);
1352 static SGGeod commandSearchPos(const SGPropertyNode* arg)
1354 if (arg->hasChild("longitude-deg") && arg->hasChild("latitude-deg")) {
1355 return SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
1356 arg->getDoubleValue("latitude-deg"));
1359 // use current viewer/aircraft position
1360 return SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
1361 fgGetDouble("/position/latitude-deg"));
1365 do_comm_search(const SGPropertyNode* arg)
1367 SGGeod pos = commandSearchPos(arg);
1368 int khz = static_cast<int>(arg->getDoubleValue("frequency-mhz") * 100.0 + 0.25);
1370 flightgear::CommStation* sta = flightgear::CommStation::findByFreq(khz, pos, NULL);
1375 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1376 sta->createBinding(result);
1381 do_nav_search(const SGPropertyNode* arg)
1383 SGGeod pos = commandSearchPos(arg);
1384 double mhz = arg->getDoubleValue("frequency-mhz");
1386 FGNavList* navList = globals->get_navlist();
1387 string type(arg->getStringValue("type", "vor"));
1388 if (type == "dme") {
1389 navList = globals->get_dmelist();
1390 } else if (type == "tacan") {
1391 navList = globals->get_tacanlist();
1394 FGNavRecord* nav = navList->findByFreq(mhz, pos);
1395 if (!nav && (type == "vor")) {
1396 // if we're searching VORs, look for localizers too
1397 nav = globals->get_loclist()->findByFreq(mhz, pos);
1404 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1405 nav->createBinding(result);
1409 ////////////////////////////////////////////////////////////////////////
1411 ////////////////////////////////////////////////////////////////////////
1415 * Table of built-in commands.
1417 * New commands do not have to be added here; any module in the application
1418 * can add a new command using globals->get_commands()->addCommand(...).
1422 SGCommandMgr::command_t command;
1424 { "null", do_null },
1425 { "nasal", do_nasal },
1426 { "exit", do_exit },
1427 { "reset", do_reset },
1428 { "reinit", do_reinit },
1429 { "suspend", do_reinit },
1430 { "resume", do_reinit },
1431 { "pause", do_pause },
1432 { "load", do_load },
1433 { "save", do_save },
1434 { "panel-load", do_panel_load },
1435 { "panel-mouse-click", do_panel_mouse_click },
1436 { "preferences-load", do_preferences_load },
1437 { "view-cycle", do_view_cycle },
1438 { "screen-capture", do_screen_capture },
1439 { "hires-screen-capture", do_hires_screen_capture },
1440 { "tile-cache-reload", do_tile_cache_reload },
1442 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1443 { "set-outside-air-temp-degc", do_set_oat_degc },
1444 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1445 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1447 { "property-toggle", do_property_toggle },
1448 { "property-assign", do_property_assign },
1449 { "property-adjust", do_property_adjust },
1450 { "property-multiply", do_property_multiply },
1451 { "property-swap", do_property_swap },
1452 { "property-scale", do_property_scale },
1453 { "property-cycle", do_property_cycle },
1454 { "property-randomize", do_property_randomize },
1455 { "data-logging-commit", do_data_logging_commit },
1456 { "dialog-new", do_dialog_new },
1457 { "dialog-show", do_dialog_show },
1458 { "dialog-close", do_dialog_close },
1459 { "dialog-update", do_dialog_update },
1460 { "dialog-apply", do_dialog_apply },
1461 { "open-browser", do_open_browser },
1462 { "gui-redraw", do_gui_redraw },
1463 { "add-model", do_add_model },
1464 { "set-cursor", do_set_cursor },
1465 { "play-audio-sample", do_play_audio_sample },
1466 { "presets-commit", do_presets_commit },
1467 { "log-level", do_log_level },
1468 { "replay", do_replay },
1470 { "decrease-visibility", do_decrease_visibility },
1471 { "increase-visibility", do_increase_visibility },
1473 { "loadxml", do_load_xml_to_proptree},
1474 { "savexml", do_save_xml_from_proptree },
1475 { "press-cockpit-button", do_press_cockpit_button },
1476 { "release-cockpit-button", do_release_cockpit_button },
1477 { "dump-scenegraph", do_dump_scene_graph },
1478 { "dump-terrainbranch", do_dump_terrain_branch },
1479 { "print-visible-scene", do_print_visible_scene_info },
1480 { "reload-shaders", do_reload_shaders },
1482 { "find-navaid", do_nav_search },
1483 { "find-comm", do_comm_search },
1485 { 0, 0 } // zero-terminated
1490 * Initialize the default built-in commands.
1492 * Other commands may be added by other parts of the application.
1497 SG_LOG(SG_GENERAL, SG_BULK, "Initializing basic built-in commands:");
1498 for (int i = 0; built_ins[i].name != 0; i++) {
1499 SG_LOG(SG_GENERAL, SG_BULK, " " << built_ins[i].name);
1500 globals->get_commands()->addCommand(built_ins[i].name,
1501 built_ins[i].command);
1504 typedef bool (*dummy)();
1505 fgTie( "/command/view/next", dummy(0), do_view_next );
1506 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1509 // end of fg_commands.cxx