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