1 // bootstrap.cxx -- bootstrap routines: main()
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 - 2002 Curtis L. Olson - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #if defined(__linux__) && defined(__i386__)
29 # include <fpu_control.h>
35 #include <simgear/compiler.h>
36 #include <simgear/structure/exception.hxx>
37 #include <simgear/debug/logstream.hxx>
44 #include "globals.hxx"
55 # include <console.h> // -dw- for command line dialog
58 // foreward declaration.
61 #if defined(__linux__) && defined(__i386__)
63 static void handleFPE (int);
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);
84 SG_LOG(SG_GENERAL, SG_ALERT, "Floating point interrupt (SIGFPE)");
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);
103 #define CPSEnableFG(psn) CPSEnableForegroundOperation(psn,0x03,0x3C,0x2C,0x1103)
108 int main ( int argc, char **argv );
109 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
110 LPSTR lpCmdLine, int nCmdShow) {
112 main( __argc, __argv );
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
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.
133 f.fc_word = get_fpc_csr();
134 f.fc_struct.flush = 1;
135 set_fpc_csr(f.fc_word);
139 int _bootstrap_OSInit;
141 // Main entry point; catch any exceptions that have made it this far.
142 int main ( int argc, char **argv ) {
144 _bootstrap_OSInit = 0;
146 // Enable floating-point exceptions for Linux/x86
147 #if defined(__linux__) && defined(__i386__)
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 );
161 #if defined( HAVE_BC5PLUS )
162 _control87(MCW_EM, MCW_EM); /* defined in float.h */
165 // Keyboard focus hack
166 #if defined(__APPLE__) && !defined(OSX_BUNDLE)
170 fgOSInit (&argc, argv);
173 CPSGetCurrentProcess(&psn);
174 CPSSetProcessName(&psn, "FlightGear");
176 CPSSetFrontProcess(&psn);
180 // FIXME: add other, more specific
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
189 cerr << "Fatal error: " << t.getFormattedMessage()
190 << "\n (received from " << t.getOrigin() << ')' << endl;
192 cerr << "Unknown exception in the main loop. Aborting..." << endl;
198 // do some clean up on exit. Specifically we want to call alutExit()
199 // which happens in the sound manager destructor.
200 void fgExitCleanup() {
202 if (_bootstrap_OSInit != 0)
203 fgSetMouseCursor(MOUSE_CURSOR_POINTER);