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/structure/exception.hxx>
19 #include <simgear/structure/commands.hxx>
20 #include <simgear/props/props.hxx>
22 #include <Cockpit/panel.hxx>
23 #include <Cockpit/panel_io.hxx>
24 #include <Cockpit/hud.hxx>
25 #include <Environment/environment.hxx>
26 #include <FDM/flight.hxx>
28 #include <GUI/new_gui.hxx>
29 #include <GUI/dialog.hxx>
30 #include <Aircraft/replay.hxx>
31 #include <Scenery/tilemgr.hxx>
32 #include <Scenery/scenery.hxx>
33 #include <Scripting/NasalSys.hxx>
34 #include <Sound/fg_fx.hxx>
35 #include <Time/sunsolver.hxx>
36 #include <Time/tmp.hxx>
38 #include "fg_init.hxx"
40 #include "fg_commands.hxx"
41 #include "fg_props.hxx"
42 #include "globals.hxx"
45 #include "viewmgr.hxx"
49 SG_USING_STD(ifstream);
50 SG_USING_STD(ofstream);
54 ////////////////////////////////////////////////////////////////////////
55 // Static helper functions.
56 ////////////////////////////////////////////////////////////////////////
59 static inline SGPropertyNode *
60 get_prop (const SGPropertyNode * arg)
62 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
65 static inline SGPropertyNode *
66 get_prop2 (const SGPropertyNode * arg)
68 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
73 * Get a double value and split it as required.
76 split_value (double full_value, const char * mask,
77 double * unmodifiable, double * modifiable)
79 if (!strcmp("integer", mask)) {
80 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
81 *unmodifiable = full_value - *modifiable;
82 } else if (!strcmp("decimal", mask)) {
83 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
84 *modifiable = full_value - *unmodifiable;
86 if (strcmp("all", mask))
87 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
88 << " assuming 'all'");
90 *modifiable = full_value;
96 * Clamp or wrap a value as specified.
99 limit_value (double * value, const SGPropertyNode * arg)
101 const SGPropertyNode * min_node = arg->getChild("min");
102 const SGPropertyNode * max_node = arg->getChild("max");
104 bool wrap = arg->getBoolValue("wrap");
106 if (min_node == 0 || max_node == 0)
109 if (wrap) { // wrap such that min <= x < max
110 double min_val = min_node->getDoubleValue();
111 double max_val = max_node->getDoubleValue();
112 double resolution = arg->getDoubleValue("resolution");
113 if (resolution > 0.0) {
114 // snap to (min + N*resolution), taking special care to handle imprecision
115 int n = (int)floor((*value - min_val) / resolution + 0.5);
116 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
117 SG_NORMALIZE_RANGE(n, 0, steps);
118 *value = min_val + resolution * n;
120 // plain circular wrapping
121 SG_NORMALIZE_RANGE(*value, min_val, max_val);
123 } else { // clamp such that min <= x <= max
124 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
125 *value = min_node->getDoubleValue();
126 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
127 *value = max_node->getDoubleValue();
132 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
134 switch (value1->getType()) {
135 case SGPropertyNode::BOOL:
136 return (value1->getBoolValue() == value2->getBoolValue());
137 case SGPropertyNode::INT:
138 return (value1->getIntValue() == value2->getIntValue());
139 case SGPropertyNode::LONG:
140 return (value1->getLongValue() == value2->getLongValue());
141 case SGPropertyNode::FLOAT:
142 return (value1->getFloatValue() == value2->getFloatValue());
143 case SGPropertyNode::DOUBLE:
144 return (value1->getDoubleValue() == value2->getDoubleValue());
146 return !strcmp(value1->getStringValue(), value2->getStringValue());
152 ////////////////////////////////////////////////////////////////////////
153 // Command implementations.
154 ////////////////////////////////////////////////////////////////////////
158 * Built-in command: do nothing.
161 do_null (const SGPropertyNode * arg)
167 * Built-in command: run a Nasal script.
170 do_nasal (const SGPropertyNode * arg)
172 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
176 * Built-in command: exit FlightGear.
178 * status: the exit status to return to the operating system (defaults to 0)
181 do_exit (const SGPropertyNode * arg)
183 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
184 fgSetBool("/sim/signals/exit", true);
186 if (fgGetBool("/sim/startup/save-on-exit")) {
188 char* envp = ::getenv( "APPDATA" );
189 if ( envp != NULL ) {
190 SGPath config( envp );
191 config.append( "flightgear.org" );
193 if ( homedir != NULL ) {
194 SGPath config( homedir );
195 config.append( ".fgfs" );
197 config.append( "autosave.xml" );
198 config.create_dir( 0700 );
199 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
201 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
202 } catch (const sg_exception &e) {
203 guiErrorMessage("Error writing autosave.xml: ", e);
206 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
209 fgExit(arg->getIntValue("status", 0));
215 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
218 do_reset (const SGPropertyNode * arg)
226 * Built-in command: reinitialize one or more subsystems.
228 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
229 * none is specified, reinitialize all of them.
232 do_reinit (const SGPropertyNode * arg)
236 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
237 if (subsystems.size() == 0) {
238 globals->get_subsystem_mgr()->reinit();
240 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
241 const char * name = subsystems[i]->getStringValue();
242 SGSubsystem * subsystem = globals->get_subsystem(name);
243 if (subsystem == 0) {
245 SG_LOG( SG_GENERAL, SG_ALERT,
246 "Subsystem " << name << "not found" );
253 globals->get_event_mgr()->reinit();
260 // these routines look useful ??? but are never used in the code ???
264 * Built-in command: suspend one or more subsystems.
266 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
269 do_suspend (const SGPropertyNode * arg)
273 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
274 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
275 const char * name = subsystems[i]->getStringValue();
276 SGSubsystem * subsystem = globals->get_subsystem(name);
277 if (subsystem == 0) {
279 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
281 subsystem->suspend();
288 * Built-in command: suspend one or more subsystems.
290 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
293 do_resume (const SGPropertyNode * arg)
297 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
298 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
299 const char * name = subsystems[i]->getStringValue();
300 SGSubsystem * subsystem = globals->get_subsystem(name);
301 if (subsystem == 0) {
303 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
315 * Built-in command: load flight.
317 * file (optional): the name of the file to load (relative to current
318 * directory). Defaults to "fgfs.sav"
321 do_load (const SGPropertyNode * arg)
323 const string &file = arg->getStringValue("file", "fgfs.sav");
325 if (!fgValidatePath(file.c_str(), false)) {
326 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
327 "(unauthorized access)");
331 ifstream input(file.c_str());
332 if (input.good() && fgLoadFlight(input)) {
334 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
337 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
344 * Built-in command: save flight.
346 * file (optional): the name of the file to save (relative to the
347 * current directory). Defaults to "fgfs.sav".
350 do_save (const SGPropertyNode * arg)
352 const string &file = arg->getStringValue("file", "fgfs.sav");
354 if (!fgValidatePath(file.c_str(), false)) {
355 SG_LOG(SG_IO, SG_ALERT, "save: reading '" << file << "' denied "
356 "(unauthorized access)");
360 bool write_all = arg->getBoolValue("write-all", false);
361 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
362 ofstream output(file.c_str());
363 if (output.good() && fgSaveFlight(output, write_all)) {
365 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
368 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
375 * Built-in command: (re)load the panel.
377 * path (optional): the file name to load the panel from
378 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
379 * and if that's unspecified, to "Panels/Default/default.xml".
382 do_panel_load (const SGPropertyNode * arg)
385 arg->getStringValue("path",
386 fgGetString("/sim/panel/path",
387 "Panels/Default/default.xml"));
388 FGPanel * new_panel = fgReadPanel(panel_path);
389 if (new_panel == 0) {
390 SG_LOG(SG_INPUT, SG_ALERT,
391 "Error reading new panel from " << panel_path);
394 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
395 globals->get_current_panel()->unbind();
396 delete globals->get_current_panel();
397 globals->set_current_panel( new_panel );
398 globals->get_current_panel()->bind();
404 * Built-in command: pass a mouse click to the panel.
406 * button: the mouse button number, zero-based.
407 * is-down: true if the button is down, false if it is up.
408 * x-pos: the x position of the mouse click.
409 * y-pos: the y position of the mouse click.
412 do_panel_mouse_click (const SGPropertyNode * arg)
414 if (globals->get_current_panel() != 0)
415 return globals->get_current_panel()
416 ->doMouseAction(arg->getIntValue("button"),
417 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
418 arg->getIntValue("x-pos"),
419 arg->getIntValue("y-pos"));
426 * Built-in command: (re)load preferences.
428 * path (optional): the file name to load the panel from (relative
429 * to FG_ROOT). Defaults to "preferences.xml".
432 do_preferences_load (const SGPropertyNode * arg)
435 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
436 globals->get_props());
437 } catch (const sg_exception &e) {
438 guiErrorMessage("Error reading global preferences: ", e);
441 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
449 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
450 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
451 if ( globals->get_viewmgr()->get_current() == 1 ) {
452 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
460 globals->get_current_view()->setHeadingOffset_deg(0.0);
461 globals->get_viewmgr()->next_view();
462 fix_hud_visibility();
468 globals->get_current_view()->setHeadingOffset_deg(0.0);
469 globals->get_viewmgr()->prev_view();
470 fix_hud_visibility();
474 * Built-in command: cycle view.
477 do_view_cycle (const SGPropertyNode * arg)
479 globals->get_current_view()->setHeadingOffset_deg(0.0);
480 globals->get_viewmgr()->next_view();
481 fix_hud_visibility();
486 * Built-in command: capture screen.
489 do_screen_capture (const SGPropertyNode * arg)
491 return fgDumpSnapShot();
495 do_dump_scene_graph (const SGPropertyNode*)
502 do_dump_terrain_branch (const SGPropertyNode*)
504 fgDumpTerrainBranch();
506 double lon_deg = fgGetDouble("/position/longitude-deg");
507 double lat_deg = fgGetDouble("/position/latitude-deg");
508 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
509 SGVec3d zero = SGVec3d::fromGeod(geodPos);
511 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
512 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
513 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
519 * Built-in command: hires capture screen.
522 do_hires_screen_capture (const SGPropertyNode * arg)
530 * Reload the tile cache.
533 do_tile_cache_reload (const SGPropertyNode * arg)
535 static const SGPropertyNode *master_freeze
536 = fgGetNode("/sim/freeze/master");
537 bool freeze = master_freeze->getBoolValue();
538 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
540 fgSetBool("/sim/freeze/master", true);
542 if ( globals->get_tile_mgr()->init() ) {
543 // Load the local scenery data
544 double visibility_meters = fgGetDouble("/environment/visibility-m");
545 globals->get_tile_mgr()->update( visibility_meters );
547 SG_LOG( SG_GENERAL, SG_ALERT,
548 "Error in Tile Manager initialization!" );
552 fgSetBool("/sim/freeze/master", false);
559 * Set the sea level outside air temperature and assigning that to all
560 * boundary and aloft environment layers.
563 do_set_sea_level_degc (const SGPropertyNode * arg)
565 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
567 SGPropertyNode *node, *child;
570 node = fgGetNode( "/environment/config/boundary" );
571 if ( node != NULL ) {
573 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
574 child->setDoubleValue( "temperature-sea-level-degc",
575 temp_sea_level_degc );
581 node = fgGetNode( "/environment/config/aloft" );
582 if ( node != NULL ) {
584 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
585 child->setDoubleValue( "temperature-sea-level-degc",
586 temp_sea_level_degc );
596 * Set the outside air temperature at the "current" altitude by first
597 * calculating the corresponding sea level temp, and assigning that to
598 * all boundary and aloft environment layers.
601 do_set_oat_degc (const SGPropertyNode * arg)
603 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
605 // check for an altitude specified in the arguments, otherwise use
606 // current aircraft altitude.
607 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
608 if ( altitude_ft == NULL ) {
609 altitude_ft = fgGetNode("/position/altitude-ft");
612 FGEnvironment dummy; // instantiate a dummy so we can leech a method
613 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
614 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
615 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
617 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
618 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
619 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
621 SGPropertyNode *node, *child;
624 node = fgGetNode( "/environment/config/boundary" );
625 if ( node != NULL ) {
627 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
628 child->setDoubleValue( "temperature-sea-level-degc",
629 temp_sea_level_degc );
635 node = fgGetNode( "/environment/config/aloft" );
636 if ( node != NULL ) {
638 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
639 child->setDoubleValue( "temperature-sea-level-degc",
640 temp_sea_level_degc );
649 * Set the sea level outside air dewpoint and assigning that to all
650 * boundary and aloft environment layers.
653 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
655 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
657 SGPropertyNode *node, *child;
660 node = fgGetNode( "/environment/config/boundary" );
661 if ( node != NULL ) {
663 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
664 child->setDoubleValue( "dewpoint-sea-level-degc",
665 dewpoint_sea_level_degc );
671 node = fgGetNode( "/environment/config/aloft" );
672 if ( node != NULL ) {
674 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
675 child->setDoubleValue( "dewpoint-sea-level-degc",
676 dewpoint_sea_level_degc );
686 * Set the outside air dewpoint at the "current" altitude by first
687 * calculating the corresponding sea level dewpoint, and assigning
688 * that to all boundary and aloft environment layers.
691 do_set_dewpoint_degc (const SGPropertyNode * arg)
693 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
695 // check for an altitude specified in the arguments, otherwise use
696 // current aircraft altitude.
697 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
698 if ( altitude_ft == NULL ) {
699 altitude_ft = fgGetNode("/position/altitude-ft");
702 FGEnvironment dummy; // instantiate a dummy so we can leech a method
703 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
704 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
705 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
707 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
708 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
709 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
711 SGPropertyNode *node, *child;
714 node = fgGetNode( "/environment/config/boundary" );
715 if ( node != NULL ) {
717 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
718 child->setDoubleValue( "dewpoint-sea-level-degc",
719 dewpoint_sea_level_degc );
725 node = fgGetNode( "/environment/config/aloft" );
726 if ( node != NULL ) {
728 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
729 child->setDoubleValue( "dewpoint-sea-level-degc",
730 dewpoint_sea_level_degc );
739 * Update the lighting manually.
742 do_timeofday (const SGPropertyNode * arg)
744 const string &offset_type = arg->getStringValue("timeofday", "noon");
746 static const SGPropertyNode *longitude
747 = fgGetNode("/position/longitude-deg");
748 static const SGPropertyNode *latitude
749 = fgGetNode("/position/latitude-deg");
750 static const SGPropertyNode *cur_time_override
751 = fgGetNode("/sim/time/cur-time-override", true);
753 int orig_warp = globals->get_warp();
754 SGTime *t = globals->get_time_params();
755 time_t cur_time = t->get_cur_time();
756 // cout << "cur_time = " << cur_time << endl;
757 // cout << "orig_warp = " << orig_warp << endl;
760 if ( offset_type == "real" ) {
762 } else if ( offset_type == "dawn" ) {
763 warp = fgTimeSecondsUntilSunAngle( cur_time,
764 longitude->getDoubleValue()
765 * SGD_DEGREES_TO_RADIANS,
766 latitude->getDoubleValue()
767 * SGD_DEGREES_TO_RADIANS,
769 } else if ( offset_type == "morning" ) {
770 warp = fgTimeSecondsUntilSunAngle( cur_time,
771 longitude->getDoubleValue()
772 * SGD_DEGREES_TO_RADIANS,
773 latitude->getDoubleValue()
774 * SGD_DEGREES_TO_RADIANS,
776 } else if ( offset_type == "noon" ) {
777 warp = fgTimeSecondsUntilSunAngle( cur_time,
778 longitude->getDoubleValue()
779 * SGD_DEGREES_TO_RADIANS,
780 latitude->getDoubleValue()
781 * SGD_DEGREES_TO_RADIANS,
783 } else if ( offset_type == "afternoon" ) {
784 warp = fgTimeSecondsUntilSunAngle( cur_time,
785 longitude->getDoubleValue()
786 * SGD_DEGREES_TO_RADIANS,
787 latitude->getDoubleValue()
788 * SGD_DEGREES_TO_RADIANS,
790 } else if ( offset_type == "dusk" ) {
791 warp = fgTimeSecondsUntilSunAngle( cur_time,
792 longitude->getDoubleValue()
793 * SGD_DEGREES_TO_RADIANS,
794 latitude->getDoubleValue()
795 * SGD_DEGREES_TO_RADIANS,
797 } else if ( offset_type == "evening" ) {
798 warp = fgTimeSecondsUntilSunAngle( cur_time,
799 longitude->getDoubleValue()
800 * SGD_DEGREES_TO_RADIANS,
801 latitude->getDoubleValue()
802 * SGD_DEGREES_TO_RADIANS,
804 } else if ( offset_type == "midnight" ) {
805 warp = fgTimeSecondsUntilSunAngle( cur_time,
806 longitude->getDoubleValue()
807 * SGD_DEGREES_TO_RADIANS,
808 latitude->getDoubleValue()
809 * SGD_DEGREES_TO_RADIANS,
812 // cout << "warp = " << warp << endl;
813 globals->set_warp( orig_warp + warp );
815 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
816 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
817 cur_time_override->getLongValue(),
818 globals->get_warp() );
825 * Built-in command: toggle a bool property value.
827 * property: The name of the property to toggle.
830 do_property_toggle (const SGPropertyNode * arg)
832 SGPropertyNode * prop = get_prop(arg);
833 return prop->setBoolValue(!prop->getBoolValue());
838 * Built-in command: assign a value to a property.
840 * property: the name of the property to assign.
841 * value: the value to assign; or
842 * property[1]: the property to copy from.
845 do_property_assign (const SGPropertyNode * arg)
847 SGPropertyNode * prop = get_prop(arg);
848 const SGPropertyNode * prop2 = get_prop2(arg);
849 const SGPropertyNode * value = arg->getNode("value");
852 return prop->setUnspecifiedValue(value->getStringValue());
854 return prop->setUnspecifiedValue(prop2->getStringValue());
861 * Built-in command: increment or decrement a property value.
863 * If the 'step' argument is present, it will be used; otherwise,
864 * the command uses 'offset' and 'factor', usually from the mouse.
866 * property: the name of the property to increment or decrement.
867 * step: the amount of the increment or decrement (default: 0).
868 * offset: offset from the current setting (used for the mouse; multiplied
870 * factor: scaling amount for the offset (defaults to 1).
871 * min: the minimum allowed value (default: no minimum).
872 * max: the maximum allowed value (default: no maximum).
873 * mask: 'integer' to apply only to the left of the decimal point,
874 * 'decimal' to apply only to the right of the decimal point,
875 * or 'all' to apply to the whole number (the default).
876 * wrap: true if the value should be wrapped when it passes min or max;
877 * both min and max must be present for this to work (default:
881 do_property_adjust (const SGPropertyNode * arg)
883 SGPropertyNode * prop = get_prop(arg);
886 if (arg->hasValue("step"))
887 amount = arg->getDoubleValue("step");
889 amount = (arg->getDoubleValue("factor", 1)
890 * arg->getDoubleValue("offset"));
892 double unmodifiable, modifiable;
893 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
894 &unmodifiable, &modifiable);
895 modifiable += amount;
896 limit_value(&modifiable, arg);
898 prop->setDoubleValue(unmodifiable + modifiable);
905 * Built-in command: multiply a property value.
907 * property: the name of the property to multiply.
908 * factor: the amount by which to multiply.
909 * min: the minimum allowed value (default: no minimum).
910 * max: the maximum allowed value (default: no maximum).
911 * mask: 'integer' to apply only to the left of the decimal point,
912 * 'decimal' to apply only to the right of the decimal point,
913 * or 'all' to apply to the whole number (the default).
914 * wrap: true if the value should be wrapped when it passes min or max;
915 * both min and max must be present for this to work (default:
919 do_property_multiply (const SGPropertyNode * arg)
921 SGPropertyNode * prop = get_prop(arg);
922 double factor = arg->getDoubleValue("factor", 1);
924 double unmodifiable, modifiable;
925 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
926 &unmodifiable, &modifiable);
927 modifiable *= factor;
928 limit_value(&modifiable, arg);
930 prop->setDoubleValue(unmodifiable + modifiable);
937 * Built-in command: swap two property values.
939 * property[0]: the name of the first property.
940 * property[1]: the name of the second property.
943 do_property_swap (const SGPropertyNode * arg)
945 SGPropertyNode * prop1 = get_prop(arg);
946 SGPropertyNode * prop2 = get_prop2(arg);
948 // FIXME: inefficient
949 const string & tmp = prop1->getStringValue();
950 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
951 prop2->setUnspecifiedValue(tmp.c_str()));
956 * Built-in command: Set a property to an axis or other moving input.
958 * property: the name of the property to set.
959 * setting: the current input setting, usually between -1.0 and 1.0.
960 * offset: the offset to shift by, before applying the factor.
961 * factor: the factor to multiply by (use negative to reverse).
964 do_property_scale (const SGPropertyNode * arg)
966 SGPropertyNode * prop = get_prop(arg);
967 double setting = arg->getDoubleValue("setting");
968 double offset = arg->getDoubleValue("offset", 0.0);
969 double factor = arg->getDoubleValue("factor", 1.0);
970 bool squared = arg->getBoolValue("squared", false);
971 int power = arg->getIntValue("power", (squared ? 2 : 1));
973 int sign = (setting < 0 ? -1 : 1);
979 setting = setting * setting * sign;
982 setting = setting * setting * setting;
985 setting = setting * setting * setting * setting * sign;
988 setting = pow(setting, power);
989 if ((power % 2) == 0)
994 return prop->setDoubleValue((setting + offset) * factor);
999 * Built-in command: cycle a property through a set of values.
1001 * If the current value isn't in the list, the cycle will
1002 * (re)start from the beginning.
1004 * property: the name of the property to cycle.
1005 * value[*]: the list of values to cycle through.
1008 do_property_cycle (const SGPropertyNode * arg)
1010 SGPropertyNode * prop = get_prop(arg);
1011 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
1013 int nSelections = values.size();
1015 if (nSelections < 1) {
1016 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
1020 // Try to find the current selection
1021 for (int i = 0; i < nSelections; i++) {
1022 if (compare_values(prop, values[i])) {
1028 // Default or wrap to the first selection
1029 if (selection < 0 || selection >= nSelections)
1032 prop->setUnspecifiedValue(values[selection]->getStringValue());
1038 * Built-in command: randomize a numeric property value.
1040 * property: the name of the property value to randomize.
1041 * min: the minimum allowed value.
1042 * max: the maximum allowed value.
1045 do_property_randomize (const SGPropertyNode * arg)
1047 SGPropertyNode * prop = get_prop(arg);
1048 double min = arg->getDoubleValue("min", DBL_MIN);
1049 double max = arg->getDoubleValue("max", DBL_MAX);
1050 prop->setDoubleValue(sg_random() * (max - min) + min);
1056 * Built-in command: reinit the data logging system based on the
1057 * current contents of the /logger tree.
1060 do_data_logging_commit (const SGPropertyNode * arg)
1062 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1068 * Built-in command: Add a dialog to the GUI system. Does *not*
1069 * display the dialog. The property node should have the same format
1070 * as a dialog XML configuration. It must include:
1072 * name: the name of the GUI dialog for future reference.
1075 do_dialog_new (const SGPropertyNode * arg)
1077 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1079 // Note the casting away of const: this is *real*. Doing a
1080 // "dialog-apply" command later on will mutate this property node.
1081 // I'm not convinced that this isn't the Right Thing though; it
1082 // allows client to create a node, pass it to dialog-new, and get
1083 // the values back from the dialog by reading the same node.
1084 // Perhaps command arguments are not as "const" as they would
1086 gui->newDialog((SGPropertyNode*)arg);
1091 * Built-in command: Show an XML-configured dialog.
1093 * dialog-name: the name of the GUI dialog to display.
1096 do_dialog_show (const SGPropertyNode * arg)
1098 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1099 gui->showDialog(arg->getStringValue("dialog-name"));
1105 * Built-in Command: Hide the active XML-configured dialog.
1108 do_dialog_close (const SGPropertyNode * arg)
1110 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1111 if(arg->hasValue("dialog-name"))
1112 return gui->closeDialog(arg->getStringValue("dialog-name"));
1113 return gui->closeActiveDialog();
1118 * Update a value in the active XML-configured dialog.
1120 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1123 do_dialog_update (const SGPropertyNode * arg)
1125 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1127 if (arg->hasValue("dialog-name"))
1128 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1130 dialog = gui->getActiveDialog();
1133 dialog->updateValues(arg->getStringValue("object-name"));
1142 * Apply a value in the active XML-configured dialog.
1144 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1147 do_dialog_apply (const SGPropertyNode * arg)
1149 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1151 if (arg->hasValue("dialog-name"))
1152 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1154 dialog = gui->getActiveDialog();
1157 dialog->applyValues(arg->getStringValue("object-name"));
1166 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1170 do_gui_redraw (const SGPropertyNode * arg)
1172 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1179 * Set mouse cursor coordinates and cursor shape.
1182 do_set_cursor (const SGPropertyNode * arg)
1184 if (arg->hasValue("x") || arg->hasValue("y")) {
1185 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1186 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1187 int x = arg->getIntValue("x", mx->getIntValue());
1188 int y = arg->getIntValue("y", my->getIntValue());
1194 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1195 if (cursor->getType() != SGPropertyNode::NONE)
1196 fgSetMouseCursor(cursor->getIntValue());
1198 cursor->setIntValue(fgGetMouseCursor());
1204 * Built-in command: play an audio message (i.e. a wav file) This is
1205 * fire and forget. Call this once per message and it will get dumped
1206 * into a queue. Messages are played sequentially so they do not
1210 do_play_audio_sample (const SGPropertyNode * arg)
1212 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1213 string path = arg->getStringValue("path");
1214 string file = arg->getStringValue("file");
1215 double volume = arg->getDoubleValue("volume");
1216 // cout << "playing " << path << " / " << file << endl;
1218 fx->play_message( path, file, volume );
1221 } catch (const sg_io_exception& e) {
1222 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1223 "failed to load" << path << '/' << file);
1229 * Built-in command: commit presets (read from in /sim/presets/)
1232 do_presets_commit (const SGPropertyNode * arg)
1234 // unbind the current fdm state so property changes
1235 // don't get lost when we subsequently delete this fdm
1236 // and create a new one.
1237 cur_fdm_state->unbind();
1239 // set position from presets
1242 fgReInitSubsystems();
1244 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1247 if ( ! fgGetBool("/sim/presets/onground") ) {
1248 fgSetBool( "/sim/freeze/master", true );
1249 fgSetBool( "/sim/freeze/clock", true );
1257 * Built-in command: set log level (0 ... 7)
1260 do_log_level (const SGPropertyNode * arg)
1262 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1268 * Built-in command: replay the FDR buffer
1271 do_replay (const SGPropertyNode * arg)
1273 // freeze the master fdm
1274 fgSetInt( "/sim/freeze/replay-state", 1 );
1276 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1278 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1279 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1280 double duration = fgGetDouble( "/sim/replay/duration" );
1281 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1282 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1284 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1287 // cout << "start = " << r->get_start_time()
1288 // << " end = " << r->get_end_time() << endl;
1295 do_decrease_visibility (const SGPropertyNode * arg)
1297 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1298 fgSetDouble("/environment/visibility-m", new_value);
1299 fgDefaultWeatherValue("visibility-m", new_value);
1300 globals->get_subsystem("environment")->reinit();
1306 do_increase_visibility (const SGPropertyNode * arg)
1308 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1309 fgSetDouble("/environment/visibility-m", new_value);
1310 fgDefaultWeatherValue("visibility-m", new_value);
1311 globals->get_subsystem("environment")->reinit();
1317 do_hud_init(const SGPropertyNode *)
1319 fgHUDInit(0); // minimal HUD
1324 do_hud_init2(const SGPropertyNode *)
1326 fgHUDInit2(0); // normal HUD
1332 * An fgcommand to allow loading of xml files via nasal,
1333 * the xml file's structure will be made available within
1334 * a property tree node defined under argument "targetnode",
1335 * or in the given argument tree under "data" otherwise.
1337 * @param filename a string to hold the complete path & filename of an XML file
1338 * @param targetnode a string pointing to a location within the property tree
1339 * where to store the parsed XML file. If <targetnode> is undefined, then the
1340 * file contents are stored under a node <data> in the argument tree.
1344 do_load_xml_to_proptree(const SGPropertyNode * arg)
1346 SGPath file(arg->getStringValue("filename"));
1347 if (file.str().empty())
1350 if (file.extension() != "xml")
1351 file.concat(".xml");
1353 if (!fgValidatePath(file.c_str(), false)) {
1354 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1355 "(unauthorized access)");
1359 SGPropertyNode *targetnode;
1360 if (arg->hasValue("targetnode"))
1361 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1363 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1366 readProperties(file.c_str(), targetnode, true);
1367 } catch (const sg_exception &e) {
1368 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1377 * An fgcommand to allow saving of xml files via nasal,
1378 * the file's structure will be determined based on what's
1379 * encountered in the passed (source) property tree node
1381 * @param filename a string to hold the complete path & filename of the (new)
1383 * @param sourcenode a string pointing to a location within the property tree
1384 * where to find the nodes that should be written recursively into an XML file
1385 * @param data if no sourcenode is given, then the file contents are taken from
1386 * the argument tree's "data" node.
1390 do_save_xml_from_proptree(const SGPropertyNode * arg)
1392 SGPath file(arg->getStringValue("filename"));
1393 if (file.str().empty())
1396 if (file.extension() != "xml")
1397 file.concat(".xml");
1399 if (!fgValidatePath(file.c_str(), true)) {
1400 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1401 "(unauthorized access)");
1405 SGPropertyNode *sourcenode;
1406 if (arg->hasValue("sourcenode"))
1407 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1408 else if (arg->getNode("data", false))
1409 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1414 writeProperties (file.c_str(), sourcenode, true);
1415 } catch (const sg_exception &e) {
1416 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1424 do_press_cockpit_button (const SGPropertyNode *arg)
1426 const char *prefix = arg->getStringValue("prefix");
1428 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1431 string prop = string(prefix) + "-button";
1434 if (arg->getBoolValue("latching"))
1435 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1439 fgSetDouble(prop.c_str(), value);
1440 fgSetBool(arg->getStringValue("discrete"), value > 0);
1446 do_release_cockpit_button (const SGPropertyNode *arg)
1448 const char *prefix = arg->getStringValue("prefix");
1450 if (arg->getBoolValue("guarded")) {
1451 string prop = string(prefix) + "-guard";
1452 if (fgGetDouble(prop.c_str()) < 1) {
1453 fgSetDouble(prop.c_str(), 1);
1458 if (! arg->getBoolValue("latching")) {
1459 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1460 fgSetBool(arg->getStringValue("discrete"), false);
1467 ////////////////////////////////////////////////////////////////////////
1469 ////////////////////////////////////////////////////////////////////////
1473 * Table of built-in commands.
1475 * New commands do not have to be added here; any module in the application
1476 * can add a new command using globals->get_commands()->addCommand(...).
1480 SGCommandMgr::command_t command;
1482 { "null", do_null },
1483 { "nasal", do_nasal },
1484 { "exit", do_exit },
1485 { "reset", do_reset },
1486 { "reinit", do_reinit },
1487 { "suspend", do_reinit },
1488 { "resume", do_reinit },
1489 { "load", do_load },
1490 { "save", do_save },
1491 { "panel-load", do_panel_load },
1492 { "panel-mouse-click", do_panel_mouse_click },
1493 { "preferences-load", do_preferences_load },
1494 { "view-cycle", do_view_cycle },
1495 { "screen-capture", do_screen_capture },
1496 { "hires-screen-capture", do_hires_screen_capture },
1497 { "tile-cache-reload", do_tile_cache_reload },
1498 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1499 { "set-outside-air-temp-degc", do_set_oat_degc },
1500 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1501 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1502 { "timeofday", do_timeofday },
1503 { "property-toggle", do_property_toggle },
1504 { "property-assign", do_property_assign },
1505 { "property-adjust", do_property_adjust },
1506 { "property-multiply", do_property_multiply },
1507 { "property-swap", do_property_swap },
1508 { "property-scale", do_property_scale },
1509 { "property-cycle", do_property_cycle },
1510 { "property-randomize", do_property_randomize },
1511 { "data-logging-commit", do_data_logging_commit },
1512 { "dialog-new", do_dialog_new },
1513 { "dialog-show", do_dialog_show },
1514 { "dialog-close", do_dialog_close },
1515 { "dialog-update", do_dialog_update },
1516 { "dialog-apply", do_dialog_apply },
1517 { "gui-redraw", do_gui_redraw },
1518 { "set-cursor", do_set_cursor },
1519 { "play-audio-sample", do_play_audio_sample },
1520 { "presets-commit", do_presets_commit },
1521 { "log-level", do_log_level },
1522 { "replay", do_replay },
1523 { "decrease-visibility", do_decrease_visibility },
1524 { "increase-visibility", do_increase_visibility },
1525 { "hud-init", do_hud_init },
1526 { "hud-init2", do_hud_init2 },
1527 { "loadxml", do_load_xml_to_proptree},
1528 { "savexml", do_save_xml_from_proptree },
1529 { "press-cockpit-button", do_press_cockpit_button },
1530 { "release-cockpit-button", do_release_cockpit_button },
1531 { "dump-scenegraph", do_dump_scene_graph },
1532 { "dump-terrainbranch", do_dump_terrain_branch },
1533 { 0, 0 } // zero-terminated
1538 * Initialize the default built-in commands.
1540 * Other commands may be added by other parts of the application.
1545 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1546 for (int i = 0; built_ins[i].name != 0; i++) {
1547 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1548 globals->get_commands()->addCommand(built_ins[i].name,
1549 built_ins[i].command);
1552 typedef bool (*dummy)();
1553 fgTie( "/command/view/next", dummy(0), do_view_next );
1554 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1557 // end of fg_commands.cxx