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"
33 ////////////////////////////////////////////////////////////////////////
34 // Saved command states.
35 ////////////////////////////////////////////////////////////////////////
39 * Base saved state for property commands.
41 * Since this class isn't publicly visible, it is simply an aggregate
42 * of all the stuff any property command needs.
44 class PropertyCommandState : public SGCommandState
47 PropertyCommandState (const SGPropertyNode * arg);
48 virtual SGPropertyNode * getProp () const { return _prop; }
49 virtual SGPropertyNode * getProp2 () const { return _prop2; }
50 virtual const SGPropertyNode * getValue () const
51 { return _value ? _value : &_dummy_0; }
52 virtual const SGPropertyNode * getStep () const
53 { return _step ? _step : &_dummy_0; }
54 virtual const SGPropertyNode * getFactor () const
55 { return _factor ? _factor : &_dummy_1; }
56 virtual const SGPropertyNode * getSetting () const
57 { return _setting ? _setting : &_dummy_0; }
58 virtual const SGPropertyNode * getOffset () const
59 { return _offset ? _offset : &_dummy_0; }
61 static SGPropertyNode _dummy_0;
62 static SGPropertyNode _dummy_1;
63 mutable SGPropertyNode * _prop;
64 mutable SGPropertyNode * _prop2;
65 const SGPropertyNode * _value;
66 const SGPropertyNode * _step;
67 const SGPropertyNode * _factor;
68 const SGPropertyNode * _setting;
69 const SGPropertyNode * _offset;
72 SGPropertyNode PropertyCommandState::_dummy_0;
73 SGPropertyNode PropertyCommandState::_dummy_1;
75 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
76 : SGCommandState(arg),
77 _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
78 _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
79 _value(arg->getNode("value")),
80 _step(arg->getNode("step")),
81 _factor(arg->getNode("factor")),
82 _setting(arg->getNode("setting")),
83 _offset(arg->getNode("offset"))
85 // It would be better not to do this
86 // every time, but it's not that big
87 // a deal. I don't know enough about
88 // C++ static initialization to fix it.
89 _dummy_1.setDoubleValue(1.0);
95 ////////////////////////////////////////////////////////////////////////
96 // Command implementations.
97 ////////////////////////////////////////////////////////////////////////
101 * Built-in command: do nothing.
104 do_null (const SGPropertyNode * arg, SGCommandState ** state)
111 * Built-in command: exit FlightGear.
113 * TODO: show a confirm dialog.
116 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
118 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
125 * Built-in command: load flight.
127 * file (optional): the name of the file to load (relative to current
128 * directory). Defaults to "fgfs.sav".
131 do_load (const SGPropertyNode * arg, SGCommandState ** state)
133 const string &file = arg->getStringValue("file", "fgfs.sav");
134 ifstream input(file.c_str());
135 if (input.good() && fgLoadFlight(input)) {
137 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
140 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
147 * Built-in command: save flight.
149 * file (optional): the name of the file to save (relative to the
150 * current directory). Defaults to "fgfs.sav".
153 do_save (const SGPropertyNode * arg, SGCommandState ** state)
155 const string &file = arg->getStringValue("file", "fgfs.sav");
156 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
157 ofstream output(file.c_str());
158 if (output.good() && fgSaveFlight(output)) {
160 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
163 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
170 * Built-in command: (re)load the panel.
172 * path (optional): the file name to load the panel from
173 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
174 * and if that's unspecified, to "Panels/Default/default.xml".
177 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
180 arg->getStringValue("path",
181 fgGetString("/sim/panel/path",
182 "Panels/Default/default.xml"));
183 FGPanel * new_panel = fgReadPanel(panel_path);
184 if (new_panel == 0) {
185 SG_LOG(SG_INPUT, SG_ALERT,
186 "Error reading new panel from " << panel_path);
189 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
190 current_panel->unbind();
191 delete current_panel;
192 current_panel = new_panel;
193 current_panel->bind();
199 * Built-in command: (re)load preferences.
201 * path (optional): the file name to load the panel from (relative
202 * to FG_ROOT). Defaults to "preferences.xml".
205 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
207 const string &path = arg->getStringValue("path", "preferences.xml");
208 SGPath props_path(globals->get_fg_root());
209 props_path.append(path);
210 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
211 << props_path.str());
213 readProperties(props_path.str(), globals->get_props());
214 } catch (const sg_io_exception &e) {
215 string message = "Error reading global preferences: ";
216 message += e.getMessage();
218 message += e.getLocation().asString();
219 SG_LOG(SG_INPUT, SG_ALERT, message);
220 mkDialog(message.c_str());
223 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
229 * Built-in command: cycle view.
232 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
234 globals->get_current_view()->set_view_offset(0.0);
235 globals->set_current_view(globals->get_viewmgr()->next_view());
236 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
242 * Built-in command: capture screen.
245 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
253 * Reload the tile cache.
256 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
258 bool freeze = globals->get_freeze();
259 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
261 globals->set_freeze( true );
263 if ( global_tile_mgr.init() ) {
264 // Load the local scenery data
265 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
266 fgGetDouble("/position/latitude-deg"));
268 SG_LOG( SG_GENERAL, SG_ALERT,
269 "Error in Tile Manager initialization!" );
274 globals->set_freeze( false );
280 * Update the lighting manually.
283 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
285 fgUpdateSkyAndLightingParams();
291 * Built-in command: toggle a bool property value.
293 * property: The name of the property to toggle.
296 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
299 *state = new PropertyCommandState(arg);
300 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
301 return prop->setBoolValue(!prop->getBoolValue());
306 * Built-in command: assign a value to a property.
308 * property: the name of the property to assign.
309 * value: the value to assign.
312 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
315 *state = new PropertyCommandState(arg);
316 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
317 const SGPropertyNode * value =
318 ((PropertyCommandState *)(*state))->getValue();
320 switch (prop->getType()) {
321 case SGPropertyNode::BOOL:
322 return prop->setBoolValue(value->getBoolValue());
323 case SGPropertyNode::INT:
324 return prop->setIntValue(value->getIntValue());
325 case SGPropertyNode::LONG:
326 return prop->setLongValue(value->getLongValue());
327 case SGPropertyNode::FLOAT:
328 return prop->setFloatValue(value->getFloatValue());
329 case SGPropertyNode::DOUBLE:
330 return prop->setDoubleValue(value->getDoubleValue());
331 case SGPropertyNode::STRING:
332 return prop->setStringValue(value->getStringValue());
334 return prop->setUnspecifiedValue(value->getStringValue());
340 * Built-in command: increment or decrement a property value.
342 * property: the name of the property to increment or decrement.
343 * step: the amount of the increment or decrement.
346 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
349 *state = new PropertyCommandState(arg);
350 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
351 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
353 switch (prop->getType()) {
354 case SGPropertyNode::BOOL:
355 if (step->getBoolValue())
356 return prop->setBoolValue(!prop->getBoolValue());
359 case SGPropertyNode::INT:
360 return prop->setIntValue(prop->getIntValue()
361 + step->getIntValue());
362 case SGPropertyNode::LONG:
363 return prop->setLongValue(prop->getLongValue()
364 + step->getLongValue());
365 case SGPropertyNode::FLOAT:
366 return prop->setFloatValue(prop->getFloatValue()
367 + step->getFloatValue());
368 case SGPropertyNode::DOUBLE:
369 case SGPropertyNode::UNSPECIFIED:
370 return prop->setDoubleValue(prop->getDoubleValue()
371 + step->getDoubleValue());
372 default: // doesn't make sense with strings
379 * Built-in command: multiply a property value.
381 * property: the name of the property to multiply.
382 * factor: the amount by which to multiply.
385 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
388 *state = new PropertyCommandState(arg);
389 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
390 const SGPropertyNode * factor =
391 ((PropertyCommandState *)(*state))->getFactor();
393 switch (prop->getType()) {
394 case SGPropertyNode::BOOL:
395 return prop->setBoolValue(prop->getBoolValue() &&
396 factor->getBoolValue());
397 case SGPropertyNode::INT:
398 return prop->setIntValue(int(prop->getIntValue()
399 * factor->getDoubleValue()));
400 case SGPropertyNode::LONG:
401 return prop->setLongValue(long(prop->getLongValue()
402 * factor->getDoubleValue()));
403 case SGPropertyNode::FLOAT:
404 return prop->setFloatValue(float(prop->getFloatValue()
405 * factor->getDoubleValue()));
406 case SGPropertyNode::DOUBLE:
407 case SGPropertyNode::UNSPECIFIED:
408 return prop->setDoubleValue(prop->getDoubleValue()
409 * factor->getDoubleValue());
410 default: // doesn't make sense with strings
417 * Built-in command: swap two property values.
419 * property[0]: the name of the first property.
420 * property[1]: the name of the second property.
423 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
426 *state = new PropertyCommandState(arg);
427 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
428 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
430 // FIXME: inefficient
431 const string & tmp = prop1->getStringValue();
432 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
433 prop2->setUnspecifiedValue(tmp));
438 * Set a property to an axis or other moving input.
440 * property: the name of the property to set.
441 * setting: the current input setting, usually between -1.0 and 1.0.
442 * offset: the offset to shift by, before applying the factor.
443 * factor: the factor to multiply by (use negative to reverse).
446 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
449 *state = new PropertyCommandState(arg);
450 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
452 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
454 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
456 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
458 return prop->setDoubleValue((setting + offset) * factor);
463 ////////////////////////////////////////////////////////////////////////
465 ////////////////////////////////////////////////////////////////////////
469 * Table of built-in commands.
471 * New commands do not have to be added here; any module in the application
472 * can add a new command using globals->get_commands()->addCommand(...).
476 SGCommandMgr::command_t command;
482 { "panel-load", do_panel_load },
483 { "preferences-load", do_preferences_load },
484 { "view-cycle", do_view_cycle },
485 { "screen-capture", do_screen_capture },
486 { "tile-cache-reload", do_tile_cache_reload },
487 { "lighting-update", do_lighting_update },
488 { "property-toggle", do_property_toggle },
489 { "property-assign", do_property_assign },
490 { "property-adjust", do_property_adjust },
491 { "property-multiply", do_property_multiply },
492 { "property-swap", do_property_swap },
493 { "property-scale", do_property_scale },
494 { 0, 0 } // zero-terminated
499 * Initialize the default built-in commands.
501 * Other commands may be added by other parts of the application.
506 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
507 for (int i = 0; built_ins[i].name != 0; i++) {
508 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
509 globals->get_commands()->addCommand(built_ins[i].name,
510 built_ins[i].command);
514 // end of fg_commands.hxx