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 #include <Time/tmp.hxx>
24 #include "fg_init.hxx"
25 #include "fg_commands.hxx"
28 SG_USING_STD(ifstream);
29 SG_USING_STD(ofstream);
31 #include "fg_props.hxx"
33 #include "globals.hxx"
34 #include "viewmgr.hxx"
38 ////////////////////////////////////////////////////////////////////////
39 // Static helper functions.
40 ////////////////////////////////////////////////////////////////////////
43 static inline SGPropertyNode *
44 get_prop (const SGPropertyNode * arg)
46 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
49 static inline SGPropertyNode *
50 get_prop2 (const SGPropertyNode * arg)
52 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
57 * Get a double value and split it as required.
60 split_value (double full_value, const char * mask,
61 double * unmodifiable, double * modifiable)
63 if (!strcmp("integer", mask)) {
64 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
65 *unmodifiable = full_value - *modifiable;
66 } else if (!strcmp("decimal", mask)) {
67 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
68 *modifiable = full_value - *unmodifiable;
70 if (strcmp("all", mask))
71 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
72 << " assuming 'all'");
74 *modifiable = full_value;
80 * Clamp or wrap a value as specified.
83 limit_value (double * value, const SGPropertyNode * arg)
85 const SGPropertyNode * min_node = arg->getChild("min");
86 const SGPropertyNode * max_node = arg->getChild("max");
88 bool wrap = arg->getBoolValue("wrap");
90 if (min_node == 0 || max_node == 0)
93 if (wrap) { // wrap such that min <= x < max
94 double min_val = min_node->getDoubleValue();
95 double max_val = max_node->getDoubleValue();
96 double resolution = arg->getDoubleValue("resolution");
97 if (resolution > 0.0) {
98 // snap to (min + N*resolution), taking special care to handle imprecision
99 int n = (int)floor((*value - min_val) / resolution + 0.5);
100 int steps = (int)floor((max_val - min_val) / resolution + 0.5);
101 SG_NORMALIZE_RANGE(n, 0, steps);
102 *value = min_val + resolution * n;
104 // plain circular wrapping
105 SG_NORMALIZE_RANGE(*value, min_val, max_val);
107 } else { // clamp such that min <= x <= max
108 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
109 *value = min_node->getDoubleValue();
110 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
111 *value = max_node->getDoubleValue();
117 ////////////////////////////////////////////////////////////////////////
118 // Command implementations.
119 ////////////////////////////////////////////////////////////////////////
123 * Built-in command: do nothing.
126 do_null (const SGPropertyNode * arg)
133 * Built-in command: exit FlightGear.
135 * TODO: show a confirm dialog.
138 do_exit (const SGPropertyNode * arg)
140 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
147 * Built-in command: load flight.
149 * file (optional): the name of the file to load (relative to current
150 * directory). Defaults to "fgfs.sav".
153 do_load (const SGPropertyNode * arg)
155 const string &file = arg->getStringValue("file", "fgfs.sav");
156 ifstream input(file.c_str());
157 if (input.good() && fgLoadFlight(input)) {
159 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
162 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
169 * Built-in command: save flight.
171 * file (optional): the name of the file to save (relative to the
172 * current directory). Defaults to "fgfs.sav".
175 do_save (const SGPropertyNode * arg)
177 const string &file = arg->getStringValue("file", "fgfs.sav");
178 bool write_all = arg->getBoolValue("write-all", false);
179 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
180 ofstream output(file.c_str());
181 if (output.good() && fgSaveFlight(output, write_all)) {
183 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
186 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
193 * Built-in command: (re)load the panel.
195 * path (optional): the file name to load the panel from
196 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
197 * and if that's unspecified, to "Panels/Default/default.xml".
200 do_panel_load (const SGPropertyNode * arg)
203 arg->getStringValue("path",
204 fgGetString("/sim/panel/path",
205 "Panels/Default/default.xml"));
206 FGPanel * new_panel = fgReadPanel(panel_path);
207 if (new_panel == 0) {
208 SG_LOG(SG_INPUT, SG_ALERT,
209 "Error reading new panel from " << panel_path);
212 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
213 current_panel->unbind();
214 delete current_panel;
215 current_panel = new_panel;
216 current_panel->bind();
222 * Built-in command: pass a mouse click to the panel.
224 * button: the mouse button number, zero-based.
225 * is-down: true if the button is down, false if it is up.
226 * x-pos: the x position of the mouse click.
227 * y-pos: the y position of the mouse click.
230 do_panel_mouse_click (const SGPropertyNode * arg)
232 if (current_panel != 0)
234 ->doMouseAction(arg->getIntValue("button"),
235 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
236 arg->getIntValue("x-pos"),
237 arg->getIntValue("y-pos"));
244 * Built-in command: (re)load preferences.
246 * path (optional): the file name to load the panel from (relative
247 * to FG_ROOT). Defaults to "preferences.xml".
250 do_preferences_load (const SGPropertyNode * arg)
253 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
254 globals->get_props());
255 } catch (const sg_exception &e) {
256 guiErrorMessage("Error reading global preferences: ", e);
259 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
267 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
268 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
269 if ( globals->get_viewmgr()->get_current() == 1 ) {
270 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
278 globals->get_current_view()->setHeadingOffset_deg(0.0);
279 globals->get_viewmgr()->next_view();
280 fix_hud_visibility();
281 globals->get_tile_mgr()->refresh_view_timestamps();
287 globals->get_current_view()->setHeadingOffset_deg(0.0);
288 globals->get_viewmgr()->prev_view();
289 fix_hud_visibility();
290 globals->get_tile_mgr()->refresh_view_timestamps();
294 * Built-in command: cycle view.
297 do_view_cycle (const SGPropertyNode * arg)
299 globals->get_current_view()->setHeadingOffset_deg(0.0);
300 globals->get_viewmgr()->next_view();
301 fix_hud_visibility();
302 globals->get_tile_mgr()->refresh_view_timestamps();
303 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
308 * Built-in command: capture screen.
311 do_screen_capture (const SGPropertyNode * arg)
319 * Reload the tile cache.
322 do_tile_cache_reload (const SGPropertyNode * arg)
324 static const SGPropertyNode *master_freeze
325 = fgGetNode("/sim/freeze/master");
326 bool freeze = master_freeze->getBoolValue();
327 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
329 fgSetBool("/sim/freeze/master", true);
332 if ( globals->get_tile_mgr()->init() ) {
333 // Load the local scenery data
334 double visibility_meters = fgGetDouble("/environment/visibility-m");
335 globals->get_tile_mgr()->update( visibility_meters );
337 SG_LOG( SG_GENERAL, SG_ALERT,
338 "Error in Tile Manager initialization!" );
343 fgSetBool("/sim/freeze/master", false);
350 * Update the lighting manually.
353 do_lighting_update (const SGPropertyNode * arg)
355 fgUpdateSkyAndLightingParams();
361 * Built-in command: toggle a bool property value.
363 * property: The name of the property to toggle.
366 do_property_toggle (const SGPropertyNode * arg)
368 SGPropertyNode * prop = get_prop(arg);
369 return prop->setBoolValue(!prop->getBoolValue());
374 * Built-in command: assign a value to a property.
376 * property: the name of the property to assign.
377 * value: the value to assign; or
378 * property[1]: the property to copy from.
381 do_property_assign (const SGPropertyNode * arg)
383 SGPropertyNode * prop = get_prop(arg);
384 const SGPropertyNode * prop2 = get_prop2(arg);
385 const SGPropertyNode * value = arg->getNode("value");
388 return prop->setUnspecifiedValue(value->getStringValue());
390 return prop->setUnspecifiedValue(prop2->getStringValue());
397 * Built-in command: increment or decrement a property value.
399 * If the 'step' argument is present, it will be used; otherwise,
400 * the command uses 'offset' and 'factor', usually from the mouse.
402 * property: the name of the property to increment or decrement.
403 * step: the amount of the increment or decrement (default: 0).
404 * offset: offset from the current setting (used for the mouse; multiplied
406 * factor: scaling amount for the offset (defaults to 1).
407 * min: the minimum allowed value (default: no minimum).
408 * max: the maximum allowed value (default: no maximum).
409 * mask: 'integer' to apply only to the left of the decimal point,
410 * 'decimal' to apply only to the right of the decimal point,
411 * or 'all' to apply to the whole number (the default).
412 * wrap: true if the value should be wrapped when it passes min or max;
413 * both min and max must be present for this to work (default:
417 do_property_adjust (const SGPropertyNode * arg)
419 SGPropertyNode * prop = get_prop(arg);
422 if (arg->hasValue("step"))
423 amount = arg->getDoubleValue("step");
425 amount = (arg->getDoubleValue("factor", 1)
426 * arg->getDoubleValue("offset"));
428 double unmodifiable, modifiable;
429 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
430 &unmodifiable, &modifiable);
431 modifiable += amount;
432 limit_value(&modifiable, arg);
434 prop->setDoubleValue(unmodifiable + modifiable);
441 * Built-in command: multiply a property value.
443 * property: the name of the property to multiply.
444 * factor: the amount by which to multiply.
445 * min: the minimum allowed value (default: no minimum).
446 * max: the maximum allowed value (default: no maximum).
447 * mask: 'integer' to apply only to the left of the decimal point,
448 * 'decimal' to apply only to the right of the decimal point,
449 * or 'all' to apply to the whole number (the default).
450 * wrap: true if the value should be wrapped when it passes min or max;
451 * both min and max must be present for this to work (default:
455 do_property_multiply (const SGPropertyNode * arg)
457 SGPropertyNode * prop = get_prop(arg);
458 double factor = arg->getDoubleValue("factor", 1);
460 double unmodifiable, modifiable;
461 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
462 &unmodifiable, &modifiable);
463 modifiable *= factor;
464 limit_value(&modifiable, arg);
466 prop->setDoubleValue(unmodifiable + modifiable);
473 * Built-in command: swap two property values.
475 * property[0]: the name of the first property.
476 * property[1]: the name of the second property.
479 do_property_swap (const SGPropertyNode * arg)
481 SGPropertyNode * prop1 = get_prop(arg);
482 SGPropertyNode * prop2 = get_prop2(arg);
484 // FIXME: inefficient
485 const string & tmp = prop1->getStringValue();
486 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
487 prop2->setUnspecifiedValue(tmp.c_str()));
492 * Built-in command: Set a property to an axis or other moving input.
494 * property: the name of the property to set.
495 * setting: the current input setting, usually between -1.0 and 1.0.
496 * offset: the offset to shift by, before applying the factor.
497 * factor: the factor to multiply by (use negative to reverse).
500 do_property_scale (const SGPropertyNode * arg)
502 SGPropertyNode * prop = get_prop(arg);
503 double setting = arg->getDoubleValue("setting");
504 double offset = arg->getDoubleValue("offset", 0.0);
505 double factor = arg->getDoubleValue("factor", 1.0);
506 bool squared = arg->getBoolValue("squared", false);
509 setting = (setting < 0 ? -1 : 1) * setting * setting;
511 return prop->setDoubleValue((setting + offset) * factor);
516 * Built-in command: Show an XML-configured dialog.
518 * dialog-name: the name of the GUI dialog to display.
521 do_dialog_show (const SGPropertyNode * arg)
523 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
524 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
525 gui->display(arg->getStringValue("dialog-name"));
531 * Built-in Command: Hide the active XML-configured dialog.
534 do_dialog_close (const SGPropertyNode * arg)
536 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
537 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
538 GUIWidget * widget = gui->getCurrentWidget();
541 gui->setCurrentWidget(0);
550 * Update a value in the active XML-configured dialog.
552 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
555 do_dialog_update (const SGPropertyNode * arg)
557 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
558 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
559 GUIWidget * widget = gui->getCurrentWidget();
561 if (arg->hasValue("object-name")) {
562 gui->getCurrentWidget()
563 ->updateValue(arg->getStringValue("object-name"));
565 gui->getCurrentWidget()->updateValues();
575 * Apply a value in the active XML-configured dialog.
577 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
580 do_dialog_apply (const SGPropertyNode * arg)
582 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
583 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
584 GUIWidget * widget = gui->getCurrentWidget();
586 if (arg->hasValue("object-name")) {
587 gui->getCurrentWidget()
588 ->applyValue(arg->getStringValue("object-name"));
590 gui->getCurrentWidget()->applyValues();
600 * Built-in command: commit presets (read from in /sim/presets/)
603 do_presets_commit (const SGPropertyNode * arg)
605 // unbind the current fdm state so property changes
606 // don't get lost when we subsequently delete this fdm
607 // and create a new one.
608 cur_fdm_state->unbind();
610 // set position from presets
614 fgReInitSubsystems();
616 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
618 if ( ! fgGetBool("/sim/presets/onground") ) {
619 fgSetBool( "/sim/freeze/master", true );
620 fgSetBool( "/sim/freeze/clock", true );
628 ////////////////////////////////////////////////////////////////////////
630 ////////////////////////////////////////////////////////////////////////
634 * Table of built-in commands.
636 * New commands do not have to be added here; any module in the application
637 * can add a new command using globals->get_commands()->addCommand(...).
641 SGCommandMgr::command_t command;
647 { "panel-load", do_panel_load },
648 { "panel-mouse-click", do_panel_mouse_click },
649 { "preferences-load", do_preferences_load },
650 { "view-cycle", do_view_cycle },
651 { "screen-capture", do_screen_capture },
652 { "tile-cache-reload", do_tile_cache_reload },
653 { "lighting-update", do_lighting_update },
654 { "property-toggle", do_property_toggle },
655 { "property-assign", do_property_assign },
656 { "property-adjust", do_property_adjust },
657 { "property-multiply", do_property_multiply },
658 { "property-swap", do_property_swap },
659 { "property-scale", do_property_scale },
660 { "dialog-show", do_dialog_show },
661 { "dialog-close", do_dialog_close },
662 { "dialog-show", do_dialog_show },
663 { "dialog-update", do_dialog_update },
664 { "dialog-apply", do_dialog_apply },
665 { "presets-commit", do_presets_commit },
666 { 0, 0 } // zero-terminated
671 * Initialize the default built-in commands.
673 * Other commands may be added by other parts of the application.
678 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
679 for (int i = 0; built_ins[i].name != 0; i++) {
680 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
681 globals->get_commands()->addCommand(built_ins[i].name,
682 built_ins[i].command);
685 typedef bool (*dummy)();
686 fgTie( "/command/view/next", dummy(0), do_view_next );
687 fgTie( "/command/view/prev", dummy(0), do_view_prev );
690 // end of fg_commands.cxx