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 <Scenery/tilemgr.hxx>
22 #if defined(HAVE_PLIB_PSL)
23 #include <Scripting/scriptmgr.hxx>
25 #include <Time/tmp.hxx>
27 #include "fg_init.hxx"
28 #include "fg_commands.hxx"
31 SG_USING_STD(ifstream);
32 SG_USING_STD(ofstream);
34 #include "fg_props.hxx"
36 #include "globals.hxx"
37 #include "viewmgr.hxx"
41 ////////////////////////////////////////////////////////////////////////
42 // Static helper functions.
43 ////////////////////////////////////////////////////////////////////////
46 static inline SGPropertyNode *
47 get_prop (const SGPropertyNode * arg)
49 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
52 static inline SGPropertyNode *
53 get_prop2 (const SGPropertyNode * arg)
55 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
60 * Get a double value and split it as required.
63 split_value (double full_value, const char * mask,
64 double * unmodifiable, double * modifiable)
66 if (!strcmp("integer", mask)) {
67 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
68 *unmodifiable = full_value - *modifiable;
69 } else if (!strcmp("decimal", mask)) {
70 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
71 *modifiable = full_value - *unmodifiable;
73 if (strcmp("all", mask))
74 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
75 << " assuming 'all'");
77 *modifiable = full_value;
83 * Clamp or wrap a value as specified.
86 limit_value (double * value, const SGPropertyNode * arg)
88 const SGPropertyNode * min_node = arg->getChild("min");
89 const SGPropertyNode * max_node = arg->getChild("max");
91 bool wrap = arg->getBoolValue("wrap");
93 if (min_node == 0 || max_node == 0)
96 if (wrap) { // wrap such that min <= x < max
97 double min_val = min_node->getDoubleValue();
98 double max_val = max_node->getDoubleValue();
99 double resolution = arg->getDoubleValue("resolution");
100 if (resolution > 0.0) {
101 // snap to (min + N*resolution), taking special care to handle imprecision
102 int n = (int)floor((*value - min_val) / resolution + 0.5);
103 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
104 SG_NORMALIZE_RANGE(n, 0, steps);
105 *value = min_val + resolution * n;
107 // plain circular wrapping
108 SG_NORMALIZE_RANGE(*value, min_val, max_val);
110 } else { // clamp such that min <= x <= max
111 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
112 *value = min_node->getDoubleValue();
113 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
114 *value = max_node->getDoubleValue();
120 ////////////////////////////////////////////////////////////////////////
121 // Command implementations.
122 ////////////////////////////////////////////////////////////////////////
126 * Built-in command: do nothing.
129 do_null (const SGPropertyNode * arg)
134 #if defined(HAVE_PLIB_PSL)
136 * Built-in command: run a PSL script.
139 do_script (const SGPropertyNode * arg)
141 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem_mgr()
142 ->get_group(FGSubsystemMgr::GENERAL)->get_subsystem("scripting");
144 return mgr->run(arg->getStringValue("script"));
146 #endif // HAVE_PLIB_PSL
150 * Built-in command: exit FlightGear.
152 * TODO: show a confirm dialog.
155 do_exit (const SGPropertyNode * arg)
157 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
164 * Built-in command: load flight.
166 * file (optional): the name of the file to load (relative to current
167 * directory). Defaults to "fgfs.sav".
170 do_load (const SGPropertyNode * arg)
172 const string &file = arg->getStringValue("file", "fgfs.sav");
173 ifstream input(file.c_str());
174 if (input.good() && fgLoadFlight(input)) {
176 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
179 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
186 * Built-in command: save flight.
188 * file (optional): the name of the file to save (relative to the
189 * current directory). Defaults to "fgfs.sav".
192 do_save (const SGPropertyNode * arg)
194 const string &file = arg->getStringValue("file", "fgfs.sav");
195 bool write_all = arg->getBoolValue("write-all", false);
196 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
197 ofstream output(file.c_str());
198 if (output.good() && fgSaveFlight(output, write_all)) {
200 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
203 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
210 * Built-in command: (re)load the panel.
212 * path (optional): the file name to load the panel from
213 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
214 * and if that's unspecified, to "Panels/Default/default.xml".
217 do_panel_load (const SGPropertyNode * arg)
220 arg->getStringValue("path",
221 fgGetString("/sim/panel/path",
222 "Panels/Default/default.xml"));
223 FGPanel * new_panel = fgReadPanel(panel_path);
224 if (new_panel == 0) {
225 SG_LOG(SG_INPUT, SG_ALERT,
226 "Error reading new panel from " << panel_path);
229 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
230 current_panel->unbind();
231 delete current_panel;
232 current_panel = new_panel;
233 current_panel->bind();
239 * Built-in command: pass a mouse click to the panel.
241 * button: the mouse button number, zero-based.
242 * is-down: true if the button is down, false if it is up.
243 * x-pos: the x position of the mouse click.
244 * y-pos: the y position of the mouse click.
247 do_panel_mouse_click (const SGPropertyNode * arg)
249 if (current_panel != 0)
251 ->doMouseAction(arg->getIntValue("button"),
252 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
253 arg->getIntValue("x-pos"),
254 arg->getIntValue("y-pos"));
261 * Built-in command: (re)load preferences.
263 * path (optional): the file name to load the panel from (relative
264 * to FG_ROOT). Defaults to "preferences.xml".
267 do_preferences_load (const SGPropertyNode * arg)
270 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
271 globals->get_props());
272 } catch (const sg_exception &e) {
273 guiErrorMessage("Error reading global preferences: ", e);
276 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
284 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
285 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
286 if ( globals->get_viewmgr()->get_current() == 1 ) {
287 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
295 globals->get_current_view()->setHeadingOffset_deg(0.0);
296 globals->get_viewmgr()->next_view();
297 fix_hud_visibility();
298 globals->get_tile_mgr()->refresh_view_timestamps();
304 globals->get_current_view()->setHeadingOffset_deg(0.0);
305 globals->get_viewmgr()->prev_view();
306 fix_hud_visibility();
307 globals->get_tile_mgr()->refresh_view_timestamps();
311 * Built-in command: cycle view.
314 do_view_cycle (const SGPropertyNode * arg)
316 globals->get_current_view()->setHeadingOffset_deg(0.0);
317 globals->get_viewmgr()->next_view();
318 fix_hud_visibility();
319 globals->get_tile_mgr()->refresh_view_timestamps();
320 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
325 * Built-in command: capture screen.
328 do_screen_capture (const SGPropertyNode * arg)
336 * Reload the tile cache.
339 do_tile_cache_reload (const SGPropertyNode * arg)
341 static const SGPropertyNode *master_freeze
342 = fgGetNode("/sim/freeze/master");
343 bool freeze = master_freeze->getBoolValue();
344 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
346 fgSetBool("/sim/freeze/master", true);
349 if ( globals->get_tile_mgr()->init() ) {
350 // Load the local scenery data
351 double visibility_meters = fgGetDouble("/environment/visibility-m");
352 globals->get_tile_mgr()->update( visibility_meters );
354 SG_LOG( SG_GENERAL, SG_ALERT,
355 "Error in Tile Manager initialization!" );
360 fgSetBool("/sim/freeze/master", false);
367 * Update the lighting manually.
370 do_lighting_update (const SGPropertyNode * arg)
372 fgUpdateSkyAndLightingParams();
378 * Built-in command: toggle a bool property value.
380 * property: The name of the property to toggle.
383 do_property_toggle (const SGPropertyNode * arg)
385 SGPropertyNode * prop = get_prop(arg);
386 return prop->setBoolValue(!prop->getBoolValue());
391 * Built-in command: assign a value to a property.
393 * property: the name of the property to assign.
394 * value: the value to assign; or
395 * property[1]: the property to copy from.
398 do_property_assign (const SGPropertyNode * arg)
400 SGPropertyNode * prop = get_prop(arg);
401 const SGPropertyNode * prop2 = get_prop2(arg);
402 const SGPropertyNode * value = arg->getNode("value");
405 return prop->setUnspecifiedValue(value->getStringValue());
407 return prop->setUnspecifiedValue(prop2->getStringValue());
414 * Built-in command: increment or decrement a property value.
416 * If the 'step' argument is present, it will be used; otherwise,
417 * the command uses 'offset' and 'factor', usually from the mouse.
419 * property: the name of the property to increment or decrement.
420 * step: the amount of the increment or decrement (default: 0).
421 * offset: offset from the current setting (used for the mouse; multiplied
423 * factor: scaling amount for the offset (defaults to 1).
424 * min: the minimum allowed value (default: no minimum).
425 * max: the maximum allowed value (default: no maximum).
426 * mask: 'integer' to apply only to the left of the decimal point,
427 * 'decimal' to apply only to the right of the decimal point,
428 * or 'all' to apply to the whole number (the default).
429 * wrap: true if the value should be wrapped when it passes min or max;
430 * both min and max must be present for this to work (default:
434 do_property_adjust (const SGPropertyNode * arg)
436 SGPropertyNode * prop = get_prop(arg);
439 if (arg->hasValue("step"))
440 amount = arg->getDoubleValue("step");
442 amount = (arg->getDoubleValue("factor", 1)
443 * arg->getDoubleValue("offset"));
445 double unmodifiable, modifiable;
446 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
447 &unmodifiable, &modifiable);
448 modifiable += amount;
449 limit_value(&modifiable, arg);
451 prop->setDoubleValue(unmodifiable + modifiable);
458 * Built-in command: multiply a property value.
460 * property: the name of the property to multiply.
461 * factor: the amount by which to multiply.
462 * min: the minimum allowed value (default: no minimum).
463 * max: the maximum allowed value (default: no maximum).
464 * mask: 'integer' to apply only to the left of the decimal point,
465 * 'decimal' to apply only to the right of the decimal point,
466 * or 'all' to apply to the whole number (the default).
467 * wrap: true if the value should be wrapped when it passes min or max;
468 * both min and max must be present for this to work (default:
472 do_property_multiply (const SGPropertyNode * arg)
474 SGPropertyNode * prop = get_prop(arg);
475 double factor = arg->getDoubleValue("factor", 1);
477 double unmodifiable, modifiable;
478 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
479 &unmodifiable, &modifiable);
480 modifiable *= factor;
481 limit_value(&modifiable, arg);
483 prop->setDoubleValue(unmodifiable + modifiable);
490 * Built-in command: swap two property values.
492 * property[0]: the name of the first property.
493 * property[1]: the name of the second property.
496 do_property_swap (const SGPropertyNode * arg)
498 SGPropertyNode * prop1 = get_prop(arg);
499 SGPropertyNode * prop2 = get_prop2(arg);
501 // FIXME: inefficient
502 const string & tmp = prop1->getStringValue();
503 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
504 prop2->setUnspecifiedValue(tmp.c_str()));
509 * Built-in command: Set a property to an axis or other moving input.
511 * property: the name of the property to set.
512 * setting: the current input setting, usually between -1.0 and 1.0.
513 * offset: the offset to shift by, before applying the factor.
514 * factor: the factor to multiply by (use negative to reverse).
517 do_property_scale (const SGPropertyNode * arg)
519 SGPropertyNode * prop = get_prop(arg);
520 double setting = arg->getDoubleValue("setting");
521 double offset = arg->getDoubleValue("offset", 0.0);
522 double factor = arg->getDoubleValue("factor", 1.0);
523 bool squared = arg->getBoolValue("squared", false);
526 setting = (setting < 0 ? -1 : 1) * setting * setting;
528 return prop->setDoubleValue((setting + offset) * factor);
533 * Built-in command: Show an XML-configured dialog.
535 * dialog-name: the name of the GUI dialog to display.
538 do_dialog_show (const SGPropertyNode * arg)
540 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
541 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
542 gui->display(arg->getStringValue("dialog-name"));
548 * Built-in Command: Hide the active XML-configured dialog.
551 do_dialog_close (const SGPropertyNode * arg)
553 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
554 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
555 GUIWidget * widget = gui->getCurrentWidget();
558 gui->setCurrentWidget(0);
567 * Update a value in the active XML-configured dialog.
569 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
572 do_dialog_update (const SGPropertyNode * arg)
574 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
575 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
576 GUIWidget * widget = gui->getCurrentWidget();
578 if (arg->hasValue("object-name")) {
579 gui->getCurrentWidget()
580 ->updateValue(arg->getStringValue("object-name"));
582 gui->getCurrentWidget()->updateValues();
592 * Apply a value in the active XML-configured dialog.
594 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
597 do_dialog_apply (const SGPropertyNode * arg)
599 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
600 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
601 GUIWidget * widget = gui->getCurrentWidget();
603 if (arg->hasValue("object-name")) {
604 gui->getCurrentWidget()
605 ->applyValue(arg->getStringValue("object-name"));
607 gui->getCurrentWidget()->applyValues();
617 * Built-in command: commit presets (read from in /sim/presets/)
620 do_presets_commit (const SGPropertyNode * arg)
622 // unbind the current fdm state so property changes
623 // don't get lost when we subsequently delete this fdm
624 // and create a new one.
625 cur_fdm_state->unbind();
627 // set position from presets
631 fgReInitSubsystems();
633 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
635 if ( ! fgGetBool("/sim/presets/onground") ) {
636 fgSetBool( "/sim/freeze/master", true );
637 fgSetBool( "/sim/freeze/clock", true );
645 ////////////////////////////////////////////////////////////////////////
647 ////////////////////////////////////////////////////////////////////////
651 * Table of built-in commands.
653 * New commands do not have to be added here; any module in the application
654 * can add a new command using globals->get_commands()->addCommand(...).
658 SGCommandMgr::command_t command;
661 #if defined(HAVE_PLIB_PSL)
662 { "script", do_script },
663 #endif // HAVE_PLIB_PSL
667 { "panel-load", do_panel_load },
668 { "panel-mouse-click", do_panel_mouse_click },
669 { "preferences-load", do_preferences_load },
670 { "view-cycle", do_view_cycle },
671 { "screen-capture", do_screen_capture },
672 { "tile-cache-reload", do_tile_cache_reload },
673 { "lighting-update", do_lighting_update },
674 { "property-toggle", do_property_toggle },
675 { "property-assign", do_property_assign },
676 { "property-adjust", do_property_adjust },
677 { "property-multiply", do_property_multiply },
678 { "property-swap", do_property_swap },
679 { "property-scale", do_property_scale },
680 { "dialog-show", do_dialog_show },
681 { "dialog-close", do_dialog_close },
682 { "dialog-show", do_dialog_show },
683 { "dialog-update", do_dialog_update },
684 { "dialog-apply", do_dialog_apply },
685 { "presets-commit", do_presets_commit },
686 { 0, 0 } // zero-terminated
691 * Initialize the default built-in commands.
693 * Other commands may be added by other parts of the application.
698 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
699 for (int i = 0; built_ins[i].name != 0; i++) {
700 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
701 globals->get_commands()->addCommand(built_ins[i].name,
702 built_ins[i].command);
705 typedef bool (*dummy)();
706 fgTie( "/command/view/next", dummy(0), do_view_next );
707 fgTie( "/command/view/prev", dummy(0), do_view_prev );
710 // end of fg_commands.cxx