1 // fg_commands.cxx - internal FGFS commands.
3 #include <simgear/compiler.h>
8 #include <simgear/debug/logstream.hxx>
9 #include <simgear/misc/commands.hxx>
10 #include <simgear/misc/props.hxx>
13 #include <Cockpit/panel.hxx>
14 #include <Cockpit/panel_io.hxx>
15 #include <Scenery/tilemgr.hxx>
16 #include <Time/tmp.hxx>
18 #include "fg_commands.hxx"
21 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
22 SG_USING_STD(ifstream);
23 SG_USING_STD(ofstream);
26 #include "fg_props.hxx"
28 #include "globals.hxx"
32 ////////////////////////////////////////////////////////////////////////
33 // Saved command states.
34 ////////////////////////////////////////////////////////////////////////
38 * Base saved state for property commands.
40 * Since this class isn't publicly visible, it is simply an aggregate
41 * of all the stuff any property command needs.
43 class PropertyCommandState : public SGCommandState
46 PropertyCommandState (const SGPropertyNode * arg);
47 virtual SGPropertyNode * getProp () const { return _prop; }
48 virtual SGPropertyNode * getProp2 () const { return _prop2; }
49 virtual const SGPropertyNode * getValue () const
50 { return _value ? _value : &_dummy_0; }
51 virtual const SGPropertyNode * getStep () const
52 { return _step ? _step : &_dummy_0; }
53 virtual const SGPropertyNode * getFactor () const
54 { return _factor ? _factor : &_dummy_1; }
55 virtual const SGPropertyNode * getSetting () const
56 { return _setting ? _setting : &_dummy_0; }
57 virtual const SGPropertyNode * getOffset () const
58 { return _offset ? _offset : &_dummy_0; }
60 static SGPropertyNode _dummy_0;
61 static SGPropertyNode _dummy_1;
62 mutable SGPropertyNode * _prop;
63 mutable SGPropertyNode * _prop2;
64 const SGPropertyNode * _value;
65 const SGPropertyNode * _step;
66 const SGPropertyNode * _factor;
67 const SGPropertyNode * _setting;
68 const SGPropertyNode * _offset;
71 SGPropertyNode PropertyCommandState::_dummy_0;
72 SGPropertyNode PropertyCommandState::_dummy_1;
74 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
75 : SGCommandState(arg),
76 _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
77 _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
78 _value(arg->getNode("value")),
79 _step(arg->getNode("step")),
80 _factor(arg->getNode("factor")),
81 _setting(arg->getNode("setting")),
82 _offset(arg->getNode("offset"))
84 // It would be better not to do this
85 // every time, but it's not that big
86 // a deal. I don't know enough about
87 // C++ static initialization to fix it.
88 _dummy_1.setDoubleValue(1.0);
94 ////////////////////////////////////////////////////////////////////////
95 // Command implementations.
96 ////////////////////////////////////////////////////////////////////////
100 * Built-in command: do nothing.
103 do_null (const SGPropertyNode * arg, SGCommandState ** state)
110 * Built-in command: exit FlightGear.
112 * TODO: show a confirm dialog.
115 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
117 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
124 * Built-in command: load flight.
126 * file (optional): the name of the file to load (relative to current
127 * directory). Defaults to "fgfs.sav".
130 do_load (const SGPropertyNode * arg, SGCommandState ** state)
132 const string &file = arg->getStringValue("file", "fgfs.sav");
133 ifstream input(file.c_str());
134 if (input.good() && fgLoadFlight(input)) {
136 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
139 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
146 * Built-in command: save flight.
148 * file (optional): the name of the file to save (relative to the
149 * current directory). Defaults to "fgfs.sav".
152 do_save (const SGPropertyNode * arg, SGCommandState ** state)
154 const string &file = arg->getStringValue("file", "fgfs.sav");
155 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
156 ofstream output(file.c_str());
157 if (output.good() && fgSaveFlight(output)) {
159 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
162 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
169 * Built-in command: (re)load the panel.
171 * path (optional): the file name to load the panel from
172 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
173 * and if that's unspecified, to "Panels/Default/default.xml".
176 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
179 arg->getStringValue("path",
180 fgGetString("/sim/panel/path",
181 "Panels/Default/default.xml"));
182 FGPanel * new_panel = fgReadPanel(panel_path);
183 if (new_panel == 0) {
184 SG_LOG(SG_INPUT, SG_ALERT,
185 "Error reading new panel from " << panel_path);
188 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
189 current_panel->unbind();
190 delete current_panel;
191 current_panel = new_panel;
192 current_panel->bind();
198 * Built-in command: (re)load preferences.
200 * path (optional): the file name to load the panel from (relative
201 * to FG_ROOT). Defaults to "preferences.xml".
204 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
206 const string &path = arg->getStringValue("path", "preferences.xml");
207 SGPath props_path(globals->get_fg_root());
208 props_path.append(path);
209 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
210 << props_path.str());
211 if (!readProperties(props_path.str(), globals->get_props())) {
212 SG_LOG(SG_INPUT, SG_ALERT, "Failed to reread global preferences");
215 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
222 * Built-in command: cycle view.
225 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
227 globals->get_current_view()->set_view_offset(0.0);
228 globals->set_current_view(globals->get_viewmgr()->next_view());
229 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
235 * Built-in command: capture screen.
238 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
246 * Reload the tile cache.
249 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
251 bool freeze = globals->get_freeze();
252 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
254 globals->set_freeze( true );
256 if ( global_tile_mgr.init() ) {
257 // Load the local scenery data
258 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
259 fgGetDouble("/position/latitude-deg"));
261 SG_LOG( SG_GENERAL, SG_ALERT,
262 "Error in Tile Manager initialization!" );
267 globals->set_freeze( false );
273 * Update the lighting manually.
276 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
278 fgUpdateSkyAndLightingParams();
284 * Built-in command: toggle a bool property value.
286 * property: The name of the property to toggle.
289 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
292 *state = new PropertyCommandState(arg);
293 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
294 return prop->setBoolValue(!prop->getBoolValue());
299 * Built-in command: assign a value to a property.
301 * property: the name of the property to assign.
302 * value: the value to assign.
305 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
308 *state = new PropertyCommandState(arg);
309 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
310 const SGPropertyNode * value =
311 ((PropertyCommandState *)(*state))->getValue();
313 switch (prop->getType()) {
314 case SGPropertyNode::BOOL:
315 return prop->setBoolValue(value->getBoolValue());
316 case SGPropertyNode::INT:
317 return prop->setIntValue(value->getIntValue());
318 case SGPropertyNode::LONG:
319 return prop->setLongValue(value->getLongValue());
320 case SGPropertyNode::FLOAT:
321 return prop->setFloatValue(value->getFloatValue());
322 case SGPropertyNode::DOUBLE:
323 return prop->setDoubleValue(value->getDoubleValue());
324 case SGPropertyNode::STRING:
325 return prop->setStringValue(value->getStringValue());
327 return prop->setUnspecifiedValue(value->getStringValue());
333 * Built-in command: increment or decrement a property value.
335 * property: the name of the property to increment or decrement.
336 * step: the amount of the increment or decrement.
339 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
342 *state = new PropertyCommandState(arg);
343 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
344 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
346 switch (prop->getType()) {
347 case SGPropertyNode::BOOL:
348 if (step->getBoolValue())
349 return prop->setBoolValue(!prop->getBoolValue());
352 case SGPropertyNode::INT:
353 return prop->setIntValue(prop->getIntValue()
354 + step->getIntValue());
355 case SGPropertyNode::LONG:
356 return prop->setLongValue(prop->getLongValue()
357 + step->getLongValue());
358 case SGPropertyNode::FLOAT:
359 return prop->setFloatValue(prop->getFloatValue()
360 + step->getFloatValue());
361 case SGPropertyNode::DOUBLE:
362 case SGPropertyNode::UNSPECIFIED:
363 return prop->setDoubleValue(prop->getDoubleValue()
364 + step->getDoubleValue());
365 default: // doesn't make sense with strings
372 * Built-in command: multiply a property value.
374 * property: the name of the property to multiply.
375 * factor: the amount by which to multiply.
378 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
381 *state = new PropertyCommandState(arg);
382 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
383 const SGPropertyNode * factor =
384 ((PropertyCommandState *)(*state))->getFactor();
386 switch (prop->getType()) {
387 case SGPropertyNode::BOOL:
388 return prop->setBoolValue(prop->getBoolValue() &&
389 factor->getBoolValue());
390 case SGPropertyNode::INT:
391 return prop->setIntValue(int(prop->getIntValue()
392 * factor->getDoubleValue()));
393 case SGPropertyNode::LONG:
394 return prop->setLongValue(long(prop->getLongValue()
395 * factor->getDoubleValue()));
396 case SGPropertyNode::FLOAT:
397 return prop->setFloatValue(float(prop->getFloatValue()
398 * factor->getDoubleValue()));
399 case SGPropertyNode::DOUBLE:
400 case SGPropertyNode::UNSPECIFIED:
401 return prop->setDoubleValue(prop->getDoubleValue()
402 * factor->getDoubleValue());
403 default: // doesn't make sense with strings
410 * Built-in command: swap two property values.
412 * property[0]: the name of the first property.
413 * property[1]: the name of the second property.
416 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
419 *state = new PropertyCommandState(arg);
420 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
421 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
423 // FIXME: inefficient
424 const string & tmp = prop1->getStringValue();
425 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
426 prop2->setUnspecifiedValue(tmp));
431 * Set a property to an axis or other moving input.
433 * property: the name of the property to set.
434 * setting: the current input setting, usually between -1.0 and 1.0.
435 * offset: the offset to shift by, before applying the factor.
436 * factor: the factor to multiply by (use negative to reverse).
439 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
442 *state = new PropertyCommandState(arg);
443 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
445 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
447 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
449 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
451 return prop->setDoubleValue((setting + offset) * factor);
456 ////////////////////////////////////////////////////////////////////////
458 ////////////////////////////////////////////////////////////////////////
462 * Table of built-in commands.
464 * New commands do not have to be added here; any module in the application
465 * can add a new command using globals->get_commands()->addCommand(...).
469 SGCommandMgr::command_t command;
475 { "panel-load", do_panel_load },
476 { "preferences-load", do_preferences_load },
477 { "view-cycle", do_view_cycle },
478 { "screen-capture", do_screen_capture },
479 { "tile-cache-reload", do_tile_cache_reload },
480 { "lighting-update", do_lighting_update },
481 { "property-toggle", do_property_toggle },
482 { "property-assign", do_property_assign },
483 { "property-adjust", do_property_adjust },
484 { "property-multiply", do_property_multiply },
485 { "property-swap", do_property_swap },
486 { "property-scale", do_property_scale },
487 { 0, 0 } // zero-terminated
492 * Initialize the default built-in commands.
494 * Other commands may be added by other parts of the application.
499 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
500 for (int i = 0; built_ins[i].name != 0; i++) {
501 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
502 globals->get_commands()->addCommand(built_ins[i].name,
503 built_ins[i].command);
507 // end of fg_commands.hxx