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")) {
195 char* envp = ::getenv( "APPDATA" );
196 if ( envp != NULL ) {
197 SGPath config( envp );
198 config.append( "flightgear.org" );
200 if ( homedir != NULL ) {
201 SGPath config( homedir );
202 config.append( ".fgfs" );
204 config.append( "autosave.xml" );
205 config.create_dir( 0700 );
206 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
208 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
209 } catch (const sg_exception &e) {
210 guiErrorMessage("Error writing autosave.xml: ", e);
213 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
217 fgOSExit(arg->getIntValue("status", 0));
223 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
226 do_reset (const SGPropertyNode * arg)
228 fgReInitSubsystems();
234 * Built-in command: reinitialize one or more subsystems.
236 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
237 * none is specified, reinitialize all of them.
240 do_reinit (const SGPropertyNode * arg)
244 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
245 if (subsystems.size() == 0) {
246 globals->get_subsystem_mgr()->reinit();
248 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
249 const char * name = subsystems[i]->getStringValue();
250 SGSubsystem * subsystem = globals->get_subsystem(name);
251 if (subsystem == 0) {
253 SG_LOG( SG_GENERAL, SG_ALERT,
254 "Subsystem " << name << " not found" );
261 globals->get_event_mgr()->reinit();
268 // these routines look useful ??? but are never used in the code ???
272 * Built-in command: suspend one or more subsystems.
274 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
277 do_suspend (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");
289 subsystem->suspend();
296 * Built-in command: suspend one or more subsystems.
298 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
301 do_resume (const SGPropertyNode * arg)
305 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
306 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
307 const char * name = subsystems[i]->getStringValue();
308 SGSubsystem * subsystem = globals->get_subsystem(name);
309 if (subsystem == 0) {
311 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
322 do_pause (const SGPropertyNode * arg)
324 bool paused = fgGetBool("/sim/freeze/master",true) || fgGetBool("/sim/freeze/clock",true);
325 fgSetBool("/sim/freeze/master",!paused);
326 fgSetBool("/sim/freeze/clock",!paused);
327 if (fgGetBool("/sim/freeze/replay-state",false))
328 fgSetBool("/sim/replay/disable",true);
333 * Built-in command: load flight.
335 * file (optional): the name of the file to load (relative to current
336 * directory). Defaults to "fgfs.sav"
339 do_load (const SGPropertyNode * arg)
341 string file = arg->getStringValue("file", "fgfs.sav");
342 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
345 if (!fgValidatePath(file.c_str(), false)) {
346 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
347 "(unauthorized access)");
351 ifstream input(file.c_str());
352 if (input.good() && fgLoadFlight(input)) {
354 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
357 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
364 * Built-in command: save flight.
366 * file (optional): the name of the file to save (relative to the
367 * current directory). Defaults to "fgfs.sav".
370 do_save (const SGPropertyNode * arg)
372 string file = arg->getStringValue("file", "fgfs.sav");
373 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
376 if (!fgValidatePath(file.c_str(), false)) {
377 SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
378 "(unauthorized access)");
382 bool write_all = arg->getBoolValue("write-all", false);
383 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
384 ofstream output(file.c_str());
385 if (output.good() && fgSaveFlight(output, write_all)) {
387 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
390 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
397 * Built-in command: (re)load the panel.
399 * path (optional): the file name to load the panel from
400 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
401 * and if that's unspecified, to "Panels/Default/default.xml".
404 do_panel_load (const SGPropertyNode * arg)
407 arg->getStringValue("path", fgGetString("/sim/panel/path"));
408 if (panel_path.empty()) {
412 FGPanel * new_panel = fgReadPanel(panel_path);
413 if (new_panel == 0) {
414 SG_LOG(SG_INPUT, SG_ALERT,
415 "Error reading new panel from " << panel_path);
418 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
419 globals->get_current_panel()->unbind();
420 delete globals->get_current_panel();
421 globals->set_current_panel( new_panel );
422 globals->get_current_panel()->bind();
428 * Built-in command: pass a mouse click to the panel.
430 * button: the mouse button number, zero-based.
431 * is-down: true if the button is down, false if it is up.
432 * x-pos: the x position of the mouse click.
433 * y-pos: the y position of the mouse click.
436 do_panel_mouse_click (const SGPropertyNode * arg)
438 if (globals->get_current_panel() != 0)
439 return globals->get_current_panel()
440 ->doMouseAction(arg->getIntValue("button"),
441 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
442 arg->getIntValue("x-pos"),
443 arg->getIntValue("y-pos"));
450 * Built-in command: (re)load preferences.
452 * path (optional): the file name to load the panel from (relative
453 * to FG_ROOT). Defaults to "preferences.xml".
456 do_preferences_load (const SGPropertyNode * arg)
459 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
460 globals->get_props());
461 } catch (const sg_exception &e) {
462 guiErrorMessage("Error reading global preferences: ", e);
465 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
472 globals->get_current_view()->setHeadingOffset_deg(0.0);
473 globals->get_viewmgr()->next_view();
479 globals->get_current_view()->setHeadingOffset_deg(0.0);
480 globals->get_viewmgr()->prev_view();
484 * Built-in command: cycle view.
487 do_view_cycle (const SGPropertyNode * arg)
489 globals->get_current_view()->setHeadingOffset_deg(0.0);
490 globals->get_viewmgr()->next_view();
495 * Built-in command: capture screen.
498 do_screen_capture (const SGPropertyNode * arg)
500 return fgDumpSnapShot();
504 do_reload_shaders (const SGPropertyNode*)
506 simgear::reload_shaders();
511 do_dump_scene_graph (const SGPropertyNode*)
518 do_dump_terrain_branch (const SGPropertyNode*)
520 fgDumpTerrainBranch();
522 double lon_deg = fgGetDouble("/position/longitude-deg");
523 double lat_deg = fgGetDouble("/position/latitude-deg");
524 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
525 SGVec3d zero = SGVec3d::fromGeod(geodPos);
527 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
528 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
529 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
535 do_print_visible_scene_info(const SGPropertyNode*)
537 fgPrintVisibleSceneInfoCommand();
542 * Built-in command: hires capture screen.
545 do_hires_screen_capture (const SGPropertyNode * arg)
553 * Reload the tile cache.
556 do_tile_cache_reload (const SGPropertyNode * arg)
558 static const SGPropertyNode *master_freeze
559 = fgGetNode("/sim/freeze/master");
560 bool freeze = master_freeze->getBoolValue();
561 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
563 fgSetBool("/sim/freeze/master", true);
566 globals->get_subsystem("tile-manager")->reinit();
569 fgSetBool("/sim/freeze/master", false);
576 These do_set_(some-environment-parameters) are deprecated and no longer
577 useful/functional - Torsten Dreyer, January 2011
579 * Set the sea level outside air temperature and assigning that to all
580 * boundary and aloft environment layers.
583 do_set_sea_level_degc ( double temp_sea_level_degc)
585 SGPropertyNode *node, *child;
588 node = fgGetNode( "/environment/config/boundary" );
589 if ( node != NULL ) {
591 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
592 child->setDoubleValue( "temperature-sea-level-degc",
593 temp_sea_level_degc );
599 node = fgGetNode( "/environment/config/aloft" );
600 if ( node != NULL ) {
602 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
603 child->setDoubleValue( "temperature-sea-level-degc",
604 temp_sea_level_degc );
613 do_set_sea_level_degc (const SGPropertyNode * arg)
615 return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
620 * Set the outside air temperature at the "current" altitude by first
621 * calculating the corresponding sea level temp, and assigning that to
622 * all boundary and aloft environment layers.
625 do_set_oat_degc (const SGPropertyNode * arg)
627 double oat_degc = arg->getDoubleValue("temp-degc", 15.0);
628 // check for an altitude specified in the arguments, otherwise use
629 // current aircraft altitude.
630 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
631 if ( altitude_ft == NULL ) {
632 altitude_ft = fgGetNode("/position/altitude-ft");
635 FGEnvironment dummy; // instantiate a dummy so we can leech a method
636 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
637 dummy.set_temperature_degc( oat_degc );
638 return do_set_sea_level_degc( dummy.get_temperature_sea_level_degc());
642 * Set the sea level outside air dewpoint and assigning that to all
643 * boundary and aloft environment layers.
646 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
649 SGPropertyNode *node, *child;
652 node = fgGetNode( "/environment/config/boundary" );
653 if ( node != NULL ) {
655 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
656 child->setDoubleValue( "dewpoint-sea-level-degc",
657 dewpoint_sea_level_degc );
663 node = fgGetNode( "/environment/config/aloft" );
664 if ( node != NULL ) {
666 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
667 child->setDoubleValue( "dewpoint-sea-level-degc",
668 dewpoint_sea_level_degc );
677 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
679 return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
683 * Set the outside air dewpoint at the "current" altitude by first
684 * calculating the corresponding sea level dewpoint, and assigning
685 * that to all boundary and aloft environment layers.
688 do_set_dewpoint_degc (const SGPropertyNode * arg)
690 double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
692 // check for an altitude specified in the arguments, otherwise use
693 // current aircraft altitude.
694 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
695 if ( altitude_ft == NULL ) {
696 altitude_ft = fgGetNode("/position/altitude-ft");
699 FGEnvironment dummy; // instantiate a dummy so we can leech a method
700 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
701 dummy.set_dewpoint_degc( dewpoint_degc );
702 return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
707 * Built-in command: toggle a bool property value.
709 * property: The name of the property to toggle.
712 do_property_toggle (const SGPropertyNode * arg)
714 SGPropertyNode * prop = get_prop(arg);
715 return prop->setBoolValue(!prop->getBoolValue());
720 * Built-in command: assign a value to a property.
722 * property: the name of the property to assign.
723 * value: the value to assign; or
724 * property[1]: the property to copy from.
727 do_property_assign (const SGPropertyNode * arg)
729 SGPropertyNode * prop = get_prop(arg);
730 const SGPropertyNode * value = arg->getNode("value");
733 return prop->setUnspecifiedValue(value->getStringValue());
736 const SGPropertyNode * prop2 = get_prop2(arg);
738 return prop->setUnspecifiedValue(prop2->getStringValue());
746 * Built-in command: increment or decrement a property value.
748 * If the 'step' argument is present, it will be used; otherwise,
749 * the command uses 'offset' and 'factor', usually from the mouse.
751 * property: the name of the property to increment or decrement.
752 * step: the amount of the increment or decrement (default: 0).
753 * offset: offset from the current setting (used for the mouse; multiplied
755 * factor: scaling amount for the offset (defaults to 1).
756 * min: the minimum allowed value (default: no minimum).
757 * max: the maximum allowed value (default: no maximum).
758 * mask: 'integer' to apply only to the left of the decimal point,
759 * 'decimal' to apply only to the right of the decimal point,
760 * or 'all' to apply to the whole number (the default).
761 * wrap: true if the value should be wrapped when it passes min or max;
762 * both min and max must be present for this to work (default:
766 do_property_adjust (const SGPropertyNode * arg)
768 SGPropertyNode * prop = get_prop(arg);
771 if (arg->hasValue("step"))
772 amount = arg->getDoubleValue("step");
774 amount = (arg->getDoubleValue("factor", 1)
775 * arg->getDoubleValue("offset"));
777 double unmodifiable, modifiable;
778 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
779 &unmodifiable, &modifiable);
780 modifiable += amount;
781 limit_value(&modifiable, arg);
783 prop->setDoubleValue(unmodifiable + modifiable);
790 * Built-in command: multiply a property value.
792 * property: the name of the property to multiply.
793 * factor: the amount by which to multiply.
794 * min: the minimum allowed value (default: no minimum).
795 * max: the maximum allowed value (default: no maximum).
796 * mask: 'integer' to apply only to the left of the decimal point,
797 * 'decimal' to apply only to the right of the decimal point,
798 * or 'all' to apply to the whole number (the default).
799 * wrap: true if the value should be wrapped when it passes min or max;
800 * both min and max must be present for this to work (default:
804 do_property_multiply (const SGPropertyNode * arg)
806 SGPropertyNode * prop = get_prop(arg);
807 double factor = arg->getDoubleValue("factor", 1);
809 double unmodifiable, modifiable;
810 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
811 &unmodifiable, &modifiable);
812 modifiable *= factor;
813 limit_value(&modifiable, arg);
815 prop->setDoubleValue(unmodifiable + modifiable);
822 * Built-in command: swap two property values.
824 * property[0]: the name of the first property.
825 * property[1]: the name of the second property.
828 do_property_swap (const SGPropertyNode * arg)
830 SGPropertyNode * prop1 = get_prop(arg);
831 SGPropertyNode * prop2 = get_prop2(arg);
833 // FIXME: inefficient
834 const string & tmp = prop1->getStringValue();
835 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
836 prop2->setUnspecifiedValue(tmp.c_str()));
841 * Built-in command: Set a property to an axis or other moving input.
843 * property: the name of the property to set.
844 * setting: the current input setting, usually between -1.0 and 1.0.
845 * offset: the offset to shift by, before applying the factor.
846 * factor: the factor to multiply by (use negative to reverse).
849 do_property_scale (const SGPropertyNode * arg)
851 SGPropertyNode * prop = get_prop(arg);
852 double setting = arg->getDoubleValue("setting");
853 double offset = arg->getDoubleValue("offset", 0.0);
854 double factor = arg->getDoubleValue("factor", 1.0);
855 bool squared = arg->getBoolValue("squared", false);
856 int power = arg->getIntValue("power", (squared ? 2 : 1));
858 int sign = (setting < 0 ? -1 : 1);
864 setting = setting * setting * sign;
867 setting = setting * setting * setting;
870 setting = setting * setting * setting * setting * sign;
873 setting = pow(setting, power);
874 if ((power % 2) == 0)
879 return prop->setDoubleValue((setting + offset) * factor);
884 * Built-in command: cycle a property through a set of values.
886 * If the current value isn't in the list, the cycle will
887 * (re)start from the beginning.
889 * property: the name of the property to cycle.
890 * value[*]: the list of values to cycle through.
893 do_property_cycle (const SGPropertyNode * arg)
895 SGPropertyNode * prop = get_prop(arg);
896 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
898 int nSelections = values.size();
900 if (nSelections < 1) {
901 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
905 // Try to find the current selection
906 for (int i = 0; i < nSelections; i++) {
907 if (compare_values(prop, values[i])) {
913 // Default or wrap to the first selection
914 if (selection < 0 || selection >= nSelections)
917 prop->setUnspecifiedValue(values[selection]->getStringValue());
923 * Built-in command: randomize a numeric property value.
925 * property: the name of the property value to randomize.
926 * min: the minimum allowed value.
927 * max: the maximum allowed value.
930 do_property_randomize (const SGPropertyNode * arg)
932 SGPropertyNode * prop = get_prop(arg);
933 double min = arg->getDoubleValue("min", DBL_MIN);
934 double max = arg->getDoubleValue("max", DBL_MAX);
935 prop->setDoubleValue(sg_random() * (max - min) + min);
941 * Built-in command: reinit the data logging system based on the
942 * current contents of the /logger tree.
945 do_data_logging_commit (const SGPropertyNode * arg)
947 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
953 * Built-in command: Add a dialog to the GUI system. Does *not*
954 * display the dialog. The property node should have the same format
955 * as a dialog XML configuration. It must include:
957 * name: the name of the GUI dialog for future reference.
960 do_dialog_new (const SGPropertyNode * arg)
962 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
964 // Note the casting away of const: this is *real*. Doing a
965 // "dialog-apply" command later on will mutate this property node.
966 // I'm not convinced that this isn't the Right Thing though; it
967 // allows client to create a node, pass it to dialog-new, and get
968 // the values back from the dialog by reading the same node.
969 // Perhaps command arguments are not as "const" as they would
971 gui->newDialog((SGPropertyNode*)arg);
976 * Built-in command: Show an XML-configured dialog.
978 * dialog-name: the name of the GUI dialog to display.
981 do_dialog_show (const SGPropertyNode * arg)
983 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
984 gui->showDialog(arg->getStringValue("dialog-name"));
990 * Built-in Command: Hide the active XML-configured dialog.
993 do_dialog_close (const SGPropertyNode * arg)
995 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
996 if(arg->hasValue("dialog-name"))
997 return gui->closeDialog(arg->getStringValue("dialog-name"));
998 return gui->closeActiveDialog();
1003 * Update a value in the active XML-configured dialog.
1005 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1008 do_dialog_update (const SGPropertyNode * arg)
1010 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1012 if (arg->hasValue("dialog-name"))
1013 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1015 dialog = gui->getActiveDialog();
1018 dialog->updateValues(arg->getStringValue("object-name"));
1027 * Apply a value in the active XML-configured dialog.
1029 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1032 do_dialog_apply (const SGPropertyNode * arg)
1034 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1036 if (arg->hasValue("dialog-name"))
1037 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1039 dialog = gui->getActiveDialog();
1042 dialog->applyValues(arg->getStringValue("object-name"));
1051 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1055 do_gui_redraw (const SGPropertyNode * arg)
1057 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1064 * Adds model to the scenery. The path to the added branch (/models/model[*])
1065 * is returned in property "property".
1068 do_add_model (const SGPropertyNode * arg)
1070 SGPropertyNode * model = fgGetNode("models", true);
1072 for (i = 0; model->hasChild("model",i); i++);
1073 model = model->getChild("model", i, true);
1074 copyProperties(arg, model);
1075 if (model->hasValue("elevation-m"))
1076 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1077 * SG_METER_TO_FEET);
1078 model->getNode("load", true);
1079 model->removeChildren("load");
1080 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1086 * Set mouse cursor coordinates and cursor shape.
1089 do_set_cursor (const SGPropertyNode * arg)
1091 if (arg->hasValue("x") || arg->hasValue("y")) {
1092 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1093 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1094 int x = arg->getIntValue("x", mx->getIntValue());
1095 int y = arg->getIntValue("y", my->getIntValue());
1101 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1102 if (cursor->getType() != simgear::props::NONE)
1103 fgSetMouseCursor(cursor->getIntValue());
1105 cursor->setIntValue(fgGetMouseCursor());
1111 * Built-in command: play an audio message (i.e. a wav file) This is
1112 * fire and forget. Call this once per message and it will get dumped
1113 * into a queue. Messages are played sequentially so they do not
1117 do_play_audio_sample (const SGPropertyNode * arg)
1119 string path = arg->getStringValue("path");
1120 string file = arg->getStringValue("file");
1121 float volume = arg->getFloatValue("volume");
1122 // cout << "playing " << path << " / " << file << endl;
1124 static FGSampleQueue *queue = 0;
1126 SGSoundMgr *smgr = globals->get_soundmgr();
1127 queue = new FGSampleQueue(smgr, "chatter");
1128 queue->tie_to_listener();
1131 SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1132 msg->set_volume( volume );
1137 } catch (const sg_io_exception&) {
1138 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1139 "failed to load" << path << '/' << file);
1145 * Built-in command: commit presets (read from in /sim/presets/)
1148 do_presets_commit (const SGPropertyNode * arg)
1150 if (fgGetBool("/sim/initialized", false)) {
1151 fgReInitSubsystems();
1153 // Nasal can trigger this during initial init, which confuses
1154 // the logic in ReInitSubsystems, since initial state has not been
1155 // saved at that time. Short-circuit everything here.
1163 * Built-in command: set log level (0 ... 7)
1166 do_log_level (const SGPropertyNode * arg)
1168 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1174 * Built-in command: replay the FDR buffer
1177 do_replay (const SGPropertyNode * arg)
1179 // freeze the fdm, resume from sim pause
1180 fgSetInt( "/sim/freeze/replay-state", 1 );
1181 fgSetBool("/sim/freeze/master", 0 );
1182 fgSetBool("/sim/freeze/clock", 0 );
1183 fgSetDouble( "/sim/replay/time", -1 );
1185 // cout << "start = " << r->get_start_time()
1186 // << " end = " << r->get_end_time() << endl;
1193 do_decrease_visibility (const SGPropertyNode * arg)
1195 Environment::Presets::VisibilitySingleton::instance()->adjust( 0.9 );
1200 do_increase_visibility (const SGPropertyNode * arg)
1202 Environment::Presets::VisibilitySingleton::instance()->adjust( 1.1 );
1207 * An fgcommand to allow loading of xml files via nasal,
1208 * the xml file's structure will be made available within
1209 * a property tree node defined under argument "targetnode",
1210 * or in the given argument tree under "data" otherwise.
1212 * @param filename a string to hold the complete path & filename of an XML file
1213 * @param targetnode a string pointing to a location within the property tree
1214 * where to store the parsed XML file. If <targetnode> is undefined, then the
1215 * file contents are stored under a node <data> in the argument tree.
1219 do_load_xml_to_proptree(const SGPropertyNode * arg)
1221 SGPath file(arg->getStringValue("filename"));
1222 if (file.str().empty())
1225 if (file.extension() != "xml")
1226 file.concat(".xml");
1228 std::string icao = arg->getStringValue("icao");
1230 if (file.isRelative()) {
1231 SGPath absPath = globals->resolve_maybe_aircraft_path(file.str());
1232 if (!absPath.isNull())
1236 SG_LOG(SG_IO, SG_ALERT, "loadxml: Cannot find XML property file '"
1237 << file.str() << "'.");
1242 if (!XMLLoader::findAirportData(icao, file.str(), file)) {
1243 SG_LOG(SG_IO, SG_INFO, "loadxml: failed to find airport data for "
1244 << file.str() << " at ICAO:" << icao);
1249 if (!fgValidatePath(file.c_str(), false)) {
1250 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1251 "(unauthorized access)");
1255 SGPropertyNode *targetnode;
1256 if (arg->hasValue("targetnode"))
1257 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1259 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1262 readProperties(file.c_str(), targetnode, true);
1263 } catch (const sg_exception &e) {
1264 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1273 * An fgcommand to allow saving of xml files via nasal,
1274 * the file's structure will be determined based on what's
1275 * encountered in the passed (source) property tree node
1277 * @param filename a string to hold the complete path & filename of the (new)
1279 * @param sourcenode a string pointing to a location within the property tree
1280 * where to find the nodes that should be written recursively into an XML file
1281 * @param data if no sourcenode is given, then the file contents are taken from
1282 * the argument tree's "data" node.
1286 do_save_xml_from_proptree(const SGPropertyNode * arg)
1288 SGPath file(arg->getStringValue("filename"));
1289 if (file.str().empty())
1292 if (file.extension() != "xml")
1293 file.concat(".xml");
1295 if (!fgValidatePath(file.c_str(), true)) {
1296 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1297 "(unauthorized access)");
1301 SGPropertyNode *sourcenode;
1302 if (arg->hasValue("sourcenode"))
1303 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1304 else if (arg->getNode("data", false))
1305 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1310 writeProperties (file.c_str(), sourcenode, true);
1311 } catch (const sg_exception &e) {
1312 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1320 do_press_cockpit_button (const SGPropertyNode *arg)
1322 const char *prefix = arg->getStringValue("prefix");
1324 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1327 string prop = string(prefix) + "-button";
1330 if (arg->getBoolValue("latching"))
1331 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1335 fgSetDouble(prop.c_str(), value);
1336 fgSetBool(arg->getStringValue("discrete"), value > 0);
1342 do_release_cockpit_button (const SGPropertyNode *arg)
1344 const char *prefix = arg->getStringValue("prefix");
1346 if (arg->getBoolValue("guarded")) {
1347 string prop = string(prefix) + "-guard";
1348 if (fgGetDouble(prop.c_str()) < 1) {
1349 fgSetDouble(prop.c_str(), 1);
1354 if (! arg->getBoolValue("latching")) {
1355 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1356 fgSetBool(arg->getStringValue("discrete"), false);
1362 static SGGeod commandSearchPos(const SGPropertyNode* arg)
1364 if (arg->hasChild("longitude-deg") && arg->hasChild("latitude-deg")) {
1365 return SGGeod::fromDeg(arg->getDoubleValue("longitude-deg"),
1366 arg->getDoubleValue("latitude-deg"));
1369 // use current viewer/aircraft position
1370 return SGGeod::fromDeg(fgGetDouble("/position/longitude-deg"),
1371 fgGetDouble("/position/latitude-deg"));
1375 do_comm_search(const SGPropertyNode* arg)
1377 SGGeod pos = commandSearchPos(arg);
1378 int khz = static_cast<int>(arg->getDoubleValue("frequency-mhz") * 100.0 + 0.25);
1380 flightgear::CommStation* sta = flightgear::CommStation::findByFreq(khz, pos, NULL);
1385 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1386 sta->createBinding(result);
1391 do_nav_search(const SGPropertyNode* arg)
1393 SGGeod pos = commandSearchPos(arg);
1394 double mhz = arg->getDoubleValue("frequency-mhz");
1396 FGNavList* navList = globals->get_navlist();
1397 string type(arg->getStringValue("type", "vor"));
1398 if (type == "dme") {
1399 navList = globals->get_dmelist();
1400 } else if (type == "tacan") {
1401 navList = globals->get_tacanlist();
1404 FGNavRecord* nav = navList->findByFreq(mhz, pos);
1405 if (!nav && (type == "vor")) {
1406 // if we're searching VORs, look for localizers too
1407 nav = globals->get_loclist()->findByFreq(mhz, pos);
1414 SGPropertyNode* result = fgGetNode(arg->getStringValue("result"));
1415 nav->createBinding(result);
1419 ////////////////////////////////////////////////////////////////////////
1421 ////////////////////////////////////////////////////////////////////////
1425 * Table of built-in commands.
1427 * New commands do not have to be added here; any module in the application
1428 * can add a new command using globals->get_commands()->addCommand(...).
1432 SGCommandMgr::command_t command;
1434 { "null", do_null },
1435 { "nasal", do_nasal },
1436 { "exit", do_exit },
1437 { "reset", do_reset },
1438 { "reinit", do_reinit },
1439 { "suspend", do_reinit },
1440 { "resume", do_reinit },
1441 { "pause", do_pause },
1442 { "load", do_load },
1443 { "save", do_save },
1444 { "panel-load", do_panel_load },
1445 { "panel-mouse-click", do_panel_mouse_click },
1446 { "preferences-load", do_preferences_load },
1447 { "view-cycle", do_view_cycle },
1448 { "screen-capture", do_screen_capture },
1449 { "hires-screen-capture", do_hires_screen_capture },
1450 { "tile-cache-reload", do_tile_cache_reload },
1452 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1453 { "set-outside-air-temp-degc", do_set_oat_degc },
1454 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1455 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1457 { "property-toggle", do_property_toggle },
1458 { "property-assign", do_property_assign },
1459 { "property-adjust", do_property_adjust },
1460 { "property-multiply", do_property_multiply },
1461 { "property-swap", do_property_swap },
1462 { "property-scale", do_property_scale },
1463 { "property-cycle", do_property_cycle },
1464 { "property-randomize", do_property_randomize },
1465 { "data-logging-commit", do_data_logging_commit },
1466 { "dialog-new", do_dialog_new },
1467 { "dialog-show", do_dialog_show },
1468 { "dialog-close", do_dialog_close },
1469 { "dialog-update", do_dialog_update },
1470 { "dialog-apply", do_dialog_apply },
1471 { "gui-redraw", do_gui_redraw },
1472 { "add-model", do_add_model },
1473 { "set-cursor", do_set_cursor },
1474 { "play-audio-sample", do_play_audio_sample },
1475 { "presets-commit", do_presets_commit },
1476 { "log-level", do_log_level },
1477 { "replay", do_replay },
1479 { "decrease-visibility", do_decrease_visibility },
1480 { "increase-visibility", do_increase_visibility },
1482 { "loadxml", do_load_xml_to_proptree},
1483 { "savexml", do_save_xml_from_proptree },
1484 { "press-cockpit-button", do_press_cockpit_button },
1485 { "release-cockpit-button", do_release_cockpit_button },
1486 { "dump-scenegraph", do_dump_scene_graph },
1487 { "dump-terrainbranch", do_dump_terrain_branch },
1488 { "print-visible-scene", do_print_visible_scene_info },
1489 { "reload-shaders", do_reload_shaders },
1491 { "find-navaid", do_nav_search },
1492 { "find-comm", do_comm_search },
1494 { 0, 0 } // zero-terminated
1499 * Initialize the default built-in commands.
1501 * Other commands may be added by other parts of the application.
1506 SG_LOG(SG_GENERAL, SG_BULK, "Initializing basic built-in commands:");
1507 for (int i = 0; built_ins[i].name != 0; i++) {
1508 SG_LOG(SG_GENERAL, SG_BULK, " " << built_ins[i].name);
1509 globals->get_commands()->addCommand(built_ins[i].name,
1510 built_ins[i].command);
1513 typedef bool (*dummy)();
1514 fgTie( "/command/view/next", dummy(0), do_view_next );
1515 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1518 // end of fg_commands.cxx