]> git.mxchange.org Git - flightgear.git/blob - src/Main/bootstrap.cxx
9e4a57513f4ddacac5c668b80f636704723d43ae
[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 <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 _MSC_VER
92 int main ( int argc, char **argv );
93 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
94                              LPSTR lpCmdLine, int nCmdShow) {
95
96   logbuf::has_no_console();
97   main( __argc, __argv );
98 }
99 #endif
100
101 #if defined( sgi )
102 #include <sys/fpu.h>
103 #include <sys/sysmp.h>
104 #include <unistd.h>
105
106 /*
107  *  set the special "flush zero" bit (FS, bit 24) in the Control Status
108  *  Register of the FPU of R4k and beyond so that the result of any
109  *  underflowing operation will be clamped to zero, and no exception of
110  *  any kind will be generated on the CPU.  This has no effect on an
111  *  R3000.
112  */
113 void flush_fpe(void)
114 {
115     union fpc_csr f;
116     f.fc_word = get_fpc_csr();
117     f.fc_struct.flush = 1;
118     set_fpc_csr(f.fc_word);
119 }
120 #endif
121
122 static void fg_terminate() {
123     cerr << endl <<
124             "Uncaught Exception: you should see a meaningful error message\n"
125             "here, but your GLUT (or SDL) library was apparently compiled\n"
126             "and/or linked without exception support. Please complain to\n"
127             "its provider!"
128             << endl << endl;
129     abort();
130 }
131
132 int _bootstrap_OSInit;
133
134 // Main entry point; catch any exceptions that have made it this far.
135 int main ( int argc, char **argv ) {
136
137 #ifdef PTW32_STATIC_LIB
138     // Initialise static pthread win32 lib
139     pthread_win32_process_attach_np ();
140 #endif
141     _bootstrap_OSInit = 0;
142
143 #if defined(__linux__) && defined(__i386__)
144     // Enable floating-point exceptions for Linux/x86
145     initFPE();
146 #elif defined(__FreeBSD__)
147     // Ignore floating-point exceptions on FreeBSD
148     signal(SIGFPE, SIG_IGN);
149 #endif
150 #ifndef _MSC_VER
151     signal(SIGPIPE, SIG_IGN);
152 #endif
153
154 #if defined(sgi)
155     flush_fpe();
156
157     // Bind all non-rendering threads to CPU1
158     // This will reduce the jitter caused by them to an absolute minimum,
159     // but it will only work with superuser authority.
160     if ( geteuid() == 0 )
161     {
162        sysmp(MP_CLOCK, 0);              // bind the timer only to CPU0
163        sysmp(MP_ISOLATE, 1 );           // Isolate CPU1
164        sysmp(MP_NONPREEMPTIVE, 1 );     // disable process time slicing on CPU1
165     }
166 #endif
167
168     // Enable floating-point exceptions for Windows
169 #if defined( _MSC_VER ) && defined( DEBUG )
170     // Christian, we should document what this does
171     _control87( _EM_INEXACT, _MCW_EM );
172 #endif
173
174 #if defined( HAVE_BC5PLUS )
175     _control87(MCW_EM, MCW_EM);  /* defined in float.h */
176 #endif
177
178     // FIXME: add other, more specific
179     // exceptions.
180     try {
181         std::set_terminate(fg_terminate);
182         atexit(fgExitCleanup);
183         fgMainInit(argc, argv);
184     } catch (const sg_throwable &t) {
185                             // We must use cerr rather than
186                             // logging, since logging may be
187                             // disabled.
188         cerr << "Fatal error: " << t.getFormattedMessage() << endl;
189         if (!t.getOrigin().empty())
190             cerr << " (received from " << t.getOrigin() << ')' << endl;
191
192     } catch (const string &s) {
193         cerr << "Fatal error: " << s << endl;
194
195     } catch (const char *s) {
196         cerr << "Fatal error: " << s << endl;
197
198     } catch (...) {
199         cerr << "Unknown exception in the main loop. Aborting..." << endl;
200         if (errno)
201             perror("Possible cause");
202     }
203
204     return 0;
205 }
206
207 // do some clean up on exit.  Specifically we want to call alutExit()
208 // which happens in the sound manager destructor.
209 void fgExitCleanup() {
210
211     if (_bootstrap_OSInit != 0)
212         fgSetMouseCursor(MOUSE_CURSOR_POINTER);
213
214     delete globals;
215
216     if (free_hostname && hostname != NULL)
217         free(hostname);
218 }
219