/
sys_system.c
414 lines (349 loc) · 9.45 KB
/
sys_system.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/**\file sys_system.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2011 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* Abstract interfaces to platform-level services.
*/
// HEADER FILES ------------------------------------------------------------
#ifdef WIN32
# include <windows.h>
# include <process.h>
#endif
#include <signal.h>
#include <SDL.h>
#include <SDL_thread.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_network.h"
#include "de_refresh.h"
#include "de_graphics.h"
#include "de_audio.h"
#include "de_misc.h"
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
//int systics = 0; // System tics (every game tic).
int novideo; // if true, stay in text mode for debugging
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// CODE --------------------------------------------------------------------
#ifdef WIN32
/**
* Borrowed from Lee Killough.
*/
static void C_DECL handler(int s)
{
signal(s, SIG_IGN); // Ignore future instances of this signal.
Con_Error(s==SIGSEGV ? "Segmentation Violation\n" :
s==SIGINT ? "Interrupted by User\n" :
s==SIGILL ? "Illegal Instruction\n" :
s==SIGFPE ? "Floating Point Exception\n" :
s==SIGTERM ? "Killed\n" : "Terminated by signal\n");
}
#endif
/**
* Initialize platform level services.
*
* \note This must be called from the main thread due to issues with the devices
* we use via the WINAPI, MCI (cdaudio, mixer etc) on the WIN32 platform.
*/
void Sys_Init(void)
{
uint startTime = (verbose >= 2? Sys_GetRealTime() : 0);
Con_Message("Setting up platform state...\n");
VERBOSE( Con_Message("Initializing Timing subsystem...\n") )
Sys_InitTimer();
if(!isDedicated)
{
VERBOSE( Con_Message("Initializing Input subsystem...\n") )
if(!I_Init())
Con_Error("Failed to initialize Input subsystem.\n");
}
// Virtual devices need to be created even in dedicated mode.
I_InitVirtualInputDevices();
VERBOSE( Con_Message("Initializing Audio subsystem...\n") )
S_Init();
VERBOSE( Con_Message("Initializing Network subsystem...\n") )
Huff_Init();
N_Init();
#if defined(WIN32) && !defined(_DEBUG)
// Register handler for abnormal situations (in release build).
signal(SIGSEGV, handler);
signal(SIGTERM, handler);
signal(SIGILL, handler);
signal(SIGFPE, handler);
signal(SIGILL, handler);
signal(SIGABRT, handler);
#endif
#ifndef WIN32
// We are not worried about broken pipes. When a TCP connection closes,
// we prefer to receive an error code instead of a signal.
signal(SIGPIPE, SIG_IGN);
#endif
VERBOSE2( Con_Message("Sys_Init: Done in %.2f seconds.\n", (Sys_GetRealTime() - startTime) / 1000.0f) );
}
/**
* Return to default system state.
*/
void Sys_Shutdown(void)
{
// Time to unload *everything*.
if(!DD_IsNullGameInfo(DD_GameInfo()))
Con_Execute(CMDS_DDAY, "unload", true, false);
B_Shutdown();
Sys_ShutdownTimer();
Net_Shutdown();
Huff_Shutdown();
// Let's shut down sound first, so Windows' HD-hogging doesn't jam
// the MUS player (would produce horrible bursts of notes).
S_Shutdown();
GL_Shutdown();
DD_ClearEvents();
I_ShutdownInputDevices();
I_Shutdown();
DD_DestroyGameInfo();
}
static int showCriticalMessage(const char* msg)
{
#ifdef WIN32
char buf[256];
int ret;
HWND hWnd = Sys_GetWindowHandle(windowIDX);
if(!hWnd)
{
suspendMsgPump = true;
MessageBox(HWND_DESKTOP, ("Sys_CriticalMessage: Main window not available."), NULL, MB_ICONERROR | MB_OK);
suspendMsgPump = false;
return false;
}
ShowCursor(TRUE);
ShowCursor(TRUE);
suspendMsgPump = true;
GetWindowText(hWnd, buf, 255);
ret = (MessageBox(hWnd, (msg), (buf), MB_OK | MB_ICONEXCLAMATION) == IDYES);
suspendMsgPump = false;
ShowCursor(FALSE);
ShowCursor(FALSE);
return ret;
#else
fprintf(stderr, "--- %s\n", msg);
return 0;
#endif
}
int Sys_CriticalMessage(const char* msg)
{
return showCriticalMessage(msg);
}
int Sys_CriticalMessagef(const char* format, ...)
{
static const char* unknownMsg = "Unknown critical issue occured.";
const size_t BUF_SIZE = 655365;
const char* msg;
char* buf = 0;
va_list args;
int result;
if(format && format[0])
{
va_start(args, format);
buf = (char*) calloc(1, BUF_SIZE);
dd_vsnprintf(buf, BUF_SIZE, format, args);
msg = buf;
va_end(args);
}
else
{
msg = unknownMsg;
}
result = showCriticalMessage(msg);
if(buf) free(buf);
return result;
}
void Sys_Sleep(int millisecs)
{
#ifdef WIN32
Sleep(millisecs);
#endif
#ifdef UNIX
// Not guaranteed to be very accurate...
SDL_Delay(millisecs);
#endif
}
void Sys_ShowCursor(boolean show)
{
#ifdef WIN32
ShowCursor(show);
#endif
#ifdef UNIX
SDL_ShowCursor(show ? SDL_ENABLE : SDL_DISABLE);
#endif
}
void Sys_HideMouse(void)
{
#ifdef WIN32
if(novideo)
return;
ShowCursor(FALSE);
#endif
#ifdef UNIX
Sys_ShowCursor(false);
#endif
}
/**
* Called when Doomsday should quit (will be deferred until convienent).
*/
void Sys_Quit(void)
{
appShutdown = true;
}
void Sys_MessageBox(const char *msg, boolean iserror)
{
#ifdef WIN32
char title[300];
HWND hWnd = Sys_GetWindowHandle(windowIDX);
if(!hWnd)
{
suspendMsgPump = true;
MessageBox(HWND_DESKTOP,
"Sys_MessageBox: Main window not available.", NULL,
MB_ICONERROR | MB_OK);
suspendMsgPump = false;
return;
}
suspendMsgPump = true;
GetWindowText(hWnd, title, 300);
MessageBox(hWnd, msg, title,
MB_OK | (iserror ? MB_ICONERROR : MB_ICONINFORMATION));
suspendMsgPump = false;
#endif
#ifdef UNIX
fprintf(stderr, "%s %s\n", iserror ? "**ERROR**" : "---", msg);
#endif
}
/**
* Opens the given file in a suitable text editor.
*/
void Sys_OpenTextEditor(const char *filename)
{
#ifdef WIN32
// Everybody is bound to have Notepad.
spawnlp(P_NOWAIT, "notepad.exe", "notepad.exe", filename, 0);
#endif
}
/**
* Utilises SDL Threads on ALL systems.
* Returns the SDL_thread structure as handle to the started thread.
*/
thread_t Sys_StartThread(systhreadfunc_t startpos, void *parm)
{
SDL_Thread* thread = SDL_CreateThread(startpos, parm);
if(thread == NULL)
{
Con_Message("Sys_StartThread: Failed to start new thread (%s).\n",
SDL_GetError());
return 0;
}
return thread;
}
/**
* Suspends or resumes the execution of a thread.
*/
void Sys_SuspendThread(thread_t handle, boolean dopause)
{
Con_Error("Sys_SuspendThread: Not implemented.\n");
}
/**
* @return The return value of the thread.
*/
int Sys_WaitThread(thread_t thread)
{
int result = 0;
SDL_WaitThread(thread, &result);
return result;
}
/**
* @return The identifier of the current thread.
*/
uint Sys_ThreadID(void)
{
return SDL_ThreadID();
}
mutex_t Sys_CreateMutex(const char *name)
{
return (mutex_t) SDL_CreateMutex();
}
void Sys_DestroyMutex(mutex_t handle)
{
if(!handle)
return;
SDL_DestroyMutex((SDL_mutex *) handle);
}
void Sys_Lock(mutex_t handle)
{
if(!handle)
return;
SDL_mutexP((SDL_mutex *) handle);
}
void Sys_Unlock(mutex_t handle)
{
if(!handle)
return;
SDL_mutexV((SDL_mutex *) handle);
}
sem_t Sem_Create(uint32_t initialValue)
{
return (sem_t) SDL_CreateSemaphore(initialValue);
}
void Sem_Destroy(sem_t semaphore)
{
if(semaphore)
{
SDL_DestroySemaphore((SDL_sem *) semaphore);
}
}
void Sem_P(sem_t semaphore)
{
if(semaphore)
{
SDL_SemWait((SDL_sem *) semaphore);
}
}
void Sem_V(sem_t semaphore)
{
if(semaphore)
{
SDL_SemPost((SDL_sem *) semaphore);
}
}
uint32_t Sem_Value(sem_t semaphore)
{
if(semaphore)
{
return (uint32_t)SDL_SemValue((SDL_sem*)semaphore);
}
return 0;
}