]> git.mxchange.org Git - flightgear.git/blob - src/Main/bootstrap.cxx
Crash-fix: mat-lib is now reference-counted.
[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(HAVE_FEENABLEEXCEPT)
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
36 #include <fenv.h>
37 #elif defined(__linux__) && defined(__i386__)
38 #  include <fpu_control.h>
39 #endif
40
41 #ifndef _WIN32
42 #  include <unistd.h> // for gethostname()
43 #endif
44
45 #include <errno.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49
50 #include <simgear/compiler.h>
51 #include <simgear/structure/exception.hxx>
52
53 #include <osg/Texture>
54 #include <osg/BufferObject>
55
56 #include <cstring>
57 #include <iostream>
58 using std::cerr;
59 using std::endl;
60
61 #include <Viewer/fgviewer.hxx>
62 #include "main.hxx"
63 #include <Include/version.h>
64 #include <Main/globals.hxx>
65 #include <Main/fg_props.hxx>
66 #include <GUI/MessageBox.hxx>
67
68 #include "fg_os.hxx"
69
70 #if defined(SG_MAC)
71     #include <GUI/CocoaHelpers.h> // for transformToForegroundApp
72 #endif
73
74 #if defined(HAVE_CRASHRPT)
75         #include <CrashRpt.h>
76 #endif
77
78 std::string homedir;
79 std::string hostname;
80
81 // forward declaration.
82 void fgExitCleanup();
83
84 static bool fpeAbort = false;
85 static void initFPE();
86
87 #if defined(HAVE_FEENABLEEXCEPT)
88 static void handleFPE(int);
89 static void
90 initFPE ()
91 {
92     if (fpeAbort) {
93         int except = fegetexcept();
94         feenableexcept(except | FE_DIVBYZERO | FE_INVALID);
95     } else {
96         signal(SIGFPE, handleFPE);
97     }
98 }
99
100 static void handleFPE(int)
101 {
102     feclearexcept(FE_ALL_EXCEPT);
103     signal(SIGFPE, handleFPE);
104 }
105 #elif defined(__linux__) && defined(__i386__)
106
107 static void handleFPE(int);
108 static void
109 initFPE ()
110 {
111     fpu_control_t fpe_flags = 0;
112     _FPU_GETCW(fpe_flags);
113 //     fpe_flags &= ~_FPU_MASK_IM;      // invalid operation
114 //     fpe_flags &= ~_FPU_MASK_DM;      // denormalized operand
115 //     fpe_flags &= ~_FPU_MASK_ZM;      // zero-divide
116 //     fpe_flags &= ~_FPU_MASK_OM;      // overflow
117 //     fpe_flags &= ~_FPU_MASK_UM;      // underflow
118 //     fpe_flags &= ~_FPU_MASK_PM;      // precision (inexact result)
119     _FPU_SETCW(fpe_flags);
120     signal(SIGFPE, handleFPE);
121 }
122
123 static void
124 handleFPE (int num)
125 {
126   initFPE();
127   SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
128 }
129 #else
130 static void initFPE()
131 {
132 }
133 #endif
134
135 #if defined(SG_WINDOWS)
136 int main ( int argc, char **argv );
137 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
138                              LPSTR lpCmdLine, int nCmdShow) {
139
140   main( __argc, __argv );
141 }
142 #endif
143
144 static void fg_terminate() {
145     cerr << endl <<
146             "Uncaught Exception: missing exception handler on some thread"
147             << endl << endl;
148     abort();
149 }
150
151 int _bootstrap_OSInit;
152
153 // Main entry point; catch any exceptions that have made it this far.
154 int main ( int argc, char **argv )
155 {
156 #if defined(SG_WINDOWS)
157   // Don't show blocking "no disk in drive" error messages on Windows 7,
158   // silently return errors to application instead.
159   // See Microsoft MSDN #ms680621: "GUI apps should specify SEM_NOOPENFILEERRORBOX"
160   SetErrorMode(SEM_NOOPENFILEERRORBOX);
161
162   hostname = ::getenv( "COMPUTERNAME" );
163 #else
164   // Unix(alike) systems
165   char _hostname[256];
166   gethostname(_hostname, 256);
167   hostname = _hostname;
168     
169   signal(SIGPIPE, SIG_IGN);
170 #endif
171
172 #if defined(SG_MAC)
173     // required so native messages boxes work prior to osgViewer init
174     // (only needed when not running as a bundled app)
175     transformToForegroundApp();
176 #endif
177     
178 #ifdef PTW32_STATIC_LIB
179     // Initialise static pthread win32 lib
180     pthread_win32_process_attach_np ();
181 #endif
182     _bootstrap_OSInit = 0;
183
184 #if defined(HAVE_CRASHRPT)
185         // Define CrashRpt configuration parameters
186         CR_INSTALL_INFO info;  
187         memset(&info, 0, sizeof(CR_INSTALL_INFO));  
188         info.cb = sizeof(CR_INSTALL_INFO);    
189         info.pszAppName = "FlightGear";
190         info.pszAppVersion = FLIGHTGEAR_VERSION;
191         info.pszEmailSubject = "FlightGear " FLIGHTGEAR_VERSION " crash report";
192         info.pszEmailTo = "fgcrash@goneabitbursar.com";
193         info.pszUrl = "http://fgfs.goneabitbursar.com/crashreporter/crashrpt.php";
194         info.uPriorities[CR_HTTP] = 3; 
195         info.uPriorities[CR_SMTP] = 2;  
196         info.uPriorities[CR_SMAPI] = 1;
197
198         // Install all available exception handlers
199         info.dwFlags |= CR_INST_ALL_POSSIBLE_HANDLERS;
200   
201         // Restart the app on crash 
202         info.dwFlags |= CR_INST_SEND_QUEUED_REPORTS; 
203
204         // autoamticallty install handlers for all threads
205         info.dwFlags |= CR_INST_AUTO_THREAD_HANDLERS;
206
207         // Define the Privacy Policy URL 
208         info.pszPrivacyPolicyURL = "http://flightgear.org/crash-privacypolicy.html"; 
209   
210         // Install crash reporting
211         int nResult = crInstall(&info);    
212         if(nResult!=0) {
213                 std::cerr << "failed to install crash reporting engine" << std::endl;
214         } else {
215                 crAddProperty("hudson-build-id", HUDSON_BUILD_ID); 
216         }
217 #endif
218
219 #if defined(__FreeBSD__)
220     // Ignore floating-point exceptions on FreeBSD
221     signal(SIGFPE, SIG_IGN); 
222 #else
223     // Maybe Enable floating-point exceptions on Linux
224     for (int i = 0; i < argc; ++i) {
225         if (!strcmp("--enable-fpe", argv[i])) {
226             fpeAbort = true;
227             break;
228         }
229     }
230     initFPE();
231 #endif
232
233     // Enable floating-point exceptions for Windows
234 #if defined( _MSC_VER ) && defined( DEBUG )
235     // Christian, we should document what this does
236     _control87( _EM_INEXACT, _MCW_EM );
237 #endif
238
239     bool fgviewer = false;
240     for (int i = 0; i < argc; ++i) {
241         if (!strcmp("--fgviewer", argv[i])) {
242             fgviewer = true;
243             break;
244         }
245     }
246
247     // FIXME: add other, more specific
248     // exceptions.
249     try {
250         // http://code.google.com/p/flightgear-bugs/issues/detail?id=1231
251         // ensure sglog is inited before atexit() is registered, so logging
252         // is possible inside fgExitCleanup
253         sglog();
254         
255         // similar to above, ensure some static maps inside OSG exist before
256         // we register our at-exit handler, otherwise the statics are gone
257         // when fg_terminate runs, which causes crashes.
258         osg::Texture::getTextureObjectManager(0);
259         osg::GLBufferObjectManager::getGLBufferObjectManager(0);
260         
261         std::set_terminate(fg_terminate);
262         atexit(fgExitCleanup);
263         if (fgviewer)
264             fgviewerMain(argc, argv);
265         else
266             fgMainInit(argc, argv);
267            
268     } catch (const sg_throwable &t) {
269         std::string info;
270         if (std::strlen(t.getOrigin()) != 0)
271             info = std::string("received from ") + t.getOrigin();
272         flightgear::fatalMessageBox("Fatal exception", t.getFormattedMessage(), info);
273
274     } catch (const std::exception &e ) {
275         flightgear::fatalMessageBox("Fatal exception", e.what());
276     } catch (const std::string &s) {
277         flightgear::fatalMessageBox("Fatal exception", s);
278     } catch (const char *s) {
279         cerr << "Fatal error (const char*): " << s << endl;
280
281     } catch (...) {
282         cerr << "Unknown exception in the main loop. Aborting..." << endl;
283         if (errno)
284             perror("Possible cause");
285     }
286
287 #if defined(HAVE_CRASHRPT)
288         crUninstall();
289 #endif
290
291     return 0;
292 }
293
294 // do some clean up on exit.  Specifically we want to delete the sound-manager,
295 // so OpenAL device and context are released cleanly
296 void fgExitCleanup() {
297
298     if (_bootstrap_OSInit != 0) {
299         fgSetMouseCursor(MOUSE_CURSOR_POINTER);
300
301         fgOSCloseWindow();
302     }
303     
304     // on the common exit path globals is already deleted, and NULL,
305     // so this only happens on error paths.
306     delete globals;
307 }
308