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"
34 SG_USING_STD(ifstream);
35 SG_USING_STD(ofstream);
37 #include "fg_props.hxx"
39 #include "globals.hxx"
41 #include "viewmgr.hxx"
45 ////////////////////////////////////////////////////////////////////////
46 // Static helper functions.
47 ////////////////////////////////////////////////////////////////////////
50 static inline SGPropertyNode *
51 get_prop (const SGPropertyNode * arg)
53 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
56 static inline SGPropertyNode *
57 get_prop2 (const SGPropertyNode * arg)
59 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
64 * Get a double value and split it as required.
67 split_value (double full_value, const char * mask,
68 double * unmodifiable, double * modifiable)
70 if (!strcmp("integer", mask)) {
71 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
72 *unmodifiable = full_value - *modifiable;
73 } else if (!strcmp("decimal", mask)) {
74 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
75 *modifiable = full_value - *unmodifiable;
77 if (strcmp("all", mask))
78 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
79 << " assuming 'all'");
81 *modifiable = full_value;
87 * Clamp or wrap a value as specified.
90 limit_value (double * value, const SGPropertyNode * arg)
92 const SGPropertyNode * min_node = arg->getChild("min");
93 const SGPropertyNode * max_node = arg->getChild("max");
95 bool wrap = arg->getBoolValue("wrap");
97 if (min_node == 0 || max_node == 0)
100 if (wrap) { // wrap such that min <= x < max
101 double min_val = min_node->getDoubleValue();
102 double max_val = max_node->getDoubleValue();
103 double resolution = arg->getDoubleValue("resolution");
104 if (resolution > 0.0) {
105 // snap to (min + N*resolution), taking special care to handle imprecision
106 int n = (int)floor((*value - min_val) / resolution + 0.5);
107 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
108 SG_NORMALIZE_RANGE(n, 0, steps);
109 *value = min_val + resolution * n;
111 // plain circular wrapping
112 SG_NORMALIZE_RANGE(*value, min_val, max_val);
114 } else { // clamp such that min <= x <= max
115 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
116 *value = min_node->getDoubleValue();
117 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
118 *value = max_node->getDoubleValue();
123 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
125 switch (value1->getType()) {
126 case SGPropertyNode::BOOL:
127 return (value1->getBoolValue() == value2->getBoolValue());
128 case SGPropertyNode::INT:
129 return (value1->getIntValue() == value2->getIntValue());
130 case SGPropertyNode::LONG:
131 return (value1->getLongValue() == value2->getLongValue());
132 case SGPropertyNode::FLOAT:
133 return (value1->getFloatValue() == value2->getFloatValue());
134 case SGPropertyNode::DOUBLE:
135 return (value1->getDoubleValue() == value2->getDoubleValue());
137 return !strcmp(value1->getStringValue(), value2->getStringValue());
143 ////////////////////////////////////////////////////////////////////////
144 // Command implementations.
145 ////////////////////////////////////////////////////////////////////////
149 * Built-in command: do nothing.
152 do_null (const SGPropertyNode * arg)
157 #if defined(HAVE_PLIB_PSL)
159 * Built-in command: run a PSL script.
161 * script: the PSL script to execute
164 do_script (const SGPropertyNode * arg)
166 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
167 return mgr->run(arg->getStringValue("script"));
169 #endif // HAVE_PLIB_PSL
173 * Built-in command: exit FlightGear.
175 * status: the exit status to return to the operating system (defaults to 0)
178 do_exit (const SGPropertyNode * arg)
180 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
181 fgExit(arg->getIntValue("status", 0));
187 * Built-in command: reinitialize one or more subsystems.
189 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
190 * none is specified, reinitialize all of them.
193 do_reinit (const SGPropertyNode * arg)
197 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
198 if (subsystems.size() == 0) {
199 globals->get_subsystem_mgr()->reinit();
201 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
202 const char * name = subsystems[i]->getStringValue();
203 FGSubsystem * subsystem = globals->get_subsystem(name);
204 if (subsystem == 0) {
206 SG_LOG( SG_GENERAL, SG_ALERT,
207 "Subsystem " << name << "not found" );
218 * Built-in command: suspend one or more subsystems.
220 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
223 do_suspend (const SGPropertyNode * arg)
227 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
228 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
229 const char * name = subsystems[i]->getStringValue();
230 FGSubsystem * subsystem = globals->get_subsystem(name);
231 if (subsystem == 0) {
233 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
235 subsystem->suspend();
242 * Built-in command: suspend one or more subsystems.
244 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
247 do_resume (const SGPropertyNode * arg)
251 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
252 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
253 const char * name = subsystems[i]->getStringValue();
254 FGSubsystem * subsystem = globals->get_subsystem(name);
255 if (subsystem == 0) {
257 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
267 * Built-in command: load flight.
269 * file (optional): the name of the file to load (relative to current
270 * directory). Defaults to "fgfs.sav"
273 do_load (const SGPropertyNode * arg)
275 const string &file = arg->getStringValue("file", "fgfs.sav");
276 ifstream input(file.c_str());
277 if (input.good() && fgLoadFlight(input)) {
279 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
282 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
289 * Built-in command: save flight.
291 * file (optional): the name of the file to save (relative to the
292 * current directory). Defaults to "fgfs.sav".
295 do_save (const SGPropertyNode * arg)
297 const string &file = arg->getStringValue("file", "fgfs.sav");
298 bool write_all = arg->getBoolValue("write-all", false);
299 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
300 ofstream output(file.c_str());
301 if (output.good() && fgSaveFlight(output, write_all)) {
303 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
306 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
313 * Built-in command: (re)load the panel.
315 * path (optional): the file name to load the panel from
316 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
317 * and if that's unspecified, to "Panels/Default/default.xml".
320 do_panel_load (const SGPropertyNode * arg)
323 arg->getStringValue("path",
324 fgGetString("/sim/panel/path",
325 "Panels/Default/default.xml"));
326 FGPanel * new_panel = fgReadPanel(panel_path);
327 if (new_panel == 0) {
328 SG_LOG(SG_INPUT, SG_ALERT,
329 "Error reading new panel from " << panel_path);
332 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
333 globals->get_current_panel()->unbind();
334 delete globals->get_current_panel();
335 globals->set_current_panel( new_panel );
336 globals->get_current_panel()->bind();
342 * Built-in command: pass a mouse click to the panel.
344 * button: the mouse button number, zero-based.
345 * is-down: true if the button is down, false if it is up.
346 * x-pos: the x position of the mouse click.
347 * y-pos: the y position of the mouse click.
350 do_panel_mouse_click (const SGPropertyNode * arg)
352 if (globals->get_current_panel() != 0)
353 return globals->get_current_panel()
354 ->doMouseAction(arg->getIntValue("button"),
355 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
356 arg->getIntValue("x-pos"),
357 arg->getIntValue("y-pos"));
364 * Built-in command: (re)load preferences.
366 * path (optional): the file name to load the panel from (relative
367 * to FG_ROOT). Defaults to "preferences.xml".
370 do_preferences_load (const SGPropertyNode * arg)
373 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
374 globals->get_props());
375 } catch (const sg_exception &e) {
376 guiErrorMessage("Error reading global preferences: ", e);
379 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
387 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
388 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
389 if ( globals->get_viewmgr()->get_current() == 1 ) {
390 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
398 globals->get_current_view()->setHeadingOffset_deg(0.0);
399 globals->get_viewmgr()->next_view();
400 fix_hud_visibility();
401 globals->get_tile_mgr()->refresh_view_timestamps();
407 globals->get_current_view()->setHeadingOffset_deg(0.0);
408 globals->get_viewmgr()->prev_view();
409 fix_hud_visibility();
410 globals->get_tile_mgr()->refresh_view_timestamps();
414 * Built-in command: cycle view.
417 do_view_cycle (const SGPropertyNode * arg)
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();
423 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
428 * Built-in command: capture screen.
431 do_screen_capture (const SGPropertyNode * arg)
439 * Reload the tile cache.
442 do_tile_cache_reload (const SGPropertyNode * arg)
444 static const SGPropertyNode *master_freeze
445 = fgGetNode("/sim/freeze/master");
446 bool freeze = master_freeze->getBoolValue();
447 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
449 fgSetBool("/sim/freeze/master", true);
452 if ( globals->get_tile_mgr()->init() ) {
453 // Load the local scenery data
454 double visibility_meters = fgGetDouble("/environment/visibility-m");
455 globals->get_tile_mgr()->update( visibility_meters );
457 SG_LOG( SG_GENERAL, SG_ALERT,
458 "Error in Tile Manager initialization!" );
463 fgSetBool("/sim/freeze/master", false);
470 * Update the lighting manually.
473 do_lighting_update (const SGPropertyNode * arg)
475 fgUpdateSkyAndLightingParams();
481 * Built-in command: toggle a bool property value.
483 * property: The name of the property to toggle.
486 do_property_toggle (const SGPropertyNode * arg)
488 SGPropertyNode * prop = get_prop(arg);
489 return prop->setBoolValue(!prop->getBoolValue());
494 * Built-in command: assign a value to a property.
496 * property: the name of the property to assign.
497 * value: the value to assign; or
498 * property[1]: the property to copy from.
501 do_property_assign (const SGPropertyNode * arg)
503 SGPropertyNode * prop = get_prop(arg);
504 const SGPropertyNode * prop2 = get_prop2(arg);
505 const SGPropertyNode * value = arg->getNode("value");
508 return prop->setUnspecifiedValue(value->getStringValue());
510 return prop->setUnspecifiedValue(prop2->getStringValue());
517 * Built-in command: increment or decrement a property value.
519 * If the 'step' argument is present, it will be used; otherwise,
520 * the command uses 'offset' and 'factor', usually from the mouse.
522 * property: the name of the property to increment or decrement.
523 * step: the amount of the increment or decrement (default: 0).
524 * offset: offset from the current setting (used for the mouse; multiplied
526 * factor: scaling amount for the offset (defaults to 1).
527 * min: the minimum allowed value (default: no minimum).
528 * max: the maximum allowed value (default: no maximum).
529 * mask: 'integer' to apply only to the left of the decimal point,
530 * 'decimal' to apply only to the right of the decimal point,
531 * or 'all' to apply to the whole number (the default).
532 * wrap: true if the value should be wrapped when it passes min or max;
533 * both min and max must be present for this to work (default:
537 do_property_adjust (const SGPropertyNode * arg)
539 SGPropertyNode * prop = get_prop(arg);
542 if (arg->hasValue("step"))
543 amount = arg->getDoubleValue("step");
545 amount = (arg->getDoubleValue("factor", 1)
546 * arg->getDoubleValue("offset"));
548 double unmodifiable, modifiable;
549 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
550 &unmodifiable, &modifiable);
551 modifiable += amount;
552 limit_value(&modifiable, arg);
554 prop->setDoubleValue(unmodifiable + modifiable);
561 * Built-in command: multiply a property value.
563 * property: the name of the property to multiply.
564 * factor: the amount by which to multiply.
565 * min: the minimum allowed value (default: no minimum).
566 * max: the maximum allowed value (default: no maximum).
567 * mask: 'integer' to apply only to the left of the decimal point,
568 * 'decimal' to apply only to the right of the decimal point,
569 * or 'all' to apply to the whole number (the default).
570 * wrap: true if the value should be wrapped when it passes min or max;
571 * both min and max must be present for this to work (default:
575 do_property_multiply (const SGPropertyNode * arg)
577 SGPropertyNode * prop = get_prop(arg);
578 double factor = arg->getDoubleValue("factor", 1);
580 double unmodifiable, modifiable;
581 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
582 &unmodifiable, &modifiable);
583 modifiable *= factor;
584 limit_value(&modifiable, arg);
586 prop->setDoubleValue(unmodifiable + modifiable);
593 * Built-in command: swap two property values.
595 * property[0]: the name of the first property.
596 * property[1]: the name of the second property.
599 do_property_swap (const SGPropertyNode * arg)
601 SGPropertyNode * prop1 = get_prop(arg);
602 SGPropertyNode * prop2 = get_prop2(arg);
604 // FIXME: inefficient
605 const string & tmp = prop1->getStringValue();
606 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
607 prop2->setUnspecifiedValue(tmp.c_str()));
612 * Built-in command: Set a property to an axis or other moving input.
614 * property: the name of the property to set.
615 * setting: the current input setting, usually between -1.0 and 1.0.
616 * offset: the offset to shift by, before applying the factor.
617 * factor: the factor to multiply by (use negative to reverse).
620 do_property_scale (const SGPropertyNode * arg)
622 SGPropertyNode * prop = get_prop(arg);
623 double setting = arg->getDoubleValue("setting");
624 double offset = arg->getDoubleValue("offset", 0.0);
625 double factor = arg->getDoubleValue("factor", 1.0);
626 bool squared = arg->getBoolValue("squared", false);
627 int power = arg->getIntValue("power", (squared ? 2 : 1));
629 int sign = (setting < 0 ? -1 : 1);
635 setting = setting * setting * sign;
638 setting = setting * setting * setting;
641 setting = setting * setting * setting * setting * sign;
644 setting = pow(setting, power);
645 if ((power % 2) == 0)
650 return prop->setDoubleValue((setting + offset) * factor);
655 * Built-in command: cycle a property through a set of values.
657 * If the current value isn't in the list, the cycle will
658 * (re)start from the beginning.
660 * property: the name of the property to cycle.
661 * value[*]: the list of values to cycle through.
664 do_property_cycle (const SGPropertyNode * arg)
666 SGPropertyNode * prop = get_prop(arg);
667 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
669 int nSelections = values.size();
671 if (nSelections < 1) {
672 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
676 // Try to find the current selection
677 for (int i = 0; i < nSelections; i++) {
678 if (compare_values(prop, values[i])) {
684 // Default or wrap to the first selection
685 if (selection < 0 || selection >= nSelections)
688 prop->setUnspecifiedValue(values[selection]->getStringValue());
694 * Built-in command: randomize a numeric property value.
696 * property: the name of the property value to randomize.
697 * min: the minimum allowed value.
698 * max: the maximum allowed value.
701 do_property_randomize (const SGPropertyNode * arg)
703 SGPropertyNode * prop = get_prop(arg);
704 double min = arg->getDoubleValue("min", DBL_MIN);
705 double max = arg->getDoubleValue("max", DBL_MAX);
706 prop->setDoubleValue(sg_random() * (max - min) + min);
712 * Built-in command: Show an XML-configured dialog.
714 * dialog-name: the name of the GUI dialog to display.
717 do_dialog_show (const SGPropertyNode * arg)
719 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
720 gui->showDialog(arg->getStringValue("dialog-name"));
726 * Built-in Command: Hide the active XML-configured dialog.
729 do_dialog_close (const SGPropertyNode * arg)
731 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
732 return gui->closeActiveDialog();
737 * Update a value in the active XML-configured dialog.
739 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
742 do_dialog_update (const SGPropertyNode * arg)
744 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
745 FGDialog * dialog = gui->getActiveDialog();
747 if (arg->hasValue("object-name")) {
748 dialog->updateValue(arg->getStringValue("object-name"));
750 dialog->updateValues();
760 * Apply a value in the active XML-configured dialog.
762 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
765 do_dialog_apply (const SGPropertyNode * arg)
767 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
768 FGDialog * dialog = gui->getActiveDialog();
770 if (arg->hasValue("object-name")) {
771 const char * name = arg->getStringValue("object-name");
772 dialog->applyValue(name);
773 dialog->updateValue(name);
775 dialog->applyValues();
776 dialog->updateValues();
786 * Built-in command: commit presets (read from in /sim/presets/)
789 do_presets_commit (const SGPropertyNode * arg)
791 // unbind the current fdm state so property changes
792 // don't get lost when we subsequently delete this fdm
793 // and create a new one.
794 cur_fdm_state->unbind();
796 // set position from presets
800 fgReInitSubsystems();
802 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
805 if ( ! fgGetBool("/sim/presets/onground") ) {
806 fgSetBool( "/sim/freeze/master", true );
807 fgSetBool( "/sim/freeze/clock", true );
815 * Built-in command: set log level (0 ... 7)
818 do_log_level (const SGPropertyNode * arg)
820 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
826 * Built-in command: replay the FDR buffer
829 do_replay (const SGPropertyNode * arg)
831 // freeze the master fdm
832 fgSetBool( "/sim/freeze/replay", true );
834 FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
836 fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
837 fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
838 fgSetDouble( "/sim/replay/time", r->get_start_time() );
840 cout << "start = " << r->get_start_time()
841 << " end = " << r->get_end_time() << endl;
849 ////////////////////////////////////////////////////////////////////////
851 ////////////////////////////////////////////////////////////////////////
855 * Table of built-in commands.
857 * New commands do not have to be added here; any module in the application
858 * can add a new command using globals->get_commands()->addCommand(...).
862 SGCommandMgr::command_t command;
865 #if defined(HAVE_PLIB_PSL)
866 { "script", do_script },
867 #endif // HAVE_PLIB_PSL
869 { "reinit", do_reinit },
870 { "suspend", do_reinit },
871 { "resume", do_reinit },
874 { "panel-load", do_panel_load },
875 { "panel-mouse-click", do_panel_mouse_click },
876 { "preferences-load", do_preferences_load },
877 { "view-cycle", do_view_cycle },
878 { "screen-capture", do_screen_capture },
879 { "tile-cache-reload", do_tile_cache_reload },
880 { "lighting-update", do_lighting_update },
881 { "property-toggle", do_property_toggle },
882 { "property-assign", do_property_assign },
883 { "property-adjust", do_property_adjust },
884 { "property-multiply", do_property_multiply },
885 { "property-swap", do_property_swap },
886 { "property-scale", do_property_scale },
887 { "property-cycle", do_property_cycle },
888 { "property-randomize", do_property_randomize },
889 { "dialog-show", do_dialog_show },
890 { "dialog-close", do_dialog_close },
891 { "dialog-show", do_dialog_show },
892 { "dialog-update", do_dialog_update },
893 { "dialog-apply", do_dialog_apply },
894 { "presets-commit", do_presets_commit },
895 { "log-level", do_log_level },
896 { "replay", do_replay },
897 { 0, 0 } // zero-terminated
902 * Initialize the default built-in commands.
904 * Other commands may be added by other parts of the application.
909 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
910 for (int i = 0; built_ins[i].name != 0; i++) {
911 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
912 globals->get_commands()->addCommand(built_ins[i].name,
913 built_ins[i].command);
916 typedef bool (*dummy)();
917 fgTie( "/command/view/next", dummy(0), do_view_next );
918 fgTie( "/command/view/prev", dummy(0), do_view_prev );
921 // end of fg_commands.cxx