]> git.mxchange.org Git - flightgear.git/blob - src/Main/bootstrap.cxx
Set the format default to float instead of int.
[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  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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 #  include <signal.h>
31 #endif
32
33 #include <stdlib.h>
34
35 #include <simgear/compiler.h>
36 #include <simgear/structure/exception.hxx>
37 #include <simgear/debug/logstream.hxx>
38
39 #include STL_IOSTREAM
40 SG_USING_STD(cerr);
41 SG_USING_STD(endl);
42
43 #include "main.hxx"
44 #include "globals.hxx"
45
46
47 #ifdef HAVE_WINDOWS_H
48 #  include <windows.h>
49 #  include <float.h>
50 #endif
51
52 #include "fg_os.hxx"
53
54 #ifdef macintosh
55 #  include <console.h>          // -dw- for command line dialog
56 #endif
57
58 // foreward declaration.
59 void fgExitCleanup();
60
61 #if defined(__linux__) && defined(__i386__)
62
63 static void handleFPE (int);
64
65 static void
66 initFPE ()
67 {
68     fpu_control_t fpe_flags = 0;
69     _FPU_GETCW(fpe_flags);
70 //     fpe_flags &= ~_FPU_MASK_IM;      // invalid operation
71 //     fpe_flags &= ~_FPU_MASK_DM;      // denormalized operand
72 //     fpe_flags &= ~_FPU_MASK_ZM;      // zero-divide
73 //     fpe_flags &= ~_FPU_MASK_OM;      // overflow
74 //     fpe_flags &= ~_FPU_MASK_UM;      // underflow
75 //     fpe_flags &= ~_FPU_MASK_PM;      // precision (inexact result)
76     _FPU_SETCW(fpe_flags);
77     signal(SIGFPE, handleFPE);
78 }
79
80 static void
81 handleFPE (int num)
82 {
83   initFPE();
84   SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
85 }
86 #endif
87
88 #ifdef __APPLE__
89
90 typedef struct
91 {
92   int  lo;
93   int  hi;
94 } PSN;
95
96 extern "C" {
97   short CPSGetCurrentProcess(PSN *psn);
98   short CPSSetProcessName (PSN *psn, char *processname);
99   short CPSEnableForegroundOperation(PSN *psn, int _arg2, int _arg3, int _arg4, int _arg5);
100   short CPSSetFrontProcess(PSN *psn);
101 };
102
103 #define CPSEnableFG(psn) CPSEnableForegroundOperation(psn,0x03,0x3C,0x2C,0x1103)
104
105 #endif
106
107 #ifdef _MSC_VER
108 int main ( int argc, char **argv );
109 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
110                              LPSTR lpCmdLine, int nCmdShow) {
111
112   main( __argc, __argv );
113 }
114 #endif
115
116 #if defined( sgi )
117 #include <sys/fpu.h>
118
119 /*
120     set the special "flush zero" bit (FS, bit 24) in the Control Status
121     Register of the FPU of R4k and beyond so that the result of any
122     underflowing operation will be clamped to zero, and no exception of
123     any kind will be generated on the CPU.  This has no effect on an
124     R3000.
125
126     the FS bit is inherited by processes fork()ed out of this one,
127     but it is not inherited across an exec().  so anytime you exec()
128     a process, you must re-set the FS bit in that process.
129   */
130 void flush_fpe(void)
131 {
132     union fpc_csr f;
133     f.fc_word = get_fpc_csr();
134     f.fc_struct.flush = 1;
135     set_fpc_csr(f.fc_word);
136 }
137 #endif
138
139 int _bootstrap_OSInit;
140
141 // Main entry point; catch any exceptions that have made it this far.
142 int main ( int argc, char **argv ) {
143
144     _bootstrap_OSInit = 0;
145
146     // Enable floating-point exceptions for Linux/x86
147 #if defined(__linux__) && defined(__i386__)
148     initFPE();
149 #endif
150
151 #if defined(sgi)
152     flush_fpe();
153 #endif
154
155     // Enable floating-point exceptions for Windows
156 #if defined( _MSC_VER ) && defined( DEBUG )
157     // Christian, we should document what this does
158     _control87( _EM_INEXACT, _MCW_EM );
159 #endif
160
161 #if defined( HAVE_BC5PLUS )
162     _control87(MCW_EM, MCW_EM);  /* defined in float.h */
163 #endif
164
165     // Keyboard focus hack
166 #if defined(__APPLE__) && !defined(OSX_BUNDLE)
167     {
168       PSN psn;
169
170       fgOSInit (&argc, argv);
171       _bootstrap_OSInit++;
172
173       CPSGetCurrentProcess(&psn);
174       CPSSetProcessName(&psn, "FlightGear");
175       CPSEnableFG(&psn);
176       CPSSetFrontProcess(&psn);
177     }
178 #endif
179
180     // FIXME: add other, more specific
181     // exceptions.
182     try {
183         atexit(fgExitCleanup);
184         fgMainInit(argc, argv);
185     } catch (sg_throwable &t) {
186                             // We must use cerr rather than
187                             // logging, since logging may be
188                             // disabled.
189         cerr << "Fatal error: " << t.getFormattedMessage()
190              << "\n (received from " << t.getOrigin() << ')' << endl;
191         exit(1);
192     }
193
194     return 0;
195 }
196
197 // do some clean up on exit.  Specifically we want to call alutExit()
198 // which happens in the sound manager destructor.
199 void fgExitCleanup() {
200
201     if (_bootstrap_OSInit != 0)
202         fgSetMouseCursor(MOUSE_CURSOR_POINTER);
203
204     if (globals)
205         delete globals;
206 }
207