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/misc/commands.hxx>
13 #include <simgear/misc/props.hxx>
14 #include <simgear/sg_inlines.h>
16 #include <Cockpit/panel.hxx>
17 #include <Cockpit/panel_io.hxx>
18 #include <FDM/flight.hxx>
20 #include <GUI/new_gui.hxx>
21 #include <GUI/dialog.hxx>
22 #include <Scenery/tilemgr.hxx>
23 #if defined(HAVE_PLIB_PSL)
24 #include <Scripting/scriptmgr.hxx>
26 #include <Time/tmp.hxx>
28 #include "fg_init.hxx"
29 #include "fg_commands.hxx"
32 SG_USING_STD(ifstream);
33 SG_USING_STD(ofstream);
35 #include "fg_props.hxx"
37 #include "globals.hxx"
38 #include "viewmgr.hxx"
42 ////////////////////////////////////////////////////////////////////////
43 // Static helper functions.
44 ////////////////////////////////////////////////////////////////////////
47 static inline SGPropertyNode *
48 get_prop (const SGPropertyNode * arg)
50 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
53 static inline SGPropertyNode *
54 get_prop2 (const SGPropertyNode * arg)
56 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
61 * Get a double value and split it as required.
64 split_value (double full_value, const char * mask,
65 double * unmodifiable, double * modifiable)
67 if (!strcmp("integer", mask)) {
68 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
69 *unmodifiable = full_value - *modifiable;
70 } else if (!strcmp("decimal", mask)) {
71 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
72 *modifiable = full_value - *unmodifiable;
74 if (strcmp("all", mask))
75 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
76 << " assuming 'all'");
78 *modifiable = full_value;
84 * Clamp or wrap a value as specified.
87 limit_value (double * value, const SGPropertyNode * arg)
89 const SGPropertyNode * min_node = arg->getChild("min");
90 const SGPropertyNode * max_node = arg->getChild("max");
92 bool wrap = arg->getBoolValue("wrap");
94 if (min_node == 0 || max_node == 0)
97 if (wrap) { // wrap such that min <= x < max
98 double min_val = min_node->getDoubleValue();
99 double max_val = max_node->getDoubleValue();
100 double resolution = arg->getDoubleValue("resolution");
101 if (resolution > 0.0) {
102 // snap to (min + N*resolution), taking special care to handle imprecision
103 int n = (int)floor((*value - min_val) / resolution + 0.5);
104 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
105 SG_NORMALIZE_RANGE(n, 0, steps);
106 *value = min_val + resolution * n;
108 // plain circular wrapping
109 SG_NORMALIZE_RANGE(*value, min_val, max_val);
111 } else { // clamp such that min <= x <= max
112 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
113 *value = min_node->getDoubleValue();
114 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
115 *value = max_node->getDoubleValue();
120 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
122 switch (value1->getType()) {
123 case SGPropertyNode::BOOL:
124 return (value1->getBoolValue() == value2->getBoolValue());
125 case SGPropertyNode::INT:
126 return (value1->getIntValue() == value2->getIntValue());
127 case SGPropertyNode::LONG:
128 return (value1->getLongValue() == value2->getLongValue());
129 case SGPropertyNode::FLOAT:
130 return (value1->getFloatValue() == value2->getFloatValue());
131 case SGPropertyNode::DOUBLE:
132 return (value1->getDoubleValue() == value2->getDoubleValue());
134 return !strcmp(value1->getStringValue(), value2->getStringValue());
140 ////////////////////////////////////////////////////////////////////////
141 // Command implementations.
142 ////////////////////////////////////////////////////////////////////////
146 * Built-in command: do nothing.
149 do_null (const SGPropertyNode * arg)
154 #if defined(HAVE_PLIB_PSL)
156 * Built-in command: run a PSL script.
158 * script: the PSL script to execute
161 do_script (const SGPropertyNode * arg)
163 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
164 return mgr->run(arg->getStringValue("script"));
166 #endif // HAVE_PLIB_PSL
170 * Built-in command: exit FlightGear.
172 * TODO: show a confirm dialog.
175 do_exit (const SGPropertyNode * arg)
177 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
184 * Built-in command: reinitialize one or more subsystems.
186 * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
187 * none is specified, reinitialize all of them.
190 do_reinit (const SGPropertyNode * arg)
194 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
195 if (subsystems.size() == 0)
196 globals->get_subsystem_mgr()->reinit();
197 else for (int i = 0; i < subsystems.size(); i++) {
198 const char * name = subsystems[i]->getStringValue();
199 FGSubsystem * subsystem = globals->get_subsystem(name);
200 if (subsystem == 0) {
202 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
212 * Built-in command: suspend one or more subsystems.
214 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
217 do_suspend (const SGPropertyNode * arg)
221 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
222 for (int i = 0; i < subsystems.size(); i++) {
223 const char * name = subsystems[i]->getStringValue();
224 FGSubsystem * subsystem = globals->get_subsystem(name);
225 if (subsystem == 0) {
227 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
229 subsystem->suspend();
236 * Built-in command: suspend one or more subsystems.
238 * subsystem[*] - the name(s) of the subsystem(s) to suspend.
241 do_resume (const SGPropertyNode * arg)
245 vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
246 for (int i = 0; i < subsystems.size(); i++) {
247 const char * name = subsystems[i]->getStringValue();
248 FGSubsystem * subsystem = globals->get_subsystem(name);
249 if (subsystem == 0) {
251 SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
261 * Built-in command: load flight.
263 * file (optional): the name of the file to load (relative to current
264 * directory). Defaults to "fgfs.sav"
267 do_load (const SGPropertyNode * arg)
269 const string &file = arg->getStringValue("file", "fgfs.sav");
270 ifstream input(file.c_str());
271 if (input.good() && fgLoadFlight(input)) {
273 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
276 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
283 * Built-in command: save flight.
285 * file (optional): the name of the file to save (relative to the
286 * current directory). Defaults to "fgfs.sav".
289 do_save (const SGPropertyNode * arg)
291 const string &file = arg->getStringValue("file", "fgfs.sav");
292 bool write_all = arg->getBoolValue("write-all", false);
293 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
294 ofstream output(file.c_str());
295 if (output.good() && fgSaveFlight(output, write_all)) {
297 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
300 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
307 * Built-in command: (re)load the panel.
309 * path (optional): the file name to load the panel from
310 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
311 * and if that's unspecified, to "Panels/Default/default.xml".
314 do_panel_load (const SGPropertyNode * arg)
317 arg->getStringValue("path",
318 fgGetString("/sim/panel/path",
319 "Panels/Default/default.xml"));
320 FGPanel * new_panel = fgReadPanel(panel_path);
321 if (new_panel == 0) {
322 SG_LOG(SG_INPUT, SG_ALERT,
323 "Error reading new panel from " << panel_path);
326 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
327 current_panel->unbind();
328 delete current_panel;
329 current_panel = new_panel;
330 current_panel->bind();
336 * Built-in command: pass a mouse click to the panel.
338 * button: the mouse button number, zero-based.
339 * is-down: true if the button is down, false if it is up.
340 * x-pos: the x position of the mouse click.
341 * y-pos: the y position of the mouse click.
344 do_panel_mouse_click (const SGPropertyNode * arg)
346 if (current_panel != 0)
348 ->doMouseAction(arg->getIntValue("button"),
349 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
350 arg->getIntValue("x-pos"),
351 arg->getIntValue("y-pos"));
358 * Built-in command: (re)load preferences.
360 * path (optional): the file name to load the panel from (relative
361 * to FG_ROOT). Defaults to "preferences.xml".
364 do_preferences_load (const SGPropertyNode * arg)
367 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
368 globals->get_props());
369 } catch (const sg_exception &e) {
370 guiErrorMessage("Error reading global preferences: ", e);
373 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
381 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
382 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
383 if ( globals->get_viewmgr()->get_current() == 1 ) {
384 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
392 globals->get_current_view()->setHeadingOffset_deg(0.0);
393 globals->get_viewmgr()->next_view();
394 fix_hud_visibility();
395 globals->get_tile_mgr()->refresh_view_timestamps();
401 globals->get_current_view()->setHeadingOffset_deg(0.0);
402 globals->get_viewmgr()->prev_view();
403 fix_hud_visibility();
404 globals->get_tile_mgr()->refresh_view_timestamps();
408 * Built-in command: cycle view.
411 do_view_cycle (const SGPropertyNode * arg)
413 globals->get_current_view()->setHeadingOffset_deg(0.0);
414 globals->get_viewmgr()->next_view();
415 fix_hud_visibility();
416 globals->get_tile_mgr()->refresh_view_timestamps();
417 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
422 * Built-in command: capture screen.
425 do_screen_capture (const SGPropertyNode * arg)
433 * Reload the tile cache.
436 do_tile_cache_reload (const SGPropertyNode * arg)
438 static const SGPropertyNode *master_freeze
439 = fgGetNode("/sim/freeze/master");
440 bool freeze = master_freeze->getBoolValue();
441 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
443 fgSetBool("/sim/freeze/master", true);
446 if ( globals->get_tile_mgr()->init() ) {
447 // Load the local scenery data
448 double visibility_meters = fgGetDouble("/environment/visibility-m");
449 globals->get_tile_mgr()->update( visibility_meters );
451 SG_LOG( SG_GENERAL, SG_ALERT,
452 "Error in Tile Manager initialization!" );
457 fgSetBool("/sim/freeze/master", false);
464 * Update the lighting manually.
467 do_lighting_update (const SGPropertyNode * arg)
469 fgUpdateSkyAndLightingParams();
475 * Built-in command: toggle a bool property value.
477 * property: The name of the property to toggle.
480 do_property_toggle (const SGPropertyNode * arg)
482 SGPropertyNode * prop = get_prop(arg);
483 return prop->setBoolValue(!prop->getBoolValue());
488 * Built-in command: assign a value to a property.
490 * property: the name of the property to assign.
491 * value: the value to assign; or
492 * property[1]: the property to copy from.
495 do_property_assign (const SGPropertyNode * arg)
497 SGPropertyNode * prop = get_prop(arg);
498 const SGPropertyNode * prop2 = get_prop2(arg);
499 const SGPropertyNode * value = arg->getNode("value");
502 return prop->setUnspecifiedValue(value->getStringValue());
504 return prop->setUnspecifiedValue(prop2->getStringValue());
511 * Built-in command: increment or decrement a property value.
513 * If the 'step' argument is present, it will be used; otherwise,
514 * the command uses 'offset' and 'factor', usually from the mouse.
516 * property: the name of the property to increment or decrement.
517 * step: the amount of the increment or decrement (default: 0).
518 * offset: offset from the current setting (used for the mouse; multiplied
520 * factor: scaling amount for the offset (defaults to 1).
521 * min: the minimum allowed value (default: no minimum).
522 * max: the maximum allowed value (default: no maximum).
523 * mask: 'integer' to apply only to the left of the decimal point,
524 * 'decimal' to apply only to the right of the decimal point,
525 * or 'all' to apply to the whole number (the default).
526 * wrap: true if the value should be wrapped when it passes min or max;
527 * both min and max must be present for this to work (default:
531 do_property_adjust (const SGPropertyNode * arg)
533 SGPropertyNode * prop = get_prop(arg);
536 if (arg->hasValue("step"))
537 amount = arg->getDoubleValue("step");
539 amount = (arg->getDoubleValue("factor", 1)
540 * arg->getDoubleValue("offset"));
542 double unmodifiable, modifiable;
543 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
544 &unmodifiable, &modifiable);
545 modifiable += amount;
546 limit_value(&modifiable, arg);
548 prop->setDoubleValue(unmodifiable + modifiable);
555 * Built-in command: multiply a property value.
557 * property: the name of the property to multiply.
558 * factor: the amount by which to multiply.
559 * min: the minimum allowed value (default: no minimum).
560 * max: the maximum allowed value (default: no maximum).
561 * mask: 'integer' to apply only to the left of the decimal point,
562 * 'decimal' to apply only to the right of the decimal point,
563 * or 'all' to apply to the whole number (the default).
564 * wrap: true if the value should be wrapped when it passes min or max;
565 * both min and max must be present for this to work (default:
569 do_property_multiply (const SGPropertyNode * arg)
571 SGPropertyNode * prop = get_prop(arg);
572 double factor = arg->getDoubleValue("factor", 1);
574 double unmodifiable, modifiable;
575 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
576 &unmodifiable, &modifiable);
577 modifiable *= factor;
578 limit_value(&modifiable, arg);
580 prop->setDoubleValue(unmodifiable + modifiable);
587 * Built-in command: swap two property values.
589 * property[0]: the name of the first property.
590 * property[1]: the name of the second property.
593 do_property_swap (const SGPropertyNode * arg)
595 SGPropertyNode * prop1 = get_prop(arg);
596 SGPropertyNode * prop2 = get_prop2(arg);
598 // FIXME: inefficient
599 const string & tmp = prop1->getStringValue();
600 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
601 prop2->setUnspecifiedValue(tmp.c_str()));
606 * Built-in command: Set a property to an axis or other moving input.
608 * property: the name of the property to set.
609 * setting: the current input setting, usually between -1.0 and 1.0.
610 * offset: the offset to shift by, before applying the factor.
611 * factor: the factor to multiply by (use negative to reverse).
614 do_property_scale (const SGPropertyNode * arg)
616 SGPropertyNode * prop = get_prop(arg);
617 double setting = arg->getDoubleValue("setting");
618 double offset = arg->getDoubleValue("offset", 0.0);
619 double factor = arg->getDoubleValue("factor", 1.0);
620 bool squared = arg->getBoolValue("squared", false);
623 setting = (setting < 0 ? -1 : 1) * setting * setting;
625 return prop->setDoubleValue((setting + offset) * factor);
630 * Built-in command: cycle a property through a set of values.
632 * If the current value isn't in the list, the cycle will
633 * (re)start from the beginning.
635 * property: the name of the property to cycle.
636 * value[*]: the list of values to cycle through.
639 do_property_cycle (const SGPropertyNode * arg)
641 SGPropertyNode * prop = get_prop(arg);
642 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
644 int nSelections = values.size();
646 if (nSelections < 1) {
647 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
651 // Try to find the current selection
652 for (int i = 0; i < nSelections; i++) {
653 if (compare_values(prop, values[i])) {
659 // Default or wrap to the first selection
660 if (selection < 0 || selection >= nSelections)
663 prop->setUnspecifiedValue(values[selection]->getStringValue());
669 * Built-in command: Show an XML-configured dialog.
671 * dialog-name: the name of the GUI dialog to display.
674 do_dialog_show (const SGPropertyNode * arg)
676 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
677 gui->showDialog(arg->getStringValue("dialog-name"));
683 * Built-in Command: Hide the active XML-configured dialog.
686 do_dialog_close (const SGPropertyNode * arg)
688 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
689 gui->closeActiveDialog();
694 * Update a value in the active XML-configured dialog.
696 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
699 do_dialog_update (const SGPropertyNode * arg)
701 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
702 FGDialog * dialog = gui->getActiveDialog();
704 if (arg->hasValue("object-name")) {
705 dialog->updateValue(arg->getStringValue("object-name"));
707 dialog->updateValues();
717 * Apply a value in the active XML-configured dialog.
719 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
722 do_dialog_apply (const SGPropertyNode * arg)
724 NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
725 FGDialog * dialog = gui->getActiveDialog();
727 if (arg->hasValue("object-name")) {
728 const char * name = arg->getStringValue("object-name");
729 dialog->applyValue(name);
730 dialog->updateValue(name);
732 dialog->applyValues();
733 dialog->updateValues();
743 * Built-in command: commit presets (read from in /sim/presets/)
746 do_presets_commit (const SGPropertyNode * arg)
748 // unbind the current fdm state so property changes
749 // don't get lost when we subsequently delete this fdm
750 // and create a new one.
751 cur_fdm_state->unbind();
753 // set position from presets
757 fgReInitSubsystems();
759 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
761 if ( ! fgGetBool("/sim/presets/onground") ) {
762 fgSetBool( "/sim/freeze/master", true );
763 fgSetBool( "/sim/freeze/clock", true );
771 ////////////////////////////////////////////////////////////////////////
773 ////////////////////////////////////////////////////////////////////////
777 * Table of built-in commands.
779 * New commands do not have to be added here; any module in the application
780 * can add a new command using globals->get_commands()->addCommand(...).
784 SGCommandMgr::command_t command;
787 #if defined(HAVE_PLIB_PSL)
788 { "script", do_script },
789 #endif // HAVE_PLIB_PSL
791 { "reinit", do_reinit },
792 { "suspend", do_reinit },
793 { "resume", do_reinit },
796 { "panel-load", do_panel_load },
797 { "panel-mouse-click", do_panel_mouse_click },
798 { "preferences-load", do_preferences_load },
799 { "view-cycle", do_view_cycle },
800 { "screen-capture", do_screen_capture },
801 { "tile-cache-reload", do_tile_cache_reload },
802 { "lighting-update", do_lighting_update },
803 { "property-toggle", do_property_toggle },
804 { "property-assign", do_property_assign },
805 { "property-adjust", do_property_adjust },
806 { "property-multiply", do_property_multiply },
807 { "property-swap", do_property_swap },
808 { "property-scale", do_property_scale },
809 { "property-cycle", do_property_cycle },
810 { "dialog-show", do_dialog_show },
811 { "dialog-close", do_dialog_close },
812 { "dialog-show", do_dialog_show },
813 { "dialog-update", do_dialog_update },
814 { "dialog-apply", do_dialog_apply },
815 { "presets-commit", do_presets_commit },
816 { 0, 0 } // zero-terminated
821 * Initialize the default built-in commands.
823 * Other commands may be added by other parts of the application.
828 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
829 for (int i = 0; built_ins[i].name != 0; i++) {
830 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
831 globals->get_commands()->addCommand(built_ins[i].name,
832 built_ins[i].command);
835 typedef bool (*dummy)();
836 fgTie( "/command/view/next", dummy(0), do_view_next );
837 fgTie( "/command/view/prev", dummy(0), do_view_prev );
840 // end of fg_commands.cxx