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 string file = arg->getStringValue("file", "fgfs.sav");
324 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
327 if (!fgValidatePath(file.c_str(), false)) {
328 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
329 "(unauthorized access)");
333 ifstream input(file.c_str());
334 if (input.good() && fgLoadFlight(input)) {
336 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
339 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
346 * Built-in command: save flight.
348 * file (optional): the name of the file to save (relative to the
349 * current directory). Defaults to "fgfs.sav".
352 do_save (const SGPropertyNode * arg)
354 string file = arg->getStringValue("file", "fgfs.sav");
355 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
358 if (!fgValidatePath(file.c_str(), false)) {
359 SG_LOG(SG_IO, SG_ALERT, "save: reading '" << file << "' denied "
360 "(unauthorized access)");
364 bool write_all = arg->getBoolValue("write-all", false);
365 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
366 ofstream output(file.c_str());
367 if (output.good() && fgSaveFlight(output, write_all)) {
369 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
372 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
379 * Built-in command: (re)load the panel.
381 * path (optional): the file name to load the panel from
382 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
383 * and if that's unspecified, to "Panels/Default/default.xml".
386 do_panel_load (const SGPropertyNode * arg)
389 arg->getStringValue("path",
390 fgGetString("/sim/panel/path",
391 "Panels/Default/default.xml"));
392 FGPanel * new_panel = fgReadPanel(panel_path);
393 if (new_panel == 0) {
394 SG_LOG(SG_INPUT, SG_ALERT,
395 "Error reading new panel from " << panel_path);
398 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
399 globals->get_current_panel()->unbind();
400 delete globals->get_current_panel();
401 globals->set_current_panel( new_panel );
402 globals->get_current_panel()->bind();
408 * Built-in command: pass a mouse click to the panel.
410 * button: the mouse button number, zero-based.
411 * is-down: true if the button is down, false if it is up.
412 * x-pos: the x position of the mouse click.
413 * y-pos: the y position of the mouse click.
416 do_panel_mouse_click (const SGPropertyNode * arg)
418 if (globals->get_current_panel() != 0)
419 return globals->get_current_panel()
420 ->doMouseAction(arg->getIntValue("button"),
421 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
422 arg->getIntValue("x-pos"),
423 arg->getIntValue("y-pos"));
430 * Built-in command: (re)load preferences.
432 * path (optional): the file name to load the panel from (relative
433 * to FG_ROOT). Defaults to "preferences.xml".
436 do_preferences_load (const SGPropertyNode * arg)
439 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
440 globals->get_props());
441 } catch (const sg_exception &e) {
442 guiErrorMessage("Error reading global preferences: ", e);
445 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
453 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
454 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
455 if ( globals->get_viewmgr()->get_current() == 1 ) {
456 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
464 globals->get_current_view()->setHeadingOffset_deg(0.0);
465 globals->get_viewmgr()->next_view();
466 fix_hud_visibility();
472 globals->get_current_view()->setHeadingOffset_deg(0.0);
473 globals->get_viewmgr()->prev_view();
474 fix_hud_visibility();
478 * Built-in command: cycle view.
481 do_view_cycle (const SGPropertyNode * arg)
483 globals->get_current_view()->setHeadingOffset_deg(0.0);
484 globals->get_viewmgr()->next_view();
485 fix_hud_visibility();
490 * Built-in command: capture screen.
493 do_screen_capture (const SGPropertyNode * arg)
495 return fgDumpSnapShot();
499 do_dump_scene_graph (const SGPropertyNode*)
506 do_dump_terrain_branch (const SGPropertyNode*)
508 fgDumpTerrainBranch();
510 double lon_deg = fgGetDouble("/position/longitude-deg");
511 double lat_deg = fgGetDouble("/position/latitude-deg");
512 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
513 SGVec3d zero = SGVec3d::fromGeod(geodPos);
515 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
516 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
517 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
523 * Built-in command: hires capture screen.
526 do_hires_screen_capture (const SGPropertyNode * arg)
534 * Reload the tile cache.
537 do_tile_cache_reload (const SGPropertyNode * arg)
539 static const SGPropertyNode *master_freeze
540 = fgGetNode("/sim/freeze/master");
541 bool freeze = master_freeze->getBoolValue();
542 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
544 fgSetBool("/sim/freeze/master", true);
546 if ( globals->get_tile_mgr()->init() ) {
547 // Load the local scenery data
548 double visibility_meters = fgGetDouble("/environment/visibility-m");
549 globals->get_tile_mgr()->update( visibility_meters );
551 SG_LOG( SG_GENERAL, SG_ALERT,
552 "Error in Tile Manager initialization!" );
556 fgSetBool("/sim/freeze/master", false);
563 * Set the sea level outside air temperature and assigning that to all
564 * boundary and aloft environment layers.
567 do_set_sea_level_degc (const SGPropertyNode * arg)
569 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
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 );
600 * Set the outside air temperature at the "current" altitude by first
601 * calculating the corresponding sea level temp, and assigning that to
602 * all boundary and aloft environment layers.
605 do_set_oat_degc (const SGPropertyNode * arg)
607 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
609 // check for an altitude specified in the arguments, otherwise use
610 // current aircraft altitude.
611 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
612 if ( altitude_ft == NULL ) {
613 altitude_ft = fgGetNode("/position/altitude-ft");
616 FGEnvironment dummy; // instantiate a dummy so we can leech a method
617 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
618 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
619 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
621 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
622 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
623 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
625 SGPropertyNode *node, *child;
628 node = fgGetNode( "/environment/config/boundary" );
629 if ( node != NULL ) {
631 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
632 child->setDoubleValue( "temperature-sea-level-degc",
633 temp_sea_level_degc );
639 node = fgGetNode( "/environment/config/aloft" );
640 if ( node != NULL ) {
642 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
643 child->setDoubleValue( "temperature-sea-level-degc",
644 temp_sea_level_degc );
653 * Set the sea level outside air dewpoint and assigning that to all
654 * boundary and aloft environment layers.
657 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
659 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
661 SGPropertyNode *node, *child;
664 node = fgGetNode( "/environment/config/boundary" );
665 if ( node != NULL ) {
667 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
668 child->setDoubleValue( "dewpoint-sea-level-degc",
669 dewpoint_sea_level_degc );
675 node = fgGetNode( "/environment/config/aloft" );
676 if ( node != NULL ) {
678 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
679 child->setDoubleValue( "dewpoint-sea-level-degc",
680 dewpoint_sea_level_degc );
690 * Set the outside air dewpoint at the "current" altitude by first
691 * calculating the corresponding sea level dewpoint, and assigning
692 * that to all boundary and aloft environment layers.
695 do_set_dewpoint_degc (const SGPropertyNode * arg)
697 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
699 // check for an altitude specified in the arguments, otherwise use
700 // current aircraft altitude.
701 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
702 if ( altitude_ft == NULL ) {
703 altitude_ft = fgGetNode("/position/altitude-ft");
706 FGEnvironment dummy; // instantiate a dummy so we can leech a method
707 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
708 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
709 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
711 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
712 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
713 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
715 SGPropertyNode *node, *child;
718 node = fgGetNode( "/environment/config/boundary" );
719 if ( node != NULL ) {
721 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
722 child->setDoubleValue( "dewpoint-sea-level-degc",
723 dewpoint_sea_level_degc );
729 node = fgGetNode( "/environment/config/aloft" );
730 if ( node != NULL ) {
732 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
733 child->setDoubleValue( "dewpoint-sea-level-degc",
734 dewpoint_sea_level_degc );
743 * Update the lighting manually.
746 do_timeofday (const SGPropertyNode * arg)
748 const string &offset_type = arg->getStringValue("timeofday", "noon");
750 static const SGPropertyNode *longitude
751 = fgGetNode("/position/longitude-deg");
752 static const SGPropertyNode *latitude
753 = fgGetNode("/position/latitude-deg");
754 static const SGPropertyNode *cur_time_override
755 = fgGetNode("/sim/time/cur-time-override", true);
757 int orig_warp = globals->get_warp();
758 SGTime *t = globals->get_time_params();
759 time_t cur_time = t->get_cur_time();
760 // cout << "cur_time = " << cur_time << endl;
761 // cout << "orig_warp = " << orig_warp << endl;
764 if ( offset_type == "real" ) {
766 } else if ( offset_type == "dawn" ) {
767 warp = fgTimeSecondsUntilSunAngle( cur_time,
768 longitude->getDoubleValue()
769 * SGD_DEGREES_TO_RADIANS,
770 latitude->getDoubleValue()
771 * SGD_DEGREES_TO_RADIANS,
773 } else if ( offset_type == "morning" ) {
774 warp = fgTimeSecondsUntilSunAngle( cur_time,
775 longitude->getDoubleValue()
776 * SGD_DEGREES_TO_RADIANS,
777 latitude->getDoubleValue()
778 * SGD_DEGREES_TO_RADIANS,
780 } else if ( offset_type == "noon" ) {
781 warp = fgTimeSecondsUntilSunAngle( cur_time,
782 longitude->getDoubleValue()
783 * SGD_DEGREES_TO_RADIANS,
784 latitude->getDoubleValue()
785 * SGD_DEGREES_TO_RADIANS,
787 } else if ( offset_type == "afternoon" ) {
788 warp = fgTimeSecondsUntilSunAngle( cur_time,
789 longitude->getDoubleValue()
790 * SGD_DEGREES_TO_RADIANS,
791 latitude->getDoubleValue()
792 * SGD_DEGREES_TO_RADIANS,
794 } else if ( offset_type == "dusk" ) {
795 warp = fgTimeSecondsUntilSunAngle( cur_time,
796 longitude->getDoubleValue()
797 * SGD_DEGREES_TO_RADIANS,
798 latitude->getDoubleValue()
799 * SGD_DEGREES_TO_RADIANS,
801 } else if ( offset_type == "evening" ) {
802 warp = fgTimeSecondsUntilSunAngle( cur_time,
803 longitude->getDoubleValue()
804 * SGD_DEGREES_TO_RADIANS,
805 latitude->getDoubleValue()
806 * SGD_DEGREES_TO_RADIANS,
808 } else if ( offset_type == "midnight" ) {
809 warp = fgTimeSecondsUntilSunAngle( cur_time,
810 longitude->getDoubleValue()
811 * SGD_DEGREES_TO_RADIANS,
812 latitude->getDoubleValue()
813 * SGD_DEGREES_TO_RADIANS,
816 // cout << "warp = " << warp << endl;
817 globals->set_warp( orig_warp + warp );
819 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
820 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
821 cur_time_override->getLongValue(),
822 globals->get_warp() );
829 * Built-in command: toggle a bool property value.
831 * property: The name of the property to toggle.
834 do_property_toggle (const SGPropertyNode * arg)
836 SGPropertyNode * prop = get_prop(arg);
837 return prop->setBoolValue(!prop->getBoolValue());
842 * Built-in command: assign a value to a property.
844 * property: the name of the property to assign.
845 * value: the value to assign; or
846 * property[1]: the property to copy from.
849 do_property_assign (const SGPropertyNode * arg)
851 SGPropertyNode * prop = get_prop(arg);
852 const SGPropertyNode * prop2 = get_prop2(arg);
853 const SGPropertyNode * value = arg->getNode("value");
856 return prop->setUnspecifiedValue(value->getStringValue());
858 return prop->setUnspecifiedValue(prop2->getStringValue());
865 * Built-in command: increment or decrement a property value.
867 * If the 'step' argument is present, it will be used; otherwise,
868 * the command uses 'offset' and 'factor', usually from the mouse.
870 * property: the name of the property to increment or decrement.
871 * step: the amount of the increment or decrement (default: 0).
872 * offset: offset from the current setting (used for the mouse; multiplied
874 * factor: scaling amount for the offset (defaults to 1).
875 * min: the minimum allowed value (default: no minimum).
876 * max: the maximum allowed value (default: no maximum).
877 * mask: 'integer' to apply only to the left of the decimal point,
878 * 'decimal' to apply only to the right of the decimal point,
879 * or 'all' to apply to the whole number (the default).
880 * wrap: true if the value should be wrapped when it passes min or max;
881 * both min and max must be present for this to work (default:
885 do_property_adjust (const SGPropertyNode * arg)
887 SGPropertyNode * prop = get_prop(arg);
890 if (arg->hasValue("step"))
891 amount = arg->getDoubleValue("step");
893 amount = (arg->getDoubleValue("factor", 1)
894 * arg->getDoubleValue("offset"));
896 double unmodifiable, modifiable;
897 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
898 &unmodifiable, &modifiable);
899 modifiable += amount;
900 limit_value(&modifiable, arg);
902 prop->setDoubleValue(unmodifiable + modifiable);
909 * Built-in command: multiply a property value.
911 * property: the name of the property to multiply.
912 * factor: the amount by which to multiply.
913 * min: the minimum allowed value (default: no minimum).
914 * max: the maximum allowed value (default: no maximum).
915 * mask: 'integer' to apply only to the left of the decimal point,
916 * 'decimal' to apply only to the right of the decimal point,
917 * or 'all' to apply to the whole number (the default).
918 * wrap: true if the value should be wrapped when it passes min or max;
919 * both min and max must be present for this to work (default:
923 do_property_multiply (const SGPropertyNode * arg)
925 SGPropertyNode * prop = get_prop(arg);
926 double factor = arg->getDoubleValue("factor", 1);
928 double unmodifiable, modifiable;
929 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
930 &unmodifiable, &modifiable);
931 modifiable *= factor;
932 limit_value(&modifiable, arg);
934 prop->setDoubleValue(unmodifiable + modifiable);
941 * Built-in command: swap two property values.
943 * property[0]: the name of the first property.
944 * property[1]: the name of the second property.
947 do_property_swap (const SGPropertyNode * arg)
949 SGPropertyNode * prop1 = get_prop(arg);
950 SGPropertyNode * prop2 = get_prop2(arg);
952 // FIXME: inefficient
953 const string & tmp = prop1->getStringValue();
954 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
955 prop2->setUnspecifiedValue(tmp.c_str()));
960 * Built-in command: Set a property to an axis or other moving input.
962 * property: the name of the property to set.
963 * setting: the current input setting, usually between -1.0 and 1.0.
964 * offset: the offset to shift by, before applying the factor.
965 * factor: the factor to multiply by (use negative to reverse).
968 do_property_scale (const SGPropertyNode * arg)
970 SGPropertyNode * prop = get_prop(arg);
971 double setting = arg->getDoubleValue("setting");
972 double offset = arg->getDoubleValue("offset", 0.0);
973 double factor = arg->getDoubleValue("factor", 1.0);
974 bool squared = arg->getBoolValue("squared", false);
975 int power = arg->getIntValue("power", (squared ? 2 : 1));
977 int sign = (setting < 0 ? -1 : 1);
983 setting = setting * setting * sign;
986 setting = setting * setting * setting;
989 setting = setting * setting * setting * setting * sign;
992 setting = pow(setting, power);
993 if ((power % 2) == 0)
998 return prop->setDoubleValue((setting + offset) * factor);
1003 * Built-in command: cycle a property through a set of values.
1005 * If the current value isn't in the list, the cycle will
1006 * (re)start from the beginning.
1008 * property: the name of the property to cycle.
1009 * value[*]: the list of values to cycle through.
1012 do_property_cycle (const SGPropertyNode * arg)
1014 SGPropertyNode * prop = get_prop(arg);
1015 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
1017 int nSelections = values.size();
1019 if (nSelections < 1) {
1020 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
1024 // Try to find the current selection
1025 for (int i = 0; i < nSelections; i++) {
1026 if (compare_values(prop, values[i])) {
1032 // Default or wrap to the first selection
1033 if (selection < 0 || selection >= nSelections)
1036 prop->setUnspecifiedValue(values[selection]->getStringValue());
1042 * Built-in command: randomize a numeric property value.
1044 * property: the name of the property value to randomize.
1045 * min: the minimum allowed value.
1046 * max: the maximum allowed value.
1049 do_property_randomize (const SGPropertyNode * arg)
1051 SGPropertyNode * prop = get_prop(arg);
1052 double min = arg->getDoubleValue("min", DBL_MIN);
1053 double max = arg->getDoubleValue("max", DBL_MAX);
1054 prop->setDoubleValue(sg_random() * (max - min) + min);
1060 * Built-in command: reinit the data logging system based on the
1061 * current contents of the /logger tree.
1064 do_data_logging_commit (const SGPropertyNode * arg)
1066 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1072 * Built-in command: Add a dialog to the GUI system. Does *not*
1073 * display the dialog. The property node should have the same format
1074 * as a dialog XML configuration. It must include:
1076 * name: the name of the GUI dialog for future reference.
1079 do_dialog_new (const SGPropertyNode * arg)
1081 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1083 // Note the casting away of const: this is *real*. Doing a
1084 // "dialog-apply" command later on will mutate this property node.
1085 // I'm not convinced that this isn't the Right Thing though; it
1086 // allows client to create a node, pass it to dialog-new, and get
1087 // the values back from the dialog by reading the same node.
1088 // Perhaps command arguments are not as "const" as they would
1090 gui->newDialog((SGPropertyNode*)arg);
1095 * Built-in command: Show an XML-configured dialog.
1097 * dialog-name: the name of the GUI dialog to display.
1100 do_dialog_show (const SGPropertyNode * arg)
1102 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1103 gui->showDialog(arg->getStringValue("dialog-name"));
1109 * Built-in Command: Hide the active XML-configured dialog.
1112 do_dialog_close (const SGPropertyNode * arg)
1114 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1115 if(arg->hasValue("dialog-name"))
1116 return gui->closeDialog(arg->getStringValue("dialog-name"));
1117 return gui->closeActiveDialog();
1122 * Update a value in the active XML-configured dialog.
1124 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1127 do_dialog_update (const SGPropertyNode * arg)
1129 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1131 if (arg->hasValue("dialog-name"))
1132 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1134 dialog = gui->getActiveDialog();
1137 dialog->updateValues(arg->getStringValue("object-name"));
1146 * Apply a value in the active XML-configured dialog.
1148 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1151 do_dialog_apply (const SGPropertyNode * arg)
1153 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1155 if (arg->hasValue("dialog-name"))
1156 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1158 dialog = gui->getActiveDialog();
1161 dialog->applyValues(arg->getStringValue("object-name"));
1170 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1174 do_gui_redraw (const SGPropertyNode * arg)
1176 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1183 * Set mouse cursor coordinates and cursor shape.
1186 do_set_cursor (const SGPropertyNode * arg)
1188 if (arg->hasValue("x") || arg->hasValue("y")) {
1189 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1190 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1191 int x = arg->getIntValue("x", mx->getIntValue());
1192 int y = arg->getIntValue("y", my->getIntValue());
1198 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1199 if (cursor->getType() != SGPropertyNode::NONE)
1200 fgSetMouseCursor(cursor->getIntValue());
1202 cursor->setIntValue(fgGetMouseCursor());
1208 * Built-in command: play an audio message (i.e. a wav file) This is
1209 * fire and forget. Call this once per message and it will get dumped
1210 * into a queue. Messages are played sequentially so they do not
1214 do_play_audio_sample (const SGPropertyNode * arg)
1216 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1217 string path = arg->getStringValue("path");
1218 string file = arg->getStringValue("file");
1219 double volume = arg->getDoubleValue("volume");
1220 // cout << "playing " << path << " / " << file << endl;
1222 fx->play_message( path, file, volume );
1225 } catch (const sg_io_exception& e) {
1226 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1227 "failed to load" << path << '/' << file);
1233 * Built-in command: commit presets (read from in /sim/presets/)
1236 do_presets_commit (const SGPropertyNode * arg)
1238 // unbind the current fdm state so property changes
1239 // don't get lost when we subsequently delete this fdm
1240 // and create a new one.
1241 cur_fdm_state->unbind();
1243 // set position from presets
1246 fgReInitSubsystems();
1248 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1251 if ( ! fgGetBool("/sim/presets/onground") ) {
1252 fgSetBool( "/sim/freeze/master", true );
1253 fgSetBool( "/sim/freeze/clock", true );
1261 * Built-in command: set log level (0 ... 7)
1264 do_log_level (const SGPropertyNode * arg)
1266 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1272 * Built-in command: replay the FDR buffer
1275 do_replay (const SGPropertyNode * arg)
1277 // freeze the master fdm
1278 fgSetInt( "/sim/freeze/replay-state", 1 );
1280 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1282 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1283 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1284 double duration = fgGetDouble( "/sim/replay/duration" );
1285 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1286 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1288 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1291 // cout << "start = " << r->get_start_time()
1292 // << " end = " << r->get_end_time() << endl;
1299 do_decrease_visibility (const SGPropertyNode * arg)
1301 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1302 fgSetDouble("/environment/visibility-m", new_value);
1303 fgDefaultWeatherValue("visibility-m", new_value);
1304 globals->get_subsystem("environment")->reinit();
1310 do_increase_visibility (const SGPropertyNode * arg)
1312 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1313 fgSetDouble("/environment/visibility-m", new_value);
1314 fgDefaultWeatherValue("visibility-m", new_value);
1315 globals->get_subsystem("environment")->reinit();
1321 do_hud_init(const SGPropertyNode *)
1323 fgHUDInit(0); // minimal HUD
1328 do_hud_init2(const SGPropertyNode *)
1330 fgHUDInit2(0); // normal HUD
1336 * An fgcommand to allow loading of xml files via nasal,
1337 * the xml file's structure will be made available within
1338 * a property tree node defined under argument "targetnode",
1339 * or in the given argument tree under "data" otherwise.
1341 * @param filename a string to hold the complete path & filename of an XML file
1342 * @param targetnode a string pointing to a location within the property tree
1343 * where to store the parsed XML file. If <targetnode> is undefined, then the
1344 * file contents are stored under a node <data> in the argument tree.
1348 do_load_xml_to_proptree(const SGPropertyNode * arg)
1350 SGPath file(arg->getStringValue("filename"));
1351 if (file.str().empty())
1354 if (file.extension() != "xml")
1355 file.concat(".xml");
1357 if (!fgValidatePath(file.c_str(), false)) {
1358 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1359 "(unauthorized access)");
1363 SGPropertyNode *targetnode;
1364 if (arg->hasValue("targetnode"))
1365 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1367 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1370 readProperties(file.c_str(), targetnode, true);
1371 } catch (const sg_exception &e) {
1372 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1381 * An fgcommand to allow saving of xml files via nasal,
1382 * the file's structure will be determined based on what's
1383 * encountered in the passed (source) property tree node
1385 * @param filename a string to hold the complete path & filename of the (new)
1387 * @param sourcenode a string pointing to a location within the property tree
1388 * where to find the nodes that should be written recursively into an XML file
1389 * @param data if no sourcenode is given, then the file contents are taken from
1390 * the argument tree's "data" node.
1394 do_save_xml_from_proptree(const SGPropertyNode * arg)
1396 SGPath file(arg->getStringValue("filename"));
1397 if (file.str().empty())
1400 if (file.extension() != "xml")
1401 file.concat(".xml");
1403 if (!fgValidatePath(file.c_str(), true)) {
1404 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1405 "(unauthorized access)");
1409 SGPropertyNode *sourcenode;
1410 if (arg->hasValue("sourcenode"))
1411 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1412 else if (arg->getNode("data", false))
1413 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1418 writeProperties (file.c_str(), sourcenode, true);
1419 } catch (const sg_exception &e) {
1420 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1428 do_press_cockpit_button (const SGPropertyNode *arg)
1430 const char *prefix = arg->getStringValue("prefix");
1432 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1435 string prop = string(prefix) + "-button";
1438 if (arg->getBoolValue("latching"))
1439 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1443 fgSetDouble(prop.c_str(), value);
1444 fgSetBool(arg->getStringValue("discrete"), value > 0);
1450 do_release_cockpit_button (const SGPropertyNode *arg)
1452 const char *prefix = arg->getStringValue("prefix");
1454 if (arg->getBoolValue("guarded")) {
1455 string prop = string(prefix) + "-guard";
1456 if (fgGetDouble(prop.c_str()) < 1) {
1457 fgSetDouble(prop.c_str(), 1);
1462 if (! arg->getBoolValue("latching")) {
1463 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1464 fgSetBool(arg->getStringValue("discrete"), false);
1471 ////////////////////////////////////////////////////////////////////////
1473 ////////////////////////////////////////////////////////////////////////
1477 * Table of built-in commands.
1479 * New commands do not have to be added here; any module in the application
1480 * can add a new command using globals->get_commands()->addCommand(...).
1484 SGCommandMgr::command_t command;
1486 { "null", do_null },
1487 { "nasal", do_nasal },
1488 { "exit", do_exit },
1489 { "reset", do_reset },
1490 { "reinit", do_reinit },
1491 { "suspend", do_reinit },
1492 { "resume", do_reinit },
1493 { "load", do_load },
1494 { "save", do_save },
1495 { "panel-load", do_panel_load },
1496 { "panel-mouse-click", do_panel_mouse_click },
1497 { "preferences-load", do_preferences_load },
1498 { "view-cycle", do_view_cycle },
1499 { "screen-capture", do_screen_capture },
1500 { "hires-screen-capture", do_hires_screen_capture },
1501 { "tile-cache-reload", do_tile_cache_reload },
1502 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1503 { "set-outside-air-temp-degc", do_set_oat_degc },
1504 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1505 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1506 { "timeofday", do_timeofday },
1507 { "property-toggle", do_property_toggle },
1508 { "property-assign", do_property_assign },
1509 { "property-adjust", do_property_adjust },
1510 { "property-multiply", do_property_multiply },
1511 { "property-swap", do_property_swap },
1512 { "property-scale", do_property_scale },
1513 { "property-cycle", do_property_cycle },
1514 { "property-randomize", do_property_randomize },
1515 { "data-logging-commit", do_data_logging_commit },
1516 { "dialog-new", do_dialog_new },
1517 { "dialog-show", do_dialog_show },
1518 { "dialog-close", do_dialog_close },
1519 { "dialog-update", do_dialog_update },
1520 { "dialog-apply", do_dialog_apply },
1521 { "gui-redraw", do_gui_redraw },
1522 { "set-cursor", do_set_cursor },
1523 { "play-audio-sample", do_play_audio_sample },
1524 { "presets-commit", do_presets_commit },
1525 { "log-level", do_log_level },
1526 { "replay", do_replay },
1527 { "decrease-visibility", do_decrease_visibility },
1528 { "increase-visibility", do_increase_visibility },
1529 { "hud-init", do_hud_init },
1530 { "hud-init2", do_hud_init2 },
1531 { "loadxml", do_load_xml_to_proptree},
1532 { "savexml", do_save_xml_from_proptree },
1533 { "press-cockpit-button", do_press_cockpit_button },
1534 { "release-cockpit-button", do_release_cockpit_button },
1535 { "dump-scenegraph", do_dump_scene_graph },
1536 { "dump-terrainbranch", do_dump_terrain_branch },
1537 { 0, 0 } // zero-terminated
1542 * Initialize the default built-in commands.
1544 * Other commands may be added by other parts of the application.
1549 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1550 for (int i = 0; built_ins[i].name != 0; i++) {
1551 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1552 globals->get_commands()->addCommand(built_ins[i].name,
1553 built_ins[i].command);
1556 typedef bool (*dummy)();
1557 fgTie( "/command/view/next", dummy(0), do_view_next );
1558 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1561 // end of fg_commands.cxx