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