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);
193 if (fgGetBool("/sim/startup/save-on-exit")) {
194 SGPath autosaveFile(fgGetString("/sim/fg-home"));
195 autosaveFile.append( "autosave.xml" );
196 autosaveFile.create_dir( 0700 );
197 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << autosaveFile.str());
199 writeProperties(autosaveFile.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
200 } catch (const sg_exception &e) {
201 guiErrorMessage("Error writing autosave.xml: ", e);
204 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
208 fgOSExit(arg->getIntValue("status", 0));
214 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
217 do_reset (const SGPropertyNode * arg)
219 fgReInitSubsystems();
225 * Built-in command: reinitialize one or more subsystems.
227 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
228 * none is specified, reinitialize all of them.
231 do_reinit (const SGPropertyNode * arg)
235 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
236 if (subsystems.size() == 0) {
237 globals->get_subsystem_mgr()->reinit();
239 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
240 const char * name = subsystems[i]->getStringValue();
241 SGSubsystem * subsystem = globals->get_subsystem(name);
242 if (subsystem == 0) {
244 SG_LOG( SG_GENERAL, SG_ALERT,
245 "Subsystem " << name << " not found" );
252 globals->get_event_mgr()->reinit();
259 // these routines look useful ??? but are never used in the code ???
263 * Built-in command: suspend one or more subsystems.
265 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
268 do_suspend (const SGPropertyNode * arg)
272 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
273 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
274 const char * name = subsystems[i]->getStringValue();
275 SGSubsystem * subsystem = globals->get_subsystem(name);
276 if (subsystem == 0) {
278 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
280 subsystem->suspend();
287 * Built-in command: suspend one or more subsystems.
289 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
292 do_resume (const SGPropertyNode * arg)
296 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
297 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
298 const char * name = subsystems[i]->getStringValue();
299 SGSubsystem * subsystem = globals->get_subsystem(name);
300 if (subsystem == 0) {
302 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
313 * Built-in command: replay the FDR buffer
316 do_replay (const SGPropertyNode * arg)
318 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
323 * Built-in command: pause/unpause the sim
326 do_pause (const SGPropertyNode * arg)
328 bool paused = fgGetBool("/sim/freeze/master",true) || fgGetBool("/sim/freeze/clock",true);
329 if (paused && (fgGetInt("/sim/freeze/replay-state",0)>0))
335 fgSetBool("/sim/freeze/master",!paused);
336 fgSetBool("/sim/freeze/clock",!paused);
342 * Built-in command: load flight.
344 * file (optional): the name of the file to load (relative to current
345 * directory). Defaults to "fgfs.sav"
348 do_load (const SGPropertyNode * arg)
350 string file = arg->getStringValue("file", "fgfs.sav");
351 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
354 if (!fgValidatePath(file.c_str(), false)) {
355 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
356 "(unauthorized access)");
360 ifstream input(file.c_str());
361 if (input.good() && fgLoadFlight(input)) {
363 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
366 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
373 * Built-in command: save flight.
375 * file (optional): the name of the file to save (relative to the
376 * current directory). Defaults to "fgfs.sav".
379 do_save (const SGPropertyNode * arg)
381 string file = arg->getStringValue("file", "fgfs.sav");
382 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
385 if (!fgValidatePath(file.c_str(), false)) {
386 SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
387 "(unauthorized access)");
391 bool write_all = arg->getBoolValue("write-all", false);
392 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
393 ofstream output(file.c_str());
394 if (output.good() && fgSaveFlight(output, write_all)) {
396 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
399 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
406 * Built-in command: (re)load the panel.
408 * path (optional): the file name to load the panel from
409 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
410 * and if that's unspecified, to "Panels/Default/default.xml".
413 do_panel_load (const SGPropertyNode * arg)
416 arg->getStringValue("path", fgGetString("/sim/panel/path"));
417 if (panel_path.empty()) {
421 FGPanel * new_panel = fgReadPanel(panel_path);
422 if (new_panel == 0) {
423 SG_LOG(SG_INPUT, SG_ALERT,
424 "Error reading new panel from " << panel_path);
427 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
428 globals->get_current_panel()->unbind();
429 delete globals->get_current_panel();
430 globals->set_current_panel( new_panel );
431 globals->get_current_panel()->bind();
437 * Built-in command: pass a mouse click to the panel.
439 * button: the mouse button number, zero-based.
440 * is-down: true if the button is down, false if it is up.
441 * x-pos: the x position of the mouse click.
442 * y-pos: the y position of the mouse click.
445 do_panel_mouse_click (const SGPropertyNode * arg)
447 if (globals->get_current_panel() != 0)
448 return globals->get_current_panel()
449 ->doMouseAction(arg->getIntValue("button"),
450 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
451 arg->getIntValue("x-pos"),
452 arg->getIntValue("y-pos"));
459 * Built-in command: (re)load preferences.
461 * path (optional): the file name to load the panel from (relative
462 * to FG_ROOT). Defaults to "preferences.xml".
465 do_preferences_load (const SGPropertyNode * arg)
468 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
469 globals->get_props());
470 } catch (const sg_exception &e) {
471 guiErrorMessage("Error reading global preferences: ", e);
474 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
481 globals->get_current_view()->setHeadingOffset_deg(0.0);
482 globals->get_viewmgr()->next_view();
488 globals->get_current_view()->setHeadingOffset_deg(0.0);
489 globals->get_viewmgr()->prev_view();
493 * Built-in command: cycle view.
496 do_view_cycle (const SGPropertyNode * arg)
498 globals->get_current_view()->setHeadingOffset_deg(0.0);
499 globals->get_viewmgr()->next_view();
504 * Built-in command: capture screen.
507 do_screen_capture (const SGPropertyNode * arg)
509 return fgDumpSnapShot();
513 do_reload_shaders (const SGPropertyNode*)
515 simgear::reload_shaders();
520 do_dump_scene_graph (const SGPropertyNode*)
527 do_dump_terrain_branch (const SGPropertyNode*)
529 fgDumpTerrainBranch();
531 double lon_deg = fgGetDouble("/position/longitude-deg");
532 double lat_deg = fgGetDouble("/position/latitude-deg");
533 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
534 SGVec3d zero = SGVec3d::fromGeod(geodPos);
536 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
537 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
538 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
544 do_print_visible_scene_info(const SGPropertyNode*)
546 fgPrintVisibleSceneInfoCommand();
551 * Built-in command: hires capture screen.
554 do_hires_screen_capture (const SGPropertyNode * arg)
562 * Reload the tile cache.
565 do_tile_cache_reload (const SGPropertyNode * arg)
567 static const SGPropertyNode *master_freeze
568 = fgGetNode("/sim/freeze/master");
569 bool freeze = master_freeze->getBoolValue();
570 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
572 fgSetBool("/sim/freeze/master", true);
575 globals->get_subsystem("tile-manager")->reinit();
578 fgSetBool("/sim/freeze/master", false);
585 These do_set_(some-environment-parameters) are deprecated and no longer
586 useful/functional - Torsten Dreyer, January 2011
588 * Set the sea level outside air temperature and assigning that to all
589 * boundary and aloft environment layers.
592 do_set_sea_level_degc ( double temp_sea_level_degc)
594 SGPropertyNode *node, *child;
597 node = fgGetNode( "/environment/config/boundary" );
598 if ( node != NULL ) {
600 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
601 child->setDoubleValue( "temperature-sea-level-degc",
602 temp_sea_level_degc );
608 node = fgGetNode( "/environment/config/aloft" );
609 if ( node != NULL ) {
611 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
612 child->setDoubleValue( "temperature-sea-level-degc",
613 temp_sea_level_degc );
622 do_set_sea_level_degc (const SGPropertyNode * arg)
624 return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
629 * Set the outside air temperature at the "current" altitude by first
630 * calculating the corresponding sea level temp, and assigning that to
631 * all boundary and aloft environment layers.
634 do_set_oat_degc (const SGPropertyNode * arg)
636 double oat_degc = arg->getDoubleValue("temp-degc", 15.0);
637 // check for an altitude specified in the arguments, otherwise use
638 // current aircraft altitude.
639 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
640 if ( altitude_ft == NULL ) {
641 altitude_ft = fgGetNode("/position/altitude-ft");
644 FGEnvironment dummy; // instantiate a dummy so we can leech a method
645 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
646 dummy.set_temperature_degc( oat_degc );
647 return do_set_sea_level_degc( dummy.get_temperature_sea_level_degc());
651 * Set the sea level outside air dewpoint and assigning that to all
652 * boundary and aloft environment layers.
655 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
658 SGPropertyNode *node, *child;
661 node = fgGetNode( "/environment/config/boundary" );
662 if ( node != NULL ) {
664 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
665 child->setDoubleValue( "dewpoint-sea-level-degc",
666 dewpoint_sea_level_degc );
672 node = fgGetNode( "/environment/config/aloft" );
673 if ( node != NULL ) {
675 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
676 child->setDoubleValue( "dewpoint-sea-level-degc",
677 dewpoint_sea_level_degc );
686 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
688 return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
692 * Set the outside air dewpoint at the "current" altitude by first
693 * calculating the corresponding sea level dewpoint, and assigning
694 * that to all boundary and aloft environment layers.
697 do_set_dewpoint_degc (const SGPropertyNode * arg)
699 double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
701 // check for an altitude specified in the arguments, otherwise use
702 // current aircraft altitude.
703 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
704 if ( altitude_ft == NULL ) {
705 altitude_ft = fgGetNode("/position/altitude-ft");
708 FGEnvironment dummy; // instantiate a dummy so we can leech a method
709 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
710 dummy.set_dewpoint_degc( dewpoint_degc );
711 return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
716 * Built-in command: toggle a bool property value.
718 * property: The name of the property to toggle.
721 do_property_toggle (const SGPropertyNode * arg)
723 SGPropertyNode * prop = get_prop(arg);
724 return prop->setBoolValue(!prop->getBoolValue());
729 * Built-in command: assign a value to a property.
731 * property: the name of the property to assign.
732 * value: the value to assign; or
733 * property[1]: the property to copy from.
736 do_property_assign (const SGPropertyNode * arg)
738 SGPropertyNode * prop = get_prop(arg);
739 const SGPropertyNode * value = arg->getNode("value");
742 return prop->setUnspecifiedValue(value->getStringValue());
745 const SGPropertyNode * prop2 = get_prop2(arg);
747 return prop->setUnspecifiedValue(prop2->getStringValue());
755 * Built-in command: increment or decrement a property value.
757 * If the 'step' argument is present, it will be used; otherwise,
758 * the command uses 'offset' and 'factor', usually from the mouse.
760 * property: the name of the property to increment or decrement.
761 * step: the amount of the increment or decrement (default: 0).
762 * offset: offset from the current setting (used for the mouse; multiplied
764 * factor: scaling amount for the offset (defaults to 1).
765 * min: the minimum allowed value (default: no minimum).
766 * max: the maximum allowed value (default: no maximum).
767 * mask: 'integer' to apply only to the left of the decimal point,
768 * 'decimal' to apply only to the right of the decimal point,
769 * or 'all' to apply to the whole number (the default).
770 * wrap: true if the value should be wrapped when it passes min or max;
771 * both min and max must be present for this to work (default:
775 do_property_adjust (const SGPropertyNode * arg)
777 SGPropertyNode * prop = get_prop(arg);
780 if (arg->hasValue("step"))
781 amount = arg->getDoubleValue("step");
783 amount = (arg->getDoubleValue("factor", 1)
784 * arg->getDoubleValue("offset"));
786 double unmodifiable, modifiable;
787 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
788 &unmodifiable, &modifiable);
789 modifiable += amount;
790 limit_value(&modifiable, arg);
792 prop->setDoubleValue(unmodifiable + modifiable);
799 * Built-in command: multiply a property value.
801 * property: the name of the property to multiply.
802 * factor: the amount by which to multiply.
803 * min: the minimum allowed value (default: no minimum).
804 * max: the maximum allowed value (default: no maximum).
805 * mask: 'integer' to apply only to the left of the decimal point,
806 * 'decimal' to apply only to the right of the decimal point,
807 * or 'all' to apply to the whole number (the default).
808 * wrap: true if the value should be wrapped when it passes min or max;
809 * both min and max must be present for this to work (default:
813 do_property_multiply (const SGPropertyNode * arg)
815 SGPropertyNode * prop = get_prop(arg);
816 double factor = arg->getDoubleValue("factor", 1);
818 double unmodifiable, modifiable;
819 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
820 &unmodifiable, &modifiable);
821 modifiable *= factor;
822 limit_value(&modifiable, arg);
824 prop->setDoubleValue(unmodifiable + modifiable);
831 * Built-in command: swap two property values.
833 * property[0]: the name of the first property.
834 * property[1]: the name of the second property.
837 do_property_swap (const SGPropertyNode * arg)
839 SGPropertyNode * prop1 = get_prop(arg);
840 SGPropertyNode * prop2 = get_prop2(arg);
842 // FIXME: inefficient
843 const string & tmp = prop1->getStringValue();
844 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
845 prop2->setUnspecifiedValue(tmp.c_str()));
850 * Built-in command: Set a property to an axis or other moving input.
852 * property: the name of the property to set.
853 * setting: the current input setting, usually between -1.0 and 1.0.
854 * offset: the offset to shift by, before applying the factor.
855 * factor: the factor to multiply by (use negative to reverse).
858 do_property_scale (const SGPropertyNode * arg)
860 SGPropertyNode * prop = get_prop(arg);
861 double setting = arg->getDoubleValue("setting");
862 double offset = arg->getDoubleValue("offset", 0.0);
863 double factor = arg->getDoubleValue("factor", 1.0);
864 bool squared = arg->getBoolValue("squared", false);
865 int power = arg->getIntValue("power", (squared ? 2 : 1));
867 int sign = (setting < 0 ? -1 : 1);
873 setting = setting * setting * sign;
876 setting = setting * setting * setting;
879 setting = setting * setting * setting * setting * sign;
882 setting = pow(setting, power);
883 if ((power % 2) == 0)
888 return prop->setDoubleValue((setting + offset) * factor);
893 * Built-in command: cycle a property through a set of values.
895 * If the current value isn't in the list, the cycle will
896 * (re)start from the beginning.
898 * property: the name of the property to cycle.
899 * value[*]: the list of values to cycle through.
902 do_property_cycle (const SGPropertyNode * arg)
904 SGPropertyNode * prop = get_prop(arg);
905 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
907 int nSelections = values.size();
909 if (nSelections < 1) {
910 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
914 // Try to find the current selection
915 for (int i = 0; i < nSelections; i++) {
916 if (compare_values(prop, values[i])) {
922 // Default or wrap to the first selection
923 if (selection < 0 || selection >= nSelections)
926 prop->setUnspecifiedValue(values[selection]->getStringValue());
932 * Built-in command: randomize a numeric property value.
934 * property: the name of the property value to randomize.
935 * min: the minimum allowed value.
936 * max: the maximum allowed value.
939 do_property_randomize (const SGPropertyNode * arg)
941 SGPropertyNode * prop = get_prop(arg);
942 double min = arg->getDoubleValue("min", DBL_MIN);
943 double max = arg->getDoubleValue("max", DBL_MAX);
944 prop->setDoubleValue(sg_random() * (max - min) + min);
950 * Built-in command: reinit the data logging system based on the
951 * current contents of the /logger tree.
954 do_data_logging_commit (const SGPropertyNode * arg)
956 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
962 * Built-in command: Add a dialog to the GUI system. Does *not*
963 * display the dialog. The property node should have the same format
964 * as a dialog XML configuration. It must include:
966 * name: the name of the GUI dialog for future reference.
969 do_dialog_new (const SGPropertyNode * arg)
971 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
973 // Note the casting away of const: this is *real*. Doing a
974 // "dialog-apply" command later on will mutate this property node.
975 // I'm not convinced that this isn't the Right Thing though; it
976 // allows client to create a node, pass it to dialog-new, and get
977 // the values back from the dialog by reading the same node.
978 // Perhaps command arguments are not as "const" as they would
980 gui->newDialog((SGPropertyNode*)arg);
985 * Built-in command: Show an XML-configured dialog.
987 * dialog-name: the name of the GUI dialog to display.
990 do_dialog_show (const SGPropertyNode * arg)
992 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
993 gui->showDialog(arg->getStringValue("dialog-name"));
999 * Built-in Command: Hide the active XML-configured dialog.
1002 do_dialog_close (const SGPropertyNode * arg)
1004 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1005 if(arg->hasValue("dialog-name"))
1006 return gui->closeDialog(arg->getStringValue("dialog-name"));
1007 return gui->closeActiveDialog();
1012 * Update a value in the active XML-configured dialog.
1014 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1017 do_dialog_update (const SGPropertyNode * arg)
1019 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1021 if (arg->hasValue("dialog-name"))
1022 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1024 dialog = gui->getActiveDialog();
1027 dialog->updateValues(arg->getStringValue("object-name"));
1035 do_open_browser (const SGPropertyNode * arg)
1038 if (arg->hasValue("path"))
1039 path = arg->getStringValue("path");
1041 if (arg->hasValue("url"))
1042 path = arg->getStringValue("url");
1046 return openBrowser(path);
1050 * Apply a value in the active XML-configured dialog.
1052 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1055 do_dialog_apply (const SGPropertyNode * arg)
1057 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1059 if (arg->hasValue("dialog-name"))
1060 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1062 dialog = gui->getActiveDialog();
1065 dialog->applyValues(arg->getStringValue("object-name"));
1074 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1078 do_gui_redraw (const SGPropertyNode * arg)
1080 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1087 * Adds model to the scenery. The path to the added branch (/models/model[*])
1088 * is returned in property "property".
1091 do_add_model (const SGPropertyNode * arg)
1093 SGPropertyNode * model = fgGetNode("models", true);
1095 for (i = 0; model->hasChild("model",i); i++);
1096 model = model->getChild("model", i, true);
1097 copyProperties(arg, model);
1098 if (model->hasValue("elevation-m"))
1099 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1100 * SG_METER_TO_FEET);
1101 model->getNode("load", true);
1102 model->removeChildren("load");
1103 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1109 * Set mouse cursor coordinates and cursor shape.
1112 do_set_cursor (const SGPropertyNode * arg)
1114 if (arg->hasValue("x") || arg->hasValue("y")) {
1115 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1116 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1117 int x = arg->getIntValue("x", mx->getIntValue());
1118 int y = arg->getIntValue("y", my->getIntValue());
1124 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1125 if (cursor->getType() != simgear::props::NONE)
1126 fgSetMouseCursor(cursor->getIntValue());
1128 cursor->setIntValue(fgGetMouseCursor());
1134 * Built-in command: play an audio message (i.e. a wav file) This is
1135 * fire and forget. Call this once per message and it will get dumped
1136 * into a queue. Messages are played sequentially so they do not
1140 do_play_audio_sample (const SGPropertyNode * arg)
1142 string path = arg->getStringValue("path");
1143 string file = arg->getStringValue("file");
1144 float volume = arg->getFloatValue("volume");
1145 // cout << "playing " << path << " / " << file << endl;
1147 static FGSampleQueue *queue = 0;
1149 SGSoundMgr *smgr = globals->get_soundmgr();
1150 queue = new FGSampleQueue(smgr, "chatter");
1151 queue->tie_to_listener();
1154 SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1155 msg->set_volume( volume );
1160 } catch (const sg_io_exception&) {
1161 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1162 "failed to load" << path << '/' << file);
1168 * Built-in command: commit presets (read from in /sim/presets/)
1171 do_presets_commit (const SGPropertyNode * arg)
1173 if (fgGetBool("/sim/initialized", false)) {
1174 fgReInitSubsystems();
1176 // Nasal can trigger this during initial init, which confuses
1177 // the logic in ReInitSubsystems, since initial state has not been
1178 // saved at that time. Short-circuit everything here.
1186 * Built-in command: set log level (0 ... 7)
1189 do_log_level (const SGPropertyNode * arg)
1191 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1198 do_decrease_visibility (const SGPropertyNode * arg)
1200 Environment::Presets::VisibilitySingleton::instance()->adjust( 0.9 );
1205 do_increase_visibility (const SGPropertyNode * arg)
1207 Environment::Presets::VisibilitySingleton::instance()->adjust( 1.1 );
1212 * An fgcommand to allow loading of xml files via nasal,
1213 * the xml file's structure will be made available within
1214 * a property tree node defined under argument "targetnode",
1215 * or in the given argument tree under "data" otherwise.
1217 * @param filename a string to hold the complete path & filename of an XML file
1218 * @param targetnode a string pointing to a location within the property tree
1219 * where to store the parsed XML file. If <targetnode> is undefined, then the
1220 * file contents are stored under a node <data> in the argument tree.
1224 do_load_xml_to_proptree(const SGPropertyNode * arg)
1226 SGPath file(arg->getStringValue("filename"));
1227 if (file.str().empty())
1230 if (file.extension() != "xml")
1231 file.concat(".xml");
1233 std::string icao = arg->getStringValue("icao");
1235 if (file.isRelative()) {
1236 SGPath absPath = globals->resolve_maybe_aircraft_path(file.str());
1237 if (!absPath.isNull())
1241 SG_LOG(SG_IO, SG_ALERT, "loadxml: Cannot find XML property file '"
1242 << file.str() << "'.");
1247 if (!XMLLoader::findAirportData(icao, file.str(), file)) {
1248 SG_LOG(SG_IO, SG_INFO, "loadxml: failed to find airport data for "
1249 << file.str() << " at ICAO:" << icao);
1254 if (!fgValidatePath(file.c_str(), false)) {
1255 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1256 "(unauthorized access)");
1260 SGPropertyNode *targetnode;
1261 if (arg->hasValue("targetnode"))
1262 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1264 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1267 readProperties(file.c_str(), targetnode, true);
1268 } catch (const sg_exception &e) {
1269 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1278 * An fgcommand to allow saving of xml files via nasal,
1279 * the file's structure will be determined based on what's
1280 * encountered in the passed (source) property tree node
1282 * @param filename a string to hold the complete path & filename of the (new)
1284 * @param sourcenode a string pointing to a location within the property tree
1285 * where to find the nodes that should be written recursively into an XML file
1286 * @param data if no sourcenode is given, then the file contents are taken from
1287 * the argument tree's "data" node.
1291 do_save_xml_from_proptree(const SGPropertyNode * arg)
1293 SGPath file(arg->getStringValue("filename"));
1294 if (file.str().empty())
1297 if (file.extension() != "xml")
1298 file.concat(".xml");
1300 if (!fgValidatePath(file.c_str(), true)) {
1301 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1302 "(unauthorized access)");
1306 SGPropertyNode *sourcenode;
1307 if (arg->hasValue("sourcenode"))
1308 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1309 else if (arg->getNode("data", false))
1310 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1315 writeProperties (file.c_str(), sourcenode, true);
1316 } catch (const sg_exception &e) {
1317 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1325 do_press_cockpit_button (const SGPropertyNode *arg)
1327 const char *prefix = arg->getStringValue("prefix");
1329 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1332 string prop = string(prefix) + "-button";
1335 if (arg->getBoolValue("latching"))
1336 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1340 fgSetDouble(prop.c_str(), value);
1341 fgSetBool(arg->getStringValue("discrete"), value > 0);
1347 do_release_cockpit_button (const SGPropertyNode *arg)
1349 const char *prefix = arg->getStringValue("prefix");
1351 if (arg->getBoolValue("guarded")) {
1352 string prop = string(prefix) + "-guard";
1353 if (fgGetDouble(prop.c_str()) < 1) {
1354 fgSetDouble(prop.c_str(), 1);
1359 if (! arg->getBoolValue("latching")) {
1360 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1361 fgSetBool(arg->getStringValue("discrete"), false);
1367 static SGGeod commandSearchPos(const SGPropertyNode* arg)
1369 if (arg->hasChild("longitude-deg") && arg->hasChild("latitude-deg")) {
1370 return SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
1371 arg->getDoubleValue("latitude-deg"));
1374 // use current viewer/aircraft position
1375 return SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
1376 fgGetDouble("/position/latitude-deg"));
1380 do_comm_search(const SGPropertyNode* arg)
1382 SGGeod pos = commandSearchPos(arg);
1383 int khz = static_cast<int>(arg->getDoubleValue("frequency-mhz") * 100.0 + 0.25);
1385 flightgear::CommStation* sta = flightgear::CommStation::findByFreq(khz, pos, NULL);
1390 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1391 sta->createBinding(result);
1396 do_nav_search(const SGPropertyNode* arg)
1398 SGGeod pos = commandSearchPos(arg);
1399 double mhz = arg->getDoubleValue("frequency-mhz");
1401 FGNavList* navList = globals->get_navlist();
1402 string type(arg->getStringValue("type", "vor"));
1403 if (type == "dme") {
1404 navList = globals->get_dmelist();
1405 } else if (type == "tacan") {
1406 navList = globals->get_tacanlist();
1409 FGNavRecord* nav = navList->findByFreq(mhz, pos);
1410 if (!nav && (type == "vor")) {
1411 // if we're searching VORs, look for localizers too
1412 nav = globals->get_loclist()->findByFreq(mhz, pos);
1419 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1420 nav->createBinding(result);
1424 ////////////////////////////////////////////////////////////////////////
1426 ////////////////////////////////////////////////////////////////////////
1430 * Table of built-in commands.
1432 * New commands do not have to be added here; any module in the application
1433 * can add a new command using globals->get_commands()->addCommand(...).
1437 SGCommandMgr::command_t command;
1439 { "null", do_null },
1440 { "nasal", do_nasal },
1441 { "exit", do_exit },
1442 { "reset", do_reset },
1443 { "reinit", do_reinit },
1444 { "suspend", do_reinit },
1445 { "resume", do_reinit },
1446 { "pause", do_pause },
1447 { "load", do_load },
1448 { "save", do_save },
1449 { "panel-load", do_panel_load },
1450 { "panel-mouse-click", do_panel_mouse_click },
1451 { "preferences-load", do_preferences_load },
1452 { "view-cycle", do_view_cycle },
1453 { "screen-capture", do_screen_capture },
1454 { "hires-screen-capture", do_hires_screen_capture },
1455 { "tile-cache-reload", do_tile_cache_reload },
1457 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1458 { "set-outside-air-temp-degc", do_set_oat_degc },
1459 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1460 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1462 { "property-toggle", do_property_toggle },
1463 { "property-assign", do_property_assign },
1464 { "property-adjust", do_property_adjust },
1465 { "property-multiply", do_property_multiply },
1466 { "property-swap", do_property_swap },
1467 { "property-scale", do_property_scale },
1468 { "property-cycle", do_property_cycle },
1469 { "property-randomize", do_property_randomize },
1470 { "data-logging-commit", do_data_logging_commit },
1471 { "dialog-new", do_dialog_new },
1472 { "dialog-show", do_dialog_show },
1473 { "dialog-close", do_dialog_close },
1474 { "dialog-update", do_dialog_update },
1475 { "dialog-apply", do_dialog_apply },
1476 { "open-browser", do_open_browser },
1477 { "gui-redraw", do_gui_redraw },
1478 { "add-model", do_add_model },
1479 { "set-cursor", do_set_cursor },
1480 { "play-audio-sample", do_play_audio_sample },
1481 { "presets-commit", do_presets_commit },
1482 { "log-level", do_log_level },
1483 { "replay", do_replay },
1485 { "decrease-visibility", do_decrease_visibility },
1486 { "increase-visibility", do_increase_visibility },
1488 { "loadxml", do_load_xml_to_proptree},
1489 { "savexml", do_save_xml_from_proptree },
1490 { "press-cockpit-button", do_press_cockpit_button },
1491 { "release-cockpit-button", do_release_cockpit_button },
1492 { "dump-scenegraph", do_dump_scene_graph },
1493 { "dump-terrainbranch", do_dump_terrain_branch },
1494 { "print-visible-scene", do_print_visible_scene_info },
1495 { "reload-shaders", do_reload_shaders },
1497 { "find-navaid", do_nav_search },
1498 { "find-comm", do_comm_search },
1500 { 0, 0 } // zero-terminated
1505 * Initialize the default built-in commands.
1507 * Other commands may be added by other parts of the application.
1512 SG_LOG(SG_GENERAL, SG_BULK, "Initializing basic built-in commands:");
1513 for (int i = 0; built_ins[i].name != 0; i++) {
1514 SG_LOG(SG_GENERAL, SG_BULK, " " << built_ins[i].name);
1515 globals->get_commands()->addCommand(built_ins[i].name,
1516 built_ins[i].command);
1519 typedef bool (*dummy)();
1520 fgTie( "/command/view/next", dummy(0), do_view_next );
1521 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1524 // end of fg_commands.cxx