1 // fg_commands.cxx - internal FGFS commands.
3 #include <string.h> // strcmp()
5 #include <simgear/compiler.h>
10 #include <simgear/sg_inlines.h>
11 #include <simgear/debug/logstream.hxx>
12 #include <simgear/math/sg_random.h>
13 #include <simgear/structure/exception.hxx>
14 #include <simgear/structure/commands.hxx>
15 #include <simgear/props/props.hxx>
17 #include <Cockpit/panel.hxx>
18 #include <Cockpit/panel_io.hxx>
19 #include <Cockpit/hud.hxx>
20 #include <Environment/environment.hxx>
21 #include <FDM/flight.hxx>
23 #include <GUI/new_gui.hxx>
24 #include <GUI/dialog.hxx>
25 #include <Aircraft/replay.hxx>
26 #include <Scenery/tilemgr.hxx>
27 #if defined(HAVE_PLIB_PSL)
28 # include <Scripting/scriptmgr.hxx>
30 #include <Scripting/NasalSys.hxx>
31 #include <Time/sunsolver.hxx>
32 #include <Time/tmp.hxx>
34 #include "fg_init.hxx"
36 #include "fg_commands.hxx"
37 #include "fg_props.hxx"
38 #include "globals.hxx"
41 #include "viewmgr.hxx"
45 SG_USING_STD(ifstream);
46 SG_USING_STD(ofstream);
50 ////////////////////////////////////////////////////////////////////////
51 // Static helper functions.
52 ////////////////////////////////////////////////////////////////////////
55 static inline SGPropertyNode *
56 get_prop (const SGPropertyNode * arg)
58 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
61 static inline SGPropertyNode *
62 get_prop2 (const SGPropertyNode * arg)
64 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
69 * Get a double value and split it as required.
72 split_value (double full_value, const char * mask,
73 double * unmodifiable, double * modifiable)
75 if (!strcmp("integer", mask)) {
76 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
77 *unmodifiable = full_value - *modifiable;
78 } else if (!strcmp("decimal", mask)) {
79 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
80 *modifiable = full_value - *unmodifiable;
82 if (strcmp("all", mask))
83 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
84 << " assuming 'all'");
86 *modifiable = full_value;
92 * Clamp or wrap a value as specified.
95 limit_value (double * value, const SGPropertyNode * arg)
97 const SGPropertyNode * min_node = arg->getChild("min");
98 const SGPropertyNode * max_node = arg->getChild("max");
100 bool wrap = arg->getBoolValue("wrap");
102 if (min_node == 0 || max_node == 0)
105 if (wrap) { // wrap such that min <= x < max
106 double min_val = min_node->getDoubleValue();
107 double max_val = max_node->getDoubleValue();
108 double resolution = arg->getDoubleValue("resolution");
109 if (resolution > 0.0) {
110 // snap to (min + N*resolution), taking special care to handle imprecision
111 int n = (int)floor((*value - min_val) / resolution + 0.5);
112 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
113 SG_NORMALIZE_RANGE(n, 0, steps);
114 *value = min_val + resolution * n;
116 // plain circular wrapping
117 SG_NORMALIZE_RANGE(*value, min_val, max_val);
119 } else { // clamp such that min <= x <= max
120 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
121 *value = min_node->getDoubleValue();
122 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
123 *value = max_node->getDoubleValue();
128 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
130 switch (value1->getType()) {
131 case SGPropertyNode::BOOL:
132 return (value1->getBoolValue() == value2->getBoolValue());
133 case SGPropertyNode::INT:
134 return (value1->getIntValue() == value2->getIntValue());
135 case SGPropertyNode::LONG:
136 return (value1->getLongValue() == value2->getLongValue());
137 case SGPropertyNode::FLOAT:
138 return (value1->getFloatValue() == value2->getFloatValue());
139 case SGPropertyNode::DOUBLE:
140 return (value1->getDoubleValue() == value2->getDoubleValue());
142 return !strcmp(value1->getStringValue(), value2->getStringValue());
148 ////////////////////////////////////////////////////////////////////////
149 // Command implementations.
150 ////////////////////////////////////////////////////////////////////////
154 * Built-in command: do nothing.
157 do_null (const SGPropertyNode * arg)
162 #if defined(HAVE_PLIB_PSL)
164 * Built-in command: run a PSL script.
166 * script: the PSL script to execute
169 do_script (const SGPropertyNode * arg)
171 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
172 return mgr->run(arg->getStringValue("script"));
174 #endif // HAVE_PLIB_PSL
177 * Built-in command: run a Nasal script.
180 do_nasal (const SGPropertyNode * arg)
182 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
186 * Built-in command: exit FlightGear.
188 * status: the exit status to return to the operating system (defaults to 0)
191 do_exit (const SGPropertyNode * arg)
193 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
195 if (fgGetBool("/sim/startup/save-on-exit")) {
197 char* envp = ::getenv( "APPDATA" );
198 if ( envp != NULL ) {
199 SGPath config( envp );
200 config.append( "flightgear.org" );
202 if ( homedir != NULL ) {
203 SGPath config( homedir );
204 config.append( ".fgfs" );
206 config.append( "preferences.xml" );
207 config.create_dir( 0700 );
208 SG_LOG(SG_IO, SG_INFO, "Saving user preferences");
210 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
211 } catch (const sg_exception &e) {
212 guiErrorMessage("Error saving preferences: ", e);
215 SG_LOG(SG_INPUT, SG_BULK, "Finished Saving user preferences");
218 fgExit(arg->getIntValue("status", 0));
224 * Built-in command: reinitialize one or more subsystems.
226 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
227 * none is specified, reinitialize all of them.
230 do_reinit (const SGPropertyNode * arg)
234 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
235 if (subsystems.size() == 0) {
236 globals->get_subsystem_mgr()->reinit();
238 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
239 const char * name = subsystems[i]->getStringValue();
240 SGSubsystem * subsystem = globals->get_subsystem(name);
241 if (subsystem == 0) {
243 SG_LOG( SG_GENERAL, SG_ALERT,
244 "Subsystem " << name << "not found" );
251 globals->get_event_mgr()->reinit();
258 // these routines look useful ??? but are never used in the code ???
262 * Built-in command: suspend one or more subsystems.
264 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
267 do_suspend (const SGPropertyNode * arg)
271 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
272 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
273 const char * name = subsystems[i]->getStringValue();
274 SGSubsystem * subsystem = globals->get_subsystem(name);
275 if (subsystem == 0) {
277 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
279 subsystem->suspend();
286 * Built-in command: suspend one or more subsystems.
288 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
291 do_resume (const SGPropertyNode * arg)
295 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
296 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
297 const char * name = subsystems[i]->getStringValue();
298 SGSubsystem * subsystem = globals->get_subsystem(name);
299 if (subsystem == 0) {
301 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
313 * Built-in command: load flight.
315 * file (optional): the name of the file to load (relative to current
316 * directory). Defaults to "fgfs.sav"
319 do_load (const SGPropertyNode * arg)
321 const string &file = arg->getStringValue("file", "fgfs.sav");
322 ifstream input(file.c_str());
323 if (input.good() && fgLoadFlight(input)) {
325 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
328 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
335 * Built-in command: save flight.
337 * file (optional): the name of the file to save (relative to the
338 * current directory). Defaults to "fgfs.sav".
341 do_save (const SGPropertyNode * arg)
343 const string &file = arg->getStringValue("file", "fgfs.sav");
344 bool write_all = arg->getBoolValue("write-all", false);
345 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
346 ofstream output(file.c_str());
347 if (output.good() && fgSaveFlight(output, write_all)) {
349 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
352 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
359 * Built-in command: (re)load the panel.
361 * path (optional): the file name to load the panel from
362 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
363 * and if that's unspecified, to "Panels/Default/default.xml".
366 do_panel_load (const SGPropertyNode * arg)
369 arg->getStringValue("path",
370 fgGetString("/sim/panel/path",
371 "Panels/Default/default.xml"));
372 FGPanel * new_panel = fgReadPanel(panel_path);
373 if (new_panel == 0) {
374 SG_LOG(SG_INPUT, SG_ALERT,
375 "Error reading new panel from " << panel_path);
378 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
379 globals->get_current_panel()->unbind();
380 delete globals->get_current_panel();
381 globals->set_current_panel( new_panel );
382 globals->get_current_panel()->bind();
388 * Built-in command: pass a mouse click to the panel.
390 * button: the mouse button number, zero-based.
391 * is-down: true if the button is down, false if it is up.
392 * x-pos: the x position of the mouse click.
393 * y-pos: the y position of the mouse click.
396 do_panel_mouse_click (const SGPropertyNode * arg)
398 if (globals->get_current_panel() != 0)
399 return globals->get_current_panel()
400 ->doMouseAction(arg->getIntValue("button"),
401 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
402 arg->getIntValue("x-pos"),
403 arg->getIntValue("y-pos"));
410 * Built-in command: (re)load preferences.
412 * path (optional): the file name to load the panel from (relative
413 * to FG_ROOT). Defaults to "preferences.xml".
416 do_preferences_load (const SGPropertyNode * arg)
419 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
420 globals->get_props());
421 } catch (const sg_exception &e) {
422 guiErrorMessage("Error reading global preferences: ", e);
425 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
433 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
434 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
435 if ( globals->get_viewmgr()->get_current() == 1 ) {
436 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
444 globals->get_current_view()->setHeadingOffset_deg(0.0);
445 globals->get_viewmgr()->next_view();
446 fix_hud_visibility();
447 globals->get_tile_mgr()->refresh_view_timestamps();
453 globals->get_current_view()->setHeadingOffset_deg(0.0);
454 globals->get_viewmgr()->prev_view();
455 fix_hud_visibility();
456 globals->get_tile_mgr()->refresh_view_timestamps();
460 * Built-in command: cycle view.
463 do_view_cycle (const SGPropertyNode * arg)
465 globals->get_current_view()->setHeadingOffset_deg(0.0);
466 globals->get_viewmgr()->next_view();
467 fix_hud_visibility();
468 globals->get_tile_mgr()->refresh_view_timestamps();
469 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
474 * Built-in command: capture screen.
477 do_screen_capture (const SGPropertyNode * arg)
485 * Built-in command: hires capture screen.
488 do_hires_screen_capture (const SGPropertyNode * arg)
496 * Reload the tile cache.
499 do_tile_cache_reload (const SGPropertyNode * arg)
501 static const SGPropertyNode *master_freeze
502 = fgGetNode("/sim/freeze/master");
503 bool freeze = master_freeze->getBoolValue();
504 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
506 fgSetBool("/sim/freeze/master", true);
509 if ( globals->get_tile_mgr()->init() ) {
510 // Load the local scenery data
511 double visibility_meters = fgGetDouble("/environment/visibility-m");
512 globals->get_tile_mgr()->update( visibility_meters );
514 SG_LOG( SG_GENERAL, SG_ALERT,
515 "Error in Tile Manager initialization!" );
520 fgSetBool("/sim/freeze/master", false);
527 * Set the sea level outside air temperature and assigning that to all
528 * boundary and aloft environment layers.
531 do_set_sea_level_degc (const SGPropertyNode * arg)
533 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
535 SGPropertyNode *node, *child;
538 node = fgGetNode( "/environment/config/boundary" );
539 if ( node != NULL ) {
541 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
542 child->setDoubleValue( "temperature-sea-level-degc",
543 temp_sea_level_degc );
549 node = fgGetNode( "/environment/config/aloft" );
550 if ( node != NULL ) {
552 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
553 child->setDoubleValue( "temperature-sea-level-degc",
554 temp_sea_level_degc );
564 * Set the outside air temperature at the "current" altitude by first
565 * calculating the corresponding sea level temp, and assigning that to
566 * all boundary and aloft environment layers.
569 do_set_oat_degc (const SGPropertyNode * arg)
571 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
573 // check for an altitude specified in the arguments, otherwise use
574 // current aircraft altitude.
575 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
576 if ( altitude_ft == NULL ) {
577 altitude_ft = fgGetNode("/position/altitude-ft");
580 FGEnvironment dummy; // instantiate a dummy so we can leech a method
581 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
582 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
583 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
585 cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
586 cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
587 cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
589 SGPropertyNode *node, *child;
592 node = fgGetNode( "/environment/config/boundary" );
593 if ( node != NULL ) {
595 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
596 child->setDoubleValue( "temperature-sea-level-degc",
597 temp_sea_level_degc );
603 node = fgGetNode( "/environment/config/aloft" );
604 if ( node != NULL ) {
606 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
607 child->setDoubleValue( "temperature-sea-level-degc",
608 temp_sea_level_degc );
617 * Set the sea level outside air dewpoint and assigning that to all
618 * boundary and aloft environment layers.
621 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
623 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
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( "dewpoint-sea-level-degc",
633 dewpoint_sea_level_degc );
639 node = fgGetNode( "/environment/config/aloft" );
640 if ( node != NULL ) {
642 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
643 child->setDoubleValue( "dewpoint-sea-level-degc",
644 dewpoint_sea_level_degc );
654 * Set the outside air dewpoint at the "current" altitude by first
655 * calculating the corresponding sea level dewpoint, and assigning
656 * that to all boundary and aloft environment layers.
659 do_set_dewpoint_degc (const SGPropertyNode * arg)
661 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
663 // check for an altitude specified in the arguments, otherwise use
664 // current aircraft altitude.
665 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
666 if ( altitude_ft == NULL ) {
667 altitude_ft = fgGetNode("/position/altitude-ft");
670 FGEnvironment dummy; // instantiate a dummy so we can leech a method
671 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
672 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
673 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
675 cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
676 cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
677 cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
679 SGPropertyNode *node, *child;
682 node = fgGetNode( "/environment/config/boundary" );
683 if ( node != NULL ) {
685 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
686 child->setDoubleValue( "dewpoint-sea-level-degc",
687 dewpoint_sea_level_degc );
693 node = fgGetNode( "/environment/config/aloft" );
694 if ( node != NULL ) {
696 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
697 child->setDoubleValue( "dewpoint-sea-level-degc",
698 dewpoint_sea_level_degc );
707 * Update the lighting manually.
710 do_timeofday (const SGPropertyNode * arg)
712 const string &offset_type = arg->getStringValue("timeofday", "noon");
714 static const SGPropertyNode *longitude
715 = fgGetNode("/position/longitude-deg");
716 static const SGPropertyNode *latitude
717 = fgGetNode("/position/latitude-deg");
718 static const SGPropertyNode *cur_time_override
719 = fgGetNode("/sim/time/cur-time-override", true);
721 int orig_warp = globals->get_warp();
722 SGTime *t = globals->get_time_params();
723 time_t cur_time = t->get_cur_time();
724 // cout << "cur_time = " << cur_time << endl;
725 // cout << "orig_warp = " << orig_warp << endl;
728 if ( offset_type == "real" ) {
730 } else if ( offset_type == "dawn" ) {
731 warp = fgTimeSecondsUntilSunAngle( cur_time,
732 longitude->getDoubleValue()
733 * SGD_DEGREES_TO_RADIANS,
734 latitude->getDoubleValue()
735 * SGD_DEGREES_TO_RADIANS,
737 } else if ( offset_type == "morning" ) {
738 warp = fgTimeSecondsUntilSunAngle( cur_time,
739 longitude->getDoubleValue()
740 * SGD_DEGREES_TO_RADIANS,
741 latitude->getDoubleValue()
742 * SGD_DEGREES_TO_RADIANS,
744 } else if ( offset_type == "noon" ) {
745 warp = fgTimeSecondsUntilSunAngle( cur_time,
746 longitude->getDoubleValue()
747 * SGD_DEGREES_TO_RADIANS,
748 latitude->getDoubleValue()
749 * SGD_DEGREES_TO_RADIANS,
751 } else if ( offset_type == "afternoon" ) {
752 warp = fgTimeSecondsUntilSunAngle( cur_time,
753 longitude->getDoubleValue()
754 * SGD_DEGREES_TO_RADIANS,
755 latitude->getDoubleValue()
756 * SGD_DEGREES_TO_RADIANS,
758 } else if ( offset_type == "dusk" ) {
759 warp = fgTimeSecondsUntilSunAngle( cur_time,
760 longitude->getDoubleValue()
761 * SGD_DEGREES_TO_RADIANS,
762 latitude->getDoubleValue()
763 * SGD_DEGREES_TO_RADIANS,
765 } else if ( offset_type == "evening" ) {
766 warp = fgTimeSecondsUntilSunAngle( cur_time,
767 longitude->getDoubleValue()
768 * SGD_DEGREES_TO_RADIANS,
769 latitude->getDoubleValue()
770 * SGD_DEGREES_TO_RADIANS,
772 } else if ( offset_type == "midnight" ) {
773 warp = fgTimeSecondsUntilSunAngle( cur_time,
774 longitude->getDoubleValue()
775 * SGD_DEGREES_TO_RADIANS,
776 latitude->getDoubleValue()
777 * SGD_DEGREES_TO_RADIANS,
780 // cout << "warp = " << warp << endl;
781 globals->set_warp( orig_warp + warp );
783 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
784 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
785 cur_time_override->getLongValue(),
786 globals->get_warp() );
793 * Built-in command: toggle a bool property value.
795 * property: The name of the property to toggle.
798 do_property_toggle (const SGPropertyNode * arg)
800 SGPropertyNode * prop = get_prop(arg);
801 return prop->setBoolValue(!prop->getBoolValue());
806 * Built-in command: assign a value to a property.
808 * property: the name of the property to assign.
809 * value: the value to assign; or
810 * property[1]: the property to copy from.
813 do_property_assign (const SGPropertyNode * arg)
815 SGPropertyNode * prop = get_prop(arg);
816 const SGPropertyNode * prop2 = get_prop2(arg);
817 const SGPropertyNode * value = arg->getNode("value");
820 return prop->setUnspecifiedValue(value->getStringValue());
822 return prop->setUnspecifiedValue(prop2->getStringValue());
829 * Built-in command: increment or decrement a property value.
831 * If the 'step' argument is present, it will be used; otherwise,
832 * the command uses 'offset' and 'factor', usually from the mouse.
834 * property: the name of the property to increment or decrement.
835 * step: the amount of the increment or decrement (default: 0).
836 * offset: offset from the current setting (used for the mouse; multiplied
838 * factor: scaling amount for the offset (defaults to 1).
839 * min: the minimum allowed value (default: no minimum).
840 * max: the maximum allowed value (default: no maximum).
841 * mask: 'integer' to apply only to the left of the decimal point,
842 * 'decimal' to apply only to the right of the decimal point,
843 * or 'all' to apply to the whole number (the default).
844 * wrap: true if the value should be wrapped when it passes min or max;
845 * both min and max must be present for this to work (default:
849 do_property_adjust (const SGPropertyNode * arg)
851 SGPropertyNode * prop = get_prop(arg);
854 if (arg->hasValue("step"))
855 amount = arg->getDoubleValue("step");
857 amount = (arg->getDoubleValue("factor", 1)
858 * arg->getDoubleValue("offset"));
860 double unmodifiable, modifiable;
861 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
862 &unmodifiable, &modifiable);
863 modifiable += amount;
864 limit_value(&modifiable, arg);
866 prop->setDoubleValue(unmodifiable + modifiable);
873 * Built-in command: multiply a property value.
875 * property: the name of the property to multiply.
876 * factor: the amount by which to multiply.
877 * min: the minimum allowed value (default: no minimum).
878 * max: the maximum allowed value (default: no maximum).
879 * mask: 'integer' to apply only to the left of the decimal point,
880 * 'decimal' to apply only to the right of the decimal point,
881 * or 'all' to apply to the whole number (the default).
882 * wrap: true if the value should be wrapped when it passes min or max;
883 * both min and max must be present for this to work (default:
887 do_property_multiply (const SGPropertyNode * arg)
889 SGPropertyNode * prop = get_prop(arg);
890 double factor = arg->getDoubleValue("factor", 1);
892 double unmodifiable, modifiable;
893 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
894 &unmodifiable, &modifiable);
895 modifiable *= factor;
896 limit_value(&modifiable, arg);
898 prop->setDoubleValue(unmodifiable + modifiable);
905 * Built-in command: swap two property values.
907 * property[0]: the name of the first property.
908 * property[1]: the name of the second property.
911 do_property_swap (const SGPropertyNode * arg)
913 SGPropertyNode * prop1 = get_prop(arg);
914 SGPropertyNode * prop2 = get_prop2(arg);
916 // FIXME: inefficient
917 const string & tmp = prop1->getStringValue();
918 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
919 prop2->setUnspecifiedValue(tmp.c_str()));
924 * Built-in command: Set a property to an axis or other moving input.
926 * property: the name of the property to set.
927 * setting: the current input setting, usually between -1.0 and 1.0.
928 * offset: the offset to shift by, before applying the factor.
929 * factor: the factor to multiply by (use negative to reverse).
932 do_property_scale (const SGPropertyNode * arg)
934 SGPropertyNode * prop = get_prop(arg);
935 double setting = arg->getDoubleValue("setting");
936 double offset = arg->getDoubleValue("offset", 0.0);
937 double factor = arg->getDoubleValue("factor", 1.0);
938 bool squared = arg->getBoolValue("squared", false);
939 int power = arg->getIntValue("power", (squared ? 2 : 1));
941 int sign = (setting < 0 ? -1 : 1);
947 setting = setting * setting * sign;
950 setting = setting * setting * setting;
953 setting = setting * setting * setting * setting * sign;
956 setting = pow(setting, power);
957 if ((power % 2) == 0)
962 return prop->setDoubleValue((setting + offset) * factor);
967 * Built-in command: cycle a property through a set of values.
969 * If the current value isn't in the list, the cycle will
970 * (re)start from the beginning.
972 * property: the name of the property to cycle.
973 * value[*]: the list of values to cycle through.
976 do_property_cycle (const SGPropertyNode * arg)
978 SGPropertyNode * prop = get_prop(arg);
979 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
981 int nSelections = values.size();
983 if (nSelections < 1) {
984 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
988 // Try to find the current selection
989 for (int i = 0; i < nSelections; i++) {
990 if (compare_values(prop, values[i])) {
996 // Default or wrap to the first selection
997 if (selection < 0 || selection >= nSelections)
1000 prop->setUnspecifiedValue(values[selection]->getStringValue());
1006 * Built-in command: randomize a numeric property value.
1008 * property: the name of the property value to randomize.
1009 * min: the minimum allowed value.
1010 * max: the maximum allowed value.
1013 do_property_randomize (const SGPropertyNode * arg)
1015 SGPropertyNode * prop = get_prop(arg);
1016 double min = arg->getDoubleValue("min", DBL_MIN);
1017 double max = arg->getDoubleValue("max", DBL_MAX);
1018 prop->setDoubleValue(sg_random() * (max - min) + min);
1024 * Built-in command: reinit the data logging system based on the
1025 * current contents of the /logger tree.
1028 do_data_logging_commit (const SGPropertyNode * arg)
1030 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1036 * Built-in command: Add a dialog to the GUI system. Does *not*
1037 * display the dialog. The property node should have the same format
1038 * as a dialog XML configuration. It must include:
1040 * name: the name of the GUI dialog for future reference.
1043 do_dialog_new (const SGPropertyNode * arg)
1045 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1047 // Note the casting away of const: this is *real*. Doing a
1048 // "dialog-apply" command later on will mutate this property node.
1049 // I'm not convinced that this isn't the Right Thing though; it
1050 // allows client to create a node, pass it to dialog-new, and get
1051 // the values back from the dialog by reading the same node.
1052 // Perhaps command arguments are not as "const" as they would
1054 gui->newDialog((SGPropertyNode*)arg);
1059 * Built-in command: Show an XML-configured dialog.
1061 * dialog-name: the name of the GUI dialog to display.
1064 do_dialog_show (const SGPropertyNode * arg)
1066 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1067 gui->showDialog(arg->getStringValue("dialog-name"));
1073 * Built-in Command: Hide the active XML-configured dialog.
1076 do_dialog_close (const SGPropertyNode * arg)
1078 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1079 if(arg->hasValue("dialog-name"))
1080 return gui->closeDialog(arg->getStringValue("dialog-name"));
1081 return gui->closeActiveDialog();
1086 * Update a value in the active XML-configured dialog.
1088 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1091 do_dialog_update (const SGPropertyNode * arg)
1093 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1094 FGDialog * dialog = gui->getActiveDialog();
1096 if (arg->hasValue("object-name")) {
1097 dialog->updateValue(arg->getStringValue("object-name"));
1099 dialog->updateValues();
1109 * Apply a value in the active XML-configured dialog.
1111 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1114 do_dialog_apply (const SGPropertyNode * arg)
1116 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1117 FGDialog * dialog = gui->getActiveDialog();
1119 if (arg->hasValue("object-name")) {
1120 const char * name = arg->getStringValue("object-name");
1121 dialog->applyValue(name);
1122 dialog->updateValue(name);
1124 dialog->applyValues();
1125 dialog->updateValues();
1135 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1139 do_gui_redraw (const SGPropertyNode * arg)
1141 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1147 * Built-in command: commit presets (read from in /sim/presets/)
1150 do_presets_commit (const SGPropertyNode * arg)
1152 // unbind the current fdm state so property changes
1153 // don't get lost when we subsequently delete this fdm
1154 // and create a new one.
1155 cur_fdm_state->unbind();
1157 // set position from presets
1161 fgReInitSubsystems();
1163 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1166 if ( ! fgGetBool("/sim/presets/onground") ) {
1167 fgSetBool( "/sim/freeze/master", true );
1168 fgSetBool( "/sim/freeze/clock", true );
1176 * Built-in command: set log level (0 ... 7)
1179 do_log_level (const SGPropertyNode * arg)
1181 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1187 * Built-in command: replay the FDR buffer
1190 do_replay (const SGPropertyNode * arg)
1192 // freeze the master fdm
1193 fgSetInt( "/sim/freeze/replay-state", 1 );
1195 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1197 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1198 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1199 double duration = fgGetDouble( "/sim/replay/duration" );
1200 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1201 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1203 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1206 cout << "start = " << r->get_start_time()
1207 << " end = " << r->get_end_time() << endl;
1215 do_decrease_visibility (const SGPropertyNode * arg)
1217 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1218 fgSetDouble("/environment/visibility-m", new_value);
1219 fgDefaultWeatherValue("visibility-m", new_value);
1220 globals->get_subsystem("environment")->reinit();
1226 do_increase_visibility (const SGPropertyNode * arg)
1228 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1229 fgSetDouble("/environment/visibility-m", new_value);
1230 fgDefaultWeatherValue("visibility-m", new_value);
1231 globals->get_subsystem("environment")->reinit();
1237 do_hud_brightkey(const SGPropertyNode *)
1239 HUD_brightkey( true );
1244 do_hud_masterswitch(const SGPropertyNode *)
1246 HUD_masterswitch( true );
1251 do_hud_init(const SGPropertyNode *)
1253 fgHUDInit(0); // minimal HUD
1258 do_hud_init2(const SGPropertyNode *)
1260 fgHUDInit2(0); // normal HUD
1265 * An fgcommand to allow loading of xml files via nasal,
1266 * the xml file's structure will be made available within
1267 * a (definable) property tree node
1269 * @param filename a string to hold the complete path & filename of a XML file
1270 * @param targetnode a string pointing to a location within the property tree
1271 * where to store the parsed XML file
1275 do_load_xml_to_proptree(const SGPropertyNode * node)
1277 // SG_LOG(SG_GENERAL, SG_ALERT, "fgcommand loadxml executed");
1279 SGPropertyNode * targetnode;
1280 targetnode = fgGetNode(node->getNode("targetnode")->getStringValue(),true);
1282 const char *filename = node->getNode("filename")->getStringValue();
1284 fgLoadProps(filename, targetnode);
1285 } catch (const sg_exception &e) {
1286 string errmsg = "Error reading file " + string(filename) + ":\n";
1287 guiErrorMessage(errmsg.c_str(), e);
1296 * an fgcommand to allow saving of xml files via nasal,
1297 * the file's structure will be determined based on what's
1298 * encountered in the passed (source) property tree node
1300 * @param filename a string to hold the complete path & filename of the (new)
1302 * @param sourcenode a string pointing to a location within the property tree
1303 * where to find the nodes that should be written recursively into an XML file
1306 * deal with already existing filenames, optionally return error/success
1307 * values in a separate node to provide status information
1309 * note: it's directly using writeProperties, which is not necessarily
1310 * preferable, but for now it should work ...
1314 do_save_xml_from_proptree(const SGPropertyNode * node)
1316 //TODO: do Parameter validation !
1317 SGPropertyNode * sourcenode;
1318 sourcenode = fgGetNode(node->getNode("sourcenode")->getStringValue(),true);
1320 const char *filename = node->getNode("filename")->getStringValue();
1322 writeProperties (filename, sourcenode, true);
1323 } catch (const sg_exception &e) {
1324 string errmsg = "Error writing file " + string(filename) + ":\n";
1325 guiErrorMessage(errmsg.c_str(), e);
1333 ////////////////////////////////////////////////////////////////////////
1335 ////////////////////////////////////////////////////////////////////////
1339 * Table of built-in commands.
1341 * New commands do not have to be added here; any module in the application
1342 * can add a new command using globals->get_commands()->addCommand(...).
1346 SGCommandMgr::command_t command;
1348 { "null", do_null },
1349 #if defined(HAVE_PLIB_PSL)
1350 { "script", do_script },
1351 #endif // HAVE_PLIB_PSL
1352 { "nasal", do_nasal },
1353 { "exit", do_exit },
1354 { "reinit", do_reinit },
1355 { "suspend", do_reinit },
1356 { "resume", do_reinit },
1357 { "load", do_load },
1358 { "save", do_save },
1359 { "panel-load", do_panel_load },
1360 { "panel-mouse-click", do_panel_mouse_click },
1361 { "preferences-load", do_preferences_load },
1362 { "view-cycle", do_view_cycle },
1363 { "screen-capture", do_screen_capture },
1364 { "hires-screen-capture", do_hires_screen_capture },
1365 { "tile-cache-reload", do_tile_cache_reload },
1366 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1367 { "set-outside-air-temp-degc", do_set_oat_degc },
1368 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1369 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1370 { "timeofday", do_timeofday },
1371 { "property-toggle", do_property_toggle },
1372 { "property-assign", do_property_assign },
1373 { "property-adjust", do_property_adjust },
1374 { "property-multiply", do_property_multiply },
1375 { "property-swap", do_property_swap },
1376 { "property-scale", do_property_scale },
1377 { "property-cycle", do_property_cycle },
1378 { "property-randomize", do_property_randomize },
1379 { "data-logging-commit", do_data_logging_commit },
1380 { "dialog-new", do_dialog_new },
1381 { "dialog-show", do_dialog_show },
1382 { "dialog-close", do_dialog_close },
1383 { "dialog-update", do_dialog_update },
1384 { "dialog-apply", do_dialog_apply },
1385 { "gui-redraw", do_gui_redraw },
1386 { "presets-commit", do_presets_commit },
1387 { "log-level", do_log_level },
1388 { "replay", do_replay },
1389 { "decrease-visibility", do_decrease_visibility },
1390 { "increase-visibility", do_increase_visibility },
1391 { "hud-brightkey", do_hud_brightkey },
1392 { "hud-masterswitch", do_hud_masterswitch },
1393 { "hud-init", do_hud_init },
1394 { "hud-init2", do_hud_init2 },
1395 { "loadxml", do_load_xml_to_proptree},
1396 { "savexml", do_save_xml_from_proptree },
1397 { 0, 0 } // zero-terminated
1402 * Initialize the default built-in commands.
1404 * Other commands may be added by other parts of the application.
1409 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1410 for (int i = 0; built_ins[i].name != 0; i++) {
1411 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1412 globals->get_commands()->addCommand(built_ins[i].name,
1413 built_ins[i].command);
1416 typedef bool (*dummy)();
1417 fgTie( "/command/view/next", dummy(0), do_view_next );
1418 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1421 // end of fg_commands.cxx