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