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.
159 do_script (const SGPropertyNode * arg)
161 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem_mgr()
162 ->get_group(FGSubsystemMgr::GENERAL)->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: load flight.
186 * file (optional): the name of the file to load (relative to current
187 * directory). Defaults to "fgfs.sav".
190 do_load (const SGPropertyNode * arg)
192 const string &file = arg->getStringValue("file", "fgfs.sav");
193 ifstream input(file.c_str());
194 if (input.good() && fgLoadFlight(input)) {
196 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
199 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
206 * Built-in command: save flight.
208 * file (optional): the name of the file to save (relative to the
209 * current directory). Defaults to "fgfs.sav".
212 do_save (const SGPropertyNode * arg)
214 const string &file = arg->getStringValue("file", "fgfs.sav");
215 bool write_all = arg->getBoolValue("write-all", false);
216 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
217 ofstream output(file.c_str());
218 if (output.good() && fgSaveFlight(output, write_all)) {
220 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
223 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
230 * Built-in command: (re)load the panel.
232 * path (optional): the file name to load the panel from
233 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
234 * and if that's unspecified, to "Panels/Default/default.xml".
237 do_panel_load (const SGPropertyNode * arg)
240 arg->getStringValue("path",
241 fgGetString("/sim/panel/path",
242 "Panels/Default/default.xml"));
243 FGPanel * new_panel = fgReadPanel(panel_path);
244 if (new_panel == 0) {
245 SG_LOG(SG_INPUT, SG_ALERT,
246 "Error reading new panel from " << panel_path);
249 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
250 current_panel->unbind();
251 delete current_panel;
252 current_panel = new_panel;
253 current_panel->bind();
259 * Built-in command: pass a mouse click to the panel.
261 * button: the mouse button number, zero-based.
262 * is-down: true if the button is down, false if it is up.
263 * x-pos: the x position of the mouse click.
264 * y-pos: the y position of the mouse click.
267 do_panel_mouse_click (const SGPropertyNode * arg)
269 if (current_panel != 0)
271 ->doMouseAction(arg->getIntValue("button"),
272 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
273 arg->getIntValue("x-pos"),
274 arg->getIntValue("y-pos"));
281 * Built-in command: (re)load preferences.
283 * path (optional): the file name to load the panel from (relative
284 * to FG_ROOT). Defaults to "preferences.xml".
287 do_preferences_load (const SGPropertyNode * arg)
290 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
291 globals->get_props());
292 } catch (const sg_exception &e) {
293 guiErrorMessage("Error reading global preferences: ", e);
296 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
304 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
305 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
306 if ( globals->get_viewmgr()->get_current() == 1 ) {
307 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
315 globals->get_current_view()->setHeadingOffset_deg(0.0);
316 globals->get_viewmgr()->next_view();
317 fix_hud_visibility();
318 globals->get_tile_mgr()->refresh_view_timestamps();
324 globals->get_current_view()->setHeadingOffset_deg(0.0);
325 globals->get_viewmgr()->prev_view();
326 fix_hud_visibility();
327 globals->get_tile_mgr()->refresh_view_timestamps();
331 * Built-in command: cycle view.
334 do_view_cycle (const SGPropertyNode * arg)
336 globals->get_current_view()->setHeadingOffset_deg(0.0);
337 globals->get_viewmgr()->next_view();
338 fix_hud_visibility();
339 globals->get_tile_mgr()->refresh_view_timestamps();
340 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
345 * Built-in command: capture screen.
348 do_screen_capture (const SGPropertyNode * arg)
356 * Reload the tile cache.
359 do_tile_cache_reload (const SGPropertyNode * arg)
361 static const SGPropertyNode *master_freeze
362 = fgGetNode("/sim/freeze/master");
363 bool freeze = master_freeze->getBoolValue();
364 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
366 fgSetBool("/sim/freeze/master", true);
369 if ( globals->get_tile_mgr()->init() ) {
370 // Load the local scenery data
371 double visibility_meters = fgGetDouble("/environment/visibility-m");
372 globals->get_tile_mgr()->update( visibility_meters );
374 SG_LOG( SG_GENERAL, SG_ALERT,
375 "Error in Tile Manager initialization!" );
380 fgSetBool("/sim/freeze/master", false);
387 * Update the lighting manually.
390 do_lighting_update (const SGPropertyNode * arg)
392 fgUpdateSkyAndLightingParams();
398 * Built-in command: toggle a bool property value.
400 * property: The name of the property to toggle.
403 do_property_toggle (const SGPropertyNode * arg)
405 SGPropertyNode * prop = get_prop(arg);
406 return prop->setBoolValue(!prop->getBoolValue());
411 * Built-in command: assign a value to a property.
413 * property: the name of the property to assign.
414 * value: the value to assign; or
415 * property[1]: the property to copy from.
418 do_property_assign (const SGPropertyNode * arg)
420 SGPropertyNode * prop = get_prop(arg);
421 const SGPropertyNode * prop2 = get_prop2(arg);
422 const SGPropertyNode * value = arg->getNode("value");
425 return prop->setUnspecifiedValue(value->getStringValue());
427 return prop->setUnspecifiedValue(prop2->getStringValue());
434 * Built-in command: increment or decrement a property value.
436 * If the 'step' argument is present, it will be used; otherwise,
437 * the command uses 'offset' and 'factor', usually from the mouse.
439 * property: the name of the property to increment or decrement.
440 * step: the amount of the increment or decrement (default: 0).
441 * offset: offset from the current setting (used for the mouse; multiplied
443 * factor: scaling amount for the offset (defaults to 1).
444 * min: the minimum allowed value (default: no minimum).
445 * max: the maximum allowed value (default: no maximum).
446 * mask: 'integer' to apply only to the left of the decimal point,
447 * 'decimal' to apply only to the right of the decimal point,
448 * or 'all' to apply to the whole number (the default).
449 * wrap: true if the value should be wrapped when it passes min or max;
450 * both min and max must be present for this to work (default:
454 do_property_adjust (const SGPropertyNode * arg)
456 SGPropertyNode * prop = get_prop(arg);
459 if (arg->hasValue("step"))
460 amount = arg->getDoubleValue("step");
462 amount = (arg->getDoubleValue("factor", 1)
463 * arg->getDoubleValue("offset"));
465 double unmodifiable, modifiable;
466 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
467 &unmodifiable, &modifiable);
468 modifiable += amount;
469 limit_value(&modifiable, arg);
471 prop->setDoubleValue(unmodifiable + modifiable);
478 * Built-in command: multiply a property value.
480 * property: the name of the property to multiply.
481 * factor: the amount by which to multiply.
482 * min: the minimum allowed value (default: no minimum).
483 * max: the maximum allowed value (default: no maximum).
484 * mask: 'integer' to apply only to the left of the decimal point,
485 * 'decimal' to apply only to the right of the decimal point,
486 * or 'all' to apply to the whole number (the default).
487 * wrap: true if the value should be wrapped when it passes min or max;
488 * both min and max must be present for this to work (default:
492 do_property_multiply (const SGPropertyNode * arg)
494 SGPropertyNode * prop = get_prop(arg);
495 double factor = arg->getDoubleValue("factor", 1);
497 double unmodifiable, modifiable;
498 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
499 &unmodifiable, &modifiable);
500 modifiable *= factor;
501 limit_value(&modifiable, arg);
503 prop->setDoubleValue(unmodifiable + modifiable);
510 * Built-in command: swap two property values.
512 * property[0]: the name of the first property.
513 * property[1]: the name of the second property.
516 do_property_swap (const SGPropertyNode * arg)
518 SGPropertyNode * prop1 = get_prop(arg);
519 SGPropertyNode * prop2 = get_prop2(arg);
521 // FIXME: inefficient
522 const string & tmp = prop1->getStringValue();
523 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
524 prop2->setUnspecifiedValue(tmp.c_str()));
529 * Built-in command: Set a property to an axis or other moving input.
531 * property: the name of the property to set.
532 * setting: the current input setting, usually between -1.0 and 1.0.
533 * offset: the offset to shift by, before applying the factor.
534 * factor: the factor to multiply by (use negative to reverse).
537 do_property_scale (const SGPropertyNode * arg)
539 SGPropertyNode * prop = get_prop(arg);
540 double setting = arg->getDoubleValue("setting");
541 double offset = arg->getDoubleValue("offset", 0.0);
542 double factor = arg->getDoubleValue("factor", 1.0);
543 bool squared = arg->getBoolValue("squared", false);
546 setting = (setting < 0 ? -1 : 1) * setting * setting;
548 return prop->setDoubleValue((setting + offset) * factor);
553 * Built-in command: cycle a property through a set of values.
555 * If the current value isn't in the list, the cycle will
556 * (re)start from the beginning.
558 * property: the name of the property to cycle.
559 * value[*]: the list of values to cycle through.
562 do_property_cycle (const SGPropertyNode * arg)
564 SGPropertyNode * prop = get_prop(arg);
565 vector<SGPropertyNode_ptr> values = arg->getChildren("value");
567 int nSelections = values.size();
569 if (nSelections < 1) {
570 SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
574 // Try to find the current selection
575 for (int i = 0; i < nSelections; i++) {
576 if (compare_values(prop, values[i])) {
582 // Default or wrap to the first selection
583 if (selection < 0 || selection >= nSelections)
586 prop->setUnspecifiedValue(values[selection]->getStringValue());
592 * Built-in command: Show an XML-configured dialog.
594 * dialog-name: the name of the GUI dialog to display.
597 do_dialog_show (const SGPropertyNode * arg)
599 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
600 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
601 gui->display(arg->getStringValue("dialog-name"));
607 * Built-in Command: Hide the active XML-configured dialog.
610 do_dialog_close (const SGPropertyNode * arg)
612 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
613 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
614 FGDialog * widget = gui->getCurrentWidget();
617 gui->setCurrentWidget(0);
626 * Update a value in the active XML-configured dialog.
628 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
631 do_dialog_update (const SGPropertyNode * arg)
633 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
634 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
635 FGDialog * widget = gui->getCurrentWidget();
637 if (arg->hasValue("object-name")) {
638 gui->getCurrentWidget()
639 ->updateValue(arg->getStringValue("object-name"));
641 gui->getCurrentWidget()->updateValues();
651 * Apply a value in the active XML-configured dialog.
653 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
656 do_dialog_apply (const SGPropertyNode * arg)
658 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
659 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
660 FGDialog * widget = gui->getCurrentWidget();
662 if (arg->hasValue("object-name")) {
663 gui->getCurrentWidget()
664 ->applyValue(arg->getStringValue("object-name"));
666 gui->getCurrentWidget()->applyValues();
676 * Built-in command: commit presets (read from in /sim/presets/)
679 do_presets_commit (const SGPropertyNode * arg)
681 // unbind the current fdm state so property changes
682 // don't get lost when we subsequently delete this fdm
683 // and create a new one.
684 cur_fdm_state->unbind();
686 // set position from presets
690 fgReInitSubsystems();
692 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
694 if ( ! fgGetBool("/sim/presets/onground") ) {
695 fgSetBool( "/sim/freeze/master", true );
696 fgSetBool( "/sim/freeze/clock", true );
704 ////////////////////////////////////////////////////////////////////////
706 ////////////////////////////////////////////////////////////////////////
710 * Table of built-in commands.
712 * New commands do not have to be added here; any module in the application
713 * can add a new command using globals->get_commands()->addCommand(...).
717 SGCommandMgr::command_t command;
720 #if defined(HAVE_PLIB_PSL)
721 { "script", do_script },
722 #endif // HAVE_PLIB_PSL
726 { "panel-load", do_panel_load },
727 { "panel-mouse-click", do_panel_mouse_click },
728 { "preferences-load", do_preferences_load },
729 { "view-cycle", do_view_cycle },
730 { "screen-capture", do_screen_capture },
731 { "tile-cache-reload", do_tile_cache_reload },
732 { "lighting-update", do_lighting_update },
733 { "property-toggle", do_property_toggle },
734 { "property-assign", do_property_assign },
735 { "property-adjust", do_property_adjust },
736 { "property-multiply", do_property_multiply },
737 { "property-swap", do_property_swap },
738 { "property-scale", do_property_scale },
739 { "property-cycle", do_property_cycle },
740 { "dialog-show", do_dialog_show },
741 { "dialog-close", do_dialog_close },
742 { "dialog-show", do_dialog_show },
743 { "dialog-update", do_dialog_update },
744 { "dialog-apply", do_dialog_apply },
745 { "presets-commit", do_presets_commit },
746 { 0, 0 } // zero-terminated
751 * Initialize the default built-in commands.
753 * Other commands may be added by other parts of the application.
758 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
759 for (int i = 0; built_ins[i].name != 0; i++) {
760 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
761 globals->get_commands()->addCommand(built_ins[i].name,
762 built_ins[i].command);
765 typedef bool (*dummy)();
766 fgTie( "/command/view/next", dummy(0), do_view_next );
767 fgTie( "/command/view/prev", dummy(0), do_view_prev );
770 // end of fg_commands.cxx