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