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