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 #if defined(HAVE_PLIB_PSL)
32 # include <Scripting/scriptmgr.hxx>
34 #include <Scripting/NasalSys.hxx>
35 #include <Sound/fg_fx.hxx>
36 #include <Time/sunsolver.hxx>
37 #include <Time/tmp.hxx>
39 #include "fg_init.hxx"
41 #include "fg_commands.hxx"
42 #include "fg_props.hxx"
43 #include "globals.hxx"
46 #include "viewmgr.hxx"
50 SG_USING_STD(ifstream);
51 SG_USING_STD(ofstream);
55 ////////////////////////////////////////////////////////////////////////
56 // Static helper functions.
57 ////////////////////////////////////////////////////////////////////////
60 static inline SGPropertyNode *
61 get_prop (const SGPropertyNode * arg)
63 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
66 static inline SGPropertyNode *
67 get_prop2 (const SGPropertyNode * arg)
69 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
74 * Get a double value and split it as required.
77 split_value (double full_value, const char * mask,
78 double * unmodifiable, double * modifiable)
80 if (!strcmp("integer", mask)) {
81 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
82 *unmodifiable = full_value - *modifiable;
83 } else if (!strcmp("decimal", mask)) {
84 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
85 *modifiable = full_value - *unmodifiable;
87 if (strcmp("all", mask))
88 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
89 << " assuming 'all'");
91 *modifiable = full_value;
97 * Clamp or wrap a value as specified.
100 limit_value (double * value, const SGPropertyNode * arg)
102 const SGPropertyNode * min_node = arg->getChild("min");
103 const SGPropertyNode * max_node = arg->getChild("max");
105 bool wrap = arg->getBoolValue("wrap");
107 if (min_node == 0 || max_node == 0)
110 if (wrap) { // wrap such that min <= x < max
111 double min_val = min_node->getDoubleValue();
112 double max_val = max_node->getDoubleValue();
113 double resolution = arg->getDoubleValue("resolution");
114 if (resolution > 0.0) {
115 // snap to (min + N*resolution), taking special care to handle imprecision
116 int n = (int)floor((*value - min_val) / resolution + 0.5);
117 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
118 SG_NORMALIZE_RANGE(n, 0, steps);
119 *value = min_val + resolution * n;
121 // plain circular wrapping
122 SG_NORMALIZE_RANGE(*value, min_val, max_val);
124 } else { // clamp such that min <= x <= max
125 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
126 *value = min_node->getDoubleValue();
127 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
128 *value = max_node->getDoubleValue();
133 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
135 switch (value1->getType()) {
136 case SGPropertyNode::BOOL:
137 return (value1->getBoolValue() == value2->getBoolValue());
138 case SGPropertyNode::INT:
139 return (value1->getIntValue() == value2->getIntValue());
140 case SGPropertyNode::LONG:
141 return (value1->getLongValue() == value2->getLongValue());
142 case SGPropertyNode::FLOAT:
143 return (value1->getFloatValue() == value2->getFloatValue());
144 case SGPropertyNode::DOUBLE:
145 return (value1->getDoubleValue() == value2->getDoubleValue());
147 return !strcmp(value1->getStringValue(), value2->getStringValue());
153 ////////////////////////////////////////////////////////////////////////
154 // Command implementations.
155 ////////////////////////////////////////////////////////////////////////
159 * Built-in command: do nothing.
162 do_null (const SGPropertyNode * arg)
167 #if defined(HAVE_PLIB_PSL)
169 * Built-in command: run a PSL script.
171 * script: the PSL script to execute
174 do_script (const SGPropertyNode * arg)
176 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
177 return mgr->run(arg->getStringValue("script"));
179 #endif // HAVE_PLIB_PSL
182 * Built-in command: run a Nasal script.
185 do_nasal (const SGPropertyNode * arg)
187 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
191 * Built-in command: exit FlightGear.
193 * status: the exit status to return to the operating system (defaults to 0)
196 do_exit (const SGPropertyNode * arg)
198 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
199 fgSetBool("/sim/signals/exit", true);
201 if (fgGetBool("/sim/startup/save-on-exit")) {
203 char* envp = ::getenv( "APPDATA" );
204 if ( envp != NULL ) {
205 SGPath config( envp );
206 config.append( "flightgear.org" );
208 if ( homedir != NULL ) {
209 SGPath config( homedir );
210 config.append( ".fgfs" );
212 config.append( "autosave.xml" );
213 config.create_dir( 0700 );
214 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
216 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
217 } catch (const sg_exception &e) {
218 guiErrorMessage("Error writing autosave.xml: ", e);
221 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
224 fgExit(arg->getIntValue("status", 0));
230 * Built-in command: reinitialize one or more subsystems.
232 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
233 * none is specified, reinitialize all of them.
236 do_reinit (const SGPropertyNode * arg)
240 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
241 if (subsystems.size() == 0) {
242 globals->get_subsystem_mgr()->reinit();
244 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
245 const char * name = subsystems[i]->getStringValue();
246 SGSubsystem * subsystem = globals->get_subsystem(name);
247 if (subsystem == 0) {
249 SG_LOG( SG_GENERAL, SG_ALERT,
250 "Subsystem " << name << "not found" );
257 globals->get_event_mgr()->reinit();
264 // these routines look useful ??? but are never used in the code ???
268 * Built-in command: suspend one or more subsystems.
270 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
273 do_suspend (const SGPropertyNode * arg)
277 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
278 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
279 const char * name = subsystems[i]->getStringValue();
280 SGSubsystem * subsystem = globals->get_subsystem(name);
281 if (subsystem == 0) {
283 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
285 subsystem->suspend();
292 * Built-in command: suspend one or more subsystems.
294 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
297 do_resume (const SGPropertyNode * arg)
301 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
302 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
303 const char * name = subsystems[i]->getStringValue();
304 SGSubsystem * subsystem = globals->get_subsystem(name);
305 if (subsystem == 0) {
307 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
319 * Built-in command: load flight.
321 * file (optional): the name of the file to load (relative to current
322 * directory). Defaults to "fgfs.sav"
325 do_load (const SGPropertyNode * arg)
327 const string &file = arg->getStringValue("file", "fgfs.sav");
328 ifstream input(file.c_str());
329 if (input.good() && fgLoadFlight(input)) {
331 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
334 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
341 * Built-in command: save flight.
343 * file (optional): the name of the file to save (relative to the
344 * current directory). Defaults to "fgfs.sav".
347 do_save (const SGPropertyNode * arg)
349 const string &file = arg->getStringValue("file", "fgfs.sav");
350 bool write_all = arg->getBoolValue("write-all", false);
351 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
352 ofstream output(file.c_str());
353 if (output.good() && fgSaveFlight(output, write_all)) {
355 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
358 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
365 * Built-in command: (re)load the panel.
367 * path (optional): the file name to load the panel from
368 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
369 * and if that's unspecified, to "Panels/Default/default.xml".
372 do_panel_load (const SGPropertyNode * arg)
375 arg->getStringValue("path",
376 fgGetString("/sim/panel/path",
377 "Panels/Default/default.xml"));
378 FGPanel * new_panel = fgReadPanel(panel_path);
379 if (new_panel == 0) {
380 SG_LOG(SG_INPUT, SG_ALERT,
381 "Error reading new panel from " << panel_path);
384 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
385 globals->get_current_panel()->unbind();
386 delete globals->get_current_panel();
387 globals->set_current_panel( new_panel );
388 globals->get_current_panel()->bind();
394 * Built-in command: pass a mouse click to the panel.
396 * button: the mouse button number, zero-based.
397 * is-down: true if the button is down, false if it is up.
398 * x-pos: the x position of the mouse click.
399 * y-pos: the y position of the mouse click.
402 do_panel_mouse_click (const SGPropertyNode * arg)
404 if (globals->get_current_panel() != 0)
405 return globals->get_current_panel()
406 ->doMouseAction(arg->getIntValue("button"),
407 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
408 arg->getIntValue("x-pos"),
409 arg->getIntValue("y-pos"));
416 * Built-in command: (re)load preferences.
418 * path (optional): the file name to load the panel from (relative
419 * to FG_ROOT). Defaults to "preferences.xml".
422 do_preferences_load (const SGPropertyNode * arg)
425 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
426 globals->get_props());
427 } catch (const sg_exception &e) {
428 guiErrorMessage("Error reading global preferences: ", e);
431 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
439 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
440 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
441 if ( globals->get_viewmgr()->get_current() == 1 ) {
442 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
450 globals->get_current_view()->setHeadingOffset_deg(0.0);
451 globals->get_viewmgr()->next_view();
452 fix_hud_visibility();
453 globals->get_tile_mgr()->refresh_view_timestamps();
459 globals->get_current_view()->setHeadingOffset_deg(0.0);
460 globals->get_viewmgr()->prev_view();
461 fix_hud_visibility();
462 globals->get_tile_mgr()->refresh_view_timestamps();
466 * Built-in command: cycle view.
469 do_view_cycle (const SGPropertyNode * arg)
471 globals->get_current_view()->setHeadingOffset_deg(0.0);
472 globals->get_viewmgr()->next_view();
473 fix_hud_visibility();
474 globals->get_tile_mgr()->refresh_view_timestamps();
475 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
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);
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!" );
526 fgSetBool("/sim/freeze/master", false);
533 * Set the sea level outside air temperature and assigning that to all
534 * boundary and aloft environment layers.
537 do_set_sea_level_degc (const SGPropertyNode * arg)
539 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
541 SGPropertyNode *node, *child;
544 node = fgGetNode( "/environment/config/boundary" );
545 if ( node != NULL ) {
547 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
548 child->setDoubleValue( "temperature-sea-level-degc",
549 temp_sea_level_degc );
555 node = fgGetNode( "/environment/config/aloft" );
556 if ( node != NULL ) {
558 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
559 child->setDoubleValue( "temperature-sea-level-degc",
560 temp_sea_level_degc );
570 * Set the outside air temperature at the "current" altitude by first
571 * calculating the corresponding sea level temp, and assigning that to
572 * all boundary and aloft environment layers.
575 do_set_oat_degc (const SGPropertyNode * arg)
577 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
579 // check for an altitude specified in the arguments, otherwise use
580 // current aircraft altitude.
581 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
582 if ( altitude_ft == NULL ) {
583 altitude_ft = fgGetNode("/position/altitude-ft");
586 FGEnvironment dummy; // instantiate a dummy so we can leech a method
587 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
588 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
589 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
591 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
592 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
593 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
595 SGPropertyNode *node, *child;
598 node = fgGetNode( "/environment/config/boundary" );
599 if ( node != NULL ) {
601 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
602 child->setDoubleValue( "temperature-sea-level-degc",
603 temp_sea_level_degc );
609 node = fgGetNode( "/environment/config/aloft" );
610 if ( node != NULL ) {
612 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
613 child->setDoubleValue( "temperature-sea-level-degc",
614 temp_sea_level_degc );
623 * Set the sea level outside air dewpoint and assigning that to all
624 * boundary and aloft environment layers.
627 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
629 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
631 SGPropertyNode *node, *child;
634 node = fgGetNode( "/environment/config/boundary" );
635 if ( node != NULL ) {
637 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
638 child->setDoubleValue( "dewpoint-sea-level-degc",
639 dewpoint_sea_level_degc );
645 node = fgGetNode( "/environment/config/aloft" );
646 if ( node != NULL ) {
648 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
649 child->setDoubleValue( "dewpoint-sea-level-degc",
650 dewpoint_sea_level_degc );
660 * Set the outside air dewpoint at the "current" altitude by first
661 * calculating the corresponding sea level dewpoint, and assigning
662 * that to all boundary and aloft environment layers.
665 do_set_dewpoint_degc (const SGPropertyNode * arg)
667 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
669 // check for an altitude specified in the arguments, otherwise use
670 // current aircraft altitude.
671 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
672 if ( altitude_ft == NULL ) {
673 altitude_ft = fgGetNode("/position/altitude-ft");
676 FGEnvironment dummy; // instantiate a dummy so we can leech a method
677 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
678 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
679 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
681 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
682 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
683 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
685 SGPropertyNode *node, *child;
688 node = fgGetNode( "/environment/config/boundary" );
689 if ( node != NULL ) {
691 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
692 child->setDoubleValue( "dewpoint-sea-level-degc",
693 dewpoint_sea_level_degc );
699 node = fgGetNode( "/environment/config/aloft" );
700 if ( node != NULL ) {
702 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
703 child->setDoubleValue( "dewpoint-sea-level-degc",
704 dewpoint_sea_level_degc );
713 * Update the lighting manually.
716 do_timeofday (const SGPropertyNode * arg)
718 const string &offset_type = arg->getStringValue("timeofday", "noon");
720 static const SGPropertyNode *longitude
721 = fgGetNode("/position/longitude-deg");
722 static const SGPropertyNode *latitude
723 = fgGetNode("/position/latitude-deg");
724 static const SGPropertyNode *cur_time_override
725 = fgGetNode("/sim/time/cur-time-override", true);
727 int orig_warp = globals->get_warp();
728 SGTime *t = globals->get_time_params();
729 time_t cur_time = t->get_cur_time();
730 // cout << "cur_time = " << cur_time << endl;
731 // cout << "orig_warp = " << orig_warp << endl;
734 if ( offset_type == "real" ) {
736 } else if ( offset_type == "dawn" ) {
737 warp = fgTimeSecondsUntilSunAngle( cur_time,
738 longitude->getDoubleValue()
739 * SGD_DEGREES_TO_RADIANS,
740 latitude->getDoubleValue()
741 * SGD_DEGREES_TO_RADIANS,
743 } else if ( offset_type == "morning" ) {
744 warp = fgTimeSecondsUntilSunAngle( cur_time,
745 longitude->getDoubleValue()
746 * SGD_DEGREES_TO_RADIANS,
747 latitude->getDoubleValue()
748 * SGD_DEGREES_TO_RADIANS,
750 } else if ( offset_type == "noon" ) {
751 warp = fgTimeSecondsUntilSunAngle( cur_time,
752 longitude->getDoubleValue()
753 * SGD_DEGREES_TO_RADIANS,
754 latitude->getDoubleValue()
755 * SGD_DEGREES_TO_RADIANS,
757 } else if ( offset_type == "afternoon" ) {
758 warp = fgTimeSecondsUntilSunAngle( cur_time,
759 longitude->getDoubleValue()
760 * SGD_DEGREES_TO_RADIANS,
761 latitude->getDoubleValue()
762 * SGD_DEGREES_TO_RADIANS,
764 } else if ( offset_type == "dusk" ) {
765 warp = fgTimeSecondsUntilSunAngle( cur_time,
766 longitude->getDoubleValue()
767 * SGD_DEGREES_TO_RADIANS,
768 latitude->getDoubleValue()
769 * SGD_DEGREES_TO_RADIANS,
771 } else if ( offset_type == "evening" ) {
772 warp = fgTimeSecondsUntilSunAngle( cur_time,
773 longitude->getDoubleValue()
774 * SGD_DEGREES_TO_RADIANS,
775 latitude->getDoubleValue()
776 * SGD_DEGREES_TO_RADIANS,
778 } else if ( offset_type == "midnight" ) {
779 warp = fgTimeSecondsUntilSunAngle( cur_time,
780 longitude->getDoubleValue()
781 * SGD_DEGREES_TO_RADIANS,
782 latitude->getDoubleValue()
783 * SGD_DEGREES_TO_RADIANS,
786 // cout << "warp = " << warp << endl;
787 globals->set_warp( orig_warp + warp );
789 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
790 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
791 cur_time_override->getLongValue(),
792 globals->get_warp() );
799 * Built-in command: toggle a bool property value.
801 * property: The name of the property to toggle.
804 do_property_toggle (const SGPropertyNode * arg)
806 SGPropertyNode * prop = get_prop(arg);
807 return prop->setBoolValue(!prop->getBoolValue());
812 * Built-in command: assign a value to a property.
814 * property: the name of the property to assign.
815 * value: the value to assign; or
816 * property[1]: the property to copy from.
819 do_property_assign (const SGPropertyNode * arg)
821 SGPropertyNode * prop = get_prop(arg);
822 const SGPropertyNode * prop2 = get_prop2(arg);
823 const SGPropertyNode * value = arg->getNode("value");
826 return prop->setUnspecifiedValue(value->getStringValue());
828 return prop->setUnspecifiedValue(prop2->getStringValue());
835 * Built-in command: increment or decrement a property value.
837 * If the 'step' argument is present, it will be used; otherwise,
838 * the command uses 'offset' and 'factor', usually from the mouse.
840 * property: the name of the property to increment or decrement.
841 * step: the amount of the increment or decrement (default: 0).
842 * offset: offset from the current setting (used for the mouse; multiplied
844 * factor: scaling amount for the offset (defaults to 1).
845 * min: the minimum allowed value (default: no minimum).
846 * max: the maximum allowed value (default: no maximum).
847 * mask: 'integer' to apply only to the left of the decimal point,
848 * 'decimal' to apply only to the right of the decimal point,
849 * or 'all' to apply to the whole number (the default).
850 * wrap: true if the value should be wrapped when it passes min or max;
851 * both min and max must be present for this to work (default:
855 do_property_adjust (const SGPropertyNode * arg)
857 SGPropertyNode * prop = get_prop(arg);
860 if (arg->hasValue("step"))
861 amount = arg->getDoubleValue("step");
863 amount = (arg->getDoubleValue("factor", 1)
864 * arg->getDoubleValue("offset"));
866 double unmodifiable, modifiable;
867 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
868 &unmodifiable, &modifiable);
869 modifiable += amount;
870 limit_value(&modifiable, arg);
872 prop->setDoubleValue(unmodifiable + modifiable);
879 * Built-in command: multiply a property value.
881 * property: the name of the property to multiply.
882 * factor: the amount by which to multiply.
883 * min: the minimum allowed value (default: no minimum).
884 * max: the maximum allowed value (default: no maximum).
885 * mask: 'integer' to apply only to the left of the decimal point,
886 * 'decimal' to apply only to the right of the decimal point,
887 * or 'all' to apply to the whole number (the default).
888 * wrap: true if the value should be wrapped when it passes min or max;
889 * both min and max must be present for this to work (default:
893 do_property_multiply (const SGPropertyNode * arg)
895 SGPropertyNode * prop = get_prop(arg);
896 double factor = arg->getDoubleValue("factor", 1);
898 double unmodifiable, modifiable;
899 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
900 &unmodifiable, &modifiable);
901 modifiable *= factor;
902 limit_value(&modifiable, arg);
904 prop->setDoubleValue(unmodifiable + modifiable);
911 * Built-in command: swap two property values.
913 * property[0]: the name of the first property.
914 * property[1]: the name of the second property.
917 do_property_swap (const SGPropertyNode * arg)
919 SGPropertyNode * prop1 = get_prop(arg);
920 SGPropertyNode * prop2 = get_prop2(arg);
922 // FIXME: inefficient
923 const string & tmp = prop1->getStringValue();
924 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
925 prop2->setUnspecifiedValue(tmp.c_str()));
930 * Built-in command: Set a property to an axis or other moving input.
932 * property: the name of the property to set.
933 * setting: the current input setting, usually between -1.0 and 1.0.
934 * offset: the offset to shift by, before applying the factor.
935 * factor: the factor to multiply by (use negative to reverse).
938 do_property_scale (const SGPropertyNode * arg)
940 SGPropertyNode * prop = get_prop(arg);
941 double setting = arg->getDoubleValue("setting");
942 double offset = arg->getDoubleValue("offset", 0.0);
943 double factor = arg->getDoubleValue("factor", 1.0);
944 bool squared = arg->getBoolValue("squared", false);
945 int power = arg->getIntValue("power", (squared ? 2 : 1));
947 int sign = (setting < 0 ? -1 : 1);
953 setting = setting * setting * sign;
956 setting = setting * setting * setting;
959 setting = setting * setting * setting * setting * sign;
962 setting = pow(setting, power);
963 if ((power % 2) == 0)
968 return prop->setDoubleValue((setting + offset) * factor);
973 * Built-in command: cycle a property through a set of values.
975 * If the current value isn't in the list, the cycle will
976 * (re)start from the beginning.
978 * property: the name of the property to cycle.
979 * value[*]: the list of values to cycle through.
982 do_property_cycle (const SGPropertyNode * arg)
984 SGPropertyNode * prop = get_prop(arg);
985 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
987 int nSelections = values.size();
989 if (nSelections < 1) {
990 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
994 // Try to find the current selection
995 for (int i = 0; i < nSelections; i++) {
996 if (compare_values(prop, values[i])) {
1002 // Default or wrap to the first selection
1003 if (selection < 0 || selection >= nSelections)
1006 prop->setUnspecifiedValue(values[selection]->getStringValue());
1012 * Built-in command: randomize a numeric property value.
1014 * property: the name of the property value to randomize.
1015 * min: the minimum allowed value.
1016 * max: the maximum allowed value.
1019 do_property_randomize (const SGPropertyNode * arg)
1021 SGPropertyNode * prop = get_prop(arg);
1022 double min = arg->getDoubleValue("min", DBL_MIN);
1023 double max = arg->getDoubleValue("max", DBL_MAX);
1024 prop->setDoubleValue(sg_random() * (max - min) + min);
1030 * Built-in command: reinit the data logging system based on the
1031 * current contents of the /logger tree.
1034 do_data_logging_commit (const SGPropertyNode * arg)
1036 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1042 * Built-in command: Add a dialog to the GUI system. Does *not*
1043 * display the dialog. The property node should have the same format
1044 * as a dialog XML configuration. It must include:
1046 * name: the name of the GUI dialog for future reference.
1049 do_dialog_new (const SGPropertyNode * arg)
1051 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1053 // Note the casting away of const: this is *real*. Doing a
1054 // "dialog-apply" command later on will mutate this property node.
1055 // I'm not convinced that this isn't the Right Thing though; it
1056 // allows client to create a node, pass it to dialog-new, and get
1057 // the values back from the dialog by reading the same node.
1058 // Perhaps command arguments are not as "const" as they would
1060 gui->newDialog((SGPropertyNode*)arg);
1065 * Built-in command: Show an XML-configured dialog.
1067 * dialog-name: the name of the GUI dialog to display.
1070 do_dialog_show (const SGPropertyNode * arg)
1072 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1073 gui->showDialog(arg->getStringValue("dialog-name"));
1079 * Built-in Command: Hide the active XML-configured dialog.
1082 do_dialog_close (const SGPropertyNode * arg)
1084 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1085 if(arg->hasValue("dialog-name"))
1086 return gui->closeDialog(arg->getStringValue("dialog-name"));
1087 return gui->closeActiveDialog();
1092 * Update a value in the active XML-configured dialog.
1094 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1097 do_dialog_update (const SGPropertyNode * arg)
1099 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1101 if (arg->hasValue("dialog-name"))
1102 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1104 dialog = gui->getActiveDialog();
1107 dialog->updateValues(arg->getStringValue("object-name"));
1116 * Apply a value in the active XML-configured dialog.
1118 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1121 do_dialog_apply (const SGPropertyNode * arg)
1123 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1125 if (arg->hasValue("dialog-name"))
1126 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1128 dialog = gui->getActiveDialog();
1131 dialog->applyValues(arg->getStringValue("object-name"));
1140 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1144 do_gui_redraw (const SGPropertyNode * arg)
1146 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1152 * Built-in command: play an audio message (i.e. a wav file) This is
1153 * fire and forget. Call this once per message and it will get dumped
1154 * into a queue. Messages are played sequentially so they do not
1158 do_play_audio_message (const SGPropertyNode * arg)
1160 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1161 string path = arg->getStringValue("path");
1162 string file = arg->getStringValue("file");
1163 // cout << "playing " << path << " / " << file << endl;
1164 fx->play_message( path, file );
1170 * Built-in command: commit presets (read from in /sim/presets/)
1173 do_presets_commit (const SGPropertyNode * arg)
1175 // unbind the current fdm state so property changes
1176 // don't get lost when we subsequently delete this fdm
1177 // and create a new one.
1178 cur_fdm_state->unbind();
1180 // set position from presets
1184 fgReInitSubsystems();
1186 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1189 if ( ! fgGetBool("/sim/presets/onground") ) {
1190 fgSetBool( "/sim/freeze/master", true );
1191 fgSetBool( "/sim/freeze/clock", true );
1199 * Built-in command: set log level (0 ... 7)
1202 do_log_level (const SGPropertyNode * arg)
1204 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1210 * Built-in command: replay the FDR buffer
1213 do_replay (const SGPropertyNode * arg)
1215 // freeze the master fdm
1216 fgSetInt( "/sim/freeze/replay-state", 1 );
1218 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1220 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1221 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1222 double duration = fgGetDouble( "/sim/replay/duration" );
1223 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1224 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1226 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1229 // cout << "start = " << r->get_start_time()
1230 // << " end = " << r->get_end_time() << endl;
1238 do_decrease_visibility (const SGPropertyNode * arg)
1240 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1241 fgSetDouble("/environment/visibility-m", new_value);
1242 fgDefaultWeatherValue("visibility-m", new_value);
1243 globals->get_subsystem("environment")->reinit();
1249 do_increase_visibility (const SGPropertyNode * arg)
1251 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1252 fgSetDouble("/environment/visibility-m", new_value);
1253 fgDefaultWeatherValue("visibility-m", new_value);
1254 globals->get_subsystem("environment")->reinit();
1260 do_hud_init(const SGPropertyNode *)
1262 fgHUDInit(0); // minimal HUD
1267 do_hud_init2(const SGPropertyNode *)
1269 fgHUDInit2(0); // normal HUD
1274 * An fgcommand to allow loading of xml files via nasal,
1275 * the xml file's structure will be made available within
1276 * a (definable) property tree node
1278 * @param filename a string to hold the complete path & filename of a XML file
1279 * @param targetnode a string pointing to a location within the property tree
1280 * where to store the parsed XML file
1284 do_load_xml_to_proptree(const SGPropertyNode * node)
1286 // SG_LOG(SG_GENERAL, SG_ALERT, "fgcommand loadxml executed");
1288 SGPropertyNode * targetnode;
1289 targetnode = fgGetNode(node->getNode("targetnode")->getStringValue(),true);
1291 const char *filename = node->getNode("filename")->getStringValue();
1293 fgLoadProps(filename, targetnode);
1294 } catch (const sg_exception &e) {
1295 string errmsg = "Error reading file " + string(filename) + ":\n";
1296 guiErrorMessage(errmsg.c_str(), e);
1305 * an fgcommand to allow saving of xml files via nasal,
1306 * the file's structure will be determined based on what's
1307 * encountered in the passed (source) property tree node
1309 * @param filename a string to hold the complete path & filename of the (new)
1311 * @param sourcenode a string pointing to a location within the property tree
1312 * where to find the nodes that should be written recursively into an XML file
1315 * deal with already existing filenames, optionally return error/success
1316 * values in a separate node to provide status information
1318 * note: it's directly using writeProperties, which is not necessarily
1319 * preferable, but for now it should work ...
1323 do_save_xml_from_proptree(const SGPropertyNode * node)
1325 //TODO: do Parameter validation !
1326 SGPropertyNode * sourcenode;
1327 sourcenode = fgGetNode(node->getNode("sourcenode")->getStringValue(),true);
1329 const char *filename = node->getNode("filename")->getStringValue();
1331 writeProperties (filename, sourcenode, true);
1332 } catch (const sg_exception &e) {
1333 string errmsg = "Error writing file " + string(filename) + ":\n";
1334 guiErrorMessage(errmsg.c_str(), e);
1342 do_press_cockpit_button (const SGPropertyNode *arg)
1344 const char *prefix = arg->getStringValue("prefix");
1346 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1349 string prop = string(prefix) + "-button";
1352 if (arg->getBoolValue("latching"))
1353 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1357 fgSetDouble(prop.c_str(), value);
1358 fgSetBool(arg->getStringValue("discrete"), value > 0);
1364 do_release_cockpit_button (const SGPropertyNode *arg)
1366 const char *prefix = arg->getStringValue("prefix");
1368 if (arg->getBoolValue("guarded")) {
1369 string prop = string(prefix) + "-guard";
1370 if (fgGetDouble(prop.c_str()) < 1) {
1371 fgSetDouble(prop.c_str(), 1);
1376 if (! arg->getBoolValue("latching")) {
1377 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1378 fgSetBool(arg->getStringValue("discrete"), false);
1385 ////////////////////////////////////////////////////////////////////////
1387 ////////////////////////////////////////////////////////////////////////
1391 * Table of built-in commands.
1393 * New commands do not have to be added here; any module in the application
1394 * can add a new command using globals->get_commands()->addCommand(...).
1398 SGCommandMgr::command_t command;
1400 { "null", do_null },
1401 #if defined(HAVE_PLIB_PSL)
1402 { "script", do_script },
1403 #endif // HAVE_PLIB_PSL
1404 { "nasal", do_nasal },
1405 { "exit", do_exit },
1406 { "reinit", do_reinit },
1407 { "suspend", do_reinit },
1408 { "resume", do_reinit },
1409 { "load", do_load },
1410 { "save", do_save },
1411 { "panel-load", do_panel_load },
1412 { "panel-mouse-click", do_panel_mouse_click },
1413 { "preferences-load", do_preferences_load },
1414 { "view-cycle", do_view_cycle },
1415 { "screen-capture", do_screen_capture },
1416 { "hires-screen-capture", do_hires_screen_capture },
1417 { "tile-cache-reload", do_tile_cache_reload },
1418 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1419 { "set-outside-air-temp-degc", do_set_oat_degc },
1420 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1421 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1422 { "timeofday", do_timeofday },
1423 { "property-toggle", do_property_toggle },
1424 { "property-assign", do_property_assign },
1425 { "property-adjust", do_property_adjust },
1426 { "property-multiply", do_property_multiply },
1427 { "property-swap", do_property_swap },
1428 { "property-scale", do_property_scale },
1429 { "property-cycle", do_property_cycle },
1430 { "property-randomize", do_property_randomize },
1431 { "data-logging-commit", do_data_logging_commit },
1432 { "dialog-new", do_dialog_new },
1433 { "dialog-show", do_dialog_show },
1434 { "dialog-close", do_dialog_close },
1435 { "dialog-update", do_dialog_update },
1436 { "dialog-apply", do_dialog_apply },
1437 { "gui-redraw", do_gui_redraw },
1438 { "play-audio-message", do_play_audio_message },
1439 { "presets-commit", do_presets_commit },
1440 { "log-level", do_log_level },
1441 { "replay", do_replay },
1442 { "decrease-visibility", do_decrease_visibility },
1443 { "increase-visibility", do_increase_visibility },
1444 { "hud-init", do_hud_init },
1445 { "hud-init2", do_hud_init2 },
1446 { "loadxml", do_load_xml_to_proptree},
1447 { "savexml", do_save_xml_from_proptree },
1448 { "press-cockpit-button", do_press_cockpit_button },
1449 { "release-cockpit-button", do_release_cockpit_button },
1450 { 0, 0 } // zero-terminated
1455 * Initialize the default built-in commands.
1457 * Other commands may be added by other parts of the application.
1462 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1463 for (int i = 0; built_ins[i].name != 0; i++) {
1464 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1465 globals->get_commands()->addCommand(built_ins[i].name,
1466 built_ins[i].command);
1469 typedef bool (*dummy)();
1470 fgTie( "/command/view/next", dummy(0), do_view_next );
1471 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1474 // end of fg_commands.cxx