src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1336 3692456e7b0f
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
     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 #ifdef ENABLE_PTH
    26 #include <pth.h>
    27 #endif
    28 
    29 #include "SDL.h"
    30 #include "SDL_endian.h"
    31 #include "SDL_fatal.h"
    32 #include "SDL_stdlib.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 #if defined(__OS2__)
   257 // Building for OS/2
   258 #ifdef __WATCOMC__
   259 
   260 #define INCL_DOSERRORS
   261 #define INCL_DOSEXCEPTIONS
   262 #include <os2.h>
   263 
   264 // Exception handler to prevent the Audio thread hanging, making a zombie process!
   265 ULONG _System SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
   266                                         PEXCEPTIONREGISTRATIONRECORD pERegRec,
   267                                         PCONTEXTRECORD pCtxRec,
   268                                         PVOID p)
   269 {
   270   if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
   271     return XCPT_CONTINUE_SEARCH;
   272   if (pERepRec->fHandlerFlags & EH_UNWINDING)
   273     return XCPT_CONTINUE_SEARCH;
   274   if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
   275     return XCPT_CONTINUE_SEARCH;
   276 
   277   // Do cleanup at every fatal exception!
   278   if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) == XCPT_FATAL_EXCEPTION) &&
   279       (pERepRec->ExceptionNum != XCPT_BREAKPOINT) &&
   280       (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)
   281      )
   282   {
   283     if (SDL_initialized & SDL_INIT_AUDIO)
   284     {
   285       // This removes the zombie audio thread in case of emergency.
   286 #ifdef DEBUG_BUILD
   287       printf("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
   288 #endif
   289       SDL_CloseAudio();
   290     }
   291   }
   292   return (XCPT_CONTINUE_SEARCH);
   293 }
   294 
   295 
   296 EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand = {0, SDL_Main_ExceptionHandler};
   297 
   298 // The main DLL entry for DLL Initialization and Uninitialization:
   299 unsigned _System LibMain(unsigned hmod, unsigned termination)
   300 {
   301   if (termination)
   302   {
   303 #ifdef DEBUG_BUILD
   304 //    printf("[SDL DLL Unintialization] : Removing exception handler\n");
   305 #endif
   306     DosUnsetExceptionHandler(&SDL_Main_xcpthand);
   307     return 1;
   308   } else
   309   {
   310 #ifdef DEBUG_BUILD
   311     // Make stdout and stderr unbuffered!
   312     setbuf(stdout, NULL);
   313     setbuf(stderr, NULL);
   314 #endif
   315     // Fire up exception handler
   316 #ifdef DEBUG_BUILD
   317 //    printf("[SDL DLL Initialization] : Setting exception handler\n");
   318 #endif
   319     // Set exception handler
   320     DosSetExceptionHandler(&SDL_Main_xcpthand);
   321 
   322     return 1;
   323   }
   324 }
   325 #endif /* __WATCOMC__ */
   326 
   327 #elif defined(_WIN32)
   328 
   329 #if !defined(HAVE_LIBC) || defined(_WIN32_WCE) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   330 /* Need to include DllMain() on Windows CE and Watcom C for some reason.. */
   331 #include "SDL_windows.h"
   332 
   333 BOOL APIENTRY _DllMainCRTStartup( HANDLE hModule, 
   334                        DWORD  ul_reason_for_call, 
   335                        LPVOID lpReserved )
   336 {
   337 	switch (ul_reason_for_call) {
   338 		case DLL_PROCESS_ATTACH:
   339 		case DLL_THREAD_ATTACH:
   340 		case DLL_THREAD_DETACH:
   341 		case DLL_PROCESS_DETACH:
   342 			break;
   343 	}
   344 	return TRUE;
   345 }
   346 #endif /* _WIN32_WCE and building DLL with Watcom C */
   347 
   348 #endif /* OS/2 elif _WIN32 */