1 // fg_commands.cxx - internal FGFS commands.
3 #include <simgear/compiler.h>
4 #include <simgear/misc/exception.hxx>
9 #include <simgear/debug/logstream.hxx>
10 #include <simgear/misc/commands.hxx>
11 #include <simgear/misc/props.hxx>
14 #include <Cockpit/panel.hxx>
15 #include <Cockpit/panel_io.hxx>
16 #include <Scenery/tilemgr.hxx>
17 #include <Time/tmp.hxx>
19 #include "fg_commands.hxx"
22 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
23 SG_USING_STD(ifstream);
24 SG_USING_STD(ofstream);
27 #include "fg_props.hxx"
29 #include "globals.hxx"
30 #include "viewmgr.hxx"
34 ////////////////////////////////////////////////////////////////////////
35 // Saved command states.
36 ////////////////////////////////////////////////////////////////////////
40 * Base saved state for property commands.
42 * Since this class isn't publicly visible, it is simply an aggregate
43 * of all the stuff any property command needs.
45 class PropertyCommandState : public SGCommandState
48 PropertyCommandState (const SGPropertyNode * arg);
49 virtual SGPropertyNode * getProp () const { return _prop; }
50 virtual SGPropertyNode * getProp2 () const { return _prop2; }
51 virtual const SGPropertyNode * getValue () const
52 { return _value ? _value : &_dummy_0; }
53 virtual const SGPropertyNode * getStep () const
54 { return _step ? _step : &_dummy_0; }
55 virtual const SGPropertyNode * getMin () const { return _min; }
56 virtual const SGPropertyNode * getMax () const { return _max; }
57 virtual const SGPropertyNode * getWrap () const
58 { return _wrap ? _wrap : &_dummy_0; }
59 virtual const SGPropertyNode * getFactor () const
60 { return _factor ? _factor : &_dummy_1; }
61 virtual const SGPropertyNode * getSquared () const
62 { return _squared ? _squared : &_dummy_1; }
63 virtual const SGPropertyNode * getSetting () const
64 { return _setting ? _setting : &_dummy_0; }
65 virtual const SGPropertyNode * getOffset () const
66 { return _offset ? _offset : &_dummy_0; }
68 static SGPropertyNode _dummy_0;
69 static SGPropertyNode _dummy_1;
70 mutable SGPropertyNode * _prop;
71 mutable SGPropertyNode * _prop2;
72 const SGPropertyNode * _value;
73 const SGPropertyNode * _step;
74 const SGPropertyNode * _min;
75 const SGPropertyNode * _max;
76 const SGPropertyNode * _wrap;
77 const SGPropertyNode * _factor;
78 const SGPropertyNode * _squared;
79 const SGPropertyNode * _setting;
80 const SGPropertyNode * _offset;
83 SGPropertyNode PropertyCommandState::_dummy_0;
84 SGPropertyNode PropertyCommandState::_dummy_1;
86 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
87 : SGCommandState(arg),
88 _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
89 _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
90 _value(arg->getNode("value")),
91 _step(arg->getNode("step")),
92 _min(arg->getNode("min")),
93 _max(arg->getNode("max")),
94 _wrap(arg->getNode("wrap")),
95 _factor(arg->getNode("factor")),
96 _squared(arg->getNode("squared")),
97 _setting(arg->getNode("setting")),
98 _offset(arg->getNode("offset"))
100 // It would be better not to do this
101 // every time, but it's not that big
102 // a deal. I don't know enough about
103 // C++ static initialization to fix it.
104 _dummy_1.setDoubleValue(1.0);
110 ////////////////////////////////////////////////////////////////////////
111 // Command implementations.
112 ////////////////////////////////////////////////////////////////////////
116 * Built-in command: do nothing.
119 do_null (const SGPropertyNode * arg, SGCommandState ** state)
126 * Built-in command: exit FlightGear.
128 * TODO: show a confirm dialog.
131 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
133 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
140 * Built-in command: load flight.
142 * file (optional): the name of the file to load (relative to current
143 * directory). Defaults to "fgfs.sav".
146 do_load (const SGPropertyNode * arg, SGCommandState ** state)
148 const string &file = arg->getStringValue("file", "fgfs.sav");
149 ifstream input(file.c_str());
150 if (input.good() && fgLoadFlight(input)) {
152 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
155 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
162 * Built-in command: save flight.
164 * file (optional): the name of the file to save (relative to the
165 * current directory). Defaults to "fgfs.sav".
168 do_save (const SGPropertyNode * arg, SGCommandState ** state)
170 const string &file = arg->getStringValue("file", "fgfs.sav");
171 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
172 ofstream output(file.c_str());
173 if (output.good() && fgSaveFlight(output)) {
175 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
178 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
185 * Built-in command: (re)load the panel.
187 * path (optional): the file name to load the panel from
188 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
189 * and if that's unspecified, to "Panels/Default/default.xml".
192 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
195 arg->getStringValue("path",
196 fgGetString("/sim/panel/path",
197 "Panels/Default/default.xml"));
198 FGPanel * new_panel = fgReadPanel(panel_path);
199 if (new_panel == 0) {
200 SG_LOG(SG_INPUT, SG_ALERT,
201 "Error reading new panel from " << panel_path);
204 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
205 current_panel->unbind();
206 delete current_panel;
207 current_panel = new_panel;
208 current_panel->bind();
214 * Built-in command: (re)load preferences.
216 * path (optional): the file name to load the panel from (relative
217 * to FG_ROOT). Defaults to "preferences.xml".
220 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
222 const string &path = arg->getStringValue("path", "preferences.xml");
223 SGPath props_path(globals->get_fg_root());
224 props_path.append(path);
225 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
226 << props_path.str());
228 readProperties(props_path.str(), globals->get_props());
229 } catch (const sg_exception &e) {
230 guiErrorMessage("Error reading global preferences: ", e);
233 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
239 * Built-in command: cycle view.
242 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
244 globals->get_current_view()->set_view_offset(0.0);
245 globals->set_current_view(globals->get_viewmgr()->next_view());
246 if ( fgGetString("/sim/flight-model") == "ada" ) {
247 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
248 if ( globals->get_viewmgr()->get_current() == 1 ) {
249 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
252 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
258 * Built-in command: capture screen.
261 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
269 * Reload the tile cache.
272 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
274 static const SGPropertyNode *master_freeze
275 = fgGetNode("/sim/freeze/master");
276 bool freeze = master_freeze->getBoolValue();
277 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
279 fgSetBool("/sim/freeze/master", true);
282 if ( global_tile_mgr.init() ) {
283 // Load the local scenery data
284 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
285 fgGetDouble("/position/latitude-deg"));
287 SG_LOG( SG_GENERAL, SG_ALERT,
288 "Error in Tile Manager initialization!" );
293 fgSetBool("/sim/freeze/master", false);
300 * Update the lighting manually.
303 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
305 fgUpdateSkyAndLightingParams();
311 * Built-in command: toggle a bool property value.
313 * property: The name of the property to toggle.
316 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
319 *state = new PropertyCommandState(arg);
320 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
321 return prop->setBoolValue(!prop->getBoolValue());
326 * Built-in command: assign a value to a property.
328 * property: the name of the property to assign.
329 * value: the value to assign.
332 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
335 *state = new PropertyCommandState(arg);
336 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
337 const SGPropertyNode * value =
338 ((PropertyCommandState *)(*state))->getValue();
340 switch (prop->getType()) {
341 case SGPropertyNode::BOOL:
342 return prop->setBoolValue(value->getBoolValue());
343 case SGPropertyNode::INT:
344 return prop->setIntValue(value->getIntValue());
345 case SGPropertyNode::LONG:
346 return prop->setLongValue(value->getLongValue());
347 case SGPropertyNode::FLOAT:
348 return prop->setFloatValue(value->getFloatValue());
349 case SGPropertyNode::DOUBLE:
350 return prop->setDoubleValue(value->getDoubleValue());
351 case SGPropertyNode::STRING:
352 return prop->setStringValue(value->getStringValue());
354 return prop->setUnspecifiedValue(value->getStringValue());
360 * Built-in command: increment or decrement a property value.
362 * property: the name of the property to increment or decrement.
363 * step: the amount of the increment or decrement (default: 0).
364 * min: the minimum allowed value (default: no minimum).
365 * max: the maximum allowed value (default: no maximum).
366 * wrap: true if the value should be wrapped when it passes min or max;
367 * both min and max must be present for this to work (default:
371 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
374 *state = new PropertyCommandState(arg);
375 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
376 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
377 const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
378 const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
379 bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
381 switch (prop->getType()) {
382 case SGPropertyNode::BOOL:
383 if (step->getBoolValue())
384 return prop->setBoolValue(!prop->getBoolValue());
387 case SGPropertyNode::INT: {
388 int value = prop->getIntValue() + step->getIntValue();
389 if (min && (value < min->getIntValue())) {
391 value = max->getIntValue();
393 value = min->getIntValue();
395 if (max && value > max->getIntValue()) {
397 value = min->getIntValue();
399 value = max->getIntValue();
401 return prop->setIntValue(value);
403 case SGPropertyNode::LONG: {
404 long value = prop->getLongValue() + step->getLongValue();
405 if (min && (value < min->getLongValue())) {
407 value = max->getLongValue();
409 value = min->getLongValue();
411 if (max && value > max->getLongValue()) {
413 value = min->getLongValue();
415 value = max->getLongValue();
417 return prop->setLongValue(value);
419 case SGPropertyNode::FLOAT: {
420 float value = prop->getFloatValue() + step->getFloatValue();
421 if (min && (value < min->getFloatValue())) {
423 value = max->getFloatValue();
425 value = min->getFloatValue();
427 if (max && value > max->getFloatValue()) {
429 value = min->getFloatValue();
431 value = max->getFloatValue();
433 return prop->setFloatValue(value);
435 case SGPropertyNode::DOUBLE:
436 case SGPropertyNode::UNSPECIFIED:
437 case SGPropertyNode::NONE: {
438 double value = prop->getDoubleValue() + step->getDoubleValue();
439 if (min && (value < min->getDoubleValue())) {
441 value = max->getDoubleValue();
443 value = min->getDoubleValue();
445 if (max && value > max->getDoubleValue()) {
447 value = min->getDoubleValue();
449 value = max->getDoubleValue();
451 return prop->setDoubleValue(value);
453 case SGPropertyNode::STRING: // doesn't make sense with strings
454 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
457 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
464 * Built-in command: multiply a property value.
466 * property: the name of the property to multiply.
467 * factor: the amount by which to multiply.
470 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
473 *state = new PropertyCommandState(arg);
474 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
475 const SGPropertyNode * factor =
476 ((PropertyCommandState *)(*state))->getFactor();
478 switch (prop->getType()) {
479 case SGPropertyNode::BOOL:
480 return prop->setBoolValue(prop->getBoolValue() &&
481 factor->getBoolValue());
482 case SGPropertyNode::INT:
483 return prop->setIntValue(int(prop->getIntValue()
484 * factor->getDoubleValue()));
485 case SGPropertyNode::LONG:
486 return prop->setLongValue(long(prop->getLongValue()
487 * factor->getDoubleValue()));
488 case SGPropertyNode::FLOAT:
489 return prop->setFloatValue(float(prop->getFloatValue()
490 * factor->getDoubleValue()));
491 case SGPropertyNode::DOUBLE:
492 case SGPropertyNode::UNSPECIFIED:
493 case SGPropertyNode::NONE:
494 return prop->setDoubleValue(prop->getDoubleValue()
495 * factor->getDoubleValue());
496 default: // doesn't make sense with strings
503 * Built-in command: swap two property values.
505 * property[0]: the name of the first property.
506 * property[1]: the name of the second property.
509 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
512 *state = new PropertyCommandState(arg);
513 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
514 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
516 // FIXME: inefficient
517 const string & tmp = prop1->getStringValue();
518 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
519 prop2->setUnspecifiedValue(tmp));
524 * Set a property to an axis or other moving input.
526 * property: the name of the property to set.
527 * setting: the current input setting, usually between -1.0 and 1.0.
528 * offset: the offset to shift by, before applying the factor.
529 * factor: the factor to multiply by (use negative to reverse).
532 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
535 *state = new PropertyCommandState(arg);
536 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
538 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
540 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
542 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
544 ((PropertyCommandState *)(*state))->getSquared()->getBoolValue();
546 double result = (setting + offset) * factor;
548 result = (result < 0 ? -1 : 1) * result * result;
550 return prop->setDoubleValue(result);
555 ////////////////////////////////////////////////////////////////////////
557 ////////////////////////////////////////////////////////////////////////
561 * Table of built-in commands.
563 * New commands do not have to be added here; any module in the application
564 * can add a new command using globals->get_commands()->addCommand(...).
568 SGCommandMgr::command_t command;
574 { "panel-load", do_panel_load },
575 { "preferences-load", do_preferences_load },
576 { "view-cycle", do_view_cycle },
577 { "screen-capture", do_screen_capture },
578 { "tile-cache-reload", do_tile_cache_reload },
579 { "lighting-update", do_lighting_update },
580 { "property-toggle", do_property_toggle },
581 { "property-assign", do_property_assign },
582 { "property-adjust", do_property_adjust },
583 { "property-multiply", do_property_multiply },
584 { "property-swap", do_property_swap },
585 { "property-scale", do_property_scale },
586 { 0, 0 } // zero-terminated
591 * Initialize the default built-in commands.
593 * Other commands may be added by other parts of the application.
598 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
599 for (int i = 0; built_ins[i].name != 0; i++) {
600 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
601 globals->get_commands()->addCommand(built_ins[i].name,
602 built_ins[i].command);
606 // end of fg_commands.hxx