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 <Scenery/tilemgr.hxx>
24 #if defined(HAVE_PLIB_PSL)
25 # include <Scripting/scriptmgr.hxx>
27 #include <Time/tmp.hxx>
29 #include "fg_init.hxx"
30 #include "fg_commands.hxx"
33 SG_USING_STD(ifstream);
34 SG_USING_STD(ofstream);
36 #include "fg_props.hxx"
38 #include "globals.hxx"
40 #include "viewmgr.hxx"
44 ////////////////////////////////////////////////////////////////////////
45 // Static helper functions.
46 ////////////////////////////////////////////////////////////////////////
49 static inline SGPropertyNode *
50 get_prop (const SGPropertyNode * arg)
52 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
55 static inline SGPropertyNode *
56 get_prop2 (const SGPropertyNode * arg)
58 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
63 * Get a double value and split it as required.
66 split_value (double full_value, const char * mask,
67 double * unmodifiable, double * modifiable)
69 if (!strcmp("integer", mask)) {
70 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
71 *unmodifiable = full_value - *modifiable;
72 } else if (!strcmp("decimal", mask)) {
73 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
74 *modifiable = full_value - *unmodifiable;
76 if (strcmp("all", mask))
77 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
78 << " assuming 'all'");
80 *modifiable = full_value;
86 * Clamp or wrap a value as specified.
89 limit_value (double * value, const SGPropertyNode * arg)
91 const SGPropertyNode * min_node = arg->getChild("min");
92 const SGPropertyNode * max_node = arg->getChild("max");
94 bool wrap = arg->getBoolValue("wrap");
96 if (min_node == 0 || max_node == 0)
99 if (wrap) { // wrap such that min <= x < max
100 double min_val = min_node->getDoubleValue();
101 double max_val = max_node->getDoubleValue();
102 double resolution = arg->getDoubleValue("resolution");
103 if (resolution > 0.0) {
104 // snap to (min + N*resolution), taking special care to handle imprecision
105 int n = (int)floor((*value - min_val) / resolution + 0.5);
106 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
107 SG_NORMALIZE_RANGE(n, 0, steps);
108 *value = min_val + resolution * n;
110 // plain circular wrapping
111 SG_NORMALIZE_RANGE(*value, min_val, max_val);
113 } else { // clamp such that min <= x <= max
114 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
115 *value = min_node->getDoubleValue();
116 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
117 *value = max_node->getDoubleValue();
122 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
124 switch (value1->getType()) {
125 case SGPropertyNode::BOOL:
126 return (value1->getBoolValue() == value2->getBoolValue());
127 case SGPropertyNode::INT:
128 return (value1->getIntValue() == value2->getIntValue());
129 case SGPropertyNode::LONG:
130 return (value1->getLongValue() == value2->getLongValue());
131 case SGPropertyNode::FLOAT:
132 return (value1->getFloatValue() == value2->getFloatValue());
133 case SGPropertyNode::DOUBLE:
134 return (value1->getDoubleValue() == value2->getDoubleValue());
136 return !strcmp(value1->getStringValue(), value2->getStringValue());
142 ////////////////////////////////////////////////////////////////////////
143 // Command implementations.
144 ////////////////////////////////////////////////////////////////////////
148 * Built-in command: do nothing.
151 do_null (const SGPropertyNode * arg)
156 #if defined(HAVE_PLIB_PSL)
158 * Built-in command: run a PSL script.
160 * script: the PSL script to execute
163 do_script (const SGPropertyNode * arg)
165 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
166 return mgr->run(arg->getStringValue("script"));
168 #endif // HAVE_PLIB_PSL
172 * Built-in command: exit FlightGear.
174 * status: the exit status to return to the operating system (defaults to 0)
177 do_exit (const SGPropertyNode * arg)
179 SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
180 fgExit(arg->getIntValue("status", 0));
186 * Built-in command: reinitialize one or more subsystems.
188 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
189 * none is specified, reinitialize all of them.
192 do_reinit (const SGPropertyNode * arg)
196 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
197 if (subsystems.size() == 0) {
198 globals->get_subsystem_mgr()->reinit();
200 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
201 const char * name = subsystems[i]->getStringValue();
202 FGSubsystem * subsystem = globals->get_subsystem(name);
203 if (subsystem == 0) {
205 SG_LOG( SG_GENERAL, SG_ALERT,
206 "Subsystem " << name << "not found" );
217 * Built-in command: suspend one or more subsystems.
219 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
222 do_suspend (const SGPropertyNode * arg)
226 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
227 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
228 const char * name = subsystems[i]->getStringValue();
229 FGSubsystem * subsystem = globals->get_subsystem(name);
230 if (subsystem == 0) {
232 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
234 subsystem->suspend();
241 * Built-in command: suspend one or more subsystems.
243 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
246 do_resume (const SGPropertyNode * arg)
250 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
251 for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
252 const char * name = subsystems[i]->getStringValue();
253 FGSubsystem * subsystem = globals->get_subsystem(name);
254 if (subsystem == 0) {
256 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
266 * Built-in command: load flight.
268 * file (optional): the name of the file to load (relative to current
269 * directory). Defaults to "fgfs.sav"
272 do_load (const SGPropertyNode * arg)
274 const string &file = arg->getStringValue("file", "fgfs.sav");
275 ifstream input(file.c_str());
276 if (input.good() && fgLoadFlight(input)) {
278 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
281 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
288 * Built-in command: save flight.
290 * file (optional): the name of the file to save (relative to the
291 * current directory). Defaults to "fgfs.sav".
294 do_save (const SGPropertyNode * arg)
296 const string &file = arg->getStringValue("file", "fgfs.sav");
297 bool write_all = arg->getBoolValue("write-all", false);
298 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
299 ofstream output(file.c_str());
300 if (output.good() && fgSaveFlight(output, write_all)) {
302 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
305 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
312 * Built-in command: (re)load the panel.
314 * path (optional): the file name to load the panel from
315 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
316 * and if that's unspecified, to "Panels/Default/default.xml".
319 do_panel_load (const SGPropertyNode * arg)
322 arg->getStringValue("path",
323 fgGetString("/sim/panel/path",
324 "Panels/Default/default.xml"));
325 FGPanel * new_panel = fgReadPanel(panel_path);
326 if (new_panel == 0) {
327 SG_LOG(SG_INPUT, SG_ALERT,
328 "Error reading new panel from " << panel_path);
331 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
332 globals->get_current_panel()->unbind();
333 delete globals->get_current_panel();
334 globals->set_current_panel( new_panel );
335 globals->get_current_panel()->bind();
341 * Built-in command: pass a mouse click to the panel.
343 * button: the mouse button number, zero-based.
344 * is-down: true if the button is down, false if it is up.
345 * x-pos: the x position of the mouse click.
346 * y-pos: the y position of the mouse click.
349 do_panel_mouse_click (const SGPropertyNode * arg)
351 if (globals->get_current_panel() != 0)
352 return globals->get_current_panel()
353 ->doMouseAction(arg->getIntValue("button"),
354 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
355 arg->getIntValue("x-pos"),
356 arg->getIntValue("y-pos"));
363 * Built-in command: (re)load preferences.
365 * path (optional): the file name to load the panel from (relative
366 * to FG_ROOT). Defaults to "preferences.xml".
369 do_preferences_load (const SGPropertyNode * arg)
372 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
373 globals->get_props());
374 } catch (const sg_exception &e) {
375 guiErrorMessage("Error reading global preferences: ", e);
378 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
386 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
387 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
388 if ( globals->get_viewmgr()->get_current() == 1 ) {
389 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
397 globals->get_current_view()->setHeadingOffset_deg(0.0);
398 globals->get_viewmgr()->next_view();
399 fix_hud_visibility();
400 globals->get_tile_mgr()->refresh_view_timestamps();
406 globals->get_current_view()->setHeadingOffset_deg(0.0);
407 globals->get_viewmgr()->prev_view();
408 fix_hud_visibility();
409 globals->get_tile_mgr()->refresh_view_timestamps();
413 * Built-in command: cycle view.
416 do_view_cycle (const SGPropertyNode * arg)
418 globals->get_current_view()->setHeadingOffset_deg(0.0);
419 globals->get_viewmgr()->next_view();
420 fix_hud_visibility();
421 globals->get_tile_mgr()->refresh_view_timestamps();
422 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
427 * Built-in command: capture screen.
430 do_screen_capture (const SGPropertyNode * arg)
438 * Reload the tile cache.
441 do_tile_cache_reload (const SGPropertyNode * arg)
443 static const SGPropertyNode *master_freeze
444 = fgGetNode("/sim/freeze/master");
445 bool freeze = master_freeze->getBoolValue();
446 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
448 fgSetBool("/sim/freeze/master", true);
451 if ( globals->get_tile_mgr()->init() ) {
452 // Load the local scenery data
453 double visibility_meters = fgGetDouble("/environment/visibility-m");
454 globals->get_tile_mgr()->update( visibility_meters );
456 SG_LOG( SG_GENERAL, SG_ALERT,
457 "Error in Tile Manager initialization!" );
462 fgSetBool("/sim/freeze/master", false);
469 * Update the lighting manually.
472 do_lighting_update (const SGPropertyNode * arg)
474 fgUpdateSkyAndLightingParams();
480 * Built-in command: toggle a bool property value.
482 * property: The name of the property to toggle.
485 do_property_toggle (const SGPropertyNode * arg)
487 SGPropertyNode * prop = get_prop(arg);
488 return prop->setBoolValue(!prop->getBoolValue());
493 * Built-in command: assign a value to a property.
495 * property: the name of the property to assign.
496 * value: the value to assign; or
497 * property[1]: the property to copy from.
500 do_property_assign (const SGPropertyNode * arg)
502 SGPropertyNode * prop = get_prop(arg);
503 const SGPropertyNode * prop2 = get_prop2(arg);
504 const SGPropertyNode * value = arg->getNode("value");
507 return prop->setUnspecifiedValue(value->getStringValue());
509 return prop->setUnspecifiedValue(prop2->getStringValue());
516 * Built-in command: increment or decrement a property value.
518 * If the 'step' argument is present, it will be used; otherwise,
519 * the command uses 'offset' and 'factor', usually from the mouse.
521 * property: the name of the property to increment or decrement.
522 * step: the amount of the increment or decrement (default: 0).
523 * offset: offset from the current setting (used for the mouse; multiplied
525 * factor: scaling amount for the offset (defaults to 1).
526 * min: the minimum allowed value (default: no minimum).
527 * max: the maximum allowed value (default: no maximum).
528 * mask: 'integer' to apply only to the left of the decimal point,
529 * 'decimal' to apply only to the right of the decimal point,
530 * or 'all' to apply to the whole number (the default).
531 * wrap: true if the value should be wrapped when it passes min or max;
532 * both min and max must be present for this to work (default:
536 do_property_adjust (const SGPropertyNode * arg)
538 SGPropertyNode * prop = get_prop(arg);
541 if (arg->hasValue("step"))
542 amount = arg->getDoubleValue("step");
544 amount = (arg->getDoubleValue("factor", 1)
545 * arg->getDoubleValue("offset"));
547 double unmodifiable, modifiable;
548 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
549 &unmodifiable, &modifiable);
550 modifiable += amount;
551 limit_value(&modifiable, arg);
553 prop->setDoubleValue(unmodifiable + modifiable);
560 * Built-in command: multiply a property value.
562 * property: the name of the property to multiply.
563 * factor: the amount by which to multiply.
564 * min: the minimum allowed value (default: no minimum).
565 * max: the maximum allowed value (default: no maximum).
566 * mask: 'integer' to apply only to the left of the decimal point,
567 * 'decimal' to apply only to the right of the decimal point,
568 * or 'all' to apply to the whole number (the default).
569 * wrap: true if the value should be wrapped when it passes min or max;
570 * both min and max must be present for this to work (default:
574 do_property_multiply (const SGPropertyNode * arg)
576 SGPropertyNode * prop = get_prop(arg);
577 double factor = arg->getDoubleValue("factor", 1);
579 double unmodifiable, modifiable;
580 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
581 &unmodifiable, &modifiable);
582 modifiable *= factor;
583 limit_value(&modifiable, arg);
585 prop->setDoubleValue(unmodifiable + modifiable);
592 * Built-in command: swap two property values.
594 * property[0]: the name of the first property.
595 * property[1]: the name of the second property.
598 do_property_swap (const SGPropertyNode * arg)
600 SGPropertyNode * prop1 = get_prop(arg);
601 SGPropertyNode * prop2 = get_prop2(arg);
603 // FIXME: inefficient
604 const string & tmp = prop1->getStringValue();
605 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
606 prop2->setUnspecifiedValue(tmp.c_str()));
611 * Built-in command: Set a property to an axis or other moving input.
613 * property: the name of the property to set.
614 * setting: the current input setting, usually between -1.0 and 1.0.
615 * offset: the offset to shift by, before applying the factor.
616 * factor: the factor to multiply by (use negative to reverse).
619 do_property_scale (const SGPropertyNode * arg)
621 SGPropertyNode * prop = get_prop(arg);
622 double setting = arg->getDoubleValue("setting");
623 double offset = arg->getDoubleValue("offset", 0.0);
624 double factor = arg->getDoubleValue("factor", 1.0);
625 bool squared = arg->getBoolValue("squared", false);
626 int power = arg->getIntValue("power", (squared ? 2 : 1));
628 int sign = (setting < 0 ? -1 : 1);
634 setting = setting * setting * sign;
637 setting = setting * setting * setting;
640 setting = setting * setting * setting * setting * sign;
643 setting = pow(setting, power);
644 if ((power % 2) == 0)
649 return prop->setDoubleValue((setting + offset) * factor);
654 * Built-in command: cycle a property through a set of values.
656 * If the current value isn't in the list, the cycle will
657 * (re)start from the beginning.
659 * property: the name of the property to cycle.
660 * value[*]: the list of values to cycle through.
663 do_property_cycle (const SGPropertyNode * arg)
665 SGPropertyNode * prop = get_prop(arg);
666 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
668 int nSelections = values.size();
670 if (nSelections < 1) {
671 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
675 // Try to find the current selection
676 for (int i = 0; i < nSelections; i++) {
677 if (compare_values(prop, values[i])) {
683 // Default or wrap to the first selection
684 if (selection < 0 || selection >= nSelections)
687 prop->setUnspecifiedValue(values[selection]->getStringValue());
693 * Built-in command: randomize a numeric property value.
695 * property: the name of the property value to randomize.
696 * min: the minimum allowed value.
697 * max: the maximum allowed value.
700 do_property_randomize (const SGPropertyNode * arg)
702 SGPropertyNode * prop = get_prop(arg);
703 double min = arg->getDoubleValue("min", DBL_MIN);
704 double max = arg->getDoubleValue("max", DBL_MAX);
705 prop->setDoubleValue(sg_random() * (max - min) + min);
711 * Built-in command: Show an XML-configured dialog.
713 * dialog-name: the name of the GUI dialog to display.
716 do_dialog_show (const SGPropertyNode * arg)
718 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
719 gui->showDialog(arg->getStringValue("dialog-name"));
725 * Built-in Command: Hide the active XML-configured dialog.
728 do_dialog_close (const SGPropertyNode * arg)
730 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
731 return gui->closeActiveDialog();
736 * Update a value in the active XML-configured dialog.
738 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
741 do_dialog_update (const SGPropertyNode * arg)
743 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
744 FGDialog * dialog = gui->getActiveDialog();
746 if (arg->hasValue("object-name")) {
747 dialog->updateValue(arg->getStringValue("object-name"));
749 dialog->updateValues();
759 * Apply a value in the active XML-configured dialog.
761 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
764 do_dialog_apply (const SGPropertyNode * arg)
766 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
767 FGDialog * dialog = gui->getActiveDialog();
769 if (arg->hasValue("object-name")) {
770 const char * name = arg->getStringValue("object-name");
771 dialog->applyValue(name);
772 dialog->updateValue(name);
774 dialog->applyValues();
775 dialog->updateValues();
785 * Built-in command: commit presets (read from in /sim/presets/)
788 do_presets_commit (const SGPropertyNode * arg)
790 // unbind the current fdm state so property changes
791 // don't get lost when we subsequently delete this fdm
792 // and create a new one.
793 cur_fdm_state->unbind();
795 // set position from presets
799 fgReInitSubsystems();
801 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
804 if ( ! fgGetBool("/sim/presets/onground") ) {
805 fgSetBool( "/sim/freeze/master", true );
806 fgSetBool( "/sim/freeze/clock", true );
814 * Built-in command: set log level (0 ... 7)
817 do_log_level (const SGPropertyNode * arg)
819 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
827 ////////////////////////////////////////////////////////////////////////
829 ////////////////////////////////////////////////////////////////////////
833 * Table of built-in commands.
835 * New commands do not have to be added here; any module in the application
836 * can add a new command using globals->get_commands()->addCommand(...).
840 SGCommandMgr::command_t command;
843 #if defined(HAVE_PLIB_PSL)
844 { "script", do_script },
845 #endif // HAVE_PLIB_PSL
847 { "reinit", do_reinit },
848 { "suspend", do_reinit },
849 { "resume", do_reinit },
852 { "panel-load", do_panel_load },
853 { "panel-mouse-click", do_panel_mouse_click },
854 { "preferences-load", do_preferences_load },
855 { "view-cycle", do_view_cycle },
856 { "screen-capture", do_screen_capture },
857 { "tile-cache-reload", do_tile_cache_reload },
858 { "lighting-update", do_lighting_update },
859 { "property-toggle", do_property_toggle },
860 { "property-assign", do_property_assign },
861 { "property-adjust", do_property_adjust },
862 { "property-multiply", do_property_multiply },
863 { "property-swap", do_property_swap },
864 { "property-scale", do_property_scale },
865 { "property-cycle", do_property_cycle },
866 { "property-randomize", do_property_randomize },
867 { "dialog-show", do_dialog_show },
868 { "dialog-close", do_dialog_close },
869 { "dialog-show", do_dialog_show },
870 { "dialog-update", do_dialog_update },
871 { "dialog-apply", do_dialog_apply },
872 { "presets-commit", do_presets_commit },
873 { "log-level", do_log_level },
874 { 0, 0 } // zero-terminated
879 * Initialize the default built-in commands.
881 * Other commands may be added by other parts of the application.
886 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
887 for (int i = 0; built_ins[i].name != 0; i++) {
888 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
889 globals->get_commands()->addCommand(built_ins[i].name,
890 built_ins[i].command);
893 typedef bool (*dummy)();
894 fgTie( "/command/view/next", dummy(0), do_view_next );
895 fgTie( "/command/view/prev", dummy(0), do_view_prev );
898 // end of fg_commands.cxx