]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_props.cxx
Use the new clouds3d-enable property instead.
[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
527
528 \f
529 ////////////////////////////////////////////////////////////////////////
530 // Save and restore.
531 ////////////////////////////////////////////////////////////////////////
532
533
534 /**
535  * Save the current state of the simulator to a stream.
536  */
537 bool
538 fgSaveFlight (ostream &output, bool write_all)
539 {
540
541   fgSetBool("/sim/presets/onground", false);
542   fgSetArchivable("/sim/presets/onground");
543   fgSetBool("/sim/presets/trim", false);
544   fgSetArchivable("/sim/presets/trim");
545   fgSetString("/sim/presets/speed-set", "UVW");
546   fgSetArchivable("/sim/presets/speed-set");
547
548   try {
549     writeProperties(output, globals->get_props(), write_all);
550   } catch (const sg_exception &e) {
551     guiErrorMessage("Error saving flight: ", e);
552     return false;
553   }
554   return true;
555 }
556
557
558 /**
559  * Restore the current state of the simulator from a stream.
560  */
561 bool
562 fgLoadFlight (istream &input)
563 {
564   SGPropertyNode props;
565   try {
566     readProperties(input, &props);
567   } catch (const sg_exception &e) {
568     guiErrorMessage("Error reading saved flight: ", e);
569     return false;
570   }
571
572   fgSetBool("/sim/presets/onground", false);
573   fgSetBool("/sim/presets/trim", false);
574   fgSetString("/sim/presets/speed-set", "UVW");
575
576   copyProperties(&props, globals->get_props());
577   // When loading a flight, make it the
578   // new initial state.
579   globals->saveInitialState();
580   return true;
581 }
582
583
584 bool
585 fgLoadProps (const char * path, SGPropertyNode * props, bool in_fg_root)
586 {
587     string fullpath;
588     if (in_fg_root) {
589         SGPath loadpath(globals->get_fg_root());
590         loadpath.append(path);
591         fullpath = loadpath.str();
592     } else {
593         fullpath = path;
594     }
595
596     try {
597         readProperties(fullpath, props);
598     } catch (const sg_exception &e) {
599         guiErrorMessage("Error reading properties: ", e);
600         return false;
601     }
602     return true;
603 }
604
605
606 \f
607 ////////////////////////////////////////////////////////////////////////
608 // Property convenience functions.
609 ////////////////////////////////////////////////////////////////////////
610
611 SGPropertyNode *
612 fgGetNode (const char * path, bool create)
613 {
614   return globals->get_props()->getNode(path, create);
615 }
616
617 SGPropertyNode * 
618 fgGetNode (const char * path, int index, bool create)
619 {
620   return globals->get_props()->getNode(path, index, create);
621 }
622
623 bool
624 fgHasNode (const char * path)
625 {
626   return (fgGetNode(path, false) != 0);
627 }
628
629 void
630 fgAddChangeListener (SGPropertyChangeListener * listener, const char * path)
631 {
632   fgGetNode(path, true)->addChangeListener(listener);
633 }
634
635 void
636 fgAddChangeListener (SGPropertyChangeListener * listener,
637                      const char * path, int index)
638 {
639   fgGetNode(path, index, true)->addChangeListener(listener);
640 }
641
642 bool
643 fgGetBool (const char * name, bool defaultValue)
644 {
645   return globals->get_props()->getBoolValue(name, defaultValue);
646 }
647
648 int
649 fgGetInt (const char * name, int defaultValue)
650 {
651   return globals->get_props()->getIntValue(name, defaultValue);
652 }
653
654 int
655 fgGetLong (const char * name, long defaultValue)
656 {
657   return globals->get_props()->getLongValue(name, defaultValue);
658 }
659
660 float
661 fgGetFloat (const char * name, float defaultValue)
662 {
663   return globals->get_props()->getFloatValue(name, defaultValue);
664 }
665
666 double
667 fgGetDouble (const char * name, double defaultValue)
668 {
669   return globals->get_props()->getDoubleValue(name, defaultValue);
670 }
671
672 const char *
673 fgGetString (const char * name, const char * defaultValue)
674 {
675   return globals->get_props()->getStringValue(name, defaultValue);
676 }
677
678 bool
679 fgSetBool (const char * name, bool val)
680 {
681   return globals->get_props()->setBoolValue(name, val);
682 }
683
684 bool
685 fgSetInt (const char * name, int val)
686 {
687   return globals->get_props()->setIntValue(name, val);
688 }
689
690 bool
691 fgSetLong (const char * name, long val)
692 {
693   return globals->get_props()->setLongValue(name, val);
694 }
695
696 bool
697 fgSetFloat (const char * name, float val)
698 {
699   return globals->get_props()->setFloatValue(name, val);
700 }
701
702 bool
703 fgSetDouble (const char * name, double val)
704 {
705   return globals->get_props()->setDoubleValue(name, val);
706 }
707
708 bool
709 fgSetString (const char * name, const char * val)
710 {
711   return globals->get_props()->setStringValue(name, val);
712 }
713
714 void
715 fgSetArchivable (const char * name, bool state)
716 {
717   SGPropertyNode * node = globals->get_props()->getNode(name);
718   if (node == 0)
719     SG_LOG(SG_GENERAL, SG_DEBUG,
720            "Attempt to set archive flag for non-existant property "
721            << name);
722   else
723     node->setAttribute(SGPropertyNode::ARCHIVE, state);
724 }
725
726 void
727 fgSetReadable (const char * name, bool state)
728 {
729   SGPropertyNode * node = globals->get_props()->getNode(name);
730   if (node == 0)
731     SG_LOG(SG_GENERAL, SG_DEBUG,
732            "Attempt to set read flag for non-existant property "
733            << name);
734   else
735     node->setAttribute(SGPropertyNode::READ, state);
736 }
737
738 void
739 fgSetWritable (const char * name, bool state)
740 {
741   SGPropertyNode * node = globals->get_props()->getNode(name);
742   if (node == 0)
743     SG_LOG(SG_GENERAL, SG_DEBUG,
744            "Attempt to set write flag for non-existant property "
745            << name);
746   else
747     node->setAttribute(SGPropertyNode::WRITE, state);
748 }
749
750 void
751 fgUntie (const char * name)
752 {
753   if (!globals->get_props()->untie(name))
754     SG_LOG(SG_GENERAL, SG_WARN, "Failed to untie property " << name);
755 }
756
757
758 // end of fg_props.cxx