Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
352 lines (311 loc) · 8.72 KB

SDL.c

File metadata and controls

352 lines (311 loc) · 8.72 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library 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
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
27
/* Initialization code for SDL */
#include "SDL.h"
#include "SDL_fatal.h"
Feb 16, 2006
Feb 16, 2006
28
29
30
31
32
33
#if !SDL_VIDEO_DISABLED
#include "video/SDL_leaks.h"
#endif
#if SDL_THREAD_PTH
#include <pth.h>
Apr 26, 2001
Apr 26, 2001
34
35
36
#endif
/* Initialization/Cleanup routines */
Feb 16, 2006
Feb 16, 2006
37
#if !SDL_JOYSTICK_DISABLED
Jul 10, 2006
Jul 10, 2006
38
extern int SDL_JoystickInit(void);
Apr 26, 2001
Apr 26, 2001
39
40
extern void SDL_JoystickQuit(void);
#endif
Feb 16, 2006
Feb 16, 2006
41
#if !SDL_CDROM_DISABLED
Jul 10, 2006
Jul 10, 2006
42
extern int SDL_CDROMInit(void);
Apr 26, 2001
Apr 26, 2001
43
44
extern void SDL_CDROMQuit(void);
#endif
Feb 16, 2006
Feb 16, 2006
45
#if !SDL_TIMERS_DISABLED
Apr 26, 2001
Apr 26, 2001
46
extern void SDL_StartTicks(void);
Jul 10, 2006
Jul 10, 2006
47
extern int SDL_TimerInit(void);
Apr 26, 2001
Apr 26, 2001
48
49
50
51
52
53
54
55
56
57
58
extern void SDL_TimerQuit(void);
#endif
/* The initialized subsystems */
static Uint32 SDL_initialized = 0;
static Uint32 ticks_started = 0;
#ifdef CHECK_LEAKS
int surfaces_allocated = 0;
#endif
Jul 10, 2006
Jul 10, 2006
59
60
int
SDL_InitSubSystem(Uint32 flags)
Apr 26, 2001
Apr 26, 2001
61
{
Feb 16, 2006
Feb 16, 2006
62
#if !SDL_VIDEO_DISABLED
Jul 10, 2006
Jul 10, 2006
63
64
/* Initialize the video/event subsystem */
if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
Jul 15, 2006
Jul 15, 2006
65
if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
Jul 10, 2006
Jul 10, 2006
66
67
68
69
return (-1);
}
SDL_initialized |= SDL_INIT_VIDEO;
}
Apr 26, 2001
Apr 26, 2001
70
#else
Jul 10, 2006
Jul 10, 2006
71
72
73
74
if (flags & SDL_INIT_VIDEO) {
SDL_SetError("SDL not built with video support");
return (-1);
}
Apr 26, 2001
Apr 26, 2001
75
76
#endif
Feb 16, 2006
Feb 16, 2006
77
#if !SDL_AUDIO_DISABLED
Jul 10, 2006
Jul 10, 2006
78
79
/* Initialize the audio subsystem */
if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
Jul 15, 2006
Jul 15, 2006
80
if (SDL_AudioInit(NULL) < 0) {
Jul 10, 2006
Jul 10, 2006
81
82
83
84
return (-1);
}
SDL_initialized |= SDL_INIT_AUDIO;
}
Apr 26, 2001
Apr 26, 2001
85
#else
Jul 10, 2006
Jul 10, 2006
86
87
88
89
if (flags & SDL_INIT_AUDIO) {
SDL_SetError("SDL not built with audio support");
return (-1);
}
Apr 26, 2001
Apr 26, 2001
90
91
#endif
Feb 16, 2006
Feb 16, 2006
92
#if !SDL_TIMERS_DISABLED
Jul 10, 2006
Jul 10, 2006
93
94
95
96
97
98
99
100
101
102
103
/* Initialize the timer subsystem */
if (!ticks_started) {
SDL_StartTicks();
ticks_started = 1;
}
if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
if (SDL_TimerInit() < 0) {
return (-1);
}
SDL_initialized |= SDL_INIT_TIMER;
}
Apr 26, 2001
Apr 26, 2001
104
#else
Jul 10, 2006
Jul 10, 2006
105
106
107
108
if (flags & SDL_INIT_TIMER) {
SDL_SetError("SDL not built with timer support");
return (-1);
}
Apr 26, 2001
Apr 26, 2001
109
110
#endif
Feb 16, 2006
Feb 16, 2006
111
#if !SDL_JOYSTICK_DISABLED
Jul 10, 2006
Jul 10, 2006
112
113
114
115
116
117
118
/* Initialize the joystick subsystem */
if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
if (SDL_JoystickInit() < 0) {
return (-1);
}
SDL_initialized |= SDL_INIT_JOYSTICK;
}
Apr 26, 2001
Apr 26, 2001
119
#else
Jul 10, 2006
Jul 10, 2006
120
121
122
123
if (flags & SDL_INIT_JOYSTICK) {
SDL_SetError("SDL not built with joystick support");
return (-1);
}
Apr 26, 2001
Apr 26, 2001
124
125
#endif
Feb 16, 2006
Feb 16, 2006
126
#if !SDL_CDROM_DISABLED
Jul 10, 2006
Jul 10, 2006
127
128
129
130
131
132
133
/* Initialize the CD-ROM subsystem */
if ((flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM)) {
if (SDL_CDROMInit() < 0) {
return (-1);
}
SDL_initialized |= SDL_INIT_CDROM;
}
Apr 26, 2001
Apr 26, 2001
134
#else
Jul 10, 2006
Jul 10, 2006
135
136
137
138
if (flags & SDL_INIT_CDROM) {
SDL_SetError("SDL not built with cdrom support");
return (-1);
}
Apr 26, 2001
Apr 26, 2001
139
#endif
Jul 10, 2006
Jul 10, 2006
140
return (0);
Apr 26, 2001
Apr 26, 2001
141
142
}
Jul 10, 2006
Jul 10, 2006
143
144
int
SDL_Init(Uint32 flags)
Apr 26, 2001
Apr 26, 2001
145
{
Feb 16, 2006
Feb 16, 2006
146
#if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
Jul 10, 2006
Jul 10, 2006
147
148
149
if (!pth_init()) {
return -1;
}
Jun 10, 2002
Jun 10, 2002
150
151
#endif
Jul 10, 2006
Jul 10, 2006
152
153
/* Clear the error message */
SDL_ClearError();
Apr 26, 2001
Apr 26, 2001
154
Jul 10, 2006
Jul 10, 2006
155
156
157
158
/* Initialize the desired subsystems */
if (SDL_InitSubSystem(flags) < 0) {
return (-1);
}
Apr 26, 2001
Apr 26, 2001
159
Jul 10, 2006
Jul 10, 2006
160
161
162
163
164
/* Everything is initialized */
if (!(flags & SDL_INIT_NOPARACHUTE)) {
SDL_InstallParachute();
}
return (0);
Apr 26, 2001
Apr 26, 2001
165
166
}
Jul 10, 2006
Jul 10, 2006
167
168
void
SDL_QuitSubSystem(Uint32 flags)
Apr 26, 2001
Apr 26, 2001
169
{
Jul 10, 2006
Jul 10, 2006
170
/* Shut down requested initialized subsystems */
Feb 16, 2006
Feb 16, 2006
171
#if !SDL_CDROM_DISABLED
Jul 10, 2006
Jul 10, 2006
172
173
174
175
if ((flags & SDL_initialized & SDL_INIT_CDROM)) {
SDL_CDROMQuit();
SDL_initialized &= ~SDL_INIT_CDROM;
}
Apr 26, 2001
Apr 26, 2001
176
#endif
Feb 16, 2006
Feb 16, 2006
177
#if !SDL_JOYSTICK_DISABLED
Jul 10, 2006
Jul 10, 2006
178
179
180
181
if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
SDL_JoystickQuit();
SDL_initialized &= ~SDL_INIT_JOYSTICK;
}
Apr 26, 2001
Apr 26, 2001
182
#endif
Feb 16, 2006
Feb 16, 2006
183
#if !SDL_TIMERS_DISABLED
Jul 10, 2006
Jul 10, 2006
184
185
186
187
if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
SDL_TimerQuit();
SDL_initialized &= ~SDL_INIT_TIMER;
}
Apr 26, 2001
Apr 26, 2001
188
#endif
Feb 16, 2006
Feb 16, 2006
189
#if !SDL_AUDIO_DISABLED
Jul 10, 2006
Jul 10, 2006
190
191
192
193
if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
SDL_AudioQuit();
SDL_initialized &= ~SDL_INIT_AUDIO;
}
Apr 26, 2001
Apr 26, 2001
194
#endif
Feb 16, 2006
Feb 16, 2006
195
#if !SDL_VIDEO_DISABLED
Jul 10, 2006
Jul 10, 2006
196
197
198
199
if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
SDL_VideoQuit();
SDL_initialized &= ~SDL_INIT_VIDEO;
}
Apr 26, 2001
Apr 26, 2001
200
201
202
#endif
}
Jul 10, 2006
Jul 10, 2006
203
204
Uint32
SDL_WasInit(Uint32 flags)
Apr 26, 2001
Apr 26, 2001
205
{
Jul 10, 2006
Jul 10, 2006
206
207
208
209
if (!flags) {
flags = SDL_INIT_EVERYTHING;
}
return (SDL_initialized & flags);
Apr 26, 2001
Apr 26, 2001
210
211
}
Jul 10, 2006
Jul 10, 2006
212
213
void
SDL_Quit(void)
Apr 26, 2001
Apr 26, 2001
214
{
Jul 10, 2006
Jul 10, 2006
215
/* Quit all subsystems */
Nov 23, 2005
Nov 23, 2005
216
#ifdef DEBUG_BUILD
Jul 10, 2006
Jul 10, 2006
217
218
printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
fflush(stdout);
Nov 23, 2005
Nov 23, 2005
219
#endif
Jul 10, 2006
Jul 10, 2006
220
SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
Apr 26, 2001
Apr 26, 2001
221
222
#ifdef CHECK_LEAKS
Nov 23, 2005
Nov 23, 2005
223
#ifdef DEBUG_BUILD
Jul 10, 2006
Jul 10, 2006
224
225
printf("[SDL_Quit] : CHECK_LEAKS\n");
fflush(stdout);
Nov 23, 2005
Nov 23, 2005
226
227
#endif
Jul 10, 2006
Jul 10, 2006
228
229
230
231
232
/* Print the number of surfaces not freed */
if (surfaces_allocated != 0) {
fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
surfaces_allocated);
}
Apr 26, 2001
Apr 26, 2001
233
#endif
Nov 23, 2005
Nov 23, 2005
234
#ifdef DEBUG_BUILD
Jul 10, 2006
Jul 10, 2006
235
236
printf("[SDL_Quit] : SDL_UninstallParachute()\n");
fflush(stdout);
Nov 23, 2005
Nov 23, 2005
237
#endif
Apr 26, 2001
Apr 26, 2001
238
Jul 10, 2006
Jul 10, 2006
239
240
/* Uninstall any parachute signal handlers */
SDL_UninstallParachute();
Jun 10, 2002
Jun 10, 2002
241
Feb 16, 2006
Feb 16, 2006
242
#if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
Jul 10, 2006
Jul 10, 2006
243
pth_kill();
Jun 10, 2002
Jun 10, 2002
244
#endif
Nov 23, 2005
Nov 23, 2005
245
#ifdef DEBUG_BUILD
Jul 10, 2006
Jul 10, 2006
246
247
printf("[SDL_Quit] : Returning!\n");
fflush(stdout);
Nov 23, 2005
Nov 23, 2005
248
249
#endif
Apr 26, 2001
Apr 26, 2001
250
251
}
Jul 10, 2006
Jul 10, 2006
252
253
254
/* Get the library version number */
void
SDL_GetVersion(SDL_version * ver)
Apr 26, 2001
Apr 26, 2001
255
{
Jul 10, 2006
Jul 10, 2006
256
SDL_VERSION(ver);
Apr 26, 2001
Apr 26, 2001
257
258
}
Feb 6, 2006
Feb 6, 2006
259
#if defined(__OS2__)
Mar 9, 2006
Mar 9, 2006
260
/* Building for OS/2 */
Nov 23, 2005
Nov 23, 2005
261
262
263
264
265
266
#ifdef __WATCOMC__
#define INCL_DOSERRORS
#define INCL_DOSEXCEPTIONS
#include <os2.h>
Mar 9, 2006
Mar 9, 2006
267
/* Exception handler to prevent the Audio thread hanging, making a zombie process! */
Jul 10, 2006
Jul 10, 2006
268
269
270
271
ULONG _System
SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
PEXCEPTIONREGISTRATIONRECORD pERegRec,
PCONTEXTRECORD pCtxRec, PVOID p)
Nov 23, 2005
Nov 23, 2005
272
{
Jul 10, 2006
Jul 10, 2006
273
274
275
276
277
278
279
280
281
282
283
284
285
if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
return XCPT_CONTINUE_SEARCH;
if (pERepRec->fHandlerFlags & EH_UNWINDING)
return XCPT_CONTINUE_SEARCH;
if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
return XCPT_CONTINUE_SEARCH;
/* Do cleanup at every fatal exception! */
if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) ==
XCPT_FATAL_EXCEPTION) && (pERepRec->ExceptionNum != XCPT_BREAKPOINT)
&& (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)) {
if (SDL_initialized & SDL_INIT_AUDIO) {
/* This removes the zombie audio thread in case of emergency. */
Nov 23, 2005
Nov 23, 2005
286
#ifdef DEBUG_BUILD
Jul 10, 2006
Jul 10, 2006
287
288
printf
("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
Nov 23, 2005
Nov 23, 2005
289
#endif
Jul 10, 2006
Jul 10, 2006
290
291
SDL_CloseAudio();
}
Nov 23, 2005
Nov 23, 2005
292
}
Jul 10, 2006
Jul 10, 2006
293
return (XCPT_CONTINUE_SEARCH);
Nov 23, 2005
Nov 23, 2005
294
295
296
}
Jul 10, 2006
Jul 10, 2006
297
298
EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand =
{ 0, SDL_Main_ExceptionHandler };
Nov 23, 2005
Nov 23, 2005
299
Mar 9, 2006
Mar 9, 2006
300
/* The main DLL entry for DLL Initialization and Uninitialization: */
Jul 10, 2006
Jul 10, 2006
301
302
unsigned _System
LibMain(unsigned hmod, unsigned termination)
Nov 23, 2005
Nov 23, 2005
303
{
Jul 10, 2006
Jul 10, 2006
304
if (termination) {
Nov 23, 2005
Nov 23, 2005
305
#ifdef DEBUG_BUILD
Mar 9, 2006
Mar 9, 2006
306
/* printf("[SDL DLL Unintialization] : Removing exception handler\n"); */
Nov 23, 2005
Nov 23, 2005
307
#endif
Jul 10, 2006
Jul 10, 2006
308
309
310
DosUnsetExceptionHandler(&SDL_Main_xcpthand);
return 1;
} else {
Nov 23, 2005
Nov 23, 2005
311
#ifdef DEBUG_BUILD
Jul 10, 2006
Jul 10, 2006
312
313
314
/* Make stdout and stderr unbuffered! */
setbuf(stdout, NULL);
setbuf(stderr, NULL);
Nov 23, 2005
Nov 23, 2005
315
#endif
Jul 10, 2006
Jul 10, 2006
316
/* Fire up exception handler */
Nov 23, 2005
Nov 23, 2005
317
#ifdef DEBUG_BUILD
Mar 9, 2006
Mar 9, 2006
318
/* printf("[SDL DLL Initialization] : Setting exception handler\n"); */
Nov 23, 2005
Nov 23, 2005
319
#endif
Jul 10, 2006
Jul 10, 2006
320
321
/* Set exception handler */
DosSetExceptionHandler(&SDL_Main_xcpthand);
Nov 23, 2005
Nov 23, 2005
322
Jul 10, 2006
Jul 10, 2006
323
324
return 1;
}
Nov 23, 2005
Nov 23, 2005
325
}
Feb 6, 2006
Feb 6, 2006
326
#endif /* __WATCOMC__ */
Nov 23, 2005
Nov 23, 2005
327
Feb 21, 2006
Feb 21, 2006
328
#elif defined(__WIN32__)
Feb 6, 2006
Feb 6, 2006
329
Mar 4, 2006
Mar 4, 2006
330
331
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
/* Need to include DllMain() on Watcom C for some reason.. */
Feb 25, 2006
Feb 25, 2006
332
333
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
Feb 6, 2006
Feb 6, 2006
334
Jul 10, 2006
Jul 10, 2006
335
336
337
BOOL APIENTRY
_DllMainCRTStartup(HANDLE hModule,
DWORD ul_reason_for_call, LPVOID lpReserved)
Feb 6, 2006
Feb 6, 2006
338
{
Jul 10, 2006
Jul 10, 2006
339
340
341
342
343
344
345
346
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
Feb 6, 2006
Feb 6, 2006
347
}
Mar 4, 2006
Mar 4, 2006
348
#endif /* building DLL with Watcom C */
Feb 6, 2006
Feb 6, 2006
349
Feb 21, 2006
Feb 21, 2006
350
#endif /* OS/2 elif __WIN32__ */
Jul 10, 2006
Jul 10, 2006
351
352
/* vi: set ts=4 sw=4 expandtab: */