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/debug/logstream.hxx>
12 #include <simgear/math/sg_random.h>
13 #include <simgear/misc/commands.hxx>
14 #include <simgear/misc/props.hxx>
15 #include <simgear/sg_inlines.h>
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();
199 else for (int i = 0; i < subsystems.size(); i++) {
200 const char * name = subsystems[i]->getStringValue();
201 FGSubsystem * subsystem = globals->get_subsystem(name);
202 if (subsystem == 0) {
204 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
213 * Built-in command: suspend one or more subsystems.
215 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
218 do_suspend (const SGPropertyNode * arg)
222 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
223 for (int i = 0; i < subsystems.size(); i++) {
224 const char * name = subsystems[i]->getStringValue();
225 FGSubsystem * subsystem = globals->get_subsystem(name);
226 if (subsystem == 0) {
228 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
230 subsystem->suspend();
237 * Built-in command: suspend one or more subsystems.
239 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
242 do_resume (const SGPropertyNode * arg)
246 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
247 for (int i = 0; i < subsystems.size(); i++) {
248 const char * name = subsystems[i]->getStringValue();
249 FGSubsystem * subsystem = globals->get_subsystem(name);
250 if (subsystem == 0) {
252 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
262 * Built-in command: load flight.
264 * file (optional): the name of the file to load (relative to current
265 * directory). Defaults to "fgfs.sav"
268 do_load (const SGPropertyNode * arg)
270 const string &file = arg->getStringValue("file", "fgfs.sav");
271 ifstream input(file.c_str());
272 if (input.good() && fgLoadFlight(input)) {
274 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
277 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
284 * Built-in command: save flight.
286 * file (optional): the name of the file to save (relative to the
287 * current directory). Defaults to "fgfs.sav".
290 do_save (const SGPropertyNode * arg)
292 const string &file = arg->getStringValue("file", "fgfs.sav");
293 bool write_all = arg->getBoolValue("write-all", false);
294 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
295 ofstream output(file.c_str());
296 if (output.good() && fgSaveFlight(output, write_all)) {
298 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
301 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
308 * Built-in command: (re)load the panel.
310 * path (optional): the file name to load the panel from
311 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
312 * and if that's unspecified, to "Panels/Default/default.xml".
315 do_panel_load (const SGPropertyNode * arg)
318 arg->getStringValue("path",
319 fgGetString("/sim/panel/path",
320 "Panels/Default/default.xml"));
321 FGPanel * new_panel = fgReadPanel(panel_path);
322 if (new_panel == 0) {
323 SG_LOG(SG_INPUT, SG_ALERT,
324 "Error reading new panel from " << panel_path);
327 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
328 globals->get_current_panel()->unbind();
329 delete globals->get_current_panel();
330 globals->set_current_panel( new_panel );
331 globals->get_current_panel()->bind();
337 * Built-in command: pass a mouse click to the panel.
339 * button: the mouse button number, zero-based.
340 * is-down: true if the button is down, false if it is up.
341 * x-pos: the x position of the mouse click.
342 * y-pos: the y position of the mouse click.
345 do_panel_mouse_click (const SGPropertyNode * arg)
347 if (globals->get_current_panel() != 0)
348 return globals->get_current_panel()
349 ->doMouseAction(arg->getIntValue("button"),
350 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
351 arg->getIntValue("x-pos"),
352 arg->getIntValue("y-pos"));
359 * Built-in command: (re)load preferences.
361 * path (optional): the file name to load the panel from (relative
362 * to FG_ROOT). Defaults to "preferences.xml".
365 do_preferences_load (const SGPropertyNode * arg)
368 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
369 globals->get_props());
370 } catch (const sg_exception &e) {
371 guiErrorMessage("Error reading global preferences: ", e);
374 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
382 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
383 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
384 if ( globals->get_viewmgr()->get_current() == 1 ) {
385 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
393 globals->get_current_view()->setHeadingOffset_deg(0.0);
394 globals->get_viewmgr()->next_view();
395 fix_hud_visibility();
396 globals->get_tile_mgr()->refresh_view_timestamps();
402 globals->get_current_view()->setHeadingOffset_deg(0.0);
403 globals->get_viewmgr()->prev_view();
404 fix_hud_visibility();
405 globals->get_tile_mgr()->refresh_view_timestamps();
409 * Built-in command: cycle view.
412 do_view_cycle (const SGPropertyNode * arg)
414 globals->get_current_view()->setHeadingOffset_deg(0.0);
415 globals->get_viewmgr()->next_view();
416 fix_hud_visibility();
417 globals->get_tile_mgr()->refresh_view_timestamps();
418 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
423 * Built-in command: capture screen.
426 do_screen_capture (const SGPropertyNode * arg)
434 * Reload the tile cache.
437 do_tile_cache_reload (const SGPropertyNode * arg)
439 static const SGPropertyNode *master_freeze
440 = fgGetNode("/sim/freeze/master");
441 bool freeze = master_freeze->getBoolValue();
442 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
444 fgSetBool("/sim/freeze/master", true);
447 if ( globals->get_tile_mgr()->init() ) {
448 // Load the local scenery data
449 double visibility_meters = fgGetDouble("/environment/visibility-m");
450 globals->get_tile_mgr()->update( visibility_meters );
452 SG_LOG( SG_GENERAL, SG_ALERT,
453 "Error in Tile Manager initialization!" );
458 fgSetBool("/sim/freeze/master", false);
465 * Update the lighting manually.
468 do_lighting_update (const SGPropertyNode * arg)
470 fgUpdateSkyAndLightingParams();
476 * Built-in command: toggle a bool property value.
478 * property: The name of the property to toggle.
481 do_property_toggle (const SGPropertyNode * arg)
483 SGPropertyNode * prop = get_prop(arg);
484 return prop->setBoolValue(!prop->getBoolValue());
489 * Built-in command: assign a value to a property.
491 * property: the name of the property to assign.
492 * value: the value to assign; or
493 * property[1]: the property to copy from.
496 do_property_assign (const SGPropertyNode * arg)
498 SGPropertyNode * prop = get_prop(arg);
499 const SGPropertyNode * prop2 = get_prop2(arg);
500 const SGPropertyNode * value = arg->getNode("value");
503 return prop->setUnspecifiedValue(value->getStringValue());
505 return prop->setUnspecifiedValue(prop2->getStringValue());
512 * Built-in command: increment or decrement a property value.
514 * If the 'step' argument is present, it will be used; otherwise,
515 * the command uses 'offset' and 'factor', usually from the mouse.
517 * property: the name of the property to increment or decrement.
518 * step: the amount of the increment or decrement (default: 0).
519 * offset: offset from the current setting (used for the mouse; multiplied
521 * factor: scaling amount for the offset (defaults to 1).
522 * min: the minimum allowed value (default: no minimum).
523 * max: the maximum allowed value (default: no maximum).
524 * mask: 'integer' to apply only to the left of the decimal point,
525 * 'decimal' to apply only to the right of the decimal point,
526 * or 'all' to apply to the whole number (the default).
527 * wrap: true if the value should be wrapped when it passes min or max;
528 * both min and max must be present for this to work (default:
532 do_property_adjust (const SGPropertyNode * arg)
534 SGPropertyNode * prop = get_prop(arg);
537 if (arg->hasValue("step"))
538 amount = arg->getDoubleValue("step");
540 amount = (arg->getDoubleValue("factor", 1)
541 * arg->getDoubleValue("offset"));
543 double unmodifiable, modifiable;
544 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
545 &unmodifiable, &modifiable);
546 modifiable += amount;
547 limit_value(&modifiable, arg);
549 prop->setDoubleValue(unmodifiable + modifiable);
556 * Built-in command: multiply a property value.
558 * property: the name of the property to multiply.
559 * factor: the amount by which to multiply.
560 * min: the minimum allowed value (default: no minimum).
561 * max: the maximum allowed value (default: no maximum).
562 * mask: 'integer' to apply only to the left of the decimal point,
563 * 'decimal' to apply only to the right of the decimal point,
564 * or 'all' to apply to the whole number (the default).
565 * wrap: true if the value should be wrapped when it passes min or max;
566 * both min and max must be present for this to work (default:
570 do_property_multiply (const SGPropertyNode * arg)
572 SGPropertyNode * prop = get_prop(arg);
573 double factor = arg->getDoubleValue("factor", 1);
575 double unmodifiable, modifiable;
576 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
577 &unmodifiable, &modifiable);
578 modifiable *= factor;
579 limit_value(&modifiable, arg);
581 prop->setDoubleValue(unmodifiable + modifiable);
588 * Built-in command: swap two property values.
590 * property[0]: the name of the first property.
591 * property[1]: the name of the second property.
594 do_property_swap (const SGPropertyNode * arg)
596 SGPropertyNode * prop1 = get_prop(arg);
597 SGPropertyNode * prop2 = get_prop2(arg);
599 // FIXME: inefficient
600 const string & tmp = prop1->getStringValue();
601 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
602 prop2->setUnspecifiedValue(tmp.c_str()));
607 * Built-in command: Set a property to an axis or other moving input.
609 * property: the name of the property to set.
610 * setting: the current input setting, usually between -1.0 and 1.0.
611 * offset: the offset to shift by, before applying the factor.
612 * factor: the factor to multiply by (use negative to reverse).
615 do_property_scale (const SGPropertyNode * arg)
617 SGPropertyNode * prop = get_prop(arg);
618 double setting = arg->getDoubleValue("setting");
619 double offset = arg->getDoubleValue("offset", 0.0);
620 double factor = arg->getDoubleValue("factor", 1.0);
621 bool squared = arg->getBoolValue("squared", false);
622 int power = arg->getIntValue("power", (squared ? 2 : 1));
624 int sign = (setting < 0 ? -1 : 1);
630 setting = setting * setting * sign;
633 setting = setting * setting * setting;
636 setting = setting * setting * setting * setting * sign;
639 setting = pow(setting, power);
640 if ((power % 2) == 0)
645 return prop->setDoubleValue((setting + offset) * factor);
650 * Built-in command: cycle a property through a set of values.
652 * If the current value isn't in the list, the cycle will
653 * (re)start from the beginning.
655 * property: the name of the property to cycle.
656 * value[*]: the list of values to cycle through.
659 do_property_cycle (const SGPropertyNode * arg)
661 SGPropertyNode * prop = get_prop(arg);
662 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
664 int nSelections = values.size();
666 if (nSelections < 1) {
667 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
671 // Try to find the current selection
672 for (int i = 0; i < nSelections; i++) {
673 if (compare_values(prop, values[i])) {
679 // Default or wrap to the first selection
680 if (selection < 0 || selection >= nSelections)
683 prop->setUnspecifiedValue(values[selection]->getStringValue());
689 * Built-in command: randomize a numeric property value.
691 * property: the name of the property value to randomize.
692 * min: the minimum allowed value.
693 * max: the maximum allowed value.
696 do_property_randomize (const SGPropertyNode * arg)
698 SGPropertyNode * prop = get_prop(arg);
699 double min = arg->getDoubleValue("min", DBL_MIN);
700 double max = arg->getDoubleValue("max", DBL_MAX);
701 prop->setDoubleValue(sg_random() * (max - min) + min);
707 * Built-in command: Show an XML-configured dialog.
709 * dialog-name: the name of the GUI dialog to display.
712 do_dialog_show (const SGPropertyNode * arg)
714 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
715 gui->showDialog(arg->getStringValue("dialog-name"));
721 * Built-in Command: Hide the active XML-configured dialog.
724 do_dialog_close (const SGPropertyNode * arg)
726 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
727 return gui->closeActiveDialog();
732 * Update a value in the active XML-configured dialog.
734 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
737 do_dialog_update (const SGPropertyNode * arg)
739 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
740 FGDialog * dialog = gui->getActiveDialog();
742 if (arg->hasValue("object-name")) {
743 dialog->updateValue(arg->getStringValue("object-name"));
745 dialog->updateValues();
755 * Apply a value in the active XML-configured dialog.
757 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
760 do_dialog_apply (const SGPropertyNode * arg)
762 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
763 FGDialog * dialog = gui->getActiveDialog();
765 if (arg->hasValue("object-name")) {
766 const char * name = arg->getStringValue("object-name");
767 dialog->applyValue(name);
768 dialog->updateValue(name);
770 dialog->applyValues();
771 dialog->updateValues();
781 * Built-in command: commit presets (read from in /sim/presets/)
784 do_presets_commit (const SGPropertyNode * arg)
786 // unbind the current fdm state so property changes
787 // don't get lost when we subsequently delete this fdm
788 // and create a new one.
789 cur_fdm_state->unbind();
791 // set position from presets
795 fgReInitSubsystems();
797 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
800 if ( ! fgGetBool("/sim/presets/onground") ) {
801 fgSetBool( "/sim/freeze/master", true );
802 fgSetBool( "/sim/freeze/clock", true );
810 * Built-in command: set log level (0 ... 7)
813 do_log_level (const SGPropertyNode * arg)
815 sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
823 ////////////////////////////////////////////////////////////////////////
825 ////////////////////////////////////////////////////////////////////////
829 * Table of built-in commands.
831 * New commands do not have to be added here; any module in the application
832 * can add a new command using globals->get_commands()->addCommand(...).
836 SGCommandMgr::command_t command;
839 #if defined(HAVE_PLIB_PSL)
840 { "script", do_script },
841 #endif // HAVE_PLIB_PSL
843 { "reinit", do_reinit },
844 { "suspend", do_reinit },
845 { "resume", do_reinit },
848 { "panel-load", do_panel_load },
849 { "panel-mouse-click", do_panel_mouse_click },
850 { "preferences-load", do_preferences_load },
851 { "view-cycle", do_view_cycle },
852 { "screen-capture", do_screen_capture },
853 { "tile-cache-reload", do_tile_cache_reload },
854 { "lighting-update", do_lighting_update },
855 { "property-toggle", do_property_toggle },
856 { "property-assign", do_property_assign },
857 { "property-adjust", do_property_adjust },
858 { "property-multiply", do_property_multiply },
859 { "property-swap", do_property_swap },
860 { "property-scale", do_property_scale },
861 { "property-cycle", do_property_cycle },
862 { "property-randomize", do_property_randomize },
863 { "dialog-show", do_dialog_show },
864 { "dialog-close", do_dialog_close },
865 { "dialog-show", do_dialog_show },
866 { "dialog-update", do_dialog_update },
867 { "dialog-apply", do_dialog_apply },
868 { "presets-commit", do_presets_commit },
869 { "log-level", do_log_level },
870 { 0, 0 } // zero-terminated
875 * Initialize the default built-in commands.
877 * Other commands may be added by other parts of the application.
882 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
883 for (int i = 0; built_ins[i].name != 0; i++) {
884 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
885 globals->get_commands()->addCommand(built_ins[i].name,
886 built_ins[i].command);
889 typedef bool (*dummy)();
890 fgTie( "/command/view/next", dummy(0), do_view_next );
891 fgTie( "/command/view/prev", dummy(0), do_view_prev );
894 // end of fg_commands.cxx