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 bool write_all = arg->getBoolValue("write-all", false);
172 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
173 ofstream output(file.c_str());
174 if (output.good() && fgSaveFlight(output, write_all)) {
176 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
179 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
186 * Built-in command: (re)load the panel.
188 * path (optional): the file name to load the panel from
189 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
190 * and if that's unspecified, to "Panels/Default/default.xml".
193 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
196 arg->getStringValue("path",
197 fgGetString("/sim/panel/path",
198 "Panels/Default/default.xml"));
199 FGPanel * new_panel = fgReadPanel(panel_path);
200 if (new_panel == 0) {
201 SG_LOG(SG_INPUT, SG_ALERT,
202 "Error reading new panel from " << panel_path);
205 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
206 current_panel->unbind();
207 delete current_panel;
208 current_panel = new_panel;
209 current_panel->bind();
215 * Built-in command: (re)load preferences.
217 * path (optional): the file name to load the panel from (relative
218 * to FG_ROOT). Defaults to "preferences.xml".
221 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
223 const string &path = arg->getStringValue("path", "preferences.xml");
224 SGPath props_path(globals->get_fg_root());
225 props_path.append(path);
226 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
227 << props_path.str());
229 readProperties(props_path.str(), globals->get_props());
230 } catch (const sg_exception &e) {
231 guiErrorMessage("Error reading global preferences: ", e);
234 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
240 * Built-in command: cycle view.
243 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
245 globals->get_current_view()->set_view_offset(0.0);
246 globals->set_current_view(globals->get_viewmgr()->next_view());
247 if ( fgGetString("/sim/flight-model") == "ada" ) {
248 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
249 if ( globals->get_viewmgr()->get_current() == 1 ) {
250 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
253 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
259 * Built-in command: capture screen.
262 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
270 * Reload the tile cache.
273 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
275 static const SGPropertyNode *master_freeze
276 = fgGetNode("/sim/freeze/master");
277 bool freeze = master_freeze->getBoolValue();
278 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
280 fgSetBool("/sim/freeze/master", true);
283 if ( global_tile_mgr.init() ) {
284 // Load the local scenery data
285 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
286 fgGetDouble("/position/latitude-deg"));
288 SG_LOG( SG_GENERAL, SG_ALERT,
289 "Error in Tile Manager initialization!" );
294 fgSetBool("/sim/freeze/master", false);
301 * Update the lighting manually.
304 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
306 fgUpdateSkyAndLightingParams();
312 * Built-in command: toggle a bool property value.
314 * property: The name of the property to toggle.
317 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
320 *state = new PropertyCommandState(arg);
321 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
322 return prop->setBoolValue(!prop->getBoolValue());
327 * Built-in command: assign a value to a property.
329 * property: the name of the property to assign.
330 * value: the value to assign.
333 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
336 *state = new PropertyCommandState(arg);
337 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
338 const SGPropertyNode * value =
339 ((PropertyCommandState *)(*state))->getValue();
341 switch (prop->getType()) {
342 case SGPropertyNode::BOOL:
343 return prop->setBoolValue(value->getBoolValue());
344 case SGPropertyNode::INT:
345 return prop->setIntValue(value->getIntValue());
346 case SGPropertyNode::LONG:
347 return prop->setLongValue(value->getLongValue());
348 case SGPropertyNode::FLOAT:
349 return prop->setFloatValue(value->getFloatValue());
350 case SGPropertyNode::DOUBLE:
351 return prop->setDoubleValue(value->getDoubleValue());
352 case SGPropertyNode::STRING:
353 return prop->setStringValue(value->getStringValue());
355 return prop->setUnspecifiedValue(value->getStringValue());
361 * Built-in command: increment or decrement a property value.
363 * property: the name of the property to increment or decrement.
364 * step: the amount of the increment or decrement (default: 0).
365 * min: the minimum allowed value (default: no minimum).
366 * max: the maximum allowed value (default: no maximum).
367 * wrap: true if the value should be wrapped when it passes min or max;
368 * both min and max must be present for this to work (default:
372 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
375 *state = new PropertyCommandState(arg);
376 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
377 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
378 const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
379 const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
380 bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
382 switch (prop->getType()) {
383 case SGPropertyNode::BOOL:
384 if (step->getBoolValue())
385 return prop->setBoolValue(!prop->getBoolValue());
388 case SGPropertyNode::INT: {
389 int value = prop->getIntValue() + step->getIntValue();
390 if (min && (value < min->getIntValue())) {
392 value = max->getIntValue();
394 value = min->getIntValue();
396 if (max && value > max->getIntValue()) {
398 value = min->getIntValue();
400 value = max->getIntValue();
402 return prop->setIntValue(value);
404 case SGPropertyNode::LONG: {
405 long value = prop->getLongValue() + step->getLongValue();
406 if (min && (value < min->getLongValue())) {
408 value = max->getLongValue();
410 value = min->getLongValue();
412 if (max && value > max->getLongValue()) {
414 value = min->getLongValue();
416 value = max->getLongValue();
418 return prop->setLongValue(value);
420 case SGPropertyNode::FLOAT: {
421 float value = prop->getFloatValue() + step->getFloatValue();
422 if (min && (value < min->getFloatValue())) {
424 value = max->getFloatValue();
426 value = min->getFloatValue();
428 if (max && value > max->getFloatValue()) {
430 value = min->getFloatValue();
432 value = max->getFloatValue();
434 return prop->setFloatValue(value);
436 case SGPropertyNode::DOUBLE:
437 case SGPropertyNode::UNSPECIFIED:
438 case SGPropertyNode::NONE: {
439 double value = prop->getDoubleValue() + step->getDoubleValue();
440 if (min && (value < min->getDoubleValue())) {
442 value = max->getDoubleValue();
444 value = min->getDoubleValue();
446 if (max && value > max->getDoubleValue()) {
448 value = min->getDoubleValue();
450 value = max->getDoubleValue();
452 return prop->setDoubleValue(value);
454 case SGPropertyNode::STRING: // doesn't make sense with strings
455 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
458 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
465 * Built-in command: multiply a property value.
467 * property: the name of the property to multiply.
468 * factor: the amount by which to multiply.
471 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
474 *state = new PropertyCommandState(arg);
475 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
476 const SGPropertyNode * factor =
477 ((PropertyCommandState *)(*state))->getFactor();
479 switch (prop->getType()) {
480 case SGPropertyNode::BOOL:
481 return prop->setBoolValue(prop->getBoolValue() &&
482 factor->getBoolValue());
483 case SGPropertyNode::INT:
484 return prop->setIntValue(int(prop->getIntValue()
485 * factor->getDoubleValue()));
486 case SGPropertyNode::LONG:
487 return prop->setLongValue(long(prop->getLongValue()
488 * factor->getDoubleValue()));
489 case SGPropertyNode::FLOAT:
490 return prop->setFloatValue(float(prop->getFloatValue()
491 * factor->getDoubleValue()));
492 case SGPropertyNode::DOUBLE:
493 case SGPropertyNode::UNSPECIFIED:
494 case SGPropertyNode::NONE:
495 return prop->setDoubleValue(prop->getDoubleValue()
496 * factor->getDoubleValue());
497 default: // doesn't make sense with strings
504 * Built-in command: swap two property values.
506 * property[0]: the name of the first property.
507 * property[1]: the name of the second property.
510 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
513 *state = new PropertyCommandState(arg);
514 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
515 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
517 // FIXME: inefficient
518 const string & tmp = prop1->getStringValue();
519 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
520 prop2->setUnspecifiedValue(tmp));
525 * Set a property to an axis or other moving input.
527 * property: the name of the property to set.
528 * setting: the current input setting, usually between -1.0 and 1.0.
529 * offset: the offset to shift by, before applying the factor.
530 * factor: the factor to multiply by (use negative to reverse).
533 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
536 *state = new PropertyCommandState(arg);
537 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
539 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
541 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
543 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
545 ((PropertyCommandState *)(*state))->getSquared()->getBoolValue();
548 setting = (setting < 0 ? -1 : 1) * setting * setting;
549 double result = (setting + offset) * factor;
551 return prop->setDoubleValue(result);
556 ////////////////////////////////////////////////////////////////////////
558 ////////////////////////////////////////////////////////////////////////
562 * Table of built-in commands.
564 * New commands do not have to be added here; any module in the application
565 * can add a new command using globals->get_commands()->addCommand(...).
569 SGCommandMgr::command_t command;
575 { "panel-load", do_panel_load },
576 { "preferences-load", do_preferences_load },
577 { "view-cycle", do_view_cycle },
578 { "screen-capture", do_screen_capture },
579 { "tile-cache-reload", do_tile_cache_reload },
580 { "lighting-update", do_lighting_update },
581 { "property-toggle", do_property_toggle },
582 { "property-assign", do_property_assign },
583 { "property-adjust", do_property_adjust },
584 { "property-multiply", do_property_multiply },
585 { "property-swap", do_property_swap },
586 { "property-scale", do_property_scale },
587 { 0, 0 } // zero-terminated
592 * Initialize the default built-in commands.
594 * Other commands may be added by other parts of the application.
599 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
600 for (int i = 0; built_ins[i].name != 0; i++) {
601 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
602 globals->get_commands()->addCommand(built_ins[i].name,
603 built_ins[i].command);
607 // end of fg_commands.hxx