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