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 * getFactor () const
56 { return _factor ? _factor : &_dummy_1; }
57 virtual const SGPropertyNode * getSetting () const
58 { return _setting ? _setting : &_dummy_0; }
59 virtual const SGPropertyNode * getOffset () const
60 { return _offset ? _offset : &_dummy_0; }
62 static SGPropertyNode _dummy_0;
63 static SGPropertyNode _dummy_1;
64 mutable SGPropertyNode * _prop;
65 mutable SGPropertyNode * _prop2;
66 const SGPropertyNode * _value;
67 const SGPropertyNode * _step;
68 const SGPropertyNode * _factor;
69 const SGPropertyNode * _setting;
70 const SGPropertyNode * _offset;
73 SGPropertyNode PropertyCommandState::_dummy_0;
74 SGPropertyNode PropertyCommandState::_dummy_1;
76 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
77 : SGCommandState(arg),
78 _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
79 _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
80 _value(arg->getNode("value")),
81 _step(arg->getNode("step")),
82 _factor(arg->getNode("factor")),
83 _setting(arg->getNode("setting")),
84 _offset(arg->getNode("offset"))
86 // It would be better not to do this
87 // every time, but it's not that big
88 // a deal. I don't know enough about
89 // C++ static initialization to fix it.
90 _dummy_1.setDoubleValue(1.0);
96 ////////////////////////////////////////////////////////////////////////
97 // Command implementations.
98 ////////////////////////////////////////////////////////////////////////
102 * Built-in command: do nothing.
105 do_null (const SGPropertyNode * arg, SGCommandState ** state)
112 * Built-in command: exit FlightGear.
114 * TODO: show a confirm dialog.
117 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
119 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
126 * Built-in command: load flight.
128 * file (optional): the name of the file to load (relative to current
129 * directory). Defaults to "fgfs.sav".
132 do_load (const SGPropertyNode * arg, SGCommandState ** state)
134 const string &file = arg->getStringValue("file", "fgfs.sav");
135 ifstream input(file.c_str());
136 if (input.good() && fgLoadFlight(input)) {
138 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
141 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
148 * Built-in command: save flight.
150 * file (optional): the name of the file to save (relative to the
151 * current directory). Defaults to "fgfs.sav".
154 do_save (const SGPropertyNode * arg, SGCommandState ** state)
156 const string &file = arg->getStringValue("file", "fgfs.sav");
157 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
158 ofstream output(file.c_str());
159 if (output.good() && fgSaveFlight(output)) {
161 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
164 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
171 * Built-in command: (re)load the panel.
173 * path (optional): the file name to load the panel from
174 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
175 * and if that's unspecified, to "Panels/Default/default.xml".
178 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
181 arg->getStringValue("path",
182 fgGetString("/sim/panel/path",
183 "Panels/Default/default.xml"));
184 FGPanel * new_panel = fgReadPanel(panel_path);
185 if (new_panel == 0) {
186 SG_LOG(SG_INPUT, SG_ALERT,
187 "Error reading new panel from " << panel_path);
190 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
191 current_panel->unbind();
192 delete current_panel;
193 current_panel = new_panel;
194 current_panel->bind();
200 * Built-in command: (re)load preferences.
202 * path (optional): the file name to load the panel from (relative
203 * to FG_ROOT). Defaults to "preferences.xml".
206 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
208 const string &path = arg->getStringValue("path", "preferences.xml");
209 SGPath props_path(globals->get_fg_root());
210 props_path.append(path);
211 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
212 << props_path.str());
214 readProperties(props_path.str(), globals->get_props());
215 } catch (const sg_io_exception &e) {
216 string message = "Error reading global preferences: ";
217 message += e.getMessage();
219 message += e.getLocation().asString();
220 SG_LOG(SG_INPUT, SG_ALERT, message);
221 mkDialog(message.c_str());
224 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
230 * Built-in command: cycle view.
233 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
235 globals->get_current_view()->set_view_offset(0.0);
236 globals->set_current_view(globals->get_viewmgr()->next_view());
237 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
243 * Built-in command: capture screen.
246 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
254 * Reload the tile cache.
257 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
259 bool freeze = globals->get_freeze();
260 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
262 globals->set_freeze( true );
264 if ( global_tile_mgr.init() ) {
265 // Load the local scenery data
266 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
267 fgGetDouble("/position/latitude-deg"));
269 SG_LOG( SG_GENERAL, SG_ALERT,
270 "Error in Tile Manager initialization!" );
275 globals->set_freeze( false );
281 * Update the lighting manually.
284 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
286 fgUpdateSkyAndLightingParams();
292 * Built-in command: toggle a bool property value.
294 * property: The name of the property to toggle.
297 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
300 *state = new PropertyCommandState(arg);
301 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
302 return prop->setBoolValue(!prop->getBoolValue());
307 * Built-in command: assign a value to a property.
309 * property: the name of the property to assign.
310 * value: the value to assign.
313 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
316 *state = new PropertyCommandState(arg);
317 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
318 const SGPropertyNode * value =
319 ((PropertyCommandState *)(*state))->getValue();
321 switch (prop->getType()) {
322 case SGPropertyNode::BOOL:
323 return prop->setBoolValue(value->getBoolValue());
324 case SGPropertyNode::INT:
325 return prop->setIntValue(value->getIntValue());
326 case SGPropertyNode::LONG:
327 return prop->setLongValue(value->getLongValue());
328 case SGPropertyNode::FLOAT:
329 return prop->setFloatValue(value->getFloatValue());
330 case SGPropertyNode::DOUBLE:
331 return prop->setDoubleValue(value->getDoubleValue());
332 case SGPropertyNode::STRING:
333 return prop->setStringValue(value->getStringValue());
335 return prop->setUnspecifiedValue(value->getStringValue());
341 * Built-in command: increment or decrement a property value.
343 * property: the name of the property to increment or decrement.
344 * step: the amount of the increment or decrement.
347 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
350 *state = new PropertyCommandState(arg);
351 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
352 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
354 switch (prop->getType()) {
355 case SGPropertyNode::BOOL:
356 if (step->getBoolValue())
357 return prop->setBoolValue(!prop->getBoolValue());
360 case SGPropertyNode::INT:
361 return prop->setIntValue(prop->getIntValue()
362 + step->getIntValue());
363 case SGPropertyNode::LONG:
364 return prop->setLongValue(prop->getLongValue()
365 + step->getLongValue());
366 case SGPropertyNode::FLOAT:
367 return prop->setFloatValue(prop->getFloatValue()
368 + step->getFloatValue());
369 case SGPropertyNode::DOUBLE:
370 case SGPropertyNode::UNSPECIFIED:
371 return prop->setDoubleValue(prop->getDoubleValue()
372 + step->getDoubleValue());
373 default: // doesn't make sense with strings
380 * Built-in command: multiply a property value.
382 * property: the name of the property to multiply.
383 * factor: the amount by which to multiply.
386 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
389 *state = new PropertyCommandState(arg);
390 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
391 const SGPropertyNode * factor =
392 ((PropertyCommandState *)(*state))->getFactor();
394 switch (prop->getType()) {
395 case SGPropertyNode::BOOL:
396 return prop->setBoolValue(prop->getBoolValue() &&
397 factor->getBoolValue());
398 case SGPropertyNode::INT:
399 return prop->setIntValue(int(prop->getIntValue()
400 * factor->getDoubleValue()));
401 case SGPropertyNode::LONG:
402 return prop->setLongValue(long(prop->getLongValue()
403 * factor->getDoubleValue()));
404 case SGPropertyNode::FLOAT:
405 return prop->setFloatValue(float(prop->getFloatValue()
406 * factor->getDoubleValue()));
407 case SGPropertyNode::DOUBLE:
408 case SGPropertyNode::UNSPECIFIED:
409 return prop->setDoubleValue(prop->getDoubleValue()
410 * factor->getDoubleValue());
411 default: // doesn't make sense with strings
418 * Built-in command: swap two property values.
420 * property[0]: the name of the first property.
421 * property[1]: the name of the second property.
424 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
427 *state = new PropertyCommandState(arg);
428 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
429 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
431 // FIXME: inefficient
432 const string & tmp = prop1->getStringValue();
433 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
434 prop2->setUnspecifiedValue(tmp));
439 * Set a property to an axis or other moving input.
441 * property: the name of the property to set.
442 * setting: the current input setting, usually between -1.0 and 1.0.
443 * offset: the offset to shift by, before applying the factor.
444 * factor: the factor to multiply by (use negative to reverse).
447 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
450 *state = new PropertyCommandState(arg);
451 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
453 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
455 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
457 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
459 return prop->setDoubleValue((setting + offset) * factor);
464 ////////////////////////////////////////////////////////////////////////
466 ////////////////////////////////////////////////////////////////////////
470 * Table of built-in commands.
472 * New commands do not have to be added here; any module in the application
473 * can add a new command using globals->get_commands()->addCommand(...).
477 SGCommandMgr::command_t command;
483 { "panel-load", do_panel_load },
484 { "preferences-load", do_preferences_load },
485 { "view-cycle", do_view_cycle },
486 { "screen-capture", do_screen_capture },
487 { "tile-cache-reload", do_tile_cache_reload },
488 { "lighting-update", do_lighting_update },
489 { "property-toggle", do_property_toggle },
490 { "property-assign", do_property_assign },
491 { "property-adjust", do_property_adjust },
492 { "property-multiply", do_property_multiply },
493 { "property-swap", do_property_swap },
494 { "property-scale", do_property_scale },
495 { 0, 0 } // zero-terminated
500 * Initialize the default built-in commands.
502 * Other commands may be added by other parts of the application.
507 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
508 for (int i = 0; built_ins[i].name != 0; i++) {
509 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
510 globals->get_commands()->addCommand(built_ins[i].name,
511 built_ins[i].command);
515 // end of fg_commands.hxx