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