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