Debian Codegraph @

prboom2/src/SDL/i_main.c

raw
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
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44
45 #ifdef _WIN32
46 #define WIN32_LEAN_AND_MEAN
47 #include <windows.h>
48 #endif
49
50 #include <errno.h>
51
52 #include "doomdef.h"
53 #include "d_main.h"
54 #include "m_fixed.h"
55 #include "i_system.h"
56 #include "i_video.h"
57 #include "z_zone.h"
58 #include "lprintf.h"
59 #include "m_random.h"
60 #include "doomstat.h"
61 #include "g_game.h"
62 #include "m_misc.h"
63 #include "i_sound.h"
64 #include "i_main.h"
65 #include "r_fps.h"
66 #include "lprintf.h"
67
68 #include <signal.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71
72 #include "e6y.h"
73
74 #include "dsda.h"
75 #include "dsda/args.h"
76 #include "dsda/analysis.h"
77 #include "dsda/args.h"
78 #include "dsda/endoom.h"
79 #include "dsda/settings.h"
80 #include "dsda/signal_context.h"
81 #include "dsda/split_tracker.h"
82 #include "dsda/text_file.h"
83 #include "dsda/time.h"
84 #include "dsda/wad_stats.h"
85 #include "dsda/zipfile.h"
86
87 /* Most of the following has been rewritten by Lee Killough
88 *
89 * killough 4/13/98: Make clock rate adjustable by scale factor
90 * cphipps - much made static
91 */
92
93 void I_Init(void)
94 {
95 dsda_ResetTimeFunctions(fastdemo);
96 I_InitSound();
97 }
98
99 //e6y
100 void I_Init2(void)
101 {
102 dsda_ResetTimeFunctions(fastdemo);
103
104 force_singletics_to = gametic + BACKUPTICS;
105 }
106
107 int signal_context;
108
109 static volatile sig_atomic_t interrupted = 0;
110
111 dboolean I_Interrupted(void)
112 {
113 return interrupted;
114 }
115
116 static void I_SignalHandler(int s)
117 {
118 char buf[2048];
119
120 signal(s, SIG_IGN); /* Ignore future instances of this signal.*/
121
122 // Terminal Interrupt
123 if (s == 2)
124 I_DisableMessageBoxes();
125
126 I_SigString(buf, sizeof(buf), s);
127
128 I_Error("The game has crashed!\n"
129 "Please report the following information: %s (0x%04x)",
130 buf, signal_context);
131 }
132
133 static void I_IntHandler(int s)
134 {
135 interrupted = 1;
136 }
137
138 static void PrintVer(void)
139 {
140 char vbuf[200];
141 lprintf(LO_INFO,"%s\n",I_GetVersionString(vbuf,200));
142 }
143
144 // Schedule a function to be called when the program exits.
145 // If run_if_error is true, the function is called if the exit
146 // is due to an error (I_Error)
147 // Copyright(C) 2005-2014 Simon Howard
148
149 typedef struct atexit_listentry_s atexit_listentry_t;
150
151 struct atexit_listentry_s
152 {
153 atexit_func_t func;
154 dboolean run_on_error;
155 atexit_listentry_t *next;
156 const char* name;
157 };
158
159 static atexit_listentry_t *exit_funcs[exit_priority_max];
160 static int exit_priority;
161
162 void I_AtExit(atexit_func_t func, dboolean run_on_error,
163 const char* name, exit_priority_t priority)
164 {
165 atexit_listentry_t *entry;
166
167 entry = Z_Malloc(sizeof(*entry));
168
169 entry->func = func;
170 entry->run_on_error = run_on_error;
171 entry->next = exit_funcs[priority];
172 entry->name = name;
173 exit_funcs[priority] = entry;
174 }
175
176 void I_SafeExit(int rc)
177 {
178 atexit_listentry_t *entry;
179
180 lprintf(LO_DEBUG, "\n"); // Separator after game loop
181
182 // Run through all exit functions
183 for (; exit_priority < exit_priority_max; ++exit_priority)
184 {
185 while ((entry = exit_funcs[exit_priority]))
186 {
187 exit_funcs[exit_priority] = exit_funcs[exit_priority]->next;
188
189 if (rc == 0 || entry->run_on_error)
190 {
191 lprintf(LO_DEBUG, "Exit Sequence[%d]: %s (%d)\n", exit_priority, entry->name, rc);
192 entry->func();
193 }
194 }
195 }
196
197 exit(rc);
198 }
199
200 static void I_EssentialQuit (void)
201 {
202 if (demorecording)
203 {
204 G_CheckDemoStatus();
205 }
206 dsda_ExportTextFile();
207 dsda_WriteAnalysis();
208 dsda_WriteSplits();
209 dsda_SaveWadStats();
210 // We need to close out all wad handles/memory mappings before we can remove
211 // temporary wads on Windows
212 // Read Endoom before dumping the wads!
213 dsda_CacheEndoom();
214 W_Shutdown();
215 dsda_CleanZipTempDirs();
216 }
217
218 static void I_Quit (void)
219 {
220 M_SaveDefaults ();
221 dsda_DumpEndoom();
222 }
223
224 //
225 // Sets the priority class for the prboom-plus process
226 //
227
228 void I_SetProcessPriority(void)
229 {
230 int process_priority = dsda_IntConfig(dsda_config_process_priority);
231
232 if (process_priority)
233 {
234 const char *errbuf = NULL;
235
236 #ifdef _WIN32
237 {
238 DWORD dwPriorityClass = NORMAL_PRIORITY_CLASS;
239
240 if (process_priority == 1)
241 dwPriorityClass = HIGH_PRIORITY_CLASS;
242 else if (process_priority == 2)
243 dwPriorityClass = REALTIME_PRIORITY_CLASS;
244
245 if (SetPriorityClass(GetCurrentProcess(), dwPriorityClass) == 0)
246 {
247 errbuf = WINError();
248 }
249 }
250 #else
251 return;
252 #endif
253
254 if (errbuf == NULL)
255 {
256 lprintf(LO_INFO, "I_SetProcessPriority: priority for the process is %d\n", process_priority);
257 }
258 else
259 {
260 lprintf(LO_ERROR, "I_SetProcessPriority: failed to set priority for the process (%s)\n", errbuf);
261 }
262 }
263 }
264
265 //int main(int argc, const char * const * argv)
266 int main(int argc, char **argv)
267 {
268 dsda_ParseCommandLineArgs(argc, argv);
269
270 if (dsda_Flag(dsda_arg_verbose))
271 I_EnableVerboseLogging();
272
273 if (dsda_Flag(dsda_arg_quiet))
274 I_DisableAllLogging();
275
276 // Print the version and exit
277 if (dsda_Flag(dsda_arg_v))
278 {
279 PrintVer();
280 return 0;
281 }
282
283 // e6y: Check for conflicts.
284 // Conflicting command-line parameters could cause the engine to be confused
285 // in some cases. Added checks to prevent this.
286 // Example: dsda-doom.exe -record mydemo -playdemo demoname
287 ParamsMatchingCheck();
288
289 // e6y: was moved from D_DoomMainSetup
290 // init subsystems
291 //jff 9/3/98 use logical output routine
292 lprintf(LO_DEBUG, "M_LoadDefaults: Load system defaults.\n");
293 M_LoadDefaults(); // load before initing other systems
294 lprintf(LO_DEBUG, "\n");
295
296 /* Version info */
297 PrintVer();
298
299 /*
300 killough 1/98:
301
302 This fixes some problems with exit handling
303 during abnormal situations.
304
305 The old code called I_Quit() to end program,
306 while now I_Quit() is installed as an exit
307 handler and exit() is called to exit, either
308 normally or abnormally. Seg faults are caught
309 and the error handler is used, to prevent
310 being left in graphics mode or having very
311 loud SFX noise because the sound card is
312 left in an unstable state.
313 */
314
315 I_AtExit(I_EssentialQuit, true, "I_EssentialQuit", exit_priority_first);
316 I_AtExit(I_Quit, false, "I_Quit", exit_priority_last);
317 #ifndef PRBOOM_DEBUG
318 if (!dsda_Flag(dsda_arg_sigsegv))
319 {
320 signal(SIGSEGV, I_SignalHandler);
321 }
322 signal(SIGFPE, I_SignalHandler);
323 signal(SIGILL, I_SignalHandler);
324 signal(SIGABRT, I_SignalHandler);
325
326 signal(SIGTERM, I_IntHandler);
327 signal(SIGINT, I_IntHandler);
328 #endif
329
330 // Priority class for the prboom-plus process
331 I_SetProcessPriority();
332
333 /* cphipps - call to video specific startup code */
334 I_PreInitGraphics();
335
336 D_DoomMain ();
337 return 0;
338 }