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>
16 #include "fg_commands.hxx"
19 SG_USING_STD(ifstream);
20 SG_USING_STD(ofstream);
22 #include "fg_props.hxx"
24 #include "globals.hxx"
28 * Built-in command: do nothing.
31 do_null (const SGPropertyNode * arg)
38 * Built-in command: exit FlightGear.
40 * TODO: show a confirm dialog.
43 do_exit (const SGPropertyNode * arg)
52 * Built-in command: load flight.
54 * file (optional): the name of the file to load (relative to current
55 * directory). Defaults to "fgfs.sav".
58 do_load (const SGPropertyNode * arg)
60 const string &file = arg->getStringValue("file", "fgfs.sav");
61 ifstream input(file.c_str());
62 if (input.good() && fgLoadFlight(input)) {
64 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
67 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
74 * Built-in command: save flight.
76 * file (optional): the name of the file to save (relative to the
77 * current directory). Defaults to "fgfs.sav".
80 do_save (const SGPropertyNode * arg)
82 const string &file = arg->getStringValue("file", "fgfs.sav");
83 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
84 ofstream output(file.c_str());
85 if (output.good() && fgSaveFlight(output)) {
87 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
90 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
97 * Built-in command: (re)load the panel.
99 * path (optional): the file name to load the panel from
100 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
101 * and if that's unspecified, to "Panels/Default/default.xml".
104 do_panel_load (const SGPropertyNode * arg)
107 arg->getStringValue("path",
108 fgGetString("/sim/panel/path",
109 "Panels/Default/default.xml"));
110 FGPanel * new_panel = fgReadPanel(panel_path);
111 if (new_panel == 0) {
112 SG_LOG(SG_INPUT, SG_ALERT,
113 "Error reading new panel from " << panel_path);
116 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
117 current_panel->unbind();
118 delete current_panel;
119 current_panel = new_panel;
120 current_panel->bind();
126 * Built-in command: (re)load preferences.
128 * path (optional): the file name to load the panel from (relative
129 * to FG_ROOT). Defaults to "preferences.xml".
132 do_preferences_load (const SGPropertyNode * arg)
134 const string &path = arg->getStringValue("path", "preferences.xml");
135 SGPath props_path(globals->get_fg_root());
136 props_path.append(path);
137 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
138 << props_path.str());
139 if (!readProperties(props_path.str(), globals->get_props())) {
140 SG_LOG(SG_INPUT, SG_ALERT, "Failed to reread global preferences");
143 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
150 * Built-in command: cycle view.
153 do_view_cycle (const SGPropertyNode * arg)
155 globals->get_current_view()->set_view_offset(0.0);
156 globals->set_current_view(globals->get_viewmgr()->next_view());
157 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
163 * Built-in command: capture screen.
166 do_screen_capture (const SGPropertyNode * arg)
174 * Built-in command: toggle a bool property value.
176 * property: The name of the property to toggle.
179 do_property_toggle (const SGPropertyNode * arg)
181 const string & propname = arg->getStringValue("property", "");
185 SGPropertyNode * node = fgGetNode(propname);
186 return node->setBoolValue(!node->getBoolValue());
191 * Built-in command: assign a value to a property.
193 * property: the name of the property to assign.
194 * value: the value to assign.
197 do_property_assign (const SGPropertyNode * arg)
199 const string & propname = arg->getStringValue("property", "");
203 SGPropertyNode * node = fgGetNode(propname, true);
205 switch (node->getType()) {
207 return node->setBoolValue(arg->getBoolValue("value"));
209 return node->setIntValue(arg->getIntValue("value"));
211 return node->setLongValue(arg->getLongValue("value"));
213 return node->setFloatValue(arg->getFloatValue("value"));
214 case SGValue::DOUBLE:
215 return node->setDoubleValue(arg->getDoubleValue("value"));
216 case SGValue::STRING:
217 return node->setStringValue(arg->getStringValue("value"));
219 return node->setUnknownValue(arg->getStringValue("value"));
225 * Built-in command: increment or decrement a property value.
227 * property: the name of the property to increment or decrement.
228 * step: the amount of the increment or decrement.
231 do_property_adjust (const SGPropertyNode * arg)
233 const string & propname = arg->getStringValue("property", "");
237 SGPropertyNode * node = fgGetNode(propname, true);
239 switch (node->getType()) {
241 if (arg->getBoolValue("step"))
242 return node->setBoolValue(!node->getBoolValue());
246 return node->setIntValue(node->getIntValue()
247 + arg->getIntValue("step"));
249 return node->setLongValue(node->getLongValue()
250 + arg->getLongValue("step"));
252 return node->setFloatValue(node->getFloatValue()
253 + arg->getFloatValue("step"));
254 case SGValue::DOUBLE:
255 return node->setDoubleValue(node->getDoubleValue()
256 + arg->getDoubleValue("step"));
257 default: // doesn't make sense with strings
264 * Built-in command: swap two property values.
266 * property[0]: the name of the first property.
267 * property[1]: the name of the second property.
270 do_property_swap (const SGPropertyNode * arg)
272 const string &propname1 = arg->getStringValue("property[0]", "");
273 const string &propname2 = arg->getStringValue("property[1]", "");
274 if (propname1 == "" || propname2 == "")
277 SGPropertyNode * node1 = fgGetNode(propname1, true);
278 SGPropertyNode * node2 = fgGetNode(propname2, true);
279 const string & tmp = node1->getStringValue();
280 return (node1->setUnknownValue(node2->getStringValue()) &&
281 node2->setUnknownValue(tmp));
286 * Set a property to an axis or other moving input.
288 * property: the name of the property to set.
289 * setting: the current input setting, usually between -1.0 and 1.0.
290 * offset: the offset to shift by, before applying the factor.
291 * factor: the factor to multiply by (use negative to reverse).
294 do_property_scale (const SGPropertyNode * arg)
296 const string &propname = arg->getStringValue("property");
297 double setting = arg->getDoubleValue("setting", 0.0);
298 double offset = arg->getDoubleValue("offset", 0.0);
299 double factor = arg->getDoubleValue("factor", 1.0);
300 return fgSetDouble(propname, (setting + offset) * factor);
306 * Table of built-in commands.
308 * New commands do not have to be added here; any module in the application
309 * can add a new command using globals->get_commands()->addCommand(...).
313 SGCommandMgr::command_t command;
319 "panel-load", do_panel_load,
320 "preferences-load", do_preferences_load,
321 "view-cycle", do_view_cycle,
322 "screen-capture", do_screen_capture,
323 "property-toggle", do_property_toggle,
324 "property-assign", do_property_assign,
325 "property-adjust", do_property_adjust,
326 "property-swap", do_property_swap,
327 "property-scale", do_property_scale,
328 0, 0 // zero-terminated
333 * Initialize the default built-in commands.
335 * Other commands may be added by other parts of the application.
340 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
341 for (int i = 0; built_ins[i].name != 0; i++) {
342 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
343 globals->get_commands()->addCommand(built_ins[i].name,
344 built_ins[i].command);
348 // end of fg_commands.hxx