]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
Added static port system and a new altimeter model connected to it.
[flightgear.git] / src / Main / fg_commands.cxx
1 // fg_commands.cxx - internal FGFS commands.
2
3 #include <string.h>             // strcmp()
4
5 #include <simgear/compiler.h>
6 #include <simgear/misc/exception.hxx>
7
8 #include STL_STRING
9 #include STL_FSTREAM
10
11 #include <simgear/debug/logstream.hxx>
12 #include <simgear/misc/commands.hxx>
13 #include <simgear/misc/props.hxx>
14
15 #include <GUI/gui.h>
16 #include <Cockpit/panel.hxx>
17 #include <Cockpit/panel_io.hxx>
18 #include <Scenery/tilemgr.hxx>
19 #include <Time/tmp.hxx>
20
21 #include "fg_commands.hxx"
22
23 SG_USING_STD(string);
24 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
25 SG_USING_STD(ifstream);
26 SG_USING_STD(ofstream);
27 #endif
28
29 #include "fg_props.hxx"
30 #include "fg_io.hxx"
31 #include "globals.hxx"
32 #include "viewmgr.hxx"
33
34
35 \f
36 ////////////////////////////////////////////////////////////////////////
37 // Saved command states.
38 ////////////////////////////////////////////////////////////////////////
39
40
41 /**
42  * Base saved state for property commands.
43  *
44  * Since this class isn't publicly visible, it is simply an aggregate
45  * of all the stuff any property command needs.
46  */
47 class PropertyCommandState : public SGCommandState
48 {
49 public:
50   PropertyCommandState (const SGPropertyNode * arg);
51   virtual SGPropertyNode * getProp () const { return _prop; }
52   virtual SGPropertyNode * getProp2 () const { return _prop2; }
53   virtual const SGPropertyNode * getValue () const
54     { return _value ? _value : &_dummy_0; }
55   virtual const bool hasStep () const { return _step != 0; }
56   virtual const SGPropertyNode * getStep () const 
57     { return _step ? _step : &_dummy_0; }
58   virtual const SGPropertyNode * getMin () const { return _min; }
59   virtual const SGPropertyNode * getMax () const { return _max; }
60   virtual const SGPropertyNode * getWrap () const
61     { return _wrap ? _wrap : &_dummy_0; }
62   virtual const SGPropertyNode * getFactor () const 
63     { return _factor ? _factor : &_dummy_1; }
64   virtual const SGPropertyNode * getSquared () const 
65     { return _squared ? _squared : &_dummy_0; }
66   virtual const SGPropertyNode * getSetting () const 
67     { return _setting ? _setting : &_dummy_0; }
68   virtual const SGPropertyNode * getOffset () const
69     { return _offset ? _offset : &_dummy_0; }
70 private:
71   static SGPropertyNode _dummy_0;
72   static SGPropertyNode _dummy_1;
73   mutable SGPropertyNode * _prop;
74   mutable SGPropertyNode * _prop2;
75   const SGPropertyNode * _value;
76   const SGPropertyNode * _step;
77   const SGPropertyNode * _min;
78   const SGPropertyNode * _max;
79   const SGPropertyNode * _wrap;
80   const SGPropertyNode * _factor;
81   const SGPropertyNode * _squared;
82   const SGPropertyNode * _setting;
83   const SGPropertyNode * _offset;
84 };
85
86 SGPropertyNode PropertyCommandState::_dummy_0;
87 SGPropertyNode PropertyCommandState::_dummy_1;
88
89 PropertyCommandState::PropertyCommandState (const SGPropertyNode * arg)
90   : SGCommandState(arg),
91     _prop(fgGetNode(arg->getStringValue("property[0]", "/null"), true)),
92     _prop2(fgGetNode(arg->getStringValue("property[1]", "/null"), true)),
93     _value(arg->getNode("value")),
94     _step(arg->getNode("step")),
95     _min(arg->getNode("min")),
96     _max(arg->getNode("max")),
97     _wrap(arg->getNode("wrap")),
98     _factor(arg->getNode("factor")),
99     _squared(arg->getNode("squared")),
100     _setting(arg->getNode("setting")),
101     _offset(arg->getNode("offset"))
102 {
103                                 // It would be better not to do this
104                                 // every time, but it's not that big
105                                 // a deal.  I don't know enough about
106                                 // C++ static initialization to fix it.
107   _dummy_1.setDoubleValue(1.0);
108 }
109
110
111
112 \f
113 ////////////////////////////////////////////////////////////////////////
114 // Command implementations.
115 ////////////////////////////////////////////////////////////////////////
116
117
118 /**
119  * Built-in command: do nothing.
120  */
121 static bool
122 do_null (const SGPropertyNode * arg, SGCommandState ** state)
123 {
124   return true;
125 }
126
127
128 /**
129  * Built-in command: exit FlightGear.
130  *
131  * TODO: show a confirm dialog.
132  */
133 static bool
134 do_exit (const SGPropertyNode * arg, SGCommandState ** state)
135 {
136   SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
137   ConfirmExitDialog();
138   return true;
139 }
140
141
142 /**
143  * Built-in command: load flight.
144  *
145  * file (optional): the name of the file to load (relative to current
146  * directory).  Defaults to "fgfs.sav".
147  */
148 static bool
149 do_load (const SGPropertyNode * arg, SGCommandState ** state)
150 {
151   const string &file = arg->getStringValue("file", "fgfs.sav");
152   ifstream input(file.c_str());
153   if (input.good() && fgLoadFlight(input)) {
154     input.close();
155     SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
156     return true;
157   } else {
158     SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
159     return false;
160   }
161 }
162
163
164 /**
165  * Built-in command: save flight.
166  *
167  * file (optional): the name of the file to save (relative to the
168  * current directory).  Defaults to "fgfs.sav".
169  */
170 static bool
171 do_save (const SGPropertyNode * arg, SGCommandState ** state)
172 {
173   const string &file = arg->getStringValue("file", "fgfs.sav");
174   bool write_all = arg->getBoolValue("write-all", false);
175   SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
176   ofstream output(file.c_str());
177   if (output.good() && fgSaveFlight(output, write_all)) {
178     output.close();
179     SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
180     return true;
181   } else {
182     SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
183     return false;
184   }
185 }
186
187
188 /**
189  * Built-in command: (re)load the panel.
190  *
191  * path (optional): the file name to load the panel from 
192  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
193  * and if that's unspecified, to "Panels/Default/default.xml".
194  */
195 static bool
196 do_panel_load (const SGPropertyNode * arg, SGCommandState ** state)
197 {
198   string panel_path =
199     arg->getStringValue("path",
200                         fgGetString("/sim/panel/path",
201                                     "Panels/Default/default.xml"));
202   FGPanel * new_panel = fgReadPanel(panel_path);
203   if (new_panel == 0) {
204     SG_LOG(SG_INPUT, SG_ALERT,
205            "Error reading new panel from " << panel_path);
206     return false;
207   }
208   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
209   current_panel->unbind();
210   delete current_panel;
211   current_panel = new_panel;
212   current_panel->bind();
213   return true;
214 }
215
216
217 /**
218  * Built-in command: pass a mouse click to the panel.
219  *
220  * button: the mouse button number, zero-based.
221  * is-down: true if the button is down, false if it is up.
222  * x-pos: the x position of the mouse click.
223  * y-pos: the y position of the mouse click.
224  */
225 static bool
226 do_panel_mouse_click (const SGPropertyNode * arg, SGCommandState ** state)
227 {
228   if (current_panel != 0)
229     return current_panel
230       ->doMouseAction(arg->getIntValue("button"),
231                       arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
232                       arg->getIntValue("x-pos"),
233                       arg->getIntValue("y-pos"));
234   else
235     return false;
236 }
237
238
239 /**
240  * Built-in command: (re)load preferences.
241  *
242  * path (optional): the file name to load the panel from (relative
243  * to FG_ROOT). Defaults to "preferences.xml".
244  */
245 static bool
246 do_preferences_load (const SGPropertyNode * arg, SGCommandState ** state)
247 {
248   const string &path = arg->getStringValue("path", "preferences.xml");
249   SGPath props_path(globals->get_fg_root());
250   props_path.append(path);
251   SG_LOG(SG_INPUT, SG_INFO, "Reading global preferences from "
252          << props_path.str());
253   try {
254     readProperties(props_path.str(), globals->get_props());
255   } catch (const sg_exception &e) {
256     guiErrorMessage("Error reading global preferences: ", e);
257     return false;
258   }
259   SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
260   return true;
261 }
262
263
264 static void
265 fix_hud_visibility()
266 {
267   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
268       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
269       if ( globals->get_viewmgr()->get_current() == 1 ) {
270           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
271       }
272   }
273 }
274
275 void
276 do_view_next( bool )
277 {
278     globals->get_current_view()->setHeadingOffset_deg(0.0);
279     globals->get_viewmgr()->next_view();
280     fix_hud_visibility();
281   global_tile_mgr.refresh_view_timestamps();
282 }
283
284 void
285 do_view_prev( bool )
286 {
287     globals->get_current_view()->setHeadingOffset_deg(0.0);
288     globals->get_viewmgr()->prev_view();
289     fix_hud_visibility();
290   global_tile_mgr.refresh_view_timestamps();
291 }
292
293 /**
294  * Built-in command: cycle view.
295  */
296 static bool
297 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
298 {
299   globals->get_current_view()->setHeadingOffset_deg(0.0);
300   globals->get_viewmgr()->next_view();
301   fix_hud_visibility();
302   global_tile_mgr.refresh_view_timestamps();
303 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
304   return true;
305 }
306
307 /**
308  * Built-in command: capture screen.
309  */
310 static bool
311 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
312 {
313   fgDumpSnapShot();
314   return true;
315 }
316
317
318 /**
319  * Reload the tile cache.
320  */
321 static bool
322 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
323 {
324     static const SGPropertyNode *master_freeze
325         = fgGetNode("/sim/freeze/master");
326     bool freeze = master_freeze->getBoolValue();
327     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
328     if ( !freeze ) {
329         fgSetBool("/sim/freeze/master", true);
330     }
331     // BusyCursor(0);
332     if ( global_tile_mgr.init() ) {
333         // Load the local scenery data
334         double visibility_meters = fgGetDouble("/environment/visibility-m");
335         global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
336                                fgGetDouble("/position/latitude-deg"),
337                                visibility_meters);
338     } else {
339         SG_LOG( SG_GENERAL, SG_ALERT, 
340                 "Error in Tile Manager initialization!" );
341         exit(-1);
342     }
343     // BusyCursor(1);
344     if ( !freeze ) {
345         fgSetBool("/sim/freeze/master", false);
346     }
347     return true;
348 }
349
350
351 /**
352  * Update the lighting manually.
353  */
354 static bool
355 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
356 {
357   fgUpdateSkyAndLightingParams();
358   return true;
359 }
360
361
362 /**
363  * Built-in command: toggle a bool property value.
364  *
365  * property: The name of the property to toggle.
366  */
367 static bool
368 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
369 {
370   if (*state == 0)
371     *state = new PropertyCommandState(arg);
372   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
373   return prop->setBoolValue(!prop->getBoolValue());
374 }
375
376
377 /**
378  * Built-in command: assign a value to a property.
379  *
380  * property: the name of the property to assign.
381  * value: the value to assign.
382  */
383 static bool
384 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
385 {
386   if (*state == 0)
387     *state = new PropertyCommandState(arg);
388   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
389   const SGPropertyNode * value =
390     ((PropertyCommandState *)(*state))->getValue();
391
392   switch (prop->getType()) {
393   case SGPropertyNode::BOOL:
394     return prop->setBoolValue(value->getBoolValue());
395   case SGPropertyNode::INT:
396     return prop->setIntValue(value->getIntValue());
397   case SGPropertyNode::LONG:
398     return prop->setLongValue(value->getLongValue());
399   case SGPropertyNode::FLOAT:
400     return prop->setFloatValue(value->getFloatValue());
401   case SGPropertyNode::DOUBLE:
402     return prop->setDoubleValue(value->getDoubleValue());
403   case SGPropertyNode::STRING:
404     return prop->setStringValue(value->getStringValue());
405   default:
406     return prop->setUnspecifiedValue(value->getStringValue());
407   }
408 }
409
410
411 /**
412  * Built-in command: increment or decrement a property value.
413  *
414  * property: the name of the property to increment or decrement.
415  * step: the amount of the increment or decrement (default: 0).
416  * offset: a normalized amount to offset by (if step is not present).
417  * factor: the amount by which to multiply the offset (if step is not present).
418  * min: the minimum allowed value (default: no minimum).
419  * max: the maximum allowed value (default: no maximum).
420  * wrap: true if the value should be wrapped when it passes min or max;
421  *       both min and max must be present for this to work (default:
422  *       false).
423  */
424 static bool
425 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
426 {
427   if (*state == 0)
428     *state = new PropertyCommandState(arg);
429   bool hasStep = ((PropertyCommandState *)(*state))->hasStep();
430   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
431   const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
432   const SGPropertyNode * offset =
433     ((PropertyCommandState *)(*state))->getOffset();
434   const SGPropertyNode * factor =
435     ((PropertyCommandState *)(*state))->getFactor();
436   const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
437   const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
438   bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
439
440   double amount = 0;
441   if (!hasStep) {
442     amount = offset->getDoubleValue() * factor->getDoubleValue();
443   }
444
445
446   switch (prop->getType()) {
447   case SGPropertyNode::BOOL:
448     bool value;
449     if (hasStep)
450       value = step->getBoolValue();
451     else
452       value = (0.0 != amount);
453     if (value)
454       return prop->setBoolValue(!prop->getBoolValue());
455     else
456       return true;
457   case SGPropertyNode::INT: {
458     int value;
459     if (hasStep)
460       value = prop->getIntValue() + step->getIntValue();
461     else
462       value = prop->getIntValue() + int(amount);
463     if (min && (value < min->getIntValue())) {
464       if (wrap && max)
465         value = max->getIntValue();
466       else
467         value = min->getIntValue();
468     }
469     if (max && value > max->getIntValue()) {
470       if (wrap && min)
471         value = min->getIntValue();
472       else
473         value = max->getIntValue();
474     }
475     return prop->setIntValue(value);
476   }
477   case SGPropertyNode::LONG: {
478     long value;
479     if (hasStep)
480       value = prop->getLongValue() + step->getLongValue();
481     else
482       value = prop->getLongValue() + long(amount);
483     if (min && (value < min->getLongValue())) {
484       if (wrap && max)
485         value = max->getLongValue();
486       else
487         value = min->getLongValue();
488     }
489     if (max && value > max->getLongValue()) {
490       if (wrap && min)
491         value = min->getLongValue();
492       else
493         value = max->getLongValue();
494     }
495     return prop->setLongValue(value);
496   }
497   case SGPropertyNode::FLOAT: {
498     float value;
499     if (hasStep)
500       value = prop->getFloatValue() + step->getFloatValue();
501     else
502       value = prop->getFloatValue() + float(amount);
503     if (min && (value < min->getFloatValue())) {
504       if (wrap && max)
505         value = max->getFloatValue();
506       else
507         value = min->getFloatValue();
508     }
509     if (max && value > max->getFloatValue()) {
510       if (wrap && min)
511         value = min->getFloatValue();
512       else
513         value = max->getFloatValue();
514     }
515     return prop->setFloatValue(value);
516   }
517   case SGPropertyNode::DOUBLE:
518   case SGPropertyNode::UNSPECIFIED:
519   case SGPropertyNode::NONE: {
520     double value;
521     if (hasStep)
522       value = prop->getDoubleValue() + step->getDoubleValue();
523     else
524       value = prop->getDoubleValue() + amount;
525     if (min && (value < min->getDoubleValue())) {
526       if (wrap && max)
527         value = max->getDoubleValue();
528       else
529         value = min->getDoubleValue();
530     }
531     if (max && value > max->getDoubleValue()) {
532       if (wrap && min)
533         value = min->getDoubleValue();
534       else
535         value = max->getDoubleValue();
536     }
537     return prop->setDoubleValue(value);
538   }
539   case SGPropertyNode::STRING: // doesn't make sense with strings
540     SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
541     return false;
542   default:
543     SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
544     return false;
545   }
546 }
547
548
549 /**
550  * Built-in command: multiply a property value.
551  *
552  * property: the name of the property to multiply.
553  * factor: the amount by which to multiply.
554  */
555 static bool
556 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
557 {
558   if (*state == 0)
559     *state = new PropertyCommandState(arg);
560   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
561   const SGPropertyNode * factor =
562     ((PropertyCommandState *)(*state))->getFactor();
563
564   switch (prop->getType()) {
565   case SGPropertyNode::BOOL:
566     return prop->setBoolValue(prop->getBoolValue() &&
567                               factor->getBoolValue());
568   case SGPropertyNode::INT:
569     return prop->setIntValue(int(prop->getIntValue()
570                                  * factor->getDoubleValue()));
571   case SGPropertyNode::LONG:
572     return prop->setLongValue(long(prop->getLongValue()
573                                    * factor->getDoubleValue()));
574   case SGPropertyNode::FLOAT:
575     return prop->setFloatValue(float(prop->getFloatValue()
576                                      * factor->getDoubleValue()));
577   case SGPropertyNode::DOUBLE:
578   case SGPropertyNode::UNSPECIFIED:
579   case SGPropertyNode::NONE:
580     return prop->setDoubleValue(prop->getDoubleValue()
581                                 * factor->getDoubleValue());
582   default:                      // doesn't make sense with strings
583     return false;
584   }
585 }
586
587
588 /**
589  * Built-in command: swap two property values.
590  *
591  * property[0]: the name of the first property.
592  * property[1]: the name of the second property.
593  */
594 static bool
595 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
596 {
597   if (*state == 0)
598     *state = new PropertyCommandState(arg);
599   SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
600   SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
601
602                                 // FIXME: inefficient
603   const string & tmp = prop1->getStringValue();
604   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
605           prop2->setUnspecifiedValue(tmp.c_str()));
606 }
607
608
609 /**
610  * Set a property to an axis or other moving input.
611  *
612  * property: the name of the property to set.
613  * setting: the current input setting, usually between -1.0 and 1.0.
614  * offset: the offset to shift by, before applying the factor.
615  * factor: the factor to multiply by (use negative to reverse).
616  */
617 static bool
618 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
619 {
620   if (*state == 0)
621     *state = new PropertyCommandState(arg);
622   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
623   double setting =
624     ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
625   double offset =
626     ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
627   double factor =
628     ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
629   bool squared =
630     ((PropertyCommandState *)(*state))->getSquared()->getBoolValue();
631
632   if (squared)
633     setting = (setting < 0 ? -1 : 1) * setting * setting;
634   double result = (setting + offset) * factor;
635
636   return prop->setDoubleValue(result);
637 }
638
639
640 \f
641 ////////////////////////////////////////////////////////////////////////
642 // Command setup.
643 ////////////////////////////////////////////////////////////////////////
644
645
646 /**
647  * Table of built-in commands.
648  *
649  * New commands do not have to be added here; any module in the application
650  * can add a new command using globals->get_commands()->addCommand(...).
651  */
652 static struct {
653   const char * name;
654   SGCommandMgr::command_t command;
655 } built_ins [] = {
656     { "null", do_null },
657     { "exit", do_exit },
658     { "load", do_load },
659     { "save", do_save },
660     { "panel-load", do_panel_load },
661     { "panel-mouse-click", do_panel_mouse_click },
662     { "preferences-load", do_preferences_load },
663     { "view-cycle", do_view_cycle },
664     { "screen-capture", do_screen_capture },
665     { "tile-cache-reload", do_tile_cache_reload },
666     { "lighting-update", do_lighting_update },
667     { "property-toggle", do_property_toggle },
668     { "property-assign", do_property_assign },
669     { "property-adjust", do_property_adjust },
670     { "property-multiply", do_property_multiply },
671     { "property-swap", do_property_swap },
672     { "property-scale", do_property_scale },
673     { 0, 0 }                    // zero-terminated
674 };
675
676
677 /**
678  * Initialize the default built-in commands.
679  *
680  * Other commands may be added by other parts of the application.
681  */
682 void
683 fgInitCommands ()
684 {
685   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
686   for (int i = 0; built_ins[i].name != 0; i++) {
687     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
688     globals->get_commands()->addCommand(built_ins[i].name,
689                                         built_ins[i].command);
690   }
691
692   typedef bool (*dummy)();
693   fgTie( "/command/view/next", dummy(0), do_view_next );
694   fgTie( "/command/view/prev", dummy(0), do_view_prev );
695 }
696
697 // end of fg_commands.cxx