Debian Codegraph @

Descent3/sdlmain.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 *
21 * Linux Main and any Linux specific code that MUST be in the main application
22 * goes here.
23 * $NoKeywords: $
24 */
25
26 #include <cstdlib>
27 #include <cstring>
28 #include <filesystem>
29
30 #ifndef WIN32
31 #include <unistd.h>
32 #include <csignal>
33 #endif
34
35 #include <SDL.h>
36
37 #include "program.h"
38 #include "dedicated_server.h"
39 #include "descent.h"
40 #include "application.h"
41 #include "appdatabase.h"
42 #include "args.h"
43 #include "init.h"
44 #include "debug.h"
45
46 #include "osiris_dll.h"
47
48 #include "log.h"
49
50 extern bool ddio_mouseGrabbed;
51
52 std::filesystem::path orig_pwd;
53
54 bool linux_permit_gamma = false;
55
56 static volatile char already_tried_signal_cleanup = 0;
57
58 void ddio_InternalClose(); // needed for emergency cleanup.
59
60 void just_exit(void) {
61 ddio_InternalClose(); // try to reset serial port.
62
63 SDL_Quit();
64 #if defined(POSIX)
65 sync(); // just in case.
66 #endif
67 _exit(0);
68 }
69
70 #if defined(POSIX)
71 void fatal_signal_handler(int signum) {
72 switch (signum) {
73 case SIGHUP:
74 case SIGTRAP:
75 case SIGABRT:
76 case SIGBUS:
77 case SIGFPE:
78 case SIGILL:
79 case SIGQUIT:
80 case SIGSEGV:
81 case SIGTERM:
82 case SIGVTALRM:
83 case SIGINT:
84 if (already_tried_signal_cleanup)
85 fprintf(stderr, "Recursive signal cleanup! Hard exit! AHHGGGG!\n");
86 else {
87 already_tried_signal_cleanup = 1;
88 fprintf(stderr, "SIGNAL %d caught, aborting\n", signum);
89 just_exit();
90 } // else
91 break;
92 case SIGXCPU:
93 case SIGXFSZ:
94 break;
95 }
96
97 _exit(-10);
98 }
99
100 void safe_signal_handler(int signum) {}
101
102 void install_signal_handlers() {
103 struct sigaction sact, fact;
104
105 memset(&sact, 0, sizeof(sact));
106 memset(&fact, 0, sizeof(fact));
107 sact.sa_handler = safe_signal_handler;
108 fact.sa_handler = fatal_signal_handler;
109
110 if (sigaction(SIGHUP, &fact, NULL))
111 fprintf(stderr, "SIG: Unable to install SIGHUP\n");
112 if (sigaction(SIGABRT, &fact, NULL))
113 fprintf(stderr, "SIG: Unable to install SIGABRT\n");
114 if (sigaction(SIGINT, &fact, NULL))
115 fprintf(stderr, "SIG: Unable to install SIGINT\n");
116 if (sigaction(SIGBUS, &fact, NULL))
117 fprintf(stderr, "SIG: Unable to install SIGBUS\n");
118 if (sigaction(SIGFPE, &fact, NULL))
119 fprintf(stderr, "SIG: Unable to install SIGFPE\n");
120 if (sigaction(SIGILL, &fact, NULL))
121 fprintf(stderr, "SIG: Unable to install SIGILL\n");
122 if (sigaction(SIGQUIT, &fact, NULL))
123 fprintf(stderr, "SIG: Unable to install SIGQUIT\n");
124 if (sigaction(SIGSEGV, &fact, NULL))
125 fprintf(stderr, "SIG: Unable to install SIGSEGV\n");
126 if (sigaction(SIGTERM, &fact, NULL))
127 fprintf(stderr, "SIG: Unable to install SIGTERM\n");
128 if (sigaction(SIGXCPU, &fact, NULL))
129 fprintf(stderr, "SIG: Unable to install SIGXCPU\n");
130 if (sigaction(SIGXFSZ, &fact, NULL))
131 fprintf(stderr, "SIG: Unable to install SIGXFSZ\n");
132 if (sigaction(SIGVTALRM, &fact, NULL))
133 fprintf(stderr, "SIG: Unable to install SIGVTALRM\n");
134 if (sigaction(SIGTRAP, &fact, NULL))
135 fprintf(stderr, "SIG: Unable to install SIGTRAP\n");
136 }
137 #else
138 void install_signal_handlers() {
139 SetUnhandledExceptionFilter(RecordExceptionInfo);
140 }
141 #endif
142
143 // ---------------------------------------------------------------------------
144 // Define our operating system specific extensions to the gameos system
145 // ---------------------------------------------------------------------------
146 class oeD3LnxApp : public oeLnxApplication {
147 bool shutdown, final_shutdown;
148 int old_screen_mode;
149
150 public:
151 oeD3LnxApp(unsigned flags) : oeLnxApplication(flags) {
152 Descent = this;
153 shutdown = false;
154 final_shutdown = false;
155 }
156 virtual ~oeD3LnxApp() { final_shutdown = true; };
157
158 void run() { Descent3(); };
159 };
160
161 class oeD3LnxDatabase : public oeLnxAppDatabase {
162 public:
163 oeD3LnxDatabase() {
164 char path[_MAX_PATH];
165 char netpath[_MAX_PATH];
166
167 // put directories into database
168
169 #ifdef EDITOR
170 create_record("D3Edit");
171 #else
172 create_record("Descent3");
173 #endif
174
175 char *dir = getenv("D3_LOCAL");
176 char *netdir = getenv("D3_DIR");
177
178 if (!dir)
179 strcpy(path, Base_directory);
180 else
181 strcpy(path, dir);
182
183 if (!netdir)
184 strcpy(netpath, "");
185 else
186 strcpy(netpath, netdir);
187
188 write("local directory", path, strlen(path) + 1);
189 write("net directory", netpath, strlen(netpath) + 1);
190 Database = this;
191 }
192 };
193
194 int sdlKeyFilter(const SDL_Event *event);
195 int sdlMouseButtonUpFilter(const SDL_Event *event);
196 int sdlMouseButtonDownFilter(const SDL_Event *event);
197 int sdlMouseWheelFilter(const SDL_Event *event);
198 int sdlMouseMotionFilter(const SDL_Event *event);
199
200 int SDLCALL d3SDLEventFilter(void *userdata, SDL_Event *event) {
201 switch (event->type) {
202 case SDL_KEYUP:
203 case SDL_KEYDOWN:
204 return (sdlKeyFilter(event));
205 case SDL_JOYBALLMOTION:
206 case SDL_MOUSEMOTION:
207 return (sdlMouseMotionFilter(event));
208 case SDL_MOUSEBUTTONUP:
209 return (sdlMouseButtonUpFilter(event));
210 case SDL_MOUSEBUTTONDOWN:
211 return (sdlMouseButtonDownFilter(event));
212 case SDL_MOUSEWHEEL:
213 return (sdlMouseWheelFilter(event));
214 case SDL_QUIT:
215 SDL_Quit();
216 _exit(0);
217 break;
218 default:
219 break;
220 } // switch
221
222 return (1);
223 }
224
225 // ---------------------------------------------------------------------------
226 // Main
227 // creates all the OS objects and then runs Descent 3.
228 // this is all this function should do.
229 // ---------------------------------------------------------------------------
230 #ifdef WIN32
231 int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR szCmdLine, int nCmdShow) {
232 strupr(szCmdLine);
233 GatherArgs(szCmdLine);
234 #else
235 int main(int argc, char *argv[]) {
236 GatherArgs(argv);
237 #endif
238
239 orig_pwd = std::filesystem::current_path();
240
241 /* Setup the logging system */
242 InitLog();
243
244 setbuf(stdout, NULL);
245 setbuf(stderr, NULL);
246
247 #ifdef DEDICATED
248 setenv("SDL_VIDEODRIVER", "dummy", 1);
249 #endif
250
251 int rc = SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO);
252 if (rc != 0) {
253 fprintf(stderr, "SDL: SDL_Init() failed! rc == (%d).\n", rc);
254 fprintf(stderr, "SDL_GetError() reports \"%s\".\n", SDL_GetError());
255 return (0);
256 }
257
258 // !!! FIXME: Don't use an event filter!
259 SDL_SetEventFilter(d3SDLEventFilter, NULL);
260 install_signal_handlers();
261
262 int winArg = FindArgChar("-windowed", 'w');
263 int fsArg = FindArgChar("-fullscreen", 'f');
264
265 if ((fsArg) && (winArg)) {
266 fprintf(stderr, "ERROR: %s AND %s specified!\n", GameArgs[winArg], GameArgs[fsArg]);
267 return (0);
268 } // if
269
270 if (FindArg("-game_checksum")) {
271 extern tOSIRISModuleInit Osiris_module_init;
272 extern void Osiris_CreateModuleInitStruct(tOSIRISModuleInit * st);
273 extern uint32_t Osiris_CreateGameChecksum(void);
274
275 Osiris_CreateModuleInitStruct(&Osiris_module_init);
276 uint32_t checksum = Osiris_CreateGameChecksum();
277 printf("Descent 3\n");
278 printf("Game Checksum: %u\n", checksum);
279 return (0);
280 } else {
281 /*initialize our OS Object. This could be a game dependant OS object, or a default OS object.
282 once we create it, if successful, we can start the game.
283 */
284 int flags = 0;
285
286 if (!FindArgChar("-dedicated", 'd')) {
287 #ifndef DEDICATED
288 // check for a renderer
289
290 if ((FindArgChar("-windowed", 'w')) && (FindArgChar("-fullscreen", 'f'))) {
291 fprintf(stderr, "Error: Both windowed and fullscreen mode requested.");
292 return 0;
293 }
294
295 if (FindArgChar("-nomousegrab", 'm')) {
296 flags |= APPFLAG_NOMOUSECAPTURE;
297 }
298
299 ddio_mouseGrabbed = (FindArgChar("-nomousegrab", 'm') == 0);
300
301 if (!FindArg("-sharedmemory")) {
302 flags |= APPFLAG_NOSHAREDMEMORY;
303 }
304 flags |= APPFLAG_WINDOWEDMODE;
305
306 if (!FindArg("-nodgamouse")) {
307 flags |= APPFLAG_DGAMOUSE;
308 }
309 #else
310 fprintf(stderr, "Error: \"--dedicated\" or \"-d\" flag required\n");
311 return 0;
312 #endif
313
314 } else {
315 // Dedicated Server Mode
316 flags |= OEAPP_CONSOLE;
317
318 // service flag overrides others here in the group
319 if (FindArg("-service")) {
320 flags |= APPFLAG_USESERVICE;
321 }
322 }
323
324 bool run_d3 = true;
325 #if defined(POSIX)
326 if (flags & APPFLAG_USESERVICE) {
327 run_d3 = false;
328 pid_t np = fork();
329 if (np == -1) {
330 fprintf(stderr, "Unable to fork process\n");
331 }
332 if (np == 0) {
333 run_d3 = true;
334 np = setsid();
335 pid_t pp = getpid();
336 printf("Successfully forked process [new sid=%d pid=%d]\n", np, pp);
337 }
338 }
339 #endif
340
341 if (run_d3) {
342 oeD3LnxApp d3(flags);
343 oeD3LnxDatabase dbase;
344 StartDedicatedServer();
345 PreInitD3Systems();
346
347 d3.init();
348 d3.run();
349 }
350 }
351
352 just_exit();
353 return 0;
354 }