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