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>
15 #include <Cockpit/panel.hxx>
16 #include <Cockpit/panel_io.hxx>
17 #include <FDM/flight.hxx>
19 #include <GUI/new_gui.hxx>
20 #include <Scenery/tilemgr.hxx>
21 #include <Time/tmp.hxx>
23 #include "fg_init.hxx"
24 #include "fg_commands.hxx"
27 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
28 SG_USING_STD(ifstream);
29 SG_USING_STD(ofstream);
32 #include "fg_props.hxx"
34 #include "globals.hxx"
35 #include "viewmgr.hxx"
39 ////////////////////////////////////////////////////////////////////////
40 // Static helper functions.
41 ////////////////////////////////////////////////////////////////////////
45 * Template function to wrap a value.
49 do_wrap (T * value, T min, T max)
51 if (min >= max) { // basic sanity check
53 } else if (*value > max) {
55 } else if (*value < min) {
60 static inline SGPropertyNode *
61 get_prop (const SGPropertyNode * arg)
63 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
66 static inline SGPropertyNode *
67 get_prop2 (const SGPropertyNode * arg)
69 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
74 * Get a double value and split it as required.
77 split_value (double full_value, const char * mask,
78 double * unmodifiable, double * modifiable)
80 if (!strcmp("integer", mask)) {
81 *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
82 *unmodifiable = full_value - *modifiable;
83 } else if (!strcmp("decimal", mask)) {
84 *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
85 *modifiable = full_value - *unmodifiable;
87 if (strcmp("all", mask))
88 SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
89 << " assuming 'all'");
91 *modifiable = full_value;
97 * Clamp or wrap a value as specified.
100 limit_value (double * value, const SGPropertyNode * arg)
102 const SGPropertyNode * min_node = arg->getChild("min");
103 const SGPropertyNode * max_node = arg->getChild("max");
105 bool wrap = arg->getBoolValue("wrap");
107 if (min_node == 0 || max_node == 0)
111 if (*value < min_node->getDoubleValue())
112 *value = max_node->getDoubleValue();
113 else if (*value > max_node->getDoubleValue())
114 *value = min_node->getDoubleValue();
116 if ((min_node != 0) && (*value < min_node->getDoubleValue()))
117 *value = min_node->getDoubleValue();
118 else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
119 *value = max_node->getDoubleValue();
125 ////////////////////////////////////////////////////////////////////////
126 // Command implementations.
127 ////////////////////////////////////////////////////////////////////////
131 * Built-in command: do nothing.
134 do_null (const SGPropertyNode * arg)
141 * Built-in command: exit FlightGear.
143 * TODO: show a confirm dialog.
146 do_exit (const SGPropertyNode * arg)
148 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
155 * Built-in command: load flight.
157 * file (optional): the name of the file to load (relative to current
158 * directory). Defaults to "fgfs.sav".
161 do_load (const SGPropertyNode * arg)
163 const string &file = arg->getStringValue("file", "fgfs.sav");
164 ifstream input(file.c_str());
165 if (input.good() && fgLoadFlight(input)) {
167 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
170 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
177 * Built-in command: save flight.
179 * file (optional): the name of the file to save (relative to the
180 * current directory). Defaults to "fgfs.sav".
183 do_save (const SGPropertyNode * arg)
185 const string &file = arg->getStringValue("file", "fgfs.sav");
186 bool write_all = arg->getBoolValue("write-all", false);
187 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
188 ofstream output(file.c_str());
189 if (output.good() && fgSaveFlight(output, write_all)) {
191 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
194 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
201 * Built-in command: (re)load the panel.
203 * path (optional): the file name to load the panel from
204 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
205 * and if that's unspecified, to "Panels/Default/default.xml".
208 do_panel_load (const SGPropertyNode * arg)
211 arg->getStringValue("path",
212 fgGetString("/sim/panel/path",
213 "Panels/Default/default.xml"));
214 FGPanel * new_panel = fgReadPanel(panel_path);
215 if (new_panel == 0) {
216 SG_LOG(SG_INPUT, SG_ALERT,
217 "Error reading new panel from " << panel_path);
220 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
221 current_panel->unbind();
222 delete current_panel;
223 current_panel = new_panel;
224 current_panel->bind();
230 * Built-in command: pass a mouse click to the panel.
232 * button: the mouse button number, zero-based.
233 * is-down: true if the button is down, false if it is up.
234 * x-pos: the x position of the mouse click.
235 * y-pos: the y position of the mouse click.
238 do_panel_mouse_click (const SGPropertyNode * arg)
240 if (current_panel != 0)
242 ->doMouseAction(arg->getIntValue("button"),
243 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
244 arg->getIntValue("x-pos"),
245 arg->getIntValue("y-pos"));
252 * Built-in command: (re)load preferences.
254 * path (optional): the file name to load the panel from (relative
255 * to FG_ROOT). Defaults to "preferences.xml".
258 do_preferences_load (const SGPropertyNode * arg)
261 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
262 globals->get_props());
263 } catch (const sg_exception &e) {
264 guiErrorMessage("Error reading global preferences: ", e);
267 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
275 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
276 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
277 if ( globals->get_viewmgr()->get_current() == 1 ) {
278 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
286 globals->get_current_view()->setHeadingOffset_deg(0.0);
287 globals->get_viewmgr()->next_view();
288 fix_hud_visibility();
289 globals->get_tile_mgr()->refresh_view_timestamps();
295 globals->get_current_view()->setHeadingOffset_deg(0.0);
296 globals->get_viewmgr()->prev_view();
297 fix_hud_visibility();
298 globals->get_tile_mgr()->refresh_view_timestamps();
302 * Built-in command: cycle view.
305 do_view_cycle (const SGPropertyNode * arg)
307 globals->get_current_view()->setHeadingOffset_deg(0.0);
308 globals->get_viewmgr()->next_view();
309 fix_hud_visibility();
310 globals->get_tile_mgr()->refresh_view_timestamps();
311 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
316 * Built-in command: capture screen.
319 do_screen_capture (const SGPropertyNode * arg)
327 * Reload the tile cache.
330 do_tile_cache_reload (const SGPropertyNode * arg)
332 static const SGPropertyNode *master_freeze
333 = fgGetNode("/sim/freeze/master");
334 bool freeze = master_freeze->getBoolValue();
335 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
337 fgSetBool("/sim/freeze/master", true);
340 if ( globals->get_tile_mgr()->init() ) {
341 // Load the local scenery data
342 double visibility_meters = fgGetDouble("/environment/visibility-m");
343 globals->get_tile_mgr()->update( visibility_meters );
345 SG_LOG( SG_GENERAL, SG_ALERT,
346 "Error in Tile Manager initialization!" );
351 fgSetBool("/sim/freeze/master", false);
358 * Update the lighting manually.
361 do_lighting_update (const SGPropertyNode * arg)
363 fgUpdateSkyAndLightingParams();
369 * Built-in command: toggle a bool property value.
371 * property: The name of the property to toggle.
374 do_property_toggle (const SGPropertyNode * arg)
376 SGPropertyNode * prop = get_prop(arg);
377 return prop->setBoolValue(!prop->getBoolValue());
382 * Built-in command: assign a value to a property.
384 * property: the name of the property to assign.
385 * value: the value to assign; or
386 * property[1]: the property to copy from.
389 do_property_assign (const SGPropertyNode * arg)
391 SGPropertyNode * prop = get_prop(arg);
392 const SGPropertyNode * prop2 = get_prop2(arg);
393 const SGPropertyNode * value = arg->getNode("value");
396 return prop->setUnspecifiedValue(value->getStringValue());
398 return prop->setUnspecifiedValue(prop2->getStringValue());
405 * Built-in command: increment or decrement a property value.
407 * property: the name of the property to increment or decrement.
408 * step: the amount of the increment or decrement (default: 0).
409 * min: the minimum allowed value (default: no minimum).
410 * max: the maximum allowed value (default: no maximum).
411 * mask: 'integer' to apply only to the left of the decimal point,
412 * 'decimal' to apply only to the right of the decimal point,
413 * or 'all' to apply to the whole number (the default).
414 * wrap: true if the value should be wrapped when it passes min or max;
415 * both min and max must be present for this to work (default:
419 do_property_adjust (const SGPropertyNode * arg)
421 SGPropertyNode * prop = get_prop(arg);
422 double step = arg->getDoubleValue("step");
424 double unmodifiable, modifiable;
425 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
426 &unmodifiable, &modifiable);
428 limit_value(&modifiable, arg);
430 prop->setDoubleValue(unmodifiable + modifiable);
435 * Built-in command: multiply a property value.
437 * property: the name of the property to multiply.
438 * factor: the amount by which to multiply.
439 * min: the minimum allowed value (default: no minimum).
440 * max: the maximum allowed value (default: no maximum).
441 * mask: 'integer' to apply only to the left of the decimal point,
442 * 'decimal' to apply only to the right of the decimal point,
443 * or 'all' to apply to the whole number (the default).
444 * wrap: true if the value should be wrapped when it passes min or max;
445 * both min and max must be present for this to work (default:
449 do_property_multiply (const SGPropertyNode * arg)
451 SGPropertyNode * prop = get_prop(arg);
452 double factor = arg->getDoubleValue("factor", 1);
454 double unmodifiable, modifiable;
455 split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
456 &unmodifiable, &modifiable);
457 modifiable *= factor;
458 limit_value(&modifiable, arg);
460 prop->setDoubleValue(unmodifiable + modifiable);
465 * Built-in command: swap two property values.
467 * property[0]: the name of the first property.
468 * property[1]: the name of the second property.
471 do_property_swap (const SGPropertyNode * arg)
473 SGPropertyNode * prop1 = get_prop(arg);
474 SGPropertyNode * prop2 = get_prop2(arg);
476 // FIXME: inefficient
477 const string & tmp = prop1->getStringValue();
478 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
479 prop2->setUnspecifiedValue(tmp.c_str()));
484 * Built-in command: Set a property to an axis or other moving input.
486 * property: the name of the property to set.
487 * setting: the current input setting, usually between -1.0 and 1.0.
488 * offset: the offset to shift by, before applying the factor.
489 * factor: the factor to multiply by (use negative to reverse).
492 do_property_scale (const SGPropertyNode * arg)
494 SGPropertyNode * prop = get_prop(arg);
495 double setting = arg->getDoubleValue("setting");
496 double offset = arg->getDoubleValue("offset", 0.0);
497 double factor = arg->getDoubleValue("factor", 1.0);
498 bool squared = arg->getBoolValue("squared", false);
501 setting = (setting < 0 ? -1 : 1) * setting * setting;
503 return prop->setDoubleValue((setting + offset) * factor);
508 * Built-in command: Show an XML-configured dialog.
510 * dialog-name: the name of the GUI dialog to display.
513 do_dialog_show (const SGPropertyNode * arg)
515 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
516 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
517 gui->display(arg->getStringValue("dialog-name"));
523 * Hide the active XML-configured dialog.
526 do_dialog_close (const SGPropertyNode * arg)
528 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
529 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
530 GUIWidget * widget = gui->getCurrentWidget();
533 gui->setCurrentWidget(0);
542 * Update a value in the active XML-configured dialog.
544 * object-name: The name of the GUI object(s) (all GUI objects if omitted).
547 do_dialog_update (const SGPropertyNode * arg)
549 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
550 ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
551 GUIWidget * widget = gui->getCurrentWidget();
553 if (arg->hasValue("object-name")) {
554 gui->getCurrentWidget()
555 ->updateValue(arg->getStringValue("object-name"));
557 gui->getCurrentWidget()->updateValues();
567 * Apply 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_apply (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 ->applyValue(arg->getStringValue("object-name"));
582 gui->getCurrentWidget()->applyValues();
592 * Built-in command: commit presets (read from in /sim/presets/)
595 do_presets_commit (const SGPropertyNode * arg)
597 // unbind the current fdm state so property changes
598 // don't get lost when we subsequently delete this fdm
599 // and create a new one.
600 cur_fdm_state->unbind();
602 // set position from presets
606 fgReInitSubsystems();
608 globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
610 if ( ! fgGetBool("/sim/presets/onground") ) {
611 fgSetBool( "/sim/freeze/master", true );
612 fgSetBool( "/sim/freeze/clock", true );
620 ////////////////////////////////////////////////////////////////////////
622 ////////////////////////////////////////////////////////////////////////
626 * Table of built-in commands.
628 * New commands do not have to be added here; any module in the application
629 * can add a new command using globals->get_commands()->addCommand(...).
633 SGCommandMgr::command_t command;
639 { "panel-load", do_panel_load },
640 { "panel-mouse-click", do_panel_mouse_click },
641 { "preferences-load", do_preferences_load },
642 { "view-cycle", do_view_cycle },
643 { "screen-capture", do_screen_capture },
644 { "tile-cache-reload", do_tile_cache_reload },
645 { "lighting-update", do_lighting_update },
646 { "property-toggle", do_property_toggle },
647 { "property-assign", do_property_assign },
648 { "property-adjust", do_property_adjust },
649 { "property-multiply", do_property_multiply },
650 { "property-swap", do_property_swap },
651 { "property-scale", do_property_scale },
652 { "dialog-show", do_dialog_show },
653 { "dialog-close", do_dialog_close },
654 { "dialog-show", do_dialog_show },
655 { "dialog-update", do_dialog_update },
656 { "dialog-apply", do_dialog_apply },
657 { 0, 0 } // zero-terminated
662 * Initialize the default built-in commands.
664 * Other commands may be added by other parts of the application.
669 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
670 for (int i = 0; built_ins[i].name != 0; i++) {
671 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
672 globals->get_commands()->addCommand(built_ins[i].name,
673 built_ins[i].command);
676 typedef bool (*dummy)();
677 fgTie( "/command/view/next", dummy(0), do_view_next );
678 fgTie( "/command/view/prev", dummy(0), do_view_prev );
681 // end of fg_commands.cxx