]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
Turn back to default lighting when specular-highlight gets disabled
[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/sg_inlines.h>
12 #include <simgear/debug/logstream.hxx>
13 #include <simgear/math/sg_random.h>
14 #include <simgear/misc/commands.hxx>
15 #include <simgear/props/props.hxx>
16
17 #include <Cockpit/panel.hxx>
18 #include <Cockpit/panel_io.hxx>
19 #include <FDM/flight.hxx>
20 #include <GUI/gui.h>
21 #include <GUI/new_gui.hxx>
22 #include <GUI/dialog.hxx>
23 #include <Replay/replay.hxx>
24 #include <Scenery/tilemgr.hxx>
25 #if defined(HAVE_PLIB_PSL)
26 #  include <Scripting/scriptmgr.hxx>
27 #endif
28 #include <Time/tmp.hxx>
29
30 #include "fg_init.hxx"
31 #include "fg_commands.hxx"
32 #include "fg_props.hxx"
33
34 SG_USING_STD(string);
35 SG_USING_STD(ifstream);
36 SG_USING_STD(ofstream);
37
38 #include "fg_props.hxx"
39 #include "fg_io.hxx"
40 #include "globals.hxx"
41 #include "util.hxx"
42 #include "viewmgr.hxx"
43
44
45 \f
46 ////////////////////////////////////////////////////////////////////////
47 // Static helper functions.
48 ////////////////////////////////////////////////////////////////////////
49
50
51 static inline SGPropertyNode *
52 get_prop (const SGPropertyNode * arg)
53 {
54     return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
55 }
56
57 static inline SGPropertyNode *
58 get_prop2 (const SGPropertyNode * arg)
59 {
60     return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
61 }
62
63
64 /**
65  * Get a double value and split it as required.
66  */
67 static void
68 split_value (double full_value, const char * mask,
69              double * unmodifiable, double * modifiable)
70 {
71     if (!strcmp("integer", mask)) {
72         *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
73         *unmodifiable = full_value - *modifiable;
74     } else if (!strcmp("decimal", mask)) {
75         *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
76         *modifiable = full_value - *unmodifiable;
77     } else {
78         if (strcmp("all", mask))
79             SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
80                    << " assuming 'all'");
81         *unmodifiable = 0;
82         *modifiable = full_value;
83     }
84 }
85
86
87 /**
88  * Clamp or wrap a value as specified.
89  */
90 static void
91 limit_value (double * value, const SGPropertyNode * arg)
92 {
93     const SGPropertyNode * min_node = arg->getChild("min");
94     const SGPropertyNode * max_node = arg->getChild("max");
95
96     bool wrap = arg->getBoolValue("wrap");
97
98     if (min_node == 0 || max_node == 0)
99         wrap = false;
100   
101     if (wrap) {                 // wrap such that min <= x < max
102         double min_val = min_node->getDoubleValue();
103         double max_val = max_node->getDoubleValue();
104         double resolution = arg->getDoubleValue("resolution");
105         if (resolution > 0.0) {
106             // snap to (min + N*resolution), taking special care to handle imprecision
107             int n = (int)floor((*value - min_val) / resolution + 0.5);
108             int steps = (int)floor((max_val - min_val) / resolution + 0.5);
109             SG_NORMALIZE_RANGE(n, 0, steps);
110             *value = min_val + resolution * n;
111         } else {
112             // plain circular wrapping
113             SG_NORMALIZE_RANGE(*value, min_val, max_val);
114         }
115     } else {                    // clamp such that min <= x <= max
116         if ((min_node != 0) && (*value < min_node->getDoubleValue()))
117             *value = min_node->getDoubleValue();
118         else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
119             *value = max_node->getDoubleValue();
120     }
121 }
122
123 static bool
124 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
125 {
126     switch (value1->getType()) {
127     case SGPropertyNode::BOOL:
128         return (value1->getBoolValue() == value2->getBoolValue());
129     case SGPropertyNode::INT:
130         return (value1->getIntValue() == value2->getIntValue());
131     case SGPropertyNode::LONG:
132         return (value1->getLongValue() == value2->getLongValue());
133     case SGPropertyNode::FLOAT:
134         return (value1->getFloatValue() == value2->getFloatValue());
135     case SGPropertyNode::DOUBLE:
136         return (value1->getDoubleValue() == value2->getDoubleValue());
137     default:
138         return !strcmp(value1->getStringValue(), value2->getStringValue());
139     }
140 }
141
142
143 \f
144 ////////////////////////////////////////////////////////////////////////
145 // Command implementations.
146 ////////////////////////////////////////////////////////////////////////
147
148
149 /**
150  * Built-in command: do nothing.
151  */
152 static bool
153 do_null (const SGPropertyNode * arg)
154 {
155   return true;
156 }
157
158 #if defined(HAVE_PLIB_PSL)
159 /**
160  * Built-in command: run a PSL script.
161  *
162  * script: the PSL script to execute
163  */
164 static bool
165 do_script (const SGPropertyNode * arg)
166 {
167     FGScriptMgr * mgr = (FGScriptMgr *)globals->get_subsystem("scripting");
168     return mgr->run(arg->getStringValue("script"));
169 }
170 #endif // HAVE_PLIB_PSL
171
172
173 /**
174  * Built-in command: exit FlightGear.
175  *
176  * status: the exit status to return to the operating system (defaults to 0)
177  */
178 static bool
179 do_exit (const SGPropertyNode * arg)
180 {
181   SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
182   fgExit(arg->getIntValue("status", 0));
183   return true;
184 }
185
186
187 /**
188  * Built-in command: reinitialize one or more subsystems.
189  *
190  * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
191  * none is specified, reinitialize all of them.
192  */
193 static bool
194 do_reinit (const SGPropertyNode * arg)
195 {
196     bool result = true;
197
198     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
199     if (subsystems.size() == 0) {
200         globals->get_subsystem_mgr()->reinit();
201     } else {
202         for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
203             const char * name = subsystems[i]->getStringValue();
204             FGSubsystem * subsystem = globals->get_subsystem(name);
205             if (subsystem == 0) {
206                 result = false;
207                 SG_LOG( SG_GENERAL, SG_ALERT,
208                         "Subsystem " << name << "not found" );
209             } else {
210                 subsystem->reinit();
211             }
212         }
213     }
214
215     return result;
216 }
217
218 /**
219  * Built-in command: suspend one or more subsystems.
220  *
221  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
222  */
223 static bool
224 do_suspend (const SGPropertyNode * arg)
225 {
226     bool result = true;
227
228     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
229     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
230         const char * name = subsystems[i]->getStringValue();
231         FGSubsystem * subsystem = globals->get_subsystem(name);
232         if (subsystem == 0) {
233             result = false;
234             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
235         } else {
236             subsystem->suspend();
237         }
238     }
239     return result;
240 }
241
242 /**
243  * Built-in command: suspend one or more subsystems.
244  *
245  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
246  */
247 static bool
248 do_resume (const SGPropertyNode * arg)
249 {
250     bool result = true;
251
252     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
253     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
254         const char * name = subsystems[i]->getStringValue();
255         FGSubsystem * subsystem = globals->get_subsystem(name);
256         if (subsystem == 0) {
257             result = false;
258             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
259         } else {
260             subsystem->resume();
261         }
262     }
263     return result;
264 }
265
266
267 /**
268  * Built-in command: load flight.
269  *
270  * file (optional): the name of the file to load (relative to current
271  *   directory).  Defaults to "fgfs.sav"
272  */
273 static bool
274 do_load (const SGPropertyNode * arg)
275 {
276   const string &file = arg->getStringValue("file", "fgfs.sav");
277   ifstream input(file.c_str());
278   if (input.good() && fgLoadFlight(input)) {
279     input.close();
280     SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
281     return true;
282   } else {
283     SG_LOG(SG_INPUT, SG_ALERT, "Cannot load flight from " << file);
284     return false;
285   }
286 }
287
288
289 /**
290  * Built-in command: save flight.
291  *
292  * file (optional): the name of the file to save (relative to the
293  * current directory).  Defaults to "fgfs.sav".
294  */
295 static bool
296 do_save (const SGPropertyNode * arg)
297 {
298   const string &file = arg->getStringValue("file", "fgfs.sav");
299   bool write_all = arg->getBoolValue("write-all", false);
300   SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
301   ofstream output(file.c_str());
302   if (output.good() && fgSaveFlight(output, write_all)) {
303     output.close();
304     SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
305     return true;
306   } else {
307     SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
308     return false;
309   }
310 }
311
312
313 /**
314  * Built-in command: (re)load the panel.
315  *
316  * path (optional): the file name to load the panel from 
317  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
318  * and if that's unspecified, to "Panels/Default/default.xml".
319  */
320 static bool
321 do_panel_load (const SGPropertyNode * arg)
322 {
323   string panel_path =
324     arg->getStringValue("path",
325                         fgGetString("/sim/panel/path",
326                                     "Panels/Default/default.xml"));
327   FGPanel * new_panel = fgReadPanel(panel_path);
328   if (new_panel == 0) {
329     SG_LOG(SG_INPUT, SG_ALERT,
330            "Error reading new panel from " << panel_path);
331     return false;
332   }
333   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
334   globals->get_current_panel()->unbind();
335   delete globals->get_current_panel();
336   globals->set_current_panel( new_panel );
337   globals->get_current_panel()->bind();
338   return true;
339 }
340
341
342 /**
343  * Built-in command: pass a mouse click to the panel.
344  *
345  * button: the mouse button number, zero-based.
346  * is-down: true if the button is down, false if it is up.
347  * x-pos: the x position of the mouse click.
348  * y-pos: the y position of the mouse click.
349  */
350 static bool
351 do_panel_mouse_click (const SGPropertyNode * arg)
352 {
353   if (globals->get_current_panel() != 0)
354     return globals->get_current_panel()
355       ->doMouseAction(arg->getIntValue("button"),
356                       arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
357                       arg->getIntValue("x-pos"),
358                       arg->getIntValue("y-pos"));
359   else
360     return false;
361 }
362
363
364 /**
365  * Built-in command: (re)load preferences.
366  *
367  * path (optional): the file name to load the panel from (relative
368  * to FG_ROOT). Defaults to "preferences.xml".
369  */
370 static bool
371 do_preferences_load (const SGPropertyNode * arg)
372 {
373   try {
374     fgLoadProps(arg->getStringValue("path", "preferences.xml"),
375                 globals->get_props());
376   } catch (const sg_exception &e) {
377     guiErrorMessage("Error reading global preferences: ", e);
378     return false;
379   }
380   SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
381   return true;
382 }
383
384
385 static void
386 fix_hud_visibility()
387 {
388   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
389       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
390       if ( globals->get_viewmgr()->get_current() == 1 ) {
391           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
392       }
393   }
394 }
395
396 static void
397 do_view_next( bool )
398 {
399     globals->get_current_view()->setHeadingOffset_deg(0.0);
400     globals->get_viewmgr()->next_view();
401     fix_hud_visibility();
402     globals->get_tile_mgr()->refresh_view_timestamps();
403 }
404
405 static void
406 do_view_prev( bool )
407 {
408     globals->get_current_view()->setHeadingOffset_deg(0.0);
409     globals->get_viewmgr()->prev_view();
410     fix_hud_visibility();
411     globals->get_tile_mgr()->refresh_view_timestamps();
412 }
413
414 /**
415  * Built-in command: cycle view.
416  */
417 static bool
418 do_view_cycle (const SGPropertyNode * arg)
419 {
420   globals->get_current_view()->setHeadingOffset_deg(0.0);
421   globals->get_viewmgr()->next_view();
422   fix_hud_visibility();
423   globals->get_tile_mgr()->refresh_view_timestamps();
424 //   fgReshape(fgGetInt("/sim/startup/xsize"), fgGetInt("/sim/startup/ysize"));
425   return true;
426 }
427
428 /**
429  * Built-in command: capture screen.
430  */
431 static bool
432 do_screen_capture (const SGPropertyNode * arg)
433 {
434   fgDumpSnapShot();
435   return true;
436 }
437
438
439 /**
440  * Reload the tile cache.
441  */
442 static bool
443 do_tile_cache_reload (const SGPropertyNode * arg)
444 {
445     static const SGPropertyNode *master_freeze
446         = fgGetNode("/sim/freeze/master");
447     bool freeze = master_freeze->getBoolValue();
448     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
449     if ( !freeze ) {
450         fgSetBool("/sim/freeze/master", true);
451     }
452     // BusyCursor(0);
453     if ( globals->get_tile_mgr()->init() ) {
454         // Load the local scenery data
455         double visibility_meters = fgGetDouble("/environment/visibility-m");
456         globals->get_tile_mgr()->update( visibility_meters );
457     } else {
458         SG_LOG( SG_GENERAL, SG_ALERT, 
459                 "Error in Tile Manager initialization!" );
460         exit(-1);
461     }
462     // BusyCursor(1);
463     if ( !freeze ) {
464         fgSetBool("/sim/freeze/master", false);
465     }
466     return true;
467 }
468
469
470 /**
471  * Update the lighting manually.
472  */
473 static bool
474 do_lighting_update (const SGPropertyNode * arg)
475 {
476   fgUpdateSkyAndLightingParams();
477   return true;
478 }
479
480
481 /**
482  * Built-in command: toggle a bool property value.
483  *
484  * property: The name of the property to toggle.
485  */
486 static bool
487 do_property_toggle (const SGPropertyNode * arg)
488 {
489   SGPropertyNode * prop = get_prop(arg);
490   return prop->setBoolValue(!prop->getBoolValue());
491 }
492
493
494 /**
495  * Built-in command: assign a value to a property.
496  *
497  * property: the name of the property to assign.
498  * value: the value to assign; or
499  * property[1]: the property to copy from.
500  */
501 static bool
502 do_property_assign (const SGPropertyNode * arg)
503 {
504   SGPropertyNode * prop = get_prop(arg);
505   const SGPropertyNode * prop2 = get_prop2(arg);
506   const SGPropertyNode * value = arg->getNode("value");
507
508   if (value != 0)
509       return prop->setUnspecifiedValue(value->getStringValue());
510   else if (prop2)
511       return prop->setUnspecifiedValue(prop2->getStringValue());
512   else
513       return false;
514 }
515
516
517 /**
518  * Built-in command: increment or decrement a property value.
519  *
520  * If the 'step' argument is present, it will be used; otherwise,
521  * the command uses 'offset' and 'factor', usually from the mouse.
522  *
523  * property: the name of the property to increment or decrement.
524  * step: the amount of the increment or decrement (default: 0).
525  * offset: offset from the current setting (used for the mouse; multiplied 
526  *         by factor)
527  * factor: scaling amount for the offset (defaults to 1).
528  * min: the minimum allowed value (default: no minimum).
529  * max: the maximum allowed value (default: no maximum).
530  * mask: 'integer' to apply only to the left of the decimal point, 
531  *       'decimal' to apply only to the right of the decimal point,
532  *       or 'all' to apply to the whole number (the default).
533  * wrap: true if the value should be wrapped when it passes min or max;
534  *       both min and max must be present for this to work (default:
535  *       false).
536  */
537 static bool
538 do_property_adjust (const SGPropertyNode * arg)
539 {
540   SGPropertyNode * prop = get_prop(arg);
541
542   double amount = 0;
543   if (arg->hasValue("step"))
544       amount = arg->getDoubleValue("step");
545   else
546       amount = (arg->getDoubleValue("factor", 1)
547                 * arg->getDoubleValue("offset"));
548           
549   double unmodifiable, modifiable;
550   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
551               &unmodifiable, &modifiable);
552   modifiable += amount;
553   limit_value(&modifiable, arg);
554
555   prop->setDoubleValue(unmodifiable + modifiable);
556
557   return true;
558 }
559
560
561 /**
562  * Built-in command: multiply a property value.
563  *
564  * property: the name of the property to multiply.
565  * factor: the amount by which to multiply.
566  * min: the minimum allowed value (default: no minimum).
567  * max: the maximum allowed value (default: no maximum).
568  * mask: 'integer' to apply only to the left of the decimal point, 
569  *       'decimal' to apply only to the right of the decimal point,
570  *       or 'all' to apply to the whole number (the default).
571  * wrap: true if the value should be wrapped when it passes min or max;
572  *       both min and max must be present for this to work (default:
573  *       false).
574  */
575 static bool
576 do_property_multiply (const SGPropertyNode * arg)
577 {
578   SGPropertyNode * prop = get_prop(arg);
579   double factor = arg->getDoubleValue("factor", 1);
580
581   double unmodifiable, modifiable;
582   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
583               &unmodifiable, &modifiable);
584   modifiable *= factor;
585   limit_value(&modifiable, arg);
586
587   prop->setDoubleValue(unmodifiable + modifiable);
588
589   return true;
590 }
591
592
593 /**
594  * Built-in command: swap two property values.
595  *
596  * property[0]: the name of the first property.
597  * property[1]: the name of the second property.
598  */
599 static bool
600 do_property_swap (const SGPropertyNode * arg)
601 {
602   SGPropertyNode * prop1 = get_prop(arg);
603   SGPropertyNode * prop2 = get_prop2(arg);
604
605                                 // FIXME: inefficient
606   const string & tmp = prop1->getStringValue();
607   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
608           prop2->setUnspecifiedValue(tmp.c_str()));
609 }
610
611
612 /**
613  * Built-in command: Set a property to an axis or other moving input.
614  *
615  * property: the name of the property to set.
616  * setting: the current input setting, usually between -1.0 and 1.0.
617  * offset: the offset to shift by, before applying the factor.
618  * factor: the factor to multiply by (use negative to reverse).
619  */
620 static bool
621 do_property_scale (const SGPropertyNode * arg)
622 {
623   SGPropertyNode * prop = get_prop(arg);
624   double setting = arg->getDoubleValue("setting");
625   double offset = arg->getDoubleValue("offset", 0.0);
626   double factor = arg->getDoubleValue("factor", 1.0);
627   bool squared = arg->getBoolValue("squared", false);
628   int power = arg->getIntValue("power", (squared ? 2 : 1));
629
630   int sign = (setting < 0 ? -1 : 1);
631
632   switch (power) {
633   case 1:
634       break;
635   case 2:
636       setting = setting * setting * sign;
637       break;
638   case 3:
639       setting = setting * setting * setting;
640       break;
641   case 4:
642       setting = setting * setting * setting * setting * sign;
643       break;
644   default:
645       setting =  pow(setting, power);
646       if ((power % 2) == 0)
647           setting *= sign;
648       break;
649   }
650
651   return prop->setDoubleValue((setting + offset) * factor);
652 }
653
654
655 /**
656  * Built-in command: cycle a property through a set of values.
657  *
658  * If the current value isn't in the list, the cycle will
659  * (re)start from the beginning.
660  *
661  * property: the name of the property to cycle.
662  * value[*]: the list of values to cycle through.
663  */
664 static bool
665 do_property_cycle (const SGPropertyNode * arg)
666 {
667     SGPropertyNode * prop = get_prop(arg);
668     vector<SGPropertyNode_ptr> values = arg->getChildren("value");
669     int selection = -1;
670     int nSelections = values.size();
671
672     if (nSelections < 1) {
673         SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
674         return false;
675     }
676
677                                 // Try to find the current selection
678     for (int i = 0; i < nSelections; i++) {
679         if (compare_values(prop, values[i])) {
680             selection = i + 1;
681             break;
682         }
683     }
684
685                                 // Default or wrap to the first selection
686     if (selection < 0 || selection >= nSelections)
687         selection = 0;
688
689     prop->setUnspecifiedValue(values[selection]->getStringValue());
690     return true;
691 }
692
693
694 /**
695  * Built-in command: randomize a numeric property value.
696  *
697  * property: the name of the property value to randomize.
698  * min: the minimum allowed value.
699  * max: the maximum allowed value.
700  */
701 static bool
702 do_property_randomize (const SGPropertyNode * arg)
703 {
704     SGPropertyNode * prop = get_prop(arg);
705     double min = arg->getDoubleValue("min", DBL_MIN);
706     double max = arg->getDoubleValue("max", DBL_MAX);
707     prop->setDoubleValue(sg_random() * (max - min) + min);
708     return true;
709 }
710
711
712 /**
713  * Built-in command: Show an XML-configured dialog.
714  *
715  * dialog-name: the name of the GUI dialog to display.
716  */
717 static bool
718 do_dialog_show (const SGPropertyNode * arg)
719 {
720     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
721     gui->showDialog(arg->getStringValue("dialog-name"));
722     return true;
723 }
724
725
726 /**
727  * Built-in Command: Hide the active XML-configured dialog.
728  */
729 static bool
730 do_dialog_close (const SGPropertyNode * arg)
731 {
732     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
733     return gui->closeActiveDialog();
734 }
735
736
737 /**
738  * Update a value in the active XML-configured dialog.
739  *
740  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
741  */
742 static bool
743 do_dialog_update (const SGPropertyNode * arg)
744 {
745     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
746     FGDialog * dialog = gui->getActiveDialog();
747     if (dialog != 0) {
748         if (arg->hasValue("object-name")) {
749             dialog->updateValue(arg->getStringValue("object-name"));
750         } else {
751             dialog->updateValues();
752         }
753         return true;
754     } else {
755         return false;
756     }
757 }
758
759
760 /**
761  * Apply a value in the active XML-configured dialog.
762  *
763  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
764  */
765 static bool
766 do_dialog_apply (const SGPropertyNode * arg)
767 {
768     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
769     FGDialog * dialog = gui->getActiveDialog();
770     if (dialog != 0) {
771         if (arg->hasValue("object-name")) {
772             const char * name = arg->getStringValue("object-name");
773             dialog->applyValue(name);
774             dialog->updateValue(name);
775         } else {
776             dialog->applyValues();
777             dialog->updateValues();
778         }
779         return true;
780     } else {
781         return false;
782     }
783 }
784
785
786 /**
787  * Built-in command: commit presets (read from in /sim/presets/)
788  */
789 static bool
790 do_presets_commit (const SGPropertyNode * arg)
791 {
792     // unbind the current fdm state so property changes
793     // don't get lost when we subsequently delete this fdm
794     // and create a new one.
795     cur_fdm_state->unbind();
796         
797     // set position from presets
798     fgInitPosition();
799
800     // BusyCursor(0);
801     fgReInitSubsystems();
802
803     globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
804
805 #if 0
806     if ( ! fgGetBool("/sim/presets/onground") ) {
807         fgSetBool( "/sim/freeze/master", true );
808         fgSetBool( "/sim/freeze/clock", true );
809     }
810 #endif
811
812     return true;
813 }
814
815 /**
816  * Built-in command: set log level (0 ... 7)
817  */
818 static bool
819 do_log_level (const SGPropertyNode * arg)
820 {
821    sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
822
823    return true;
824 }
825
826 /**
827  * Built-in command: replay the FDR buffer
828  */
829 static bool
830 do_replay (const SGPropertyNode * arg)
831 {
832     // freeze the master fdm
833     fgSetBool( "/sim/freeze/replay", true );
834
835     FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
836
837     fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
838     fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
839     double duration = fgGetDouble( "/sim/replay/duration" );
840     if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
841         fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
842     } else {
843         fgSetDouble( "/sim/replay/time", r->get_start_time() );
844     }
845
846     cout << "start = " << r->get_start_time()
847          << "  end = " << r->get_end_time() << endl;
848
849     return true;
850 }
851
852
853
854 static bool
855 do_decrease_visibility (const SGPropertyNode * arg)
856 {
857     double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
858     fgSetDouble("/environment/visibility-m", new_value);
859     fgDefaultWeatherValue("visibility-m", new_value);
860     globals->get_subsystem("environment")->reinit();
861
862     return true;
863 }
864  
865 static bool
866 do_increase_visibility (const SGPropertyNode * arg)
867 {
868     double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
869     fgSetDouble("/environment/visibility-m", new_value);
870     fgDefaultWeatherValue("visibility-m", new_value);
871     globals->get_subsystem("environment")->reinit();
872
873     return true;
874 }
875
876
877 ////////////////////////////////////////////////////////////////////////
878 // Command setup.
879 ////////////////////////////////////////////////////////////////////////
880
881
882 /**
883  * Table of built-in commands.
884  *
885  * New commands do not have to be added here; any module in the application
886  * can add a new command using globals->get_commands()->addCommand(...).
887  */
888 static struct {
889   const char * name;
890   SGCommandMgr::command_t command;
891 } built_ins [] = {
892     { "null", do_null },
893 #if defined(HAVE_PLIB_PSL)
894     { "script", do_script },
895 #endif // HAVE_PLIB_PSL
896     { "exit", do_exit },
897     { "reinit", do_reinit },
898     { "suspend", do_reinit },
899     { "resume", do_reinit },
900     { "load", do_load },
901     { "save", do_save },
902     { "panel-load", do_panel_load },
903     { "panel-mouse-click", do_panel_mouse_click },
904     { "preferences-load", do_preferences_load },
905     { "view-cycle", do_view_cycle },
906     { "screen-capture", do_screen_capture },
907     { "tile-cache-reload", do_tile_cache_reload },
908     { "lighting-update", do_lighting_update },
909     { "property-toggle", do_property_toggle },
910     { "property-assign", do_property_assign },
911     { "property-adjust", do_property_adjust },
912     { "property-multiply", do_property_multiply },
913     { "property-swap", do_property_swap },
914     { "property-scale", do_property_scale },
915     { "property-cycle", do_property_cycle },
916     { "property-randomize", do_property_randomize },
917     { "dialog-show", do_dialog_show },
918     { "dialog-close", do_dialog_close },
919     { "dialog-show", do_dialog_show },
920     { "dialog-update", do_dialog_update },
921     { "dialog-apply", do_dialog_apply },
922     { "presets-commit", do_presets_commit },
923     { "log-level", do_log_level },
924     { "replay", do_replay },
925     { "decrease-visibility", do_decrease_visibility },
926     { "increase-visibility", do_increase_visibility },
927     { 0, 0 }                    // zero-terminated
928 };
929
930
931 /**
932  * Initialize the default built-in commands.
933  *
934  * Other commands may be added by other parts of the application.
935  */
936 void
937 fgInitCommands ()
938 {
939   SG_LOG(SG_GENERAL, SG_INFO, "Initializing basic built-in commands:");
940   for (int i = 0; built_ins[i].name != 0; i++) {
941     SG_LOG(SG_GENERAL, SG_INFO, "  " << built_ins[i].name);
942     globals->get_commands()->addCommand(built_ins[i].name,
943                                         built_ins[i].command);
944   }
945
946   typedef bool (*dummy)();
947   fgTie( "/command/view/next", dummy(0), do_view_next );
948   fgTie( "/command/view/prev", dummy(0), do_view_prev );
949 }
950
951 // end of fg_commands.cxx