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 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
20 SG_USING_STD(ifstream);
21 SG_USING_STD(ofstream);
24 #include "fg_props.hxx"
26 #include "globals.hxx"
30 * Built-in command: do nothing.
33 do_null (const SGPropertyNode * arg)
40 * Built-in command: exit FlightGear.
42 * TODO: show a confirm dialog.
45 do_exit (const SGPropertyNode * arg)
54 * Built-in command: load flight.
56 * file (optional): the name of the file to load (relative to current
57 * directory). Defaults to "fgfs.sav".
60 do_load (const SGPropertyNode * arg)
62 const string &file = arg->getStringValue("file", "fgfs.sav");
63 ifstream input(file.c_str());
64 if (input.good() && fgLoadFlight(input)) {
66 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
69 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
76 * Built-in command: save flight.
78 * file (optional): the name of the file to save (relative to the
79 * current directory). Defaults to "fgfs.sav".
82 do_save (const SGPropertyNode * arg)
84 const string &file = arg->getStringValue("file", "fgfs.sav");
85 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
86 ofstream output(file.c_str());
87 if (output.good() && fgSaveFlight(output)) {
89 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
92 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
99 * Built-in command: (re)load the panel.
101 * path (optional): the file name to load the panel from
102 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
103 * and if that's unspecified, to "Panels/Default/default.xml".
106 do_panel_load (const SGPropertyNode * arg)
109 arg->getStringValue("path",
110 fgGetString("/sim/panel/path",
111 "Panels/Default/default.xml"));
112 FGPanel * new_panel = fgReadPanel(panel_path);
113 if (new_panel == 0) {
114 SG_LOG(SG_INPUT, SG_ALERT,
115 "Error reading new panel from " << panel_path);
118 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
119 current_panel->unbind();
120 delete current_panel;
121 current_panel = new_panel;
122 current_panel->bind();
128 * Built-in command: (re)load preferences.
130 * path (optional): the file name to load the panel from (relative
131 * to FG_ROOT). Defaults to "preferences.xml".
134 do_preferences_load (const SGPropertyNode * arg)
136 const string &path = arg->getStringValue("path", "preferences.xml");
137 SGPath props_path(globals->get_fg_root());
138 props_path.append(path);
139 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
140 << props_path.str());
141 if (!readProperties(props_path.str(), globals->get_props())) {
142 SG_LOG(SG_INPUT, SG_ALERT, "Failed to reread global preferences");
145 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
152 * Built-in command: cycle view.
155 do_view_cycle (const SGPropertyNode * arg)
157 globals->get_current_view()->set_view_offset(0.0);
158 globals->set_current_view(globals->get_viewmgr()->next_view());
159 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
165 * Built-in command: capture screen.
168 do_screen_capture (const SGPropertyNode * arg)
176 * Built-in command: toggle a bool property value.
178 * property: The name of the property to toggle.
181 do_property_toggle (const SGPropertyNode * arg)
183 const string & propname = arg->getStringValue("property", "");
187 SGPropertyNode * node = fgGetNode(propname);
188 return node->setBoolValue(!node->getBoolValue());
193 * Built-in command: assign a value to a property.
195 * property: the name of the property to assign.
196 * value: the value to assign.
199 do_property_assign (const SGPropertyNode * arg)
201 const string & propname = arg->getStringValue("property", "");
205 SGPropertyNode * node = fgGetNode(propname, true);
207 switch (node->getType()) {
208 case SGPropertyNode::BOOL:
209 return node->setBoolValue(arg->getBoolValue("value"));
210 case SGPropertyNode::INT:
211 return node->setIntValue(arg->getIntValue("value"));
212 case SGPropertyNode::LONG:
213 return node->setLongValue(arg->getLongValue("value"));
214 case SGPropertyNode::FLOAT:
215 return node->setFloatValue(arg->getFloatValue("value"));
216 case SGPropertyNode::DOUBLE:
217 return node->setDoubleValue(arg->getDoubleValue("value"));
218 case SGPropertyNode::STRING:
219 return node->setStringValue(arg->getStringValue("value"));
221 return node->setUnknownValue(arg->getStringValue("value"));
227 * Built-in command: increment or decrement a property value.
229 * property: the name of the property to increment or decrement.
230 * step: the amount of the increment or decrement.
233 do_property_adjust (const SGPropertyNode * arg)
235 const string & propname = arg->getStringValue("property", "");
239 SGPropertyNode * node = fgGetNode(propname, true);
241 switch (node->getType()) {
242 case SGPropertyNode::BOOL:
243 if (arg->getBoolValue("step"))
244 return node->setBoolValue(!node->getBoolValue());
247 case SGPropertyNode::INT:
248 return node->setIntValue(node->getIntValue()
249 + arg->getIntValue("step"));
250 case SGPropertyNode::LONG:
251 return node->setLongValue(node->getLongValue()
252 + arg->getLongValue("step"));
253 case SGPropertyNode::FLOAT:
254 return node->setFloatValue(node->getFloatValue()
255 + arg->getFloatValue("step"));
256 case SGPropertyNode::DOUBLE:
257 return node->setDoubleValue(node->getDoubleValue()
258 + arg->getDoubleValue("step"));
259 default: // doesn't make sense with strings
266 * Built-in command: swap two property values.
268 * property[0]: the name of the first property.
269 * property[1]: the name of the second property.
272 do_property_swap (const SGPropertyNode * arg)
274 const string &propname1 = arg->getStringValue("property[0]", "");
275 const string &propname2 = arg->getStringValue("property[1]", "");
276 if (propname1 == "" || propname2 == "")
279 SGPropertyNode * node1 = fgGetNode(propname1, true);
280 SGPropertyNode * node2 = fgGetNode(propname2, true);
281 const string & tmp = node1->getStringValue();
282 return (node1->setUnknownValue(node2->getStringValue()) &&
283 node2->setUnknownValue(tmp));
288 * Set a property to an axis or other moving input.
290 * property: the name of the property to set.
291 * setting: the current input setting, usually between -1.0 and 1.0.
292 * offset: the offset to shift by, before applying the factor.
293 * factor: the factor to multiply by (use negative to reverse).
296 do_property_scale (const SGPropertyNode * arg)
298 const string &propname = arg->getStringValue("property");
299 double setting = arg->getDoubleValue("setting", 0.0);
300 double offset = arg->getDoubleValue("offset", 0.0);
301 double factor = arg->getDoubleValue("factor", 1.0);
302 return fgSetDouble(propname, (setting + offset) * factor);
308 * Table of built-in commands.
310 * New commands do not have to be added here; any module in the application
311 * can add a new command using globals->get_commands()->addCommand(...).
315 SGCommandMgr::command_t command;
321 { "panel-load", do_panel_load },
322 { "preferences-load", do_preferences_load },
323 { "view-cycle", do_view_cycle },
324 { "screen-capture", do_screen_capture },
325 { "property-toggle", do_property_toggle },
326 { "property-assign", do_property_assign },
327 { "property-adjust", do_property_adjust },
328 { "property-swap", do_property_swap },
329 { "property-scale", do_property_scale },
330 { 0, 0 } // zero-terminated
335 * Initialize the default built-in commands.
337 * Other commands may be added by other parts of the application.
342 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
343 for (int i = 0; built_ins[i].name != 0; i++) {
344 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
345 globals->get_commands()->addCommand(built_ins[i].name,
346 built_ins[i].command);
350 // end of fg_commands.hxx