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