diff options
Diffstat (limited to 'src/KINDLE/i_main.c')
-rw-r--r-- | src/KINDLE/i_main.c | 409 |
1 files changed, 409 insertions, 0 deletions
diff --git a/src/KINDLE/i_main.c b/src/KINDLE/i_main.c new file mode 100644 index 0000000..8c929b6 --- /dev/null +++ b/src/KINDLE/i_main.c | |||
@@ -0,0 +1,409 @@ | |||
1 | /* Emacs style mode select -*- C++ -*- | ||
2 | *----------------------------------------------------------------------------- | ||
3 | * | ||
4 | * | ||
5 | * PrBoom: a Doom port merged with LxDoom and LSDLDoom | ||
6 | * based on BOOM, a modified and improved DOOM engine | ||
7 | * Copyright (C) 1999 by | ||
8 | * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman | ||
9 | * Copyright (C) 1999-2000 by | ||
10 | * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze | ||
11 | * Copyright 2005, 2006 by | ||
12 | * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or | ||
15 | * modify it under the terms of the GNU General Public License | ||
16 | * as published by the Free Software Foundation; either version 2 | ||
17 | * of the License, or (at your option) any later version. | ||
18 | * | ||
19 | * This program is distributed in the hope that it will be useful, | ||
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
22 | * GNU General Public License for more details. | ||
23 | * | ||
24 | * You should have received a copy of the GNU General Public License | ||
25 | * along with this program; if not, write to the Free Software | ||
26 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
27 | * 02111-1307, USA. | ||
28 | * | ||
29 | * DESCRIPTION: | ||
30 | * Startup and quit functions. Handles signals, inits the | ||
31 | * memory management, then calls D_DoomMain. Also contains | ||
32 | * I_Init which does other system-related startup stuff. | ||
33 | * | ||
34 | *----------------------------------------------------------------------------- | ||
35 | */ | ||
36 | |||
37 | #ifdef HAVE_CONFIG_H | ||
38 | #include "config.h" | ||
39 | #endif | ||
40 | #ifdef HAVE_UNISTD_H | ||
41 | #include <unistd.h> | ||
42 | #endif | ||
43 | #ifdef USE_SDL | ||
44 | #include "SDL.h" | ||
45 | #endif | ||
46 | #include "doomdef.h" | ||
47 | #include "m_argv.h" | ||
48 | #include "d_main.h" | ||
49 | #include "m_fixed.h" | ||
50 | #include "i_system.h" | ||
51 | #include "i_video.h" | ||
52 | #include "z_zone.h" | ||
53 | #include "lprintf.h" | ||
54 | #include "m_random.h" | ||
55 | #include "doomstat.h" | ||
56 | #include "g_game.h" | ||
57 | #include "m_misc.h" | ||
58 | #include "i_sound.h" | ||
59 | #include "i_main.h" | ||
60 | #include "r_fps.h" | ||
61 | #include "lprintf.h" | ||
62 | |||
63 | #include <signal.h> | ||
64 | #include <stdio.h> | ||
65 | #include <stdlib.h> | ||
66 | |||
67 | /* Most of the following has been rewritten by Lee Killough | ||
68 | * | ||
69 | * I_GetTime | ||
70 | * killough 4/13/98: Make clock rate adjustable by scale factor | ||
71 | * cphipps - much made static | ||
72 | */ | ||
73 | |||
74 | int realtic_clock_rate = 100; | ||
75 | static int_64_t I_GetTime_Scale = 1<<24; | ||
76 | |||
77 | static int I_GetTime_Scaled(void) | ||
78 | { | ||
79 | return (int)( (int_64_t) I_GetTime_RealTime() * I_GetTime_Scale >> 24); | ||
80 | } | ||
81 | |||
82 | |||
83 | |||
84 | static int I_GetTime_FastDemo(void) | ||
85 | { | ||
86 | static int fasttic; | ||
87 | return fasttic++; | ||
88 | } | ||
89 | |||
90 | |||
91 | |||
92 | static int I_GetTime_Error(void) | ||
93 | { | ||
94 | I_Error("I_GetTime_Error: GetTime() used before initialization"); | ||
95 | return 0; | ||
96 | } | ||
97 | |||
98 | |||
99 | |||
100 | int (*I_GetTime)(void) = I_GetTime_Error; | ||
101 | |||
102 | void I_Init(void) | ||
103 | { | ||
104 | /* killough 4/14/98: Adjustable speedup based on realtic_clock_rate */ | ||
105 | if (fastdemo) | ||
106 | I_GetTime = I_GetTime_FastDemo; | ||
107 | else | ||
108 | if (realtic_clock_rate != 100) | ||
109 | { | ||
110 | I_GetTime_Scale = ((int_64_t) realtic_clock_rate << 24) / 100; | ||
111 | I_GetTime = I_GetTime_Scaled; | ||
112 | } | ||
113 | else | ||
114 | I_GetTime = I_GetTime_RealTime; | ||
115 | |||
116 | { | ||
117 | /* killough 2/21/98: avoid sound initialization if no sound & no music */ | ||
118 | if (!(nomusicparm && nosfxparm)) | ||
119 | I_InitSound(); | ||
120 | } | ||
121 | |||
122 | R_InitInterpolation(); | ||
123 | } | ||
124 | |||
125 | /* cleanup handling -- killough: | ||
126 | */ | ||
127 | static void I_SignalHandler(int s) | ||
128 | { | ||
129 | char buf[2048]; | ||
130 | |||
131 | signal(s,SIG_IGN); /* Ignore future instances of this signal.*/ | ||
132 | |||
133 | strcpy(buf,"Exiting on signal: "); | ||
134 | I_SigString(buf+strlen(buf),2000-strlen(buf),s); | ||
135 | |||
136 | /* If corrupted memory could cause crash, dump memory | ||
137 | * allocation history, which points out probable causes | ||
138 | */ | ||
139 | if (s==SIGSEGV || s==SIGILL || s==SIGFPE) | ||
140 | Z_DumpHistory(buf); | ||
141 | |||
142 | I_Error("I_SignalHandler: %s", buf); | ||
143 | } | ||
144 | |||
145 | |||
146 | |||
147 | /* killough 2/22/98: Add support for ENDBOOM, which is PC-specific | ||
148 | * | ||
149 | * this converts BIOS color codes to ANSI codes. | ||
150 | * Its not pretty, but it does the job - rain | ||
151 | * CPhipps - made static | ||
152 | */ | ||
153 | |||
154 | inline static int convert(int color, int *bold) | ||
155 | { | ||
156 | if (color > 7) { | ||
157 | color -= 8; | ||
158 | *bold = 1; | ||
159 | } | ||
160 | switch (color) { | ||
161 | case 0: | ||
162 | return 0; | ||
163 | case 1: | ||
164 | return 4; | ||
165 | case 2: | ||
166 | return 2; | ||
167 | case 3: | ||
168 | return 6; | ||
169 | case 4: | ||
170 | return 1; | ||
171 | case 5: | ||
172 | return 5; | ||
173 | case 6: | ||
174 | return 3; | ||
175 | case 7: | ||
176 | return 7; | ||
177 | } | ||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | /* CPhipps - flags controlling ENDOOM behaviour */ | ||
182 | enum { | ||
183 | endoom_colours = 1, | ||
184 | endoom_nonasciichars = 2, | ||
185 | endoom_droplastline = 4 | ||
186 | }; | ||
187 | |||
188 | unsigned int endoom_mode; | ||
189 | |||
190 | static void PrintVer(void) | ||
191 | { | ||
192 | char vbuf[200]; | ||
193 | lprintf(LO_INFO,"%s\n",I_GetVersionString(vbuf,200)); | ||
194 | } | ||
195 | |||
196 | /* I_EndDoom | ||
197 | * Prints out ENDOOM or ENDBOOM, using some common sense to decide which. | ||
198 | * cphipps - moved to l_main.c, made static | ||
199 | */ | ||
200 | static void I_EndDoom(void) | ||
201 | { | ||
202 | int lump_eb, lump_ed, lump = -1; | ||
203 | |||
204 | /* CPhipps - ENDOOM/ENDBOOM selection */ | ||
205 | lump_eb = W_CheckNumForName("ENDBOOM");/* jff 4/1/98 sign our work */ | ||
206 | lump_ed = W_CheckNumForName("ENDOOM"); /* CPhipps - also maybe ENDOOM */ | ||
207 | |||
208 | if (lump_eb == -1) | ||
209 | lump = lump_ed; | ||
210 | else if (lump_ed == -1) | ||
211 | lump = lump_eb; | ||
212 | else | ||
213 | { /* Both ENDOOM and ENDBOOM are present */ | ||
214 | #define LUMP_IS_NEW(num) (!((lumpinfo[num].source == source_iwad) || (lumpinfo[num].source == source_auto_load))) | ||
215 | switch ((LUMP_IS_NEW(lump_ed) ? 1 : 0 ) | | ||
216 | (LUMP_IS_NEW(lump_eb) ? 2 : 0)) { | ||
217 | case 1: | ||
218 | lump = lump_ed; | ||
219 | break; | ||
220 | case 2: | ||
221 | lump = lump_eb; | ||
222 | break; | ||
223 | default: | ||
224 | /* Both lumps have equal priority, both present */ | ||
225 | lump = (P_Random(pr_misc) & 1) ? lump_ed : lump_eb; | ||
226 | break; | ||
227 | } | ||
228 | } | ||
229 | |||
230 | if (lump != -1) | ||
231 | { | ||
232 | const char (*endoom)[2] = (const void*)W_CacheLumpNum(lump); | ||
233 | int i, l = W_LumpLength(lump) / 2; | ||
234 | |||
235 | /* cph - colour ENDOOM by rain */ | ||
236 | int oldbg = -1, oldcolor = -1, bold = 0, oldbold = -1, color = 0; | ||
237 | #ifndef _WIN32 | ||
238 | if (endoom_mode & endoom_nonasciichars) | ||
239 | /* switch to secondary charset, and set to cp437 (IBM charset) */ | ||
240 | printf("\e)K\016"); | ||
241 | #endif /* _WIN32 */ | ||
242 | |||
243 | /* cph - optionally drop the last line, so everything fits on one screen */ | ||
244 | if (endoom_mode & endoom_droplastline) | ||
245 | l -= 80; | ||
246 | lprintf(LO_INFO,"\n"); | ||
247 | for (i=0; i<l; i++) | ||
248 | { | ||
249 | #ifdef _WIN32 | ||
250 | I_ConTextAttr(endoom[i][1]); | ||
251 | #elif defined (DJGPP) | ||
252 | textattr(endoom[i][1]); | ||
253 | #else | ||
254 | if (endoom_mode & endoom_colours) | ||
255 | { | ||
256 | if (!(i % 80)) | ||
257 | { | ||
258 | /* reset color but not bold when we start a new line */ | ||
259 | oldbg = -1; | ||
260 | oldcolor = -1; | ||
261 | printf("\e[39m\e[49m\n"); | ||
262 | } | ||
263 | /* foreground color */ | ||
264 | bold = 0; | ||
265 | color = endoom[i][1] % 16; | ||
266 | if (color != oldcolor) | ||
267 | { | ||
268 | oldcolor = color; | ||
269 | color = convert(color, &bold); | ||
270 | if (oldbold != bold) | ||
271 | { | ||
272 | oldbold = bold; | ||
273 | printf("\e[%cm", bold + '0'); | ||
274 | if (!bold) oldbg = -1; | ||
275 | } | ||
276 | /* we buffer everything or output is horrendously slow */ | ||
277 | printf("\e[%dm", color + 30); | ||
278 | bold = 0; | ||
279 | } | ||
280 | /* background color */ | ||
281 | color = endoom[i][1] / 16; | ||
282 | if (color != oldbg) | ||
283 | { | ||
284 | oldbg = color; | ||
285 | color = convert(color, &bold); | ||
286 | printf("\e[%dm", color + 40); | ||
287 | } | ||
288 | } | ||
289 | #endif | ||
290 | /* cph - portable ascii printout if requested */ | ||
291 | if (isascii(endoom[i][0]) || (endoom_mode & endoom_nonasciichars)) | ||
292 | lprintf(LO_INFO,"%c",endoom[i][0]); | ||
293 | else /* Probably a box character, so do #'s */ | ||
294 | lprintf(LO_INFO,"#"); | ||
295 | } | ||
296 | #ifndef _WIN32 | ||
297 | lprintf(LO_INFO,"\b"); /* hack workaround for extra newline at bottom of screen */ | ||
298 | lprintf(LO_INFO,"\r"); | ||
299 | if (endoom_mode & endoom_nonasciichars) | ||
300 | printf("%c",'\017'); /* restore primary charset */ | ||
301 | #endif /* _WIN32 */ | ||
302 | W_UnlockLumpNum(lump); | ||
303 | } | ||
304 | #ifndef _WIN32 | ||
305 | if (endoom_mode & endoom_colours) | ||
306 | puts("\e[0m"); /* cph - reset colours */ | ||
307 | PrintVer(); | ||
308 | #endif /* _WIN32 */ | ||
309 | } | ||
310 | |||
311 | static int has_exited; | ||
312 | |||
313 | /* I_SafeExit | ||
314 | * This function is called instead of exit() by functions that might be called | ||
315 | * during the exit process (i.e. after exit() has already been called) | ||
316 | * Prevent infinitely recursive exits -- killough | ||
317 | */ | ||
318 | |||
319 | void I_SafeExit(int rc) | ||
320 | { | ||
321 | if (!has_exited) /* If it hasn't exited yet, exit now -- killough */ | ||
322 | { | ||
323 | has_exited=rc ? 2 : 1; | ||
324 | exit(rc); | ||
325 | } | ||
326 | } | ||
327 | |||
328 | static void I_Quit (void) | ||
329 | { | ||
330 | if (!has_exited) | ||
331 | has_exited=1; /* Prevent infinitely recursive exits -- killough */ | ||
332 | |||
333 | if (has_exited == 1) { | ||
334 | I_EndDoom(); | ||
335 | if (demorecording) | ||
336 | G_CheckDemoStatus(); | ||
337 | M_SaveDefaults (); | ||
338 | } | ||
339 | } | ||
340 | |||
341 | #ifdef SECURE_UID | ||
342 | uid_t stored_euid = -1; | ||
343 | #endif | ||
344 | |||
345 | //int main(int argc, const char * const * argv) | ||
346 | int main(int argc, char **argv) | ||
347 | { | ||
348 | #ifdef SECURE_UID | ||
349 | /* First thing, revoke setuid status (if any) */ | ||
350 | stored_euid = geteuid(); | ||
351 | if (getuid() != stored_euid) | ||
352 | if (seteuid(getuid()) < 0) | ||
353 | fprintf(stderr, "Failed to revoke setuid\n"); | ||
354 | else | ||
355 | fprintf(stderr, "Revoked uid %d\n",stored_euid); | ||
356 | #endif | ||
357 | |||
358 | myargc = argc; | ||
359 | myargv = argv; | ||
360 | |||
361 | #ifdef _WIN32 | ||
362 | if (!M_CheckParm("-nodraw")) { | ||
363 | /* initialize the console window */ | ||
364 | Init_ConsoleWin(); | ||
365 | atexit(Done_ConsoleWin); | ||
366 | } | ||
367 | #endif | ||
368 | /* Version info */ | ||
369 | lprintf(LO_INFO,"\n"); | ||
370 | PrintVer(); | ||
371 | |||
372 | /* cph - Z_Close must be done after I_Quit, so we register it first. */ | ||
373 | atexit(Z_Close); | ||
374 | /* | ||
375 | killough 1/98: | ||
376 | |||
377 | This fixes some problems with exit handling | ||
378 | during abnormal situations. | ||
379 | |||
380 | The old code called I_Quit() to end program, | ||
381 | while now I_Quit() is installed as an exit | ||
382 | handler and exit() is called to exit, either | ||
383 | normally or abnormally. Seg faults are caught | ||
384 | and the error handler is used, to prevent | ||
385 | being left in graphics mode or having very | ||
386 | loud SFX noise because the sound card is | ||
387 | left in an unstable state. | ||
388 | */ | ||
389 | |||
390 | Z_Init(); /* 1/18/98 killough: start up memory stuff first */ | ||
391 | |||
392 | atexit(I_Quit); | ||
393 | #ifndef _DEBUG | ||
394 | signal(SIGSEGV, I_SignalHandler); | ||
395 | signal(SIGTERM, I_SignalHandler); | ||
396 | signal(SIGFPE, I_SignalHandler); | ||
397 | signal(SIGILL, I_SignalHandler); | ||
398 | signal(SIGINT, I_SignalHandler); /* killough 3/6/98: allow CTRL-BRK during init */ | ||
399 | signal(SIGABRT, I_SignalHandler); | ||
400 | #endif | ||
401 | |||
402 | I_SetAffinityMask(); | ||
403 | |||
404 | /* cphipps - call to video specific startup code */ | ||
405 | I_PreInitGraphics(); | ||
406 | |||
407 | D_DoomMain (); | ||
408 | return 0; | ||
409 | } | ||