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)
42 * Built-in command: exit FlightGear.
44 * TODO: show a confirm dialog.
47 do_exit (const SGPropertyNode * arg)
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)
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)
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)
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)
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)
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)
178 * Reload the tile cache.
181 do_tile_cache_reload (const SGPropertyNode * arg)
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"),
191 fgGetDouble("/position/latitude"));
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)
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)
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)
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->setUnknownValue(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)
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::UNKNOWN:
298 return node->setDoubleValue(node->getDoubleValue()
299 + arg->getDoubleValue("step"));
300 default: // doesn't make sense with strings
307 * Built-in command: multiply a property value.
309 * property: the name of the property to multiply.
310 * factor: the amount by which to multiply.
313 do_property_multiply (const SGPropertyNode * arg)
315 const string & propname = arg->getStringValue("property", "");
319 SGPropertyNode * node = fgGetNode(propname, true);
321 switch (node->getType()) {
322 case SGPropertyNode::BOOL:
323 return node->setBoolValue(node->getBoolValue() &&
324 arg->getBoolValue("factor"));
325 case SGPropertyNode::INT:
326 return node->setIntValue(int(node->getIntValue()
327 * arg->getDoubleValue("factor")));
328 case SGPropertyNode::LONG:
329 return node->setLongValue(long(node->getLongValue()
330 * arg->getDoubleValue("factor")));
331 case SGPropertyNode::FLOAT:
332 return node->setFloatValue(float(node->getFloatValue()
333 * arg->getDoubleValue("factor")));
334 case SGPropertyNode::DOUBLE:
335 case SGPropertyNode::UNKNOWN:
336 return node->setDoubleValue(node->getDoubleValue()
337 * arg->getDoubleValue("factor"));
338 default: // doesn't make sense with strings
345 * Built-in command: swap two property values.
347 * property[0]: the name of the first property.
348 * property[1]: the name of the second property.
351 do_property_swap (const SGPropertyNode * arg)
353 const string &propname1 = arg->getStringValue("property[0]", "");
354 const string &propname2 = arg->getStringValue("property[1]", "");
355 if (propname1 == "" || propname2 == "")
358 SGPropertyNode * node1 = fgGetNode(propname1, true);
359 SGPropertyNode * node2 = fgGetNode(propname2, true);
360 const string & tmp = node1->getStringValue();
361 return (node1->setUnknownValue(node2->getStringValue()) &&
362 node2->setUnknownValue(tmp));
367 * Set a property to an axis or other moving input.
369 * property: the name of the property to set.
370 * setting: the current input setting, usually between -1.0 and 1.0.
371 * offset: the offset to shift by, before applying the factor.
372 * factor: the factor to multiply by (use negative to reverse).
375 do_property_scale (const SGPropertyNode * arg)
377 const string &propname = arg->getStringValue("property");
378 double setting = arg->getDoubleValue("setting", 0.0);
379 double offset = arg->getDoubleValue("offset", 0.0);
380 double factor = arg->getDoubleValue("factor", 1.0);
381 return fgSetDouble(propname, (setting + offset) * factor);
387 * Table of built-in commands.
389 * New commands do not have to be added here; any module in the application
390 * can add a new command using globals->get_commands()->addCommand(...).
394 SGCommandMgr::command_t command;
400 { "panel-load", do_panel_load },
401 { "preferences-load", do_preferences_load },
402 { "view-cycle", do_view_cycle },
403 { "screen-capture", do_screen_capture },
404 { "tile-cache-reload", do_tile_cache_reload },
405 { "lighting-update", do_lighting_update },
406 { "property-toggle", do_property_toggle },
407 { "property-assign", do_property_assign },
408 { "property-adjust", do_property_adjust },
409 { "property-multiply", do_property_multiply },
410 { "property-swap", do_property_swap },
411 { "property-scale", do_property_scale },
412 { 0, 0 } // zero-terminated
417 * Initialize the default built-in commands.
419 * Other commands may be added by other parts of the application.
424 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
425 for (int i = 0; built_ins[i].name != 0; i++) {
426 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
427 globals->get_commands()->addCommand(built_ins[i].name,
428 built_ins[i].command);
432 // end of fg_commands.hxx