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