]> git.mxchange.org Git - flightgear.git/blob - src/Main/bootstrap.cxx
Canvas: update for new bounding box getters.
[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(SG_MAC)
71     #include <GUI/CocoaHelpers.h> // for transformToForegroundApp
72 #endif
73
74 #if defined(HAVE_CRASHRPT)
75         #include <CrashRpt.h>
76
77 bool global_crashRptEnabled = false;
78
79 #endif
80
81 using std::cerr;
82 using std::endl;
83
84 std::string homedir;
85 std::string hostname;
86
87 // forward declaration.
88 void fgExitCleanup();
89
90 static void initFPE(bool enableExceptions);
91
92 #if defined(__linux__)
93
94 static void handleFPE(int);
95 static void
96 initFPE (bool fpeAbort)
97 {
98     if (fpeAbort) {
99         int except = fegetexcept();
100         feenableexcept(except | FE_DIVBYZERO | FE_INVALID);
101     } else {
102         signal(SIGFPE, handleFPE);
103     }
104 }
105
106 static void handleFPE(int)
107 {
108     feclearexcept(FE_ALL_EXCEPT);
109     SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
110     signal(SIGFPE, handleFPE);
111 }
112 #elif defined (SG_WINDOWS)
113
114 static void initFPE(bool fpeAbort)
115 {
116 // Enable floating-point exceptions for Windows
117     if (fpeAbort) {
118         // set following link for what this does (note it does set SSE
119         // flags too, it's not just for the x87 FPU)
120         // http://msdn.microsoft.com/en-us/library/e9b52ceh.aspx
121         _control87( _EM_INEXACT, _MCW_EM );
122     }
123 }
124
125 #else
126 static void initFPE(bool)
127 {
128     // Ignore floating-point exceptions on FreeBSD, OS-X, other Unices
129     signal(SIGFPE, SIG_IGN);
130 }
131 #endif
132
133 #if defined(SG_WINDOWS)
134 int main ( int argc, char **argv );
135 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
136                              LPSTR lpCmdLine, int nCmdShow) {
137
138   main( __argc, __argv );
139 }
140 #endif
141
142 static void fg_terminate()
143 {
144     flightgear::fatalMessageBox("Fatal exception", "Uncaught exception on some thread");
145 }
146
147 int _bootstrap_OSInit;
148
149 // Main entry point; catch any exceptions that have made it this far.
150 int main ( int argc, char **argv )
151 {
152 #if defined(SG_WINDOWS)
153   // Don't show blocking "no disk in drive" error messages on Windows 7,
154   // silently return errors to application instead.
155   // See Microsoft MSDN #ms680621: "GUI apps should specify SEM_NOOPENFILEERRORBOX"
156   SetErrorMode(SEM_NOOPENFILEERRORBOX);
157
158   hostname = ::getenv( "COMPUTERNAME" );
159 #else
160   // Unix(alike) systems
161   char _hostname[256];
162   gethostname(_hostname, 256);
163   hostname = _hostname;
164     
165   signal(SIGPIPE, SIG_IGN);
166 #endif
167
168     _bootstrap_OSInit = 0;
169
170 #if defined(HAVE_CRASHRPT)
171         // Define CrashRpt configuration parameters
172         CR_INSTALL_INFO info;  
173         memset(&info, 0, sizeof(CR_INSTALL_INFO));  
174         info.cb = sizeof(CR_INSTALL_INFO);    
175         info.pszAppName = "FlightGear";
176         info.pszAppVersion = FLIGHTGEAR_VERSION;
177         info.pszEmailSubject = "FlightGear " FLIGHTGEAR_VERSION " crash report";
178         info.pszEmailTo = "fgcrash@goneabitbursar.com";
179         info.pszUrl = "http://fgfs.goneabitbursar.com/crashreporter/crashrpt.php";
180         info.uPriorities[CR_HTTP] = 3; 
181         info.uPriorities[CR_SMTP] = 2;  
182         info.uPriorities[CR_SMAPI] = 1;
183
184         // Install all available exception handlers
185         info.dwFlags |= CR_INST_ALL_POSSIBLE_HANDLERS;
186   
187         // Restart the app on crash 
188         info.dwFlags |= CR_INST_SEND_QUEUED_REPORTS; 
189
190         // automatically install handlers for all threads
191         info.dwFlags |= CR_INST_AUTO_THREAD_HANDLERS;
192
193         // Define the Privacy Policy URL 
194         info.pszPrivacyPolicyURL = "http://flightgear.org/crash-privacypolicy.html"; 
195   
196         // Install crash reporting
197         int nResult = crInstall(&info);    
198         if(nResult!=0) {
199                 char buf[1024];
200                 crGetLastErrorMsg(buf, 1024);
201                 flightgear::modalMessageBox("CrashRpt setup failed", 
202                         "Failed to setup crash-reporting engine, check the installation is not damaged.",
203                         buf);
204         } else {
205                 global_crashRptEnabled = true;
206
207                 crAddProperty("hudson-build-id", HUDSON_BUILD_ID);
208                 char buf[16];
209                 ::snprintf(buf, 16, "%d", HUDSON_BUILD_NUMBER);
210                 crAddProperty("hudson-build-number", buf);
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         // similar to above, ensure some static maps inside OSG exist before
224         // we register our at-exit handler, otherwise the statics are gone
225         // when fg_terminate runs, which causes crashes.
226         osg::Texture::getTextureObjectManager(0);
227         osg::GLBufferObjectManager::getGLBufferObjectManager(0);
228         
229         std::set_terminate(fg_terminate);
230         atexit(fgExitCleanup);
231         if (fgviewer)
232             fgviewerMain(argc, argv);
233         else
234             fgMainInit(argc, argv);
235            
236     } catch (const sg_throwable &t) {
237         std::string info;
238         if (std::strlen(t.getOrigin()) != 0)
239             info = std::string("received from ") + t.getOrigin();
240         flightgear::fatalMessageBox("Fatal exception", t.getFormattedMessage(), info);
241
242     } catch (const std::exception &e ) {
243         flightgear::fatalMessageBox("Fatal exception", e.what());
244     } catch (const std::string &s) {
245         flightgear::fatalMessageBox("Fatal exception", s);
246     } catch (const char *s) {
247         std::cerr << "Fatal error (const char*): " << s << std::endl;
248
249     } catch (...) {
250         std::cerr << "Unknown exception in the main loop. Aborting..." << std::endl;
251         if (errno)
252             perror("Possible cause");
253     }
254
255 #if defined(HAVE_CRASHRPT)
256         crUninstall();
257 #endif
258
259     return 0;
260 }
261
262 // do some clean up on exit.  Specifically we want to delete the sound-manager,
263 // so OpenAL device and context are released cleanly
264 void fgExitCleanup() {
265
266     if (_bootstrap_OSInit != 0) {
267         fgSetMouseCursor(MOUSE_CURSOR_POINTER);
268
269         fgOSCloseWindow();
270     }
271     
272     // on the common exit path globals is already deleted, and NULL,
273     // so this only happens on error paths.
274     delete globals;
275 }
276