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 * getSetting () const
62 { return _setting ? _setting : &_dummy_0; }
63 virtual const SGPropertyNode * getOffset () const
64 { return _offset ? _offset : &_dummy_0; }
66 static SGPropertyNode _dummy_0;
67 static SGPropertyNode _dummy_1;
68 mutable SGPropertyNode * _prop;
69 mutable SGPropertyNode * _prop2;
70 const SGPropertyNode * _value;
71 const SGPropertyNode * _step;
72 const SGPropertyNode * _min;
73 const SGPropertyNode * _max;
74 const SGPropertyNode * _wrap;
75 const SGPropertyNode * _factor;
76 const SGPropertyNode * _setting;
77 const SGPropertyNode * _offset;
80 SGPropertyNode PropertyCommandState::_dummy_0;
81 SGPropertyNode PropertyCommandState::_dummy_1;
83 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
84 : SGCommandState(arg),
85 _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
86 _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
87 _value(arg->getNode("value")),
88 _step(arg->getNode("step")),
89 _min(arg->getNode("min")),
90 _max(arg->getNode("max")),
91 _wrap(arg->getNode("wrap")),
92 _factor(arg->getNode("factor")),
93 _setting(arg->getNode("setting")),
94 _offset(arg->getNode("offset"))
96 // It would be better not to do this
97 // every time, but it's not that big
98 // a deal. I don't know enough about
99 // C++ static initialization to fix it.
100 _dummy_1.setDoubleValue(1.0);
106 ////////////////////////////////////////////////////////////////////////
107 // Command implementations.
108 ////////////////////////////////////////////////////////////////////////
112 * Built-in command: do nothing.
115 do_null (const SGPropertyNode * arg, SGCommandState ** state)
122 * Built-in command: exit FlightGear.
124 * TODO: show a confirm dialog.
127 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
129 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
136 * Built-in command: load flight.
138 * file (optional): the name of the file to load (relative to current
139 * directory). Defaults to "fgfs.sav".
142 do_load (const SGPropertyNode * arg, SGCommandState ** state)
144 const string &file = arg->getStringValue("file", "fgfs.sav");
145 ifstream input(file.c_str());
146 if (input.good() && fgLoadFlight(input)) {
148 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
151 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
158 * Built-in command: save flight.
160 * file (optional): the name of the file to save (relative to the
161 * current directory). Defaults to "fgfs.sav".
164 do_save (const SGPropertyNode * arg, SGCommandState ** state)
166 const string &file = arg->getStringValue("file", "fgfs.sav");
167 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
168 ofstream output(file.c_str());
169 if (output.good() && fgSaveFlight(output)) {
171 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
174 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
181 * Built-in command: (re)load the panel.
183 * path (optional): the file name to load the panel from
184 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
185 * and if that's unspecified, to "Panels/Default/default.xml".
188 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
191 arg->getStringValue("path",
192 fgGetString("/sim/panel/path",
193 "Panels/Default/default.xml"));
194 FGPanel * new_panel = fgReadPanel(panel_path);
195 if (new_panel == 0) {
196 SG_LOG(SG_INPUT, SG_ALERT,
197 "Error reading new panel from " << panel_path);
200 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
201 current_panel->unbind();
202 delete current_panel;
203 current_panel = new_panel;
204 current_panel->bind();
210 * Built-in command: (re)load preferences.
212 * path (optional): the file name to load the panel from (relative
213 * to FG_ROOT). Defaults to "preferences.xml".
216 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
218 const string &path = arg->getStringValue("path", "preferences.xml");
219 SGPath props_path(globals->get_fg_root());
220 props_path.append(path);
221 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
222 << props_path.str());
224 readProperties(props_path.str(), globals->get_props());
225 } catch (const sg_exception &e) {
226 guiErrorMessage("Error reading global preferences: ", e);
229 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
235 * Built-in command: cycle view.
238 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
240 globals->get_current_view()->set_view_offset(0.0);
241 globals->set_current_view(globals->get_viewmgr()->next_view());
242 if ( fgGetString("/sim/flight-model") == "ada" ) {
243 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
244 if ( globals->get_viewmgr()->get_current() == 1 ) {
245 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
248 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
254 * Built-in command: capture screen.
257 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
265 * Reload the tile cache.
268 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
270 bool freeze = globals->get_freeze();
271 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
273 globals->set_freeze( true );
275 if ( global_tile_mgr.init() ) {
276 // Load the local scenery data
277 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
278 fgGetDouble("/position/latitude-deg"));
280 SG_LOG( SG_GENERAL, SG_ALERT,
281 "Error in Tile Manager initialization!" );
286 globals->set_freeze( false );
292 * Update the lighting manually.
295 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
297 fgUpdateSkyAndLightingParams();
303 * Built-in command: toggle a bool property value.
305 * property: The name of the property to toggle.
308 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
311 *state = new PropertyCommandState(arg);
312 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
313 return prop->setBoolValue(!prop->getBoolValue());
318 * Built-in command: assign a value to a property.
320 * property: the name of the property to assign.
321 * value: the value to assign.
324 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
327 *state = new PropertyCommandState(arg);
328 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
329 const SGPropertyNode * value =
330 ((PropertyCommandState *)(*state))->getValue();
332 switch (prop->getType()) {
333 case SGPropertyNode::BOOL:
334 return prop->setBoolValue(value->getBoolValue());
335 case SGPropertyNode::INT:
336 return prop->setIntValue(value->getIntValue());
337 case SGPropertyNode::LONG:
338 return prop->setLongValue(value->getLongValue());
339 case SGPropertyNode::FLOAT:
340 return prop->setFloatValue(value->getFloatValue());
341 case SGPropertyNode::DOUBLE:
342 return prop->setDoubleValue(value->getDoubleValue());
343 case SGPropertyNode::STRING:
344 return prop->setStringValue(value->getStringValue());
346 return prop->setUnspecifiedValue(value->getStringValue());
352 * Built-in command: increment or decrement a property value.
354 * property: the name of the property to increment or decrement.
355 * step: the amount of the increment or decrement (default: 0).
356 * min: the minimum allowed value (default: no minimum).
357 * max: the maximum allowed value (default: no maximum).
358 * wrap: true if the value should be wrapped when it passes min or max;
359 * both min and max must be present for this to work (default:
363 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
366 *state = new PropertyCommandState(arg);
367 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
368 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
369 const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
370 const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
371 bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
373 switch (prop->getType()) {
374 case SGPropertyNode::BOOL:
375 if (step->getBoolValue())
376 return prop->setBoolValue(!prop->getBoolValue());
379 case SGPropertyNode::INT: {
380 int value = prop->getIntValue() + step->getIntValue();
381 if (min && (value < min->getIntValue())) {
383 value = max->getIntValue();
385 value = min->getIntValue();
387 if (max && value > max->getIntValue()) {
389 value = min->getIntValue();
391 value = max->getIntValue();
393 return prop->setIntValue(value);
395 case SGPropertyNode::LONG: {
396 long value = prop->getLongValue() + step->getLongValue();
397 if (min && (value < min->getLongValue())) {
399 value = max->getLongValue();
401 value = min->getLongValue();
403 if (max && value > max->getLongValue()) {
405 value = min->getLongValue();
407 value = max->getLongValue();
409 return prop->setLongValue(value);
411 case SGPropertyNode::FLOAT: {
412 float value = prop->getFloatValue() + step->getFloatValue();
413 if (min && (value < min->getFloatValue())) {
415 value = max->getFloatValue();
417 value = min->getFloatValue();
419 if (max && value > max->getFloatValue()) {
421 value = min->getFloatValue();
423 value = max->getFloatValue();
425 return prop->setFloatValue(value);
427 case SGPropertyNode::DOUBLE:
428 case SGPropertyNode::UNSPECIFIED:
429 case SGPropertyNode::NONE: {
430 double value = prop->getDoubleValue() + step->getDoubleValue();
431 if (min && (value < min->getDoubleValue())) {
433 value = max->getDoubleValue();
435 value = min->getDoubleValue();
437 if (max && value > max->getDoubleValue()) {
439 value = min->getDoubleValue();
441 value = max->getDoubleValue();
443 return prop->setDoubleValue(value);
445 case SGPropertyNode::STRING: // doesn't make sense with strings
446 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
449 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
456 * Built-in command: multiply a property value.
458 * property: the name of the property to multiply.
459 * factor: the amount by which to multiply.
462 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
465 *state = new PropertyCommandState(arg);
466 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
467 const SGPropertyNode * factor =
468 ((PropertyCommandState *)(*state))->getFactor();
470 switch (prop->getType()) {
471 case SGPropertyNode::BOOL:
472 return prop->setBoolValue(prop->getBoolValue() &&
473 factor->getBoolValue());
474 case SGPropertyNode::INT:
475 return prop->setIntValue(int(prop->getIntValue()
476 * factor->getDoubleValue()));
477 case SGPropertyNode::LONG:
478 return prop->setLongValue(long(prop->getLongValue()
479 * factor->getDoubleValue()));
480 case SGPropertyNode::FLOAT:
481 return prop->setFloatValue(float(prop->getFloatValue()
482 * factor->getDoubleValue()));
483 case SGPropertyNode::DOUBLE:
484 case SGPropertyNode::UNSPECIFIED:
485 case SGPropertyNode::NONE:
486 return prop->setDoubleValue(prop->getDoubleValue()
487 * factor->getDoubleValue());
488 default: // doesn't make sense with strings
495 * Built-in command: swap two property values.
497 * property[0]: the name of the first property.
498 * property[1]: the name of the second property.
501 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
504 *state = new PropertyCommandState(arg);
505 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
506 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
508 // FIXME: inefficient
509 const string & tmp = prop1->getStringValue();
510 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
511 prop2->setUnspecifiedValue(tmp));
516 * Set a property to an axis or other moving input.
518 * property: the name of the property to set.
519 * setting: the current input setting, usually between -1.0 and 1.0.
520 * offset: the offset to shift by, before applying the factor.
521 * factor: the factor to multiply by (use negative to reverse).
524 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
527 *state = new PropertyCommandState(arg);
528 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
530 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
532 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
534 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
536 return prop->setDoubleValue((setting + offset) * factor);
541 ////////////////////////////////////////////////////////////////////////
543 ////////////////////////////////////////////////////////////////////////
547 * Table of built-in commands.
549 * New commands do not have to be added here; any module in the application
550 * can add a new command using globals->get_commands()->addCommand(...).
554 SGCommandMgr::command_t command;
560 { "panel-load", do_panel_load },
561 { "preferences-load", do_preferences_load },
562 { "view-cycle", do_view_cycle },
563 { "screen-capture", do_screen_capture },
564 { "tile-cache-reload", do_tile_cache_reload },
565 { "lighting-update", do_lighting_update },
566 { "property-toggle", do_property_toggle },
567 { "property-assign", do_property_assign },
568 { "property-adjust", do_property_adjust },
569 { "property-multiply", do_property_multiply },
570 { "property-swap", do_property_swap },
571 { "property-scale", do_property_scale },
572 { 0, 0 } // zero-terminated
577 * Initialize the default built-in commands.
579 * Other commands may be added by other parts of the application.
584 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
585 for (int i = 0; built_ins[i].name != 0; i++) {
586 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
587 globals->get_commands()->addCommand(built_ins[i].name,
588 built_ins[i].command);
592 // end of fg_commands.hxx