src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1190 173c063d4f55
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     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.
     9 
    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.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* Initialization code for SDL */
    24 
    25 #include <stdlib.h>		/* For getenv() */
    26 #ifdef ENABLE_PTH
    27 #include <pth.h>
    28 #endif
    29 
    30 #include "SDL.h"
    31 #include "SDL_endian.h"
    32 #include "SDL_fatal.h"
    33 #ifndef DISABLE_VIDEO
    34 #include "SDL_leaks.h"
    35 #endif
    36 
    37 /* Initialization/Cleanup routines */
    38 #ifndef DISABLE_JOYSTICK
    39 extern int  SDL_JoystickInit(void);
    40 extern void SDL_JoystickQuit(void);
    41 #endif
    42 #ifndef DISABLE_CDROM
    43 extern int  SDL_CDROMInit(void);
    44 extern void SDL_CDROMQuit(void);
    45 #endif
    46 #ifndef DISABLE_TIMERS
    47 extern void SDL_StartTicks(void);
    48 extern int  SDL_TimerInit(void);
    49 extern void SDL_TimerQuit(void);
    50 #endif
    51 
    52 /* The current SDL version */
    53 static SDL_version version = 
    54 	{ SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };
    55 
    56 /* The initialized subsystems */
    57 static Uint32 SDL_initialized = 0;
    58 static Uint32 ticks_started = 0;
    59 
    60 #ifdef CHECK_LEAKS
    61 int surfaces_allocated = 0;
    62 #endif
    63 
    64 int SDL_InitSubSystem(Uint32 flags)
    65 {
    66 #ifndef DISABLE_VIDEO
    67 	/* Initialize the video/event subsystem */
    68 	if ( (flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO) ) {
    69 		if ( SDL_VideoInit(getenv("SDL_VIDEODRIVER"),
    70 		                   (flags&SDL_INIT_EVENTTHREAD)) < 0 ) {
    71 			return(-1);
    72 		}
    73 		SDL_initialized |= SDL_INIT_VIDEO;
    74 	}
    75 #else
    76 	if ( flags & SDL_INIT_VIDEO ) {
    77 		SDL_SetError("SDL not built with video support");
    78 		return(-1);
    79 	}
    80 #endif
    81 
    82 #ifndef DISABLE_AUDIO
    83 	/* Initialize the audio subsystem */
    84 	if ( (flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO) ) {
    85 		if ( SDL_AudioInit(getenv("SDL_AUDIODRIVER")) < 0 ) {
    86 			return(-1);
    87 		}
    88 		SDL_initialized |= SDL_INIT_AUDIO;
    89 	}
    90 #else
    91 	if ( flags & SDL_INIT_AUDIO ) {
    92 		SDL_SetError("SDL not built with audio support");
    93 		return(-1);
    94 	}
    95 #endif
    96 
    97 #ifndef DISABLE_TIMERS
    98 	/* Initialize the timer subsystem */
    99 	if ( ! ticks_started ) {
   100 		SDL_StartTicks();
   101 		ticks_started = 1;
   102 	}
   103 	if ( (flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER) ) {
   104 		if ( SDL_TimerInit() < 0 ) {
   105 			return(-1);
   106 		}
   107 		SDL_initialized |= SDL_INIT_TIMER;
   108 	}
   109 #else
   110 	if ( flags & SDL_INIT_TIMER ) {
   111 		SDL_SetError("SDL not built with timer support");
   112 		return(-1);
   113 	}
   114 #endif
   115 
   116 #ifndef DISABLE_JOYSTICK
   117 	/* Initialize the joystick subsystem */
   118 	if ( (flags & SDL_INIT_JOYSTICK) &&
   119 	     !(SDL_initialized & SDL_INIT_JOYSTICK) ) {
   120 		if ( SDL_JoystickInit() < 0 ) {
   121 			return(-1);
   122 		}
   123 		SDL_initialized |= SDL_INIT_JOYSTICK;
   124 	}
   125 #else
   126 	if ( flags & SDL_INIT_JOYSTICK ) {
   127 		SDL_SetError("SDL not built with joystick support");
   128 		return(-1);
   129 	}
   130 #endif
   131 
   132 #ifndef DISABLE_CDROM
   133 	/* Initialize the CD-ROM subsystem */
   134 	if ( (flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM) ) {
   135 		if ( SDL_CDROMInit() < 0 ) {
   136 			return(-1);
   137 		}
   138 		SDL_initialized |= SDL_INIT_CDROM;
   139 	}
   140 #else
   141 	if ( flags & SDL_INIT_CDROM ) {
   142 		SDL_SetError("SDL not built with cdrom support");
   143 		return(-1);
   144 	}
   145 #endif
   146 	return(0);
   147 }
   148 
   149 int SDL_Init(Uint32 flags)
   150 {
   151 #if !defined(DISABLE_THREADS) && defined(ENABLE_PTH)
   152 	if (!pth_init()) {
   153 		return -1;
   154 	}
   155 #endif
   156 
   157 	/* Clear the error message */
   158 	SDL_ClearError();
   159 
   160 	/* Initialize the desired subsystems */
   161 	if ( SDL_InitSubSystem(flags) < 0 ) {
   162 		return(-1);
   163 	}
   164 
   165 	/* Everything is initialized */
   166 	if ( !(flags & SDL_INIT_NOPARACHUTE) ) {
   167 		SDL_InstallParachute();
   168 	}
   169 	return(0);
   170 }
   171 
   172 void SDL_QuitSubSystem(Uint32 flags)
   173 {
   174 	/* Shut down requested initialized subsystems */
   175 #ifndef DISABLE_CDROM
   176 	if ( (flags & SDL_initialized & SDL_INIT_CDROM) ) {
   177 		SDL_CDROMQuit();
   178 		SDL_initialized &= ~SDL_INIT_CDROM;
   179 	}
   180 #endif
   181 #ifndef DISABLE_JOYSTICK
   182 	if ( (flags & SDL_initialized & SDL_INIT_JOYSTICK) ) {
   183 		SDL_JoystickQuit();
   184 		SDL_initialized &= ~SDL_INIT_JOYSTICK;
   185 	}
   186 #endif
   187 #ifndef DISABLE_TIMERS
   188 	if ( (flags & SDL_initialized & SDL_INIT_TIMER) ) {
   189 		SDL_TimerQuit();
   190 		SDL_initialized &= ~SDL_INIT_TIMER;
   191 	}
   192 #endif
   193 #ifndef DISABLE_AUDIO
   194 	if ( (flags & SDL_initialized & SDL_INIT_AUDIO) ) {
   195 		SDL_AudioQuit();
   196 		SDL_initialized &= ~SDL_INIT_AUDIO;
   197 	}
   198 #endif
   199 #ifndef DISABLE_VIDEO
   200 	if ( (flags & SDL_initialized & SDL_INIT_VIDEO) ) {
   201 		SDL_VideoQuit();
   202 		SDL_initialized &= ~SDL_INIT_VIDEO;
   203 	}
   204 #endif
   205 }
   206 
   207 Uint32 SDL_WasInit(Uint32 flags)
   208 {
   209 	if ( ! flags ) {
   210 		flags = SDL_INIT_EVERYTHING;
   211 	}
   212 	return (SDL_initialized&flags);
   213 }
   214 
   215 void SDL_Quit(void)
   216 {
   217 	/* Quit all subsystems */
   218 #ifdef DEBUG_BUILD
   219   printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n"); fflush(stdout);
   220 #endif
   221 	SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   222 
   223 #ifdef CHECK_LEAKS
   224 #ifdef DEBUG_BUILD
   225   printf("[SDL_Quit] : CHECK_LEAKS\n"); fflush(stdout);
   226 #endif
   227 
   228 	/* Print the number of surfaces not freed */
   229 	if ( surfaces_allocated != 0 ) {
   230 		fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", 
   231 							surfaces_allocated);
   232 	}
   233 #endif
   234 #ifdef DEBUG_BUILD
   235   printf("[SDL_Quit] : SDL_UninstallParachute()\n"); fflush(stdout);
   236 #endif
   237 
   238 	/* Uninstall any parachute signal handlers */
   239 	SDL_UninstallParachute();
   240 
   241 #if !defined(DISABLE_THREADS) && defined(ENABLE_PTH)
   242 	pth_kill();
   243 #endif
   244 #ifdef DEBUG_BUILD
   245   printf("[SDL_Quit] : Returning!\n"); fflush(stdout);
   246 #endif
   247 
   248 }
   249 
   250 /* Return the library version number */
   251 const SDL_version * SDL_Linked_Version(void)
   252 {
   253 	return(&version);
   254 }
   255 
   256 #ifndef __OS2__
   257 #if defined(_WIN32_WCE) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   258 /* Need to include DllMain() on Windows CE and Watcom C for some reason.. */
   259 #include <windows.h>
   260 
   261 BOOL APIENTRY DllMain( HANDLE hModule, 
   262                        DWORD  ul_reason_for_call, 
   263                        LPVOID lpReserved )
   264 {
   265 	switch (ul_reason_for_call) {
   266 		case DLL_PROCESS_ATTACH:
   267 		case DLL_THREAD_ATTACH:
   268 		case DLL_THREAD_DETACH:
   269 		case DLL_PROCESS_DETACH:
   270 			break;
   271 	}
   272 	return TRUE;
   273 }
   274 #endif /* _WIN32_WCE and building DLL with Watcom C */
   275 #else
   276 // Building for OS/2
   277 #ifdef __WATCOMC__
   278 
   279 #define INCL_DOSERRORS
   280 #define INCL_DOSEXCEPTIONS
   281 #include <os2.h>
   282 
   283 // Exception handler to prevent the Audio thread hanging, making a zombie process!
   284 ULONG _System SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
   285                                         PEXCEPTIONREGISTRATIONRECORD pERegRec,
   286                                         PCONTEXTRECORD pCtxRec,
   287                                         PVOID p)
   288 {
   289   if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
   290     return XCPT_CONTINUE_SEARCH;
   291   if (pERepRec->fHandlerFlags & EH_UNWINDING)
   292     return XCPT_CONTINUE_SEARCH;
   293   if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
   294     return XCPT_CONTINUE_SEARCH;
   295 
   296   // Do cleanup at every fatal exception!
   297   if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) == XCPT_FATAL_EXCEPTION) &&
   298       (pERepRec->ExceptionNum != XCPT_BREAKPOINT) &&
   299       (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)
   300      )
   301   {
   302     if (SDL_initialized & SDL_INIT_AUDIO)
   303     {
   304       // This removes the zombie audio thread in case of emergency.
   305 #ifdef DEBUG_BUILD
   306       printf("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
   307 #endif
   308       SDL_CloseAudio();
   309     }
   310   }
   311   return (XCPT_CONTINUE_SEARCH);
   312 }
   313 
   314 
   315 EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand = {0, SDL_Main_ExceptionHandler};
   316 
   317 // The main DLL entry for DLL Initialization and Uninitialization:
   318 unsigned _System LibMain(unsigned hmod, unsigned termination)
   319 {
   320   if (termination)
   321   {
   322 #ifdef DEBUG_BUILD
   323 //    printf("[SDL DLL Unintialization] : Removing exception handler\n");
   324 #endif
   325     DosUnsetExceptionHandler(&SDL_Main_xcpthand);
   326     return 1;
   327   } else
   328   {
   329 #ifdef DEBUG_BUILD
   330     // Make stdout and stderr unbuffered!
   331     setbuf(stdout, NULL);
   332     setbuf(stderr, NULL);
   333 #endif
   334     // Fire up exception handler
   335 #ifdef DEBUG_BUILD
   336 //    printf("[SDL DLL Initialization] : Setting exception handler\n");
   337 #endif
   338     // Set exception handler
   339     DosSetExceptionHandler(&SDL_Main_xcpthand);
   340 
   341     return 1;
   342   }
   343 }
   344 
   345 #endif
   346 #endif