1 // fg_commands.cxx - internal FGFS commands.
3 #include <string.h> // strcmp()
5 #include <simgear/compiler.h>
6 #include <simgear/misc/exception.hxx>
11 #include <simgear/sg_inlines.h>
12 #include <simgear/debug/logstream.hxx>
13 #include <simgear/math/sg_random.h>
14 #include <simgear/misc/commands.hxx>
15 #include <simgear/props/props.hxx>
17 #include <Cockpit/panel.hxx>
18 #include <Cockpit/panel_io.hxx>
19 #include <FDM/flight.hxx>
21 #include <GUI/new_gui.hxx>
22 #include <GUI/dialog.hxx>
23 #include <Replay/replay.hxx>
24 #include <Scenery/tilemgr.hxx>
25 #if defined(HAVE_PLIB_PSL)
26 # include <Scripting/scriptmgr.hxx>
28 #include <Time/tmp.hxx>
30 #include "fg_init.hxx"
31 #include "fg_commands.hxx"
32 #include "fg_props.hxx"
35 SG_USING_STD(ifstream);
36 SG_USING_STD(ofstream);
38 #include "fg_props.hxx"
40 #include "globals.hxx"
42 #include "viewmgr.hxx"
46 ////////////////////////////////////////////////////////////////////////
47 // Static helper functions.
48 ////////////////////////////////////////////////////////////////////////
51 static inline SGPropertyNode *
52 get_prop (const SGPropertyNode * arg)
54 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
57 static inline SGPropertyNode *
58 get_prop2 (const SGPropertyNode * arg)
60 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
65 * Get a double value and split it as required.
68 split_value (double full_value, const char * mask,
69 double * unmodifiable, double * modifiable)
71 if (!strcmp("integer", mask)) {
72 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
73 *unmodifiable = full_value - *modifiable;
74 } else if (!strcmp("decimal", mask)) {
75 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
76 *modifiable = full_value - *unmodifiable;
78 if (strcmp("all", mask))
79 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
80 << " assuming 'all'");
82 *modifiable = full_value;
88 * Clamp or wrap a value as specified.
91 limit_value (double * value, const SGPropertyNode * arg)
93 const SGPropertyNode * min_node = arg->getChild("min");
94 const SGPropertyNode * max_node = arg->getChild("max");
96 bool wrap = arg->getBoolValue("wrap");
98 if (min_node == 0 || max_node == 0)
101 if (wrap) { // wrap such that min <= x < max
102 double min_val = min_node->getDoubleValue();
103 double max_val = max_node->getDoubleValue();
104 double resolution = arg->getDoubleValue("resolution");
105 if (resolution > 0.0) {
106 // snap to (min + N*resolution), taking special care to handle imprecision
107 int n = (int)floor((*value - min_val) / resolution + 0.5);
108 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
109 SG_NORMALIZE_RANGE(n, 0, steps);
110 *value = min_val + resolution * n;
112 // plain circular wrapping
113 SG_NORMALIZE_RANGE(*value, min_val, max_val);
115 } else { // clamp such that min <= x <= max
116 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
117 *value = min_node->getDoubleValue();
118 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
119 *value = max_node->getDoubleValue();
124 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
126 switch (value1->getType()) {
127 case SGPropertyNode::BOOL:
128 return (value1->getBoolValue() == value2->getBoolValue());
129 case SGPropertyNode::INT:
130 return (value1->getIntValue() == value2->getIntValue());
131 case SGPropertyNode::LONG:
132 return (value1->getLongValue() == value2->getLongValue());
133 case SGPropertyNode::FLOAT:
134 return (value1->getFloatValue() == value2->getFloatValue());
135 case SGPropertyNode::DOUBLE:
136 return (value1->getDoubleValue() == value2->getDoubleValue());
138 return !strcmp(value1->getStringValue(), value2->getStringValue());
144 ////////////////////////////////////////////////////////////////////////
145 // Command implementations.
146 ////////////////////////////////////////////////////////////////////////
150 * Built-in command: do nothing.
153 do_null (const SGPropertyNode * arg)
158 #if defined(HAVE_PLIB_PSL)
160 * Built-in command: run a PSL script.
162 * script: the PSL script to execute
165 do_script (const SGPropertyNode * arg)
167 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
168 return mgr->run(arg->getStringValue("script"));
170 #endif // HAVE_PLIB_PSL
174 * Built-in command: exit FlightGear.
176 * status: the exit status to return to the operating system (defaults to 0)
179 do_exit (const SGPropertyNode * arg)
181 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
182 fgExit(arg->getIntValue("status", 0));
188 * Built-in command: reinitialize one or more subsystems.
190 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
191 * none is specified, reinitialize all of them.
194 do_reinit (const SGPropertyNode * arg)
198 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
199 if (subsystems.size() == 0) {
200 globals->get_subsystem_mgr()->reinit();
202 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
203 const char * name = subsystems[i]->getStringValue();
204 FGSubsystem * subsystem = globals->get_subsystem(name);
205 if (subsystem == 0) {
207 SG_LOG( SG_GENERAL, SG_ALERT,
208 "Subsystem " << name << "not found" );
219 * Built-in command: suspend one or more subsystems.
221 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
224 do_suspend (const SGPropertyNode * arg)
228 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
229 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
230 const char * name = subsystems[i]->getStringValue();
231 FGSubsystem * subsystem = globals->get_subsystem(name);
232 if (subsystem == 0) {
234 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
236 subsystem->suspend();
243 * Built-in command: suspend one or more subsystems.
245 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
248 do_resume (const SGPropertyNode * arg)
252 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
253 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
254 const char * name = subsystems[i]->getStringValue();
255 FGSubsystem * subsystem = globals->get_subsystem(name);
256 if (subsystem == 0) {
258 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
268 * Built-in command: load flight.
270 * file (optional): the name of the file to load (relative to current
271 * directory). Defaults to "fgfs.sav"
274 do_load (const SGPropertyNode * arg)
276 const string &file = arg->getStringValue("file", "fgfs.sav");
277 ifstream input(file.c_str());
278 if (input.good() && fgLoadFlight(input)) {
280 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
283 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
290 * Built-in command: save flight.
292 * file (optional): the name of the file to save (relative to the
293 * current directory). Defaults to "fgfs.sav".
296 do_save (const SGPropertyNode * arg)
298 const string &file = arg->getStringValue("file", "fgfs.sav");
299 bool write_all = arg->getBoolValue("write-all", false);
300 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
301 ofstream output(file.c_str());
302 if (output.good() && fgSaveFlight(output, write_all)) {
304 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
307 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
314 * Built-in command: (re)load the panel.
316 * path (optional): the file name to load the panel from
317 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
318 * and if that's unspecified, to "Panels/Default/default.xml".
321 do_panel_load (const SGPropertyNode * arg)
324 arg->getStringValue("path",
325 fgGetString("/sim/panel/path",
326 "Panels/Default/default.xml"));
327 FGPanel * new_panel = fgReadPanel(panel_path);
328 if (new_panel == 0) {
329 SG_LOG(SG_INPUT, SG_ALERT,
330 "Error reading new panel from " << panel_path);
333 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
334 globals->get_current_panel()->unbind();
335 delete globals->get_current_panel();
336 globals->set_current_panel( new_panel );
337 globals->get_current_panel()->bind();
343 * Built-in command: pass a mouse click to the panel.
345 * button: the mouse button number, zero-based.
346 * is-down: true if the button is down, false if it is up.
347 * x-pos: the x position of the mouse click.
348 * y-pos: the y position of the mouse click.
351 do_panel_mouse_click (const SGPropertyNode * arg)
353 if (globals->get_current_panel() != 0)
354 return globals->get_current_panel()
355 ->doMouseAction(arg->getIntValue("button"),
356 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
357 arg->getIntValue("x-pos"),
358 arg->getIntValue("y-pos"));
365 * Built-in command: (re)load preferences.
367 * path (optional): the file name to load the panel from (relative
368 * to FG_ROOT). Defaults to "preferences.xml".
371 do_preferences_load (const SGPropertyNode * arg)
374 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
375 globals->get_props());
376 } catch (const sg_exception &e) {
377 guiErrorMessage("Error reading global preferences: ", e);
380 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
388 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
389 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
390 if ( globals->get_viewmgr()->get_current() == 1 ) {
391 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
399 globals->get_current_view()->setHeadingOffset_deg(0.0);
400 globals->get_viewmgr()->next_view();
401 fix_hud_visibility();
402 globals->get_tile_mgr()->refresh_view_timestamps();
408 globals->get_current_view()->setHeadingOffset_deg(0.0);
409 globals->get_viewmgr()->prev_view();
410 fix_hud_visibility();
411 globals->get_tile_mgr()->refresh_view_timestamps();
415 * Built-in command: cycle view.
418 do_view_cycle (const SGPropertyNode * arg)
420 globals->get_current_view()->setHeadingOffset_deg(0.0);
421 globals->get_viewmgr()->next_view();
422 fix_hud_visibility();
423 globals->get_tile_mgr()->refresh_view_timestamps();
424 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
429 * Built-in command: capture screen.
432 do_screen_capture (const SGPropertyNode * arg)
440 * Reload the tile cache.
443 do_tile_cache_reload (const SGPropertyNode * arg)
445 static const SGPropertyNode *master_freeze
446 = fgGetNode("/sim/freeze/master");
447 bool freeze = master_freeze->getBoolValue();
448 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
450 fgSetBool("/sim/freeze/master", true);
453 if ( globals->get_tile_mgr()->init() ) {
454 // Load the local scenery data
455 double visibility_meters = fgGetDouble("/environment/visibility-m");
456 globals->get_tile_mgr()->update( visibility_meters );
458 SG_LOG( SG_GENERAL, SG_ALERT,
459 "Error in Tile Manager initialization!" );
464 fgSetBool("/sim/freeze/master", false);
471 * Update the lighting manually.
474 do_lighting_update (const SGPropertyNode * arg)
476 fgUpdateSkyAndLightingParams();
482 * Built-in command: toggle a bool property value.
484 * property: The name of the property to toggle.
487 do_property_toggle (const SGPropertyNode * arg)
489 SGPropertyNode * prop = get_prop(arg);
490 return prop->setBoolValue(!prop->getBoolValue());
495 * Built-in command: assign a value to a property.
497 * property: the name of the property to assign.
498 * value: the value to assign; or
499 * property[1]: the property to copy from.
502 do_property_assign (const SGPropertyNode * arg)
504 SGPropertyNode * prop = get_prop(arg);
505 const SGPropertyNode * prop2 = get_prop2(arg);
506 const SGPropertyNode * value = arg->getNode("value");
509 return prop->setUnspecifiedValue(value->getStringValue());
511 return prop->setUnspecifiedValue(prop2->getStringValue());
518 * Built-in command: increment or decrement a property value.
520 * If the 'step' argument is present, it will be used; otherwise,
521 * the command uses 'offset' and 'factor', usually from the mouse.
523 * property: the name of the property to increment or decrement.
524 * step: the amount of the increment or decrement (default: 0).
525 * offset: offset from the current setting (used for the mouse; multiplied
527 * factor: scaling amount for the offset (defaults to 1).
528 * min: the minimum allowed value (default: no minimum).
529 * max: the maximum allowed value (default: no maximum).
530 * mask: 'integer' to apply only to the left of the decimal point,
531 * 'decimal' to apply only to the right of the decimal point,
532 * or 'all' to apply to the whole number (the default).
533 * wrap: true if the value should be wrapped when it passes min or max;
534 * both min and max must be present for this to work (default:
538 do_property_adjust (const SGPropertyNode * arg)
540 SGPropertyNode * prop = get_prop(arg);
543 if (arg->hasValue("step"))
544 amount = arg->getDoubleValue("step");
546 amount = (arg->getDoubleValue("factor", 1)
547 * arg->getDoubleValue("offset"));
549 double unmodifiable, modifiable;
550 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
551 &unmodifiable, &modifiable);
552 modifiable += amount;
553 limit_value(&modifiable, arg);
555 prop->setDoubleValue(unmodifiable + modifiable);
562 * Built-in command: multiply a property value.
564 * property: the name of the property to multiply.
565 * factor: the amount by which to multiply.
566 * min: the minimum allowed value (default: no minimum).
567 * max: the maximum allowed value (default: no maximum).
568 * mask: 'integer' to apply only to the left of the decimal point,
569 * 'decimal' to apply only to the right of the decimal point,
570 * or 'all' to apply to the whole number (the default).
571 * wrap: true if the value should be wrapped when it passes min or max;
572 * both min and max must be present for this to work (default:
576 do_property_multiply (const SGPropertyNode * arg)
578 SGPropertyNode * prop = get_prop(arg);
579 double factor = arg->getDoubleValue("factor", 1);
581 double unmodifiable, modifiable;
582 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
583 &unmodifiable, &modifiable);
584 modifiable *= factor;
585 limit_value(&modifiable, arg);
587 prop->setDoubleValue(unmodifiable + modifiable);
594 * Built-in command: swap two property values.
596 * property[0]: the name of the first property.
597 * property[1]: the name of the second property.
600 do_property_swap (const SGPropertyNode * arg)
602 SGPropertyNode * prop1 = get_prop(arg);
603 SGPropertyNode * prop2 = get_prop2(arg);
605 // FIXME: inefficient
606 const string & tmp = prop1->getStringValue();
607 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
608 prop2->setUnspecifiedValue(tmp.c_str()));
613 * Built-in command: Set a property to an axis or other moving input.
615 * property: the name of the property to set.
616 * setting: the current input setting, usually between -1.0 and 1.0.
617 * offset: the offset to shift by, before applying the factor.
618 * factor: the factor to multiply by (use negative to reverse).
621 do_property_scale (const SGPropertyNode * arg)
623 SGPropertyNode * prop = get_prop(arg);
624 double setting = arg->getDoubleValue("setting");
625 double offset = arg->getDoubleValue("offset", 0.0);
626 double factor = arg->getDoubleValue("factor", 1.0);
627 bool squared = arg->getBoolValue("squared", false);
628 int power = arg->getIntValue("power", (squared ? 2 : 1));
630 int sign = (setting < 0 ? -1 : 1);
636 setting = setting * setting * sign;
639 setting = setting * setting * setting;
642 setting = setting * setting * setting * setting * sign;
645 setting = pow(setting, power);
646 if ((power % 2) == 0)
651 return prop->setDoubleValue((setting + offset) * factor);
656 * Built-in command: cycle a property through a set of values.
658 * If the current value isn't in the list, the cycle will
659 * (re)start from the beginning.
661 * property: the name of the property to cycle.
662 * value[*]: the list of values to cycle through.
665 do_property_cycle (const SGPropertyNode * arg)
667 SGPropertyNode * prop = get_prop(arg);
668 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
670 int nSelections = values.size();
672 if (nSelections < 1) {
673 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
677 // Try to find the current selection
678 for (int i = 0; i < nSelections; i++) {
679 if (compare_values(prop, values[i])) {
685 // Default or wrap to the first selection
686 if (selection < 0 || selection >= nSelections)
689 prop->setUnspecifiedValue(values[selection]->getStringValue());
695 * Built-in command: randomize a numeric property value.
697 * property: the name of the property value to randomize.
698 * min: the minimum allowed value.
699 * max: the maximum allowed value.
702 do_property_randomize (const SGPropertyNode * arg)
704 SGPropertyNode * prop = get_prop(arg);
705 double min = arg->getDoubleValue("min", DBL_MIN);
706 double max = arg->getDoubleValue("max", DBL_MAX);
707 prop->setDoubleValue(sg_random() * (max - min) + min);
713 * Built-in command: Show an XML-configured dialog.
715 * dialog-name: the name of the GUI dialog to display.
718 do_dialog_show (const SGPropertyNode * arg)
720 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
721 gui->showDialog(arg->getStringValue("dialog-name"));
727 * Built-in Command: Hide the active XML-configured dialog.
730 do_dialog_close (const SGPropertyNode * arg)
732 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
733 return gui->closeActiveDialog();
738 * Update a value in the active XML-configured dialog.
740 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
743 do_dialog_update (const SGPropertyNode * arg)
745 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
746 FGDialog * dialog = gui->getActiveDialog();
748 if (arg->hasValue("object-name")) {
749 dialog->updateValue(arg->getStringValue("object-name"));
751 dialog->updateValues();
761 * Apply a value in the active XML-configured dialog.
763 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
766 do_dialog_apply (const SGPropertyNode * arg)
768 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
769 FGDialog * dialog = gui->getActiveDialog();
771 if (arg->hasValue("object-name")) {
772 const char * name = arg->getStringValue("object-name");
773 dialog->applyValue(name);
774 dialog->updateValue(name);
776 dialog->applyValues();
777 dialog->updateValues();
787 * Built-in command: commit presets (read from in /sim/presets/)
790 do_presets_commit (const SGPropertyNode * arg)
792 // unbind the current fdm state so property changes
793 // don't get lost when we subsequently delete this fdm
794 // and create a new one.
795 cur_fdm_state->unbind();
797 // set position from presets
801 fgReInitSubsystems();
803 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
806 if ( ! fgGetBool("/sim/presets/onground") ) {
807 fgSetBool( "/sim/freeze/master", true );
808 fgSetBool( "/sim/freeze/clock", true );
816 * Built-in command: set log level (0 ... 7)
819 do_log_level (const SGPropertyNode * arg)
821 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
827 * Built-in command: replay the FDR buffer
830 do_replay (const SGPropertyNode * arg)
832 // freeze the master fdm
833 fgSetBool( "/sim/freeze/replay", true );
835 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
837 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
838 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
839 fgSetDouble( "/sim/replay/time", r->get_start_time() );
841 cout << "start = " << r->get_start_time()
842 << " end = " << r->get_end_time() << endl;
850 do_decrease_visability (const SGPropertyNode * arg)
852 double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
853 fgSetDouble("/environment/visibility-m", new_value);
854 fgDefaultWeatherValue("visibility-m", new_value);
855 globals->get_subsystem("environment")->reinit();
859 do_increase_visability (const SGPropertyNode * arg)
861 double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
862 fgSetDouble("/environment/visibility-m", new_value);
863 fgDefaultWeatherValue("visibility-m", new_value);
864 globals->get_subsystem("environment")->reinit();
868 ////////////////////////////////////////////////////////////////////////
870 ////////////////////////////////////////////////////////////////////////
874 * Table of built-in commands.
876 * New commands do not have to be added here; any module in the application
877 * can add a new command using globals->get_commands()->addCommand(...).
881 SGCommandMgr::command_t command;
884 #if defined(HAVE_PLIB_PSL)
885 { "script", do_script },
886 #endif // HAVE_PLIB_PSL
888 { "reinit", do_reinit },
889 { "suspend", do_reinit },
890 { "resume", do_reinit },
893 { "panel-load", do_panel_load },
894 { "panel-mouse-click", do_panel_mouse_click },
895 { "preferences-load", do_preferences_load },
896 { "view-cycle", do_view_cycle },
897 { "screen-capture", do_screen_capture },
898 { "tile-cache-reload", do_tile_cache_reload },
899 { "lighting-update", do_lighting_update },
900 { "property-toggle", do_property_toggle },
901 { "property-assign", do_property_assign },
902 { "property-adjust", do_property_adjust },
903 { "property-multiply", do_property_multiply },
904 { "property-swap", do_property_swap },
905 { "property-scale", do_property_scale },
906 { "property-cycle", do_property_cycle },
907 { "property-randomize", do_property_randomize },
908 { "dialog-show", do_dialog_show },
909 { "dialog-close", do_dialog_close },
910 { "dialog-show", do_dialog_show },
911 { "dialog-update", do_dialog_update },
912 { "dialog-apply", do_dialog_apply },
913 { "presets-commit", do_presets_commit },
914 { "log-level", do_log_level },
915 { "replay", do_replay },
916 { "decrease-visibility", do_decrease_visability },
917 { "increase-visibility", do_increase_visability },
918 { 0, 0 } // zero-terminated
923 * Initialize the default built-in commands.
925 * Other commands may be added by other parts of the application.
930 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
931 for (int i = 0; built_ins[i].name != 0; i++) {
932 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
933 globals->get_commands()->addCommand(built_ins[i].name,
934 built_ins[i].command);
937 typedef bool (*dummy)();
938 fgTie( "/command/view/next", dummy(0), do_view_next );
939 fgTie( "/command/view/prev", dummy(0), do_view_prev );
942 // end of fg_commands.cxx