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 bool hasStep () const { return _step != 0; }
56 virtual const SGPropertyNode * getStep () const
57 { return _step ? _step : &_dummy_0; }
58 virtual const SGPropertyNode * getMin () const { return _min; }
59 virtual const SGPropertyNode * getMax () const { return _max; }
60 virtual const SGPropertyNode * getWrap () const
61 { return _wrap ? _wrap : &_dummy_0; }
62 virtual const SGPropertyNode * getFactor () const
63 { return _factor ? _factor : &_dummy_1; }
64 virtual const SGPropertyNode * getSquared () const
65 { return _squared ? _squared : &_dummy_0; }
66 virtual const SGPropertyNode * getSetting () const
67 { return _setting ? _setting : &_dummy_0; }
68 virtual const SGPropertyNode * getOffset () const
69 { return _offset ? _offset : &_dummy_0; }
71 static SGPropertyNode _dummy_0;
72 static SGPropertyNode _dummy_1;
73 mutable SGPropertyNode * _prop;
74 mutable SGPropertyNode * _prop2;
75 const SGPropertyNode * _value;
76 const SGPropertyNode * _step;
77 const SGPropertyNode * _min;
78 const SGPropertyNode * _max;
79 const SGPropertyNode * _wrap;
80 const SGPropertyNode * _factor;
81 const SGPropertyNode * _squared;
82 const SGPropertyNode * _setting;
83 const SGPropertyNode * _offset;
86 SGPropertyNode PropertyCommandState::_dummy_0;
87 SGPropertyNode PropertyCommandState::_dummy_1;
89 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
90 : SGCommandState(arg),
91 _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
92 _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
93 _value(arg->getNode("value")),
94 _step(arg->getNode("step")),
95 _min(arg->getNode("min")),
96 _max(arg->getNode("max")),
97 _wrap(arg->getNode("wrap")),
98 _factor(arg->getNode("factor")),
99 _squared(arg->getNode("squared")),
100 _setting(arg->getNode("setting")),
101 _offset(arg->getNode("offset"))
103 // It would be better not to do this
104 // every time, but it's not that big
105 // a deal. I don't know enough about
106 // C++ static initialization to fix it.
107 _dummy_1.setDoubleValue(1.0);
113 ////////////////////////////////////////////////////////////////////////
114 // Command implementations.
115 ////////////////////////////////////////////////////////////////////////
119 * Built-in command: do nothing.
122 do_null (const SGPropertyNode * arg, SGCommandState ** state)
129 * Built-in command: exit FlightGear.
131 * TODO: show a confirm dialog.
134 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
136 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
143 * Built-in command: load flight.
145 * file (optional): the name of the file to load (relative to current
146 * directory). Defaults to "fgfs.sav".
149 do_load (const SGPropertyNode * arg, SGCommandState ** state)
151 const string &file = arg->getStringValue("file", "fgfs.sav");
152 ifstream input(file.c_str());
153 if (input.good() && fgLoadFlight(input)) {
155 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
158 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
165 * Built-in command: save flight.
167 * file (optional): the name of the file to save (relative to the
168 * current directory). Defaults to "fgfs.sav".
171 do_save (const SGPropertyNode * arg, SGCommandState ** state)
173 const string &file = arg->getStringValue("file", "fgfs.sav");
174 bool write_all = arg->getBoolValue("write-all", false);
175 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
176 ofstream output(file.c_str());
177 if (output.good() && fgSaveFlight(output, write_all)) {
179 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
182 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
189 * Built-in command: (re)load the panel.
191 * path (optional): the file name to load the panel from
192 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
193 * and if that's unspecified, to "Panels/Default/default.xml".
196 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
199 arg->getStringValue("path",
200 fgGetString("/sim/panel/path",
201 "Panels/Default/default.xml"));
202 FGPanel * new_panel = fgReadPanel(panel_path);
203 if (new_panel == 0) {
204 SG_LOG(SG_INPUT, SG_ALERT,
205 "Error reading new panel from " << panel_path);
208 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
209 current_panel->unbind();
210 delete current_panel;
211 current_panel = new_panel;
212 current_panel->bind();
218 * Built-in command: pass a mouse click to the panel.
220 * button: the mouse button number, zero-based.
221 * is-down: true if the button is down, false if it is up.
222 * x-pos: the x position of the mouse click.
223 * y-pos: the y position of the mouse click.
226 do_panel_mouse_click (const SGPropertyNode * arg, SGCommandState ** state)
228 if (current_panel != 0)
230 ->doMouseAction(arg->getIntValue("button"),
231 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
232 arg->getIntValue("x-pos"),
233 arg->getIntValue("y-pos"));
240 * Built-in command: (re)load preferences.
242 * path (optional): the file name to load the panel from (relative
243 * to FG_ROOT). Defaults to "preferences.xml".
246 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
248 const string &path = arg->getStringValue("path", "preferences.xml");
249 SGPath props_path(globals->get_fg_root());
250 props_path.append(path);
251 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
252 << props_path.str());
254 readProperties(props_path.str(), globals->get_props());
255 } catch (const sg_exception &e) {
256 guiErrorMessage("Error reading global preferences: ", e);
259 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
265 * Built-in command: cycle view.
268 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
270 globals->get_current_view()->setHeadingOffset_deg(0.0);
271 globals->get_viewmgr()->next_view();
272 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
273 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
274 if ( globals->get_viewmgr()->get_current() == 1 ) {
275 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
278 global_tile_mgr.refresh_view_timestamps();
279 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
285 * Built-in command: capture screen.
288 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
296 * Reload the tile cache.
299 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
301 static const SGPropertyNode *master_freeze
302 = fgGetNode("/sim/freeze/master");
303 bool freeze = master_freeze->getBoolValue();
304 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
306 fgSetBool("/sim/freeze/master", true);
309 if ( global_tile_mgr.init() ) {
310 // Load the local scenery data
311 double visibility_meters = fgGetDouble("/environment/visibility-m");
312 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
313 fgGetDouble("/position/latitude-deg"),
316 SG_LOG( SG_GENERAL, SG_ALERT,
317 "Error in Tile Manager initialization!" );
322 fgSetBool("/sim/freeze/master", false);
329 * Update the lighting manually.
332 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
334 fgUpdateSkyAndLightingParams();
340 * Built-in command: toggle a bool property value.
342 * property: The name of the property to toggle.
345 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
348 *state = new PropertyCommandState(arg);
349 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
350 return prop->setBoolValue(!prop->getBoolValue());
355 * Built-in command: assign a value to a property.
357 * property: the name of the property to assign.
358 * value: the value to assign.
361 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
364 *state = new PropertyCommandState(arg);
365 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
366 const SGPropertyNode * value =
367 ((PropertyCommandState *)(*state))->getValue();
369 switch (prop->getType()) {
370 case SGPropertyNode::BOOL:
371 return prop->setBoolValue(value->getBoolValue());
372 case SGPropertyNode::INT:
373 return prop->setIntValue(value->getIntValue());
374 case SGPropertyNode::LONG:
375 return prop->setLongValue(value->getLongValue());
376 case SGPropertyNode::FLOAT:
377 return prop->setFloatValue(value->getFloatValue());
378 case SGPropertyNode::DOUBLE:
379 return prop->setDoubleValue(value->getDoubleValue());
380 case SGPropertyNode::STRING:
381 return prop->setStringValue(value->getStringValue());
383 return prop->setUnspecifiedValue(value->getStringValue());
389 * Built-in command: increment or decrement a property value.
391 * property: the name of the property to increment or decrement.
392 * step: the amount of the increment or decrement (default: 0).
393 * offset: a normalized amount to offset by (if step is not present).
394 * factor: the amount by which to multiply the offset (if step is not present).
395 * min: the minimum allowed value (default: no minimum).
396 * max: the maximum allowed value (default: no maximum).
397 * wrap: true if the value should be wrapped when it passes min or max;
398 * both min and max must be present for this to work (default:
402 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
405 *state = new PropertyCommandState(arg);
406 bool hasStep = ((PropertyCommandState *)(*state))->hasStep();
407 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
408 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
409 const SGPropertyNode * offset =
410 ((PropertyCommandState *)(*state))->getOffset();
411 const SGPropertyNode * factor =
412 ((PropertyCommandState *)(*state))->getFactor();
413 const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
414 const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
415 bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
419 amount = offset->getDoubleValue() * factor->getDoubleValue();
423 switch (prop->getType()) {
424 case SGPropertyNode::BOOL:
427 value = step->getBoolValue();
429 value = (0.0 != amount);
431 return prop->setBoolValue(!prop->getBoolValue());
434 case SGPropertyNode::INT: {
437 value = prop->getIntValue() + step->getIntValue();
439 value = prop->getIntValue() + int(amount);
440 if (min && (value < min->getIntValue())) {
442 value = max->getIntValue();
444 value = min->getIntValue();
446 if (max && value > max->getIntValue()) {
448 value = min->getIntValue();
450 value = max->getIntValue();
452 return prop->setIntValue(value);
454 case SGPropertyNode::LONG: {
457 value = prop->getLongValue() + step->getLongValue();
459 value = prop->getLongValue() + long(amount);
460 if (min && (value < min->getLongValue())) {
462 value = max->getLongValue();
464 value = min->getLongValue();
466 if (max && value > max->getLongValue()) {
468 value = min->getLongValue();
470 value = max->getLongValue();
472 return prop->setLongValue(value);
474 case SGPropertyNode::FLOAT: {
477 value = prop->getFloatValue() + step->getFloatValue();
479 value = prop->getFloatValue() + float(amount);
480 if (min && (value < min->getFloatValue())) {
482 value = max->getFloatValue();
484 value = min->getFloatValue();
486 if (max && value > max->getFloatValue()) {
488 value = min->getFloatValue();
490 value = max->getFloatValue();
492 return prop->setFloatValue(value);
494 case SGPropertyNode::DOUBLE:
495 case SGPropertyNode::UNSPECIFIED:
496 case SGPropertyNode::NONE: {
499 value = prop->getDoubleValue() + step->getDoubleValue();
501 value = prop->getDoubleValue() + amount;
502 if (min && (value < min->getDoubleValue())) {
504 value = max->getDoubleValue();
506 value = min->getDoubleValue();
508 if (max && value > max->getDoubleValue()) {
510 value = min->getDoubleValue();
512 value = max->getDoubleValue();
514 return prop->setDoubleValue(value);
516 case SGPropertyNode::STRING: // doesn't make sense with strings
517 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
520 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
527 * Built-in command: multiply a property value.
529 * property: the name of the property to multiply.
530 * factor: the amount by which to multiply.
533 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
536 *state = new PropertyCommandState(arg);
537 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
538 const SGPropertyNode * factor =
539 ((PropertyCommandState *)(*state))->getFactor();
541 switch (prop->getType()) {
542 case SGPropertyNode::BOOL:
543 return prop->setBoolValue(prop->getBoolValue() &&
544 factor->getBoolValue());
545 case SGPropertyNode::INT:
546 return prop->setIntValue(int(prop->getIntValue()
547 * factor->getDoubleValue()));
548 case SGPropertyNode::LONG:
549 return prop->setLongValue(long(prop->getLongValue()
550 * factor->getDoubleValue()));
551 case SGPropertyNode::FLOAT:
552 return prop->setFloatValue(float(prop->getFloatValue()
553 * factor->getDoubleValue()));
554 case SGPropertyNode::DOUBLE:
555 case SGPropertyNode::UNSPECIFIED:
556 case SGPropertyNode::NONE:
557 return prop->setDoubleValue(prop->getDoubleValue()
558 * factor->getDoubleValue());
559 default: // doesn't make sense with strings
566 * Built-in command: swap two property values.
568 * property[0]: the name of the first property.
569 * property[1]: the name of the second property.
572 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
575 *state = new PropertyCommandState(arg);
576 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
577 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
579 // FIXME: inefficient
580 const string & tmp = prop1->getStringValue();
581 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
582 prop2->setUnspecifiedValue(tmp.c_str()));
587 * Set a property to an axis or other moving input.
589 * property: the name of the property to set.
590 * setting: the current input setting, usually between -1.0 and 1.0.
591 * offset: the offset to shift by, before applying the factor.
592 * factor: the factor to multiply by (use negative to reverse).
595 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
598 *state = new PropertyCommandState(arg);
599 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
601 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
603 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
605 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
607 ((PropertyCommandState *)(*state))->getSquared()->getBoolValue();
610 setting = (setting < 0 ? -1 : 1) * setting * setting;
611 double result = (setting + offset) * factor;
613 return prop->setDoubleValue(result);
618 ////////////////////////////////////////////////////////////////////////
620 ////////////////////////////////////////////////////////////////////////
624 * Table of built-in commands.
626 * New commands do not have to be added here; any module in the application
627 * can add a new command using globals->get_commands()->addCommand(...).
631 SGCommandMgr::command_t command;
637 { "panel-load", do_panel_load },
638 { "panel-mouse-click", do_panel_mouse_click },
639 { "preferences-load", do_preferences_load },
640 { "view-cycle", do_view_cycle },
641 { "screen-capture", do_screen_capture },
642 { "tile-cache-reload", do_tile_cache_reload },
643 { "lighting-update", do_lighting_update },
644 { "property-toggle", do_property_toggle },
645 { "property-assign", do_property_assign },
646 { "property-adjust", do_property_adjust },
647 { "property-multiply", do_property_multiply },
648 { "property-swap", do_property_swap },
649 { "property-scale", do_property_scale },
650 { 0, 0 } // zero-terminated
655 * Initialize the default built-in commands.
657 * Other commands may be added by other parts of the application.
662 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
663 for (int i = 0; built_ins[i].name != 0; i++) {
664 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
665 globals->get_commands()->addCommand(built_ins[i].name,
666 built_ins[i].command);
670 // end of fg_commands.cxx