]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_commands.cxx
Merge commit 'refs/merge-requests/1552' of git@gitorious.org:fg/flightgear into next
[flightgear.git] / src / Main / fg_commands.cxx
1 // fg_commands.cxx - internal FGFS commands.
2
3 #ifdef HAVE_CONFIG_H
4 #  include "config.h"
5 #endif
6
7 #include <string.h>             // strcmp()
8
9 #include <simgear/compiler.h>
10
11 #include <string>
12 #include <fstream>
13
14 #include <simgear/sg_inlines.h>
15 #include <simgear/debug/logstream.hxx>
16 #include <simgear/math/sg_random.h>
17 #include <simgear/scene/material/mat.hxx>
18 #include <simgear/scene/material/matlib.hxx>
19 #include <simgear/structure/exception.hxx>
20 #include <simgear/structure/commands.hxx>
21 #include <simgear/props/props.hxx>
22 #include <simgear/structure/event_mgr.hxx>
23 #include <simgear/sound/soundmgr_openal.hxx>
24 #include <simgear/timing/sg_time.hxx>
25
26 #include <Cockpit/panel.hxx>
27 #include <Cockpit/panel_io.hxx>
28 #include <Cockpit/hud.hxx>
29 #include <Environment/environment.hxx>
30 #include <FDM/flight.hxx>
31 #include <GUI/gui.h>
32 #include <GUI/new_gui.hxx>
33 #include <GUI/dialog.hxx>
34 #include <Aircraft/replay.hxx>
35 #include <Scenery/tilemgr.hxx>
36 #include <Scenery/scenery.hxx>
37 #include <Scripting/NasalSys.hxx>
38 #include <Sound/sample_queue.hxx>
39 #include <Time/sunsolver.hxx>
40
41 #include "fg_init.hxx"
42 #include "fg_io.hxx"
43 #include "fg_os.hxx"
44 #include "fg_commands.hxx"
45 #include "fg_props.hxx"
46 #include "globals.hxx"
47 #include "logger.hxx"
48 #include "util.hxx"
49 #include "viewmgr.hxx"
50 #include "main.hxx"
51 #include <Main/viewer.hxx>
52
53 using std::string;
54 using std::ifstream;
55 using std::ofstream;
56
57
58 \f
59 ////////////////////////////////////////////////////////////////////////
60 // Static helper functions.
61 ////////////////////////////////////////////////////////////////////////
62
63
64 static inline SGPropertyNode *
65 get_prop (const SGPropertyNode * arg)
66 {
67     return fgGetNode(arg->getStringValue("property[0]", "/null"), true);
68 }
69
70 static inline SGPropertyNode *
71 get_prop2 (const SGPropertyNode * arg)
72 {
73     return fgGetNode(arg->getStringValue("property[1]", "/null"), true);
74 }
75
76
77 /**
78  * Get a double value and split it as required.
79  */
80 static void
81 split_value (double full_value, const char * mask,
82              double * unmodifiable, double * modifiable)
83 {
84     if (!strcmp("integer", mask)) {
85         *modifiable = (full_value < 0 ? ceil(full_value) : floor (full_value));
86         *unmodifiable = full_value - *modifiable;
87     } else if (!strcmp("decimal", mask)) {
88         *unmodifiable = (full_value < 0 ? ceil(full_value) : floor(full_value));
89         *modifiable = full_value - *unmodifiable;
90     } else {
91         if (strcmp("all", mask))
92             SG_LOG(SG_GENERAL, SG_ALERT, "Bad value " << mask << " for mask;"
93                    << " assuming 'all'");
94         *unmodifiable = 0;
95         *modifiable = full_value;
96     }
97 }
98
99
100 /**
101  * Clamp or wrap a value as specified.
102  */
103 static void
104 limit_value (double * value, const SGPropertyNode * arg)
105 {
106     const SGPropertyNode * min_node = arg->getChild("min");
107     const SGPropertyNode * max_node = arg->getChild("max");
108
109     bool wrap = arg->getBoolValue("wrap");
110
111     if (min_node == 0 || max_node == 0)
112         wrap = false;
113   
114     if (wrap) {                 // wrap such that min <= x < max
115         double min_val = min_node->getDoubleValue();
116         double max_val = max_node->getDoubleValue();
117         double resolution = arg->getDoubleValue("resolution");
118         if (resolution > 0.0) {
119             // snap to (min + N*resolution), taking special care to handle imprecision
120             int n = (int)floor((*value - min_val) / resolution + 0.5);
121             int steps = (int)floor((max_val - min_val) / resolution + 0.5);
122             SG_NORMALIZE_RANGE(n, 0, steps);
123             *value = min_val + resolution * n;
124         } else {
125             // plain circular wrapping
126             SG_NORMALIZE_RANGE(*value, min_val, max_val);
127         }
128     } else {                    // clamp such that min <= x <= max
129         if ((min_node != 0) && (*value < min_node->getDoubleValue()))
130             *value = min_node->getDoubleValue();
131         else if ((max_node != 0) && (*value > max_node->getDoubleValue()))
132             *value = max_node->getDoubleValue();
133     }
134 }
135
136 static bool
137 compare_values (SGPropertyNode * value1, SGPropertyNode * value2)
138 {
139     switch (value1->getType()) {
140     case simgear::props::BOOL:
141         return (value1->getBoolValue() == value2->getBoolValue());
142     case simgear::props::INT:
143         return (value1->getIntValue() == value2->getIntValue());
144     case simgear::props::LONG:
145         return (value1->getLongValue() == value2->getLongValue());
146     case simgear::props::FLOAT:
147         return (value1->getFloatValue() == value2->getFloatValue());
148     case simgear::props::DOUBLE:
149         return (value1->getDoubleValue() == value2->getDoubleValue());
150     default:
151         return !strcmp(value1->getStringValue(), value2->getStringValue());
152     }
153 }
154
155
156 \f
157 ////////////////////////////////////////////////////////////////////////
158 // Command implementations.
159 ////////////////////////////////////////////////////////////////////////
160
161
162 /**
163  * Built-in command: do nothing.
164  */
165 static bool
166 do_null (const SGPropertyNode * arg)
167 {
168   return true;
169 }
170
171 /**
172  * Built-in command: run a Nasal script.
173  */
174 static bool
175 do_nasal (const SGPropertyNode * arg)
176 {
177     return ((FGNasalSys*)globals->get_subsystem("nasal"))->handleCommand(arg);
178 }
179
180 /**
181  * Built-in command: exit FlightGear.
182  *
183  * status: the exit status to return to the operating system (defaults to 0)
184  */
185 static bool
186 do_exit (const SGPropertyNode * arg)
187 {
188     SG_LOG(SG_INPUT, SG_INFO, "Program exit requested.");
189     fgSetBool("/sim/signals/exit", true);
190
191     if (fgGetBool("/sim/startup/save-on-exit")) {
192 #ifdef _WIN32
193         char* envp = ::getenv( "APPDATA" );
194         if ( envp != NULL ) {
195             SGPath config( envp );
196             config.append( "flightgear.org" );
197 #else
198         if ( homedir != NULL ) {
199             SGPath config( homedir );
200             config.append( ".fgfs" );
201 #endif
202             config.append( "autosave.xml" );
203             config.create_dir( 0700 );
204             SG_LOG(SG_IO, SG_INFO, "Saving user settings to " << config.str());
205             try {
206                 writeProperties(config.str(), globals->get_props(), false, SGPropertyNode::USERARCHIVE);
207             } catch (const sg_exception &e) {
208                 guiErrorMessage("Error writing autosave.xml: ", e);
209             }
210
211             SG_LOG(SG_INPUT, SG_DEBUG, "Finished Saving user settings");
212         }
213     }
214     fgExit(arg->getIntValue("status", 0));
215     return true;
216 }
217
218
219 /**
220  * Reset FlightGear (Shift-Escape or Menu->File->Reset)
221  */
222 static bool
223 do_reset (const SGPropertyNode * arg)
224 {
225     doSimulatorReset();
226     return true;
227 }
228
229
230 /**
231  * Built-in command: reinitialize one or more subsystems.
232  *
233  * subsystem[*]: the name(s) of the subsystem(s) to reinitialize; if
234  * none is specified, reinitialize all of them.
235  */
236 static bool
237 do_reinit (const SGPropertyNode * arg)
238 {
239     bool result = true;
240
241     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
242     if (subsystems.size() == 0) {
243         globals->get_subsystem_mgr()->reinit();
244     } else {
245         for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
246             const char * name = subsystems[i]->getStringValue();
247             SGSubsystem * subsystem = globals->get_subsystem(name);
248             if (subsystem == 0) {
249                 result = false;
250                 SG_LOG( SG_GENERAL, SG_ALERT,
251                         "Subsystem " << name << "not found" );
252             } else {
253                 subsystem->reinit();
254             }
255         }
256     }
257
258     globals->get_event_mgr()->reinit();
259
260     return result;
261 }
262
263 #if 0
264   //
265   // these routines look useful ??? but are never used in the code ???
266   //
267
268 /**
269  * Built-in command: suspend one or more subsystems.
270  *
271  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
272  */
273 static bool
274 do_suspend (const SGPropertyNode * arg)
275 {
276     bool result = true;
277
278     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
279     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
280         const char * name = subsystems[i]->getStringValue();
281         SGSubsystem * subsystem = globals->get_subsystem(name);
282         if (subsystem == 0) {
283             result = false;
284             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
285         } else {
286             subsystem->suspend();
287         }
288     }
289     return result;
290 }
291
292 /**
293  * Built-in command: suspend one or more subsystems.
294  *
295  * subsystem[*] - the name(s) of the subsystem(s) to suspend.
296  */
297 static bool
298 do_resume (const SGPropertyNode * arg)
299 {
300     bool result = true;
301
302     vector<SGPropertyNode_ptr> subsystems = arg->getChildren("subsystem");
303     for ( unsigned int i = 0; i < subsystems.size(); i++ ) {
304         const char * name = subsystems[i]->getStringValue();
305         SGSubsystem * subsystem = globals->get_subsystem(name);
306         if (subsystem == 0) {
307             result = false;
308             SG_LOG(SG_GENERAL, SG_ALERT, "Subsystem " << name << "not found");
309         } else {
310             subsystem->resume();
311         }
312     }
313     return result;
314 }
315
316 #endif
317
318
319 /**
320  * Built-in command: load flight.
321  *
322  * file (optional): the name of the file to load (relative to current
323  *   directory).  Defaults to "fgfs.sav"
324  */
325 static bool
326 do_load (const SGPropertyNode * arg)
327 {
328     string file = arg->getStringValue("file", "fgfs.sav");
329     if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
330         file += ".sav";
331
332     if (!fgValidatePath(file.c_str(), false)) {
333         SG_LOG(SG_IO, SG_ALERT, "load: reading '" << file << "' denied "
334                 "(unauthorized access)");
335         return false;
336     }
337
338     ifstream input(file.c_str());
339     if (input.good() && fgLoadFlight(input)) {
340         input.close();
341         SG_LOG(SG_INPUT, SG_INFO, "Restored flight from " << file);
342         return true;
343     } else {
344         SG_LOG(SG_INPUT, SG_WARN, "Cannot load flight from " << file);
345         return false;
346     }
347 }
348
349
350 /**
351  * Built-in command: save flight.
352  *
353  * file (optional): the name of the file to save (relative to the
354  * current directory).  Defaults to "fgfs.sav".
355  */
356 static bool
357 do_save (const SGPropertyNode * arg)
358 {
359     string file = arg->getStringValue("file", "fgfs.sav");
360     if (file.size() < 4 || file.substr(file.size() - 4) != ".sav")
361         file += ".sav";
362
363     if (!fgValidatePath(file.c_str(), false)) {
364         SG_LOG(SG_IO, SG_ALERT, "save: writing '" << file << "' denied "
365                 "(unauthorized access)");
366         return false;
367     }
368
369     bool write_all = arg->getBoolValue("write-all", false);
370     SG_LOG(SG_INPUT, SG_INFO, "Saving flight");
371     ofstream output(file.c_str());
372     if (output.good() && fgSaveFlight(output, write_all)) {
373         output.close();
374         SG_LOG(SG_INPUT, SG_INFO, "Saved flight to " << file);
375         return true;
376     } else {
377         SG_LOG(SG_INPUT, SG_ALERT, "Cannot save flight to " << file);
378         return false;
379     }
380 }
381
382
383 /**
384  * Built-in command: (re)load the panel.
385  *
386  * path (optional): the file name to load the panel from 
387  * (relative to FG_ROOT).  Defaults to the value of /sim/panel/path,
388  * and if that's unspecified, to "Panels/Default/default.xml".
389  */
390 static bool
391 do_panel_load (const SGPropertyNode * arg)
392 {
393   string panel_path =
394     arg->getStringValue("path",
395                         fgGetString("/sim/panel/path",
396                                     "Panels/Default/default.xml"));
397   FGPanel * new_panel = fgReadPanel(panel_path);
398   if (new_panel == 0) {
399     SG_LOG(SG_INPUT, SG_ALERT,
400            "Error reading new panel from " << panel_path);
401     return false;
402   }
403   SG_LOG(SG_INPUT, SG_INFO, "Loaded new panel from " << panel_path);
404   globals->get_current_panel()->unbind();
405   delete globals->get_current_panel();
406   globals->set_current_panel( new_panel );
407   globals->get_current_panel()->bind();
408   return true;
409 }
410
411
412 /**
413  * Built-in command: pass a mouse click to the panel.
414  *
415  * button: the mouse button number, zero-based.
416  * is-down: true if the button is down, false if it is up.
417  * x-pos: the x position of the mouse click.
418  * y-pos: the y position of the mouse click.
419  */
420 static bool
421 do_panel_mouse_click (const SGPropertyNode * arg)
422 {
423   if (globals->get_current_panel() != 0)
424     return globals->get_current_panel()
425       ->doMouseAction(arg->getIntValue("button"),
426                       arg->getBoolValue("is-down") ? PU_DOWN : PU_UP,
427                       arg->getIntValue("x-pos"),
428                       arg->getIntValue("y-pos"));
429   else
430     return false;
431 }
432
433
434 /**
435  * Built-in command: (re)load preferences.
436  *
437  * path (optional): the file name to load the panel from (relative
438  * to FG_ROOT). Defaults to "preferences.xml".
439  */
440 static bool
441 do_preferences_load (const SGPropertyNode * arg)
442 {
443   try {
444     fgLoadProps(arg->getStringValue("path", "preferences.xml"),
445                 globals->get_props());
446   } catch (const sg_exception &e) {
447     guiErrorMessage("Error reading global preferences: ", e);
448     return false;
449   }
450   SG_LOG(SG_INPUT, SG_INFO, "Successfully read global preferences.");
451   return true;
452 }
453
454
455 static void
456 fix_hud_visibility()
457 {
458   if ( !strcmp(fgGetString("/sim/flight-model"), "ada") ) {
459       globals->get_props()->setBoolValue( "/sim/hud/visibility", true );
460       if ( globals->get_viewmgr()->get_current() == 1 ) {
461           globals->get_props()->setBoolValue( "/sim/hud/visibility", false );
462       }
463   }
464 }
465
466 static void
467 do_view_next( bool )
468 {
469     globals->get_current_view()->setHeadingOffset_deg(0.0);
470     globals->get_viewmgr()->next_view();
471     fix_hud_visibility();
472 }
473
474 static void
475 do_view_prev( bool )
476 {
477     globals->get_current_view()->setHeadingOffset_deg(0.0);
478     globals->get_viewmgr()->prev_view();
479     fix_hud_visibility();
480 }
481
482 /**
483  * Built-in command: cycle view.
484  */
485 static bool
486 do_view_cycle (const SGPropertyNode * arg)
487 {
488   globals->get_current_view()->setHeadingOffset_deg(0.0);
489   globals->get_viewmgr()->next_view();
490   fix_hud_visibility();
491   return true;
492 }
493
494 /**
495  * Built-in command: capture screen.
496  */
497 static bool
498 do_screen_capture (const SGPropertyNode * arg)
499 {
500   return fgDumpSnapShot();
501 }
502
503 static bool
504 do_reload_shaders (const SGPropertyNode*)
505 {
506     simgear::reload_shaders();
507     return true;
508 }
509
510 static bool
511 do_dump_scene_graph (const SGPropertyNode*)
512 {
513     fgDumpSceneGraph();
514     return true;
515 }
516
517 static bool
518 do_dump_terrain_branch (const SGPropertyNode*)
519 {
520     fgDumpTerrainBranch();
521
522     double lon_deg = fgGetDouble("/position/longitude-deg");
523     double lat_deg = fgGetDouble("/position/latitude-deg");
524     SGGeod geodPos = SGGeod::fromDegFt(lon_deg, lat_deg, 0.0);
525     SGVec3d zero = SGVec3d::fromGeod(geodPos);
526
527     SG_LOG(SG_INPUT, SG_INFO, "Model parameters:");
528     SG_LOG(SG_INPUT, SG_INFO, "Center: " << zero.x() << ", " << zero.y() << ", " << zero.z() );
529     SG_LOG(SG_INPUT, SG_INFO, "Rotation: " << lat_deg << ", " << lon_deg );
530
531     return true;
532 }
533
534 /**
535  * Built-in command: hires capture screen.
536  */
537 static bool
538 do_hires_screen_capture (const SGPropertyNode * arg)
539 {
540   fgHiResDump();
541   return true;
542 }
543
544
545 /**
546  * Reload the tile cache.
547  */
548 static bool
549 do_tile_cache_reload (const SGPropertyNode * arg)
550 {
551     static const SGPropertyNode *master_freeze
552         = fgGetNode("/sim/freeze/master");
553     bool freeze = master_freeze->getBoolValue();
554     SG_LOG(SG_INPUT, SG_INFO, "ReIniting TileCache");
555     if ( !freeze ) {
556         fgSetBool("/sim/freeze/master", true);
557     }
558     if ( globals->get_tile_mgr()->init() ) {
559         // Load the local scenery data
560         double visibility_meters = fgGetDouble("/environment/visibility-m");
561         globals->get_tile_mgr()->update( visibility_meters );
562     } else {
563         SG_LOG( SG_GENERAL, SG_ALERT, 
564                 "Error in Tile Manager initialization!" );
565         exit(-1);
566     }
567     if ( !freeze ) {
568         fgSetBool("/sim/freeze/master", false);
569     }
570     return true;
571 }
572
573
574 /**
575  * Set the sea level outside air temperature and assigning that to all
576  * boundary and aloft environment layers.
577  */
578 static bool
579 do_set_sea_level_degc ( double temp_sea_level_degc)
580 {
581     SGPropertyNode *node, *child;
582
583     // boundary layers
584     node = fgGetNode( "/environment/config/boundary" );
585     if ( node != NULL ) {
586       int i = 0;
587       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
588         child->setDoubleValue( "temperature-sea-level-degc",
589                                temp_sea_level_degc );
590         ++i;
591       }
592     }
593
594     // aloft layers
595     node = fgGetNode( "/environment/config/aloft" );
596     if ( node != NULL ) {
597       int i = 0;
598       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
599         child->setDoubleValue( "temperature-sea-level-degc",
600                                temp_sea_level_degc );
601         ++i;
602       }
603     }
604
605     return true;
606 }
607
608 static bool
609 do_set_sea_level_degc (const SGPropertyNode * arg)
610 {
611     return do_set_sea_level_degc( arg->getDoubleValue("temp-degc", 15.0) );
612 }
613
614
615 /**
616  * Set the outside air temperature at the "current" altitude by first
617  * calculating the corresponding sea level temp, and assigning that to
618  * all boundary and aloft environment layers.
619  */
620 static bool
621 do_set_oat_degc (const SGPropertyNode * arg)
622 {
623     double oat_degc = arg->getDoubleValue("temp-degc", 15.0);
624     // check for an altitude specified in the arguments, otherwise use
625     // current aircraft altitude.
626     const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
627     if ( altitude_ft == NULL ) {
628         altitude_ft = fgGetNode("/position/altitude-ft");
629     }
630
631     FGEnvironment dummy;        // instantiate a dummy so we can leech a method
632     dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
633     dummy.set_temperature_degc( oat_degc );
634     return do_set_sea_level_degc( dummy.get_temperature_sea_level_degc());
635 }
636
637 /**
638  * Set the sea level outside air dewpoint and assigning that to all
639  * boundary and aloft environment layers.
640  */
641 static bool
642 do_set_dewpoint_sea_level_degc (double dewpoint_sea_level_degc)
643 {
644
645     SGPropertyNode *node, *child;
646
647     // boundary layers
648     node = fgGetNode( "/environment/config/boundary" );
649     if ( node != NULL ) {
650       int i = 0;
651       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
652         child->setDoubleValue( "dewpoint-sea-level-degc",
653                                dewpoint_sea_level_degc );
654         ++i;
655       }
656     }
657
658     // aloft layers
659     node = fgGetNode( "/environment/config/aloft" );
660     if ( node != NULL ) {
661       int i = 0;
662       while ( ( child = node->getNode( "entry", i ) ) != NULL ) {
663         child->setDoubleValue( "dewpoint-sea-level-degc",
664                                dewpoint_sea_level_degc );
665         ++i;
666       }
667     }
668
669     return true;
670 }
671
672 static bool
673 do_set_dewpoint_sea_level_degc (const SGPropertyNode * arg)
674 {
675     return do_set_dewpoint_sea_level_degc(arg->getDoubleValue("dewpoint-degc", 5.0));
676 }
677
678 /**
679  * Set the outside air dewpoint at the "current" altitude by first
680  * calculating the corresponding sea level dewpoint, and assigning
681  * that to all boundary and aloft environment layers.
682  */
683 static bool
684 do_set_dewpoint_degc (const SGPropertyNode * arg)
685 {
686     double dewpoint_degc = arg->getDoubleValue("dewpoint-degc", 5.0);
687
688     // check for an altitude specified in the arguments, otherwise use
689     // current aircraft altitude.
690     const SGPropertyNode *altitude_ft = arg->getChild("altitude-ft");
691     if ( altitude_ft == NULL ) {
692         altitude_ft = fgGetNode("/position/altitude-ft");
693     }
694
695     FGEnvironment dummy;        // instantiate a dummy so we can leech a method
696     dummy.set_elevation_ft( altitude_ft->getDoubleValue() );
697     dummy.set_dewpoint_degc( dewpoint_degc );
698     return do_set_dewpoint_sea_level_degc(dummy.get_dewpoint_sea_level_degc());
699 }
700
701 /**
702  * Update the lighting manually.
703  */
704 static bool
705 do_timeofday (const SGPropertyNode * arg)
706 {
707     const string &offset_type = arg->getStringValue("timeofday", "noon");
708
709     static const SGPropertyNode *longitude
710         = fgGetNode("/position/longitude-deg");
711     static const SGPropertyNode *latitude
712         = fgGetNode("/position/latitude-deg");
713     static const SGPropertyNode *cur_time_override
714         = fgGetNode("/sim/time/cur-time-override", true);
715
716     int orig_warp = globals->get_warp();
717     SGTime *t = globals->get_time_params();
718     time_t cur_time = t->get_cur_time();
719     // cout << "cur_time = " << cur_time << endl;
720     // cout << "orig_warp = " << orig_warp << endl;
721
722     int warp = 0;
723     if ( offset_type == "real" ) {
724         warp = -orig_warp;
725     } else if ( offset_type == "dawn" ) {
726         warp = fgTimeSecondsUntilSunAngle( cur_time,
727                                            longitude->getDoubleValue()
728                                              * SGD_DEGREES_TO_RADIANS,
729                                            latitude->getDoubleValue()
730                                              * SGD_DEGREES_TO_RADIANS,
731                                            90.0, true ); 
732     } else if ( offset_type == "morning" ) {
733         warp = fgTimeSecondsUntilSunAngle( cur_time,
734                                            longitude->getDoubleValue()
735                                              * SGD_DEGREES_TO_RADIANS,
736                                            latitude->getDoubleValue()
737                                              * SGD_DEGREES_TO_RADIANS,
738                                            75.0, true ); 
739     } else if ( offset_type == "noon" ) {
740         warp = fgTimeSecondsUntilSunAngle( cur_time,
741                                            longitude->getDoubleValue()
742                                              * SGD_DEGREES_TO_RADIANS,
743                                            latitude->getDoubleValue()
744                                              * SGD_DEGREES_TO_RADIANS,
745                                            0.0, true ); 
746     } else if ( offset_type == "afternoon" ) {
747         warp = fgTimeSecondsUntilSunAngle( cur_time,
748                                            longitude->getDoubleValue()
749                                              * SGD_DEGREES_TO_RADIANS,
750                                            latitude->getDoubleValue()
751                                              * SGD_DEGREES_TO_RADIANS,
752                                            60.0, false ); 
753      } else if ( offset_type == "dusk" ) {
754         warp = fgTimeSecondsUntilSunAngle( cur_time,
755                                            longitude->getDoubleValue()
756                                              * SGD_DEGREES_TO_RADIANS,
757                                            latitude->getDoubleValue()
758                                              * SGD_DEGREES_TO_RADIANS,
759                                            90.0, false ); 
760      } else if ( offset_type == "evening" ) {
761         warp = fgTimeSecondsUntilSunAngle( cur_time,
762                                            longitude->getDoubleValue()
763                                              * SGD_DEGREES_TO_RADIANS,
764                                            latitude->getDoubleValue()
765                                              * SGD_DEGREES_TO_RADIANS,
766                                            100.0, false ); 
767     } else if ( offset_type == "midnight" ) {
768         warp = fgTimeSecondsUntilSunAngle( cur_time,
769                                            longitude->getDoubleValue()
770                                              * SGD_DEGREES_TO_RADIANS,
771                                            latitude->getDoubleValue()
772                                              * SGD_DEGREES_TO_RADIANS,
773                                            180.0, false ); 
774     }
775     // cout << "warp = " << warp << endl;
776     globals->set_warp( orig_warp + warp );
777
778     t->update( longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
779                latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS,
780                cur_time_override->getLongValue(),
781                globals->get_warp() );
782
783     return true;
784 }
785
786
787 /**
788  * Built-in command: toggle a bool property value.
789  *
790  * property: The name of the property to toggle.
791  */
792 static bool
793 do_property_toggle (const SGPropertyNode * arg)
794 {
795   SGPropertyNode * prop = get_prop(arg);
796   return prop->setBoolValue(!prop->getBoolValue());
797 }
798
799
800 /**
801  * Built-in command: assign a value to a property.
802  *
803  * property: the name of the property to assign.
804  * value: the value to assign; or
805  * property[1]: the property to copy from.
806  */
807 static bool
808 do_property_assign (const SGPropertyNode * arg)
809 {
810   SGPropertyNode * prop = get_prop(arg);
811   const SGPropertyNode * prop2 = get_prop2(arg);
812   const SGPropertyNode * value = arg->getNode("value");
813
814   if (value != 0)
815       return prop->setUnspecifiedValue(value->getStringValue());
816   else if (prop2)
817       return prop->setUnspecifiedValue(prop2->getStringValue());
818   else
819       return false;
820 }
821
822
823 /**
824  * Built-in command: increment or decrement a property value.
825  *
826  * If the 'step' argument is present, it will be used; otherwise,
827  * the command uses 'offset' and 'factor', usually from the mouse.
828  *
829  * property: the name of the property to increment or decrement.
830  * step: the amount of the increment or decrement (default: 0).
831  * offset: offset from the current setting (used for the mouse; multiplied 
832  *         by factor)
833  * factor: scaling amount for the offset (defaults to 1).
834  * min: the minimum allowed value (default: no minimum).
835  * max: the maximum allowed value (default: no maximum).
836  * mask: 'integer' to apply only to the left of the decimal point, 
837  *       'decimal' to apply only to the right of the decimal point,
838  *       or 'all' to apply to the whole number (the default).
839  * wrap: true if the value should be wrapped when it passes min or max;
840  *       both min and max must be present for this to work (default:
841  *       false).
842  */
843 static bool
844 do_property_adjust (const SGPropertyNode * arg)
845 {
846   SGPropertyNode * prop = get_prop(arg);
847
848   double amount = 0;
849   if (arg->hasValue("step"))
850       amount = arg->getDoubleValue("step");
851   else
852       amount = (arg->getDoubleValue("factor", 1)
853                 * arg->getDoubleValue("offset"));
854           
855   double unmodifiable, modifiable;
856   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
857               &unmodifiable, &modifiable);
858   modifiable += amount;
859   limit_value(&modifiable, arg);
860
861   prop->setDoubleValue(unmodifiable + modifiable);
862
863   return true;
864 }
865
866
867 /**
868  * Built-in command: multiply a property value.
869  *
870  * property: the name of the property to multiply.
871  * factor: the amount by which to multiply.
872  * min: the minimum allowed value (default: no minimum).
873  * max: the maximum allowed value (default: no maximum).
874  * mask: 'integer' to apply only to the left of the decimal point, 
875  *       'decimal' to apply only to the right of the decimal point,
876  *       or 'all' to apply to the whole number (the default).
877  * wrap: true if the value should be wrapped when it passes min or max;
878  *       both min and max must be present for this to work (default:
879  *       false).
880  */
881 static bool
882 do_property_multiply (const SGPropertyNode * arg)
883 {
884   SGPropertyNode * prop = get_prop(arg);
885   double factor = arg->getDoubleValue("factor", 1);
886
887   double unmodifiable, modifiable;
888   split_value(prop->getDoubleValue(), arg->getStringValue("mask", "all"),
889               &unmodifiable, &modifiable);
890   modifiable *= factor;
891   limit_value(&modifiable, arg);
892
893   prop->setDoubleValue(unmodifiable + modifiable);
894
895   return true;
896 }
897
898
899 /**
900  * Built-in command: swap two property values.
901  *
902  * property[0]: the name of the first property.
903  * property[1]: the name of the second property.
904  */
905 static bool
906 do_property_swap (const SGPropertyNode * arg)
907 {
908   SGPropertyNode * prop1 = get_prop(arg);
909   SGPropertyNode * prop2 = get_prop2(arg);
910
911                                 // FIXME: inefficient
912   const string & tmp = prop1->getStringValue();
913   return (prop1->setUnspecifiedValue(prop2->getStringValue()) &&
914           prop2->setUnspecifiedValue(tmp.c_str()));
915 }
916
917
918 /**
919  * Built-in command: Set a property to an axis or other moving input.
920  *
921  * property: the name of the property to set.
922  * setting: the current input setting, usually between -1.0 and 1.0.
923  * offset: the offset to shift by, before applying the factor.
924  * factor: the factor to multiply by (use negative to reverse).
925  */
926 static bool
927 do_property_scale (const SGPropertyNode * arg)
928 {
929   SGPropertyNode * prop = get_prop(arg);
930   double setting = arg->getDoubleValue("setting");
931   double offset = arg->getDoubleValue("offset", 0.0);
932   double factor = arg->getDoubleValue("factor", 1.0);
933   bool squared = arg->getBoolValue("squared", false);
934   int power = arg->getIntValue("power", (squared ? 2 : 1));
935
936   int sign = (setting < 0 ? -1 : 1);
937
938   switch (power) {
939   case 1:
940       break;
941   case 2:
942       setting = setting * setting * sign;
943       break;
944   case 3:
945       setting = setting * setting * setting;
946       break;
947   case 4:
948       setting = setting * setting * setting * setting * sign;
949       break;
950   default:
951       setting =  pow(setting, power);
952       if ((power % 2) == 0)
953           setting *= sign;
954       break;
955   }
956
957   return prop->setDoubleValue((setting + offset) * factor);
958 }
959
960
961 /**
962  * Built-in command: cycle a property through a set of values.
963  *
964  * If the current value isn't in the list, the cycle will
965  * (re)start from the beginning.
966  *
967  * property: the name of the property to cycle.
968  * value[*]: the list of values to cycle through.
969  */
970 static bool
971 do_property_cycle (const SGPropertyNode * arg)
972 {
973     SGPropertyNode * prop = get_prop(arg);
974     vector<SGPropertyNode_ptr> values = arg->getChildren("value");
975     int selection = -1;
976     int nSelections = values.size();
977
978     if (nSelections < 1) {
979         SG_LOG(SG_GENERAL, SG_ALERT, "No values for property-cycle");
980         return false;
981     }
982
983                                 // Try to find the current selection
984     for (int i = 0; i < nSelections; i++) {
985         if (compare_values(prop, values[i])) {
986             selection = i + 1;
987             break;
988         }
989     }
990
991                                 // Default or wrap to the first selection
992     if (selection < 0 || selection >= nSelections)
993         selection = 0;
994
995     prop->setUnspecifiedValue(values[selection]->getStringValue());
996     return true;
997 }
998
999
1000 /**
1001  * Built-in command: randomize a numeric property value.
1002  *
1003  * property: the name of the property value to randomize.
1004  * min: the minimum allowed value.
1005  * max: the maximum allowed value.
1006  */
1007 static bool
1008 do_property_randomize (const SGPropertyNode * arg)
1009 {
1010     SGPropertyNode * prop = get_prop(arg);
1011     double min = arg->getDoubleValue("min", DBL_MIN);
1012     double max = arg->getDoubleValue("max", DBL_MAX);
1013     prop->setDoubleValue(sg_random() * (max - min) + min);
1014     return true;
1015 }
1016
1017
1018 /**
1019  * Built-in command: reinit the data logging system based on the
1020  * current contents of the /logger tree.
1021  */
1022 static bool
1023 do_data_logging_commit (const SGPropertyNode * arg)
1024 {
1025     FGLogger *log = (FGLogger *)globals->get_subsystem("logger");
1026     log->reinit();
1027     return true;
1028 }
1029
1030 /**
1031  * Built-in command: Add a dialog to the GUI system.  Does *not*
1032  * display the dialog.  The property node should have the same format
1033  * as a dialog XML configuration.  It must include:
1034  *
1035  * name: the name of the GUI dialog for future reference.
1036  */
1037 static bool
1038 do_dialog_new (const SGPropertyNode * arg)
1039 {
1040     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1041
1042     // Note the casting away of const: this is *real*.  Doing a
1043     // "dialog-apply" command later on will mutate this property node.
1044     // I'm not convinced that this isn't the Right Thing though; it
1045     // allows client to create a node, pass it to dialog-new, and get
1046     // the values back from the dialog by reading the same node.
1047     // Perhaps command arguments are not as "const" as they would
1048     // seem?
1049     gui->newDialog((SGPropertyNode*)arg);
1050     return true;
1051 }
1052
1053 /**
1054  * Built-in command: Show an XML-configured dialog.
1055  *
1056  * dialog-name: the name of the GUI dialog to display.
1057  */
1058 static bool
1059 do_dialog_show (const SGPropertyNode * arg)
1060 {
1061     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1062     gui->showDialog(arg->getStringValue("dialog-name"));
1063     return true;
1064 }
1065
1066
1067 /**
1068  * Built-in Command: Hide the active XML-configured dialog.
1069  */
1070 static bool
1071 do_dialog_close (const SGPropertyNode * arg)
1072 {
1073     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1074     if(arg->hasValue("dialog-name"))
1075         return gui->closeDialog(arg->getStringValue("dialog-name"));
1076     return gui->closeActiveDialog();
1077 }
1078
1079
1080 /**
1081  * Update a value in the active XML-configured dialog.
1082  *
1083  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1084  */
1085 static bool
1086 do_dialog_update (const SGPropertyNode * arg)
1087 {
1088     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1089     FGDialog * dialog;
1090     if (arg->hasValue("dialog-name"))
1091         dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1092     else
1093         dialog = gui->getActiveDialog();
1094
1095     if (dialog != 0) {
1096         dialog->updateValues(arg->getStringValue("object-name"));
1097         return true;
1098     } else {
1099         return false;
1100     }
1101 }
1102
1103
1104 /**
1105  * Apply a value in the active XML-configured dialog.
1106  *
1107  * object-name: The name of the GUI object(s) (all GUI objects if omitted).
1108  */
1109 static bool
1110 do_dialog_apply (const SGPropertyNode * arg)
1111 {
1112     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1113     FGDialog * dialog;
1114     if (arg->hasValue("dialog-name"))
1115         dialog = gui->getDialog(arg->getStringValue("dialog-name"));
1116     else
1117         dialog = gui->getActiveDialog();
1118
1119     if (dialog != 0) {
1120         dialog->applyValues(arg->getStringValue("object-name"));
1121         return true;
1122     } else {
1123         return false;
1124     }
1125 }
1126
1127
1128 /**
1129  * Redraw GUI (applying new widget colors). Doesn't reload the dialogs,
1130  * unlike reinit().
1131  */
1132 static bool
1133 do_gui_redraw (const SGPropertyNode * arg)
1134 {
1135     NewGUI * gui = (NewGUI *)globals->get_subsystem("gui");
1136     gui->redraw();
1137     return true;
1138 }
1139
1140
1141 /**
1142  * Adds model to the scenery. The path to the added branch (/models/model[*])
1143  * is returned in property "property".
1144  */
1145 static bool
1146 do_add_model (const SGPropertyNode * arg)
1147 {
1148     SGPropertyNode * model = fgGetNode("models", true);
1149     for (int i = 0;; i++) {
1150         if (i < 0)
1151             return false;
1152         if (!model->getChild("model", i, false)) {
1153             model = model->getChild("model", i, true);
1154             break;
1155         }
1156     }
1157     copyProperties(arg, model);
1158     if (model->hasValue("elevation-m"))
1159         model->setDoubleValue("elevation-ft", model->getDoubleValue("elevation-m")
1160                 * SG_METER_TO_FEET);
1161     model->getNode("load", true);
1162     model->removeChildren("load");
1163     const_cast<SGPropertyNode *>(arg)->setStringValue("property", model->getPath());
1164     return true;
1165 }
1166
1167
1168 /**
1169  * Set mouse cursor coordinates and cursor shape.
1170  */
1171 static bool
1172 do_set_cursor (const SGPropertyNode * arg)
1173 {
1174     if (arg->hasValue("x") || arg->hasValue("y")) {
1175         SGPropertyNode *mx = fgGetNode("/devices/status/mice/mouse/x", true);
1176         SGPropertyNode *my = fgGetNode("/devices/status/mice/mouse/y", true);
1177         int x = arg->getIntValue("x", mx->getIntValue());
1178         int y = arg->getIntValue("y", my->getIntValue());
1179         fgWarpMouse(x, y);
1180         mx->setIntValue(x);
1181         my->setIntValue(y);
1182     }
1183
1184     SGPropertyNode *cursor = const_cast<SGPropertyNode *>(arg)->getNode("cursor", true);
1185     if (cursor->getType() != simgear::props::NONE)
1186         fgSetMouseCursor(cursor->getIntValue());
1187
1188     cursor->setIntValue(fgGetMouseCursor());
1189     return true;
1190 }
1191
1192
1193 /**
1194  * Built-in command: play an audio message (i.e. a wav file) This is
1195  * fire and forget.  Call this once per message and it will get dumped
1196  * into a queue.  Messages are played sequentially so they do not
1197  * overlap.
1198  */
1199 static bool
1200 do_play_audio_sample (const SGPropertyNode * arg)
1201 {
1202     string path = arg->getStringValue("path");
1203     string file = arg->getStringValue("file");
1204     float volume = arg->getFloatValue("volume");
1205     // cout << "playing " << path << " / " << file << endl;
1206     try {
1207         static FGSampleQueue *queue = 0;
1208         if ( !queue ) {
1209            SGSoundMgr *smgr = globals->get_soundmgr();
1210            queue = new FGSampleQueue(smgr, "chatter");
1211            queue->tie_to_listener();
1212         }
1213
1214         SGSoundSample *msg = new SGSoundSample(file.c_str(), path);
1215         msg->set_volume( volume );
1216         queue->add( msg );
1217
1218         return true;
1219
1220     } catch (const sg_io_exception&) {
1221         SG_LOG(SG_GENERAL, SG_ALERT, "play-audio-sample: "
1222                 "failed to load" << path << '/' << file);
1223         return false;
1224     }
1225 }
1226
1227 /**
1228  * Built-in command: commit presets (read from in /sim/presets/)
1229  */
1230 static bool
1231 do_presets_commit (const SGPropertyNode * arg)
1232 {
1233     // unbind the current fdm state so property changes
1234     // don't get lost when we subsequently delete this fdm
1235     // and create a new one.
1236     globals->get_subsystem("flight")->unbind();
1237
1238     // set position from presets
1239     fgInitPosition();
1240
1241     fgReInitSubsystems();
1242
1243     globals->get_tile_mgr()->update( fgGetDouble("/environment/visibility-m") );
1244
1245 #if 0
1246     if ( ! fgGetBool("/sim/presets/onground") ) {
1247         fgSetBool( "/sim/freeze/master", true );
1248         fgSetBool( "/sim/freeze/clock", true );
1249     }
1250 #endif
1251
1252     return true;
1253 }
1254
1255 /**
1256  * Built-in command: set log level (0 ... 7)
1257  */
1258 static bool
1259 do_log_level (const SGPropertyNode * arg)
1260 {
1261    sglog().setLogLevels( SG_ALL, (sgDebugPriority)arg->getIntValue() );
1262
1263    return true;
1264 }
1265
1266 /**
1267  * Built-in command: replay the FDR buffer
1268  */
1269 static bool
1270 do_replay (const SGPropertyNode * arg)
1271 {
1272     // freeze the master fdm
1273     fgSetInt( "/sim/freeze/replay-state", 1 );
1274
1275     FGReplay *r = (FGReplay *)(globals->get_subsystem( "replay" ));
1276
1277     fgSetDouble( "/sim/replay/start-time", r->get_start_time() );
1278     fgSetDouble( "/sim/replay/end-time", r->get_end_time() );
1279     double duration = fgGetDouble( "/sim/replay/duration" );
1280     if( duration && duration < (r->get_end_time() - r->get_start_time()) ) {
1281         fgSetDouble( "/sim/replay/time", r->get_end_time() - duration );
1282     } else {
1283         fgSetDouble( "/sim/replay/time", r->get_start_time() );
1284     }
1285
1286     // cout << "start = " << r->get_start_time()
1287     //      << "  end = " << r->get_end_time() << endl;
1288
1289     return true;
1290 }
1291
1292
1293 static bool
1294 do_decrease_visibility (const SGPropertyNode * arg)
1295 {
1296     double new_value = fgGetDouble("/environment/visibility-m") * 0.9;
1297     fgSetDouble("/environment/visibility-m", new_value);
1298     fgDefaultWeatherValue("visibility-m", new_value);
1299     globals->get_subsystem("environment")->reinit();
1300
1301     return true;
1302 }
1303  
1304 static bool
1305 do_increase_visibility (const SGPropertyNode * arg)
1306 {
1307     double new_value = fgGetDouble("/environment/visibility-m") * 1.1;
1308     fgSetDouble("/environment/visibility-m", new_value);
1309     fgDefaultWeatherValue("visibility-m", new_value);
1310     globals->get_subsystem("environment")->reinit();
1311
1312     return true;
1313 }
1314
1315 static bool
1316 do_hud_init(const SGPropertyNode *)
1317 {
1318     fgHUDInit(); // minimal HUD
1319     return true;
1320 }
1321
1322 static bool
1323 do_hud_init2(const SGPropertyNode *)
1324 {
1325     fgHUDInit2();  // normal HUD
1326     return true;
1327 }
1328
1329
1330 /**
1331  * An fgcommand to allow loading of xml files via nasal,
1332  * the xml file's structure will be made available within
1333  * a property tree node defined under argument "targetnode",
1334  * or in the given argument tree under "data" otherwise.
1335  *
1336  * @param filename a string to hold the complete path & filename of an XML file
1337  * @param targetnode a string pointing to a location within the property tree
1338  * where to store the parsed XML file. If <targetnode> is undefined, then the
1339  * file contents are stored under a node <data> in the argument tree.
1340  */
1341
1342 static bool
1343 do_load_xml_to_proptree(const SGPropertyNode * arg)
1344 {
1345     SGPath file(arg->getStringValue("filename"));
1346     if (file.str().empty())
1347         return false;
1348
1349     if (file.extension() != "xml")
1350         file.concat(".xml");
1351
1352     if (file.isRelative()) {
1353       file = globals->resolve_maybe_aircraft_path(file.str());
1354     }
1355
1356     if (!fgValidatePath(file.c_str(), false)) {
1357         SG_LOG(SG_IO, SG_ALERT, "loadxml: reading '" << file.str() << "' denied "
1358                 "(unauthorized access)");
1359         return false;
1360     }
1361
1362     SGPropertyNode *targetnode;
1363     if (arg->hasValue("targetnode"))
1364         targetnode = fgGetNode(arg->getStringValue("targetnode"), true);
1365     else
1366         targetnode = const_cast<SGPropertyNode *>(arg)->getNode("data", true);
1367
1368     try {
1369         readProperties(file.c_str(), targetnode, true);
1370     } catch (const sg_exception &e) {
1371         SG_LOG(SG_IO, SG_WARN, "loadxml: " << e.getFormattedMessage());
1372         return false;
1373     }
1374
1375     return true;
1376 }
1377
1378
1379 /**
1380  * An fgcommand to allow saving of xml files via nasal,
1381  * the file's structure will be determined based on what's
1382  * encountered in the passed (source) property tree node
1383  *
1384  * @param filename a string to hold the complete path & filename of the (new)
1385  * XML file
1386  * @param sourcenode a string pointing to a location within the property tree
1387  * where to find the nodes that should be written recursively into an XML file
1388  * @param data if no sourcenode is given, then the file contents are taken from
1389  * the argument tree's "data" node.
1390  */
1391
1392 static bool
1393 do_save_xml_from_proptree(const SGPropertyNode * arg)
1394 {
1395     SGPath file(arg->getStringValue("filename"));
1396     if (file.str().empty())
1397         return false;
1398
1399     if (file.extension() != "xml")
1400         file.concat(".xml");
1401
1402     if (!fgValidatePath(file.c_str(), true)) {
1403         SG_LOG(SG_IO, SG_ALERT, "savexml: writing to '" << file.str() << "' denied "
1404                 "(unauthorized access)");
1405         return false;
1406     }
1407
1408     SGPropertyNode *sourcenode;
1409     if (arg->hasValue("sourcenode"))
1410         sourcenode = fgGetNode(arg->getStringValue("sourcenode"), true);
1411     else if (arg->getNode("data", false))
1412         sourcenode = const_cast<SGPropertyNode *>(arg)->getNode("data");
1413     else
1414         return false;
1415
1416     try {
1417         writeProperties (file.c_str(), sourcenode, true);
1418     } catch (const sg_exception &e) {
1419         SG_LOG(SG_IO, SG_WARN, "savexml: " << e.getFormattedMessage());
1420         return false;
1421     }
1422
1423     return true;
1424 }
1425
1426 static bool
1427 do_press_cockpit_button (const SGPropertyNode *arg)
1428 {
1429   const char *prefix = arg->getStringValue("prefix");
1430
1431   if (arg->getBoolValue("guarded") && fgGetDouble((string(prefix) + "-guard").c_str()) < 1)
1432     return true;
1433
1434   string prop = string(prefix) + "-button";
1435   double value;
1436
1437   if (arg->getBoolValue("latching"))
1438     value = fgGetDouble(prop.c_str()) > 0 ? 0 : 1;
1439   else
1440     value = 1;
1441
1442   fgSetDouble(prop.c_str(), value);
1443   fgSetBool(arg->getStringValue("discrete"), value > 0);
1444
1445   return true;
1446 }
1447
1448 static bool
1449 do_release_cockpit_button (const SGPropertyNode *arg)
1450 {
1451   const char *prefix = arg->getStringValue("prefix");
1452
1453   if (arg->getBoolValue("guarded")) {
1454     string prop = string(prefix) + "-guard";
1455     if (fgGetDouble(prop.c_str()) < 1) {
1456       fgSetDouble(prop.c_str(), 1);
1457       return true;
1458     }
1459   }
1460
1461   if (! arg->getBoolValue("latching")) {
1462     fgSetDouble((string(prefix) + "-button").c_str(), 0);
1463     fgSetBool(arg->getStringValue("discrete"), false);
1464   }
1465
1466   return true;
1467 }
1468
1469
1470 ////////////////////////////////////////////////////////////////////////
1471 // Command setup.
1472 ////////////////////////////////////////////////////////////////////////
1473
1474
1475 /**
1476  * Table of built-in commands.
1477  *
1478  * New commands do not have to be added here; any module in the application
1479  * can add a new command using globals->get_commands()->addCommand(...).
1480  */
1481 static struct {
1482   const char * name;
1483   SGCommandMgr::command_t command;
1484 } built_ins [] = {
1485     { "null", do_null },
1486     { "nasal", do_nasal },
1487     { "exit", do_exit },
1488     { "reset", do_reset },
1489     { "reinit", do_reinit },
1490     { "suspend", do_reinit },
1491     { "resume", do_reinit },
1492     { "load", do_load },
1493     { "save", do_save },
1494     { "panel-load", do_panel_load },
1495     { "panel-mouse-click", do_panel_mouse_click },
1496     { "preferences-load", do_preferences_load },
1497     { "view-cycle", do_view_cycle },
1498     { "screen-capture", do_screen_capture },
1499     { "hires-screen-capture", do_hires_screen_capture },
1500     { "tile-cache-reload", do_tile_cache_reload },
1501     { "set-sea-level-air-temp-degc", do_set_sea_level_degc },
1502     { "set-outside-air-temp-degc", do_set_oat_degc },
1503     { "set-dewpoint-sea-level-air-temp-degc", do_set_dewpoint_sea_level_degc },
1504     { "set-dewpoint-temp-degc", do_set_dewpoint_degc },
1505     { "timeofday", do_timeofday },
1506     { "property-toggle", do_property_toggle },
1507     { "property-assign", do_property_assign },
1508     { "property-adjust", do_property_adjust },
1509     { "property-multiply", do_property_multiply },
1510     { "property-swap", do_property_swap },
1511     { "property-scale", do_property_scale },
1512     { "property-cycle", do_property_cycle },
1513     { "property-randomize", do_property_randomize },
1514     { "data-logging-commit", do_data_logging_commit },
1515     { "dialog-new", do_dialog_new },
1516     { "dialog-show", do_dialog_show },
1517     { "dialog-close", do_dialog_close },
1518     { "dialog-update", do_dialog_update },
1519     { "dialog-apply", do_dialog_apply },
1520     { "gui-redraw", do_gui_redraw },
1521     { "add-model", do_add_model },
1522     { "set-cursor", do_set_cursor },
1523     { "play-audio-sample", do_play_audio_sample },
1524     { "presets-commit", do_presets_commit },
1525     { "log-level", do_log_level },
1526     { "replay", do_replay },
1527     { "decrease-visibility", do_decrease_visibility },
1528     { "increase-visibility", do_increase_visibility },
1529     { "hud-init", do_hud_init },
1530     { "hud-init2", do_hud_init2 },
1531     { "loadxml", do_load_xml_to_proptree},
1532     { "savexml", do_save_xml_from_proptree },
1533     { "press-cockpit-button", do_press_cockpit_button },
1534     { "release-cockpit-button", do_release_cockpit_button },
1535     { "dump-scenegraph", do_dump_scene_graph },
1536     { "dump-terrainbranch", do_dump_terrain_branch },
1537     { "reload-shaders", do_reload_shaders },
1538     { 0, 0 }                    // zero-terminated
1539 };
1540
1541
1542 /**
1543  * Initialize the default built-in commands.
1544  *
1545  * Other commands may be added by other parts of the application.
1546  */
1547 void
1548 fgInitCommands ()
1549 {
1550   SG_LOG(SG_GENERAL, SG_BULK, "Initializing basic built-in commands:");
1551   for (int i = 0; built_ins[i].name != 0; i++) {
1552     SG_LOG(SG_GENERAL, SG_BULK, "  " << built_ins[i].name);
1553     globals->get_commands()->addCommand(built_ins[i].name,
1554                                         built_ins[i].command);
1555   }
1556
1557   typedef bool (*dummy)();
1558   fgTie( "/command/view/next", dummy(0), do_view_next );
1559   fgTie( "/command/view/prev", dummy(0), do_view_prev );
1560 }
1561
1562 // end of fg_commands.cxx