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();
121 ////////////////////////////////////////////////////////////////////////
122 // Command implementations.
123 ////////////////////////////////////////////////////////////////////////
127 * Built-in command: do nothing.
130 do_null (const SGPropertyNode * arg)
135 #if defined(HAVE_PLIB_PSL)
137 * Built-in command: run a PSL script.
140 do_script (const SGPropertyNode * arg)
142 FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem_mgr()
143 ->get_group(FGSubsystemMgr::GENERAL)->get_subsystem("scripting");
145 return mgr->run(arg->getStringValue("script"));
147 #endif // HAVE_PLIB_PSL
151 * Built-in command: exit FlightGear.
153 * TODO: show a confirm dialog.
156 do_exit (const SGPropertyNode * arg)
158 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
165 * Built-in command: load flight.
167 * file (optional): the name of the file to load (relative to current
168 * directory). Defaults to "fgfs.sav".
171 do_load (const SGPropertyNode * arg)
173 const string &file = arg->getStringValue("file", "fgfs.sav");
174 ifstream input(file.c_str());
175 if (input.good() && fgLoadFlight(input)) {
177 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
180 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
187 * Built-in command: save flight.
189 * file (optional): the name of the file to save (relative to the
190 * current directory). Defaults to "fgfs.sav".
193 do_save (const SGPropertyNode * arg)
195 const string &file = arg->getStringValue("file", "fgfs.sav");
196 bool write_all = arg->getBoolValue("write-all", false);
197 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
198 ofstream output(file.c_str());
199 if (output.good() && fgSaveFlight(output, write_all)) {
201 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
204 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
211 * Built-in command: (re)load the panel.
213 * path (optional): the file name to load the panel from
214 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
215 * and if that's unspecified, to "Panels/Default/default.xml".
218 do_panel_load (const SGPropertyNode * arg)
221 arg->getStringValue("path",
222 fgGetString("/sim/panel/path",
223 "Panels/Default/default.xml"));
224 FGPanel * new_panel = fgReadPanel(panel_path);
225 if (new_panel == 0) {
226 SG_LOG(SG_INPUT, SG_ALERT,
227 "Error reading new panel from " << panel_path);
230 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
231 current_panel->unbind();
232 delete current_panel;
233 current_panel = new_panel;
234 current_panel->bind();
240 * Built-in command: pass a mouse click to the panel.
242 * button: the mouse button number, zero-based.
243 * is-down: true if the button is down, false if it is up.
244 * x-pos: the x position of the mouse click.
245 * y-pos: the y position of the mouse click.
248 do_panel_mouse_click (const SGPropertyNode * arg)
250 if (current_panel != 0)
252 ->doMouseAction(arg->getIntValue("button"),
253 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
254 arg->getIntValue("x-pos"),
255 arg->getIntValue("y-pos"));
262 * Built-in command: (re)load preferences.
264 * path (optional): the file name to load the panel from (relative
265 * to FG_ROOT). Defaults to "preferences.xml".
268 do_preferences_load (const SGPropertyNode * arg)
271 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
272 globals->get_props());
273 } catch (const sg_exception &e) {
274 guiErrorMessage("Error reading global preferences: ", e);
277 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
285 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
286 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
287 if ( globals->get_viewmgr()->get_current() == 1 ) {
288 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
296 globals->get_current_view()->setHeadingOffset_deg(0.0);
297 globals->get_viewmgr()->next_view();
298 fix_hud_visibility();
299 globals->get_tile_mgr()->refresh_view_timestamps();
305 globals->get_current_view()->setHeadingOffset_deg(0.0);
306 globals->get_viewmgr()->prev_view();
307 fix_hud_visibility();
308 globals->get_tile_mgr()->refresh_view_timestamps();
312 * Built-in command: cycle view.
315 do_view_cycle (const SGPropertyNode * arg)
317 globals->get_current_view()->setHeadingOffset_deg(0.0);
318 globals->get_viewmgr()->next_view();
319 fix_hud_visibility();
320 globals->get_tile_mgr()->refresh_view_timestamps();
321 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
326 * Built-in command: capture screen.
329 do_screen_capture (const SGPropertyNode * arg)
337 * Reload the tile cache.
340 do_tile_cache_reload (const SGPropertyNode * arg)
342 static const SGPropertyNode *master_freeze
343 = fgGetNode("/sim/freeze/master");
344 bool freeze = master_freeze->getBoolValue();
345 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
347 fgSetBool("/sim/freeze/master", true);
350 if ( globals->get_tile_mgr()->init() ) {
351 // Load the local scenery data
352 double visibility_meters = fgGetDouble("/environment/visibility-m");
353 globals->get_tile_mgr()->update( visibility_meters );
355 SG_LOG( SG_GENERAL, SG_ALERT,
356 "Error in Tile Manager initialization!" );
361 fgSetBool("/sim/freeze/master", false);
368 * Update the lighting manually.
371 do_lighting_update (const SGPropertyNode * arg)
373 fgUpdateSkyAndLightingParams();
379 * Built-in command: toggle a bool property value.
381 * property: The name of the property to toggle.
384 do_property_toggle (const SGPropertyNode * arg)
386 SGPropertyNode * prop = get_prop(arg);
387 return prop->setBoolValue(!prop->getBoolValue());
392 * Built-in command: assign a value to a property.
394 * property: the name of the property to assign.
395 * value: the value to assign; or
396 * property[1]: the property to copy from.
399 do_property_assign (const SGPropertyNode * arg)
401 SGPropertyNode * prop = get_prop(arg);
402 const SGPropertyNode * prop2 = get_prop2(arg);
403 const SGPropertyNode * value = arg->getNode("value");
406 return prop->setUnspecifiedValue(value->getStringValue());
408 return prop->setUnspecifiedValue(prop2->getStringValue());
415 * Built-in command: increment or decrement a property value.
417 * If the 'step' argument is present, it will be used; otherwise,
418 * the command uses 'offset' and 'factor', usually from the mouse.
420 * property: the name of the property to increment or decrement.
421 * step: the amount of the increment or decrement (default: 0).
422 * offset: offset from the current setting (used for the mouse; multiplied
424 * factor: scaling amount for the offset (defaults to 1).
425 * min: the minimum allowed value (default: no minimum).
426 * max: the maximum allowed value (default: no maximum).
427 * mask: 'integer' to apply only to the left of the decimal point,
428 * 'decimal' to apply only to the right of the decimal point,
429 * or 'all' to apply to the whole number (the default).
430 * wrap: true if the value should be wrapped when it passes min or max;
431 * both min and max must be present for this to work (default:
435 do_property_adjust (const SGPropertyNode * arg)
437 SGPropertyNode * prop = get_prop(arg);
440 if (arg->hasValue("step"))
441 amount = arg->getDoubleValue("step");
443 amount = (arg->getDoubleValue("factor", 1)
444 * arg->getDoubleValue("offset"));
446 double unmodifiable, modifiable;
447 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
448 &unmodifiable, &modifiable);
449 modifiable += amount;
450 limit_value(&modifiable, arg);
452 prop->setDoubleValue(unmodifiable + modifiable);
459 * Built-in command: multiply a property value.
461 * property: the name of the property to multiply.
462 * factor: the amount by which to multiply.
463 * min: the minimum allowed value (default: no minimum).
464 * max: the maximum allowed value (default: no maximum).
465 * mask: 'integer' to apply only to the left of the decimal point,
466 * 'decimal' to apply only to the right of the decimal point,
467 * or 'all' to apply to the whole number (the default).
468 * wrap: true if the value should be wrapped when it passes min or max;
469 * both min and max must be present for this to work (default:
473 do_property_multiply (const SGPropertyNode * arg)
475 SGPropertyNode * prop = get_prop(arg);
476 double factor = arg->getDoubleValue("factor", 1);
478 double unmodifiable, modifiable;
479 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
480 &unmodifiable, &modifiable);
481 modifiable *= factor;
482 limit_value(&modifiable, arg);
484 prop->setDoubleValue(unmodifiable + modifiable);
491 * Built-in command: swap two property values.
493 * property[0]: the name of the first property.
494 * property[1]: the name of the second property.
497 do_property_swap (const SGPropertyNode * arg)
499 SGPropertyNode * prop1 = get_prop(arg);
500 SGPropertyNode * prop2 = get_prop2(arg);
502 // FIXME: inefficient
503 const string & tmp = prop1->getStringValue();
504 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
505 prop2->setUnspecifiedValue(tmp.c_str()));
510 * Built-in command: Set a property to an axis or other moving input.
512 * property: the name of the property to set.
513 * setting: the current input setting, usually between -1.0 and 1.0.
514 * offset: the offset to shift by, before applying the factor.
515 * factor: the factor to multiply by (use negative to reverse).
518 do_property_scale (const SGPropertyNode * arg)
520 SGPropertyNode * prop = get_prop(arg);
521 double setting = arg->getDoubleValue("setting");
522 double offset = arg->getDoubleValue("offset", 0.0);
523 double factor = arg->getDoubleValue("factor", 1.0);
524 bool squared = arg->getBoolValue("squared", false);
527 setting = (setting < 0 ? -1 : 1) * setting * setting;
529 return prop->setDoubleValue((setting + offset) * factor);
534 * Built-in command: Show an XML-configured dialog.
536 * dialog-name: the name of the GUI dialog to display.
539 do_dialog_show (const SGPropertyNode * arg)
541 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
542 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
543 gui->display(arg->getStringValue("dialog-name"));
549 * Built-in Command: Hide the active XML-configured dialog.
552 do_dialog_close (const SGPropertyNode * arg)
554 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
555 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
556 FGDialog * widget = gui->getCurrentWidget();
559 gui->setCurrentWidget(0);
568 * Update a value in the active XML-configured dialog.
570 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
573 do_dialog_update (const SGPropertyNode * arg)
575 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
576 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
577 FGDialog * widget = gui->getCurrentWidget();
579 if (arg->hasValue("object-name")) {
580 gui->getCurrentWidget()
581 ->updateValue(arg->getStringValue("object-name"));
583 gui->getCurrentWidget()->updateValues();
593 * Apply a value in the active XML-configured dialog.
595 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
598 do_dialog_apply (const SGPropertyNode * arg)
600 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
601 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
602 FGDialog * widget = gui->getCurrentWidget();
604 if (arg->hasValue("object-name")) {
605 gui->getCurrentWidget()
606 ->applyValue(arg->getStringValue("object-name"));
608 gui->getCurrentWidget()->applyValues();
618 * Built-in command: commit presets (read from in /sim/presets/)
621 do_presets_commit (const SGPropertyNode * arg)
623 // unbind the current fdm state so property changes
624 // don't get lost when we subsequently delete this fdm
625 // and create a new one.
626 cur_fdm_state->unbind();
628 // set position from presets
632 fgReInitSubsystems();
634 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
636 if ( ! fgGetBool("/sim/presets/onground") ) {
637 fgSetBool( "/sim/freeze/master", true );
638 fgSetBool( "/sim/freeze/clock", true );
646 ////////////////////////////////////////////////////////////////////////
648 ////////////////////////////////////////////////////////////////////////
652 * Table of built-in commands.
654 * New commands do not have to be added here; any module in the application
655 * can add a new command using globals->get_commands()->addCommand(...).
659 SGCommandMgr::command_t command;
662 #if defined(HAVE_PLIB_PSL)
663 { "script", do_script },
664 #endif // HAVE_PLIB_PSL
668 { "panel-load", do_panel_load },
669 { "panel-mouse-click", do_panel_mouse_click },
670 { "preferences-load", do_preferences_load },
671 { "view-cycle", do_view_cycle },
672 { "screen-capture", do_screen_capture },
673 { "tile-cache-reload", do_tile_cache_reload },
674 { "lighting-update", do_lighting_update },
675 { "property-toggle", do_property_toggle },
676 { "property-assign", do_property_assign },
677 { "property-adjust", do_property_adjust },
678 { "property-multiply", do_property_multiply },
679 { "property-swap", do_property_swap },
680 { "property-scale", do_property_scale },
681 { "dialog-show", do_dialog_show },
682 { "dialog-close", do_dialog_close },
683 { "dialog-show", do_dialog_show },
684 { "dialog-update", do_dialog_update },
685 { "dialog-apply", do_dialog_apply },
686 { "presets-commit", do_presets_commit },
687 { 0, 0 } // zero-terminated
692 * Initialize the default built-in commands.
694 * Other commands may be added by other parts of the application.
699 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
700 for (int i = 0; built_ins[i].name != 0; i++) {
701 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
702 globals->get_commands()->addCommand(built_ins[i].name,
703 built_ins[i].command);
706 typedef bool (*dummy)();
707 fgTie( "/command/view/next", dummy(0), do_view_next );
708 fgTie( "/command/view/prev", dummy(0), do_view_prev );
711 // end of fg_commands.cxx