2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Initialization code for SDL */
27 #include "SDL_fatal.h"
28 #if !SDL_VIDEO_DISABLED
29 #include "video/SDL_leaks.h"
36 /* Initialization/Cleanup routines */
37 #if !SDL_JOYSTICK_DISABLED
38 extern int SDL_JoystickInit(void);
39 extern void SDL_JoystickQuit(void);
41 #if !SDL_CDROM_DISABLED
42 extern int SDL_CDROMInit(void);
43 extern void SDL_CDROMQuit(void);
45 #if !SDL_TIMERS_DISABLED
46 extern void SDL_StartTicks(void);
47 extern int SDL_TimerInit(void);
48 extern void SDL_TimerQuit(void);
51 /* The current SDL version */
52 static SDL_version version =
53 { SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };
55 /* The initialized subsystems */
56 static Uint32 SDL_initialized = 0;
57 #if !SDL_TIMERS_DISABLED
58 static Uint32 ticks_started = 0;
62 int surfaces_allocated = 0;
65 int SDL_InitSubSystem(Uint32 flags)
67 #if !SDL_VIDEO_DISABLED
68 /* Initialize the video/event subsystem */
69 if ( (flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO) ) {
70 if ( SDL_VideoInit(SDL_getenv("SDL_VIDEODRIVER"),
71 (flags&SDL_INIT_EVENTTHREAD)) < 0 ) {
74 SDL_initialized |= SDL_INIT_VIDEO;
77 if ( flags & SDL_INIT_VIDEO ) {
78 SDL_SetError("SDL not built with video support");
83 #if !SDL_AUDIO_DISABLED
84 /* Initialize the audio subsystem */
85 if ( (flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO) ) {
86 if ( SDL_AudioInit(SDL_getenv("SDL_AUDIODRIVER")) < 0 ) {
89 SDL_initialized |= SDL_INIT_AUDIO;
92 if ( flags & SDL_INIT_AUDIO ) {
93 SDL_SetError("SDL not built with audio support");
98 #if !SDL_TIMERS_DISABLED
99 /* Initialize the timer subsystem */
100 if ( ! ticks_started ) {
104 if ( (flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER) ) {
105 if ( SDL_TimerInit() < 0 ) {
108 SDL_initialized |= SDL_INIT_TIMER;
111 if ( flags & SDL_INIT_TIMER ) {
112 SDL_SetError("SDL not built with timer support");
117 #if !SDL_JOYSTICK_DISABLED
118 /* Initialize the joystick subsystem */
119 if ( (flags & SDL_INIT_JOYSTICK) &&
120 !(SDL_initialized & SDL_INIT_JOYSTICK) ) {
121 if ( SDL_JoystickInit() < 0 ) {
124 SDL_initialized |= SDL_INIT_JOYSTICK;
127 if ( flags & SDL_INIT_JOYSTICK ) {
128 SDL_SetError("SDL not built with joystick support");
133 #if !SDL_CDROM_DISABLED
134 /* Initialize the CD-ROM subsystem */
135 if ( (flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM) ) {
136 if ( SDL_CDROMInit() < 0 ) {
139 SDL_initialized |= SDL_INIT_CDROM;
142 if ( flags & SDL_INIT_CDROM ) {
143 SDL_SetError("SDL not built with cdrom support");
150 int SDL_Init(Uint32 flags)
152 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
158 /* Clear the error message */
161 /* Initialize the desired subsystems */
162 if ( SDL_InitSubSystem(flags) < 0 ) {
166 /* Everything is initialized */
167 if ( !(flags & SDL_INIT_NOPARACHUTE) ) {
168 SDL_InstallParachute();
173 void SDL_QuitSubSystem(Uint32 flags)
175 /* Shut down requested initialized subsystems */
176 #if !SDL_CDROM_DISABLED
177 if ( (flags & SDL_initialized & SDL_INIT_CDROM) ) {
179 SDL_initialized &= ~SDL_INIT_CDROM;
182 #if !SDL_JOYSTICK_DISABLED
183 if ( (flags & SDL_initialized & SDL_INIT_JOYSTICK) ) {
185 SDL_initialized &= ~SDL_INIT_JOYSTICK;
188 #if !SDL_TIMERS_DISABLED
189 if ( (flags & SDL_initialized & SDL_INIT_TIMER) ) {
191 SDL_initialized &= ~SDL_INIT_TIMER;
194 #if !SDL_AUDIO_DISABLED
195 if ( (flags & SDL_initialized & SDL_INIT_AUDIO) ) {
197 SDL_initialized &= ~SDL_INIT_AUDIO;
200 #if !SDL_VIDEO_DISABLED
201 if ( (flags & SDL_initialized & SDL_INIT_VIDEO) ) {
203 SDL_initialized &= ~SDL_INIT_VIDEO;
208 Uint32 SDL_WasInit(Uint32 flags)
211 flags = SDL_INIT_EVERYTHING;
213 return (SDL_initialized&flags);
218 /* Quit all subsystems */
220 printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n"); fflush(stdout);
222 SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
226 printf("[SDL_Quit] : CHECK_LEAKS\n"); fflush(stdout);
229 /* Print the number of surfaces not freed */
230 if ( surfaces_allocated != 0 ) {
231 fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
236 printf("[SDL_Quit] : SDL_UninstallParachute()\n"); fflush(stdout);
239 /* Uninstall any parachute signal handlers */
240 SDL_UninstallParachute();
242 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
246 printf("[SDL_Quit] : Returning!\n"); fflush(stdout);
251 /* Return the library version number */
252 const SDL_version * SDL_Linked_Version(void)
258 /* Building for OS/2 */
261 #define INCL_DOSERRORS
262 #define INCL_DOSEXCEPTIONS
265 /* Exception handler to prevent the Audio thread hanging, making a zombie process! */
266 ULONG _System SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
267 PEXCEPTIONREGISTRATIONRECORD pERegRec,
268 PCONTEXTRECORD pCtxRec,
271 if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
272 return XCPT_CONTINUE_SEARCH;
273 if (pERepRec->fHandlerFlags & EH_UNWINDING)
274 return XCPT_CONTINUE_SEARCH;
275 if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
276 return XCPT_CONTINUE_SEARCH;
278 /* Do cleanup at every fatal exception! */
279 if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) == XCPT_FATAL_EXCEPTION) &&
280 (pERepRec->ExceptionNum != XCPT_BREAKPOINT) &&
281 (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)
284 if (SDL_initialized & SDL_INIT_AUDIO)
286 /* This removes the zombie audio thread in case of emergency. */
288 printf("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
293 return (XCPT_CONTINUE_SEARCH);
297 EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand = {0, SDL_Main_ExceptionHandler};
299 /* The main DLL entry for DLL Initialization and Uninitialization: */
300 unsigned _System LibMain(unsigned hmod, unsigned termination)
305 /* printf("[SDL DLL Unintialization] : Removing exception handler\n"); */
307 DosUnsetExceptionHandler(&SDL_Main_xcpthand);
312 /* Make stdout and stderr unbuffered! */
313 setbuf(stdout, NULL);
314 setbuf(stderr, NULL);
316 /* Fire up exception handler */
318 /* printf("[SDL DLL Initialization] : Setting exception handler\n"); */
320 /* Set exception handler */
321 DosSetExceptionHandler(&SDL_Main_xcpthand);
326 #endif /* __WATCOMC__ */
328 #elif defined(__WIN32__) && !defined(__SYMBIAN32__)
330 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
331 /* Need to include DllMain() on Watcom C for some reason.. */
332 #define WIN32_LEAN_AND_MEAN
335 BOOL APIENTRY _DllMainCRTStartup( HANDLE hModule,
336 DWORD ul_reason_for_call,
339 switch (ul_reason_for_call) {
340 case DLL_PROCESS_ATTACH:
341 case DLL_THREAD_ATTACH:
342 case DLL_THREAD_DETACH:
343 case DLL_PROCESS_DETACH:
348 #endif /* building DLL with Watcom C */
350 #endif /* OS/2 elif __WIN32__ */