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