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 * Built-in command: do nothing.
35 do_null (const SGPropertyNode * arg, SGCommandState ** state)
42 * Built-in command: exit FlightGear.
44 * TODO: show a confirm dialog.
47 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
49 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
56 * Built-in command: load flight.
58 * file (optional): the name of the file to load (relative to current
59 * directory). Defaults to "fgfs.sav".
62 do_load (const SGPropertyNode * arg, SGCommandState ** state)
64 const string &file = arg->getStringValue("file", "fgfs.sav");
65 ifstream input(file.c_str());
66 if (input.good() && fgLoadFlight(input)) {
68 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
71 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
78 * Built-in command: save flight.
80 * file (optional): the name of the file to save (relative to the
81 * current directory). Defaults to "fgfs.sav".
84 do_save (const SGPropertyNode * arg, SGCommandState ** state)
86 const string &file = arg->getStringValue("file", "fgfs.sav");
87 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
88 ofstream output(file.c_str());
89 if (output.good() && fgSaveFlight(output)) {
91 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
94 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
101 * Built-in command: (re)load the panel.
103 * path (optional): the file name to load the panel from
104 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
105 * and if that's unspecified, to "Panels/Default/default.xml".
108 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
111 arg->getStringValue("path",
112 fgGetString("/sim/panel/path",
113 "Panels/Default/default.xml"));
114 FGPanel * new_panel = fgReadPanel(panel_path);
115 if (new_panel == 0) {
116 SG_LOG(SG_INPUT, SG_ALERT,
117 "Error reading new panel from " << panel_path);
120 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
121 current_panel->unbind();
122 delete current_panel;
123 current_panel = new_panel;
124 current_panel->bind();
130 * Built-in command: (re)load preferences.
132 * path (optional): the file name to load the panel from (relative
133 * to FG_ROOT). Defaults to "preferences.xml".
136 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
138 const string &path = arg->getStringValue("path", "preferences.xml");
139 SGPath props_path(globals->get_fg_root());
140 props_path.append(path);
141 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
142 << props_path.str());
143 if (!readProperties(props_path.str(), globals->get_props())) {
144 SG_LOG(SG_INPUT, SG_ALERT, "Failed to reread global preferences");
147 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
154 * Built-in command: cycle view.
157 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
159 globals->get_current_view()->set_view_offset(0.0);
160 globals->set_current_view(globals->get_viewmgr()->next_view());
161 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
167 * Built-in command: capture screen.
170 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
178 * Reload the tile cache.
181 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
183 bool freeze = globals->get_freeze();
184 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
186 globals->set_freeze( true );
188 if ( global_tile_mgr.init() ) {
189 // Load the local scenery data
190 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
191 fgGetDouble("/position/latitude-deg"));
193 SG_LOG( SG_GENERAL, SG_ALERT,
194 "Error in Tile Manager initialization!" );
199 globals->set_freeze( false );
205 * Update the lighting manually.
208 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
210 fgUpdateSkyAndLightingParams();
216 * Built-in command: toggle a bool property value.
218 * property: The name of the property to toggle.
221 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
223 const string & propname = arg->getStringValue("property", "");
227 SGPropertyNode * node = fgGetNode(propname);
228 return node->setBoolValue(!node->getBoolValue());
233 * Built-in command: assign a value to a property.
235 * property: the name of the property to assign.
236 * value: the value to assign.
239 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
241 const string & propname = arg->getStringValue("property", "");
245 SGPropertyNode * node = fgGetNode(propname, true);
247 switch (node->getType()) {
248 case SGPropertyNode::BOOL:
249 return node->setBoolValue(arg->getBoolValue("value"));
250 case SGPropertyNode::INT:
251 return node->setIntValue(arg->getIntValue("value"));
252 case SGPropertyNode::LONG:
253 return node->setLongValue(arg->getLongValue("value"));
254 case SGPropertyNode::FLOAT:
255 return node->setFloatValue(arg->getFloatValue("value"));
256 case SGPropertyNode::DOUBLE:
257 return node->setDoubleValue(arg->getDoubleValue("value"));
258 case SGPropertyNode::STRING:
259 return node->setStringValue(arg->getStringValue("value"));
261 return node->setUnspecifiedValue(arg->getStringValue("value"));
267 * Built-in command: increment or decrement a property value.
269 * property: the name of the property to increment or decrement.
270 * step: the amount of the increment or decrement.
273 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
275 const string & propname = arg->getStringValue("property", "");
279 SGPropertyNode * node = fgGetNode(propname, true);
281 switch (node->getType()) {
282 case SGPropertyNode::BOOL:
283 if (arg->getBoolValue("step"))
284 return node->setBoolValue(!node->getBoolValue());
287 case SGPropertyNode::INT:
288 return node->setIntValue(node->getIntValue()
289 + arg->getIntValue("step"));
290 case SGPropertyNode::LONG:
291 return node->setLongValue(node->getLongValue()
292 + arg->getLongValue("step"));
293 case SGPropertyNode::FLOAT:
294 return node->setFloatValue(node->getFloatValue()
295 + arg->getFloatValue("step"));
296 case SGPropertyNode::DOUBLE:
297 case SGPropertyNode::UNSPECIFIED:
298 return node->setDoubleValue(node->getDoubleValue()
299 + arg->getDoubleValue("step"));
300 default: // doesn't make sense with strings
307 <<<<<<< fg_commands.cxx
308 * Built-in command: multiply a property value.
310 * property: the name of the property to multiply.
311 * factor: the amount by which to multiply.
314 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
316 const string & propname = arg->getStringValue("property", "");
320 SGPropertyNode * node = fgGetNode(propname, true);
322 switch (node->getType()) {
323 case SGPropertyNode::BOOL:
324 return node->setBoolValue(node->getBoolValue() &&
325 arg->getBoolValue("factor"));
326 case SGPropertyNode::INT:
327 return node->setIntValue(int(node->getIntValue()
328 * arg->getDoubleValue("factor")));
329 case SGPropertyNode::LONG:
330 return node->setLongValue(long(node->getLongValue()
331 * arg->getDoubleValue("factor")));
332 case SGPropertyNode::FLOAT:
333 return node->setFloatValue(float(node->getFloatValue()
334 * arg->getDoubleValue("factor")));
335 case SGPropertyNode::DOUBLE:
336 case SGPropertyNode::UNSPECIFIED:
337 return node->setDoubleValue(node->getDoubleValue()
338 * arg->getDoubleValue("factor"));
339 default: // doesn't make sense with strings
347 * Built-in command: multiply a property value.
349 * property: the name of the property to multiply.
350 * factor: the amount by which to multiply.
353 do_property_multiply (const SGPropertyNode * arg)
355 const string & propname = arg->getStringValue("property", "");
359 SGPropertyNode * node = fgGetNode(propname, true);
361 switch (node->getType()) {
362 case SGPropertyNode::BOOL:
363 return node->setBoolValue(node->getBoolValue() &&
364 arg->getBoolValue("factor"));
365 case SGPropertyNode::INT:
366 return node->setIntValue(int(node->getIntValue()
367 * arg->getDoubleValue("factor")));
368 case SGPropertyNode::LONG:
369 return node->setLongValue(long(node->getLongValue()
370 * arg->getDoubleValue("factor")));
371 case SGPropertyNode::FLOAT:
372 return node->setFloatValue(float(node->getFloatValue()
373 * arg->getDoubleValue("factor")));
374 case SGPropertyNode::DOUBLE:
375 case SGPropertyNode::UNSPECIFIED:
376 return node->setDoubleValue(node->getDoubleValue()
377 * arg->getDoubleValue("factor"));
378 default: // doesn't make sense with strings
385 * Built-in command: swap two property values.
387 * property[0]: the name of the first property.
388 * property[1]: the name of the second property.
391 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
393 const string &propname1 = arg->getStringValue("property[0]", "");
394 const string &propname2 = arg->getStringValue("property[1]", "");
395 if (propname1 == "" || propname2 == "")
398 SGPropertyNode * node1 = fgGetNode(propname1, true);
399 SGPropertyNode * node2 = fgGetNode(propname2, true);
400 const string & tmp = node1->getStringValue();
401 return (node1->setUnspecifiedValue(node2->getStringValue()) &&
402 node2->setUnspecifiedValue(tmp));
407 * Set a property to an axis or other moving input.
409 * property: the name of the property to set.
410 * setting: the current input setting, usually between -1.0 and 1.0.
411 * offset: the offset to shift by, before applying the factor.
412 * factor: the factor to multiply by (use negative to reverse).
415 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
417 const string &propname = arg->getStringValue("property");
418 double setting = arg->getDoubleValue("setting", 0.0);
419 double offset = arg->getDoubleValue("offset", 0.0);
420 double factor = arg->getDoubleValue("factor", 1.0);
421 return fgSetDouble(propname, (setting + offset) * factor);
427 * Table of built-in commands.
429 * New commands do not have to be added here; any module in the application
430 * can add a new command using globals->get_commands()->addCommand(...).
434 SGCommandMgr::command_t command;
440 { "panel-load", do_panel_load },
441 { "preferences-load", do_preferences_load },
442 { "view-cycle", do_view_cycle },
443 { "screen-capture", do_screen_capture },
444 { "tile-cache-reload", do_tile_cache_reload },
445 { "lighting-update", do_lighting_update },
446 { "property-toggle", do_property_toggle },
447 { "property-assign", do_property_assign },
448 { "property-adjust", do_property_adjust },
449 { "property-multiply", do_property_multiply },
450 { "property-swap", do_property_swap },
451 { "property-scale", do_property_scale },
452 { 0, 0 } // zero-terminated
457 * Initialize the default built-in commands.
459 * Other commands may be added by other parts of the application.
464 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
465 for (int i = 0; built_ins[i].name != 0; i++) {
466 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
467 globals->get_commands()->addCommand(built_ins[i].name,
468 built_ins[i].command);
472 // end of fg_commands.hxx