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