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>
16 #include <Cockpit/panel.hxx>
17 #include <Cockpit/panel_io.hxx>
18 #include <Scenery/tilemgr.hxx>
19 #include <Time/tmp.hxx>
21 #include "fg_commands.hxx"
24 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
25 SG_USING_STD(ifstream);
26 SG_USING_STD(ofstream);
29 #include "fg_props.hxx"
31 #include "globals.hxx"
32 #include "viewmgr.hxx"
36 ////////////////////////////////////////////////////////////////////////
37 // Saved command states.
38 ////////////////////////////////////////////////////////////////////////
42 * Base saved state for property commands.
44 * Since this class isn't publicly visible, it is simply an aggregate
45 * of all the stuff any property command needs.
47 class PropertyCommandState : public SGCommandState
50 PropertyCommandState (const SGPropertyNode * arg);
51 virtual SGPropertyNode * getProp () const { return _prop; }
52 virtual SGPropertyNode * getProp2 () const { return _prop2; }
53 virtual const SGPropertyNode * getValue () const
54 { return _value ? _value : &_dummy_0; }
55 virtual const SGPropertyNode * getStep () const
56 { return _step ? _step : &_dummy_0; }
57 virtual const SGPropertyNode * getMin () const { return _min; }
58 virtual const SGPropertyNode * getMax () const { return _max; }
59 virtual const SGPropertyNode * getWrap () const
60 { return _wrap ? _wrap : &_dummy_0; }
61 virtual const SGPropertyNode * getFactor () const
62 { return _factor ? _factor : &_dummy_1; }
63 virtual const SGPropertyNode * getSquared () const
64 { return _squared ? _squared : &_dummy_1; }
65 virtual const SGPropertyNode * getSetting () const
66 { return _setting ? _setting : &_dummy_0; }
67 virtual const SGPropertyNode * getOffset () const
68 { return _offset ? _offset : &_dummy_0; }
70 static SGPropertyNode _dummy_0;
71 static SGPropertyNode _dummy_1;
72 mutable SGPropertyNode * _prop;
73 mutable SGPropertyNode * _prop2;
74 const SGPropertyNode * _value;
75 const SGPropertyNode * _step;
76 const SGPropertyNode * _min;
77 const SGPropertyNode * _max;
78 const SGPropertyNode * _wrap;
79 const SGPropertyNode * _factor;
80 const SGPropertyNode * _squared;
81 const SGPropertyNode * _setting;
82 const SGPropertyNode * _offset;
85 SGPropertyNode PropertyCommandState::_dummy_0;
86 SGPropertyNode PropertyCommandState::_dummy_1;
88 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
89 : SGCommandState(arg),
90 _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
91 _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
92 _value(arg->getNode("value")),
93 _step(arg->getNode("step")),
94 _min(arg->getNode("min")),
95 _max(arg->getNode("max")),
96 _wrap(arg->getNode("wrap")),
97 _factor(arg->getNode("factor")),
98 _squared(arg->getNode("squared")),
99 _setting(arg->getNode("setting")),
100 _offset(arg->getNode("offset"))
102 // It would be better not to do this
103 // every time, but it's not that big
104 // a deal. I don't know enough about
105 // C++ static initialization to fix it.
106 _dummy_1.setDoubleValue(1.0);
112 ////////////////////////////////////////////////////////////////////////
113 // Command implementations.
114 ////////////////////////////////////////////////////////////////////////
118 * Built-in command: do nothing.
121 do_null (const SGPropertyNode * arg, SGCommandState ** state)
128 * Built-in command: exit FlightGear.
130 * TODO: show a confirm dialog.
133 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
135 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
142 * Built-in command: load flight.
144 * file (optional): the name of the file to load (relative to current
145 * directory). Defaults to "fgfs.sav".
148 do_load (const SGPropertyNode * arg, SGCommandState ** state)
150 const string &file = arg->getStringValue("file", "fgfs.sav");
151 ifstream input(file.c_str());
152 if (input.good() && fgLoadFlight(input)) {
154 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
157 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
164 * Built-in command: save flight.
166 * file (optional): the name of the file to save (relative to the
167 * current directory). Defaults to "fgfs.sav".
170 do_save (const SGPropertyNode * arg, SGCommandState ** state)
172 const string &file = arg->getStringValue("file", "fgfs.sav");
173 bool write_all = arg->getBoolValue("write-all", false);
174 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
175 ofstream output(file.c_str());
176 if (output.good() && fgSaveFlight(output, write_all)) {
178 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
181 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
188 * Built-in command: let PUI handle a mouse click.
190 * button: the mouse button number, zero-based.
191 * is-down: true if the button is down, false if it is up.
192 * x-pos: the x position of the mouse click.
193 * y-pos: the y position of the mouse click.
196 do_pui_mouse_click (const SGPropertyNode * arg, SGCommandState ** state)
198 return puMouse(arg->getIntValue("button"),
199 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
200 arg->getIntValue("x-pos"),
201 arg->getIntValue("y-pos"));
206 * Built-in command: let PUI *or* the panel handle a mouse click.
208 * button: the mouse button number, zero-based.
209 * is-down: true if the button is down, false if it is up.
210 * x-pos: the x position of the mouse click.
211 * y-pos: the y position of the mouse click.
214 do_pui_or_panel_mouse_click (const SGPropertyNode * arg,
215 SGCommandState ** state)
217 int button = arg->getIntValue("button");
218 bool is_down = arg->getBoolValue("is-down");
219 int x = arg->getIntValue("x-pos");
220 int y = arg->getIntValue("y-pos");
221 return (puMouse(button, is_down ? PU_DOWN : PU_UP, x, y) ||
222 (current_panel != 0 &&
223 current_panel->doMouseAction(button,
224 is_down ? PU_DOWN : PU_UP, x, y)));
229 * Built-in command: (re)load the panel.
231 * path (optional): the file name to load the panel from
232 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
233 * and if that's unspecified, to "Panels/Default/default.xml".
236 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
239 arg->getStringValue("path",
240 fgGetString("/sim/panel/path",
241 "Panels/Default/default.xml"));
242 FGPanel * new_panel = fgReadPanel(panel_path);
243 if (new_panel == 0) {
244 SG_LOG(SG_INPUT, SG_ALERT,
245 "Error reading new panel from " << panel_path);
248 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
249 current_panel->unbind();
250 delete current_panel;
251 current_panel = new_panel;
252 current_panel->bind();
258 * Built-in command: pass a mouse click to the panel.
260 * button: the mouse button number, zero-based.
261 * is-down: true if the button is down, false if it is up.
262 * x-pos: the x position of the mouse click.
263 * y-pos: the y position of the mouse click.
266 do_panel_mouse_click (const SGPropertyNode * arg, SGCommandState ** state)
268 if (current_panel != 0)
270 ->doMouseAction(arg->getIntValue("button"),
271 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
272 arg->getIntValue("x-pos"),
273 arg->getIntValue("y-pos"));
280 * Built-in command: (re)load preferences.
282 * path (optional): the file name to load the panel from (relative
283 * to FG_ROOT). Defaults to "preferences.xml".
286 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
288 const string &path = arg->getStringValue("path", "preferences.xml");
289 SGPath props_path(globals->get_fg_root());
290 props_path.append(path);
291 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
292 << props_path.str());
294 readProperties(props_path.str(), globals->get_props());
295 } catch (const sg_exception &e) {
296 guiErrorMessage("Error reading global preferences: ", e);
299 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
305 * Built-in command: cycle view.
308 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
310 globals->get_current_view()->setHeadingOffset_deg(0.0);
311 globals->get_viewmgr()->next_view();
312 if ( !strcmp("/sim/flight-model", "ada") ) {
313 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
314 if ( globals->get_viewmgr()->get_current() == 1 ) {
315 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
318 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
324 * Built-in command: capture screen.
327 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
335 * Reload the tile cache.
338 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
340 static const SGPropertyNode *master_freeze
341 = fgGetNode("/sim/freeze/master");
342 bool freeze = master_freeze->getBoolValue();
343 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
345 fgSetBool("/sim/freeze/master", true);
348 if ( global_tile_mgr.init() ) {
349 // Load the local scenery data
350 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
351 fgGetDouble("/position/latitude-deg"));
353 SG_LOG( SG_GENERAL, SG_ALERT,
354 "Error in Tile Manager initialization!" );
359 fgSetBool("/sim/freeze/master", false);
366 * Update the lighting manually.
369 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
371 fgUpdateSkyAndLightingParams();
377 * Built-in command: toggle a bool property value.
379 * property: The name of the property to toggle.
382 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
385 *state = new PropertyCommandState(arg);
386 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
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.
398 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
401 *state = new PropertyCommandState(arg);
402 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
403 const SGPropertyNode * value =
404 ((PropertyCommandState *)(*state))->getValue();
406 switch (prop->getType()) {
407 case SGPropertyNode::BOOL:
408 return prop->setBoolValue(value->getBoolValue());
409 case SGPropertyNode::INT:
410 return prop->setIntValue(value->getIntValue());
411 case SGPropertyNode::LONG:
412 return prop->setLongValue(value->getLongValue());
413 case SGPropertyNode::FLOAT:
414 return prop->setFloatValue(value->getFloatValue());
415 case SGPropertyNode::DOUBLE:
416 return prop->setDoubleValue(value->getDoubleValue());
417 case SGPropertyNode::STRING:
418 return prop->setStringValue(value->getStringValue());
420 return prop->setUnspecifiedValue(value->getStringValue());
426 * Built-in command: increment or decrement a property value.
428 * property: the name of the property to increment or decrement.
429 * step: the amount of the increment or decrement (default: 0).
430 * min: the minimum allowed value (default: no minimum).
431 * max: the maximum allowed value (default: no maximum).
432 * wrap: true if the value should be wrapped when it passes min or max;
433 * both min and max must be present for this to work (default:
437 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
440 *state = new PropertyCommandState(arg);
441 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
442 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
443 const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
444 const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
445 bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
447 switch (prop->getType()) {
448 case SGPropertyNode::BOOL:
449 if (step->getBoolValue())
450 return prop->setBoolValue(!prop->getBoolValue());
453 case SGPropertyNode::INT: {
454 int value = prop->getIntValue() + step->getIntValue();
455 if (min && (value < min->getIntValue())) {
457 value = max->getIntValue();
459 value = min->getIntValue();
461 if (max && value > max->getIntValue()) {
463 value = min->getIntValue();
465 value = max->getIntValue();
467 return prop->setIntValue(value);
469 case SGPropertyNode::LONG: {
470 long value = prop->getLongValue() + step->getLongValue();
471 if (min && (value < min->getLongValue())) {
473 value = max->getLongValue();
475 value = min->getLongValue();
477 if (max && value > max->getLongValue()) {
479 value = min->getLongValue();
481 value = max->getLongValue();
483 return prop->setLongValue(value);
485 case SGPropertyNode::FLOAT: {
486 float value = prop->getFloatValue() + step->getFloatValue();
487 if (min && (value < min->getFloatValue())) {
489 value = max->getFloatValue();
491 value = min->getFloatValue();
493 if (max && value > max->getFloatValue()) {
495 value = min->getFloatValue();
497 value = max->getFloatValue();
499 return prop->setFloatValue(value);
501 case SGPropertyNode::DOUBLE:
502 case SGPropertyNode::UNSPECIFIED:
503 case SGPropertyNode::NONE: {
504 double value = prop->getDoubleValue() + step->getDoubleValue();
505 if (min && (value < min->getDoubleValue())) {
507 value = max->getDoubleValue();
509 value = min->getDoubleValue();
511 if (max && value > max->getDoubleValue()) {
513 value = min->getDoubleValue();
515 value = max->getDoubleValue();
517 return prop->setDoubleValue(value);
519 case SGPropertyNode::STRING: // doesn't make sense with strings
520 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
523 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
530 * Built-in command: multiply a property value.
532 * property: the name of the property to multiply.
533 * factor: the amount by which to multiply.
536 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
539 *state = new PropertyCommandState(arg);
540 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
541 const SGPropertyNode * factor =
542 ((PropertyCommandState *)(*state))->getFactor();
544 switch (prop->getType()) {
545 case SGPropertyNode::BOOL:
546 return prop->setBoolValue(prop->getBoolValue() &&
547 factor->getBoolValue());
548 case SGPropertyNode::INT:
549 return prop->setIntValue(int(prop->getIntValue()
550 * factor->getDoubleValue()));
551 case SGPropertyNode::LONG:
552 return prop->setLongValue(long(prop->getLongValue()
553 * factor->getDoubleValue()));
554 case SGPropertyNode::FLOAT:
555 return prop->setFloatValue(float(prop->getFloatValue()
556 * factor->getDoubleValue()));
557 case SGPropertyNode::DOUBLE:
558 case SGPropertyNode::UNSPECIFIED:
559 case SGPropertyNode::NONE:
560 return prop->setDoubleValue(prop->getDoubleValue()
561 * factor->getDoubleValue());
562 default: // doesn't make sense with strings
569 * Built-in command: swap two property values.
571 * property[0]: the name of the first property.
572 * property[1]: the name of the second property.
575 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
578 *state = new PropertyCommandState(arg);
579 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
580 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
582 // FIXME: inefficient
583 const string & tmp = prop1->getStringValue();
584 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
585 prop2->setUnspecifiedValue(tmp.c_str()));
590 * Set a property to an axis or other moving input.
592 * property: the name of the property to set.
593 * setting: the current input setting, usually between -1.0 and 1.0.
594 * offset: the offset to shift by, before applying the factor.
595 * factor: the factor to multiply by (use negative to reverse).
598 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
601 *state = new PropertyCommandState(arg);
602 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
604 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
606 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
608 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
610 ((PropertyCommandState *)(*state))->getSquared()->getBoolValue();
613 setting = (setting < 0 ? -1 : 1) * setting * setting;
614 double result = (setting + offset) * factor;
616 return prop->setDoubleValue(result);
621 ////////////////////////////////////////////////////////////////////////
623 ////////////////////////////////////////////////////////////////////////
627 * Table of built-in commands.
629 * New commands do not have to be added here; any module in the application
630 * can add a new command using globals->get_commands()->addCommand(...).
634 SGCommandMgr::command_t command;
640 { "pui-mouse-click", do_pui_mouse_click },
641 { "pui-or-panel-mouse-click", do_pui_or_panel_mouse_click },
642 { "panel-load", do_panel_load },
643 { "panel-mouse-click", do_panel_mouse_click },
644 { "preferences-load", do_preferences_load },
645 { "view-cycle", do_view_cycle },
646 { "screen-capture", do_screen_capture },
647 { "tile-cache-reload", do_tile_cache_reload },
648 { "lighting-update", do_lighting_update },
649 { "property-toggle", do_property_toggle },
650 { "property-assign", do_property_assign },
651 { "property-adjust", do_property_adjust },
652 { "property-multiply", do_property_multiply },
653 { "property-swap", do_property_swap },
654 { "property-scale", do_property_scale },
655 { 0, 0 } // zero-terminated
660 * Initialize the default built-in commands.
662 * Other commands may be added by other parts of the application.
667 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
668 for (int i = 0; built_ins[i].name != 0; i++) {
669 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
670 globals->get_commands()->addCommand(built_ins[i].name,
671 built_ins[i].command);
675 // end of fg_commands.hxx