]> git.mxchange.org Git - flightgear.git/blob - src/Main/bootstrap.cxx
Merge branch 'timoore/ptrfix'
[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 #if defined(HAVE_FEENABLEEXCEPT)
29 #ifndef _GNU_SOURCE
30 #define _GNU_SOURCE
31 #endif
32 #include <fenv.h>
33 #elif defined(__linux__) && defined(__i386__)
34 #  include <fpu_control.h>
35 #endif
36
37 #include <errno.h>
38 #include <signal.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41
42 #include <simgear/compiler.h>
43 #include <simgear/structure/exception.hxx>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/math/SGMath.hxx>
46
47 #include <cstring>
48 #include <iostream>
49 using std::cerr;
50 using std::endl;
51
52 #include "main.hxx"
53 #include "globals.hxx"
54 #include "fg_props.hxx"
55 #include "fgviewer.hxx"
56
57
58 #include "fg_os.hxx"
59
60 char *homedir = ::getenv( "HOME" );
61 char *hostname = ::getenv( "HOSTNAME" );
62 bool free_hostname = false;
63
64 // foreward declaration.
65 void fgExitCleanup();
66
67 static bool fpeAbort = false;
68 static void handleFPE(int);
69 static void initFPE();
70
71 #if defined(HAVE_FEENABLEEXCEPT)
72 static void
73 initFPE ()
74 {
75     if (fpeAbort) {
76         int except = fegetexcept();
77         feenableexcept(except | FE_DIVBYZERO | FE_INVALID);
78     } else {
79         signal(SIGFPE, handleFPE);
80     }
81 }
82
83 static void handleFPE(int)
84 {
85     feclearexcept(FE_ALL_EXCEPT);
86     signal(SIGFPE, handleFPE);
87 }
88 #elif defined(__linux__) && defined(__i386__)
89
90 static void
91 initFPE ()
92 {
93     fpu_control_t fpe_flags = 0;
94     _FPU_GETCW(fpe_flags);
95 //     fpe_flags &= ~_FPU_MASK_IM;      // invalid operation
96 //     fpe_flags &= ~_FPU_MASK_DM;      // denormalized operand
97 //     fpe_flags &= ~_FPU_MASK_ZM;      // zero-divide
98 //     fpe_flags &= ~_FPU_MASK_OM;      // overflow
99 //     fpe_flags &= ~_FPU_MASK_UM;      // underflow
100 //     fpe_flags &= ~_FPU_MASK_PM;      // precision (inexact result)
101     _FPU_SETCW(fpe_flags);
102     signal(SIGFPE, handleFPE);
103 }
104
105 static void
106 handleFPE (int num)
107 {
108   initFPE();
109   SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
110 }
111 #else
112 static void handleFPE(int)
113 {
114 }
115
116 static void initFPE()
117 {
118 }
119 #endif
120
121 #ifdef _MSC_VER
122 int main ( int argc, char **argv );
123 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
124                              LPSTR lpCmdLine, int nCmdShow) {
125
126   logbuf::has_no_console();
127   main( __argc, __argv );
128 }
129 #endif
130
131 #if defined( sgi )
132 #include <sys/fpu.h>
133 #include <sys/sysmp.h>
134 #include <unistd.h>
135
136 /*
137  *  set the special "flush zero" bit (FS, bit 24) in the Control Status
138  *  Register of the FPU of R4k and beyond so that the result of any
139  *  underflowing operation will be clamped to zero, and no exception of
140  *  any kind will be generated on the CPU.  This has no effect on an
141  *  R3000.
142  */
143 void flush_fpe(void)
144 {
145     union fpc_csr f;
146     f.fc_word = get_fpc_csr();
147     f.fc_struct.flush = 1;
148     set_fpc_csr(f.fc_word);
149 }
150 #endif
151
152 static void fg_terminate() {
153     cerr << endl <<
154             "Uncaught Exception: you should see a meaningful error message\n"
155             "here, but your GLUT (or SDL) library was apparently compiled\n"
156             "and/or linked without exception support. Please complain to\n"
157             "its provider!"
158             << endl << endl;
159     abort();
160 }
161
162 int _bootstrap_OSInit;
163
164 // Main entry point; catch any exceptions that have made it this far.
165 int main ( int argc, char **argv ) {
166
167 #ifdef PTW32_STATIC_LIB
168     // Initialise static pthread win32 lib
169     pthread_win32_process_attach_np ();
170 #endif
171     _bootstrap_OSInit = 0;
172
173 #if defined(__FreeBSD__)
174     // Ignore floating-point exceptions on FreeBSD
175     signal(SIGFPE, SIG_IGN); 
176 #else
177     // Maybe Enable floating-point exceptions on Linux
178     for (int i = 0; i < argc; ++i) {
179         if (!strcmp("--enable-fpe", argv[i])) {
180             fpeAbort = true;
181             break;
182         }
183     }
184     initFPE();
185 #endif
186 #ifndef _WIN32
187     signal(SIGPIPE, SIG_IGN);
188 #endif
189
190 #if defined(sgi)
191     flush_fpe();
192
193     // Bind all non-rendering threads to CPU1
194     // This will reduce the jitter caused by them to an absolute minimum,
195     // but it will only work with superuser authority.
196     if ( geteuid() == 0 )
197     {
198        sysmp(MP_CLOCK, 0);              // bind the timer only to CPU0
199        sysmp(MP_ISOLATE, 1 );           // Isolate CPU1
200        sysmp(MP_NONPREEMPTIVE, 1 );     // disable process time slicing on CPU1
201     }
202 #endif
203
204     // Enable floating-point exceptions for Windows
205 #if defined( _MSC_VER ) && defined( DEBUG )
206     // Christian, we should document what this does
207     _control87( _EM_INEXACT, _MCW_EM );
208 #endif
209
210 #if defined( HAVE_BC5PLUS )
211     _control87(MCW_EM, MCW_EM);  /* defined in float.h */
212 #endif
213     bool fgviewer = false;
214     for (int i = 0; i < argc; ++i) {
215         if (!strcmp("--fgviewer", argv[i])) {
216             fgviewer = true;
217             break;
218         }
219     }
220
221     // FIXME: add other, more specific
222     // exceptions.
223     try {
224         std::set_terminate(fg_terminate);
225         atexit(fgExitCleanup);
226         if (fgviewer)
227             fgviewerMain(argc, argv);
228         else
229             fgMainInit(argc, argv);
230     } catch (const sg_throwable &t) {
231                             // We must use cerr rather than
232                             // logging, since logging may be
233                             // disabled.
234         cerr << "Fatal error: " << t.getFormattedMessage() << endl;
235         if (std::strlen(t.getOrigin()) != 0)
236             cerr << " (received from " << t.getOrigin() << ')' << endl;
237
238     } catch (const string &s) {
239         cerr << "Fatal error: " << s << endl;
240
241     } catch (const char *s) {
242         cerr << "Fatal error: " << s << endl;
243
244     } catch (...) {
245         cerr << "Unknown exception in the main loop. Aborting..." << endl;
246         if (errno)
247             perror("Possible cause");
248     }
249
250     return 0;
251 }
252
253 // do some clean up on exit.  Specifically we want to call alutExit()
254 // which happens in the sound manager destructor.
255 void fgExitCleanup() {
256
257     if (_bootstrap_OSInit != 0)
258         fgSetMouseCursor(MOUSE_CURSOR_POINTER);
259
260     delete globals;
261
262     if (free_hostname && hostname != NULL)
263         free(hostname);
264 }
265