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 #if defined(HAVE_PLIB_PSL)
34 # include <Scripting/scriptmgr.hxx>
36 #include <Scripting/NasalSys.hxx>
37 #include <Sound/fg_fx.hxx>
38 #include <Time/sunsolver.hxx>
39 #include <Time/tmp.hxx>
41 #include "fg_init.hxx"
43 #include "fg_commands.hxx"
44 #include "fg_props.hxx"
45 #include "globals.hxx"
48 #include "viewmgr.hxx"
52 SG_USING_STD(ifstream);
53 SG_USING_STD(ofstream);
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 SGPropertyNode::BOOL:
139 return (value1->getBoolValue() == value2->getBoolValue());
140 case SGPropertyNode::INT:
141 return (value1->getIntValue() == value2->getIntValue());
142 case SGPropertyNode::LONG:
143 return (value1->getLongValue() == value2->getLongValue());
144 case SGPropertyNode::FLOAT:
145 return (value1->getFloatValue() == value2->getFloatValue());
146 case SGPropertyNode::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)
169 #if defined(HAVE_PLIB_PSL)
171 * Built-in command: run a PSL script.
173 * script: the PSL script to execute
176 do_script (const SGPropertyNode * arg)
178 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
179 return mgr->run(arg->getStringValue("script"));
181 #endif // HAVE_PLIB_PSL
184 * Built-in command: run a Nasal script.
187 do_nasal (const SGPropertyNode * arg)
189 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
193 * Built-in command: exit FlightGear.
195 * status: the exit status to return to the operating system (defaults to 0)
198 do_exit (const SGPropertyNode * arg)
200 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
201 fgSetBool("/sim/signals/exit", true);
203 if (fgGetBool("/sim/startup/save-on-exit")) {
205 char* envp = ::getenv( "APPDATA" );
206 if ( envp != NULL ) {
207 SGPath config( envp );
208 config.append( "flightgear.org" );
210 if ( homedir != NULL ) {
211 SGPath config( homedir );
212 config.append( ".fgfs" );
214 config.append( "autosave.xml" );
215 config.create_dir( 0700 );
216 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
218 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
219 } catch (const sg_exception &e) {
220 guiErrorMessage("Error writing autosave.xml: ", e);
223 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
226 fgExit(arg->getIntValue("status", 0));
232 * Built-in command: reinitialize one or more subsystems.
234 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
235 * none is specified, reinitialize all of them.
238 do_reinit (const SGPropertyNode * arg)
242 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
243 if (subsystems.size() == 0) {
244 globals->get_subsystem_mgr()->reinit();
246 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
247 const char * name = subsystems[i]->getStringValue();
248 SGSubsystem * subsystem = globals->get_subsystem(name);
249 if (subsystem == 0) {
251 SG_LOG( SG_GENERAL, SG_ALERT,
252 "Subsystem " << name << "not found" );
259 globals->get_event_mgr()->reinit();
266 // these routines look useful ??? but are never used in the code ???
270 * Built-in command: suspend one or more subsystems.
272 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
275 do_suspend (const SGPropertyNode * arg)
279 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
280 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
281 const char * name = subsystems[i]->getStringValue();
282 SGSubsystem * subsystem = globals->get_subsystem(name);
283 if (subsystem == 0) {
285 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
287 subsystem->suspend();
294 * Built-in command: suspend one or more subsystems.
296 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
299 do_resume (const SGPropertyNode * arg)
303 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
304 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
305 const char * name = subsystems[i]->getStringValue();
306 SGSubsystem * subsystem = globals->get_subsystem(name);
307 if (subsystem == 0) {
309 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
321 * Built-in command: load flight.
323 * file (optional): the name of the file to load (relative to current
324 * directory). Defaults to "fgfs.sav"
327 do_load (const SGPropertyNode * arg)
329 const string &file = arg->getStringValue("file", "fgfs.sav");
330 ifstream input(file.c_str());
331 if (input.good() && fgLoadFlight(input)) {
333 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
336 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
343 * Built-in command: save flight.
345 * file (optional): the name of the file to save (relative to the
346 * current directory). Defaults to "fgfs.sav".
349 do_save (const SGPropertyNode * arg)
351 const string &file = arg->getStringValue("file", "fgfs.sav");
352 bool write_all = arg->getBoolValue("write-all", false);
353 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
354 ofstream output(file.c_str());
355 if (output.good() && fgSaveFlight(output, write_all)) {
357 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
360 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
367 * Built-in command: (re)load the panel.
369 * path (optional): the file name to load the panel from
370 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
371 * and if that's unspecified, to "Panels/Default/default.xml".
374 do_panel_load (const SGPropertyNode * arg)
377 arg->getStringValue("path",
378 fgGetString("/sim/panel/path",
379 "Panels/Default/default.xml"));
380 FGPanel * new_panel = fgReadPanel(panel_path);
381 if (new_panel == 0) {
382 SG_LOG(SG_INPUT, SG_ALERT,
383 "Error reading new panel from " << panel_path);
386 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
387 globals->get_current_panel()->unbind();
388 delete globals->get_current_panel();
389 globals->set_current_panel( new_panel );
390 globals->get_current_panel()->bind();
396 * Built-in command: pass a mouse click to the panel.
398 * button: the mouse button number, zero-based.
399 * is-down: true if the button is down, false if it is up.
400 * x-pos: the x position of the mouse click.
401 * y-pos: the y position of the mouse click.
404 do_panel_mouse_click (const SGPropertyNode * arg)
406 if (globals->get_current_panel() != 0)
407 return globals->get_current_panel()
408 ->doMouseAction(arg->getIntValue("button"),
409 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
410 arg->getIntValue("x-pos"),
411 arg->getIntValue("y-pos"));
418 * Built-in command: (re)load preferences.
420 * path (optional): the file name to load the panel from (relative
421 * to FG_ROOT). Defaults to "preferences.xml".
424 do_preferences_load (const SGPropertyNode * arg)
427 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
428 globals->get_props());
429 } catch (const sg_exception &e) {
430 guiErrorMessage("Error reading global preferences: ", e);
433 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
441 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
442 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
443 if ( globals->get_viewmgr()->get_current() == 1 ) {
444 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
452 globals->get_current_view()->setHeadingOffset_deg(0.0);
453 globals->get_viewmgr()->next_view();
454 fix_hud_visibility();
455 globals->get_tile_mgr()->refresh_view_timestamps();
461 globals->get_current_view()->setHeadingOffset_deg(0.0);
462 globals->get_viewmgr()->prev_view();
463 fix_hud_visibility();
464 globals->get_tile_mgr()->refresh_view_timestamps();
468 * Built-in command: cycle view.
471 do_view_cycle (const SGPropertyNode * arg)
473 globals->get_current_view()->setHeadingOffset_deg(0.0);
474 globals->get_viewmgr()->next_view();
475 fix_hud_visibility();
476 globals->get_tile_mgr()->refresh_view_timestamps();
481 * Built-in command: capture screen.
484 do_screen_capture (const SGPropertyNode * arg)
491 do_dump_scene_graph (const SGPropertyNode*)
498 * Built-in command: hires capture screen.
501 do_hires_screen_capture (const SGPropertyNode * arg)
509 * Reload the tile cache.
512 do_tile_cache_reload (const SGPropertyNode * arg)
514 static const SGPropertyNode *master_freeze
515 = fgGetNode("/sim/freeze/master");
516 bool freeze = master_freeze->getBoolValue();
517 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
519 fgSetBool("/sim/freeze/master", true);
521 if ( globals->get_tile_mgr()->init() ) {
522 // Load the local scenery data
523 double visibility_meters = fgGetDouble("/environment/visibility-m");
524 globals->get_tile_mgr()->update( visibility_meters );
526 SG_LOG( SG_GENERAL, SG_ALERT,
527 "Error in Tile Manager initialization!" );
531 fgSetBool("/sim/freeze/master", false);
538 * Set the sea level outside air temperature and assigning that to all
539 * boundary and aloft environment layers.
542 do_set_sea_level_degc (const SGPropertyNode * arg)
544 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
546 SGPropertyNode *node, *child;
549 node = fgGetNode( "/environment/config/boundary" );
550 if ( node != NULL ) {
552 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
553 child->setDoubleValue( "temperature-sea-level-degc",
554 temp_sea_level_degc );
560 node = fgGetNode( "/environment/config/aloft" );
561 if ( node != NULL ) {
563 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
564 child->setDoubleValue( "temperature-sea-level-degc",
565 temp_sea_level_degc );
575 * Set the outside air temperature at the "current" altitude by first
576 * calculating the corresponding sea level temp, and assigning that to
577 * all boundary and aloft environment layers.
580 do_set_oat_degc (const SGPropertyNode * arg)
582 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
584 // check for an altitude specified in the arguments, otherwise use
585 // current aircraft altitude.
586 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
587 if ( altitude_ft == NULL ) {
588 altitude_ft = fgGetNode("/position/altitude-ft");
591 FGEnvironment dummy; // instantiate a dummy so we can leech a method
592 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
593 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
594 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
596 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
597 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
598 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
600 SGPropertyNode *node, *child;
603 node = fgGetNode( "/environment/config/boundary" );
604 if ( node != NULL ) {
606 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
607 child->setDoubleValue( "temperature-sea-level-degc",
608 temp_sea_level_degc );
614 node = fgGetNode( "/environment/config/aloft" );
615 if ( node != NULL ) {
617 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
618 child->setDoubleValue( "temperature-sea-level-degc",
619 temp_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 (const SGPropertyNode * arg)
634 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
636 SGPropertyNode *node, *child;
639 node = fgGetNode( "/environment/config/boundary" );
640 if ( node != NULL ) {
642 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
643 child->setDoubleValue( "dewpoint-sea-level-degc",
644 dewpoint_sea_level_degc );
650 node = fgGetNode( "/environment/config/aloft" );
651 if ( node != NULL ) {
653 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
654 child->setDoubleValue( "dewpoint-sea-level-degc",
655 dewpoint_sea_level_degc );
665 * Set the outside air dewpoint at the "current" altitude by first
666 * calculating the corresponding sea level dewpoint, and assigning
667 * that to all boundary and aloft environment layers.
670 do_set_dewpoint_degc (const SGPropertyNode * arg)
672 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
674 // check for an altitude specified in the arguments, otherwise use
675 // current aircraft altitude.
676 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
677 if ( altitude_ft == NULL ) {
678 altitude_ft = fgGetNode("/position/altitude-ft");
681 FGEnvironment dummy; // instantiate a dummy so we can leech a method
682 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
683 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
684 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
686 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
687 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
688 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
690 SGPropertyNode *node, *child;
693 node = fgGetNode( "/environment/config/boundary" );
694 if ( node != NULL ) {
696 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
697 child->setDoubleValue( "dewpoint-sea-level-degc",
698 dewpoint_sea_level_degc );
704 node = fgGetNode( "/environment/config/aloft" );
705 if ( node != NULL ) {
707 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
708 child->setDoubleValue( "dewpoint-sea-level-degc",
709 dewpoint_sea_level_degc );
718 * Update the lighting manually.
721 do_timeofday (const SGPropertyNode * arg)
723 const string &offset_type = arg->getStringValue("timeofday", "noon");
725 static const SGPropertyNode *longitude
726 = fgGetNode("/position/longitude-deg");
727 static const SGPropertyNode *latitude
728 = fgGetNode("/position/latitude-deg");
729 static const SGPropertyNode *cur_time_override
730 = fgGetNode("/sim/time/cur-time-override", true);
732 int orig_warp = globals->get_warp();
733 SGTime *t = globals->get_time_params();
734 time_t cur_time = t->get_cur_time();
735 // cout << "cur_time = " << cur_time << endl;
736 // cout << "orig_warp = " << orig_warp << endl;
739 if ( offset_type == "real" ) {
741 } else if ( offset_type == "dawn" ) {
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 == "morning" ) {
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 == "noon" ) {
756 warp = fgTimeSecondsUntilSunAngle( cur_time,
757 longitude->getDoubleValue()
758 * SGD_DEGREES_TO_RADIANS,
759 latitude->getDoubleValue()
760 * SGD_DEGREES_TO_RADIANS,
762 } else if ( offset_type == "afternoon" ) {
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 == "dusk" ) {
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 == "evening" ) {
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 == "midnight" ) {
784 warp = fgTimeSecondsUntilSunAngle( cur_time,
785 longitude->getDoubleValue()
786 * SGD_DEGREES_TO_RADIANS,
787 latitude->getDoubleValue()
788 * SGD_DEGREES_TO_RADIANS,
791 // cout << "warp = " << warp << endl;
792 globals->set_warp( orig_warp + warp );
794 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
795 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
796 cur_time_override->getLongValue(),
797 globals->get_warp() );
804 * Built-in command: toggle a bool property value.
806 * property: The name of the property to toggle.
809 do_property_toggle (const SGPropertyNode * arg)
811 SGPropertyNode * prop = get_prop(arg);
812 return prop->setBoolValue(!prop->getBoolValue());
817 * Built-in command: assign a value to a property.
819 * property: the name of the property to assign.
820 * value: the value to assign; or
821 * property[1]: the property to copy from.
824 do_property_assign (const SGPropertyNode * arg)
826 SGPropertyNode * prop = get_prop(arg);
827 const SGPropertyNode * prop2 = get_prop2(arg);
828 const SGPropertyNode * value = arg->getNode("value");
831 return prop->setUnspecifiedValue(value->getStringValue());
833 return prop->setUnspecifiedValue(prop2->getStringValue());
840 * Built-in command: increment or decrement a property value.
842 * If the 'step' argument is present, it will be used; otherwise,
843 * the command uses 'offset' and 'factor', usually from the mouse.
845 * property: the name of the property to increment or decrement.
846 * step: the amount of the increment or decrement (default: 0).
847 * offset: offset from the current setting (used for the mouse; multiplied
849 * factor: scaling amount for the offset (defaults to 1).
850 * min: the minimum allowed value (default: no minimum).
851 * max: the maximum allowed value (default: no maximum).
852 * mask: 'integer' to apply only to the left of the decimal point,
853 * 'decimal' to apply only to the right of the decimal point,
854 * or 'all' to apply to the whole number (the default).
855 * wrap: true if the value should be wrapped when it passes min or max;
856 * both min and max must be present for this to work (default:
860 do_property_adjust (const SGPropertyNode * arg)
862 SGPropertyNode * prop = get_prop(arg);
865 if (arg->hasValue("step"))
866 amount = arg->getDoubleValue("step");
868 amount = (arg->getDoubleValue("factor", 1)
869 * arg->getDoubleValue("offset"));
871 double unmodifiable, modifiable;
872 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
873 &unmodifiable, &modifiable);
874 modifiable += amount;
875 limit_value(&modifiable, arg);
877 prop->setDoubleValue(unmodifiable + modifiable);
884 * Built-in command: multiply a property value.
886 * property: the name of the property to multiply.
887 * factor: the amount by which to multiply.
888 * min: the minimum allowed value (default: no minimum).
889 * max: the maximum allowed value (default: no maximum).
890 * mask: 'integer' to apply only to the left of the decimal point,
891 * 'decimal' to apply only to the right of the decimal point,
892 * or 'all' to apply to the whole number (the default).
893 * wrap: true if the value should be wrapped when it passes min or max;
894 * both min and max must be present for this to work (default:
898 do_property_multiply (const SGPropertyNode * arg)
900 SGPropertyNode * prop = get_prop(arg);
901 double factor = arg->getDoubleValue("factor", 1);
903 double unmodifiable, modifiable;
904 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
905 &unmodifiable, &modifiable);
906 modifiable *= factor;
907 limit_value(&modifiable, arg);
909 prop->setDoubleValue(unmodifiable + modifiable);
916 * Built-in command: swap two property values.
918 * property[0]: the name of the first property.
919 * property[1]: the name of the second property.
922 do_property_swap (const SGPropertyNode * arg)
924 SGPropertyNode * prop1 = get_prop(arg);
925 SGPropertyNode * prop2 = get_prop2(arg);
927 // FIXME: inefficient
928 const string & tmp = prop1->getStringValue();
929 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
930 prop2->setUnspecifiedValue(tmp.c_str()));
935 * Built-in command: Set a property to an axis or other moving input.
937 * property: the name of the property to set.
938 * setting: the current input setting, usually between -1.0 and 1.0.
939 * offset: the offset to shift by, before applying the factor.
940 * factor: the factor to multiply by (use negative to reverse).
943 do_property_scale (const SGPropertyNode * arg)
945 SGPropertyNode * prop = get_prop(arg);
946 double setting = arg->getDoubleValue("setting");
947 double offset = arg->getDoubleValue("offset", 0.0);
948 double factor = arg->getDoubleValue("factor", 1.0);
949 bool squared = arg->getBoolValue("squared", false);
950 int power = arg->getIntValue("power", (squared ? 2 : 1));
952 int sign = (setting < 0 ? -1 : 1);
958 setting = setting * setting * sign;
961 setting = setting * setting * setting;
964 setting = setting * setting * setting * setting * sign;
967 setting = pow(setting, power);
968 if ((power % 2) == 0)
973 return prop->setDoubleValue((setting + offset) * factor);
978 * Built-in command: cycle a property through a set of values.
980 * If the current value isn't in the list, the cycle will
981 * (re)start from the beginning.
983 * property: the name of the property to cycle.
984 * value[*]: the list of values to cycle through.
987 do_property_cycle (const SGPropertyNode * arg)
989 SGPropertyNode * prop = get_prop(arg);
990 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
992 int nSelections = values.size();
994 if (nSelections < 1) {
995 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
999 // Try to find the current selection
1000 for (int i = 0; i < nSelections; i++) {
1001 if (compare_values(prop, values[i])) {
1007 // Default or wrap to the first selection
1008 if (selection < 0 || selection >= nSelections)
1011 prop->setUnspecifiedValue(values[selection]->getStringValue());
1017 * Built-in command: randomize a numeric property value.
1019 * property: the name of the property value to randomize.
1020 * min: the minimum allowed value.
1021 * max: the maximum allowed value.
1024 do_property_randomize (const SGPropertyNode * arg)
1026 SGPropertyNode * prop = get_prop(arg);
1027 double min = arg->getDoubleValue("min", DBL_MIN);
1028 double max = arg->getDoubleValue("max", DBL_MAX);
1029 prop->setDoubleValue(sg_random() * (max - min) + min);
1035 * Built-in command: reinit the data logging system based on the
1036 * current contents of the /logger tree.
1039 do_data_logging_commit (const SGPropertyNode * arg)
1041 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1047 * Built-in command: Add a dialog to the GUI system. Does *not*
1048 * display the dialog. The property node should have the same format
1049 * as a dialog XML configuration. It must include:
1051 * name: the name of the GUI dialog for future reference.
1054 do_dialog_new (const SGPropertyNode * arg)
1056 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1058 // Note the casting away of const: this is *real*. Doing a
1059 // "dialog-apply" command later on will mutate this property node.
1060 // I'm not convinced that this isn't the Right Thing though; it
1061 // allows client to create a node, pass it to dialog-new, and get
1062 // the values back from the dialog by reading the same node.
1063 // Perhaps command arguments are not as "const" as they would
1065 gui->newDialog((SGPropertyNode*)arg);
1070 * Built-in command: Show an XML-configured dialog.
1072 * dialog-name: the name of the GUI dialog to display.
1075 do_dialog_show (const SGPropertyNode * arg)
1077 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1078 gui->showDialog(arg->getStringValue("dialog-name"));
1084 * Built-in Command: Hide the active XML-configured dialog.
1087 do_dialog_close (const SGPropertyNode * arg)
1089 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1090 if(arg->hasValue("dialog-name"))
1091 return gui->closeDialog(arg->getStringValue("dialog-name"));
1092 return gui->closeActiveDialog();
1097 * Update a value in the active XML-configured dialog.
1099 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1102 do_dialog_update (const SGPropertyNode * arg)
1104 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1106 if (arg->hasValue("dialog-name"))
1107 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1109 dialog = gui->getActiveDialog();
1112 dialog->updateValues(arg->getStringValue("object-name"));
1121 * Apply a value in the active XML-configured dialog.
1123 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1126 do_dialog_apply (const SGPropertyNode * arg)
1128 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1130 if (arg->hasValue("dialog-name"))
1131 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1133 dialog = gui->getActiveDialog();
1136 dialog->applyValues(arg->getStringValue("object-name"));
1145 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1149 do_gui_redraw (const SGPropertyNode * arg)
1151 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1157 * Built-in command: play an audio message (i.e. a wav file) This is
1158 * fire and forget. Call this once per message and it will get dumped
1159 * into a queue. Messages are played sequentially so they do not
1163 do_play_audio_sample (const SGPropertyNode * arg)
1165 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1166 string path = arg->getStringValue("path");
1167 string file = arg->getStringValue("file");
1168 double volume = arg->getDoubleValue("volume");
1169 // cout << "playing " << path << " / " << file << endl;
1171 fx->play_message( path, file, volume );
1174 } catch (const sg_io_exception& e) {
1175 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1176 "failed to load" << path << '/' << file);
1182 * Built-in command: commit presets (read from in /sim/presets/)
1185 do_presets_commit (const SGPropertyNode * arg)
1187 // unbind the current fdm state so property changes
1188 // don't get lost when we subsequently delete this fdm
1189 // and create a new one.
1190 cur_fdm_state->unbind();
1192 // set position from presets
1195 fgReInitSubsystems();
1197 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1200 if ( ! fgGetBool("/sim/presets/onground") ) {
1201 fgSetBool( "/sim/freeze/master", true );
1202 fgSetBool( "/sim/freeze/clock", true );
1210 * Built-in command: set log level (0 ... 7)
1213 do_log_level (const SGPropertyNode * arg)
1215 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1221 * Built-in command: replay the FDR buffer
1224 do_replay (const SGPropertyNode * arg)
1226 // freeze the master fdm
1227 fgSetInt( "/sim/freeze/replay-state", 1 );
1229 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1231 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1232 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1233 double duration = fgGetDouble( "/sim/replay/duration" );
1234 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1235 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1237 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1240 // cout << "start = " << r->get_start_time()
1241 // << " end = " << r->get_end_time() << endl;
1248 * Return terrain elevation for given longitude/latitude pair.
1251 do_terrain_elevation (const SGPropertyNode * arg)
1253 SGPropertyNode *a = const_cast<SGPropertyNode *>(arg);
1254 const SGMaterial *mat;
1257 double lon = a->getDoubleValue("longitude-deg", 0.0);
1258 double lat = a->getDoubleValue("latitude-deg", 0.0);
1259 bool ret = globals->get_scenery()->get_elevation_m(lat, lon, 10000.0, elev, &mat);
1262 const char *matname = "";
1264 solid = mat->get_solid();
1265 const vector<string> names = mat->get_names();
1267 matname = names[0].c_str();
1269 a->setBoolValue("solid", solid);
1270 a->setStringValue("material", matname);
1271 a->setDoubleValue("elevation-m", elev);
1277 do_decrease_visibility (const SGPropertyNode * arg)
1279 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1280 fgSetDouble("/environment/visibility-m", new_value);
1281 fgDefaultWeatherValue("visibility-m", new_value);
1282 globals->get_subsystem("environment")->reinit();
1288 do_increase_visibility (const SGPropertyNode * arg)
1290 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1291 fgSetDouble("/environment/visibility-m", new_value);
1292 fgDefaultWeatherValue("visibility-m", new_value);
1293 globals->get_subsystem("environment")->reinit();
1299 do_hud_init(const SGPropertyNode *)
1301 fgHUDInit(0); // minimal HUD
1306 do_hud_init2(const SGPropertyNode *)
1308 fgHUDInit2(0); // normal HUD
1313 * An fgcommand to allow loading of xml files via nasal,
1314 * the xml file's structure will be made available within
1315 * a property tree node defined under argument "targetnode",
1316 * or in the given argument tree under "data" otherwise.
1318 * @param filename a string to hold the complete path & filename of a XML file
1319 * @param targetnode a string pointing to a location within the property tree
1320 * where to store the parsed XML file
1324 do_load_xml_to_proptree(const SGPropertyNode * arg)
1326 SGPropertyNode *a = const_cast<SGPropertyNode *>(arg);
1327 SGPropertyNode *targetnode;
1328 if (a->hasValue("targetnode"))
1329 targetnode = fgGetNode(a->getStringValue("targetnode"), true);
1331 targetnode = a->getNode("data", true);
1333 const char *filename = a->getNode("filename")->getStringValue();
1335 fgLoadProps(filename, targetnode);
1336 } catch (const sg_exception &e) {
1337 string errmsg = "Error reading file " + string(filename) + ":\n";
1338 guiErrorMessage(errmsg.c_str(), e);
1347 * an fgcommand to allow saving of xml files via nasal,
1348 * the file's structure will be determined based on what's
1349 * encountered in the passed (source) property tree node
1351 * @param filename a string to hold the complete path & filename of the (new)
1353 * @param sourcenode a string pointing to a location within the property tree
1354 * where to find the nodes that should be written recursively into an XML file
1355 * @param data if no sourcenode is given, then the file contents are taken from
1356 * the argument tree's "data" node.
1359 * deal with already existing filenames, optionally return error/success
1360 * values in a separate node to provide status information
1362 * note: it's directly using writeProperties, which is not necessarily
1363 * preferable, but for now it should work ...
1367 do_save_xml_from_proptree(const SGPropertyNode * node)
1369 SGPropertyNode * sourcenode;
1370 if (node->hasValue("sourcenode"))
1371 sourcenode = fgGetNode(node->getStringValue("sourcenode"), true);
1372 else if (node->getNode("data", false))
1373 sourcenode = const_cast<SGPropertyNode *>(node)->getNode("data");
1377 const char *filename = node->getStringValue("filename", 0);
1382 writeProperties (filename, sourcenode, true);
1383 } catch (const sg_exception &e) {
1384 string errmsg = "Error writing file " + string(filename) + ":\n";
1385 guiErrorMessage(errmsg.c_str(), e);
1393 do_press_cockpit_button (const SGPropertyNode *arg)
1395 const char *prefix = arg->getStringValue("prefix");
1397 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1400 string prop = string(prefix) + "-button";
1403 if (arg->getBoolValue("latching"))
1404 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1408 fgSetDouble(prop.c_str(), value);
1409 fgSetBool(arg->getStringValue("discrete"), value > 0);
1415 do_release_cockpit_button (const SGPropertyNode *arg)
1417 const char *prefix = arg->getStringValue("prefix");
1419 if (arg->getBoolValue("guarded")) {
1420 string prop = string(prefix) + "-guard";
1421 if (fgGetDouble(prop.c_str()) < 1) {
1422 fgSetDouble(prop.c_str(), 1);
1427 if (! arg->getBoolValue("latching")) {
1428 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1429 fgSetBool(arg->getStringValue("discrete"), false);
1436 ////////////////////////////////////////////////////////////////////////
1438 ////////////////////////////////////////////////////////////////////////
1442 * Table of built-in commands.
1444 * New commands do not have to be added here; any module in the application
1445 * can add a new command using globals->get_commands()->addCommand(...).
1449 SGCommandMgr::command_t command;
1451 { "null", do_null },
1452 #if defined(HAVE_PLIB_PSL)
1453 { "script", do_script },
1454 #endif // HAVE_PLIB_PSL
1455 { "nasal", do_nasal },
1456 { "exit", do_exit },
1457 { "reinit", do_reinit },
1458 { "suspend", do_reinit },
1459 { "resume", do_reinit },
1460 { "load", do_load },
1461 { "save", do_save },
1462 { "panel-load", do_panel_load },
1463 { "panel-mouse-click", do_panel_mouse_click },
1464 { "preferences-load", do_preferences_load },
1465 { "view-cycle", do_view_cycle },
1466 { "screen-capture", do_screen_capture },
1467 { "hires-screen-capture", do_hires_screen_capture },
1468 { "tile-cache-reload", do_tile_cache_reload },
1469 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1470 { "set-outside-air-temp-degc", do_set_oat_degc },
1471 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1472 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1473 { "timeofday", do_timeofday },
1474 { "property-toggle", do_property_toggle },
1475 { "property-assign", do_property_assign },
1476 { "property-adjust", do_property_adjust },
1477 { "property-multiply", do_property_multiply },
1478 { "property-swap", do_property_swap },
1479 { "property-scale", do_property_scale },
1480 { "property-cycle", do_property_cycle },
1481 { "property-randomize", do_property_randomize },
1482 { "data-logging-commit", do_data_logging_commit },
1483 { "dialog-new", do_dialog_new },
1484 { "dialog-show", do_dialog_show },
1485 { "dialog-close", do_dialog_close },
1486 { "dialog-update", do_dialog_update },
1487 { "dialog-apply", do_dialog_apply },
1488 { "gui-redraw", do_gui_redraw },
1489 { "play-audio-sample", do_play_audio_sample },
1490 { "presets-commit", do_presets_commit },
1491 { "log-level", do_log_level },
1492 { "replay", do_replay },
1493 { "terrain-elevation", do_terrain_elevation },
1494 { "decrease-visibility", do_decrease_visibility },
1495 { "increase-visibility", do_increase_visibility },
1496 { "hud-init", do_hud_init },
1497 { "hud-init2", do_hud_init2 },
1498 { "loadxml", do_load_xml_to_proptree},
1499 { "savexml", do_save_xml_from_proptree },
1500 { "press-cockpit-button", do_press_cockpit_button },
1501 { "release-cockpit-button", do_release_cockpit_button },
1502 { "dump-scenegraph", do_dump_scene_graph },
1503 { 0, 0 } // zero-terminated
1508 * Initialize the default built-in commands.
1510 * Other commands may be added by other parts of the application.
1515 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1516 for (int i = 0; built_ins[i].name != 0; i++) {
1517 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1518 globals->get_commands()->addCommand(built_ins[i].name,
1519 built_ins[i].command);
1522 typedef bool (*dummy)();
1523 fgTie( "/command/view/next", dummy(0), do_view_next );
1524 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1527 // end of fg_commands.cxx