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 <Cockpit/panel.hxx>
17 #include <Cockpit/panel_io.hxx>
18 #include <Scenery/tilemgr.hxx>
19 #include <Time/tmp.hxx>
21 #include "fg_commands.hxx"
24 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
25 SG_USING_STD(ifstream);
26 SG_USING_STD(ofstream);
29 #include "fg_props.hxx"
31 #include "globals.hxx"
32 #include "viewmgr.hxx"
36 ////////////////////////////////////////////////////////////////////////
37 // Static helper functions.
38 ////////////////////////////////////////////////////////////////////////
42 * Template function to wrap a value.
46 do_wrap (T * value, T min, T max)
48 if (min >= max) { // basic sanity check
59 static inline SGPropertyNode *
60 get_prop (const SGPropertyNode * arg)
62 return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
65 static inline SGPropertyNode *
66 get_prop2 (const SGPropertyNode * arg)
68 return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
73 ////////////////////////////////////////////////////////////////////////
74 // Command implementations.
75 ////////////////////////////////////////////////////////////////////////
79 * Built-in command: do nothing.
82 do_null (const SGPropertyNode * arg, SGCommandState ** state)
89 * Built-in command: exit FlightGear.
91 * TODO: show a confirm dialog.
94 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
96 SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
103 * Built-in command: load flight.
105 * file (optional): the name of the file to load (relative to current
106 * directory). Defaults to "fgfs.sav".
109 do_load (const SGPropertyNode * arg, SGCommandState ** state)
111 const string &file = arg->getStringValue("file", "fgfs.sav");
112 ifstream input(file.c_str());
113 if (input.good() && fgLoadFlight(input)) {
115 SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
118 SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
125 * Built-in command: save flight.
127 * file (optional): the name of the file to save (relative to the
128 * current directory). Defaults to "fgfs.sav".
131 do_save (const SGPropertyNode * arg, SGCommandState ** state)
133 const string &file = arg->getStringValue("file", "fgfs.sav");
134 bool write_all = arg->getBoolValue("write-all", false);
135 SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
136 ofstream output(file.c_str());
137 if (output.good() && fgSaveFlight(output, write_all)) {
139 SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
142 SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
149 * Built-in command: (re)load the panel.
151 * path (optional): the file name to load the panel from
152 * (relative to FG_ROOT). Defaults to the value of /sim/panel/path,
153 * and if that's unspecified, to "Panels/Default/default.xml".
156 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
159 arg->getStringValue("path",
160 fgGetString("/sim/panel/path",
161 "Panels/Default/default.xml"));
162 FGPanel * new_panel = fgReadPanel(panel_path);
163 if (new_panel == 0) {
164 SG_LOG(SG_INPUT, SG_ALERT,
165 "Error reading new panel from " << panel_path);
168 SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
169 current_panel->unbind();
170 delete current_panel;
171 current_panel = new_panel;
172 current_panel->bind();
178 * Built-in command: pass a mouse click to the panel.
180 * button: the mouse button number, zero-based.
181 * is-down: true if the button is down, false if it is up.
182 * x-pos: the x position of the mouse click.
183 * y-pos: the y position of the mouse click.
186 do_panel_mouse_click (const SGPropertyNode * arg, SGCommandState ** state)
188 if (current_panel != 0)
190 ->doMouseAction(arg->getIntValue("button"),
191 arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
192 arg->getIntValue("x-pos"),
193 arg->getIntValue("y-pos"));
200 * Built-in command: (re)load preferences.
202 * path (optional): the file name to load the panel from (relative
203 * to FG_ROOT). Defaults to "preferences.xml".
206 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
208 const string &path = arg->getStringValue("path", "preferences.xml");
209 SGPath props_path(globals->get_fg_root());
210 props_path.append(path);
211 SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
212 << props_path.str());
214 readProperties(props_path.str(), globals->get_props());
215 } catch (const sg_exception &e) {
216 guiErrorMessage("Error reading global preferences: ", e);
219 SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
227 if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
228 globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
229 if ( globals->get_viewmgr()->get_current() == 1 ) {
230 globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
238 globals->get_current_view()->setHeadingOffset_deg(0.0);
239 globals->get_viewmgr()->next_view();
240 fix_hud_visibility();
241 global_tile_mgr.refresh_view_timestamps();
247 globals->get_current_view()->setHeadingOffset_deg(0.0);
248 globals->get_viewmgr()->prev_view();
249 fix_hud_visibility();
250 global_tile_mgr.refresh_view_timestamps();
254 * Built-in command: cycle view.
257 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
259 globals->get_current_view()->setHeadingOffset_deg(0.0);
260 globals->get_viewmgr()->next_view();
261 fix_hud_visibility();
262 global_tile_mgr.refresh_view_timestamps();
263 // fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
268 * Built-in command: capture screen.
271 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
279 * Reload the tile cache.
282 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
284 static const SGPropertyNode *master_freeze
285 = fgGetNode("/sim/freeze/master");
286 bool freeze = master_freeze->getBoolValue();
287 SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
289 fgSetBool("/sim/freeze/master", true);
292 if ( global_tile_mgr.init() ) {
293 // Load the local scenery data
294 double visibility_meters = fgGetDouble("/environment/visibility-m");
295 global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
296 fgGetDouble("/position/latitude-deg"),
299 SG_LOG( SG_GENERAL, SG_ALERT,
300 "Error in Tile Manager initialization!" );
305 fgSetBool("/sim/freeze/master", false);
312 * Update the lighting manually.
315 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
317 fgUpdateSkyAndLightingParams();
323 * Built-in command: toggle a bool property value.
325 * property: The name of the property to toggle.
328 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
330 SGPropertyNode * prop = get_prop(arg);
331 return prop->setBoolValue(!prop->getBoolValue());
336 * Built-in command: assign a value to a property.
338 * property: the name of the property to assign.
339 * value: the value to assign.
342 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
344 SGPropertyNode * prop = get_prop(arg);
345 const SGPropertyNode * value = arg->getNode("value");
347 switch (prop->getType()) {
349 case SGPropertyNode::BOOL:
350 return prop->setBoolValue(value->getBoolValue());
352 case SGPropertyNode::INT:
353 return prop->setIntValue(value->getIntValue());
355 case SGPropertyNode::LONG:
356 return prop->setLongValue(value->getLongValue());
358 case SGPropertyNode::FLOAT:
359 return prop->setFloatValue(value->getFloatValue());
361 case SGPropertyNode::DOUBLE:
362 return prop->setDoubleValue(value->getDoubleValue());
364 case SGPropertyNode::STRING:
365 return prop->setStringValue(value->getStringValue());
368 return prop->setUnspecifiedValue(value->getStringValue());
375 * Built-in command: increment or decrement a property value.
377 * property: the name of the property to increment or decrement.
378 * step: the amount of the increment or decrement (default: 0).
379 * offset: a normalized amount to offset by (if step is not present).
380 * factor: the amount by which to multiply the offset (if step is not present).
381 * min: the minimum allowed value (default: no minimum).
382 * max: the maximum allowed value (default: no maximum).
383 * wrap: true if the value should be wrapped when it passes min or max;
384 * both min and max must be present for this to work (default:
388 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
390 SGPropertyNode * prop = get_prop(arg);
391 const SGPropertyNode * step = arg->getChild("step");
392 const SGPropertyNode * offset = arg->getChild("offset");
393 const SGPropertyNode * factor = arg->getChild("factor");
394 const SGPropertyNode * min = arg->getChild("min");
395 const SGPropertyNode * max = arg->getChild("max");
396 bool wrap = arg->getBoolValue("wrap");
398 if (min == 0 || max == 0)
403 amount = offset->getDoubleValue() * factor->getDoubleValue();
405 switch (prop->getType()) {
407 case SGPropertyNode::BOOL: {
410 value = step->getBoolValue();
412 value = (0.0 != amount);
414 return prop->setBoolValue(!prop->getBoolValue());
419 case SGPropertyNode::INT: {
422 value = prop->getIntValue() + step->getIntValue();
424 value = prop->getIntValue() + int(amount);
426 do_wrap(&value, min->getIntValue(), max->getIntValue());
428 if (min != 0 && value < min->getIntValue())
429 value = min->getIntValue();
430 if (max != 0 && value > max->getIntValue())
431 value = max->getIntValue();
433 return prop->setIntValue(value);
436 case SGPropertyNode::LONG: {
439 value = prop->getLongValue() + step->getLongValue();
441 value = prop->getLongValue() + long(amount);
443 do_wrap(&value, min->getLongValue(), max->getLongValue());
445 if (min != 0 && value < min->getLongValue())
446 value = min->getLongValue();
447 if (max != 0 && value > max->getLongValue())
448 value = max->getLongValue();
450 return prop->setLongValue(value);
453 case SGPropertyNode::FLOAT: {
456 value = prop->getFloatValue() + step->getFloatValue();
458 value = prop->getFloatValue() + float(amount);
460 do_wrap(&value, min->getFloatValue(), max->getFloatValue());
462 if (min != 0 && value < min->getFloatValue())
463 value = min->getFloatValue();
464 if (max != 0 && value > max->getFloatValue())
465 value = max->getFloatValue();
467 return prop->setFloatValue(value);
470 case SGPropertyNode::DOUBLE:
471 case SGPropertyNode::UNSPECIFIED:
472 case SGPropertyNode::NONE: {
475 value = prop->getDoubleValue() + step->getDoubleValue();
477 value = prop->getDoubleValue() + amount;
479 do_wrap(&value, min->getDoubleValue(), max->getDoubleValue());
481 if (min != 0 && value < min->getDoubleValue())
482 value = min->getDoubleValue();
483 if (max != 0 && value > max->getDoubleValue())
484 value = max->getDoubleValue();
486 return prop->setDoubleValue(value);
489 case SGPropertyNode::STRING: // doesn't make sense with strings
490 SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
494 SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
502 * Built-in command: multiply a property value.
504 * property: the name of the property to multiply.
505 * factor: the amount by which to multiply.
508 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
510 SGPropertyNode * prop = get_prop(arg);
511 const SGPropertyNode * factor = arg->getChild("factor");
516 switch (prop->getType()) {
518 case SGPropertyNode::BOOL:
519 return prop->setBoolValue(prop->getBoolValue() && factor->getBoolValue());
521 case SGPropertyNode::INT:
522 return prop->setIntValue(int(prop->getIntValue()
523 * factor->getDoubleValue()));
525 case SGPropertyNode::LONG:
526 return prop->setLongValue(long(prop->getLongValue()
527 * factor->getDoubleValue()));
529 case SGPropertyNode::FLOAT:
530 return prop->setFloatValue(float(prop->getFloatValue()
531 * factor->getDoubleValue()));
533 case SGPropertyNode::DOUBLE:
534 case SGPropertyNode::UNSPECIFIED:
535 case SGPropertyNode::NONE:
536 return prop->setDoubleValue(prop->getDoubleValue()
537 * factor->getDoubleValue());
539 default: // doesn't make sense with strings
546 * Built-in command: swap two property values.
548 * property[0]: the name of the first property.
549 * property[1]: the name of the second property.
552 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
554 SGPropertyNode * prop1 = get_prop(arg);
555 SGPropertyNode * prop2 = get_prop2(arg);
557 // FIXME: inefficient
558 const string & tmp = prop1->getStringValue();
559 return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
560 prop2->setUnspecifiedValue(tmp.c_str()));
565 * Set a property to an axis or other moving input.
567 * property: the name of the property to set.
568 * setting: the current input setting, usually between -1.0 and 1.0.
569 * offset: the offset to shift by, before applying the factor.
570 * factor: the factor to multiply by (use negative to reverse).
573 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
575 SGPropertyNode * prop = get_prop(arg);
576 double setting = arg->getDoubleValue("setting");
577 double offset = arg->getDoubleValue("offset", 0.0);
578 double factor = arg->getDoubleValue("factor", 1.0);
579 bool squared = arg->getBoolValue("squared", false);
582 setting = (setting < 0 ? -1 : 1) * setting * setting;
584 return prop->setDoubleValue((setting + offset) * factor);
589 ////////////////////////////////////////////////////////////////////////
591 ////////////////////////////////////////////////////////////////////////
595 * Table of built-in commands.
597 * New commands do not have to be added here; any module in the application
598 * can add a new command using globals->get_commands()->addCommand(...).
602 SGCommandMgr::command_t command;
608 { "panel-load", do_panel_load },
609 { "panel-mouse-click", do_panel_mouse_click },
610 { "preferences-load", do_preferences_load },
611 { "view-cycle", do_view_cycle },
612 { "screen-capture", do_screen_capture },
613 { "tile-cache-reload", do_tile_cache_reload },
614 { "lighting-update", do_lighting_update },
615 { "property-toggle", do_property_toggle },
616 { "property-assign", do_property_assign },
617 { "property-adjust", do_property_adjust },
618 { "property-multiply", do_property_multiply },
619 { "property-swap", do_property_swap },
620 { "property-scale", do_property_scale },
621 { 0, 0 } // zero-terminated
626 * Initialize the default built-in commands.
628 * Other commands may be added by other parts of the application.
633 SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
634 for (int i = 0; built_ins[i].name != 0; i++) {
635 SG_LOG(SG_GENERAL, SG_INFO, " " << built_ins[i].name);
636 globals->get_commands()->addCommand(built_ins[i].name,
637 built_ins[i].command);
640 typedef bool (*dummy)();
641 fgTie( "/command/view/next", dummy(0), do_view_next );
642 fgTie( "/command/view/prev", dummy(0), do_view_prev );
645 // end of fg_commands.cxx