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