]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
9c621a132b7721db0c3f2c01b2b90b46af82f652
[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 <Cockpit/panel.hxx>
16 #include <Cockpit/panel_io.hxx>
17 #include <FDM/flight.hxx>
18 #include <GUI/gui.h>
19 #include <GUI/new_gui.hxx>
20 #include <Scenery/tilemgr.hxx>
21 #include <Time/tmp.hxx>
22
23 #include "fg_init.hxx"
24 #include "fg_commands.hxx"
25
26 SG_USING_STD(string);
27 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
28 SG_USING_STD(ifstream);
29 SG_USING_STD(ofstream);
30 #endif
31
32 #include "fg_props.hxx"
33 #include "fg_io.hxx"
34 #include "globals.hxx"
35 #include "viewmgr.hxx"
36
37
38 \f
39 ////////////////////////////////////////////////////////////////////////
40 // Static helper functions.
41 ////////////////////////////////////////////////////////////////////////
42
43
44 /**
45  * Template function to wrap a value.
46  */
47 template <class T>
48 static inline void
49 do_wrap (T * value, T min, T max)
50 {
51     if (min >= max) {           // basic sanity check
52         *value = min;
53     } else {
54         T range = max - min;
55         while (*value < min)
56             *value += range;
57         while (*value > max)
58             *value -= range;
59     }
60 }
61
62 static inline SGPropertyNode *
63 get_prop (const SGPropertyNode * arg)
64 {
65     return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
66 }
67
68 static inline SGPropertyNode *
69 get_prop2 (const SGPropertyNode * arg)
70 {
71     return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
72 }
73
74
75 \f
76 ////////////////////////////////////////////////////////////////////////
77 // Command implementations.
78 ////////////////////////////////////////////////////////////////////////
79
80
81 /**
82  * Built-in command: do nothing.
83  */
84 static bool
85 do_null (const SGPropertyNode * arg)
86 {
87   return true;
88 }
89
90
91 /**
92  * Built-in command: exit FlightGear.
93  *
94  * TODO: show a confirm dialog.
95  */
96 static bool
97 do_exit (const SGPropertyNode * arg)
98 {
99   SG_LOG(SG_INPUT, SG_ALERT, "Program exit requested.");
100   ConfirmExitDialog();
101   return true;
102 }
103
104
105 /**
106  * Built-in command: load flight.
107  *
108  * file (optional): the name of the file to load (relative to current
109  * directory).  Defaults to "fgfs.sav".
110  */
111 static bool
112 do_load (const SGPropertyNode * arg)
113 {
114   const string &file = arg->getStringValue("file", "fgfs.sav");
115   ifstream input(file.c_str());
116   if (input.good() && fgLoadFlight(input)) {
117     input.close();
118     SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
119     return true;
120   } else {
121     SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
122     return false;
123   }
124 }
125
126
127 /**
128  * Built-in command: save flight.
129  *
130  * file (optional): the name of the file to save (relative to the
131  * current directory).  Defaults to "fgfs.sav".
132  */
133 static bool
134 do_save (const SGPropertyNode * arg)
135 {
136   const string &file = arg->getStringValue("file", "fgfs.sav");
137   bool write_all = arg->getBoolValue("write-all", false);
138   SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
139   ofstream output(file.c_str());
140   if (output.good() && fgSaveFlight(output, write_all)) {
141     output.close();
142     SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
143     return true;
144   } else {
145     SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
146     return false;
147   }
148 }
149
150
151 /**
152  * Built-in command: (re)load the panel.
153  *
154  * path (optional): the file name to load the panel from 
155  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
156  * and if that's unspecified, to "Panels/Default/default.xml".
157  */
158 static bool
159 do_panel_load (const SGPropertyNode * arg)
160 {
161   string panel_path =
162     arg->getStringValue("path",
163                         fgGetString("/sim/panel/path",
164                                     "Panels/Default/default.xml"));
165   FGPanel * new_panel = fgReadPanel(panel_path);
166   if (new_panel == 0) {
167     SG_LOG(SG_INPUT, SG_ALERT,
168            "Error reading new panel from " << panel_path);
169     return false;
170   }
171   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
172   current_panel->unbind();
173   delete current_panel;
174   current_panel = new_panel;
175   current_panel->bind();
176   return true;
177 }
178
179
180 /**
181  * Built-in command: pass a mouse click to the panel.
182  *
183  * button: the mouse button number, zero-based.
184  * is-down: true if the button is down, false if it is up.
185  * x-pos: the x position of the mouse click.
186  * y-pos: the y position of the mouse click.
187  */
188 static bool
189 do_panel_mouse_click (const SGPropertyNode * arg)
190 {
191   if (current_panel != 0)
192     return current_panel
193       ->doMouseAction(arg->getIntValue("button"),
194                       arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
195                       arg->getIntValue("x-pos"),
196                       arg->getIntValue("y-pos"));
197   else
198     return false;
199 }
200
201
202 /**
203  * Built-in command: (re)load preferences.
204  *
205  * path (optional): the file name to load the panel from (relative
206  * to FG_ROOT). Defaults to "preferences.xml".
207  */
208 static bool
209 do_preferences_load (const SGPropertyNode * arg)
210 {
211   try {
212     fgLoadProps(arg->getStringValue("path", "preferences.xml"),
213                 globals->get_props());
214   } catch (const sg_exception &e) {
215     guiErrorMessage("Error reading global preferences: ", e);
216     return false;
217   }
218   SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
219   return true;
220 }
221
222
223 static void
224 fix_hud_visibility()
225 {
226   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
227       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
228       if ( globals->get_viewmgr()->get_current() == 1 ) {
229           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
230       }
231   }
232 }
233
234 void
235 do_view_next( bool )
236 {
237     globals->get_current_view()->setHeadingOffset_deg(0.0);
238     globals->get_viewmgr()->next_view();
239     fix_hud_visibility();
240     globals->get_tile_mgr()->refresh_view_timestamps();
241 }
242
243 void
244 do_view_prev( bool )
245 {
246     globals->get_current_view()->setHeadingOffset_deg(0.0);
247     globals->get_viewmgr()->prev_view();
248     fix_hud_visibility();
249     globals->get_tile_mgr()->refresh_view_timestamps();
250 }
251
252 /**
253  * Built-in command: cycle view.
254  */
255 static bool
256 do_view_cycle (const SGPropertyNode * arg)
257 {
258   globals->get_current_view()->setHeadingOffset_deg(0.0);
259   globals->get_viewmgr()->next_view();
260   fix_hud_visibility();
261   globals->get_tile_mgr()->refresh_view_timestamps();
262 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
263   return true;
264 }
265
266 /**
267  * Built-in command: capture screen.
268  */
269 static bool
270 do_screen_capture (const SGPropertyNode * arg)
271 {
272   fgDumpSnapShot();
273   return true;
274 }
275
276
277 /**
278  * Reload the tile cache.
279  */
280 static bool
281 do_tile_cache_reload (const SGPropertyNode * arg)
282 {
283     static const SGPropertyNode *master_freeze
284         = fgGetNode("/sim/freeze/master");
285     bool freeze = master_freeze->getBoolValue();
286     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
287     if ( !freeze ) {
288         fgSetBool("/sim/freeze/master", true);
289     }
290     // BusyCursor(0);
291     if ( globals->get_tile_mgr()->init() ) {
292         // Load the local scenery data
293         double visibility_meters = fgGetDouble("/environment/visibility-m");
294         globals->get_tile_mgr()->update( visibility_meters );
295     } else {
296         SG_LOG( SG_GENERAL, SG_ALERT, 
297                 "Error in Tile Manager initialization!" );
298         exit(-1);
299     }
300     // BusyCursor(1);
301     if ( !freeze ) {
302         fgSetBool("/sim/freeze/master", false);
303     }
304     return true;
305 }
306
307
308 /**
309  * Update the lighting manually.
310  */
311 static bool
312 do_lighting_update (const SGPropertyNode * arg)
313 {
314   fgUpdateSkyAndLightingParams();
315   return true;
316 }
317
318
319 /**
320  * Built-in command: toggle a bool property value.
321  *
322  * property: The name of the property to toggle.
323  */
324 static bool
325 do_property_toggle (const SGPropertyNode * arg)
326 {
327   SGPropertyNode * prop = get_prop(arg);
328   return prop->setBoolValue(!prop->getBoolValue());
329 }
330
331
332 /**
333  * Built-in command: assign a value to a property.
334  *
335  * property: the name of the property to assign.
336  * value: the value to assign.
337  */
338 static bool
339 do_property_assign (const SGPropertyNode * arg)
340 {
341   SGPropertyNode * prop = get_prop(arg);
342   const SGPropertyNode * value = arg->getNode("value");
343
344   switch (prop->getType()) {
345
346   case SGPropertyNode::BOOL:
347     return prop->setBoolValue(value->getBoolValue());
348
349   case SGPropertyNode::INT:
350     return prop->setIntValue(value->getIntValue());
351
352   case SGPropertyNode::LONG:
353     return prop->setLongValue(value->getLongValue());
354
355   case SGPropertyNode::FLOAT:
356     return prop->setFloatValue(value->getFloatValue());
357
358   case SGPropertyNode::DOUBLE:
359     return prop->setDoubleValue(value->getDoubleValue());
360
361   case SGPropertyNode::STRING:
362     return prop->setStringValue(value->getStringValue());
363
364   default:
365     return prop->setUnspecifiedValue(value->getStringValue());
366
367   }
368 }
369
370
371 /**
372  * Built-in command: increment or decrement a property value.
373  *
374  * property: the name of the property to increment or decrement.
375  * step: the amount of the increment or decrement (default: 0).
376  * offset: a normalized amount to offset by (if step is not present).
377  * factor: the amount by which to multiply the offset (if step is not present).
378  * min: the minimum allowed value (default: no minimum).
379  * max: the maximum allowed value (default: no maximum).
380  * wrap: true if the value should be wrapped when it passes min or max;
381  *       both min and max must be present for this to work (default:
382  *       false).
383  */
384 static bool
385 do_property_adjust (const SGPropertyNode * arg)
386 {
387   SGPropertyNode * prop = get_prop(arg);
388   const SGPropertyNode * step = arg->getChild("step");
389   const SGPropertyNode * offset = arg->getChild("offset");
390   const SGPropertyNode * factor = arg->getChild("factor");
391   const SGPropertyNode * min = arg->getChild("min");
392   const SGPropertyNode * max = arg->getChild("max");
393   bool wrap = arg->getBoolValue("wrap");
394
395   if (min == 0 || max == 0)
396       wrap = false;
397
398   double amount = 0;
399   if (step == 0)
400     amount = offset->getDoubleValue() * factor->getDoubleValue();
401
402   switch (prop->getType()) {
403
404   case SGPropertyNode::BOOL: {
405     bool value;
406     if (step != 0)
407       value = step->getBoolValue();
408     else
409       value = (0.0 != amount);
410     if (value)
411       return prop->setBoolValue(!prop->getBoolValue());
412     else
413       return true;
414   }
415
416   case SGPropertyNode::INT: {
417     int value;
418     if (step != 0)
419       value = prop->getIntValue() + step->getIntValue();
420     else
421       value = prop->getIntValue() + int(amount);
422     if (wrap) {
423         do_wrap(&value, min->getIntValue(), max->getIntValue());
424     } else {
425         if (min != 0 && value < min->getIntValue())
426             value = min->getIntValue();
427         if (max != 0 && value > max->getIntValue())
428             value = max->getIntValue();
429     }
430     return prop->setIntValue(value);
431   }
432
433   case SGPropertyNode::LONG: {
434     long value;
435     if (step != 0)
436       value = prop->getLongValue() + step->getLongValue();
437     else
438       value = prop->getLongValue() + long(amount);
439     if (wrap) {
440         do_wrap(&value, min->getLongValue(), max->getLongValue());
441     } else {
442         if (min != 0 && value < min->getLongValue())
443             value = min->getLongValue();
444         if (max != 0 && value > max->getLongValue())
445             value = max->getLongValue();
446     }
447     return prop->setLongValue(value);
448   }
449
450   case SGPropertyNode::FLOAT: {
451     float value;
452     if (step != 0)
453       value = prop->getFloatValue() + step->getFloatValue();
454     else
455       value = prop->getFloatValue() + float(amount);
456     if (wrap) {
457         do_wrap(&value, min->getFloatValue(), max->getFloatValue());
458     } else {
459         if (min != 0 && value < min->getFloatValue())
460             value = min->getFloatValue();
461         if (max != 0 && value > max->getFloatValue())
462             value = max->getFloatValue();
463     }
464     return prop->setFloatValue(value);
465   }
466
467   case SGPropertyNode::DOUBLE:
468   case SGPropertyNode::UNSPECIFIED:
469   case SGPropertyNode::NONE: {
470     double value;
471     if (step != 0)
472       value = prop->getDoubleValue() + step->getDoubleValue();
473     else
474       value = prop->getDoubleValue() + amount;
475     if (wrap) {
476         do_wrap(&value, min->getDoubleValue(), max->getDoubleValue());
477     } else {
478         if (min != 0 && value < min->getDoubleValue())
479             value = min->getDoubleValue();
480         if (max != 0 && value > max->getDoubleValue())
481             value = max->getDoubleValue();
482     }
483     return prop->setDoubleValue(value);
484   }
485
486   case SGPropertyNode::STRING: // doesn't make sense with strings
487     SG_LOG(SG_INPUT, SG_ALERT, "Cannot adjust a string value");
488     return false;
489
490   default:
491     SG_LOG(SG_INPUT, SG_ALERT, "Unknown value type");
492     return false;
493
494   }
495 }
496
497
498 /**
499  * Built-in command: multiply a property value.
500  *
501  * property: the name of the property to multiply.
502  * factor: the amount by which to multiply.
503  */
504 static bool
505 do_property_multiply (const SGPropertyNode * arg)
506 {
507   SGPropertyNode * prop = get_prop(arg);
508   const SGPropertyNode * factor = arg->getChild("factor");
509
510   if (factor == 0)
511       return true;
512
513   switch (prop->getType()) {
514
515   case SGPropertyNode::BOOL:
516     return prop->setBoolValue(prop->getBoolValue() && factor->getBoolValue());
517
518   case SGPropertyNode::INT:
519     return prop->setIntValue(int(prop->getIntValue()
520                                  * factor->getDoubleValue()));
521
522   case SGPropertyNode::LONG:
523     return prop->setLongValue(long(prop->getLongValue()
524                                    * factor->getDoubleValue()));
525
526   case SGPropertyNode::FLOAT:
527     return prop->setFloatValue(float(prop->getFloatValue()
528                                      * factor->getDoubleValue()));
529
530   case SGPropertyNode::DOUBLE:
531   case SGPropertyNode::UNSPECIFIED:
532   case SGPropertyNode::NONE:
533     return prop->setDoubleValue(prop->getDoubleValue()
534                                 * factor->getDoubleValue());
535
536   default:                      // doesn't make sense with strings
537     return false;
538   }
539 }
540
541
542 /**
543  * Built-in command: swap two property values.
544  *
545  * property[0]: the name of the first property.
546  * property[1]: the name of the second property.
547  */
548 static bool
549 do_property_swap (const SGPropertyNode * arg)
550 {
551   SGPropertyNode * prop1 = get_prop(arg);
552   SGPropertyNode * prop2 = get_prop2(arg);
553
554                                 // FIXME: inefficient
555   const string & tmp = prop1->getStringValue();
556   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
557           prop2->setUnspecifiedValue(tmp.c_str()));
558 }
559
560
561 /**
562  * Built-in command: Set a property to an axis or other moving input.
563  *
564  * property: the name of the property to set.
565  * setting: the current input setting, usually between -1.0 and 1.0.
566  * offset: the offset to shift by, before applying the factor.
567  * factor: the factor to multiply by (use negative to reverse).
568  */
569 static bool
570 do_property_scale (const SGPropertyNode * arg)
571 {
572   SGPropertyNode * prop = get_prop(arg);
573   double setting = arg->getDoubleValue("setting");
574   double offset = arg->getDoubleValue("offset", 0.0);
575   double factor = arg->getDoubleValue("factor", 1.0);
576   bool squared = arg->getBoolValue("squared", false);
577
578   if (squared)
579     setting = (setting < 0 ? -1 : 1) * setting * setting;
580
581   return prop->setDoubleValue((setting + offset) * factor);
582 }
583
584
585 /**
586  * Built-in command: Show an XML-configured dialog.
587  *
588  * dialog-name: the name of the GUI dialog to display.
589  */
590 static bool
591 do_dialog_show (const SGPropertyNode * arg)
592 {
593     NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
594         ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
595     gui->display(arg->getStringValue("dialog-name"));
596     return true;
597 }
598
599
600 /**
601  * Hide the active XML-configured dialog.
602  */
603 static bool
604 do_dialog_close (const SGPropertyNode * arg)
605 {
606     NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
607         ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
608     GUIWidget * widget = gui->getCurrentWidget();
609     if (widget != 0) {
610         delete widget;
611         gui->setCurrentWidget(0);
612         return true;
613     } else {
614         return false;
615     }
616 }
617
618
619 /**
620  * Update a value in the active XML-configured dialog.
621  *
622  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
623  */
624 static bool
625 do_dialog_update (const SGPropertyNode * arg)
626 {
627     NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
628         ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
629     GUIWidget * widget = gui->getCurrentWidget();
630     if (widget != 0) {
631         if (arg->hasValue("object-name")) {
632             gui->getCurrentWidget()
633                 ->updateValue(arg->getStringValue("object-name"));
634         } else {
635             gui->getCurrentWidget()->updateValues();
636         }
637         return true;
638     } else {
639         return false;
640     }
641 }
642
643
644 /**
645  * Apply a value in the active XML-configured dialog.
646  *
647  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
648  */
649 static bool
650 do_dialog_apply (const SGPropertyNode * arg)
651 {
652     NewGUI * gui = (NewGUI *)globals->get_subsystem_mgr()
653         ->get_group(FGSubsystemMgr::INIT)->get_subsystem("gui");
654     GUIWidget * widget = gui->getCurrentWidget();
655     if (widget != 0) {
656         if (arg->hasValue("object-name")) {
657             gui->getCurrentWidget()
658                 ->applyValue(arg->getStringValue("object-name"));
659         } else {
660             gui->getCurrentWidget()->applyValues();
661         }
662         return true;
663     } else {
664         return false;
665     }
666 }
667
668
669 /**
670  * Built-in command: commit presets (read from in /sim/presets/)
671  */
672 static bool
673 do_presets_commit (const SGPropertyNode * arg)
674 {
675     // unbind the current fdm state so property changes
676     // don't get lost when we subsequently delete this fdm
677     // and create a new one.
678     cur_fdm_state->unbind();
679         
680     // set position from presets
681     fgInitPosition();
682
683     // BusyCursor(0);
684     fgReInitSubsystems();
685
686     globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
687
688     if ( ! fgGetBool("/sim/presets/onground") ) {
689         fgSetBool( "/sim/freeze/master", true );
690         fgSetBool( "/sim/freeze/clock", true );
691     }
692
693     return true;
694 }
695
696
697 \f
698 ////////////////////////////////////////////////////////////////////////
699 // Command setup.
700 ////////////////////////////////////////////////////////////////////////
701
702
703 /**
704  * Table of built-in commands.
705  *
706  * New commands do not have to be added here; any module in the application
707  * can add a new command using globals->get_commands()->addCommand(...).
708  */
709 static struct {
710   const char * name;
711   SGCommandMgr::command_t command;
712 } built_ins [] = {
713     { "null", do_null },
714     { "exit", do_exit },
715     { "load", do_load },
716     { "save", do_save },
717     { "panel-load", do_panel_load },
718     { "panel-mouse-click", do_panel_mouse_click },
719     { "preferences-load", do_preferences_load },
720     { "view-cycle", do_view_cycle },
721     { "screen-capture", do_screen_capture },
722     { "tile-cache-reload", do_tile_cache_reload },
723     { "lighting-update", do_lighting_update },
724     { "property-toggle", do_property_toggle },
725     { "property-assign", do_property_assign },
726     { "property-adjust", do_property_adjust },
727     { "property-multiply", do_property_multiply },
728     { "property-swap", do_property_swap },
729     { "property-scale", do_property_scale },
730     { "dialog-show", do_dialog_show },
731     { "dialog-close", do_dialog_close },
732     { "dialog-show", do_dialog_show },
733     { "dialog-update", do_dialog_update },
734     { "dialog-apply", do_dialog_apply },
735     { 0, 0 }                    // zero-terminated
736 };
737
738
739 /**
740  * Initialize the default built-in commands.
741  *
742  * Other commands may be added by other parts of the application.
743  */
744 void
745 fgInitCommands ()
746 {
747   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
748   for (int i = 0; built_ins[i].name != 0; i++) {
749     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
750     globals->get_commands()->addCommand(built_ins[i].name,
751                                         built_ins[i].command);
752   }
753
754   typedef bool (*dummy)();
755   fgTie( "/command/view/next", dummy(0), do_view_next );
756   fgTie( "/command/view/prev", dummy(0), do_view_prev );
757 }
758
759 // end of fg_commands.cxx