]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
More cleanups. Removed the pui-* commands, and added a pass-through
[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_1; }
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 /**
265  * Built-in command: cycle view.
266  */
267 static bool
268 do_view_cycle (const SGPropertyNode * arg, SGCommandState ** state)
269 {
270   globals->get_current_view()->setHeadingOffset_deg(0.0);
271   globals->get_viewmgr()->next_view();
272   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
273       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
274       if ( globals->get_viewmgr()->get_current() == 1 ) {
275           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
276       }
277   }
278 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
279   return true;
280 }
281
282
283 /**
284  * Built-in command: capture screen.
285  */
286 static bool
287 do_screen_capture (const SGPropertyNode * arg, SGCommandState ** state)
288 {
289   fgDumpSnapShot();
290   return true;
291 }
292
293
294 /**
295  * Reload the tile cache.
296  */
297 static bool
298 do_tile_cache_reload (const SGPropertyNode * arg, SGCommandState ** state)
299 {
300     static const SGPropertyNode *master_freeze
301         = fgGetNode("/sim/freeze/master");
302     bool freeze = master_freeze->getBoolValue();
303     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
304     if ( !freeze ) {
305         fgSetBool("/sim/freeze/master", true);
306     }
307     // BusyCursor(0);
308     if ( global_tile_mgr.init() ) {
309         // Load the local scenery data
310         global_tile_mgr.update(fgGetDouble("/position/longitude-deg"),
311                                fgGetDouble("/position/latitude-deg"));
312     } else {
313         SG_LOG( SG_GENERAL, SG_ALERT, 
314                 "Error in Tile Manager initialization!" );
315         exit(-1);
316     }
317     // BusyCursor(1);
318     if ( !freeze ) {
319         fgSetBool("/sim/freeze/master", false);
320     }
321     return true;
322 }
323
324
325 /**
326  * Update the lighting manually.
327  */
328 static bool
329 do_lighting_update (const SGPropertyNode * arg, SGCommandState ** state)
330 {
331   fgUpdateSkyAndLightingParams();
332   return true;
333 }
334
335
336 /**
337  * Built-in command: toggle a bool property value.
338  *
339  * property: The name of the property to toggle.
340  */
341 static bool
342 do_property_toggle (const SGPropertyNode * arg, SGCommandState ** state)
343 {
344   if (*state == 0)
345     *state = new PropertyCommandState(arg);
346   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
347   return prop->setBoolValue(!prop->getBoolValue());
348 }
349
350
351 /**
352  * Built-in command: assign a value to a property.
353  *
354  * property: the name of the property to assign.
355  * value: the value to assign.
356  */
357 static bool
358 do_property_assign (const SGPropertyNode * arg, SGCommandState ** state)
359 {
360   if (*state == 0)
361     *state = new PropertyCommandState(arg);
362   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
363   const SGPropertyNode * value =
364     ((PropertyCommandState *)(*state))->getValue();
365
366   switch (prop->getType()) {
367   case SGPropertyNode::BOOL:
368     return prop->setBoolValue(value->getBoolValue());
369   case SGPropertyNode::INT:
370     return prop->setIntValue(value->getIntValue());
371   case SGPropertyNode::LONG:
372     return prop->setLongValue(value->getLongValue());
373   case SGPropertyNode::FLOAT:
374     return prop->setFloatValue(value->getFloatValue());
375   case SGPropertyNode::DOUBLE:
376     return prop->setDoubleValue(value->getDoubleValue());
377   case SGPropertyNode::STRING:
378     return prop->setStringValue(value->getStringValue());
379   default:
380     return prop->setUnspecifiedValue(value->getStringValue());
381   }
382 }
383
384
385 /**
386  * Built-in command: increment or decrement a property value.
387  *
388  * property: the name of the property to increment or decrement.
389  * step: the amount of the increment or decrement (default: 0).
390  * offset: a normalized amount to offset by (if step is not present).
391  * factor: the amount by which to multiply the offset (if step is not present).
392  * min: the minimum allowed value (default: no minimum).
393  * max: the maximum allowed value (default: no maximum).
394  * wrap: true if the value should be wrapped when it passes min or max;
395  *       both min and max must be present for this to work (default:
396  *       false).
397  */
398 static bool
399 do_property_adjust (const SGPropertyNode * arg, SGCommandState ** state)
400 {
401   if (*state == 0)
402     *state = new PropertyCommandState(arg);
403   bool hasStep = ((PropertyCommandState *)(*state))->hasStep();
404   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
405   const SGPropertyNode * step = ((PropertyCommandState *)(*state))->getStep();
406   const SGPropertyNode * offset =
407     ((PropertyCommandState *)(*state))->getOffset();
408   const SGPropertyNode * factor =
409     ((PropertyCommandState *)(*state))->getFactor();
410   const SGPropertyNode * min = ((PropertyCommandState *)(*state))->getMin();
411   const SGPropertyNode * max = ((PropertyCommandState *)(*state))->getMax();
412   bool wrap = ((PropertyCommandState *)(*state))->getWrap()->getBoolValue();
413
414   double amount = 0;
415   if (!hasStep) {
416     amount = offset->getDoubleValue() * factor->getDoubleValue();
417   }
418
419
420   switch (prop->getType()) {
421   case SGPropertyNode::BOOL:
422     bool value;
423     if (hasStep)
424       value = step->getBoolValue();
425     else
426       value = amount;
427     if (value)
428       return prop->setBoolValue(!prop->getBoolValue());
429     else
430       return true;
431   case SGPropertyNode::INT: {
432     int value;
433     if (hasStep)
434       value = prop->getIntValue() + step->getIntValue();
435     else
436       value = prop->getIntValue() + int(amount);
437     if (min && (value < min->getIntValue())) {
438       if (wrap && max)
439         value = max->getIntValue();
440       else
441         value = min->getIntValue();
442     }
443     if (max && value > max->getIntValue()) {
444       if (wrap && min)
445         value = min->getIntValue();
446       else
447         value = max->getIntValue();
448     }
449     return prop->setIntValue(value);
450   }
451   case SGPropertyNode::LONG: {
452     long value;
453     if (hasStep)
454       value = prop->getLongValue() + step->getLongValue();
455     else
456       value = prop->getLongValue() + long(amount);
457     if (min && (value < min->getLongValue())) {
458       if (wrap && max)
459         value = max->getLongValue();
460       else
461         value = min->getLongValue();
462     }
463     if (max && value > max->getLongValue()) {
464       if (wrap && min)
465         value = min->getLongValue();
466       else
467         value = max->getLongValue();
468     }
469     return prop->setLongValue(value);
470   }
471   case SGPropertyNode::FLOAT: {
472     float value;
473     if (hasStep)
474       value = prop->getFloatValue() + step->getFloatValue();
475     else
476       value = prop->getFloatValue() + float(amount);
477     if (min && (value < min->getFloatValue())) {
478       if (wrap && max)
479         value = max->getFloatValue();
480       else
481         value = min->getFloatValue();
482     }
483     if (max && value > max->getFloatValue()) {
484       if (wrap && min)
485         value = min->getFloatValue();
486       else
487         value = max->getFloatValue();
488     }
489     return prop->setFloatValue(value);
490   }
491   case SGPropertyNode::DOUBLE:
492   case SGPropertyNode::UNSPECIFIED:
493   case SGPropertyNode::NONE: {
494     double value;
495     if (hasStep)
496       value = prop->getDoubleValue() + step->getDoubleValue();
497     else
498       value = prop->getDoubleValue() + amount;
499     if (min && (value < min->getDoubleValue())) {
500       if (wrap && max)
501         value = max->getDoubleValue();
502       else
503         value = min->getDoubleValue();
504     }
505     if (max && value > max->getDoubleValue()) {
506       if (wrap && min)
507         value = min->getDoubleValue();
508       else
509         value = max->getDoubleValue();
510     }
511     return prop->setDoubleValue(value);
512   }
513   case SGPropertyNode::STRING: // doesn't make sense with strings
514     SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
515     return false;
516   default:
517     SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
518     return false;
519   }
520 }
521
522
523 /**
524  * Built-in command: multiply a property value.
525  *
526  * property: the name of the property to multiply.
527  * factor: the amount by which to multiply.
528  */
529 static bool
530 do_property_multiply (const SGPropertyNode * arg, SGCommandState ** state)
531 {
532   if (*state == 0)
533     *state = new PropertyCommandState(arg);
534   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
535   const SGPropertyNode * factor =
536     ((PropertyCommandState *)(*state))->getFactor();
537
538   switch (prop->getType()) {
539   case SGPropertyNode::BOOL:
540     return prop->setBoolValue(prop->getBoolValue() &&
541                               factor->getBoolValue());
542   case SGPropertyNode::INT:
543     return prop->setIntValue(int(prop->getIntValue()
544                                  * factor->getDoubleValue()));
545   case SGPropertyNode::LONG:
546     return prop->setLongValue(long(prop->getLongValue()
547                                    * factor->getDoubleValue()));
548   case SGPropertyNode::FLOAT:
549     return prop->setFloatValue(float(prop->getFloatValue()
550                                      * factor->getDoubleValue()));
551   case SGPropertyNode::DOUBLE:
552   case SGPropertyNode::UNSPECIFIED:
553   case SGPropertyNode::NONE:
554     return prop->setDoubleValue(prop->getDoubleValue()
555                                 * factor->getDoubleValue());
556   default:                      // doesn't make sense with strings
557     return false;
558   }
559 }
560
561
562 /**
563  * Built-in command: swap two property values.
564  *
565  * property[0]: the name of the first property.
566  * property[1]: the name of the second property.
567  */
568 static bool
569 do_property_swap (const SGPropertyNode * arg, SGCommandState ** state)
570 {
571   if (*state == 0)
572     *state = new PropertyCommandState(arg);
573   SGPropertyNode * prop1 = ((PropertyCommandState *)(*state))->getProp();
574   SGPropertyNode * prop2 = ((PropertyCommandState *)(*state))->getProp2();
575
576                                 // FIXME: inefficient
577   const string & tmp = prop1->getStringValue();
578   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
579           prop2->setUnspecifiedValue(tmp.c_str()));
580 }
581
582
583 /**
584  * Set a property to an axis or other moving input.
585  *
586  * property: the name of the property to set.
587  * setting: the current input setting, usually between -1.0 and 1.0.
588  * offset: the offset to shift by, before applying the factor.
589  * factor: the factor to multiply by (use negative to reverse).
590  */
591 static bool
592 do_property_scale (const SGPropertyNode * arg, SGCommandState ** state)
593 {
594   if (*state == 0)
595     *state = new PropertyCommandState(arg);
596   SGPropertyNode * prop = ((PropertyCommandState *)(*state))->getProp();
597   double setting =
598     ((PropertyCommandState *)(*state))->getSetting()->getDoubleValue();
599   double offset =
600     ((PropertyCommandState *)(*state))->getOffset()->getDoubleValue();
601   double factor =
602     ((PropertyCommandState *)(*state))->getFactor()->getDoubleValue();
603   bool squared =
604     ((PropertyCommandState *)(*state))->getSquared()->getBoolValue();
605
606   if (squared)
607     setting = (setting < 0 ? -1 : 1) * setting * setting;
608   double result = (setting + offset) * factor;
609
610   return prop->setDoubleValue(result);
611 }
612
613
614 \f
615 ////////////////////////////////////////////////////////////////////////
616 // Command setup.
617 ////////////////////////////////////////////////////////////////////////
618
619
620 /**
621  * Table of built-in commands.
622  *
623  * New commands do not have to be added here; any module in the application
624  * can add a new command using globals->get_commands()->addCommand(...).
625  */
626 static struct {
627   const char * name;
628   SGCommandMgr::command_t command;
629 } built_ins [] = {
630     { "null", do_null },
631     { "exit", do_exit },
632     { "load", do_load },
633     { "save", do_save },
634     { "panel-load", do_panel_load },
635     { "panel-mouse-click", do_panel_mouse_click },
636     { "preferences-load", do_preferences_load },
637     { "view-cycle", do_view_cycle },
638     { "screen-capture", do_screen_capture },
639     { "tile-cache-reload", do_tile_cache_reload },
640     { "lighting-update", do_lighting_update },
641     { "property-toggle", do_property_toggle },
642     { "property-assign", do_property_assign },
643     { "property-adjust", do_property_adjust },
644     { "property-multiply", do_property_multiply },
645     { "property-swap", do_property_swap },
646     { "property-scale", do_property_scale },
647     { 0, 0 }                    // zero-terminated
648 };
649
650
651 /**
652  * Initialize the default built-in commands.
653  *
654  * Other commands may be added by other parts of the application.
655  */
656 void
657 fgInitCommands ()
658 {
659   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
660   for (int i = 0; built_ins[i].name != 0; i++) {
661     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
662     globals->get_commands()->addCommand(built_ins[i].name,
663                                         built_ins[i].command);
664   }
665 }
666
667 // end of fg_commands.hxx
668