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/structure/exception.hxx>
18 #include <simgear/structure/commands.hxx>
19 #include <simgear/props/props.hxx>
21 #include <Cockpit/panel.hxx>
22 #include <Cockpit/panel_io.hxx>
23 #include <Cockpit/hud.hxx>
24 #include <Environment/environment.hxx>
25 #include <FDM/flight.hxx>
27 #include <GUI/new_gui.hxx>
28 #include <GUI/dialog.hxx>
29 #include <Aircraft/replay.hxx>
30 #include <Scenery/tilemgr.hxx>
31 #include <Scenery/scenery.hxx>
32 #if defined(HAVE_PLIB_PSL)
33 # include <Scripting/scriptmgr.hxx>
35 #include <Scripting/NasalSys.hxx>
36 #include <Sound/fg_fx.hxx>
37 #include <Time/sunsolver.hxx>
38 #include <Time/tmp.hxx>
40 #include "fg_init.hxx"
42 #include "fg_commands.hxx"
43 #include "fg_props.hxx"
44 #include "globals.hxx"
47 #include "viewmgr.hxx"
51 SG_USING_STD(ifstream);
52 SG_USING_STD(ofstream);
56 ////////////////////////////////////////////////////////////////////////
57 // Static helper functions.
58 ////////////////////////////////////////////////////////////////////////
61 static inline SGPropertyNode *
62 get_prop (const SGPropertyNode * arg)
64 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
67 static inline SGPropertyNode *
68 get_prop2 (const SGPropertyNode * arg)
70 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
75 * Get a double value and split it as required.
78 split_value (double full_value, const char * mask,
79 double * unmodifiable, double * modifiable)
81 if (!strcmp("integer", mask)) {
82 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
83 *unmodifiable = full_value - *modifiable;
84 } else if (!strcmp("decimal", mask)) {
85 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
86 *modifiable = full_value - *unmodifiable;
88 if (strcmp("all", mask))
89 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
90 << " assuming 'all'");
92 *modifiable = full_value;
98 * Clamp or wrap a value as specified.
101 limit_value (double * value, const SGPropertyNode * arg)
103 const SGPropertyNode * min_node = arg->getChild("min");
104 const SGPropertyNode * max_node = arg->getChild("max");
106 bool wrap = arg->getBoolValue("wrap");
108 if (min_node == 0 || max_node == 0)
111 if (wrap) { // wrap such that min <= x < max
112 double min_val = min_node->getDoubleValue();
113 double max_val = max_node->getDoubleValue();
114 double resolution = arg->getDoubleValue("resolution");
115 if (resolution > 0.0) {
116 // snap to (min + N*resolution), taking special care to handle imprecision
117 int n = (int)floor((*value - min_val) / resolution + 0.5);
118 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
119 SG_NORMALIZE_RANGE(n, 0, steps);
120 *value = min_val + resolution * n;
122 // plain circular wrapping
123 SG_NORMALIZE_RANGE(*value, min_val, max_val);
125 } else { // clamp such that min <= x <= max
126 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
127 *value = min_node->getDoubleValue();
128 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
129 *value = max_node->getDoubleValue();
134 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
136 switch (value1->getType()) {
137 case SGPropertyNode::BOOL:
138 return (value1->getBoolValue() == value2->getBoolValue());
139 case SGPropertyNode::INT:
140 return (value1->getIntValue() == value2->getIntValue());
141 case SGPropertyNode::LONG:
142 return (value1->getLongValue() == value2->getLongValue());
143 case SGPropertyNode::FLOAT:
144 return (value1->getFloatValue() == value2->getFloatValue());
145 case SGPropertyNode::DOUBLE:
146 return (value1->getDoubleValue() == value2->getDoubleValue());
148 return !strcmp(value1->getStringValue(), value2->getStringValue());
154 ////////////////////////////////////////////////////////////////////////
155 // Command implementations.
156 ////////////////////////////////////////////////////////////////////////
160 * Built-in command: do nothing.
163 do_null (const SGPropertyNode * arg)
168 #if defined(HAVE_PLIB_PSL)
170 * Built-in command: run a PSL script.
172 * script: the PSL script to execute
175 do_script (const SGPropertyNode * arg)
177 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
178 return mgr->run(arg->getStringValue("script"));
180 #endif // HAVE_PLIB_PSL
183 * Built-in command: run a Nasal script.
186 do_nasal (const SGPropertyNode * arg)
188 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
192 * Built-in command: exit FlightGear.
194 * status: the exit status to return to the operating system (defaults to 0)
197 do_exit (const SGPropertyNode * arg)
199 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
200 fgSetBool("/sim/signals/exit", true);
202 if (fgGetBool("/sim/startup/save-on-exit")) {
204 char* envp = ::getenv( "APPDATA" );
205 if ( envp != NULL ) {
206 SGPath config( envp );
207 config.append( "flightgear.org" );
209 if ( homedir != NULL ) {
210 SGPath config( homedir );
211 config.append( ".fgfs" );
213 config.append( "autosave.xml" );
214 config.create_dir( 0700 );
215 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
217 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
218 } catch (const sg_exception &e) {
219 guiErrorMessage("Error writing autosave.xml: ", e);
222 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
225 fgExit(arg->getIntValue("status", 0));
231 * Built-in command: reinitialize one or more subsystems.
233 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
234 * none is specified, reinitialize all of them.
237 do_reinit (const SGPropertyNode * arg)
241 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
242 if (subsystems.size() == 0) {
243 globals->get_subsystem_mgr()->reinit();
245 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
246 const char * name = subsystems[i]->getStringValue();
247 SGSubsystem * subsystem = globals->get_subsystem(name);
248 if (subsystem == 0) {
250 SG_LOG( SG_GENERAL, SG_ALERT,
251 "Subsystem " << name << "not found" );
258 globals->get_event_mgr()->reinit();
265 // these routines look useful ??? but are never used in the code ???
269 * Built-in command: suspend one or more subsystems.
271 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
274 do_suspend (const SGPropertyNode * arg)
278 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
279 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
280 const char * name = subsystems[i]->getStringValue();
281 SGSubsystem * subsystem = globals->get_subsystem(name);
282 if (subsystem == 0) {
284 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
286 subsystem->suspend();
293 * Built-in command: suspend one or more subsystems.
295 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
298 do_resume (const SGPropertyNode * arg)
302 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
303 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
304 const char * name = subsystems[i]->getStringValue();
305 SGSubsystem * subsystem = globals->get_subsystem(name);
306 if (subsystem == 0) {
308 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
320 * Built-in command: load flight.
322 * file (optional): the name of the file to load (relative to current
323 * directory). Defaults to "fgfs.sav"
326 do_load (const SGPropertyNode * arg)
328 const string &file = arg->getStringValue("file", "fgfs.sav");
329 ifstream input(file.c_str());
330 if (input.good() && fgLoadFlight(input)) {
332 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
335 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
342 * Built-in command: save flight.
344 * file (optional): the name of the file to save (relative to the
345 * current directory). Defaults to "fgfs.sav".
348 do_save (const SGPropertyNode * arg)
350 const string &file = arg->getStringValue("file", "fgfs.sav");
351 bool write_all = arg->getBoolValue("write-all", false);
352 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
353 ofstream output(file.c_str());
354 if (output.good() && fgSaveFlight(output, write_all)) {
356 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
359 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
366 * Built-in command: (re)load the panel.
368 * path (optional): the file name to load the panel from
369 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
370 * and if that's unspecified, to "Panels/Default/default.xml".
373 do_panel_load (const SGPropertyNode * arg)
376 arg->getStringValue("path",
377 fgGetString("/sim/panel/path",
378 "Panels/Default/default.xml"));
379 FGPanel * new_panel = fgReadPanel(panel_path);
380 if (new_panel == 0) {
381 SG_LOG(SG_INPUT, SG_ALERT,
382 "Error reading new panel from " << panel_path);
385 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
386 globals->get_current_panel()->unbind();
387 delete globals->get_current_panel();
388 globals->set_current_panel( new_panel );
389 globals->get_current_panel()->bind();
395 * Built-in command: pass a mouse click to the panel.
397 * button: the mouse button number, zero-based.
398 * is-down: true if the button is down, false if it is up.
399 * x-pos: the x position of the mouse click.
400 * y-pos: the y position of the mouse click.
403 do_panel_mouse_click (const SGPropertyNode * arg)
405 if (globals->get_current_panel() != 0)
406 return globals->get_current_panel()
407 ->doMouseAction(arg->getIntValue("button"),
408 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
409 arg->getIntValue("x-pos"),
410 arg->getIntValue("y-pos"));
417 * Built-in command: (re)load preferences.
419 * path (optional): the file name to load the panel from (relative
420 * to FG_ROOT). Defaults to "preferences.xml".
423 do_preferences_load (const SGPropertyNode * arg)
426 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
427 globals->get_props());
428 } catch (const sg_exception &e) {
429 guiErrorMessage("Error reading global preferences: ", e);
432 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
440 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
441 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
442 if ( globals->get_viewmgr()->get_current() == 1 ) {
443 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
451 globals->get_current_view()->setHeadingOffset_deg(0.0);
452 globals->get_viewmgr()->next_view();
453 fix_hud_visibility();
454 globals->get_tile_mgr()->refresh_view_timestamps();
460 globals->get_current_view()->setHeadingOffset_deg(0.0);
461 globals->get_viewmgr()->prev_view();
462 fix_hud_visibility();
463 globals->get_tile_mgr()->refresh_view_timestamps();
467 * Built-in command: cycle view.
470 do_view_cycle (const SGPropertyNode * arg)
472 globals->get_current_view()->setHeadingOffset_deg(0.0);
473 globals->get_viewmgr()->next_view();
474 fix_hud_visibility();
475 globals->get_tile_mgr()->refresh_view_timestamps();
480 * Built-in command: capture screen.
483 do_screen_capture (const SGPropertyNode * arg)
491 * Built-in command: hires capture screen.
494 do_hires_screen_capture (const SGPropertyNode * arg)
502 * Reload the tile cache.
505 do_tile_cache_reload (const SGPropertyNode * arg)
507 static const SGPropertyNode *master_freeze
508 = fgGetNode("/sim/freeze/master");
509 bool freeze = master_freeze->getBoolValue();
510 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
512 fgSetBool("/sim/freeze/master", true);
514 if ( globals->get_tile_mgr()->init() ) {
515 // Load the local scenery data
516 double visibility_meters = fgGetDouble("/environment/visibility-m");
517 globals->get_tile_mgr()->update( visibility_meters );
519 SG_LOG( SG_GENERAL, SG_ALERT,
520 "Error in Tile Manager initialization!" );
524 fgSetBool("/sim/freeze/master", false);
531 * Set the sea level outside air temperature and assigning that to all
532 * boundary and aloft environment layers.
535 do_set_sea_level_degc (const SGPropertyNode * arg)
537 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
539 SGPropertyNode *node, *child;
542 node = fgGetNode( "/environment/config/boundary" );
543 if ( node != NULL ) {
545 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
546 child->setDoubleValue( "temperature-sea-level-degc",
547 temp_sea_level_degc );
553 node = fgGetNode( "/environment/config/aloft" );
554 if ( node != NULL ) {
556 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
557 child->setDoubleValue( "temperature-sea-level-degc",
558 temp_sea_level_degc );
568 * Set the outside air temperature at the "current" altitude by first
569 * calculating the corresponding sea level temp, and assigning that to
570 * all boundary and aloft environment layers.
573 do_set_oat_degc (const SGPropertyNode * arg)
575 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
577 // check for an altitude specified in the arguments, otherwise use
578 // current aircraft altitude.
579 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
580 if ( altitude_ft == NULL ) {
581 altitude_ft = fgGetNode("/position/altitude-ft");
584 FGEnvironment dummy; // instantiate a dummy so we can leech a method
585 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
586 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
587 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
589 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
590 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
591 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
593 SGPropertyNode *node, *child;
596 node = fgGetNode( "/environment/config/boundary" );
597 if ( node != NULL ) {
599 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
600 child->setDoubleValue( "temperature-sea-level-degc",
601 temp_sea_level_degc );
607 node = fgGetNode( "/environment/config/aloft" );
608 if ( node != NULL ) {
610 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
611 child->setDoubleValue( "temperature-sea-level-degc",
612 temp_sea_level_degc );
621 * Set the sea level outside air dewpoint and assigning that to all
622 * boundary and aloft environment layers.
625 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
627 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
629 SGPropertyNode *node, *child;
632 node = fgGetNode( "/environment/config/boundary" );
633 if ( node != NULL ) {
635 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
636 child->setDoubleValue( "dewpoint-sea-level-degc",
637 dewpoint_sea_level_degc );
643 node = fgGetNode( "/environment/config/aloft" );
644 if ( node != NULL ) {
646 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
647 child->setDoubleValue( "dewpoint-sea-level-degc",
648 dewpoint_sea_level_degc );
658 * Set the outside air dewpoint at the "current" altitude by first
659 * calculating the corresponding sea level dewpoint, and assigning
660 * that to all boundary and aloft environment layers.
663 do_set_dewpoint_degc (const SGPropertyNode * arg)
665 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
667 // check for an altitude specified in the arguments, otherwise use
668 // current aircraft altitude.
669 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
670 if ( altitude_ft == NULL ) {
671 altitude_ft = fgGetNode("/position/altitude-ft");
674 FGEnvironment dummy; // instantiate a dummy so we can leech a method
675 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
676 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
677 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
679 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
680 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
681 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
683 SGPropertyNode *node, *child;
686 node = fgGetNode( "/environment/config/boundary" );
687 if ( node != NULL ) {
689 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
690 child->setDoubleValue( "dewpoint-sea-level-degc",
691 dewpoint_sea_level_degc );
697 node = fgGetNode( "/environment/config/aloft" );
698 if ( node != NULL ) {
700 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
701 child->setDoubleValue( "dewpoint-sea-level-degc",
702 dewpoint_sea_level_degc );
711 * Update the lighting manually.
714 do_timeofday (const SGPropertyNode * arg)
716 const string &offset_type = arg->getStringValue("timeofday", "noon");
718 static const SGPropertyNode *longitude
719 = fgGetNode("/position/longitude-deg");
720 static const SGPropertyNode *latitude
721 = fgGetNode("/position/latitude-deg");
722 static const SGPropertyNode *cur_time_override
723 = fgGetNode("/sim/time/cur-time-override", true);
725 int orig_warp = globals->get_warp();
726 SGTime *t = globals->get_time_params();
727 time_t cur_time = t->get_cur_time();
728 // cout << "cur_time = " << cur_time << endl;
729 // cout << "orig_warp = " << orig_warp << endl;
732 if ( offset_type == "real" ) {
734 } else if ( offset_type == "dawn" ) {
735 warp = fgTimeSecondsUntilSunAngle( cur_time,
736 longitude->getDoubleValue()
737 * SGD_DEGREES_TO_RADIANS,
738 latitude->getDoubleValue()
739 * SGD_DEGREES_TO_RADIANS,
741 } else if ( offset_type == "morning" ) {
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 == "noon" ) {
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 == "afternoon" ) {
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 == "dusk" ) {
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 == "evening" ) {
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 == "midnight" ) {
777 warp = fgTimeSecondsUntilSunAngle( cur_time,
778 longitude->getDoubleValue()
779 * SGD_DEGREES_TO_RADIANS,
780 latitude->getDoubleValue()
781 * SGD_DEGREES_TO_RADIANS,
784 // cout << "warp = " << warp << endl;
785 globals->set_warp( orig_warp + warp );
787 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
788 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
789 cur_time_override->getLongValue(),
790 globals->get_warp() );
797 * Built-in command: toggle a bool property value.
799 * property: The name of the property to toggle.
802 do_property_toggle (const SGPropertyNode * arg)
804 SGPropertyNode * prop = get_prop(arg);
805 return prop->setBoolValue(!prop->getBoolValue());
810 * Built-in command: assign a value to a property.
812 * property: the name of the property to assign.
813 * value: the value to assign; or
814 * property[1]: the property to copy from.
817 do_property_assign (const SGPropertyNode * arg)
819 SGPropertyNode * prop = get_prop(arg);
820 const SGPropertyNode * prop2 = get_prop2(arg);
821 const SGPropertyNode * value = arg->getNode("value");
824 return prop->setUnspecifiedValue(value->getStringValue());
826 return prop->setUnspecifiedValue(prop2->getStringValue());
833 * Built-in command: increment or decrement a property value.
835 * If the 'step' argument is present, it will be used; otherwise,
836 * the command uses 'offset' and 'factor', usually from the mouse.
838 * property: the name of the property to increment or decrement.
839 * step: the amount of the increment or decrement (default: 0).
840 * offset: offset from the current setting (used for the mouse; multiplied
842 * factor: scaling amount for the offset (defaults to 1).
843 * min: the minimum allowed value (default: no minimum).
844 * max: the maximum allowed value (default: no maximum).
845 * mask: 'integer' to apply only to the left of the decimal point,
846 * 'decimal' to apply only to the right of the decimal point,
847 * or 'all' to apply to the whole number (the default).
848 * wrap: true if the value should be wrapped when it passes min or max;
849 * both min and max must be present for this to work (default:
853 do_property_adjust (const SGPropertyNode * arg)
855 SGPropertyNode * prop = get_prop(arg);
858 if (arg->hasValue("step"))
859 amount = arg->getDoubleValue("step");
861 amount = (arg->getDoubleValue("factor", 1)
862 * arg->getDoubleValue("offset"));
864 double unmodifiable, modifiable;
865 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
866 &unmodifiable, &modifiable);
867 modifiable += amount;
868 limit_value(&modifiable, arg);
870 prop->setDoubleValue(unmodifiable + modifiable);
877 * Built-in command: multiply a property value.
879 * property: the name of the property to multiply.
880 * factor: the amount by which to multiply.
881 * min: the minimum allowed value (default: no minimum).
882 * max: the maximum allowed value (default: no maximum).
883 * mask: 'integer' to apply only to the left of the decimal point,
884 * 'decimal' to apply only to the right of the decimal point,
885 * or 'all' to apply to the whole number (the default).
886 * wrap: true if the value should be wrapped when it passes min or max;
887 * both min and max must be present for this to work (default:
891 do_property_multiply (const SGPropertyNode * arg)
893 SGPropertyNode * prop = get_prop(arg);
894 double factor = arg->getDoubleValue("factor", 1);
896 double unmodifiable, modifiable;
897 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
898 &unmodifiable, &modifiable);
899 modifiable *= factor;
900 limit_value(&modifiable, arg);
902 prop->setDoubleValue(unmodifiable + modifiable);
909 * Built-in command: swap two property values.
911 * property[0]: the name of the first property.
912 * property[1]: the name of the second property.
915 do_property_swap (const SGPropertyNode * arg)
917 SGPropertyNode * prop1 = get_prop(arg);
918 SGPropertyNode * prop2 = get_prop2(arg);
920 // FIXME: inefficient
921 const string & tmp = prop1->getStringValue();
922 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
923 prop2->setUnspecifiedValue(tmp.c_str()));
928 * Built-in command: Set a property to an axis or other moving input.
930 * property: the name of the property to set.
931 * setting: the current input setting, usually between -1.0 and 1.0.
932 * offset: the offset to shift by, before applying the factor.
933 * factor: the factor to multiply by (use negative to reverse).
936 do_property_scale (const SGPropertyNode * arg)
938 SGPropertyNode * prop = get_prop(arg);
939 double setting = arg->getDoubleValue("setting");
940 double offset = arg->getDoubleValue("offset", 0.0);
941 double factor = arg->getDoubleValue("factor", 1.0);
942 bool squared = arg->getBoolValue("squared", false);
943 int power = arg->getIntValue("power", (squared ? 2 : 1));
945 int sign = (setting < 0 ? -1 : 1);
951 setting = setting * setting * sign;
954 setting = setting * setting * setting;
957 setting = setting * setting * setting * setting * sign;
960 setting = pow(setting, power);
961 if ((power % 2) == 0)
966 return prop->setDoubleValue((setting + offset) * factor);
971 * Built-in command: cycle a property through a set of values.
973 * If the current value isn't in the list, the cycle will
974 * (re)start from the beginning.
976 * property: the name of the property to cycle.
977 * value[*]: the list of values to cycle through.
980 do_property_cycle (const SGPropertyNode * arg)
982 SGPropertyNode * prop = get_prop(arg);
983 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
985 int nSelections = values.size();
987 if (nSelections < 1) {
988 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
992 // Try to find the current selection
993 for (int i = 0; i < nSelections; i++) {
994 if (compare_values(prop, values[i])) {
1000 // Default or wrap to the first selection
1001 if (selection < 0 || selection >= nSelections)
1004 prop->setUnspecifiedValue(values[selection]->getStringValue());
1010 * Built-in command: randomize a numeric property value.
1012 * property: the name of the property value to randomize.
1013 * min: the minimum allowed value.
1014 * max: the maximum allowed value.
1017 do_property_randomize (const SGPropertyNode * arg)
1019 SGPropertyNode * prop = get_prop(arg);
1020 double min = arg->getDoubleValue("min", DBL_MIN);
1021 double max = arg->getDoubleValue("max", DBL_MAX);
1022 prop->setDoubleValue(sg_random() * (max - min) + min);
1028 * Built-in command: reinit the data logging system based on the
1029 * current contents of the /logger tree.
1032 do_data_logging_commit (const SGPropertyNode * arg)
1034 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1040 * Built-in command: Add a dialog to the GUI system. Does *not*
1041 * display the dialog. The property node should have the same format
1042 * as a dialog XML configuration. It must include:
1044 * name: the name of the GUI dialog for future reference.
1047 do_dialog_new (const SGPropertyNode * arg)
1049 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1051 // Note the casting away of const: this is *real*. Doing a
1052 // "dialog-apply" command later on will mutate this property node.
1053 // I'm not convinced that this isn't the Right Thing though; it
1054 // allows client to create a node, pass it to dialog-new, and get
1055 // the values back from the dialog by reading the same node.
1056 // Perhaps command arguments are not as "const" as they would
1058 gui->newDialog((SGPropertyNode*)arg);
1063 * Built-in command: Show an XML-configured dialog.
1065 * dialog-name: the name of the GUI dialog to display.
1068 do_dialog_show (const SGPropertyNode * arg)
1070 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1071 gui->showDialog(arg->getStringValue("dialog-name"));
1077 * Built-in Command: Hide the active XML-configured dialog.
1080 do_dialog_close (const SGPropertyNode * arg)
1082 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1083 if(arg->hasValue("dialog-name"))
1084 return gui->closeDialog(arg->getStringValue("dialog-name"));
1085 return gui->closeActiveDialog();
1090 * Update a value in the active XML-configured dialog.
1092 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1095 do_dialog_update (const SGPropertyNode * arg)
1097 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1099 if (arg->hasValue("dialog-name"))
1100 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1102 dialog = gui->getActiveDialog();
1105 dialog->updateValues(arg->getStringValue("object-name"));
1114 * Apply a value in the active XML-configured dialog.
1116 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1119 do_dialog_apply (const SGPropertyNode * arg)
1121 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1123 if (arg->hasValue("dialog-name"))
1124 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1126 dialog = gui->getActiveDialog();
1129 dialog->applyValues(arg->getStringValue("object-name"));
1138 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1142 do_gui_redraw (const SGPropertyNode * arg)
1144 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1150 * Built-in command: play an audio message (i.e. a wav file) This is
1151 * fire and forget. Call this once per message and it will get dumped
1152 * into a queue. Messages are played sequentially so they do not
1156 do_play_audio_message (const SGPropertyNode * arg)
1158 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1159 string path = arg->getStringValue("path");
1160 string file = arg->getStringValue("file");
1161 double volume = arg->getDoubleValue("volume");
1162 // cout << "playing " << path << " / " << file << endl;
1163 fx->play_message( path, file, volume );
1169 * Built-in command: commit presets (read from in /sim/presets/)
1172 do_presets_commit (const SGPropertyNode * arg)
1174 // unbind the current fdm state so property changes
1175 // don't get lost when we subsequently delete this fdm
1176 // and create a new one.
1177 cur_fdm_state->unbind();
1179 // set position from presets
1182 fgReInitSubsystems();
1184 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1187 if ( ! fgGetBool("/sim/presets/onground") ) {
1188 fgSetBool( "/sim/freeze/master", true );
1189 fgSetBool( "/sim/freeze/clock", true );
1197 * Built-in command: set log level (0 ... 7)
1200 do_log_level (const SGPropertyNode * arg)
1202 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1208 * Built-in command: replay the FDR buffer
1211 do_replay (const SGPropertyNode * arg)
1213 // freeze the master fdm
1214 fgSetInt( "/sim/freeze/replay-state", 1 );
1216 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1218 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1219 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1220 double duration = fgGetDouble( "/sim/replay/duration" );
1221 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1222 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1224 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1227 // cout << "start = " << r->get_start_time()
1228 // << " end = " << r->get_end_time() << endl;
1235 * Return terrain elevation for given longitude/latitude pair.
1238 do_terrain_elevation (const SGPropertyNode * arg)
1240 double lon = arg->getDoubleValue("longitude-deg", 0.0);
1241 double lat = arg->getDoubleValue("latitude-deg", 0.0);
1243 bool ret = globals->get_scenery()->get_elevation_m(lat, lon, 10000.0, elev, 0, false);
1244 const_cast<SGPropertyNode *>(arg)->setDoubleValue("elevation-m", elev);
1250 do_decrease_visibility (const SGPropertyNode * arg)
1252 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1253 fgSetDouble("/environment/visibility-m", new_value);
1254 fgDefaultWeatherValue("visibility-m", new_value);
1255 globals->get_subsystem("environment")->reinit();
1261 do_increase_visibility (const SGPropertyNode * arg)
1263 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1264 fgSetDouble("/environment/visibility-m", new_value);
1265 fgDefaultWeatherValue("visibility-m", new_value);
1266 globals->get_subsystem("environment")->reinit();
1272 do_hud_init(const SGPropertyNode *)
1274 fgHUDInit(0); // minimal HUD
1279 do_hud_init2(const SGPropertyNode *)
1281 fgHUDInit2(0); // normal HUD
1286 * An fgcommand to allow loading of xml files via nasal,
1287 * the xml file's structure will be made available within
1288 * a (definable) property tree node
1290 * @param filename a string to hold the complete path & filename of a XML file
1291 * @param targetnode a string pointing to a location within the property tree
1292 * where to store the parsed XML file
1296 do_load_xml_to_proptree(const SGPropertyNode * node)
1298 // SG_LOG(SG_GENERAL, SG_ALERT, "fgcommand loadxml executed");
1300 SGPropertyNode * targetnode;
1301 targetnode = fgGetNode(node->getNode("targetnode")->getStringValue(),true);
1303 const char *filename = node->getNode("filename")->getStringValue();
1305 fgLoadProps(filename, targetnode);
1306 } catch (const sg_exception &e) {
1307 string errmsg = "Error reading file " + string(filename) + ":\n";
1308 guiErrorMessage(errmsg.c_str(), e);
1317 * an fgcommand to allow saving of xml files via nasal,
1318 * the file's structure will be determined based on what's
1319 * encountered in the passed (source) property tree node
1321 * @param filename a string to hold the complete path & filename of the (new)
1323 * @param sourcenode a string pointing to a location within the property tree
1324 * where to find the nodes that should be written recursively into an XML file
1327 * deal with already existing filenames, optionally return error/success
1328 * values in a separate node to provide status information
1330 * note: it's directly using writeProperties, which is not necessarily
1331 * preferable, but for now it should work ...
1335 do_save_xml_from_proptree(const SGPropertyNode * node)
1337 //TODO: do Parameter validation !
1338 SGPropertyNode * sourcenode;
1339 sourcenode = fgGetNode(node->getNode("sourcenode")->getStringValue(),true);
1341 const char *filename = node->getNode("filename")->getStringValue();
1343 writeProperties (filename, sourcenode, true);
1344 } catch (const sg_exception &e) {
1345 string errmsg = "Error writing file " + string(filename) + ":\n";
1346 guiErrorMessage(errmsg.c_str(), e);
1354 do_press_cockpit_button (const SGPropertyNode *arg)
1356 const char *prefix = arg->getStringValue("prefix");
1358 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1361 string prop = string(prefix) + "-button";
1364 if (arg->getBoolValue("latching"))
1365 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1369 fgSetDouble(prop.c_str(), value);
1370 fgSetBool(arg->getStringValue("discrete"), value > 0);
1376 do_release_cockpit_button (const SGPropertyNode *arg)
1378 const char *prefix = arg->getStringValue("prefix");
1380 if (arg->getBoolValue("guarded")) {
1381 string prop = string(prefix) + "-guard";
1382 if (fgGetDouble(prop.c_str()) < 1) {
1383 fgSetDouble(prop.c_str(), 1);
1388 if (! arg->getBoolValue("latching")) {
1389 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1390 fgSetBool(arg->getStringValue("discrete"), false);
1397 ////////////////////////////////////////////////////////////////////////
1399 ////////////////////////////////////////////////////////////////////////
1403 * Table of built-in commands.
1405 * New commands do not have to be added here; any module in the application
1406 * can add a new command using globals->get_commands()->addCommand(...).
1410 SGCommandMgr::command_t command;
1412 { "null", do_null },
1413 #if defined(HAVE_PLIB_PSL)
1414 { "script", do_script },
1415 #endif // HAVE_PLIB_PSL
1416 { "nasal", do_nasal },
1417 { "exit", do_exit },
1418 { "reinit", do_reinit },
1419 { "suspend", do_reinit },
1420 { "resume", do_reinit },
1421 { "load", do_load },
1422 { "save", do_save },
1423 { "panel-load", do_panel_load },
1424 { "panel-mouse-click", do_panel_mouse_click },
1425 { "preferences-load", do_preferences_load },
1426 { "view-cycle", do_view_cycle },
1427 { "screen-capture", do_screen_capture },
1428 { "hires-screen-capture", do_hires_screen_capture },
1429 { "tile-cache-reload", do_tile_cache_reload },
1430 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1431 { "set-outside-air-temp-degc", do_set_oat_degc },
1432 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1433 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1434 { "timeofday", do_timeofday },
1435 { "property-toggle", do_property_toggle },
1436 { "property-assign", do_property_assign },
1437 { "property-adjust", do_property_adjust },
1438 { "property-multiply", do_property_multiply },
1439 { "property-swap", do_property_swap },
1440 { "property-scale", do_property_scale },
1441 { "property-cycle", do_property_cycle },
1442 { "property-randomize", do_property_randomize },
1443 { "data-logging-commit", do_data_logging_commit },
1444 { "dialog-new", do_dialog_new },
1445 { "dialog-show", do_dialog_show },
1446 { "dialog-close", do_dialog_close },
1447 { "dialog-update", do_dialog_update },
1448 { "dialog-apply", do_dialog_apply },
1449 { "gui-redraw", do_gui_redraw },
1450 { "play-audio-message", do_play_audio_message },
1451 { "presets-commit", do_presets_commit },
1452 { "log-level", do_log_level },
1453 { "replay", do_replay },
1454 { "terrain-elevation", do_terrain_elevation },
1455 { "decrease-visibility", do_decrease_visibility },
1456 { "increase-visibility", do_increase_visibility },
1457 { "hud-init", do_hud_init },
1458 { "hud-init2", do_hud_init2 },
1459 { "loadxml", do_load_xml_to_proptree},
1460 { "savexml", do_save_xml_from_proptree },
1461 { "press-cockpit-button", do_press_cockpit_button },
1462 { "release-cockpit-button", do_release_cockpit_button },
1463 { 0, 0 } // zero-terminated
1468 * Initialize the default built-in commands.
1470 * Other commands may be added by other parts of the application.
1475 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1476 for (int i = 0; built_ins[i].name != 0; i++) {
1477 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1478 globals->get_commands()->addCommand(built_ins[i].name,
1479 built_ins[i].command);
1482 typedef bool (*dummy)();
1483 fgTie( "/command/view/next", dummy(0), do_view_next );
1484 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1487 // end of fg_commands.cxx