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 <Replay/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"
44 SG_USING_STD(ifstream);
45 SG_USING_STD(ofstream);
49 ////////////////////////////////////////////////////////////////////////
50 // Static helper functions.
51 ////////////////////////////////////////////////////////////////////////
54 static inline SGPropertyNode *
55 get_prop (const SGPropertyNode * arg)
57 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
60 static inline SGPropertyNode *
61 get_prop2 (const SGPropertyNode * arg)
63 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
68 * Get a double value and split it as required.
71 split_value (double full_value, const char * mask,
72 double * unmodifiable, double * modifiable)
74 if (!strcmp("integer", mask)) {
75 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
76 *unmodifiable = full_value - *modifiable;
77 } else if (!strcmp("decimal", mask)) {
78 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
79 *modifiable = full_value - *unmodifiable;
81 if (strcmp("all", mask))
82 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
83 << " assuming 'all'");
85 *modifiable = full_value;
91 * Clamp or wrap a value as specified.
94 limit_value (double * value, const SGPropertyNode * arg)
96 const SGPropertyNode * min_node = arg->getChild("min");
97 const SGPropertyNode * max_node = arg->getChild("max");
99 bool wrap = arg->getBoolValue("wrap");
101 if (min_node == 0 || max_node == 0)
104 if (wrap) { // wrap such that min <= x < max
105 double min_val = min_node->getDoubleValue();
106 double max_val = max_node->getDoubleValue();
107 double resolution = arg->getDoubleValue("resolution");
108 if (resolution > 0.0) {
109 // snap to (min + N*resolution), taking special care to handle imprecision
110 int n = (int)floor((*value - min_val) / resolution + 0.5);
111 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
112 SG_NORMALIZE_RANGE(n, 0, steps);
113 *value = min_val + resolution * n;
115 // plain circular wrapping
116 SG_NORMALIZE_RANGE(*value, min_val, max_val);
118 } else { // clamp such that min <= x <= max
119 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
120 *value = min_node->getDoubleValue();
121 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
122 *value = max_node->getDoubleValue();
127 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
129 switch (value1->getType()) {
130 case SGPropertyNode::BOOL:
131 return (value1->getBoolValue() == value2->getBoolValue());
132 case SGPropertyNode::INT:
133 return (value1->getIntValue() == value2->getIntValue());
134 case SGPropertyNode::LONG:
135 return (value1->getLongValue() == value2->getLongValue());
136 case SGPropertyNode::FLOAT:
137 return (value1->getFloatValue() == value2->getFloatValue());
138 case SGPropertyNode::DOUBLE:
139 return (value1->getDoubleValue() == value2->getDoubleValue());
141 return !strcmp(value1->getStringValue(), value2->getStringValue());
147 ////////////////////////////////////////////////////////////////////////
148 // Command implementations.
149 ////////////////////////////////////////////////////////////////////////
153 * Built-in command: do nothing.
156 do_null (const SGPropertyNode * arg)
161 #if defined(HAVE_PLIB_PSL)
163 * Built-in command: run a PSL script.
165 * script: the PSL script to execute
168 do_script (const SGPropertyNode * arg)
170 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
171 return mgr->run(arg->getStringValue("script"));
173 #endif // HAVE_PLIB_PSL
176 * Built-in command: run a Nasal script.
179 do_nasal (const SGPropertyNode * arg)
181 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
185 * Built-in command: exit FlightGear.
187 * status: the exit status to return to the operating system (defaults to 0)
190 do_exit (const SGPropertyNode * arg)
192 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
193 fgExit(arg->getIntValue("status", 0));
199 * Built-in command: reinitialize one or more subsystems.
201 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
202 * none is specified, reinitialize all of them.
205 do_reinit (const SGPropertyNode * arg)
209 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
210 if (subsystems.size() == 0) {
211 globals->get_subsystem_mgr()->reinit();
213 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
214 const char * name = subsystems[i]->getStringValue();
215 SGSubsystem * subsystem = globals->get_subsystem(name);
216 if (subsystem == 0) {
218 SG_LOG( SG_GENERAL, SG_ALERT,
219 "Subsystem " << name << "not found" );
226 globals->get_event_mgr()->reinit();
233 // these routines look useful ??? but are never used in the code ???
237 * Built-in command: suspend one or more subsystems.
239 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
242 do_suspend (const SGPropertyNode * arg)
246 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
247 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
248 const char * name = subsystems[i]->getStringValue();
249 SGSubsystem * subsystem = globals->get_subsystem(name);
250 if (subsystem == 0) {
252 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
254 subsystem->suspend();
261 * Built-in command: suspend one or more subsystems.
263 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
266 do_resume (const SGPropertyNode * arg)
270 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
271 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
272 const char * name = subsystems[i]->getStringValue();
273 SGSubsystem * subsystem = globals->get_subsystem(name);
274 if (subsystem == 0) {
276 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
288 * Built-in command: load flight.
290 * file (optional): the name of the file to load (relative to current
291 * directory). Defaults to "fgfs.sav"
294 do_load (const SGPropertyNode * arg)
296 const string &file = arg->getStringValue("file", "fgfs.sav");
297 ifstream input(file.c_str());
298 if (input.good() && fgLoadFlight(input)) {
300 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
303 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
310 * Built-in command: save flight.
312 * file (optional): the name of the file to save (relative to the
313 * current directory). Defaults to "fgfs.sav".
316 do_save (const SGPropertyNode * arg)
318 const string &file = arg->getStringValue("file", "fgfs.sav");
319 bool write_all = arg->getBoolValue("write-all", false);
320 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
321 ofstream output(file.c_str());
322 if (output.good() && fgSaveFlight(output, write_all)) {
324 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
327 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
334 * Built-in command: (re)load the panel.
336 * path (optional): the file name to load the panel from
337 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
338 * and if that's unspecified, to "Panels/Default/default.xml".
341 do_panel_load (const SGPropertyNode * arg)
344 arg->getStringValue("path",
345 fgGetString("/sim/panel/path",
346 "Panels/Default/default.xml"));
347 FGPanel * new_panel = fgReadPanel(panel_path);
348 if (new_panel == 0) {
349 SG_LOG(SG_INPUT, SG_ALERT,
350 "Error reading new panel from " << panel_path);
353 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
354 globals->get_current_panel()->unbind();
355 delete globals->get_current_panel();
356 globals->set_current_panel( new_panel );
357 globals->get_current_panel()->bind();
363 * Built-in command: pass a mouse click to the panel.
365 * button: the mouse button number, zero-based.
366 * is-down: true if the button is down, false if it is up.
367 * x-pos: the x position of the mouse click.
368 * y-pos: the y position of the mouse click.
371 do_panel_mouse_click (const SGPropertyNode * arg)
373 if (globals->get_current_panel() != 0)
374 return globals->get_current_panel()
375 ->doMouseAction(arg->getIntValue("button"),
376 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
377 arg->getIntValue("x-pos"),
378 arg->getIntValue("y-pos"));
385 * Built-in command: (re)load preferences.
387 * path (optional): the file name to load the panel from (relative
388 * to FG_ROOT). Defaults to "preferences.xml".
391 do_preferences_load (const SGPropertyNode * arg)
394 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
395 globals->get_props());
396 } catch (const sg_exception &e) {
397 guiErrorMessage("Error reading global preferences: ", e);
400 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
408 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
409 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
410 if ( globals->get_viewmgr()->get_current() == 1 ) {
411 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
419 globals->get_current_view()->setHeadingOffset_deg(0.0);
420 globals->get_viewmgr()->next_view();
421 fix_hud_visibility();
422 globals->get_tile_mgr()->refresh_view_timestamps();
428 globals->get_current_view()->setHeadingOffset_deg(0.0);
429 globals->get_viewmgr()->prev_view();
430 fix_hud_visibility();
431 globals->get_tile_mgr()->refresh_view_timestamps();
435 * Built-in command: cycle view.
438 do_view_cycle (const SGPropertyNode * arg)
440 globals->get_current_view()->setHeadingOffset_deg(0.0);
441 globals->get_viewmgr()->next_view();
442 fix_hud_visibility();
443 globals->get_tile_mgr()->refresh_view_timestamps();
444 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
449 * Built-in command: capture screen.
452 do_screen_capture (const SGPropertyNode * arg)
460 * Built-in command: hires capture screen.
463 do_hires_screen_capture (const SGPropertyNode * arg)
471 * Reload the tile cache.
474 do_tile_cache_reload (const SGPropertyNode * arg)
476 static const SGPropertyNode *master_freeze
477 = fgGetNode("/sim/freeze/master");
478 bool freeze = master_freeze->getBoolValue();
479 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
481 fgSetBool("/sim/freeze/master", true);
484 if ( globals->get_tile_mgr()->init() ) {
485 // Load the local scenery data
486 double visibility_meters = fgGetDouble("/environment/visibility-m");
487 globals->get_tile_mgr()->update( visibility_meters );
489 SG_LOG( SG_GENERAL, SG_ALERT,
490 "Error in Tile Manager initialization!" );
495 fgSetBool("/sim/freeze/master", false);
502 * Set the sea level outside air temperature and assigning that to all
503 * boundary and aloft environment layers.
506 do_set_sea_level_degc (const SGPropertyNode * arg)
508 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
510 SGPropertyNode *node, *child;
513 node = fgGetNode( "/environment/config/boundary" );
514 if ( node != NULL ) {
516 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
517 child->setDoubleValue( "temperature-sea-level-degc",
518 temp_sea_level_degc );
524 node = fgGetNode( "/environment/config/aloft" );
525 if ( node != NULL ) {
527 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
528 child->setDoubleValue( "temperature-sea-level-degc",
529 temp_sea_level_degc );
539 * Set the outside air temperature at the "current" altitude by first
540 * calculating the corresponding sea level temp, and assigning that to
541 * all boundary and aloft environment layers.
544 do_set_oat_degc (const SGPropertyNode * arg)
546 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
548 // check for an altitude specified in the arguments, otherwise use
549 // current aircraft altitude.
550 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
551 if ( altitude_ft == NULL ) {
552 altitude_ft = fgGetNode("/position/altitude-ft");
555 FGEnvironment dummy; // instantiate a dummy so we can leech a method
556 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
557 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
558 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
560 cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
561 cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
562 cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
564 SGPropertyNode *node, *child;
567 node = fgGetNode( "/environment/config/boundary" );
568 if ( node != NULL ) {
570 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
571 child->setDoubleValue( "temperature-sea-level-degc",
572 temp_sea_level_degc );
578 node = fgGetNode( "/environment/config/aloft" );
579 if ( node != NULL ) {
581 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
582 child->setDoubleValue( "temperature-sea-level-degc",
583 temp_sea_level_degc );
592 * Set the sea level outside air dewpoint and assigning that to all
593 * boundary and aloft environment layers.
596 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
598 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
600 SGPropertyNode *node, *child;
603 node = fgGetNode( "/environment/config/boundary" );
604 if ( node != NULL ) {
606 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
607 child->setDoubleValue( "dewpoint-sea-level-degc",
608 dewpoint_sea_level_degc );
614 node = fgGetNode( "/environment/config/aloft" );
615 if ( node != NULL ) {
617 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
618 child->setDoubleValue( "dewpoint-sea-level-degc",
619 dewpoint_sea_level_degc );
629 * Set the outside air dewpoint at the "current" altitude by first
630 * calculating the corresponding sea level dewpoint, and assigning
631 * that to all boundary and aloft environment layers.
634 do_set_dewpoint_degc (const SGPropertyNode * arg)
636 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
638 // check for an altitude specified in the arguments, otherwise use
639 // current aircraft altitude.
640 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
641 if ( altitude_ft == NULL ) {
642 altitude_ft = fgGetNode("/position/altitude-ft");
645 FGEnvironment dummy; // instantiate a dummy so we can leech a method
646 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
647 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
648 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
650 cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
651 cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
652 cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
654 SGPropertyNode *node, *child;
657 node = fgGetNode( "/environment/config/boundary" );
658 if ( node != NULL ) {
660 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
661 child->setDoubleValue( "dewpoint-sea-level-degc",
662 dewpoint_sea_level_degc );
668 node = fgGetNode( "/environment/config/aloft" );
669 if ( node != NULL ) {
671 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
672 child->setDoubleValue( "dewpoint-sea-level-degc",
673 dewpoint_sea_level_degc );
682 * Update the lighting manually.
685 do_timeofday (const SGPropertyNode * arg)
687 const string &offset_type = arg->getStringValue("timeofday", "noon");
689 static const SGPropertyNode *longitude
690 = fgGetNode("/position/longitude-deg");
691 static const SGPropertyNode *latitude
692 = fgGetNode("/position/latitude-deg");
693 static const SGPropertyNode *cur_time_override
694 = fgGetNode("/sim/time/cur-time-override", true);
696 int orig_warp = globals->get_warp();
697 SGTime *t = globals->get_time_params();
698 time_t cur_time = t->get_cur_time();
699 // cout << "cur_time = " << cur_time << endl;
700 // cout << "orig_warp = " << orig_warp << endl;
703 if ( offset_type == "real" ) {
705 } else if ( offset_type == "dawn" ) {
706 warp = fgTimeSecondsUntilSunAngle( cur_time,
707 longitude->getDoubleValue()
708 * SGD_DEGREES_TO_RADIANS,
709 latitude->getDoubleValue()
710 * SGD_DEGREES_TO_RADIANS,
712 } else if ( offset_type == "morning" ) {
713 warp = fgTimeSecondsUntilSunAngle( cur_time,
714 longitude->getDoubleValue()
715 * SGD_DEGREES_TO_RADIANS,
716 latitude->getDoubleValue()
717 * SGD_DEGREES_TO_RADIANS,
719 } else if ( offset_type == "noon" ) {
720 warp = fgTimeSecondsUntilSunAngle( cur_time,
721 longitude->getDoubleValue()
722 * SGD_DEGREES_TO_RADIANS,
723 latitude->getDoubleValue()
724 * SGD_DEGREES_TO_RADIANS,
726 } else if ( offset_type == "afternoon" ) {
727 warp = fgTimeSecondsUntilSunAngle( cur_time,
728 longitude->getDoubleValue()
729 * SGD_DEGREES_TO_RADIANS,
730 latitude->getDoubleValue()
731 * SGD_DEGREES_TO_RADIANS,
733 } else if ( offset_type == "dusk" ) {
734 warp = fgTimeSecondsUntilSunAngle( cur_time,
735 longitude->getDoubleValue()
736 * SGD_DEGREES_TO_RADIANS,
737 latitude->getDoubleValue()
738 * SGD_DEGREES_TO_RADIANS,
740 } else if ( offset_type == "evening" ) {
741 warp = fgTimeSecondsUntilSunAngle( cur_time,
742 longitude->getDoubleValue()
743 * SGD_DEGREES_TO_RADIANS,
744 latitude->getDoubleValue()
745 * SGD_DEGREES_TO_RADIANS,
747 } else if ( offset_type == "midnight" ) {
748 warp = fgTimeSecondsUntilSunAngle( cur_time,
749 longitude->getDoubleValue()
750 * SGD_DEGREES_TO_RADIANS,
751 latitude->getDoubleValue()
752 * SGD_DEGREES_TO_RADIANS,
755 // cout << "warp = " << warp << endl;
756 globals->set_warp( orig_warp + warp );
758 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
759 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
760 cur_time_override->getLongValue(),
761 globals->get_warp() );
768 * Built-in command: toggle a bool property value.
770 * property: The name of the property to toggle.
773 do_property_toggle (const SGPropertyNode * arg)
775 SGPropertyNode * prop = get_prop(arg);
776 return prop->setBoolValue(!prop->getBoolValue());
781 * Built-in command: assign a value to a property.
783 * property: the name of the property to assign.
784 * value: the value to assign; or
785 * property[1]: the property to copy from.
788 do_property_assign (const SGPropertyNode * arg)
790 SGPropertyNode * prop = get_prop(arg);
791 const SGPropertyNode * prop2 = get_prop2(arg);
792 const SGPropertyNode * value = arg->getNode("value");
795 return prop->setUnspecifiedValue(value->getStringValue());
797 return prop->setUnspecifiedValue(prop2->getStringValue());
804 * Built-in command: increment or decrement a property value.
806 * If the 'step' argument is present, it will be used; otherwise,
807 * the command uses 'offset' and 'factor', usually from the mouse.
809 * property: the name of the property to increment or decrement.
810 * step: the amount of the increment or decrement (default: 0).
811 * offset: offset from the current setting (used for the mouse; multiplied
813 * factor: scaling amount for the offset (defaults to 1).
814 * min: the minimum allowed value (default: no minimum).
815 * max: the maximum allowed value (default: no maximum).
816 * mask: 'integer' to apply only to the left of the decimal point,
817 * 'decimal' to apply only to the right of the decimal point,
818 * or 'all' to apply to the whole number (the default).
819 * wrap: true if the value should be wrapped when it passes min or max;
820 * both min and max must be present for this to work (default:
824 do_property_adjust (const SGPropertyNode * arg)
826 SGPropertyNode * prop = get_prop(arg);
829 if (arg->hasValue("step"))
830 amount = arg->getDoubleValue("step");
832 amount = (arg->getDoubleValue("factor", 1)
833 * arg->getDoubleValue("offset"));
835 double unmodifiable, modifiable;
836 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
837 &unmodifiable, &modifiable);
838 modifiable += amount;
839 limit_value(&modifiable, arg);
841 prop->setDoubleValue(unmodifiable + modifiable);
848 * Built-in command: multiply a property value.
850 * property: the name of the property to multiply.
851 * factor: the amount by which to multiply.
852 * min: the minimum allowed value (default: no minimum).
853 * max: the maximum allowed value (default: no maximum).
854 * mask: 'integer' to apply only to the left of the decimal point,
855 * 'decimal' to apply only to the right of the decimal point,
856 * or 'all' to apply to the whole number (the default).
857 * wrap: true if the value should be wrapped when it passes min or max;
858 * both min and max must be present for this to work (default:
862 do_property_multiply (const SGPropertyNode * arg)
864 SGPropertyNode * prop = get_prop(arg);
865 double factor = arg->getDoubleValue("factor", 1);
867 double unmodifiable, modifiable;
868 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
869 &unmodifiable, &modifiable);
870 modifiable *= factor;
871 limit_value(&modifiable, arg);
873 prop->setDoubleValue(unmodifiable + modifiable);
880 * Built-in command: swap two property values.
882 * property[0]: the name of the first property.
883 * property[1]: the name of the second property.
886 do_property_swap (const SGPropertyNode * arg)
888 SGPropertyNode * prop1 = get_prop(arg);
889 SGPropertyNode * prop2 = get_prop2(arg);
891 // FIXME: inefficient
892 const string & tmp = prop1->getStringValue();
893 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
894 prop2->setUnspecifiedValue(tmp.c_str()));
899 * Built-in command: Set a property to an axis or other moving input.
901 * property: the name of the property to set.
902 * setting: the current input setting, usually between -1.0 and 1.0.
903 * offset: the offset to shift by, before applying the factor.
904 * factor: the factor to multiply by (use negative to reverse).
907 do_property_scale (const SGPropertyNode * arg)
909 SGPropertyNode * prop = get_prop(arg);
910 double setting = arg->getDoubleValue("setting");
911 double offset = arg->getDoubleValue("offset", 0.0);
912 double factor = arg->getDoubleValue("factor", 1.0);
913 bool squared = arg->getBoolValue("squared", false);
914 int power = arg->getIntValue("power", (squared ? 2 : 1));
916 int sign = (setting < 0 ? -1 : 1);
922 setting = setting * setting * sign;
925 setting = setting * setting * setting;
928 setting = setting * setting * setting * setting * sign;
931 setting = pow(setting, power);
932 if ((power % 2) == 0)
937 return prop->setDoubleValue((setting + offset) * factor);
942 * Built-in command: cycle a property through a set of values.
944 * If the current value isn't in the list, the cycle will
945 * (re)start from the beginning.
947 * property: the name of the property to cycle.
948 * value[*]: the list of values to cycle through.
951 do_property_cycle (const SGPropertyNode * arg)
953 SGPropertyNode * prop = get_prop(arg);
954 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
956 int nSelections = values.size();
958 if (nSelections < 1) {
959 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
963 // Try to find the current selection
964 for (int i = 0; i < nSelections; i++) {
965 if (compare_values(prop, values[i])) {
971 // Default or wrap to the first selection
972 if (selection < 0 || selection >= nSelections)
975 prop->setUnspecifiedValue(values[selection]->getStringValue());
981 * Built-in command: randomize a numeric property value.
983 * property: the name of the property value to randomize.
984 * min: the minimum allowed value.
985 * max: the maximum allowed value.
988 do_property_randomize (const SGPropertyNode * arg)
990 SGPropertyNode * prop = get_prop(arg);
991 double min = arg->getDoubleValue("min", DBL_MIN);
992 double max = arg->getDoubleValue("max", DBL_MAX);
993 prop->setDoubleValue(sg_random() * (max - min) + min);
999 * Built-in command: reinit the data logging system based on the
1000 * current contents of the /logger tree.
1003 do_data_logging_commit (const SGPropertyNode * arg)
1005 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1011 * Built-in command: Add a dialog to the GUI system. Does *not*
1012 * display the dialog. The property node should have the same format
1013 * as a dialog XML configuration. It must include:
1015 * name: the name of the GUI dialog for future reference.
1018 do_dialog_new (const SGPropertyNode * arg)
1020 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1022 // Note the casting away of const: this is *real*. Doing a
1023 // "dialog-apply" command later on will mutate this property node.
1024 // I'm not convinced that this isn't the Right Thing though; it
1025 // allows client to create a node, pass it to dialog-new, and get
1026 // the values back from the dialog by reading the same node.
1027 // Perhaps command arguments are not as "const" as they would
1029 gui->newDialog((SGPropertyNode*)arg);
1034 * Built-in command: Show an XML-configured dialog.
1036 * dialog-name: the name of the GUI dialog to display.
1039 do_dialog_show (const SGPropertyNode * arg)
1041 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1042 gui->showDialog(arg->getStringValue("dialog-name"));
1048 * Built-in Command: Hide the active XML-configured dialog.
1051 do_dialog_close (const SGPropertyNode * arg)
1053 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1054 if(arg->hasValue("dialog-name"))
1055 return gui->closeDialog(arg->getStringValue("dialog-name"));
1056 return gui->closeActiveDialog();
1061 * Update a value in the active XML-configured dialog.
1063 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1066 do_dialog_update (const SGPropertyNode * arg)
1068 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1069 FGDialog * dialog = gui->getActiveDialog();
1071 if (arg->hasValue("object-name")) {
1072 dialog->updateValue(arg->getStringValue("object-name"));
1074 dialog->updateValues();
1084 * Apply a value in the active XML-configured dialog.
1086 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1089 do_dialog_apply (const SGPropertyNode * arg)
1091 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1092 FGDialog * dialog = gui->getActiveDialog();
1094 if (arg->hasValue("object-name")) {
1095 const char * name = arg->getStringValue("object-name");
1096 dialog->applyValue(name);
1097 dialog->updateValue(name);
1099 dialog->applyValues();
1100 dialog->updateValues();
1110 * Built-in command: commit presets (read from in /sim/presets/)
1113 do_presets_commit (const SGPropertyNode * arg)
1115 // unbind the current fdm state so property changes
1116 // don't get lost when we subsequently delete this fdm
1117 // and create a new one.
1118 cur_fdm_state->unbind();
1120 // set position from presets
1124 fgReInitSubsystems();
1126 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1129 if ( ! fgGetBool("/sim/presets/onground") ) {
1130 fgSetBool( "/sim/freeze/master", true );
1131 fgSetBool( "/sim/freeze/clock", true );
1139 * Built-in command: set log level (0 ... 7)
1142 do_log_level (const SGPropertyNode * arg)
1144 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1150 * Built-in command: replay the FDR buffer
1153 do_replay (const SGPropertyNode * arg)
1155 // freeze the master fdm
1156 fgSetInt( "/sim/freeze/replay-state", 1 );
1158 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1160 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1161 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1162 double duration = fgGetDouble( "/sim/replay/duration" );
1163 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1164 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1166 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1169 cout << "start = " << r->get_start_time()
1170 << " end = " << r->get_end_time() << endl;
1178 do_decrease_visibility (const SGPropertyNode * arg)
1180 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1181 fgSetDouble("/environment/visibility-m", new_value);
1182 fgDefaultWeatherValue("visibility-m", new_value);
1183 globals->get_subsystem("environment")->reinit();
1189 do_increase_visibility (const SGPropertyNode * arg)
1191 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1192 fgSetDouble("/environment/visibility-m", new_value);
1193 fgDefaultWeatherValue("visibility-m", new_value);
1194 globals->get_subsystem("environment")->reinit();
1200 do_hud_brightkey(const SGPropertyNode *)
1202 HUD_brightkey( true );
1207 do_hud_masterswitch(const SGPropertyNode *)
1209 HUD_masterswitch( true );
1214 do_hud_init(const SGPropertyNode *)
1216 fgHUDInit(0); // minimal HUD
1221 do_hud_init2(const SGPropertyNode *)
1223 fgHUDInit2(0); // normal HUD
1228 * An fgcommand to allow loading of xml files via nasal,
1229 * the xml file's structure will be made available within
1230 * a (definable) property tree node
1232 * @param filename a string to hold the complete path & filename of a XML file
1233 * @param targetnode a string pointing to a location within the property tree
1234 * where to store the parsed XML file
1238 do_load_xml_to_proptree(const SGPropertyNode * node)
1240 // SG_LOG(SG_GENERAL, SG_ALERT, "fgcommand loadxml executed");
1242 SGPropertyNode * targetnode;
1243 targetnode = fgGetNode(node->getNode("targetnode")->getStringValue(),true);
1245 const char *filename = node->getNode("filename")->getStringValue();
1247 fgLoadProps(filename, targetnode);
1248 } catch (const sg_exception &e) {
1249 string errmsg = "Error reading file " + string(filename) + ":\n";
1250 guiErrorMessage(errmsg.c_str(), e);
1259 * an fgcommand to allow saving of xml files via nasal,
1260 * the file's structure will be determined based on what's
1261 * encountered in the passed (source) property tree node
1263 * @param filename a string to hold the complete path & filename of the (new)
1265 * @param sourcenode a string pointing to a location within the property tree
1266 * where to find the nodes that should be written recursively into an XML file
1269 * deal with already existing filenames, optionally return error/success
1270 * values in a separate node to provide status information
1272 * note: it's directly using writeProperties, which is not necessarily
1273 * preferable, but for now it should work ...
1277 do_save_xml_from_proptree(const SGPropertyNode * node)
1279 //TODO: do Parameter validation !
1280 SGPropertyNode * sourcenode;
1281 sourcenode = fgGetNode(node->getNode("sourcenode")->getStringValue(),true);
1283 const char *filename = node->getNode("filename")->getStringValue();
1285 writeProperties (filename, sourcenode, true);
1286 } catch (const sg_exception &e) {
1287 string errmsg = "Error writing file " + string(filename) + ":\n";
1288 guiErrorMessage(errmsg.c_str(), e);
1296 ////////////////////////////////////////////////////////////////////////
1298 ////////////////////////////////////////////////////////////////////////
1302 * Table of built-in commands.
1304 * New commands do not have to be added here; any module in the application
1305 * can add a new command using globals->get_commands()->addCommand(...).
1309 SGCommandMgr::command_t command;
1311 { "null", do_null },
1312 #if defined(HAVE_PLIB_PSL)
1313 { "script", do_script },
1314 #endif // HAVE_PLIB_PSL
1315 { "nasal", do_nasal },
1316 { "exit", do_exit },
1317 { "reinit", do_reinit },
1318 { "suspend", do_reinit },
1319 { "resume", do_reinit },
1320 { "load", do_load },
1321 { "save", do_save },
1322 { "panel-load", do_panel_load },
1323 { "panel-mouse-click", do_panel_mouse_click },
1324 { "preferences-load", do_preferences_load },
1325 { "view-cycle", do_view_cycle },
1326 { "screen-capture", do_screen_capture },
1327 { "hires-screen-capture", do_hires_screen_capture },
1328 { "tile-cache-reload", do_tile_cache_reload },
1329 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1330 { "set-outside-air-temp-degc", do_set_oat_degc },
1331 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1332 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1333 { "timeofday", do_timeofday },
1334 { "property-toggle", do_property_toggle },
1335 { "property-assign", do_property_assign },
1336 { "property-adjust", do_property_adjust },
1337 { "property-multiply", do_property_multiply },
1338 { "property-swap", do_property_swap },
1339 { "property-scale", do_property_scale },
1340 { "property-cycle", do_property_cycle },
1341 { "property-randomize", do_property_randomize },
1342 { "data-logging-commit", do_data_logging_commit },
1343 { "dialog-new", do_dialog_new },
1344 { "dialog-show", do_dialog_show },
1345 { "dialog-close", do_dialog_close },
1346 { "dialog-update", do_dialog_update },
1347 { "dialog-apply", do_dialog_apply },
1348 { "presets-commit", do_presets_commit },
1349 { "log-level", do_log_level },
1350 { "replay", do_replay },
1351 { "decrease-visibility", do_decrease_visibility },
1352 { "increase-visibility", do_increase_visibility },
1353 { "hud-brightkey", do_hud_brightkey },
1354 { "hud-masterswitch", do_hud_masterswitch },
1355 { "hud-init", do_hud_init },
1356 { "hud-init2", do_hud_init2 },
1357 { "loadxml", do_load_xml_to_proptree},
1358 { "savexml", do_save_xml_from_proptree },
1359 { 0, 0 } // zero-terminated
1364 * Initialize the default built-in commands.
1366 * Other commands may be added by other parts of the application.
1371 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1372 for (int i = 0; built_ins[i].name != 0; i++) {
1373 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1374 globals->get_commands()->addCommand(built_ins[i].name,
1375 built_ins[i].command);
1378 typedef bool (*dummy)();
1379 fgTie( "/command/view/next", dummy(0), do_view_next );
1380 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1383 // end of fg_commands.cxx