]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.cxx
0311fcd93dd333361398df7dab2ef7db8965465a
[flightgear.git] / src / Main / fg_props.cxx
1 // fg_props.cxx -- support for FlightGear properties.
2 //
3 // Written by David Megginson, started 2000.
4 //
5 // Copyright (C) 2000, 2001 David Megginson - david@megginson.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear/compiler.h>
25 #endif
26
27 #include <simgear/misc/exception.hxx>
28 #include <simgear/magvar/magvar.hxx>
29 #include <simgear/timing/sg_time.hxx>
30 #include <simgear/misc/sg_path.hxx>
31 #include <simgear/scene/material/matlib.hxx>
32 #include <simgear/sound/soundmgr.hxx>
33
34 #include STL_IOSTREAM
35
36 #include <ATC/ATCdisplay.hxx>
37 #include <Aircraft/aircraft.hxx>
38 #include <Time/tmp.hxx>
39 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
40 #ifdef FG_WEATHERCM
41 #  include <WeatherCM/FGLocalWeatherDatabase.h>
42 #else
43 #  include <Environment/environment.hxx>
44 #endif // FG_WEATHERCM
45
46 #include <GUI/gui.h>
47
48 #include "globals.hxx"
49 #include "fgfs.hxx"
50 #include "fg_props.hxx"
51
52 SG_USING_STD(istream);
53 SG_USING_STD(ostream);
54
55 #ifdef FG_WEATHERCM
56 static double getWindNorth ();
57 static double getWindEast ();
58 static double getWindDown ();
59 #endif // FG_WEATHERCM
60
61 static bool winding_ccw = true; // FIXME: temporary
62
63 static bool fdm_data_logging = false; // FIXME: temporary
64
65 static bool frozen = false;     // FIXME: temporary
66
67
68 \f
69 ////////////////////////////////////////////////////////////////////////
70 // Default property bindings (not yet handled by any module).
71 ////////////////////////////////////////////////////////////////////////
72
73 struct LogClassMapping {
74   sgDebugClass c;
75   string name;
76   LogClassMapping(sgDebugClass cc, string nname) { c = cc; name = nname; }
77 };
78
79 LogClassMapping log_class_mappings [] = {
80   LogClassMapping(SG_NONE, "none"),
81   LogClassMapping(SG_TERRAIN, "terrain"),
82   LogClassMapping(SG_ASTRO, "astro"),
83   LogClassMapping(SG_FLIGHT, "flight"),
84   LogClassMapping(SG_INPUT, "input"),
85   LogClassMapping(SG_GL, "gl"),
86   LogClassMapping(SG_VIEW, "view"),
87   LogClassMapping(SG_COCKPIT, "cockpit"),
88   LogClassMapping(SG_GENERAL, "general"),
89   LogClassMapping(SG_MATH, "math"),
90   LogClassMapping(SG_EVENT, "event"),
91   LogClassMapping(SG_AIRCRAFT, "aircraft"),
92   LogClassMapping(SG_AUTOPILOT, "autopilot"),
93   LogClassMapping(SG_IO, "io"),
94   LogClassMapping(SG_CLIPPER, "clipper"),
95   LogClassMapping(SG_NETWORK, "network"),
96   LogClassMapping(SG_UNDEFD, "")
97 };
98
99
100 /**
101  * Get the logging classes.
102  */
103 static const char *
104 getLoggingClasses ()
105 {
106   sgDebugClass classes = logbuf::get_log_classes();
107   static string result = "";    // FIXME
108   for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
109     if ((classes&log_class_mappings[i].c) > 0) {
110       if (!result.empty())
111         result += '|';
112       result += log_class_mappings[i].name;
113     }
114   }
115   return result.c_str();
116 }
117
118
119 static void
120 addLoggingClass (const string &name)
121 {
122   sgDebugClass classes = logbuf::get_log_classes();
123   for (int i = 0; log_class_mappings[i].c != SG_UNDEFD; i++) {
124     if (name == log_class_mappings[i].name) {
125       logbuf::set_log_classes(sgDebugClass(classes|log_class_mappings[i].c));
126       return;
127     }
128   }
129   SG_LOG(SG_GENERAL, SG_ALERT, "Unknown logging class: " << name);
130 }
131
132
133 /**
134  * Set the logging classes.
135  */
136 static void
137 setLoggingClasses (const char * c)
138 {
139   string classes = c;
140   logbuf::set_log_classes(SG_NONE);
141
142   if (classes == "none") {
143     SG_LOG(SG_GENERAL, SG_INFO, "Disabled all logging classes");
144     return;
145   }
146
147   if (classes.empty() || classes == "all") { // default
148     logbuf::set_log_classes(SG_ALL);
149     SG_LOG(SG_GENERAL, SG_INFO, "Enabled all logging classes: "
150            << getLoggingClasses());
151     return;
152   }
153
154   string rest = classes;
155   string name = "";
156   int sep = rest.find('|');
157   while (sep > 0) {
158     name = rest.substr(0, sep);
159     rest = rest.substr(sep+1);
160     addLoggingClass(name);
161     sep = rest.find('|');
162   }
163   addLoggingClass(rest);
164   SG_LOG(SG_GENERAL, SG_INFO, "Set logging classes to "
165          << getLoggingClasses());
166 }
167
168
169 /**
170  * Get the logging priority.
171  */
172 static const char *
173 getLoggingPriority ()
174 {
175   switch (logbuf::get_log_priority()) {
176   case SG_BULK:
177     return "bulk";
178   case SG_DEBUG:
179     return "debug";
180   case SG_INFO:
181     return "info";
182   case SG_WARN:
183     return "warn";
184   case SG_ALERT:
185     return "alert";
186   default:
187     SG_LOG(SG_GENERAL, SG_WARN, "Internal: Unknown logging priority number: "
188            << logbuf::get_log_priority());
189     return "unknown";
190   }
191 }
192
193
194 /**
195  * Set the logging priority.
196  */
197 static void
198 setLoggingPriority (const char * p)
199 {
200   string priority = p;
201   if (priority == "bulk") {
202     logbuf::set_log_priority(SG_BULK);
203   } else if (priority == "debug") {
204     logbuf::set_log_priority(SG_DEBUG);
205   } else if (priority.empty() || priority == "info") { // default
206     logbuf::set_log_priority(SG_INFO);
207   } else if (priority == "warn") {
208     logbuf::set_log_priority(SG_WARN);
209   } else if (priority == "alert") {
210     logbuf::set_log_priority(SG_ALERT);
211   } else {
212     SG_LOG(SG_GENERAL, SG_WARN, "Unknown logging priority " << priority);
213   }
214   SG_LOG(SG_GENERAL, SG_INFO, "Logging priority is " << getLoggingPriority());
215 }
216
217
218 /**
219  * Return the current frozen state.
220  */
221 static bool
222 getFreeze ()
223 {
224   return frozen;
225 }
226
227
228 /**
229  * Set the current frozen state.
230  */
231 static void
232 setFreeze (bool f)
233 {
234     frozen = f;
235
236     // Stop sound on a pause
237     SGSoundMgr *s = globals->get_soundmgr();
238     if ( s != NULL ) {
239         if ( f ) {
240             s->pause();
241         } else {
242             s->resume();
243         }
244     }
245 }
246
247
248 /**
249  * Return the current aircraft directory (UIUC) as a string.
250  */
251 static const char *
252 getAircraftDir ()
253 {
254   return aircraft_dir.c_str();
255 }
256
257
258 /**
259  * Set the current aircraft directory (UIUC).
260  */
261 static void
262 setAircraftDir (const char * dir)
263 {
264   aircraft_dir = dir;
265 }
266
267
268 /**
269  * Return the number of milliseconds elapsed since simulation started.
270  */
271 static double
272 getElapsedTime_sec ()
273 {
274   return globals->get_sim_time_sec();
275 }
276
277
278 /**
279  * Return the current Zulu time.
280  */
281 static const char *
282 getDateString ()
283 {
284   static char buf[64];          // FIXME
285   struct tm * t = globals->get_time_params()->getGmt();
286   sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
287           t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
288           t->tm_hour, t->tm_min, t->tm_sec);
289   return buf;
290 }
291
292
293 /**
294  * Set the current Zulu time.
295  */
296 static void
297 setDateString (const char * date_string)
298 {
299   static const SGPropertyNode *cur_time_override
300         = fgGetNode("/sim/time/cur-time-override", true);
301
302   SGTime * st = globals->get_time_params();
303   struct tm * current_time = st->getGmt();
304   struct tm new_time;
305
306                                 // Scan for basic ISO format
307                                 // YYYY-MM-DDTHH:MM:SS
308   int ret = sscanf(date_string, "%d-%d-%dT%d:%d:%d",
309                    &(new_time.tm_year), &(new_time.tm_mon),
310                    &(new_time.tm_mday), &(new_time.tm_hour),
311                    &(new_time.tm_min), &(new_time.tm_sec));
312
313                                 // Be pretty picky about this, so
314                                 // that strange things don't happen
315                                 // if the save file has been edited
316                                 // by hand.
317   if (ret != 6) {
318     SG_LOG(SG_INPUT, SG_ALERT, "Date/time string " << date_string
319            << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
320     return;
321   }
322
323                                 // OK, it looks like we got six
324                                 // values, one way or another.
325   new_time.tm_year -= 1900;
326   new_time.tm_mon -= 1;
327
328                                 // Now, tell flight gear to use
329                                 // the new time.  This was far
330                                 // too difficult, by the way.
331   long int warp =
332     mktime(&new_time) - mktime(current_time) + globals->get_warp();
333   double lon = current_aircraft.fdm_state->get_Longitude();
334   double lat = current_aircraft.fdm_state->get_Latitude();
335   globals->set_warp(warp);
336   st->update(lon, lat, cur_time_override->getLongValue(), warp);
337 }
338
339 /**
340  * Return the GMT as a string.
341  */
342 static const char *
343 getGMTString ()
344 {
345   static char buf[16];          // FIXME
346   struct tm *t = globals->get_time_params()->getGmt();
347   sprintf(buf, " %.2d:%.2d:%.2d",
348           t->tm_hour, t->tm_min, t->tm_sec);
349   // cout << t << " " << buf << endl;
350   return buf;
351 }
352
353
354 /**
355  * Return the magnetic variation
356  */
357 static double
358 getMagVar ()
359 {
360   return globals->get_mag()->get_magvar() * SGD_RADIANS_TO_DEGREES;
361 }
362
363
364 /**
365  * Return the magnetic dip
366  */
367 static double
368 getMagDip ()
369 {
370   return globals->get_mag()->get_magdip() * SGD_RADIANS_TO_DEGREES;
371 }
372
373
374 /**
375  * Return the current heading in degrees.
376  */
377 static double
378 getHeadingMag ()
379 {
380   double magheading;
381   magheading = current_aircraft.fdm_state->get_Psi() * SGD_RADIANS_TO_DEGREES - getMagVar();
382   if (magheading < 0) magheading += 360;
383   return magheading;
384 }
385
386
387 #ifdef FG_WEATHERCM
388
389 /**
390  * Get the current visibility (meters).
391  */
392 static double
393 getVisibility ()
394 {
395   return WeatherDatabase->getWeatherVisibility();
396 }
397
398
399 /**
400  * Set the current visibility (meters).
401  */
402 static void
403 setVisibility (double visibility)
404 {
405   WeatherDatabase->setWeatherVisibility(visibility);
406 }
407
408 /**
409  * Get the current wind north velocity (feet/second).
410  */
411 static double
412 getWindNorth ()
413 {
414   return current_aircraft.fdm_state->get_V_north_airmass();
415 }
416
417
418 /**
419  * Set the current wind north velocity (feet/second).
420  */
421 static void
422 setWindNorth (double speed)
423 {
424   current_aircraft.fdm_state
425     ->set_Velocities_Local_Airmass(speed, getWindEast(), getWindDown());
426 }
427
428
429 /**
430  * Get the current wind east velocity (feet/second).
431  */
432 static double
433 getWindEast ()
434 {
435   return current_aircraft.fdm_state->get_V_east_airmass();
436 }
437
438
439 /**
440  * Set the current wind east velocity (feet/second).
441  */
442 static void
443 setWindEast (double speed)
444 {
445   SG_LOG(SG_GENERAL, SG_INFO,, "Set wind-east to " << speed );
446   current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
447                                                            speed,
448                                                            getWindDown());
449 }
450
451
452 /**
453  * Get the current wind down velocity (feet/second).
454  */
455 static double
456 getWindDown ()
457 {
458   return current_aircraft.fdm_state->get_V_down_airmass();
459 }
460
461
462 /**
463  * Set the current wind down velocity (feet/second).
464  */
465 static void
466 setWindDown (double speed)
467 {
468   current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
469                                                            getWindEast(),
470                                                            speed);
471 }
472
473 #endif // FG_WEATHERCM
474
475 static long
476 getWarp ()
477 {
478   return globals->get_warp();
479 }
480
481 static void
482 setWarp (long warp)
483 {
484   globals->set_warp(warp);
485 }
486
487 static long
488 getWarpDelta ()
489 {
490   return globals->get_warp_delta();
491 }
492
493 static void
494 setWarpDelta (long delta)
495 {
496   globals->set_warp_delta(delta);
497 }
498
499 static bool
500 getWindingCCW ()
501 {
502   return winding_ccw;
503 }
504
505 static void
506 setWindingCCW (bool state)
507 {
508   winding_ccw = state;
509   if ( winding_ccw )
510     glFrontFace ( GL_CCW );
511   else
512     glFrontFace ( GL_CW );
513 }
514
515 static bool
516 getFullScreen ()
517 {
518 #if defined(FX) && !defined(WIN32)
519   return globals->get_fullscreen();
520 #else
521   return false;
522 #endif
523 }
524
525 static void
526 setFullScreen (bool state)
527 {
528 #if defined(FX) && !defined(WIN32)
529   globals->set_fullscreen(state);
530 #  if defined(XMESA_FX_FULLSCREEN) && defined(XMESA_FX_WINDOW)
531   XMesaSetFXmode( state ? XMESA_FX_FULLSCREEN : XMESA_FX_WINDOW );
532 #  endif
533 #endif
534 }
535
536 static bool
537 getFDMDataLogging ()
538 {
539   return fdm_data_logging;
540 }
541
542 static void
543 setFDMDataLogging (bool state)
544 {
545                                 // kludge; no getter or setter available
546   if (state != fdm_data_logging) {
547     fgToggleFDMdataLogging();
548     fdm_data_logging = state;
549   }
550 }
551
552 \f
553 ////////////////////////////////////////////////////////////////////////
554 // Tie the properties.
555 ////////////////////////////////////////////////////////////////////////
556
557 void
558 fgInitProps ()
559 {
560   SG_LOG(SG_GENERAL, SG_DEBUG, "start of fgInitProps()" );
561                                 // Simulation
562   fgTie("/sim/logging/priority", getLoggingPriority, setLoggingPriority);
563   fgTie("/sim/logging/classes", getLoggingClasses, setLoggingClasses);
564   fgTie("/sim/freeze/master", getFreeze, setFreeze);
565   fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
566
567   fgTie("/sim/time/elapsed-sec", getElapsedTime_sec);
568   fgTie("/sim/time/gmt", getDateString, setDateString);
569   fgSetArchivable("/sim/time/gmt");
570   fgTie("/sim/time/gmt-string", getGMTString);
571
572                                 // Orientation
573   fgTie("/orientation/heading-magnetic-deg", getHeadingMag);
574
575                                 // Environment
576 #ifdef FG_WEATHERCM
577   fgTie("/environment/visibility-m", getVisibility, setVisibility);
578   fgSetArchivable("/environment/visibility-m");
579   fgTie("/environment/wind-from-north-fps", getWindNorth, setWindNorth);
580   fgSetArchivable("/environment/wind-from-north-fps");
581   fgTie("/environment/wind-from-east-fps", getWindEast, setWindEast);
582   fgSetArchivable("/environment/wind-from-east-fps");
583   fgTie("/environment/wind-from-down-fps", getWindDown, setWindDown);
584   fgSetArchivable("/environment/wind-from-down-fps");
585 #endif
586
587   fgTie("/environment/magnetic-variation-deg", getMagVar);
588   fgTie("/environment/magnetic-dip-deg", getMagDip);
589
590   fgTie("/sim/time/warp", getWarp, setWarp, false);
591   fgTie("/sim/time/warp-delta", getWarpDelta, setWarpDelta);
592
593                                 // Misc. Temporary junk.
594   fgTie("/sim/temp/winding-ccw", getWindingCCW, setWindingCCW, false);
595   fgTie("/sim/temp/full-screen", getFullScreen, setFullScreen);
596   fgTie("/sim/temp/fdm-data-logging", getFDMDataLogging, setFDMDataLogging);
597
598   SG_LOG(SG_GENERAL, SG_DEBUG, "end of fgInitProps()" );
599 }
600
601
602 void
603 fgUpdateProps ()
604 {
605 }
606
607
608 \f
609 ////////////////////////////////////////////////////////////////////////
610 // Save and restore.
611 ////////////////////////////////////////////////////////////////////////
612
613
614 /**
615  * Save the current state of the simulator to a stream.
616  */
617 bool
618 fgSaveFlight (ostream &output, bool write_all)
619 {
620
621   fgSetBool("/sim/presets/onground", false);
622   fgSetArchivable("/sim/presets/onground");
623   fgSetBool("/sim/presets/trim", false);
624   fgSetArchivable("/sim/presets/trim");
625   fgSetString("/sim/presets/speed-set", "UVW");
626   fgSetArchivable("/sim/presets/speed-set");
627
628   try {
629     writeProperties(output, globals->get_props(), write_all);
630   } catch (const sg_exception &e) {
631     guiErrorMessage("Error saving flight: ", e);
632     return false;
633   }
634   return true;
635 }
636
637
638 /**
639  * Restore the current state of the simulator from a stream.
640  */
641 bool
642 fgLoadFlight (istream &input)
643 {
644   SGPropertyNode props;
645   try {
646     readProperties(input, &props);
647   } catch (const sg_exception &e) {
648     guiErrorMessage("Error reading saved flight: ", e);
649     return false;
650   }
651
652   fgSetBool("/sim/presets/onground", false);
653   fgSetBool("/sim/presets/trim", false);
654   fgSetString("/sim/presets/speed-set", "UVW");
655
656   copyProperties(&props, globals->get_props());
657   // When loading a flight, make it the
658   // new initial state.
659   globals->saveInitialState();
660   return true;
661 }
662
663
664 bool
665 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
666 {
667     string fullpath;
668     if (in_fg_root) {
669         SGPath loadpath(globals->get_fg_root());
670         loadpath.append(path);
671         fullpath = loadpath.str();
672     } else {
673         fullpath = path;
674     }
675
676     try {
677         readProperties(fullpath, props);
678     } catch (const sg_exception &e) {
679         guiErrorMessage("Error reading properties: ", e);
680         return false;
681     }
682     return true;
683 }
684
685
686 \f
687 ////////////////////////////////////////////////////////////////////////
688 // Property convenience functions.
689 ////////////////////////////////////////////////////////////////////////
690
691 SGPropertyNode *
692 fgGetNode (const char * path, bool create)
693 {
694   return globals->get_props()->getNode(path, create);
695 }
696
697 SGPropertyNode * 
698 fgGetNode (const char * path, int index, bool create)
699 {
700   return globals->get_props()->getNode(path, index, create);
701 }
702
703 bool
704 fgHasNode (const char * path)
705 {
706   return (fgGetNode(path, false) != 0);
707 }
708
709 void
710 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
711 {
712   fgGetNode(path, true)->addChangeListener(listener);
713 }
714
715 void
716 fgAddChangeListener (SGPropertyChangeListener * listener,
717                      const char * path, int index)
718 {
719   fgGetNode(path, index, true)->addChangeListener(listener);
720 }
721
722 bool
723 fgGetBool (const char * name, bool defaultValue)
724 {
725   return globals->get_props()->getBoolValue(name, defaultValue);
726 }
727
728 int
729 fgGetInt (const char * name, int defaultValue)
730 {
731   return globals->get_props()->getIntValue(name, defaultValue);
732 }
733
734 int
735 fgGetLong (const char * name, long defaultValue)
736 {
737   return globals->get_props()->getLongValue(name, defaultValue);
738 }
739
740 float
741 fgGetFloat (const char * name, float defaultValue)
742 {
743   return globals->get_props()->getFloatValue(name, defaultValue);
744 }
745
746 double
747 fgGetDouble (const char * name, double defaultValue)
748 {
749   return globals->get_props()->getDoubleValue(name, defaultValue);
750 }
751
752 const char *
753 fgGetString (const char * name, const char * defaultValue)
754 {
755   return globals->get_props()->getStringValue(name, defaultValue);
756 }
757
758 bool
759 fgSetBool (const char * name, bool val)
760 {
761   return globals->get_props()->setBoolValue(name, val);
762 }
763
764 bool
765 fgSetInt (const char * name, int val)
766 {
767   return globals->get_props()->setIntValue(name, val);
768 }
769
770 bool
771 fgSetLong (const char * name, long val)
772 {
773   return globals->get_props()->setLongValue(name, val);
774 }
775
776 bool
777 fgSetFloat (const char * name, float val)
778 {
779   return globals->get_props()->setFloatValue(name, val);
780 }
781
782 bool
783 fgSetDouble (const char * name, double val)
784 {
785   return globals->get_props()->setDoubleValue(name, val);
786 }
787
788 bool
789 fgSetString (const char * name, const char * val)
790 {
791   return globals->get_props()->setStringValue(name, val);
792 }
793
794 void
795 fgSetArchivable (const char * name, bool state)
796 {
797   SGPropertyNode * node = globals->get_props()->getNode(name);
798   if (node == 0)
799     SG_LOG(SG_GENERAL, SG_ALERT,
800            "Attempt to set archive flag for non-existant property "
801            << name);
802   else
803     node->setAttribute(SGPropertyNode::ARCHIVE, state);
804 }
805
806 void
807 fgSetReadable (const char * name, bool state)
808 {
809   SGPropertyNode * node = globals->get_props()->getNode(name);
810   if (node == 0)
811     SG_LOG(SG_GENERAL, SG_ALERT,
812            "Attempt to set read flag for non-existant property "
813            << name);
814   else
815     node->setAttribute(SGPropertyNode::READ, state);
816 }
817
818 void
819 fgSetWritable (const char * name, bool state)
820 {
821   SGPropertyNode * node = globals->get_props()->getNode(name);
822   if (node == 0)
823     SG_LOG(SG_GENERAL, SG_ALERT,
824            "Attempt to set write flag for non-existant property "
825            << name);
826   else
827     node->setAttribute(SGPropertyNode::WRITE, state);
828 }
829
830 void
831 fgUntie (const char * name)
832 {
833   if (!globals->get_props()->untie(name))
834     SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
835 }
836
837
838 // end of fg_props.cxx