1 // bootstrap.cxx -- bootstrap routines: main()
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 - 2002 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
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
44 # include <unistd.h> // for gethostname()
55 #include <simgear/compiler.h>
56 #include <simgear/structure/exception.hxx>
58 #include <osg/Texture>
59 #include <osg/BufferObject>
61 #include <Viewer/fgviewer.hxx>
63 #include <Include/version.h>
64 #include <Main/globals.hxx>
65 #include <Main/options.hxx>
66 #include <Main/fg_props.hxx>
67 #include <GUI/MessageBox.hxx>
71 #if defined(HAVE_CRASHRPT)
74 bool global_crashRptEnabled = false;
84 // forward declaration.
87 static void initFPE(bool enableExceptions);
89 #if defined(__linux__)
91 static void handleFPE(int);
93 initFPE (bool fpeAbort)
96 int except = fegetexcept();
97 feenableexcept(except | FE_DIVBYZERO | FE_INVALID);
99 signal(SIGFPE, handleFPE);
103 static void handleFPE(int)
105 feclearexcept(FE_ALL_EXCEPT);
106 SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
107 signal(SIGFPE, handleFPE);
109 #elif defined (SG_WINDOWS)
111 static void initFPE(bool fpeAbort)
113 // Enable floating-point exceptions for Windows
115 // set following link for what this does (note it does set SSE
116 // flags too, it's not just for the x87 FPU)
117 // http://msdn.microsoft.com/en-us/library/e9b52ceh.aspx
118 _control87( _EM_INEXACT, _MCW_EM );
123 static void initFPE(bool)
125 // Ignore floating-point exceptions on FreeBSD, OS-X, other Unices
126 signal(SIGFPE, SIG_IGN);
130 #if defined(SG_WINDOWS)
131 int main ( int argc, char **argv );
132 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
133 LPSTR lpCmdLine, int nCmdShow) {
135 main( __argc, __argv );
139 static void fg_terminate()
141 flightgear::fatalMessageBox("Fatal exception", "Uncaught exception on some thread");
144 int _bootstrap_OSInit;
146 // Main entry point; catch any exceptions that have made it this far.
147 int main ( int argc, char **argv )
149 #if defined(SG_WINDOWS)
150 // Don't show blocking "no disk in drive" error messages on Windows 7,
151 // silently return errors to application instead.
152 // See Microsoft MSDN #ms680621: "GUI apps should specify SEM_NOOPENFILEERRORBOX"
153 SetErrorMode(SEM_NOOPENFILEERRORBOX);
155 hostname = ::getenv( "COMPUTERNAME" );
157 // Unix(alike) systems
159 gethostname(_hostname, 256);
160 hostname = _hostname;
162 signal(SIGPIPE, SIG_IGN);
165 _bootstrap_OSInit = 0;
167 #if defined(HAVE_CRASHRPT)
168 // Define CrashRpt configuration parameters
169 CR_INSTALL_INFO info;
170 memset(&info, 0, sizeof(CR_INSTALL_INFO));
171 info.cb = sizeof(CR_INSTALL_INFO);
172 info.pszAppName = "FlightGear";
173 info.pszAppVersion = FLIGHTGEAR_VERSION;
174 info.pszEmailSubject = "FlightGear " FLIGHTGEAR_VERSION " crash report";
175 info.pszEmailTo = "fgcrash@goneabitbursar.com";
176 info.pszUrl = "http://fgfs.goneabitbursar.com/crashreporter/crashrpt.php";
177 info.uPriorities[CR_HTTP] = 3;
178 info.uPriorities[CR_SMTP] = 2;
179 info.uPriorities[CR_SMAPI] = 1;
181 // Install all available exception handlers
182 info.dwFlags |= CR_INST_ALL_POSSIBLE_HANDLERS;
184 // Restart the app on crash
185 info.dwFlags |= CR_INST_SEND_QUEUED_REPORTS;
187 // automatically install handlers for all threads
188 info.dwFlags |= CR_INST_AUTO_THREAD_HANDLERS;
190 // Define the Privacy Policy URL
191 info.pszPrivacyPolicyURL = "http://flightgear.org/crash-privacypolicy.html";
193 // Install crash reporting
194 int nResult = crInstall(&info);
197 crGetLastErrorMsg(buf, 1024);
198 flightgear::modalMessageBox("CrashRpt setup failed",
199 "Failed to setup crash-reporting engine, check the installation is not damaged.",
202 global_crashRptEnabled = true;
204 crAddProperty("hudson-build-id", HUDSON_BUILD_ID);
206 ::snprintf(buf, 16, "%d", HUDSON_BUILD_NUMBER);
207 crAddProperty("hudson-build-number", buf);
208 crAddProperty("git-revision", REVISION);
209 #if defined(FG_NIGHTLY)
210 crAddProperty("nightly-build", "true");
215 initFPE(flightgear::Options::checkForArg(argc, argv, "enable-fpe"));
217 // pick up all user locale settings, but force C locale for numerical/sorting
218 // conversions because we have lots of code which assumes standard
220 setlocale(LC_ALL, "");
221 setlocale(LC_NUMERIC, "C");
222 setlocale(LC_COLLATE, "C");
224 bool fgviewer = flightgear::Options::checkForArg(argc, argv, "fgviewer");
226 // http://code.google.com/p/flightgear-bugs/issues/detail?id=1231
227 // ensure sglog is inited before atexit() is registered, so logging
228 // is possible inside fgExitCleanup
232 #if (OPENSCENEGRAPH_MAJOR_VERSION == 3) && (OPENSCENEGRAPH_MINOR_VERSION < 5)
233 // similar to above, ensure some static maps inside OSG exist before
234 // we register our at-exit handler, otherwise the statics are gone
235 // when fg_terminate runs, which causes crashes.
236 osg::Texture::getTextureObjectManager(0);
237 osg::GLBufferObjectManager::getGLBufferObjectManager(0);
239 std::set_terminate(fg_terminate);
240 atexit(fgExitCleanup);
242 fgviewerMain(argc, argv);
244 fgMainInit(argc, argv);
246 } catch (const sg_throwable &t) {
248 if (std::strlen(t.getOrigin()) != 0)
249 info = std::string("received from ") + t.getOrigin();
250 flightgear::fatalMessageBox("Fatal exception", t.getFormattedMessage(), info);
252 } catch (const std::exception &e ) {
253 flightgear::fatalMessageBox("Fatal exception", e.what());
254 } catch (const std::string &s) {
255 flightgear::fatalMessageBox("Fatal exception", s);
256 } catch (const char *s) {
257 std::cerr << "Fatal error (const char*): " << s << std::endl;
260 std::cerr << "Unknown exception in the main loop. Aborting..." << std::endl;
262 perror("Possible cause");
265 #if defined(HAVE_CRASHRPT)
272 // do some clean up on exit. Specifically we want to delete the sound-manager,
273 // so OpenAL device and context are released cleanly
274 void fgExitCleanup() {
276 if (_bootstrap_OSInit != 0) {
277 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
282 // on the common exit path globals is already deleted, and NULL,
283 // so this only happens on error paths.