1 // fg_commands.cxx - internal FGFS commands.
7 #include <string.h> // strcmp()
9 #include <simgear/compiler.h>
14 #include <simgear/sg_inlines.h>
15 #include <simgear/debug/logstream.hxx>
16 #include <simgear/math/sg_random.h>
17 #include <simgear/scene/material/mat.hxx>
18 #include <simgear/scene/material/matlib.hxx>
19 #include <simgear/structure/exception.hxx>
20 #include <simgear/structure/commands.hxx>
21 #include <simgear/props/props.hxx>
22 #include <simgear/structure/event_mgr.hxx>
23 #include <simgear/sound/soundmgr_openal.hxx>
24 #include <simgear/timing/sg_time.hxx>
26 #include <Cockpit/panel.hxx>
27 #include <Cockpit/panel_io.hxx>
28 #include <FDM/flight.hxx>
30 #include <GUI/new_gui.hxx>
31 #include <GUI/dialog.hxx>
32 #include <Aircraft/replay.hxx>
33 #include <Scenery/scenery.hxx>
34 #include <Scripting/NasalSys.hxx>
35 #include <Sound/sample_queue.hxx>
36 #include <Time/sunsolver.hxx>
38 #include "fg_init.hxx"
41 #include "fg_commands.hxx"
42 #include "fg_props.hxx"
43 #include "globals.hxx"
46 #include "viewmgr.hxx"
48 #include <Main/viewer.hxx>
49 #include <Environment/presets.hxx>
57 ////////////////////////////////////////////////////////////////////////
58 // Static helper functions.
59 ////////////////////////////////////////////////////////////////////////
62 static inline SGPropertyNode *
63 get_prop (const SGPropertyNode * arg)
65 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
68 static inline SGPropertyNode *
69 get_prop2 (const SGPropertyNode * arg)
71 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
76 * Get a double value and split it as required.
79 split_value (double full_value, const char * mask,
80 double * unmodifiable, double * modifiable)
82 if (!strcmp("integer", mask)) {
83 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
84 *unmodifiable = full_value - *modifiable;
85 } else if (!strcmp("decimal", mask)) {
86 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
87 *modifiable = full_value - *unmodifiable;
89 if (strcmp("all", mask))
90 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
91 << " assuming 'all'");
93 *modifiable = full_value;
99 * Clamp or wrap a value as specified.
102 limit_value (double * value, const SGPropertyNode * arg)
104 const SGPropertyNode * min_node = arg->getChild("min");
105 const SGPropertyNode * max_node = arg->getChild("max");
107 bool wrap = arg->getBoolValue("wrap");
109 if (min_node == 0 || max_node == 0)
112 if (wrap) { // wrap such that min <= x < max
113 double min_val = min_node->getDoubleValue();
114 double max_val = max_node->getDoubleValue();
115 double resolution = arg->getDoubleValue("resolution");
116 if (resolution > 0.0) {
117 // snap to (min + N*resolution), taking special care to handle imprecision
118 int n = (int)floor((*value - min_val) / resolution + 0.5);
119 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
120 SG_NORMALIZE_RANGE(n, 0, steps);
121 *value = min_val + resolution * n;
123 // plain circular wrapping
124 SG_NORMALIZE_RANGE(*value, min_val, max_val);
126 } else { // clamp such that min <= x <= max
127 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
128 *value = min_node->getDoubleValue();
129 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
130 *value = max_node->getDoubleValue();
135 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
137 switch (value1->getType()) {
138 case simgear::props::BOOL:
139 return (value1->getBoolValue() == value2->getBoolValue());
140 case simgear::props::INT:
141 return (value1->getIntValue() == value2->getIntValue());
142 case simgear::props::LONG:
143 return (value1->getLongValue() == value2->getLongValue());
144 case simgear::props::FLOAT:
145 return (value1->getFloatValue() == value2->getFloatValue());
146 case simgear::props::DOUBLE:
147 return (value1->getDoubleValue() == value2->getDoubleValue());
149 return !strcmp(value1->getStringValue(), value2->getStringValue());
155 ////////////////////////////////////////////////////////////////////////
156 // Command implementations.
157 ////////////////////////////////////////////////////////////////////////
161 * Built-in command: do nothing.
164 do_null (const SGPropertyNode * arg)
170 * Built-in command: run a Nasal script.
173 do_nasal (const SGPropertyNode * arg)
175 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
179 * Built-in command: exit FlightGear.
181 * status: the exit status to return to the operating system (defaults to 0)
184 do_exit (const SGPropertyNode * arg)
186 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
187 fgSetBool("/sim/signals/exit", true);
189 if (fgGetBool("/sim/startup/save-on-exit")) {
191 char* envp = ::getenv( "APPDATA" );
192 if ( envp != NULL ) {
193 SGPath config( envp );
194 config.append( "flightgear.org" );
196 if ( homedir != NULL ) {
197 SGPath config( homedir );
198 config.append( ".fgfs" );
200 config.append( "autosave.xml" );
201 config.create_dir( 0700 );
202 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
204 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
205 } catch (const sg_exception &e) {
206 guiErrorMessage("Error writing autosave.xml: ", e);
209 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
213 fgOSExit(arg->getIntValue("status", 0));
219 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
222 do_reset (const SGPropertyNode * arg)
224 fgReInitSubsystems();
230 * Built-in command: reinitialize one or more subsystems.
232 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
233 * none is specified, reinitialize all of them.
236 do_reinit (const SGPropertyNode * arg)
240 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
241 if (subsystems.size() == 0) {
242 globals->get_subsystem_mgr()->reinit();
244 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
245 const char * name = subsystems[i]->getStringValue();
246 SGSubsystem * subsystem = globals->get_subsystem(name);
247 if (subsystem == 0) {
249 SG_LOG( SG_GENERAL, SG_ALERT,
250 "Subsystem " << name << " not found" );
257 globals->get_event_mgr()->reinit();
264 // these routines look useful ??? but are never used in the code ???
268 * Built-in command: suspend one or more subsystems.
270 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
273 do_suspend (const SGPropertyNode * arg)
277 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
278 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
279 const char * name = subsystems[i]->getStringValue();
280 SGSubsystem * subsystem = globals->get_subsystem(name);
281 if (subsystem == 0) {
283 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
285 subsystem->suspend();
292 * Built-in command: suspend one or more subsystems.
294 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
297 do_resume (const SGPropertyNode * arg)
301 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
302 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
303 const char * name = subsystems[i]->getStringValue();
304 SGSubsystem * subsystem = globals->get_subsystem(name);
305 if (subsystem == 0) {
307 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << " not found");
319 * Built-in command: load flight.
321 * file (optional): the name of the file to load (relative to current
322 * directory). Defaults to "fgfs.sav"
325 do_load (const SGPropertyNode * arg)
327 string file = arg->getStringValue("file", "fgfs.sav");
328 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
331 if (!fgValidatePath(file.c_str(), false)) {
332 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
333 "(unauthorized access)");
337 ifstream input(file.c_str());
338 if (input.good() && fgLoadFlight(input)) {
340 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
343 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
350 * Built-in command: save flight.
352 * file (optional): the name of the file to save (relative to the
353 * current directory). Defaults to "fgfs.sav".
356 do_save (const SGPropertyNode * arg)
358 string file = arg->getStringValue("file", "fgfs.sav");
359 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
362 if (!fgValidatePath(file.c_str(), false)) {
363 SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
364 "(unauthorized access)");
368 bool write_all = arg->getBoolValue("write-all", false);
369 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
370 ofstream output(file.c_str());
371 if (output.good() && fgSaveFlight(output, write_all)) {
373 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
376 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
383 * Built-in command: (re)load the panel.
385 * path (optional): the file name to load the panel from
386 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
387 * and if that's unspecified, to "Panels/Default/default.xml".
390 do_panel_load (const SGPropertyNode * arg)
393 arg->getStringValue("path", fgGetString("/sim/panel/path"));
394 if (panel_path.empty()) {
398 FGPanel * new_panel = fgReadPanel(panel_path);
399 if (new_panel == 0) {
400 SG_LOG(SG_INPUT, SG_ALERT,
401 "Error reading new panel from " << panel_path);
404 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
405 globals->get_current_panel()->unbind();
406 delete globals->get_current_panel();
407 globals->set_current_panel( new_panel );
408 globals->get_current_panel()->bind();
414 * Built-in command: pass a mouse click to the panel.
416 * button: the mouse button number, zero-based.
417 * is-down: true if the button is down, false if it is up.
418 * x-pos: the x position of the mouse click.
419 * y-pos: the y position of the mouse click.
422 do_panel_mouse_click (const SGPropertyNode * arg)
424 if (globals->get_current_panel() != 0)
425 return globals->get_current_panel()
426 ->doMouseAction(arg->getIntValue("button"),
427 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
428 arg->getIntValue("x-pos"),
429 arg->getIntValue("y-pos"));
436 * Built-in command: (re)load preferences.
438 * path (optional): the file name to load the panel from (relative
439 * to FG_ROOT). Defaults to "preferences.xml".
442 do_preferences_load (const SGPropertyNode * arg)
445 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
446 globals->get_props());
447 } catch (const sg_exception &e) {
448 guiErrorMessage("Error reading global preferences: ", e);
451 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
458 globals->get_current_view()->setHeadingOffset_deg(0.0);
459 globals->get_viewmgr()->next_view();
465 globals->get_current_view()->setHeadingOffset_deg(0.0);
466 globals->get_viewmgr()->prev_view();
470 * Built-in command: cycle view.
473 do_view_cycle (const SGPropertyNode * arg)
475 globals->get_current_view()->setHeadingOffset_deg(0.0);
476 globals->get_viewmgr()->next_view();
481 * Built-in command: capture screen.
484 do_screen_capture (const SGPropertyNode * arg)
486 return fgDumpSnapShot();
490 do_reload_shaders (const SGPropertyNode*)
492 simgear::reload_shaders();
497 do_dump_scene_graph (const SGPropertyNode*)
504 do_dump_terrain_branch (const SGPropertyNode*)
506 fgDumpTerrainBranch();
508 double lon_deg = fgGetDouble("/position/longitude-deg");
509 double lat_deg = fgGetDouble("/position/latitude-deg");
510 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
511 SGVec3d zero = SGVec3d::fromGeod(geodPos);
513 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
514 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
515 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
521 do_print_visible_scene_info(const SGPropertyNode*)
523 fgPrintVisibleSceneInfoCommand();
528 * Built-in command: hires capture screen.
531 do_hires_screen_capture (const SGPropertyNode * arg)
539 * Reload the tile cache.
542 do_tile_cache_reload (const SGPropertyNode * arg)
544 static const SGPropertyNode *master_freeze
545 = fgGetNode("/sim/freeze/master");
546 bool freeze = master_freeze->getBoolValue();
547 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
549 fgSetBool("/sim/freeze/master", true);
552 globals->get_subsystem("tile-manager")->reinit();
555 fgSetBool("/sim/freeze/master", false);
562 These do_set_(some-environment-parameters) are deprecated and no longer
563 useful/functional - Torsten Dreyer, January 2011
565 * Set the sea level outside air temperature and assigning that to all
566 * boundary and aloft environment layers.
569 do_set_sea_level_degc ( double temp_sea_level_degc)
571 SGPropertyNode *node, *child;
574 node = fgGetNode( "/environment/config/boundary" );
575 if ( node != NULL ) {
577 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
578 child->setDoubleValue( "temperature-sea-level-degc",
579 temp_sea_level_degc );
585 node = fgGetNode( "/environment/config/aloft" );
586 if ( node != NULL ) {
588 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
589 child->setDoubleValue( "temperature-sea-level-degc",
590 temp_sea_level_degc );
599 do_set_sea_level_degc (const SGPropertyNode * arg)
601 return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
606 * Set the outside air temperature at the "current" altitude by first
607 * calculating the corresponding sea level temp, and assigning that to
608 * all boundary and aloft environment layers.
611 do_set_oat_degc (const SGPropertyNode * arg)
613 double oat_degc = arg->getDoubleValue("temp-degc", 15.0);
614 // check for an altitude specified in the arguments, otherwise use
615 // current aircraft altitude.
616 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
617 if ( altitude_ft == NULL ) {
618 altitude_ft = fgGetNode("/position/altitude-ft");
621 FGEnvironment dummy; // instantiate a dummy so we can leech a method
622 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
623 dummy.set_temperature_degc( oat_degc );
624 return do_set_sea_level_degc( dummy.get_temperature_sea_level_degc());
628 * Set the sea level outside air dewpoint and assigning that to all
629 * boundary and aloft environment layers.
632 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
635 SGPropertyNode *node, *child;
638 node = fgGetNode( "/environment/config/boundary" );
639 if ( node != NULL ) {
641 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
642 child->setDoubleValue( "dewpoint-sea-level-degc",
643 dewpoint_sea_level_degc );
649 node = fgGetNode( "/environment/config/aloft" );
650 if ( node != NULL ) {
652 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
653 child->setDoubleValue( "dewpoint-sea-level-degc",
654 dewpoint_sea_level_degc );
663 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
665 return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
669 * Set the outside air dewpoint at the "current" altitude by first
670 * calculating the corresponding sea level dewpoint, and assigning
671 * that to all boundary and aloft environment layers.
674 do_set_dewpoint_degc (const SGPropertyNode * arg)
676 double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
678 // check for an altitude specified in the arguments, otherwise use
679 // current aircraft altitude.
680 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
681 if ( altitude_ft == NULL ) {
682 altitude_ft = fgGetNode("/position/altitude-ft");
685 FGEnvironment dummy; // instantiate a dummy so we can leech a method
686 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
687 dummy.set_dewpoint_degc( dewpoint_degc );
688 return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
692 * Update the lighting manually.
695 do_timeofday (const SGPropertyNode * arg)
697 const string &offset_type = arg->getStringValue("timeofday", "noon");
699 static const SGPropertyNode *longitude
700 = fgGetNode("/position/longitude-deg");
701 static const SGPropertyNode *latitude
702 = fgGetNode("/position/latitude-deg");
704 int orig_warp = globals->get_warp();
705 SGTime *t = globals->get_time_params();
706 time_t cur_time = t->get_cur_time();
707 // cout << "cur_time = " << cur_time << endl;
708 // cout << "orig_warp = " << orig_warp << endl;
711 if ( offset_type == "real" ) {
713 } else if ( offset_type == "dawn" ) {
714 warp = fgTimeSecondsUntilSunAngle( cur_time,
715 longitude->getDoubleValue()
716 * SGD_DEGREES_TO_RADIANS,
717 latitude->getDoubleValue()
718 * SGD_DEGREES_TO_RADIANS,
720 } else if ( offset_type == "morning" ) {
721 warp = fgTimeSecondsUntilSunAngle( cur_time,
722 longitude->getDoubleValue()
723 * SGD_DEGREES_TO_RADIANS,
724 latitude->getDoubleValue()
725 * SGD_DEGREES_TO_RADIANS,
727 } else if ( offset_type == "noon" ) {
728 warp = fgTimeSecondsUntilSunAngle( cur_time,
729 longitude->getDoubleValue()
730 * SGD_DEGREES_TO_RADIANS,
731 latitude->getDoubleValue()
732 * SGD_DEGREES_TO_RADIANS,
734 } else if ( offset_type == "afternoon" ) {
735 warp = fgTimeSecondsUntilSunAngle( cur_time,
736 longitude->getDoubleValue()
737 * SGD_DEGREES_TO_RADIANS,
738 latitude->getDoubleValue()
739 * SGD_DEGREES_TO_RADIANS,
741 } else if ( offset_type == "dusk" ) {
742 warp = fgTimeSecondsUntilSunAngle( cur_time,
743 longitude->getDoubleValue()
744 * SGD_DEGREES_TO_RADIANS,
745 latitude->getDoubleValue()
746 * SGD_DEGREES_TO_RADIANS,
748 } else if ( offset_type == "evening" ) {
749 warp = fgTimeSecondsUntilSunAngle( cur_time,
750 longitude->getDoubleValue()
751 * SGD_DEGREES_TO_RADIANS,
752 latitude->getDoubleValue()
753 * SGD_DEGREES_TO_RADIANS,
755 } else if ( offset_type == "midnight" ) {
756 warp = fgTimeSecondsUntilSunAngle( cur_time,
757 longitude->getDoubleValue()
758 * SGD_DEGREES_TO_RADIANS,
759 latitude->getDoubleValue()
760 * SGD_DEGREES_TO_RADIANS,
765 fgSetInt("/sim/time/warp", orig_warp + warp);
772 * Built-in command: toggle a bool property value.
774 * property: The name of the property to toggle.
777 do_property_toggle (const SGPropertyNode * arg)
779 SGPropertyNode * prop = get_prop(arg);
780 return prop->setBoolValue(!prop->getBoolValue());
785 * Built-in command: assign a value to a property.
787 * property: the name of the property to assign.
788 * value: the value to assign; or
789 * property[1]: the property to copy from.
792 do_property_assign (const SGPropertyNode * arg)
794 SGPropertyNode * prop = get_prop(arg);
795 const SGPropertyNode * prop2 = get_prop2(arg);
796 const SGPropertyNode * value = arg->getNode("value");
799 return prop->setUnspecifiedValue(value->getStringValue());
801 return prop->setUnspecifiedValue(prop2->getStringValue());
808 * Built-in command: increment or decrement a property value.
810 * If the 'step' argument is present, it will be used; otherwise,
811 * the command uses 'offset' and 'factor', usually from the mouse.
813 * property: the name of the property to increment or decrement.
814 * step: the amount of the increment or decrement (default: 0).
815 * offset: offset from the current setting (used for the mouse; multiplied
817 * factor: scaling amount for the offset (defaults to 1).
818 * min: the minimum allowed value (default: no minimum).
819 * max: the maximum allowed value (default: no maximum).
820 * mask: 'integer' to apply only to the left of the decimal point,
821 * 'decimal' to apply only to the right of the decimal point,
822 * or 'all' to apply to the whole number (the default).
823 * wrap: true if the value should be wrapped when it passes min or max;
824 * both min and max must be present for this to work (default:
828 do_property_adjust (const SGPropertyNode * arg)
830 SGPropertyNode * prop = get_prop(arg);
833 if (arg->hasValue("step"))
834 amount = arg->getDoubleValue("step");
836 amount = (arg->getDoubleValue("factor", 1)
837 * arg->getDoubleValue("offset"));
839 double unmodifiable, modifiable;
840 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
841 &unmodifiable, &modifiable);
842 modifiable += amount;
843 limit_value(&modifiable, arg);
845 prop->setDoubleValue(unmodifiable + modifiable);
852 * Built-in command: multiply a property value.
854 * property: the name of the property to multiply.
855 * factor: the amount by which to multiply.
856 * min: the minimum allowed value (default: no minimum).
857 * max: the maximum allowed value (default: no maximum).
858 * mask: 'integer' to apply only to the left of the decimal point,
859 * 'decimal' to apply only to the right of the decimal point,
860 * or 'all' to apply to the whole number (the default).
861 * wrap: true if the value should be wrapped when it passes min or max;
862 * both min and max must be present for this to work (default:
866 do_property_multiply (const SGPropertyNode * arg)
868 SGPropertyNode * prop = get_prop(arg);
869 double factor = arg->getDoubleValue("factor", 1);
871 double unmodifiable, modifiable;
872 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
873 &unmodifiable, &modifiable);
874 modifiable *= factor;
875 limit_value(&modifiable, arg);
877 prop->setDoubleValue(unmodifiable + modifiable);
884 * Built-in command: swap two property values.
886 * property[0]: the name of the first property.
887 * property[1]: the name of the second property.
890 do_property_swap (const SGPropertyNode * arg)
892 SGPropertyNode * prop1 = get_prop(arg);
893 SGPropertyNode * prop2 = get_prop2(arg);
895 // FIXME: inefficient
896 const string & tmp = prop1->getStringValue();
897 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
898 prop2->setUnspecifiedValue(tmp.c_str()));
903 * Built-in command: Set a property to an axis or other moving input.
905 * property: the name of the property to set.
906 * setting: the current input setting, usually between -1.0 and 1.0.
907 * offset: the offset to shift by, before applying the factor.
908 * factor: the factor to multiply by (use negative to reverse).
911 do_property_scale (const SGPropertyNode * arg)
913 SGPropertyNode * prop = get_prop(arg);
914 double setting = arg->getDoubleValue("setting");
915 double offset = arg->getDoubleValue("offset", 0.0);
916 double factor = arg->getDoubleValue("factor", 1.0);
917 bool squared = arg->getBoolValue("squared", false);
918 int power = arg->getIntValue("power", (squared ? 2 : 1));
920 int sign = (setting < 0 ? -1 : 1);
926 setting = setting * setting * sign;
929 setting = setting * setting * setting;
932 setting = setting * setting * setting * setting * sign;
935 setting = pow(setting, power);
936 if ((power % 2) == 0)
941 return prop->setDoubleValue((setting + offset) * factor);
946 * Built-in command: cycle a property through a set of values.
948 * If the current value isn't in the list, the cycle will
949 * (re)start from the beginning.
951 * property: the name of the property to cycle.
952 * value[*]: the list of values to cycle through.
955 do_property_cycle (const SGPropertyNode * arg)
957 SGPropertyNode * prop = get_prop(arg);
958 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
960 int nSelections = values.size();
962 if (nSelections < 1) {
963 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
967 // Try to find the current selection
968 for (int i = 0; i < nSelections; i++) {
969 if (compare_values(prop, values[i])) {
975 // Default or wrap to the first selection
976 if (selection < 0 || selection >= nSelections)
979 prop->setUnspecifiedValue(values[selection]->getStringValue());
985 * Built-in command: randomize a numeric property value.
987 * property: the name of the property value to randomize.
988 * min: the minimum allowed value.
989 * max: the maximum allowed value.
992 do_property_randomize (const SGPropertyNode * arg)
994 SGPropertyNode * prop = get_prop(arg);
995 double min = arg->getDoubleValue("min", DBL_MIN);
996 double max = arg->getDoubleValue("max", DBL_MAX);
997 prop->setDoubleValue(sg_random() * (max - min) + min);
1003 * Built-in command: reinit the data logging system based on the
1004 * current contents of the /logger tree.
1007 do_data_logging_commit (const SGPropertyNode * arg)
1009 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1015 * Built-in command: Add a dialog to the GUI system. Does *not*
1016 * display the dialog. The property node should have the same format
1017 * as a dialog XML configuration. It must include:
1019 * name: the name of the GUI dialog for future reference.
1022 do_dialog_new (const SGPropertyNode * arg)
1024 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1026 // Note the casting away of const: this is *real*. Doing a
1027 // "dialog-apply" command later on will mutate this property node.
1028 // I'm not convinced that this isn't the Right Thing though; it
1029 // allows client to create a node, pass it to dialog-new, and get
1030 // the values back from the dialog by reading the same node.
1031 // Perhaps command arguments are not as "const" as they would
1033 gui->newDialog((SGPropertyNode*)arg);
1038 * Built-in command: Show an XML-configured dialog.
1040 * dialog-name: the name of the GUI dialog to display.
1043 do_dialog_show (const SGPropertyNode * arg)
1045 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1046 gui->showDialog(arg->getStringValue("dialog-name"));
1052 * Built-in Command: Hide the active XML-configured dialog.
1055 do_dialog_close (const SGPropertyNode * arg)
1057 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1058 if(arg->hasValue("dialog-name"))
1059 return gui->closeDialog(arg->getStringValue("dialog-name"));
1060 return gui->closeActiveDialog();
1065 * Update a value in the active XML-configured dialog.
1067 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1070 do_dialog_update (const SGPropertyNode * arg)
1072 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1074 if (arg->hasValue("dialog-name"))
1075 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1077 dialog = gui->getActiveDialog();
1080 dialog->updateValues(arg->getStringValue("object-name"));
1089 * Apply a value in the active XML-configured dialog.
1091 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1094 do_dialog_apply (const SGPropertyNode * arg)
1096 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1098 if (arg->hasValue("dialog-name"))
1099 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1101 dialog = gui->getActiveDialog();
1104 dialog->applyValues(arg->getStringValue("object-name"));
1113 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1117 do_gui_redraw (const SGPropertyNode * arg)
1119 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1126 * Adds model to the scenery. The path to the added branch (/models/model[*])
1127 * is returned in property "property".
1130 do_add_model (const SGPropertyNode * arg)
1132 SGPropertyNode * model = fgGetNode("models", true);
1133 for (int i = 0;; i++) {
1136 if (!model->getChild("model", i, false)) {
1137 model = model->getChild("model", i, true);
1141 copyProperties(arg, model);
1142 if (model->hasValue("elevation-m"))
1143 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1144 * SG_METER_TO_FEET);
1145 model->getNode("load", true);
1146 model->removeChildren("load");
1147 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1153 * Set mouse cursor coordinates and cursor shape.
1156 do_set_cursor (const SGPropertyNode * arg)
1158 if (arg->hasValue("x") || arg->hasValue("y")) {
1159 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1160 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1161 int x = arg->getIntValue("x", mx->getIntValue());
1162 int y = arg->getIntValue("y", my->getIntValue());
1168 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1169 if (cursor->getType() != simgear::props::NONE)
1170 fgSetMouseCursor(cursor->getIntValue());
1172 cursor->setIntValue(fgGetMouseCursor());
1178 * Built-in command: play an audio message (i.e. a wav file) This is
1179 * fire and forget. Call this once per message and it will get dumped
1180 * into a queue. Messages are played sequentially so they do not
1184 do_play_audio_sample (const SGPropertyNode * arg)
1186 string path = arg->getStringValue("path");
1187 string file = arg->getStringValue("file");
1188 float volume = arg->getFloatValue("volume");
1189 // cout << "playing " << path << " / " << file << endl;
1191 static FGSampleQueue *queue = 0;
1193 SGSoundMgr *smgr = globals->get_soundmgr();
1194 queue = new FGSampleQueue(smgr, "chatter");
1195 queue->tie_to_listener();
1198 SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1199 msg->set_volume( volume );
1204 } catch (const sg_io_exception&) {
1205 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1206 "failed to load" << path << '/' << file);
1212 * Built-in command: commit presets (read from in /sim/presets/)
1215 do_presets_commit (const SGPropertyNode * arg)
1217 if (fgGetBool("/sim/initialized", false)) {
1218 fgReInitSubsystems();
1220 // Nasal can trigger this during initial init, which confuses
1221 // the logic in ReInitSubsystems, since initial state has not been
1222 // saved at that time. Short-circuit everything here.
1230 * Built-in command: set log level (0 ... 7)
1233 do_log_level (const SGPropertyNode * arg)
1235 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1241 * Built-in command: replay the FDR buffer
1244 do_replay (const SGPropertyNode * arg)
1246 // freeze the fdm, resume from sim pause
1247 fgSetInt( "/sim/freeze/replay-state", 1 );
1248 fgSetBool("/sim/freeze/master", 0 );
1249 fgSetBool("/sim/freeze/clock", 0 );
1251 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1253 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1254 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1255 double duration = fgGetDouble( "/sim/replay/duration" );
1256 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1257 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1259 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1262 // cout << "start = " << r->get_start_time()
1263 // << " end = " << r->get_end_time() << endl;
1270 do_decrease_visibility (const SGPropertyNode * arg)
1272 Environment::Presets::VisibilitySingleton::instance()->adjust( 0.9 );
1277 do_increase_visibility (const SGPropertyNode * arg)
1279 Environment::Presets::VisibilitySingleton::instance()->adjust( 1.1 );
1284 * An fgcommand to allow loading of xml files via nasal,
1285 * the xml file's structure will be made available within
1286 * a property tree node defined under argument "targetnode",
1287 * or in the given argument tree under "data" otherwise.
1289 * @param filename a string to hold the complete path & filename of an XML file
1290 * @param targetnode a string pointing to a location within the property tree
1291 * where to store the parsed XML file. If <targetnode> is undefined, then the
1292 * file contents are stored under a node <data> in the argument tree.
1296 do_load_xml_to_proptree(const SGPropertyNode * arg)
1298 SGPath file(arg->getStringValue("filename"));
1299 if (file.str().empty())
1302 if (file.extension() != "xml")
1303 file.concat(".xml");
1305 if (file.isRelative()) {
1306 file = globals->resolve_maybe_aircraft_path(file.str());
1309 if (!fgValidatePath(file.c_str(), false)) {
1310 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1311 "(unauthorized access)");
1315 SGPropertyNode *targetnode;
1316 if (arg->hasValue("targetnode"))
1317 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1319 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1322 readProperties(file.c_str(), targetnode, true);
1323 } catch (const sg_exception &e) {
1324 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1333 * An fgcommand to allow saving of xml files via nasal,
1334 * the file's structure will be determined based on what's
1335 * encountered in the passed (source) property tree node
1337 * @param filename a string to hold the complete path & filename of the (new)
1339 * @param sourcenode a string pointing to a location within the property tree
1340 * where to find the nodes that should be written recursively into an XML file
1341 * @param data if no sourcenode is given, then the file contents are taken from
1342 * the argument tree's "data" node.
1346 do_save_xml_from_proptree(const SGPropertyNode * arg)
1348 SGPath file(arg->getStringValue("filename"));
1349 if (file.str().empty())
1352 if (file.extension() != "xml")
1353 file.concat(".xml");
1355 if (!fgValidatePath(file.c_str(), true)) {
1356 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1357 "(unauthorized access)");
1361 SGPropertyNode *sourcenode;
1362 if (arg->hasValue("sourcenode"))
1363 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1364 else if (arg->getNode("data", false))
1365 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1370 writeProperties (file.c_str(), sourcenode, true);
1371 } catch (const sg_exception &e) {
1372 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1380 do_press_cockpit_button (const SGPropertyNode *arg)
1382 const char *prefix = arg->getStringValue("prefix");
1384 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1387 string prop = string(prefix) + "-button";
1390 if (arg->getBoolValue("latching"))
1391 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1395 fgSetDouble(prop.c_str(), value);
1396 fgSetBool(arg->getStringValue("discrete"), value > 0);
1402 do_release_cockpit_button (const SGPropertyNode *arg)
1404 const char *prefix = arg->getStringValue("prefix");
1406 if (arg->getBoolValue("guarded")) {
1407 string prop = string(prefix) + "-guard";
1408 if (fgGetDouble(prop.c_str()) < 1) {
1409 fgSetDouble(prop.c_str(), 1);
1414 if (! arg->getBoolValue("latching")) {
1415 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1416 fgSetBool(arg->getStringValue("discrete"), false);
1423 ////////////////////////////////////////////////////////////////////////
1425 ////////////////////////////////////////////////////////////////////////
1429 * Table of built-in commands.
1431 * New commands do not have to be added here; any module in the application
1432 * can add a new command using globals->get_commands()->addCommand(...).
1436 SGCommandMgr::command_t command;
1438 { "null", do_null },
1439 { "nasal", do_nasal },
1440 { "exit", do_exit },
1441 { "reset", do_reset },
1442 { "reinit", do_reinit },
1443 { "suspend", do_reinit },
1444 { "resume", do_reinit },
1445 { "load", do_load },
1446 { "save", do_save },
1447 { "panel-load", do_panel_load },
1448 { "panel-mouse-click", do_panel_mouse_click },
1449 { "preferences-load", do_preferences_load },
1450 { "view-cycle", do_view_cycle },
1451 { "screen-capture", do_screen_capture },
1452 { "hires-screen-capture", do_hires_screen_capture },
1453 { "tile-cache-reload", do_tile_cache_reload },
1455 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1456 { "set-outside-air-temp-degc", do_set_oat_degc },
1457 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1458 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1460 { "timeofday", do_timeofday },
1461 { "property-toggle", do_property_toggle },
1462 { "property-assign", do_property_assign },
1463 { "property-adjust", do_property_adjust },
1464 { "property-multiply", do_property_multiply },
1465 { "property-swap", do_property_swap },
1466 { "property-scale", do_property_scale },
1467 { "property-cycle", do_property_cycle },
1468 { "property-randomize", do_property_randomize },
1469 { "data-logging-commit", do_data_logging_commit },
1470 { "dialog-new", do_dialog_new },
1471 { "dialog-show", do_dialog_show },
1472 { "dialog-close", do_dialog_close },
1473 { "dialog-update", do_dialog_update },
1474 { "dialog-apply", do_dialog_apply },
1475 { "gui-redraw", do_gui_redraw },
1476 { "add-model", do_add_model },
1477 { "set-cursor", do_set_cursor },
1478 { "play-audio-sample", do_play_audio_sample },
1479 { "presets-commit", do_presets_commit },
1480 { "log-level", do_log_level },
1481 { "replay", do_replay },
1483 { "decrease-visibility", do_decrease_visibility },
1484 { "increase-visibility", do_increase_visibility },
1486 { "loadxml", do_load_xml_to_proptree},
1487 { "savexml", do_save_xml_from_proptree },
1488 { "press-cockpit-button", do_press_cockpit_button },
1489 { "release-cockpit-button", do_release_cockpit_button },
1490 { "dump-scenegraph", do_dump_scene_graph },
1491 { "dump-terrainbranch", do_dump_terrain_branch },
1492 { "print-visible-scene", do_print_visible_scene_info },
1493 { "reload-shaders", do_reload_shaders },
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