]> git.mxchange.org Git - flightgear.git/blob - src/Main/bootstrap.cxx
Fix a typo breaking some takeoff-state logic.
[flightgear.git] / src / Main / bootstrap.cxx
1 // bootstrap.cxx -- bootstrap routines: main()
2 //
3 // Written by Curtis Olson, started May 1997.
4 //
5 // Copyright (C) 1997 - 2002  Curtis L. Olson  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #include <windows.h>
30 #endif
31
32 #if defined(__linux__)
33 // set link for setting _GNU_SOURCE before including fenv.h
34 // http://man7.org/linux/man-pages/man3/fenv.3.html
35
36   #ifndef _GNU_SOURCE
37     #define _GNU_SOURCE
38   #endif
39
40   #include <fenv.h>
41 #endif
42
43 #ifndef _WIN32
44 #  include <unistd.h> // for gethostname()
45 #endif
46
47 #include <errno.h>
48 #include <signal.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <cstring>
52 #include <iostream>
53
54 #include <simgear/compiler.h>
55 #include <simgear/structure/exception.hxx>
56
57 #include <osg/Texture>
58 #include <osg/BufferObject>
59
60 #include <Viewer/fgviewer.hxx>
61 #include "main.hxx"
62 #include <Include/version.h>
63 #include <Main/globals.hxx>
64 #include <Main/options.hxx>
65 #include <Main/fg_props.hxx>
66 #include <GUI/MessageBox.hxx>
67
68 #include "fg_os.hxx"
69
70 #if defined(HAVE_CRASHRPT)
71         #include <CrashRpt.h>
72
73 bool global_crashRptEnabled = false;
74
75 #endif
76
77 using std::cerr;
78 using std::endl;
79
80 std::string homedir;
81 std::string hostname;
82
83 // forward declaration.
84 void fgExitCleanup();
85
86 static void initFPE(bool enableExceptions);
87
88 #if defined(__linux__)
89
90 static void handleFPE(int);
91 static void
92 initFPE (bool fpeAbort)
93 {
94     if (fpeAbort) {
95         int except = fegetexcept();
96         feenableexcept(except | FE_DIVBYZERO | FE_INVALID);
97     } else {
98         signal(SIGFPE, handleFPE);
99     }
100 }
101
102 static void handleFPE(int)
103 {
104     feclearexcept(FE_ALL_EXCEPT);
105     SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
106     signal(SIGFPE, handleFPE);
107 }
108 #elif defined (SG_WINDOWS)
109
110 static void initFPE(bool fpeAbort)
111 {
112 // Enable floating-point exceptions for Windows
113     if (fpeAbort) {
114         // set following link for what this does (note it does set SSE
115         // flags too, it's not just for the x87 FPU)
116         // http://msdn.microsoft.com/en-us/library/e9b52ceh.aspx
117         _control87( _EM_INEXACT, _MCW_EM );
118     }
119 }
120
121 #else
122 static void initFPE(bool)
123 {
124     // Ignore floating-point exceptions on FreeBSD, OS-X, other Unices
125     signal(SIGFPE, SIG_IGN);
126 }
127 #endif
128
129 #if defined(SG_WINDOWS)
130 int main ( int argc, char **argv );
131 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
132                              LPSTR lpCmdLine, int nCmdShow) {
133
134   main( __argc, __argv );
135 }
136 #endif
137
138 static void fg_terminate()
139 {
140     flightgear::fatalMessageBox("Fatal exception", "Uncaught exception on some thread");
141 }
142
143 int _bootstrap_OSInit;
144
145 // Main entry point; catch any exceptions that have made it this far.
146 int main ( int argc, char **argv )
147 {
148 #if defined(SG_WINDOWS)
149   // Don't show blocking "no disk in drive" error messages on Windows 7,
150   // silently return errors to application instead.
151   // See Microsoft MSDN #ms680621: "GUI apps should specify SEM_NOOPENFILEERRORBOX"
152   SetErrorMode(SEM_NOOPENFILEERRORBOX);
153
154   hostname = ::getenv( "COMPUTERNAME" );
155 #else
156   // Unix(alike) systems
157   char _hostname[256];
158   gethostname(_hostname, 256);
159   hostname = _hostname;
160     
161   signal(SIGPIPE, SIG_IGN);
162 #endif
163
164     _bootstrap_OSInit = 0;
165
166 #if defined(HAVE_CRASHRPT)
167         // Define CrashRpt configuration parameters
168         CR_INSTALL_INFO info;  
169         memset(&info, 0, sizeof(CR_INSTALL_INFO));  
170         info.cb = sizeof(CR_INSTALL_INFO);    
171         info.pszAppName = "FlightGear";
172         info.pszAppVersion = FLIGHTGEAR_VERSION;
173         info.pszEmailSubject = "FlightGear " FLIGHTGEAR_VERSION " crash report";
174         info.pszEmailTo = "fgcrash@goneabitbursar.com";
175         info.pszUrl = "http://fgfs.goneabitbursar.com/crashreporter/crashrpt.php";
176         info.uPriorities[CR_HTTP] = 3; 
177         info.uPriorities[CR_SMTP] = 2;  
178         info.uPriorities[CR_SMAPI] = 1;
179
180         // Install all available exception handlers
181         info.dwFlags |= CR_INST_ALL_POSSIBLE_HANDLERS;
182   
183         // Restart the app on crash 
184         info.dwFlags |= CR_INST_SEND_QUEUED_REPORTS; 
185
186         // automatically install handlers for all threads
187         info.dwFlags |= CR_INST_AUTO_THREAD_HANDLERS;
188
189         // Define the Privacy Policy URL 
190         info.pszPrivacyPolicyURL = "http://flightgear.org/crash-privacypolicy.html"; 
191   
192         // Install crash reporting
193         int nResult = crInstall(&info);    
194         if(nResult!=0) {
195                 char buf[1024];
196                 crGetLastErrorMsg(buf, 1024);
197                 flightgear::modalMessageBox("CrashRpt setup failed", 
198                         "Failed to setup crash-reporting engine, check the installation is not damaged.",
199                         buf);
200         } else {
201                 global_crashRptEnabled = true;
202
203                 crAddProperty("hudson-build-id", HUDSON_BUILD_ID);
204                 char buf[16];
205                 ::snprintf(buf, 16, "%d", HUDSON_BUILD_NUMBER);
206                 crAddProperty("hudson-build-number", buf);
207         crAddProperty("git-revision", REVISION);
208 #if defined(FG_NIGHTLY)
209         crAddProperty("nightly-build", "true");
210 #endif
211         }
212 #endif
213
214     initFPE(flightgear::Options::checkForArg(argc, argv, "enable-fpe"));
215
216     bool fgviewer = flightgear::Options::checkForArg(argc, argv, "fgviewer");
217     try {
218         // http://code.google.com/p/flightgear-bugs/issues/detail?id=1231
219         // ensure sglog is inited before atexit() is registered, so logging
220         // is possible inside fgExitCleanup
221         sglog();
222
223
224 #if (OPENSCENEGRAPH_MAJOR_VERSION == 3) && (OPENSCENEGRAPH_MINOR_VERSION < 5)
225         // similar to above, ensure some static maps inside OSG exist before
226         // we register our at-exit handler, otherwise the statics are gone
227         // when fg_terminate runs, which causes crashes.
228         osg::Texture::getTextureObjectManager(0);
229         osg::GLBufferObjectManager::getGLBufferObjectManager(0);
230 #endif
231         std::set_terminate(fg_terminate);
232         atexit(fgExitCleanup);
233         if (fgviewer)
234             fgviewerMain(argc, argv);
235         else
236             fgMainInit(argc, argv);
237            
238     } catch (const sg_throwable &t) {
239         std::string info;
240         if (std::strlen(t.getOrigin()) != 0)
241             info = std::string("received from ") + t.getOrigin();
242         flightgear::fatalMessageBox("Fatal exception", t.getFormattedMessage(), info);
243
244     } catch (const std::exception &e ) {
245         flightgear::fatalMessageBox("Fatal exception", e.what());
246     } catch (const std::string &s) {
247         flightgear::fatalMessageBox("Fatal exception", s);
248     } catch (const char *s) {
249         std::cerr << "Fatal error (const char*): " << s << std::endl;
250
251     } catch (...) {
252         std::cerr << "Unknown exception in the main loop. Aborting..." << std::endl;
253         if (errno)
254             perror("Possible cause");
255     }
256
257 #if defined(HAVE_CRASHRPT)
258         crUninstall();
259 #endif
260
261     return 0;
262 }
263
264 // do some clean up on exit.  Specifically we want to delete the sound-manager,
265 // so OpenAL device and context are released cleanly
266 void fgExitCleanup() {
267
268     if (_bootstrap_OSInit != 0) {
269         fgSetMouseCursor(MOUSE_CURSOR_POINTER);
270
271         fgOSCloseWindow();
272     }
273     
274     // on the common exit path globals is already deleted, and NULL,
275     // so this only happens on error paths.
276     delete globals;
277 }
278