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_exception &e) {
216 guiErrorMessage("Error reading global preferences: ", e);
219 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
225 * Built-in command: cycle view.
228 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
230 globals->get_current_view()->set_view_offset(0.0);
231 globals->set_current_view(globals->get_viewmgr()->next_view());
232 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
238 * Built-in command: capture screen.
241 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
249 * Reload the tile cache.
252 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
254 bool freeze = globals->get_freeze();
255 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
257 globals->set_freeze( true );
259 if ( global_tile_mgr.init() ) {
260 // Load the local scenery data
261 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
262 fgGetDouble("/position/latitude-deg"));
264 SG_LOG( SG_GENERAL, SG_ALERT,
265 "Error in Tile Manager initialization!" );
270 globals->set_freeze( false );
276 * Update the lighting manually.
279 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
281 fgUpdateSkyAndLightingParams();
287 * Built-in command: toggle a bool property value.
289 * property: The name of the property to toggle.
292 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
295 *state = new PropertyCommandState(arg);
296 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
297 return prop->setBoolValue(!prop->getBoolValue());
302 * Built-in command: assign a value to a property.
304 * property: the name of the property to assign.
305 * value: the value to assign.
308 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
311 *state = new PropertyCommandState(arg);
312 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
313 const SGPropertyNode * value =
314 ((PropertyCommandState *)(*state))->getValue();
316 switch (prop->getType()) {
317 case SGPropertyNode::BOOL:
318 return prop->setBoolValue(value->getBoolValue());
319 case SGPropertyNode::INT:
320 return prop->setIntValue(value->getIntValue());
321 case SGPropertyNode::LONG:
322 return prop->setLongValue(value->getLongValue());
323 case SGPropertyNode::FLOAT:
324 return prop->setFloatValue(value->getFloatValue());
325 case SGPropertyNode::DOUBLE:
326 return prop->setDoubleValue(value->getDoubleValue());
327 case SGPropertyNode::STRING:
328 return prop->setStringValue(value->getStringValue());
330 return prop->setUnspecifiedValue(value->getStringValue());
336 * Built-in command: increment or decrement a property value.
338 * property: the name of the property to increment or decrement.
339 * step: the amount of the increment or decrement.
342 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
345 *state = new PropertyCommandState(arg);
346 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
347 const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
349 switch (prop->getType()) {
350 case SGPropertyNode::BOOL:
351 if (step->getBoolValue())
352 return prop->setBoolValue(!prop->getBoolValue());
355 case SGPropertyNode::INT:
356 return prop->setIntValue(prop->getIntValue()
357 + step->getIntValue());
358 case SGPropertyNode::LONG:
359 return prop->setLongValue(prop->getLongValue()
360 + step->getLongValue());
361 case SGPropertyNode::FLOAT:
362 return prop->setFloatValue(prop->getFloatValue()
363 + step->getFloatValue());
364 case SGPropertyNode::DOUBLE:
365 case SGPropertyNode::UNSPECIFIED:
366 return prop->setDoubleValue(prop->getDoubleValue()
367 + step->getDoubleValue());
368 default: // doesn't make sense with strings
375 * Built-in command: multiply a property value.
377 * property: the name of the property to multiply.
378 * factor: the amount by which to multiply.
381 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
384 *state = new PropertyCommandState(arg);
385 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
386 const SGPropertyNode * factor =
387 ((PropertyCommandState *)(*state))->getFactor();
389 switch (prop->getType()) {
390 case SGPropertyNode::BOOL:
391 return prop->setBoolValue(prop->getBoolValue() &&
392 factor->getBoolValue());
393 case SGPropertyNode::INT:
394 return prop->setIntValue(int(prop->getIntValue()
395 * factor->getDoubleValue()));
396 case SGPropertyNode::LONG:
397 return prop->setLongValue(long(prop->getLongValue()
398 * factor->getDoubleValue()));
399 case SGPropertyNode::FLOAT:
400 return prop->setFloatValue(float(prop->getFloatValue()
401 * factor->getDoubleValue()));
402 case SGPropertyNode::DOUBLE:
403 case SGPropertyNode::UNSPECIFIED:
404 return prop->setDoubleValue(prop->getDoubleValue()
405 * factor->getDoubleValue());
406 default: // doesn't make sense with strings
413 * Built-in command: swap two property values.
415 * property[0]: the name of the first property.
416 * property[1]: the name of the second property.
419 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
422 *state = new PropertyCommandState(arg);
423 SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
424 SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
426 // FIXME: inefficient
427 const string & tmp = prop1->getStringValue();
428 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
429 prop2->setUnspecifiedValue(tmp));
434 * Set a property to an axis or other moving input.
436 * property: the name of the property to set.
437 * setting: the current input setting, usually between -1.0 and 1.0.
438 * offset: the offset to shift by, before applying the factor.
439 * factor: the factor to multiply by (use negative to reverse).
442 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
445 *state = new PropertyCommandState(arg);
446 SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
448 ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
450 ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
452 ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
454 return prop->setDoubleValue((setting + offset) * factor);
459 ////////////////////////////////////////////////////////////////////////
461 ////////////////////////////////////////////////////////////////////////
465 * Table of built-in commands.
467 * New commands do not have to be added here; any module in the application
468 * can add a new command using globals->get_commands()->addCommand(...).
472 SGCommandMgr::command_t command;
478 { "panel-load", do_panel_load },
479 { "preferences-load", do_preferences_load },
480 { "view-cycle", do_view_cycle },
481 { "screen-capture", do_screen_capture },
482 { "tile-cache-reload", do_tile_cache_reload },
483 { "lighting-update", do_lighting_update },
484 { "property-toggle", do_property_toggle },
485 { "property-assign", do_property_assign },
486 { "property-adjust", do_property_adjust },
487 { "property-multiply", do_property_multiply },
488 { "property-swap", do_property_swap },
489 { "property-scale", do_property_scale },
490 { 0, 0 } // zero-terminated
495 * Initialize the default built-in commands.
497 * Other commands may be added by other parts of the application.
502 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
503 for (int i = 0; built_ins[i].name != 0; i++) {
504 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
505 globals->get_commands()->addCommand(built_ins[i].name,
506 built_ins[i].command);
510 // end of fg_commands.hxx