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: (re)load the panel.
190 * path (optional): the file name to load the panel from
191 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
192 * and if that's unspecified, to "Panels/Default/default.xml".
195 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
198 arg->getStringValue("path",
199 fgGetString("/sim/panel/path",
200 "Panels/Default/default.xml"));
201 FGPanel * new_panel = fgReadPanel(panel_path);
202 if (new_panel == 0) {
203 SG_LOG(SG_INPUT, SG_ALERT,
204 "Error reading new panel from " << panel_path);
207 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
208 current_panel->unbind();
209 delete current_panel;
210 current_panel = new_panel;
211 current_panel->bind();
217 * Built-in command: (re)load preferences.
219 * path (optional): the file name to load the panel from (relative
220 * to FG_ROOT). Defaults to "preferences.xml".
223 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
225 const string &path = arg->getStringValue("path", "preferences.xml");
226 SGPath props_path(globals->get_fg_root());
227 props_path.append(path);
228 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
229 << props_path.str());
231 readProperties(props_path.str(), globals->get_props());
232 } catch (const sg_exception &e) {
233 guiErrorMessage("Error reading global preferences: ", e);
236 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
242 * Built-in command: cycle view.
245 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
247 globals->get_current_view()->setHeadingOffset_deg(0.0);
248 globals->get_viewmgr()->next_view();
249 if ( !strcmp("/sim/flight-model", "ada") ) {
250 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
251 if ( globals->get_viewmgr()->get_current() == 1 ) {
252 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
255 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
261 * Built-in command: capture screen.
264 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
272 * Reload the tile cache.
275 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
277 static const SGPropertyNode *master_freeze
278 = fgGetNode("/sim/freeze/master");
279 bool freeze = master_freeze->getBoolValue();
280 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
282 fgSetBool("/sim/freeze/master", true);
285 if ( global_tile_mgr.init() ) {
286 // Load the local scenery data
287 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
288 fgGetDouble("/position/latitude-deg"));
290 SG_LOG( SG_GENERAL, SG_ALERT,
291 "Error in Tile Manager initialization!" );
296 fgSetBool("/sim/freeze/master", false);
303 * Update the lighting manually.
306 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
308 fgUpdateSkyAndLightingParams();
314 * Built-in command: toggle a bool property value.
316 * property: The name of the property to toggle.
319 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
322 *state = new PropertyCommandState(arg);
323 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
324 return prop->setBoolValue(!prop->getBoolValue());
329 * Built-in command: assign a value to a property.
331 * property: the name of the property to assign.
332 * value: the value to assign.
335 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
338 *state = new PropertyCommandState(arg);
339 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
340 const SGPropertyNode * value =
341 ((PropertyCommandState *)(*state))->getValue();
343 switch (prop->getType()) {
344 case SGPropertyNode::BOOL:
345 return prop->setBoolValue(value->getBoolValue());
346 case SGPropertyNode::INT:
347 return prop->setIntValue(value->getIntValue());
348 case SGPropertyNode::LONG:
349 return prop->setLongValue(value->getLongValue());
350 case SGPropertyNode::FLOAT:
351 return prop->setFloatValue(value->getFloatValue());
352 case SGPropertyNode::DOUBLE:
353 return prop->setDoubleValue(value->getDoubleValue());
354 case SGPropertyNode::STRING:
355 return prop->setStringValue(value->getStringValue());
357 return prop->setUnspecifiedValue(value->getStringValue());
363 * Built-in command: increment or decrement a property value.
365 * property: the name of the property to increment or decrement.
366 * step: the amount of the increment or decrement (default: 0).
367 * min: the minimum allowed value (default: no minimum).
368 * max: the maximum allowed value (default: no maximum).
369 * wrap: true if the value should be wrapped when it passes min or max;
370 * both min and max must be present for this to work (default:
374 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
377 *state = new PropertyCommandState(arg);
378 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
379 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
380 const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
381 const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
382 bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
384 switch (prop->getType()) {
385 case SGPropertyNode::BOOL:
386 if (step->getBoolValue())
387 return prop->setBoolValue(!prop->getBoolValue());
390 case SGPropertyNode::INT: {
391 int value = prop->getIntValue() + step->getIntValue();
392 if (min && (value < min->getIntValue())) {
394 value = max->getIntValue();
396 value = min->getIntValue();
398 if (max && value > max->getIntValue()) {
400 value = min->getIntValue();
402 value = max->getIntValue();
404 return prop->setIntValue(value);
406 case SGPropertyNode::LONG: {
407 long value = prop->getLongValue() + step->getLongValue();
408 if (min && (value < min->getLongValue())) {
410 value = max->getLongValue();
412 value = min->getLongValue();
414 if (max && value > max->getLongValue()) {
416 value = min->getLongValue();
418 value = max->getLongValue();
420 return prop->setLongValue(value);
422 case SGPropertyNode::FLOAT: {
423 float value = prop->getFloatValue() + step->getFloatValue();
424 if (min && (value < min->getFloatValue())) {
426 value = max->getFloatValue();
428 value = min->getFloatValue();
430 if (max && value > max->getFloatValue()) {
432 value = min->getFloatValue();
434 value = max->getFloatValue();
436 return prop->setFloatValue(value);
438 case SGPropertyNode::DOUBLE:
439 case SGPropertyNode::UNSPECIFIED:
440 case SGPropertyNode::NONE: {
441 double value = prop->getDoubleValue() + step->getDoubleValue();
442 if (min && (value < min->getDoubleValue())) {
444 value = max->getDoubleValue();
446 value = min->getDoubleValue();
448 if (max && value > max->getDoubleValue()) {
450 value = min->getDoubleValue();
452 value = max->getDoubleValue();
454 return prop->setDoubleValue(value);
456 case SGPropertyNode::STRING: // doesn't make sense with strings
457 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
460 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
467 * Built-in command: multiply a property value.
469 * property: the name of the property to multiply.
470 * factor: the amount by which to multiply.
473 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
476 *state = new PropertyCommandState(arg);
477 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
478 const SGPropertyNode * factor =
479 ((PropertyCommandState *)(*state))->getFactor();
481 switch (prop->getType()) {
482 case SGPropertyNode::BOOL:
483 return prop->setBoolValue(prop->getBoolValue() &&
484 factor->getBoolValue());
485 case SGPropertyNode::INT:
486 return prop->setIntValue(int(prop->getIntValue()
487 * factor->getDoubleValue()));
488 case SGPropertyNode::LONG:
489 return prop->setLongValue(long(prop->getLongValue()
490 * factor->getDoubleValue()));
491 case SGPropertyNode::FLOAT:
492 return prop->setFloatValue(float(prop->getFloatValue()
493 * factor->getDoubleValue()));
494 case SGPropertyNode::DOUBLE:
495 case SGPropertyNode::UNSPECIFIED:
496 case SGPropertyNode::NONE:
497 return prop->setDoubleValue(prop->getDoubleValue()
498 * factor->getDoubleValue());
499 default: // doesn't make sense with strings
506 * Built-in command: swap two property values.
508 * property[0]: the name of the first property.
509 * property[1]: the name of the second property.
512 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
515 *state = new PropertyCommandState(arg);
516 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
517 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
519 // FIXME: inefficient
520 const string & tmp = prop1->getStringValue();
521 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
522 prop2->setUnspecifiedValue(tmp.c_str()));
527 * Set a property to an axis or other moving input.
529 * property: the name of the property to set.
530 * setting: the current input setting, usually between -1.0 and 1.0.
531 * offset: the offset to shift by, before applying the factor.
532 * factor: the factor to multiply by (use negative to reverse).
535 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
538 *state = new PropertyCommandState(arg);
539 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
541 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
543 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
545 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
547 ((PropertyCommandState *)(*state))->getSquared()->getBoolValue();
550 setting = (setting < 0 ? -1 : 1) * setting * setting;
551 double result = (setting + offset) * factor;
553 return prop->setDoubleValue(result);
558 ////////////////////////////////////////////////////////////////////////
560 ////////////////////////////////////////////////////////////////////////
564 * Table of built-in commands.
566 * New commands do not have to be added here; any module in the application
567 * can add a new command using globals->get_commands()->addCommand(...).
571 SGCommandMgr::command_t command;
577 { "panel-load", do_panel_load },
578 { "preferences-load", do_preferences_load },
579 { "view-cycle", do_view_cycle },
580 { "screen-capture", do_screen_capture },
581 { "tile-cache-reload", do_tile_cache_reload },
582 { "lighting-update", do_lighting_update },
583 { "property-toggle", do_property_toggle },
584 { "property-assign", do_property_assign },
585 { "property-adjust", do_property_adjust },
586 { "property-multiply", do_property_multiply },
587 { "property-swap", do_property_swap },
588 { "property-scale", do_property_scale },
589 { 0, 0 } // zero-terminated
594 * Initialize the default built-in commands.
596 * Other commands may be added by other parts of the application.
601 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
602 for (int i = 0; built_ins[i].name != 0; i++) {
603 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
604 globals->get_commands()->addCommand(built_ins[i].name,
605 built_ins[i].command);
609 // end of fg_commands.hxx