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()
54 #include <simgear/compiler.h>
55 #include <simgear/structure/exception.hxx>
57 #include <osg/Texture>
58 #include <osg/BufferObject>
60 #include <Viewer/fgviewer.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>
71 #include <GUI/CocoaHelpers.h> // for transformToForegroundApp
74 #if defined(HAVE_CRASHRPT)
77 bool global_crashRptEnabled = false;
87 // forward declaration.
90 static void initFPE(bool enableExceptions);
92 #if defined(__linux__)
94 static void handleFPE(int);
96 initFPE (bool fpeAbort)
99 int except = fegetexcept();
100 feenableexcept(except | FE_DIVBYZERO | FE_INVALID);
102 signal(SIGFPE, handleFPE);
106 static void handleFPE(int)
108 feclearexcept(FE_ALL_EXCEPT);
109 SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
110 signal(SIGFPE, handleFPE);
112 #elif defined (SG_WINDOWS)
114 static void initFPE(bool fpeAbort)
116 // Enable floating-point exceptions for Windows
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 );
126 static void initFPE(bool)
128 // Ignore floating-point exceptions on FreeBSD, OS-X, other Unices
129 signal(SIGFPE, SIG_IGN);
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) {
138 main( __argc, __argv );
142 static void fg_terminate()
144 flightgear::fatalMessageBox("Fatal exception", "Uncaught exception on some thread");
147 int _bootstrap_OSInit;
149 // Main entry point; catch any exceptions that have made it this far.
150 int main ( int argc, char **argv )
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);
158 hostname = ::getenv( "COMPUTERNAME" );
160 // Unix(alike) systems
162 gethostname(_hostname, 256);
163 hostname = _hostname;
165 signal(SIGPIPE, SIG_IGN);
168 _bootstrap_OSInit = 0;
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;
184 // Install all available exception handlers
185 info.dwFlags |= CR_INST_ALL_POSSIBLE_HANDLERS;
187 // Restart the app on crash
188 info.dwFlags |= CR_INST_SEND_QUEUED_REPORTS;
190 // automatically install handlers for all threads
191 info.dwFlags |= CR_INST_AUTO_THREAD_HANDLERS;
193 // Define the Privacy Policy URL
194 info.pszPrivacyPolicyURL = "http://flightgear.org/crash-privacypolicy.html";
196 // Install crash reporting
197 int nResult = crInstall(&info);
200 crGetLastErrorMsg(buf, 1024);
201 flightgear::modalMessageBox("CrashRpt setup failed",
202 "Failed to setup crash-reporting engine, check the installation is not damaged.",
205 global_crashRptEnabled = true;
207 crAddProperty("hudson-build-id", HUDSON_BUILD_ID);
209 ::snprintf(buf, 16, "%d", HUDSON_BUILD_NUMBER);
210 crAddProperty("hudson-build-number", buf);
214 initFPE(flightgear::Options::checkForArg(argc, argv, "enable-fpe"));
216 bool fgviewer = flightgear::Options::checkForArg(argc, argv, "fgviewer");
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
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);
229 std::set_terminate(fg_terminate);
230 atexit(fgExitCleanup);
232 fgviewerMain(argc, argv);
234 fgMainInit(argc, argv);
236 } catch (const sg_throwable &t) {
238 if (std::strlen(t.getOrigin()) != 0)
239 info = std::string("received from ") + t.getOrigin();
240 flightgear::fatalMessageBox("Fatal exception", t.getFormattedMessage(), info);
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;
250 std::cerr << "Unknown exception in the main loop. Aborting..." << std::endl;
252 perror("Possible cause");
255 #if defined(HAVE_CRASHRPT)
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() {
266 if (_bootstrap_OSInit != 0) {
267 fgSetMouseCursor(MOUSE_CURSOR_POINTER);
272 // on the common exit path globals is already deleted, and NULL,
273 // so this only happens on error paths.