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>
21 #include <simgear/structure/event_mgr.hxx>
23 #include <Cockpit/panel.hxx>
24 #include <Cockpit/panel_io.hxx>
25 #include <Cockpit/hud.hxx>
26 #include <Environment/environment.hxx>
27 #include <FDM/flight.hxx>
29 #include <GUI/new_gui.hxx>
30 #include <GUI/dialog.hxx>
31 #include <Aircraft/replay.hxx>
32 #include <Scenery/tilemgr.hxx>
33 #include <Scenery/scenery.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"
42 #include "fg_commands.hxx"
43 #include "fg_props.hxx"
44 #include "globals.hxx"
47 #include "viewmgr.hxx"
56 ////////////////////////////////////////////////////////////////////////
57 // Static helper functions.
58 ////////////////////////////////////////////////////////////////////////
61 static inline SGPropertyNode *
62 get_prop (const SGPropertyNode * arg)
64 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
67 static inline SGPropertyNode *
68 get_prop2 (const SGPropertyNode * arg)
70 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
75 * Get a double value and split it as required.
78 split_value (double full_value, const char * mask,
79 double * unmodifiable, double * modifiable)
81 if (!strcmp("integer", mask)) {
82 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
83 *unmodifiable = full_value - *modifiable;
84 } else if (!strcmp("decimal", mask)) {
85 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
86 *modifiable = full_value - *unmodifiable;
88 if (strcmp("all", mask))
89 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
90 << " assuming 'all'");
92 *modifiable = full_value;
98 * Clamp or wrap a value as specified.
101 limit_value (double * value, const SGPropertyNode * arg)
103 const SGPropertyNode * min_node = arg->getChild("min");
104 const SGPropertyNode * max_node = arg->getChild("max");
106 bool wrap = arg->getBoolValue("wrap");
108 if (min_node == 0 || max_node == 0)
111 if (wrap) { // wrap such that min <= x < max
112 double min_val = min_node->getDoubleValue();
113 double max_val = max_node->getDoubleValue();
114 double resolution = arg->getDoubleValue("resolution");
115 if (resolution > 0.0) {
116 // snap to (min + N*resolution), taking special care to handle imprecision
117 int n = (int)floor((*value - min_val) / resolution + 0.5);
118 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
119 SG_NORMALIZE_RANGE(n, 0, steps);
120 *value = min_val + resolution * n;
122 // plain circular wrapping
123 SG_NORMALIZE_RANGE(*value, min_val, max_val);
125 } else { // clamp such that min <= x <= max
126 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
127 *value = min_node->getDoubleValue();
128 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
129 *value = max_node->getDoubleValue();
134 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
136 switch (value1->getType()) {
137 case simgear::props::BOOL:
138 return (value1->getBoolValue() == value2->getBoolValue());
139 case simgear::props::INT:
140 return (value1->getIntValue() == value2->getIntValue());
141 case simgear::props::LONG:
142 return (value1->getLongValue() == value2->getLongValue());
143 case simgear::props::FLOAT:
144 return (value1->getFloatValue() == value2->getFloatValue());
145 case simgear::props::DOUBLE:
146 return (value1->getDoubleValue() == value2->getDoubleValue());
148 return !strcmp(value1->getStringValue(), value2->getStringValue());
154 ////////////////////////////////////////////////////////////////////////
155 // Command implementations.
156 ////////////////////////////////////////////////////////////////////////
160 * Built-in command: do nothing.
163 do_null (const SGPropertyNode * arg)
169 * Built-in command: run a Nasal script.
172 do_nasal (const SGPropertyNode * arg)
174 return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
178 * Built-in command: exit FlightGear.
180 * status: the exit status to return to the operating system (defaults to 0)
183 do_exit (const SGPropertyNode * arg)
185 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
186 fgSetBool("/sim/signals/exit", true);
188 if (fgGetBool("/sim/startup/save-on-exit")) {
190 char* envp = ::getenv( "APPDATA" );
191 if ( envp != NULL ) {
192 SGPath config( envp );
193 config.append( "flightgear.org" );
195 if ( homedir != NULL ) {
196 SGPath config( homedir );
197 config.append( ".fgfs" );
199 config.append( "autosave.xml" );
200 config.create_dir( 0700 );
201 SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
203 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
204 } catch (const sg_exception &e) {
205 guiErrorMessage("Error writing autosave.xml: ", e);
208 SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
211 fgExit(arg->getIntValue("status", 0));
217 * Reset FlightGear (Shift-Escape or Menu->File->Reset)
220 do_reset (const SGPropertyNode * arg)
228 * Built-in command: reinitialize one or more subsystems.
230 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
231 * none is specified, reinitialize all of them.
234 do_reinit (const SGPropertyNode * arg)
238 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
239 if (subsystems.size() == 0) {
240 globals->get_subsystem_mgr()->reinit();
242 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
243 const char * name = subsystems[i]->getStringValue();
244 SGSubsystem * subsystem = globals->get_subsystem(name);
245 if (subsystem == 0) {
247 SG_LOG( SG_GENERAL, SG_ALERT,
248 "Subsystem " << name << "not found" );
255 globals->get_event_mgr()->reinit();
262 // these routines look useful ??? but are never used in the code ???
266 * Built-in command: suspend one or more subsystems.
268 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
271 do_suspend (const SGPropertyNode * arg)
275 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
276 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
277 const char * name = subsystems[i]->getStringValue();
278 SGSubsystem * subsystem = globals->get_subsystem(name);
279 if (subsystem == 0) {
281 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
283 subsystem->suspend();
290 * Built-in command: suspend one or more subsystems.
292 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
295 do_resume (const SGPropertyNode * arg)
299 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
300 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
301 const char * name = subsystems[i]->getStringValue();
302 SGSubsystem * subsystem = globals->get_subsystem(name);
303 if (subsystem == 0) {
305 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
317 * Built-in command: load flight.
319 * file (optional): the name of the file to load (relative to current
320 * directory). Defaults to "fgfs.sav"
323 do_load (const SGPropertyNode * arg)
325 string file = arg->getStringValue("file", "fgfs.sav");
326 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
329 if (!fgValidatePath(file.c_str(), false)) {
330 SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
331 "(unauthorized access)");
335 ifstream input(file.c_str());
336 if (input.good() && fgLoadFlight(input)) {
338 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
341 SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
348 * Built-in command: save flight.
350 * file (optional): the name of the file to save (relative to the
351 * current directory). Defaults to "fgfs.sav".
354 do_save (const SGPropertyNode * arg)
356 string file = arg->getStringValue("file", "fgfs.sav");
357 if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
360 if (!fgValidatePath(file.c_str(), false)) {
361 SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
362 "(unauthorized access)");
366 bool write_all = arg->getBoolValue("write-all", false);
367 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
368 ofstream output(file.c_str());
369 if (output.good() && fgSaveFlight(output, write_all)) {
371 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
374 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
381 * Built-in command: (re)load the panel.
383 * path (optional): the file name to load the panel from
384 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
385 * and if that's unspecified, to "Panels/Default/default.xml".
388 do_panel_load (const SGPropertyNode * arg)
391 arg->getStringValue("path",
392 fgGetString("/sim/panel/path",
393 "Panels/Default/default.xml"));
394 FGPanel * new_panel = fgReadPanel(panel_path);
395 if (new_panel == 0) {
396 SG_LOG(SG_INPUT, SG_ALERT,
397 "Error reading new panel from " << panel_path);
400 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
401 globals->get_current_panel()->unbind();
402 delete globals->get_current_panel();
403 globals->set_current_panel( new_panel );
404 globals->get_current_panel()->bind();
410 * Built-in command: pass a mouse click to the panel.
412 * button: the mouse button number, zero-based.
413 * is-down: true if the button is down, false if it is up.
414 * x-pos: the x position of the mouse click.
415 * y-pos: the y position of the mouse click.
418 do_panel_mouse_click (const SGPropertyNode * arg)
420 if (globals->get_current_panel() != 0)
421 return globals->get_current_panel()
422 ->doMouseAction(arg->getIntValue("button"),
423 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
424 arg->getIntValue("x-pos"),
425 arg->getIntValue("y-pos"));
432 * Built-in command: (re)load preferences.
434 * path (optional): the file name to load the panel from (relative
435 * to FG_ROOT). Defaults to "preferences.xml".
438 do_preferences_load (const SGPropertyNode * arg)
441 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
442 globals->get_props());
443 } catch (const sg_exception &e) {
444 guiErrorMessage("Error reading global preferences: ", e);
447 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
455 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
456 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
457 if ( globals->get_viewmgr()->get_current() == 1 ) {
458 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
466 globals->get_current_view()->setHeadingOffset_deg(0.0);
467 globals->get_viewmgr()->next_view();
468 fix_hud_visibility();
474 globals->get_current_view()->setHeadingOffset_deg(0.0);
475 globals->get_viewmgr()->prev_view();
476 fix_hud_visibility();
480 * Built-in command: cycle view.
483 do_view_cycle (const SGPropertyNode * arg)
485 globals->get_current_view()->setHeadingOffset_deg(0.0);
486 globals->get_viewmgr()->next_view();
487 fix_hud_visibility();
492 * Built-in command: capture screen.
495 do_screen_capture (const SGPropertyNode * arg)
497 return fgDumpSnapShot();
501 do_dump_scene_graph (const SGPropertyNode*)
508 do_dump_terrain_branch (const SGPropertyNode*)
510 fgDumpTerrainBranch();
512 double lon_deg = fgGetDouble("/position/longitude-deg");
513 double lat_deg = fgGetDouble("/position/latitude-deg");
514 SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
515 SGVec3d zero = SGVec3d::fromGeod(geodPos);
517 SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
518 SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
519 SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
525 * Built-in command: hires capture screen.
528 do_hires_screen_capture (const SGPropertyNode * arg)
536 * Reload the tile cache.
539 do_tile_cache_reload (const SGPropertyNode * arg)
541 static const SGPropertyNode *master_freeze
542 = fgGetNode("/sim/freeze/master");
543 bool freeze = master_freeze->getBoolValue();
544 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
546 fgSetBool("/sim/freeze/master", true);
548 if ( globals->get_tile_mgr()->init() ) {
549 // Load the local scenery data
550 double visibility_meters = fgGetDouble("/environment/visibility-m");
551 globals->get_tile_mgr()->update( visibility_meters );
553 SG_LOG( SG_GENERAL, SG_ALERT,
554 "Error in Tile Manager initialization!" );
558 fgSetBool("/sim/freeze/master", false);
565 * Set the sea level outside air temperature and assigning that to all
566 * boundary and aloft environment layers.
569 do_set_sea_level_degc (const SGPropertyNode * arg)
571 double temp_sea_level_degc = arg->getDoubleValue("temp-degc", 15.0);
573 SGPropertyNode *node, *child;
576 node = fgGetNode( "/environment/config/boundary" );
577 if ( node != NULL ) {
579 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
580 child->setDoubleValue( "temperature-sea-level-degc",
581 temp_sea_level_degc );
587 node = fgGetNode( "/environment/config/aloft" );
588 if ( node != NULL ) {
590 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
591 child->setDoubleValue( "temperature-sea-level-degc",
592 temp_sea_level_degc );
602 * Set the outside air temperature at the "current" altitude by first
603 * calculating the corresponding sea level temp, and assigning that to
604 * all boundary and aloft environment layers.
607 do_set_oat_degc (const SGPropertyNode * arg)
609 const string &temp_str = arg->getStringValue("temp-degc", "15.0");
611 // check for an altitude specified in the arguments, otherwise use
612 // current aircraft altitude.
613 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
614 if ( altitude_ft == NULL ) {
615 altitude_ft = fgGetNode("/position/altitude-ft");
618 FGEnvironment dummy; // instantiate a dummy so we can leech a method
619 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
620 dummy.set_temperature_degc( atof( temp_str.c_str() ) );
621 double temp_sea_level_degc = dummy.get_temperature_sea_level_degc();
623 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
624 //cout << "Temp at alt (C) = " << atof( temp_str.c_str() ) << endl;
625 //cout << "Temp sea level (C) = " << temp_sea_level_degc << endl;
627 SGPropertyNode *node, *child;
630 node = fgGetNode( "/environment/config/boundary" );
631 if ( node != NULL ) {
633 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
634 child->setDoubleValue( "temperature-sea-level-degc",
635 temp_sea_level_degc );
641 node = fgGetNode( "/environment/config/aloft" );
642 if ( node != NULL ) {
644 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
645 child->setDoubleValue( "temperature-sea-level-degc",
646 temp_sea_level_degc );
655 * Set the sea level outside air dewpoint and assigning that to all
656 * boundary and aloft environment layers.
659 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
661 double dewpoint_sea_level_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
663 SGPropertyNode *node, *child;
666 node = fgGetNode( "/environment/config/boundary" );
667 if ( node != NULL ) {
669 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
670 child->setDoubleValue( "dewpoint-sea-level-degc",
671 dewpoint_sea_level_degc );
677 node = fgGetNode( "/environment/config/aloft" );
678 if ( node != NULL ) {
680 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
681 child->setDoubleValue( "dewpoint-sea-level-degc",
682 dewpoint_sea_level_degc );
692 * Set the outside air dewpoint at the "current" altitude by first
693 * calculating the corresponding sea level dewpoint, and assigning
694 * that to all boundary and aloft environment layers.
697 do_set_dewpoint_degc (const SGPropertyNode * arg)
699 const string &dewpoint_str = arg->getStringValue("dewpoint-degc", "5.0");
701 // check for an altitude specified in the arguments, otherwise use
702 // current aircraft altitude.
703 const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
704 if ( altitude_ft == NULL ) {
705 altitude_ft = fgGetNode("/position/altitude-ft");
708 FGEnvironment dummy; // instantiate a dummy so we can leech a method
709 dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
710 dummy.set_dewpoint_degc( atof( dewpoint_str.c_str() ) );
711 double dewpoint_sea_level_degc = dummy.get_dewpoint_sea_level_degc();
713 //cout << "Altitude = " << altitude_ft->getDoubleValue() << endl;
714 //cout << "Dewpoint at alt (C) = " << atof( dewpoint_str.c_str() ) << endl;
715 //cout << "Dewpoint at sea level (C) = " << dewpoint_sea_level_degc << endl;
717 SGPropertyNode *node, *child;
720 node = fgGetNode( "/environment/config/boundary" );
721 if ( node != NULL ) {
723 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
724 child->setDoubleValue( "dewpoint-sea-level-degc",
725 dewpoint_sea_level_degc );
731 node = fgGetNode( "/environment/config/aloft" );
732 if ( node != NULL ) {
734 while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
735 child->setDoubleValue( "dewpoint-sea-level-degc",
736 dewpoint_sea_level_degc );
745 * Update the lighting manually.
748 do_timeofday (const SGPropertyNode * arg)
750 const string &offset_type = arg->getStringValue("timeofday", "noon");
752 static const SGPropertyNode *longitude
753 = fgGetNode("/position/longitude-deg");
754 static const SGPropertyNode *latitude
755 = fgGetNode("/position/latitude-deg");
756 static const SGPropertyNode *cur_time_override
757 = fgGetNode("/sim/time/cur-time-override", true);
759 int orig_warp = globals->get_warp();
760 SGTime *t = globals->get_time_params();
761 time_t cur_time = t->get_cur_time();
762 // cout << "cur_time = " << cur_time << endl;
763 // cout << "orig_warp = " << orig_warp << endl;
766 if ( offset_type == "real" ) {
768 } else if ( offset_type == "dawn" ) {
769 warp = fgTimeSecondsUntilSunAngle( cur_time,
770 longitude->getDoubleValue()
771 * SGD_DEGREES_TO_RADIANS,
772 latitude->getDoubleValue()
773 * SGD_DEGREES_TO_RADIANS,
775 } else if ( offset_type == "morning" ) {
776 warp = fgTimeSecondsUntilSunAngle( cur_time,
777 longitude->getDoubleValue()
778 * SGD_DEGREES_TO_RADIANS,
779 latitude->getDoubleValue()
780 * SGD_DEGREES_TO_RADIANS,
782 } else if ( offset_type == "noon" ) {
783 warp = fgTimeSecondsUntilSunAngle( cur_time,
784 longitude->getDoubleValue()
785 * SGD_DEGREES_TO_RADIANS,
786 latitude->getDoubleValue()
787 * SGD_DEGREES_TO_RADIANS,
789 } else if ( offset_type == "afternoon" ) {
790 warp = fgTimeSecondsUntilSunAngle( cur_time,
791 longitude->getDoubleValue()
792 * SGD_DEGREES_TO_RADIANS,
793 latitude->getDoubleValue()
794 * SGD_DEGREES_TO_RADIANS,
796 } else if ( offset_type == "dusk" ) {
797 warp = fgTimeSecondsUntilSunAngle( cur_time,
798 longitude->getDoubleValue()
799 * SGD_DEGREES_TO_RADIANS,
800 latitude->getDoubleValue()
801 * SGD_DEGREES_TO_RADIANS,
803 } else if ( offset_type == "evening" ) {
804 warp = fgTimeSecondsUntilSunAngle( cur_time,
805 longitude->getDoubleValue()
806 * SGD_DEGREES_TO_RADIANS,
807 latitude->getDoubleValue()
808 * SGD_DEGREES_TO_RADIANS,
810 } else if ( offset_type == "midnight" ) {
811 warp = fgTimeSecondsUntilSunAngle( cur_time,
812 longitude->getDoubleValue()
813 * SGD_DEGREES_TO_RADIANS,
814 latitude->getDoubleValue()
815 * SGD_DEGREES_TO_RADIANS,
818 // cout << "warp = " << warp << endl;
819 globals->set_warp( orig_warp + warp );
821 t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
822 latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
823 cur_time_override->getLongValue(),
824 globals->get_warp() );
831 * Built-in command: toggle a bool property value.
833 * property: The name of the property to toggle.
836 do_property_toggle (const SGPropertyNode * arg)
838 SGPropertyNode * prop = get_prop(arg);
839 return prop->setBoolValue(!prop->getBoolValue());
844 * Built-in command: assign a value to a property.
846 * property: the name of the property to assign.
847 * value: the value to assign; or
848 * property[1]: the property to copy from.
851 do_property_assign (const SGPropertyNode * arg)
853 SGPropertyNode * prop = get_prop(arg);
854 const SGPropertyNode * prop2 = get_prop2(arg);
855 const SGPropertyNode * value = arg->getNode("value");
858 return prop->setUnspecifiedValue(value->getStringValue());
860 return prop->setUnspecifiedValue(prop2->getStringValue());
867 * Built-in command: increment or decrement a property value.
869 * If the 'step' argument is present, it will be used; otherwise,
870 * the command uses 'offset' and 'factor', usually from the mouse.
872 * property: the name of the property to increment or decrement.
873 * step: the amount of the increment or decrement (default: 0).
874 * offset: offset from the current setting (used for the mouse; multiplied
876 * factor: scaling amount for the offset (defaults to 1).
877 * min: the minimum allowed value (default: no minimum).
878 * max: the maximum allowed value (default: no maximum).
879 * mask: 'integer' to apply only to the left of the decimal point,
880 * 'decimal' to apply only to the right of the decimal point,
881 * or 'all' to apply to the whole number (the default).
882 * wrap: true if the value should be wrapped when it passes min or max;
883 * both min and max must be present for this to work (default:
887 do_property_adjust (const SGPropertyNode * arg)
889 SGPropertyNode * prop = get_prop(arg);
892 if (arg->hasValue("step"))
893 amount = arg->getDoubleValue("step");
895 amount = (arg->getDoubleValue("factor", 1)
896 * arg->getDoubleValue("offset"));
898 double unmodifiable, modifiable;
899 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
900 &unmodifiable, &modifiable);
901 modifiable += amount;
902 limit_value(&modifiable, arg);
904 prop->setDoubleValue(unmodifiable + modifiable);
911 * Built-in command: multiply a property value.
913 * property: the name of the property to multiply.
914 * factor: the amount by which to multiply.
915 * min: the minimum allowed value (default: no minimum).
916 * max: the maximum allowed value (default: no maximum).
917 * mask: 'integer' to apply only to the left of the decimal point,
918 * 'decimal' to apply only to the right of the decimal point,
919 * or 'all' to apply to the whole number (the default).
920 * wrap: true if the value should be wrapped when it passes min or max;
921 * both min and max must be present for this to work (default:
925 do_property_multiply (const SGPropertyNode * arg)
927 SGPropertyNode * prop = get_prop(arg);
928 double factor = arg->getDoubleValue("factor", 1);
930 double unmodifiable, modifiable;
931 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
932 &unmodifiable, &modifiable);
933 modifiable *= factor;
934 limit_value(&modifiable, arg);
936 prop->setDoubleValue(unmodifiable + modifiable);
943 * Built-in command: swap two property values.
945 * property[0]: the name of the first property.
946 * property[1]: the name of the second property.
949 do_property_swap (const SGPropertyNode * arg)
951 SGPropertyNode * prop1 = get_prop(arg);
952 SGPropertyNode * prop2 = get_prop2(arg);
954 // FIXME: inefficient
955 const string & tmp = prop1->getStringValue();
956 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
957 prop2->setUnspecifiedValue(tmp.c_str()));
962 * Built-in command: Set a property to an axis or other moving input.
964 * property: the name of the property to set.
965 * setting: the current input setting, usually between -1.0 and 1.0.
966 * offset: the offset to shift by, before applying the factor.
967 * factor: the factor to multiply by (use negative to reverse).
970 do_property_scale (const SGPropertyNode * arg)
972 SGPropertyNode * prop = get_prop(arg);
973 double setting = arg->getDoubleValue("setting");
974 double offset = arg->getDoubleValue("offset", 0.0);
975 double factor = arg->getDoubleValue("factor", 1.0);
976 bool squared = arg->getBoolValue("squared", false);
977 int power = arg->getIntValue("power", (squared ? 2 : 1));
979 int sign = (setting < 0 ? -1 : 1);
985 setting = setting * setting * sign;
988 setting = setting * setting * setting;
991 setting = setting * setting * setting * setting * sign;
994 setting = pow(setting, power);
995 if ((power % 2) == 0)
1000 return prop->setDoubleValue((setting + offset) * factor);
1005 * Built-in command: cycle a property through a set of values.
1007 * If the current value isn't in the list, the cycle will
1008 * (re)start from the beginning.
1010 * property: the name of the property to cycle.
1011 * value[*]: the list of values to cycle through.
1014 do_property_cycle (const SGPropertyNode * arg)
1016 SGPropertyNode * prop = get_prop(arg);
1017 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
1019 int nSelections = values.size();
1021 if (nSelections < 1) {
1022 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
1026 // Try to find the current selection
1027 for (int i = 0; i < nSelections; i++) {
1028 if (compare_values(prop, values[i])) {
1034 // Default or wrap to the first selection
1035 if (selection < 0 || selection >= nSelections)
1038 prop->setUnspecifiedValue(values[selection]->getStringValue());
1044 * Built-in command: randomize a numeric property value.
1046 * property: the name of the property value to randomize.
1047 * min: the minimum allowed value.
1048 * max: the maximum allowed value.
1051 do_property_randomize (const SGPropertyNode * arg)
1053 SGPropertyNode * prop = get_prop(arg);
1054 double min = arg->getDoubleValue("min", DBL_MIN);
1055 double max = arg->getDoubleValue("max", DBL_MAX);
1056 prop->setDoubleValue(sg_random() * (max - min) + min);
1062 * Built-in command: reinit the data logging system based on the
1063 * current contents of the /logger tree.
1066 do_data_logging_commit (const SGPropertyNode * arg)
1068 FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1074 * Built-in command: Add a dialog to the GUI system. Does *not*
1075 * display the dialog. The property node should have the same format
1076 * as a dialog XML configuration. It must include:
1078 * name: the name of the GUI dialog for future reference.
1081 do_dialog_new (const SGPropertyNode * arg)
1083 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1085 // Note the casting away of const: this is *real*. Doing a
1086 // "dialog-apply" command later on will mutate this property node.
1087 // I'm not convinced that this isn't the Right Thing though; it
1088 // allows client to create a node, pass it to dialog-new, and get
1089 // the values back from the dialog by reading the same node.
1090 // Perhaps command arguments are not as "const" as they would
1092 gui->newDialog((SGPropertyNode*)arg);
1097 * Built-in command: Show an XML-configured dialog.
1099 * dialog-name: the name of the GUI dialog to display.
1102 do_dialog_show (const SGPropertyNode * arg)
1104 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1105 gui->showDialog(arg->getStringValue("dialog-name"));
1111 * Built-in Command: Hide the active XML-configured dialog.
1114 do_dialog_close (const SGPropertyNode * arg)
1116 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1117 if(arg->hasValue("dialog-name"))
1118 return gui->closeDialog(arg->getStringValue("dialog-name"));
1119 return gui->closeActiveDialog();
1124 * Update a value in the active XML-configured dialog.
1126 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1129 do_dialog_update (const SGPropertyNode * arg)
1131 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1133 if (arg->hasValue("dialog-name"))
1134 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1136 dialog = gui->getActiveDialog();
1139 dialog->updateValues(arg->getStringValue("object-name"));
1148 * Apply a value in the active XML-configured dialog.
1150 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1153 do_dialog_apply (const SGPropertyNode * arg)
1155 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1157 if (arg->hasValue("dialog-name"))
1158 dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1160 dialog = gui->getActiveDialog();
1163 dialog->applyValues(arg->getStringValue("object-name"));
1172 * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1176 do_gui_redraw (const SGPropertyNode * arg)
1178 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1185 * Adds model to the scenery. The path to the added branch (/models/model[*])
1186 * is returned in property "property".
1189 do_add_model (const SGPropertyNode * arg)
1191 SGPropertyNode * model = fgGetNode("models", true);
1192 for (int i = 0;; i++) {
1195 if (!model->getChild("model", i, false)) {
1196 model = model->getChild("model", i, true);
1200 copyProperties(arg, model);
1201 if (model->hasValue("elevation-m"))
1202 model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1203 * SG_METER_TO_FEET);
1204 model->getNode("load", true);
1205 model->removeChildren("load");
1206 const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1212 * Set mouse cursor coordinates and cursor shape.
1215 do_set_cursor (const SGPropertyNode * arg)
1217 if (arg->hasValue("x") || arg->hasValue("y")) {
1218 SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1219 SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1220 int x = arg->getIntValue("x", mx->getIntValue());
1221 int y = arg->getIntValue("y", my->getIntValue());
1227 SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1228 if (cursor->getType() != simgear::props::NONE)
1229 fgSetMouseCursor(cursor->getIntValue());
1231 cursor->setIntValue(fgGetMouseCursor());
1237 * Built-in command: play an audio message (i.e. a wav file) This is
1238 * fire and forget. Call this once per message and it will get dumped
1239 * into a queue. Messages are played sequentially so they do not
1243 do_play_audio_sample (const SGPropertyNode * arg)
1245 FGFX *fx = (FGFX *)globals->get_subsystem("fx");
1246 string path = arg->getStringValue("path");
1247 string file = arg->getStringValue("file");
1248 double volume = arg->getDoubleValue("volume");
1249 // cout << "playing " << path << " / " << file << endl;
1251 fx->play_message( path, file, volume );
1254 } catch (const sg_io_exception&) {
1255 SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1256 "failed to load" << path << '/' << file);
1262 * Built-in command: commit presets (read from in /sim/presets/)
1265 do_presets_commit (const SGPropertyNode * arg)
1267 // unbind the current fdm state so property changes
1268 // don't get lost when we subsequently delete this fdm
1269 // and create a new one.
1270 cur_fdm_state->unbind();
1272 // set position from presets
1275 fgReInitSubsystems();
1277 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1280 if ( ! fgGetBool("/sim/presets/onground") ) {
1281 fgSetBool( "/sim/freeze/master", true );
1282 fgSetBool( "/sim/freeze/clock", true );
1290 * Built-in command: set log level (0 ... 7)
1293 do_log_level (const SGPropertyNode * arg)
1295 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1301 * Built-in command: replay the FDR buffer
1304 do_replay (const SGPropertyNode * arg)
1306 // freeze the master fdm
1307 fgSetInt( "/sim/freeze/replay-state", 1 );
1309 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1311 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1312 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1313 double duration = fgGetDouble( "/sim/replay/duration" );
1314 if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1315 fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1317 fgSetDouble( "/sim/replay/time", r->get_start_time() );
1320 // cout << "start = " << r->get_start_time()
1321 // << " end = " << r->get_end_time() << endl;
1328 do_decrease_visibility (const SGPropertyNode * arg)
1330 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1331 fgSetDouble("/environment/visibility-m", new_value);
1332 fgDefaultWeatherValue("visibility-m", new_value);
1333 globals->get_subsystem("environment")->reinit();
1339 do_increase_visibility (const SGPropertyNode * arg)
1341 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1342 fgSetDouble("/environment/visibility-m", new_value);
1343 fgDefaultWeatherValue("visibility-m", new_value);
1344 globals->get_subsystem("environment")->reinit();
1350 do_hud_init(const SGPropertyNode *)
1352 fgHUDInit(0); // minimal HUD
1357 do_hud_init2(const SGPropertyNode *)
1359 fgHUDInit2(0); // normal HUD
1365 * An fgcommand to allow loading of xml files via nasal,
1366 * the xml file's structure will be made available within
1367 * a property tree node defined under argument "targetnode",
1368 * or in the given argument tree under "data" otherwise.
1370 * @param filename a string to hold the complete path & filename of an XML file
1371 * @param targetnode a string pointing to a location within the property tree
1372 * where to store the parsed XML file. If <targetnode> is undefined, then the
1373 * file contents are stored under a node <data> in the argument tree.
1377 do_load_xml_to_proptree(const SGPropertyNode * arg)
1379 SGPath file(arg->getStringValue("filename"));
1380 if (file.str().empty())
1383 if (file.extension() != "xml")
1384 file.concat(".xml");
1386 if (!fgValidatePath(file.c_str(), false)) {
1387 SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1388 "(unauthorized access)");
1392 SGPropertyNode *targetnode;
1393 if (arg->hasValue("targetnode"))
1394 targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1396 targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1399 readProperties(file.c_str(), targetnode, true);
1400 } catch (const sg_exception &e) {
1401 SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1410 * An fgcommand to allow saving of xml files via nasal,
1411 * the file's structure will be determined based on what's
1412 * encountered in the passed (source) property tree node
1414 * @param filename a string to hold the complete path & filename of the (new)
1416 * @param sourcenode a string pointing to a location within the property tree
1417 * where to find the nodes that should be written recursively into an XML file
1418 * @param data if no sourcenode is given, then the file contents are taken from
1419 * the argument tree's "data" node.
1423 do_save_xml_from_proptree(const SGPropertyNode * arg)
1425 SGPath file(arg->getStringValue("filename"));
1426 if (file.str().empty())
1429 if (file.extension() != "xml")
1430 file.concat(".xml");
1432 if (!fgValidatePath(file.c_str(), true)) {
1433 SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1434 "(unauthorized access)");
1438 SGPropertyNode *sourcenode;
1439 if (arg->hasValue("sourcenode"))
1440 sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1441 else if (arg->getNode("data", false))
1442 sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1447 writeProperties (file.c_str(), sourcenode, true);
1448 } catch (const sg_exception &e) {
1449 SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1457 do_press_cockpit_button (const SGPropertyNode *arg)
1459 const char *prefix = arg->getStringValue("prefix");
1461 if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1464 string prop = string(prefix) + "-button";
1467 if (arg->getBoolValue("latching"))
1468 value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1472 fgSetDouble(prop.c_str(), value);
1473 fgSetBool(arg->getStringValue("discrete"), value > 0);
1479 do_release_cockpit_button (const SGPropertyNode *arg)
1481 const char *prefix = arg->getStringValue("prefix");
1483 if (arg->getBoolValue("guarded")) {
1484 string prop = string(prefix) + "-guard";
1485 if (fgGetDouble(prop.c_str()) < 1) {
1486 fgSetDouble(prop.c_str(), 1);
1491 if (! arg->getBoolValue("latching")) {
1492 fgSetDouble((string(prefix) + "-button").c_str(), 0);
1493 fgSetBool(arg->getStringValue("discrete"), false);
1500 ////////////////////////////////////////////////////////////////////////
1502 ////////////////////////////////////////////////////////////////////////
1506 * Table of built-in commands.
1508 * New commands do not have to be added here; any module in the application
1509 * can add a new command using globals->get_commands()->addCommand(...).
1513 SGCommandMgr::command_t command;
1515 { "null", do_null },
1516 { "nasal", do_nasal },
1517 { "exit", do_exit },
1518 { "reset", do_reset },
1519 { "reinit", do_reinit },
1520 { "suspend", do_reinit },
1521 { "resume", do_reinit },
1522 { "load", do_load },
1523 { "save", do_save },
1524 { "panel-load", do_panel_load },
1525 { "panel-mouse-click", do_panel_mouse_click },
1526 { "preferences-load", do_preferences_load },
1527 { "view-cycle", do_view_cycle },
1528 { "screen-capture", do_screen_capture },
1529 { "hires-screen-capture", do_hires_screen_capture },
1530 { "tile-cache-reload", do_tile_cache_reload },
1531 { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1532 { "set-outside-air-temp-degc", do_set_oat_degc },
1533 { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1534 { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1535 { "timeofday", do_timeofday },
1536 { "property-toggle", do_property_toggle },
1537 { "property-assign", do_property_assign },
1538 { "property-adjust", do_property_adjust },
1539 { "property-multiply", do_property_multiply },
1540 { "property-swap", do_property_swap },
1541 { "property-scale", do_property_scale },
1542 { "property-cycle", do_property_cycle },
1543 { "property-randomize", do_property_randomize },
1544 { "data-logging-commit", do_data_logging_commit },
1545 { "dialog-new", do_dialog_new },
1546 { "dialog-show", do_dialog_show },
1547 { "dialog-close", do_dialog_close },
1548 { "dialog-update", do_dialog_update },
1549 { "dialog-apply", do_dialog_apply },
1550 { "gui-redraw", do_gui_redraw },
1551 { "add-model", do_add_model },
1552 { "set-cursor", do_set_cursor },
1553 { "play-audio-sample", do_play_audio_sample },
1554 { "presets-commit", do_presets_commit },
1555 { "log-level", do_log_level },
1556 { "replay", do_replay },
1557 { "decrease-visibility", do_decrease_visibility },
1558 { "increase-visibility", do_increase_visibility },
1559 { "hud-init", do_hud_init },
1560 { "hud-init2", do_hud_init2 },
1561 { "loadxml", do_load_xml_to_proptree},
1562 { "savexml", do_save_xml_from_proptree },
1563 { "press-cockpit-button", do_press_cockpit_button },
1564 { "release-cockpit-button", do_release_cockpit_button },
1565 { "dump-scenegraph", do_dump_scene_graph },
1566 { "dump-terrainbranch", do_dump_terrain_branch },
1567 { 0, 0 } // zero-terminated
1572 * Initialize the default built-in commands.
1574 * Other commands may be added by other parts of the application.
1579 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
1580 for (int i = 0; built_ins[i].name != 0; i++) {
1581 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
1582 globals->get_commands()->addCommand(built_ins[i].name,
1583 built_ins[i].command);
1586 typedef bool (*dummy)();
1587 fgTie( "/command/view/next", dummy(0), do_view_next );
1588 fgTie( "/command/view/prev", dummy(0), do_view_prev );
1591 // end of fg_commands.cxx