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