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