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)
492 * Built-in command: hires capture screen.
495 do_hires_screen_capture (const SGPropertyNode * arg)
503 * Reload the tile cache.
506 do_tile_cache_reload (const SGPropertyNode * arg)
508 static const SGPropertyNode *master_freeze
509 = fgGetNode("/sim/freeze/master");
510 bool freeze = master_freeze->getBoolValue();
511 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
513 fgSetBool("/sim/freeze/master", true);
515 if ( globals->get_tile_mgr()->init() ) {
516 // Load the local scenery data
517 double visibility_meters = fgGetDouble("/environment/visibility-m");
518 globals->get_tile_mgr()->update( visibility_meters );
520 SG_LOG( SG_GENERAL, SG_ALERT,
521 "Error in Tile Manager initialization!" );
525 fgSetBool("/sim/freeze/master", false);
532 * Set the sea level outside air temperature and assigning that to all
533 * boundary and aloft environment layers.
536 do_set_sea_level_degc (const SGPropertyNode * arg)
538 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
540 SGPropertyNode *node, *child;
543 node = fgGetNode( "/environment/config/boundary" );
544 if ( node != NULL ) {
546 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
547 child->setDoubleValue( "temperature-sea-level-degc",
548 temp_sea_level_degc );
554 node = fgGetNode( "/environment/config/aloft" );
555 if ( node != NULL ) {
557 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
558 child->setDoubleValue( "temperature-sea-level-degc",
559 temp_sea_level_degc );
569 * Set the outside air temperature at the "current" altitude by first
570 * calculating the corresponding sea level temp, and assigning that to
571 * all boundary and aloft environment layers.
574 do_set_oat_degc (const SGPropertyNode * arg)
576 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
578 // check for an altitude specified in the arguments, otherwise use
579 // current aircraft altitude.
580 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
581 if ( altitude_ft == NULL ) {
582 altitude_ft = fgGetNode("/position/altitude-ft");
585 FGEnvironment dummy; // instantiate a dummy so we can leech a method
586 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
587 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
588 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
590 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
591 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
592 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
594 SGPropertyNode *node, *child;
597 node = fgGetNode( "/environment/config/boundary" );
598 if ( node != NULL ) {
600 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
601 child->setDoubleValue( "temperature-sea-level-degc",
602 temp_sea_level_degc );
608 node = fgGetNode( "/environment/config/aloft" );
609 if ( node != NULL ) {
611 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
612 child->setDoubleValue( "temperature-sea-level-degc",
613 temp_sea_level_degc );
622 * Set the sea level outside air dewpoint and assigning that to all
623 * boundary and aloft environment layers.
626 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
628 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
630 SGPropertyNode *node, *child;
633 node = fgGetNode( "/environment/config/boundary" );
634 if ( node != NULL ) {
636 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
637 child->setDoubleValue( "dewpoint-sea-level-degc",
638 dewpoint_sea_level_degc );
644 node = fgGetNode( "/environment/config/aloft" );
645 if ( node != NULL ) {
647 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
648 child->setDoubleValue( "dewpoint-sea-level-degc",
649 dewpoint_sea_level_degc );
659 * Set the outside air dewpoint at the "current" altitude by first
660 * calculating the corresponding sea level dewpoint, and assigning
661 * that to all boundary and aloft environment layers.
664 do_set_dewpoint_degc (const SGPropertyNode * arg)
666 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
668 // check for an altitude specified in the arguments, otherwise use
669 // current aircraft altitude.
670 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
671 if ( altitude_ft == NULL ) {
672 altitude_ft = fgGetNode("/position/altitude-ft");
675 FGEnvironment dummy; // instantiate a dummy so we can leech a method
676 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
677 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
678 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
680 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
681 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
682 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
684 SGPropertyNode *node, *child;
687 node = fgGetNode( "/environment/config/boundary" );
688 if ( node != NULL ) {
690 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
691 child->setDoubleValue( "dewpoint-sea-level-degc",
692 dewpoint_sea_level_degc );
698 node = fgGetNode( "/environment/config/aloft" );
699 if ( node != NULL ) {
701 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
702 child->setDoubleValue( "dewpoint-sea-level-degc",
703 dewpoint_sea_level_degc );
712 * Update the lighting manually.
715 do_timeofday (const SGPropertyNode * arg)
717 const string &offset_type = arg->getStringValue("timeofday", "noon");
719 static const SGPropertyNode *longitude
720 = fgGetNode("/position/longitude-deg");
721 static const SGPropertyNode *latitude
722 = fgGetNode("/position/latitude-deg");
723 static const SGPropertyNode *cur_time_override
724 = fgGetNode("/sim/time/cur-time-override", true);
726 int orig_warp = globals->get_warp();
727 SGTime *t = globals->get_time_params();
728 time_t cur_time = t->get_cur_time();
729 // cout << "cur_time = " << cur_time << endl;
730 // cout << "orig_warp = " << orig_warp << endl;
733 if ( offset_type == "real" ) {
735 } else if ( offset_type == "dawn" ) {
736 warp = fgTimeSecondsUntilSunAngle( cur_time,
737 longitude->getDoubleValue()
738 * SGD_DEGREES_TO_RADIANS,
739 latitude->getDoubleValue()
740 * SGD_DEGREES_TO_RADIANS,
742 } else if ( offset_type == "morning" ) {
743 warp = fgTimeSecondsUntilSunAngle( cur_time,
744 longitude->getDoubleValue()
745 * SGD_DEGREES_TO_RADIANS,
746 latitude->getDoubleValue()
747 * SGD_DEGREES_TO_RADIANS,
749 } else if ( offset_type == "noon" ) {
750 warp = fgTimeSecondsUntilSunAngle( cur_time,
751 longitude->getDoubleValue()
752 * SGD_DEGREES_TO_RADIANS,
753 latitude->getDoubleValue()
754 * SGD_DEGREES_TO_RADIANS,
756 } else if ( offset_type == "afternoon" ) {
757 warp = fgTimeSecondsUntilSunAngle( cur_time,
758 longitude->getDoubleValue()
759 * SGD_DEGREES_TO_RADIANS,
760 latitude->getDoubleValue()
761 * SGD_DEGREES_TO_RADIANS,
763 } else if ( offset_type == "dusk" ) {
764 warp = fgTimeSecondsUntilSunAngle( cur_time,
765 longitude->getDoubleValue()
766 * SGD_DEGREES_TO_RADIANS,
767 latitude->getDoubleValue()
768 * SGD_DEGREES_TO_RADIANS,
770 } else if ( offset_type == "evening" ) {
771 warp = fgTimeSecondsUntilSunAngle( cur_time,
772 longitude->getDoubleValue()
773 * SGD_DEGREES_TO_RADIANS,
774 latitude->getDoubleValue()
775 * SGD_DEGREES_TO_RADIANS,
777 } else if ( offset_type == "midnight" ) {
778 warp = fgTimeSecondsUntilSunAngle( cur_time,
779 longitude->getDoubleValue()
780 * SGD_DEGREES_TO_RADIANS,
781 latitude->getDoubleValue()
782 * SGD_DEGREES_TO_RADIANS,
785 // cout << "warp = " << warp << endl;
786 globals->set_warp( orig_warp + warp );
788 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
789 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
790 cur_time_override->getLongValue(),
791 globals->get_warp() );
798 * Built-in command: toggle a bool property value.
800 * property: The name of the property to toggle.
803 do_property_toggle (const SGPropertyNode * arg)
805 SGPropertyNode * prop = get_prop(arg);
806 return prop->setBoolValue(!prop->getBoolValue());
811 * Built-in command: assign a value to a property.
813 * property: the name of the property to assign.
814 * value: the value to assign; or
815 * property[1]: the property to copy from.
818 do_property_assign (const SGPropertyNode * arg)
820 SGPropertyNode * prop = get_prop(arg);
821 const SGPropertyNode * prop2 = get_prop2(arg);
822 const SGPropertyNode * value = arg->getNode("value");
825 return prop->setUnspecifiedValue(value->getStringValue());
827 return prop->setUnspecifiedValue(prop2->getStringValue());
834 * Built-in command: increment or decrement a property value.
836 * If the 'step' argument is present, it will be used; otherwise,
837 * the command uses 'offset' and 'factor', usually from the mouse.
839 * property: the name of the property to increment or decrement.
840 * step: the amount of the increment or decrement (default: 0).
841 * offset: offset from the current setting (used for the mouse; multiplied
843 * factor: scaling amount for the offset (defaults to 1).
844 * min: the minimum allowed value (default: no minimum).
845 * max: the maximum allowed value (default: no maximum).
846 * mask: 'integer' to apply only to the left of the decimal point,
847 * 'decimal' to apply only to the right of the decimal point,
848 * or 'all' to apply to the whole number (the default).
849 * wrap: true if the value should be wrapped when it passes min or max;
850 * both min and max must be present for this to work (default:
854 do_property_adjust (const SGPropertyNode * arg)
856 SGPropertyNode * prop = get_prop(arg);
859 if (arg->hasValue("step"))
860 amount = arg->getDoubleValue("step");
862 amount = (arg->getDoubleValue("factor", 1)
863 * arg->getDoubleValue("offset"));
865 double unmodifiable, modifiable;
866 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
867 &unmodifiable, &modifiable);
868 modifiable += amount;
869 limit_value(&modifiable, arg);
871 prop->setDoubleValue(unmodifiable + modifiable);
878 * Built-in command: multiply a property value.
880 * property: the name of the property to multiply.
881 * factor: the amount by which to multiply.
882 * min: the minimum allowed value (default: no minimum).
883 * max: the maximum allowed value (default: no maximum).
884 * mask: 'integer' to apply only to the left of the decimal point,
885 * 'decimal' to apply only to the right of the decimal point,
886 * or 'all' to apply to the whole number (the default).
887 * wrap: true if the value should be wrapped when it passes min or max;
888 * both min and max must be present for this to work (default:
892 do_property_multiply (const SGPropertyNode * arg)
894 SGPropertyNode * prop = get_prop(arg);
895 double factor = arg->getDoubleValue("factor", 1);
897 double unmodifiable, modifiable;
898 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
899 &unmodifiable, &modifiable);
900 modifiable *= factor;
901 limit_value(&modifiable, arg);
903 prop->setDoubleValue(unmodifiable + modifiable);
910 * Built-in command: swap two property values.
912 * property[0]: the name of the first property.
913 * property[1]: the name of the second property.
916 do_property_swap (const SGPropertyNode * arg)
918 SGPropertyNode * prop1 = get_prop(arg);
919 SGPropertyNode * prop2 = get_prop2(arg);
921 // FIXME: inefficient
922 const string & tmp = prop1->getStringValue();
923 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
924 prop2->setUnspecifiedValue(tmp.c_str()));
929 * Built-in command: Set a property to an axis or other moving input.
931 * property: the name of the property to set.
932 * setting: the current input setting, usually between -1.0 and 1.0.
933 * offset: the offset to shift by, before applying the factor.
934 * factor: the factor to multiply by (use negative to reverse).
937 do_property_scale (const SGPropertyNode * arg)
939 SGPropertyNode * prop = get_prop(arg);
940 double setting = arg->getDoubleValue("setting");
941 double offset = arg->getDoubleValue("offset", 0.0);
942 double factor = arg->getDoubleValue("factor", 1.0);
943 bool squared = arg->getBoolValue("squared", false);
944 int power = arg->getIntValue("power", (squared ? 2 : 1));
946 int sign = (setting < 0 ? -1 : 1);
952 setting = setting * setting * sign;
955 setting = setting * setting * setting;
958 setting = setting * setting * setting * setting * sign;
961 setting = pow(setting, power);
962 if ((power % 2) == 0)
967 return prop->setDoubleValue((setting + offset) * factor);
972 * Built-in command: cycle a property through a set of values.
974 * If the current value isn't in the list, the cycle will
975 * (re)start from the beginning.
977 * property: the name of the property to cycle.
978 * value[*]: the list of values to cycle through.
981 do_property_cycle (const SGPropertyNode * arg)
983 SGPropertyNode * prop = get_prop(arg);
984 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
986 int nSelections = values.size();
988 if (nSelections < 1) {
989 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
993 // Try to find the current selection
994 for (int i = 0; i < nSelections; i++) {
995 if (compare_values(prop, values[i])) {
1001 // Default or wrap to the first selection
1002 if (selection < 0 || selection >= nSelections)
1005 prop->setUnspecifiedValue(values[selection]->getStringValue());
1011 * Built-in command: randomize a numeric property value.
1013 * property: the name of the property value to randomize.
1014 * min: the minimum allowed value.
1015 * max: the maximum allowed value.
1018 do_property_randomize (const SGPropertyNode * arg)
1020 SGPropertyNode * prop = get_prop(arg);
1021 double min = arg->getDoubleValue("min", DBL_MIN);
1022 double max = arg->getDoubleValue("max", DBL_MAX);
1023 prop->setDoubleValue(sg_random() * (max - min) + min);
1029 * Built-in command: reinit the data logging system based on the
1030 * current contents of the /logger tree.
1033 do_data_logging_commit (const SGPropertyNode * arg)
1035 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1041 * Built-in command: Add a dialog to the GUI system. Does *not*
1042 * display the dialog. The property node should have the same format
1043 * as a dialog XML configuration. It must include:
1045 * name: the name of the GUI dialog for future reference.
1048 do_dialog_new (const SGPropertyNode * arg)
1050 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1052 // Note the casting away of const: this is *real*. Doing a
1053 // "dialog-apply" command later on will mutate this property node.
1054 // I'm not convinced that this isn't the Right Thing though; it
1055 // allows client to create a node, pass it to dialog-new, and get
1056 // the values back from the dialog by reading the same node.
1057 // Perhaps command arguments are not as "const" as they would
1059 gui->newDialog((SGPropertyNode*)arg);
1064 * Built-in command: Show an XML-configured dialog.
1066 * dialog-name: the name of the GUI dialog to display.
1069 do_dialog_show (const SGPropertyNode * arg)
1071 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1072 gui->showDialog(arg->getStringValue("dialog-name"));
1078 * Built-in Command: Hide the active XML-configured dialog.
1081 do_dialog_close (const SGPropertyNode * arg)
1083 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1084 if(arg->hasValue("dialog-name"))
1085 return gui->closeDialog(arg->getStringValue("dialog-name"));
1086 return gui->closeActiveDialog();
1091 * Update a value in the active XML-configured dialog.
1093 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1096 do_dialog_update (const SGPropertyNode * arg)
1098 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1100 if (arg->hasValue("dialog-name"))
1101 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1103 dialog = gui->getActiveDialog();
1106 dialog->updateValues(arg->getStringValue("object-name"));
1115 * Apply a value in the active XML-configured dialog.
1117 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1120 do_dialog_apply (const SGPropertyNode * arg)
1122 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1124 if (arg->hasValue("dialog-name"))
1125 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1127 dialog = gui->getActiveDialog();
1130 dialog->applyValues(arg->getStringValue("object-name"));
1139 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1143 do_gui_redraw (const SGPropertyNode * arg)
1145 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1151 * Built-in command: play an audio message (i.e. a wav file) This is
1152 * fire and forget. Call this once per message and it will get dumped
1153 * into a queue. Messages are played sequentially so they do not
1157 do_play_audio_sample (const SGPropertyNode * arg)
1159 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1160 string path = arg->getStringValue("path");
1161 string file = arg->getStringValue("file");
1162 double volume = arg->getDoubleValue("volume");
1163 // cout << "playing " << path << " / " << file << endl;
1165 fx->play_message( path, file, volume );
1168 } catch (const sg_io_exception& e) {
1169 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1170 "failed to load" << path << '/' << file);
1176 * Built-in command: commit presets (read from in /sim/presets/)
1179 do_presets_commit (const SGPropertyNode * arg)
1181 // unbind the current fdm state so property changes
1182 // don't get lost when we subsequently delete this fdm
1183 // and create a new one.
1184 cur_fdm_state->unbind();
1186 // set position from presets
1189 fgReInitSubsystems();
1191 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1194 if ( ! fgGetBool("/sim/presets/onground") ) {
1195 fgSetBool( "/sim/freeze/master", true );
1196 fgSetBool( "/sim/freeze/clock", true );
1204 * Built-in command: set log level (0 ... 7)
1207 do_log_level (const SGPropertyNode * arg)
1209 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1215 * Built-in command: replay the FDR buffer
1218 do_replay (const SGPropertyNode * arg)
1220 // freeze the master fdm
1221 fgSetInt( "/sim/freeze/replay-state", 1 );
1223 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1225 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1226 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1227 double duration = fgGetDouble( "/sim/replay/duration" );
1228 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1229 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1231 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1234 // cout << "start = " << r->get_start_time()
1235 // << " end = " << r->get_end_time() << endl;
1242 * Return terrain elevation for given longitude/latitude pair.
1245 do_terrain_elevation (const SGPropertyNode * arg)
1247 SGPropertyNode *a = const_cast<SGPropertyNode *>(arg);
1248 const SGMaterial *mat;
1251 double lon = a->getDoubleValue("longitude-deg", 0.0);
1252 double lat = a->getDoubleValue("latitude-deg", 0.0);
1253 bool ret = globals->get_scenery()->get_elevation_m(lat, lon, 10000.0, elev, &mat);
1256 const char *matname = "";
1258 solid = mat->get_solid();
1259 const vector<string> names = mat->get_names();
1261 matname = names[0].c_str();
1263 a->setBoolValue("solid", solid);
1264 a->setStringValue("material", matname);
1265 a->setDoubleValue("elevation-m", elev);
1271 do_decrease_visibility (const SGPropertyNode * arg)
1273 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1274 fgSetDouble("/environment/visibility-m", new_value);
1275 fgDefaultWeatherValue("visibility-m", new_value);
1276 globals->get_subsystem("environment")->reinit();
1282 do_increase_visibility (const SGPropertyNode * arg)
1284 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1285 fgSetDouble("/environment/visibility-m", new_value);
1286 fgDefaultWeatherValue("visibility-m", new_value);
1287 globals->get_subsystem("environment")->reinit();
1293 do_hud_init(const SGPropertyNode *)
1295 fgHUDInit(0); // minimal HUD
1300 do_hud_init2(const SGPropertyNode *)
1302 fgHUDInit2(0); // normal HUD
1307 * An fgcommand to allow loading of xml files via nasal,
1308 * the xml file's structure will be made available within
1309 * a (definable) property tree node
1311 * @param filename a string to hold the complete path & filename of a XML file
1312 * @param targetnode a string pointing to a location within the property tree
1313 * where to store the parsed XML file
1317 do_load_xml_to_proptree(const SGPropertyNode * node)
1319 // SG_LOG(SG_GENERAL, SG_ALERT, "fgcommand loadxml executed");
1321 SGPropertyNode * targetnode;
1322 targetnode = fgGetNode(node->getNode("targetnode")->getStringValue(),true);
1324 const char *filename = node->getNode("filename")->getStringValue();
1326 fgLoadProps(filename, targetnode);
1327 } catch (const sg_exception &e) {
1328 string errmsg = "Error reading file " + string(filename) + ":\n";
1329 guiErrorMessage(errmsg.c_str(), e);
1338 * an fgcommand to allow saving of xml files via nasal,
1339 * the file's structure will be determined based on what's
1340 * encountered in the passed (source) property tree node
1342 * @param filename a string to hold the complete path & filename of the (new)
1344 * @param sourcenode a string pointing to a location within the property tree
1345 * where to find the nodes that should be written recursively into an XML file
1348 * deal with already existing filenames, optionally return error/success
1349 * values in a separate node to provide status information
1351 * note: it's directly using writeProperties, which is not necessarily
1352 * preferable, but for now it should work ...
1356 do_save_xml_from_proptree(const SGPropertyNode * node)
1358 //TODO: do Parameter validation !
1359 SGPropertyNode * sourcenode;
1360 sourcenode = fgGetNode(node->getNode("sourcenode")->getStringValue(),true);
1362 const char *filename = node->getNode("filename")->getStringValue();
1364 writeProperties (filename, sourcenode, true);
1365 } catch (const sg_exception &e) {
1366 string errmsg = "Error writing file " + string(filename) + ":\n";
1367 guiErrorMessage(errmsg.c_str(), e);
1375 do_press_cockpit_button (const SGPropertyNode *arg)
1377 const char *prefix = arg->getStringValue("prefix");
1379 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1382 string prop = string(prefix) + "-button";
1385 if (arg->getBoolValue("latching"))
1386 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1390 fgSetDouble(prop.c_str(), value);
1391 fgSetBool(arg->getStringValue("discrete"), value > 0);
1397 do_release_cockpit_button (const SGPropertyNode *arg)
1399 const char *prefix = arg->getStringValue("prefix");
1401 if (arg->getBoolValue("guarded")) {
1402 string prop = string(prefix) + "-guard";
1403 if (fgGetDouble(prop.c_str()) < 1) {
1404 fgSetDouble(prop.c_str(), 1);
1409 if (! arg->getBoolValue("latching")) {
1410 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1411 fgSetBool(arg->getStringValue("discrete"), false);
1418 ////////////////////////////////////////////////////////////////////////
1420 ////////////////////////////////////////////////////////////////////////
1424 * Table of built-in commands.
1426 * New commands do not have to be added here; any module in the application
1427 * can add a new command using globals->get_commands()->addCommand(...).
1431 SGCommandMgr::command_t command;
1433 { "null", do_null },
1434 #if defined(HAVE_PLIB_PSL)
1435 { "script", do_script },
1436 #endif // HAVE_PLIB_PSL
1437 { "nasal", do_nasal },
1438 { "exit", do_exit },
1439 { "reinit", do_reinit },
1440 { "suspend", do_reinit },
1441 { "resume", do_reinit },
1442 { "load", do_load },
1443 { "save", do_save },
1444 { "panel-load", do_panel_load },
1445 { "panel-mouse-click", do_panel_mouse_click },
1446 { "preferences-load", do_preferences_load },
1447 { "view-cycle", do_view_cycle },
1448 { "screen-capture", do_screen_capture },
1449 { "hires-screen-capture", do_hires_screen_capture },
1450 { "tile-cache-reload", do_tile_cache_reload },
1451 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1452 { "set-outside-air-temp-degc", do_set_oat_degc },
1453 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1454 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1455 { "timeofday", do_timeofday },
1456 { "property-toggle", do_property_toggle },
1457 { "property-assign", do_property_assign },
1458 { "property-adjust", do_property_adjust },
1459 { "property-multiply", do_property_multiply },
1460 { "property-swap", do_property_swap },
1461 { "property-scale", do_property_scale },
1462 { "property-cycle", do_property_cycle },
1463 { "property-randomize", do_property_randomize },
1464 { "data-logging-commit", do_data_logging_commit },
1465 { "dialog-new", do_dialog_new },
1466 { "dialog-show", do_dialog_show },
1467 { "dialog-close", do_dialog_close },
1468 { "dialog-update", do_dialog_update },
1469 { "dialog-apply", do_dialog_apply },
1470 { "gui-redraw", do_gui_redraw },
1471 { "play-audio-sample", do_play_audio_sample },
1472 { "presets-commit", do_presets_commit },
1473 { "log-level", do_log_level },
1474 { "replay", do_replay },
1475 { "terrain-elevation", do_terrain_elevation },
1476 { "decrease-visibility", do_decrease_visibility },
1477 { "increase-visibility", do_increase_visibility },
1478 { "hud-init", do_hud_init },
1479 { "hud-init2", do_hud_init2 },
1480 { "loadxml", do_load_xml_to_proptree},
1481 { "savexml", do_save_xml_from_proptree },
1482 { "press-cockpit-button", do_press_cockpit_button },
1483 { "release-cockpit-button", do_release_cockpit_button },
1484 { 0, 0 } // zero-terminated
1489 * Initialize the default built-in commands.
1491 * Other commands may be added by other parts of the application.
1496 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1497 for (int i = 0; built_ins[i].name != 0; i++) {
1498 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1499 globals->get_commands()->addCommand(built_ins[i].name,
1500 built_ins[i].command);
1503 typedef bool (*dummy)();
1504 fgTie( "/command/view/next", dummy(0), do_view_next );
1505 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1508 // end of fg_commands.cxx