1 // fg_commands.cxx - internal FGFS commands.
3 #include <string.h> // strcmp()
5 #include <simgear/compiler.h>
6 #include <simgear/misc/exception.hxx>
11 #include <simgear/debug/logstream.hxx>
12 #include <simgear/misc/commands.hxx>
13 #include <simgear/misc/props.hxx>
16 #include <GUI/new_gui.hxx>
17 #include <Cockpit/panel.hxx>
18 #include <Cockpit/panel_io.hxx>
19 #include <Scenery/tilemgr.hxx>
20 #include <Time/tmp.hxx>
22 #include "fg_commands.hxx"
25 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
26 SG_USING_STD(ifstream);
27 SG_USING_STD(ofstream);
30 #include "fg_props.hxx"
32 #include "globals.hxx"
33 #include "viewmgr.hxx"
37 ////////////////////////////////////////////////////////////////////////
38 // Static helper functions.
39 ////////////////////////////////////////////////////////////////////////
43 * Template function to wrap a value.
47 do_wrap (T * value, T min, T max)
49 if (min >= max) { // basic sanity check
60 static inline SGPropertyNode *
61 get_prop (const SGPropertyNode * arg)
63 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
66 static inline SGPropertyNode *
67 get_prop2 (const SGPropertyNode * arg)
69 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
74 ////////////////////////////////////////////////////////////////////////
75 // Command implementations.
76 ////////////////////////////////////////////////////////////////////////
80 * Built-in command: do nothing.
83 do_null (const SGPropertyNode * arg)
90 * Built-in command: exit FlightGear.
92 * TODO: show a confirm dialog.
95 do_exit (const SGPropertyNode * arg)
97 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
104 * Built-in command: load flight.
106 * file (optional): the name of the file to load (relative to current
107 * directory). Defaults to "fgfs.sav".
110 do_load (const SGPropertyNode * arg)
112 const string &file = arg->getStringValue("file", "fgfs.sav");
113 ifstream input(file.c_str());
114 if (input.good() && fgLoadFlight(input)) {
116 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
119 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
126 * Built-in command: save flight.
128 * file (optional): the name of the file to save (relative to the
129 * current directory). Defaults to "fgfs.sav".
132 do_save (const SGPropertyNode * arg)
134 const string &file = arg->getStringValue("file", "fgfs.sav");
135 bool write_all = arg->getBoolValue("write-all", false);
136 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
137 ofstream output(file.c_str());
138 if (output.good() && fgSaveFlight(output, write_all)) {
140 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
143 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
150 * Built-in command: (re)load the panel.
152 * path (optional): the file name to load the panel from
153 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
154 * and if that's unspecified, to "Panels/Default/default.xml".
157 do_panel_load (const SGPropertyNode * arg)
160 arg->getStringValue("path",
161 fgGetString("/sim/panel/path",
162 "Panels/Default/default.xml"));
163 FGPanel * new_panel = fgReadPanel(panel_path);
164 if (new_panel == 0) {
165 SG_LOG(SG_INPUT, SG_ALERT,
166 "Error reading new panel from " << panel_path);
169 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
170 current_panel->unbind();
171 delete current_panel;
172 current_panel = new_panel;
173 current_panel->bind();
179 * Built-in command: pass a mouse click to the panel.
181 * button: the mouse button number, zero-based.
182 * is-down: true if the button is down, false if it is up.
183 * x-pos: the x position of the mouse click.
184 * y-pos: the y position of the mouse click.
187 do_panel_mouse_click (const SGPropertyNode * arg)
189 if (current_panel != 0)
191 ->doMouseAction(arg->getIntValue("button"),
192 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
193 arg->getIntValue("x-pos"),
194 arg->getIntValue("y-pos"));
201 * Built-in command: (re)load preferences.
203 * path (optional): the file name to load the panel from (relative
204 * to FG_ROOT). Defaults to "preferences.xml".
207 do_preferences_load (const SGPropertyNode * arg)
210 fgLoadProps(arg->getStringValue("path", "preferences.xml"),
211 globals->get_props());
212 } catch (const sg_exception &e) {
213 guiErrorMessage("Error reading global preferences: ", e);
216 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
224 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
225 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
226 if ( globals->get_viewmgr()->get_current() == 1 ) {
227 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
235 globals->get_current_view()->setHeadingOffset_deg(0.0);
236 globals->get_viewmgr()->next_view();
237 fix_hud_visibility();
238 global_tile_mgr.refresh_view_timestamps();
244 globals->get_current_view()->setHeadingOffset_deg(0.0);
245 globals->get_viewmgr()->prev_view();
246 fix_hud_visibility();
247 global_tile_mgr.refresh_view_timestamps();
251 * Built-in command: cycle view.
254 do_view_cycle (const SGPropertyNode * arg)
256 globals->get_current_view()->setHeadingOffset_deg(0.0);
257 globals->get_viewmgr()->next_view();
258 fix_hud_visibility();
259 global_tile_mgr.refresh_view_timestamps();
260 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
265 * Built-in command: capture screen.
268 do_screen_capture (const SGPropertyNode * arg)
276 * Reload the tile cache.
279 do_tile_cache_reload (const SGPropertyNode * arg)
281 static const SGPropertyNode *master_freeze
282 = fgGetNode("/sim/freeze/master");
283 bool freeze = master_freeze->getBoolValue();
284 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
286 fgSetBool("/sim/freeze/master", true);
289 if ( global_tile_mgr.init() ) {
290 // Load the local scenery data
291 double visibility_meters = fgGetDouble("/environment/visibility-m");
292 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
293 fgGetDouble("/position/latitude-deg"),
296 SG_LOG( SG_GENERAL, SG_ALERT,
297 "Error in Tile Manager initialization!" );
302 fgSetBool("/sim/freeze/master", false);
309 * Update the lighting manually.
312 do_lighting_update (const SGPropertyNode * arg)
314 fgUpdateSkyAndLightingParams();
320 * Built-in command: toggle a bool property value.
322 * property: The name of the property to toggle.
325 do_property_toggle (const SGPropertyNode * arg)
327 SGPropertyNode * prop = get_prop(arg);
328 return prop->setBoolValue(!prop->getBoolValue());
333 * Built-in command: assign a value to a property.
335 * property: the name of the property to assign.
336 * value: the value to assign.
339 do_property_assign (const SGPropertyNode * arg)
341 SGPropertyNode * prop = get_prop(arg);
342 const SGPropertyNode * value = arg->getNode("value");
344 switch (prop->getType()) {
346 case SGPropertyNode::BOOL:
347 return prop->setBoolValue(value->getBoolValue());
349 case SGPropertyNode::INT:
350 return prop->setIntValue(value->getIntValue());
352 case SGPropertyNode::LONG:
353 return prop->setLongValue(value->getLongValue());
355 case SGPropertyNode::FLOAT:
356 return prop->setFloatValue(value->getFloatValue());
358 case SGPropertyNode::DOUBLE:
359 return prop->setDoubleValue(value->getDoubleValue());
361 case SGPropertyNode::STRING:
362 return prop->setStringValue(value->getStringValue());
365 return prop->setUnspecifiedValue(value->getStringValue());
372 * Built-in command: increment or decrement a property value.
374 * property: the name of the property to increment or decrement.
375 * step: the amount of the increment or decrement (default: 0).
376 * offset: a normalized amount to offset by (if step is not present).
377 * factor: the amount by which to multiply the offset (if step is not present).
378 * min: the minimum allowed value (default: no minimum).
379 * max: the maximum allowed value (default: no maximum).
380 * wrap: true if the value should be wrapped when it passes min or max;
381 * both min and max must be present for this to work (default:
385 do_property_adjust (const SGPropertyNode * arg)
387 SGPropertyNode * prop = get_prop(arg);
388 const SGPropertyNode * step = arg->getChild("step");
389 const SGPropertyNode * offset = arg->getChild("offset");
390 const SGPropertyNode * factor = arg->getChild("factor");
391 const SGPropertyNode * min = arg->getChild("min");
392 const SGPropertyNode * max = arg->getChild("max");
393 bool wrap = arg->getBoolValue("wrap");
395 if (min == 0 || max == 0)
400 amount = offset->getDoubleValue() * factor->getDoubleValue();
402 switch (prop->getType()) {
404 case SGPropertyNode::BOOL: {
407 value = step->getBoolValue();
409 value = (0.0 != amount);
411 return prop->setBoolValue(!prop->getBoolValue());
416 case SGPropertyNode::INT: {
419 value = prop->getIntValue() + step->getIntValue();
421 value = prop->getIntValue() + int(amount);
423 do_wrap(&value, min->getIntValue(), max->getIntValue());
425 if (min != 0 && value < min->getIntValue())
426 value = min->getIntValue();
427 if (max != 0 && value > max->getIntValue())
428 value = max->getIntValue();
430 return prop->setIntValue(value);
433 case SGPropertyNode::LONG: {
436 value = prop->getLongValue() + step->getLongValue();
438 value = prop->getLongValue() + long(amount);
440 do_wrap(&value, min->getLongValue(), max->getLongValue());
442 if (min != 0 && value < min->getLongValue())
443 value = min->getLongValue();
444 if (max != 0 && value > max->getLongValue())
445 value = max->getLongValue();
447 return prop->setLongValue(value);
450 case SGPropertyNode::FLOAT: {
453 value = prop->getFloatValue() + step->getFloatValue();
455 value = prop->getFloatValue() + float(amount);
457 do_wrap(&value, min->getFloatValue(), max->getFloatValue());
459 if (min != 0 && value < min->getFloatValue())
460 value = min->getFloatValue();
461 if (max != 0 && value > max->getFloatValue())
462 value = max->getFloatValue();
464 return prop->setFloatValue(value);
467 case SGPropertyNode::DOUBLE:
468 case SGPropertyNode::UNSPECIFIED:
469 case SGPropertyNode::NONE: {
472 value = prop->getDoubleValue() + step->getDoubleValue();
474 value = prop->getDoubleValue() + amount;
476 do_wrap(&value, min->getDoubleValue(), max->getDoubleValue());
478 if (min != 0 && value < min->getDoubleValue())
479 value = min->getDoubleValue();
480 if (max != 0 && value > max->getDoubleValue())
481 value = max->getDoubleValue();
483 return prop->setDoubleValue(value);
486 case SGPropertyNode::STRING: // doesn't make sense with strings
487 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
491 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
499 * Built-in command: multiply a property value.
501 * property: the name of the property to multiply.
502 * factor: the amount by which to multiply.
505 do_property_multiply (const SGPropertyNode * arg)
507 SGPropertyNode * prop = get_prop(arg);
508 const SGPropertyNode * factor = arg->getChild("factor");
513 switch (prop->getType()) {
515 case SGPropertyNode::BOOL:
516 return prop->setBoolValue(prop->getBoolValue() && factor->getBoolValue());
518 case SGPropertyNode::INT:
519 return prop->setIntValue(int(prop->getIntValue()
520 * factor->getDoubleValue()));
522 case SGPropertyNode::LONG:
523 return prop->setLongValue(long(prop->getLongValue()
524 * factor->getDoubleValue()));
526 case SGPropertyNode::FLOAT:
527 return prop->setFloatValue(float(prop->getFloatValue()
528 * factor->getDoubleValue()));
530 case SGPropertyNode::DOUBLE:
531 case SGPropertyNode::UNSPECIFIED:
532 case SGPropertyNode::NONE:
533 return prop->setDoubleValue(prop->getDoubleValue()
534 * factor->getDoubleValue());
536 default: // doesn't make sense with strings
543 * Built-in command: swap two property values.
545 * property[0]: the name of the first property.
546 * property[1]: the name of the second property.
549 do_property_swap (const SGPropertyNode * arg)
551 SGPropertyNode * prop1 = get_prop(arg);
552 SGPropertyNode * prop2 = get_prop2(arg);
554 // FIXME: inefficient
555 const string & tmp = prop1->getStringValue();
556 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
557 prop2->setUnspecifiedValue(tmp.c_str()));
562 * Set a property to an axis or other moving input.
564 * property: the name of the property to set.
565 * setting: the current input setting, usually between -1.0 and 1.0.
566 * offset: the offset to shift by, before applying the factor.
567 * factor: the factor to multiply by (use negative to reverse).
570 do_property_scale (const SGPropertyNode * arg)
572 SGPropertyNode * prop = get_prop(arg);
573 double setting = arg->getDoubleValue("setting");
574 double offset = arg->getDoubleValue("offset", 0.0);
575 double factor = arg->getDoubleValue("factor", 1.0);
576 bool squared = arg->getBoolValue("squared", false);
579 setting = (setting < 0 ? -1 : 1) * setting * setting;
581 return prop->setDoubleValue((setting + offset) * factor);
585 do_gui (const SGPropertyNode * arg)
587 NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
588 gui->display(arg->getStringValue("name"));
594 ////////////////////////////////////////////////////////////////////////
596 ////////////////////////////////////////////////////////////////////////
600 * Table of built-in commands.
602 * New commands do not have to be added here; any module in the application
603 * can add a new command using globals->get_commands()->addCommand(...).
607 SGCommandMgr::command_t command;
613 { "panel-load", do_panel_load },
614 { "panel-mouse-click", do_panel_mouse_click },
615 { "preferences-load", do_preferences_load },
616 { "view-cycle", do_view_cycle },
617 { "screen-capture", do_screen_capture },
618 { "tile-cache-reload", do_tile_cache_reload },
619 { "lighting-update", do_lighting_update },
620 { "property-toggle", do_property_toggle },
621 { "property-assign", do_property_assign },
622 { "property-adjust", do_property_adjust },
623 { "property-multiply", do_property_multiply },
624 { "property-swap", do_property_swap },
625 { "property-scale", do_property_scale },
627 { 0, 0 } // zero-terminated
632 * Initialize the default built-in commands.
634 * Other commands may be added by other parts of the application.
639 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
640 for (int i = 0; built_ins[i].name != 0; i++) {
641 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
642 globals->get_commands()->addCommand(built_ins[i].name,
643 built_ins[i].command);
646 typedef bool (*dummy)();
647 fgTie( "/command/view/next", dummy(0), do_view_next );
648 fgTie( "/command/view/prev", dummy(0), do_view_prev );
651 // end of fg_commands.cxx