src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Aug 2009 20:45:11 +0000
changeset 3229 09172593e9b6
parent 2996 972a69e47cd9
child 3243 5db962a9a991
permissions -rw-r--r--
Fixed type size for test_bit()
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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_HAPTIC_DISABLED
    42 extern int SDL_HapticInit(void);
    43 extern int SDL_HapticQuit(void);
    44 #endif
    45 #if !SDL_CDROM_DISABLED
    46 extern int SDL_CDROMInit(void);
    47 extern void SDL_CDROMQuit(void);
    48 #endif
    49 #if !SDL_TIMERS_DISABLED
    50 extern void SDL_StartTicks(void);
    51 extern int SDL_TimerInit(void);
    52 extern void SDL_TimerQuit(void);
    53 #endif
    54 #if defined(__WIN32__)
    55 extern int SDL_HelperWindowCreate(void);
    56 extern int SDL_HelperWindowDestroy(void);
    57 #endif
    58 
    59 /* The initialized subsystems */
    60 static Uint32 SDL_initialized = 0;
    61 static Uint32 ticks_started = 0;
    62 
    63 #ifdef CHECK_LEAKS
    64 int surfaces_allocated = 0;
    65 #endif
    66 
    67 int
    68 SDL_InitSubSystem(Uint32 flags)
    69 {
    70 #if !SDL_VIDEO_DISABLED
    71     /* Initialize the video/event subsystem */
    72     if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
    73         if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
    74             return (-1);
    75         }
    76         SDL_initialized |= SDL_INIT_VIDEO;
    77     }
    78 #else
    79     if (flags & SDL_INIT_VIDEO) {
    80         SDL_SetError("SDL not built with video support");
    81         return (-1);
    82     }
    83 #endif
    84 
    85 #if !SDL_AUDIO_DISABLED
    86     /* Initialize the audio subsystem */
    87     if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
    88         if (SDL_AudioInit(NULL) < 0) {
    89             return (-1);
    90         }
    91         SDL_initialized |= SDL_INIT_AUDIO;
    92     }
    93 #else
    94     if (flags & SDL_INIT_AUDIO) {
    95         SDL_SetError("SDL not built with audio support");
    96         return (-1);
    97     }
    98 #endif
    99 
   100 #if !SDL_TIMERS_DISABLED
   101     /* Initialize the timer subsystem */
   102     if (!ticks_started) {
   103         SDL_StartTicks();
   104         ticks_started = 1;
   105     }
   106     if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
   107         if (SDL_TimerInit() < 0) {
   108             return (-1);
   109         }
   110         SDL_initialized |= SDL_INIT_TIMER;
   111     }
   112 #else
   113     if (flags & SDL_INIT_TIMER) {
   114         SDL_SetError("SDL not built with timer support");
   115         return (-1);
   116     }
   117 #endif
   118 
   119 #if !SDL_JOYSTICK_DISABLED
   120     /* Initialize the joystick subsystem */
   121     if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
   122         if (SDL_JoystickInit() < 0) {
   123             return (-1);
   124         }
   125         SDL_initialized |= SDL_INIT_JOYSTICK;
   126     }
   127 #else
   128     if (flags & SDL_INIT_JOYSTICK) {
   129         SDL_SetError("SDL not built with joystick support");
   130         return (-1);
   131     }
   132 #endif
   133 
   134 #if !SDL_HAPTIC_DISABLED
   135     /* Initialize the haptic subsystem */
   136     if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
   137         if (SDL_HapticInit() < 0) {
   138             return (-1);
   139         }
   140         SDL_initialized |= SDL_INIT_HAPTIC;
   141     }
   142 #else
   143     if (flags & SDL_INIT_HAPTIC) {
   144         SDL_SetError("SDL not built with haptic (force feedback) support");
   145         return (-1);
   146     }
   147 #endif
   148 
   149 
   150 #if !SDL_CDROM_DISABLED
   151     /* Initialize the CD-ROM subsystem */
   152     if ((flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM)) {
   153         if (SDL_CDROMInit() < 0) {
   154             return (-1);
   155         }
   156         SDL_initialized |= SDL_INIT_CDROM;
   157     }
   158 #else
   159     if (flags & SDL_INIT_CDROM) {
   160         SDL_SetError("SDL not built with cdrom support");
   161         return (-1);
   162     }
   163 #endif
   164     return (0);
   165 }
   166 
   167 int
   168 SDL_Init(Uint32 flags)
   169 {
   170 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
   171     if (!pth_init()) {
   172         return -1;
   173     }
   174 #endif
   175 
   176     /* Clear the error message */
   177     SDL_ClearError();
   178 
   179 #if defined(__WIN32__)
   180     if (SDL_HelperWindowCreate() < 0) {
   181         return -1;
   182     }
   183 #endif
   184 
   185     /* Initialize the desired subsystems */
   186     if (SDL_InitSubSystem(flags) < 0) {
   187         return (-1);
   188     }
   189 
   190     /* Everything is initialized */
   191     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   192         SDL_InstallParachute();
   193     }
   194     return (0);
   195 }
   196 
   197 void
   198 SDL_QuitSubSystem(Uint32 flags)
   199 {
   200     /* Shut down requested initialized subsystems */
   201 #if !SDL_CDROM_DISABLED
   202     if ((flags & SDL_initialized & SDL_INIT_CDROM)) {
   203         SDL_CDROMQuit();
   204         SDL_initialized &= ~SDL_INIT_CDROM;
   205     }
   206 #endif
   207 #if !SDL_JOYSTICK_DISABLED
   208     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   209         SDL_JoystickQuit();
   210         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   211     }
   212 #endif
   213 #if !SDL_HAPTIC_DISABLED
   214     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   215         SDL_HapticQuit();
   216         SDL_initialized &= ~SDL_INIT_HAPTIC;
   217     }
   218 #endif
   219 #if !SDL_TIMERS_DISABLED
   220     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   221         SDL_TimerQuit();
   222         SDL_initialized &= ~SDL_INIT_TIMER;
   223     }
   224 #endif
   225 #if !SDL_AUDIO_DISABLED
   226     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   227         SDL_AudioQuit();
   228         SDL_initialized &= ~SDL_INIT_AUDIO;
   229     }
   230 #endif
   231 #if !SDL_VIDEO_DISABLED
   232     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   233         SDL_VideoQuit();
   234         SDL_initialized &= ~SDL_INIT_VIDEO;
   235     }
   236 #endif
   237 }
   238 
   239 Uint32
   240 SDL_WasInit(Uint32 flags)
   241 {
   242     if (!flags) {
   243         flags = SDL_INIT_EVERYTHING;
   244     }
   245     return (SDL_initialized & flags);
   246 }
   247 
   248 void
   249 SDL_Quit(void)
   250 {
   251     /* Quit all subsystems */
   252 #ifdef DEBUG_BUILD
   253     printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
   254     fflush(stdout);
   255 #endif
   256 
   257 #if defined(__WIN32__)
   258     SDL_HelperWindowDestroy();
   259 #endif
   260     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   261 
   262 #ifdef CHECK_LEAKS
   263 #ifdef DEBUG_BUILD
   264     printf("[SDL_Quit] : CHECK_LEAKS\n");
   265     fflush(stdout);
   266 #endif
   267 
   268     /* Print the number of surfaces not freed */
   269     if (surfaces_allocated != 0) {
   270         fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
   271                 surfaces_allocated);
   272     }
   273 #endif
   274 #ifdef DEBUG_BUILD
   275     printf("[SDL_Quit] : SDL_UninstallParachute()\n");
   276     fflush(stdout);
   277 #endif
   278 
   279     /* Uninstall any parachute signal handlers */
   280     SDL_UninstallParachute();
   281 
   282 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
   283     pth_kill();
   284 #endif
   285 #ifdef DEBUG_BUILD
   286     printf("[SDL_Quit] : Returning!\n");
   287     fflush(stdout);
   288 #endif
   289 
   290 }
   291 
   292 /* Get the library version number */
   293 void
   294 SDL_GetVersion(SDL_version * ver)
   295 {
   296     SDL_VERSION(ver);
   297 }
   298 
   299 /* Get the library source revision */
   300 int
   301 SDL_GetRevision(void)
   302 {
   303     return SDL_REVISION;
   304 }
   305 
   306 #if defined(__OS2__)
   307 /* Building for OS/2 */
   308 #ifdef __WATCOMC__
   309 
   310 #define INCL_DOSERRORS
   311 #define INCL_DOSEXCEPTIONS
   312 #include <os2.h>
   313 
   314 /* Exception handler to prevent the Audio thread hanging, making a zombie process! */
   315 ULONG _System
   316 SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
   317                           PEXCEPTIONREGISTRATIONRECORD pERegRec,
   318                           PCONTEXTRECORD pCtxRec, PVOID p)
   319 {
   320     if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
   321         return XCPT_CONTINUE_SEARCH;
   322     if (pERepRec->fHandlerFlags & EH_UNWINDING)
   323         return XCPT_CONTINUE_SEARCH;
   324     if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
   325         return XCPT_CONTINUE_SEARCH;
   326 
   327     /* Do cleanup at every fatal exception! */
   328     if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) ==
   329          XCPT_FATAL_EXCEPTION) && (pERepRec->ExceptionNum != XCPT_BREAKPOINT)
   330         && (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)) {
   331         if (SDL_initialized & SDL_INIT_AUDIO) {
   332             /* This removes the zombie audio thread in case of emergency. */
   333 #ifdef DEBUG_BUILD
   334             printf
   335                 ("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
   336 #endif
   337             SDL_CloseAudio();
   338         }
   339     }
   340     return (XCPT_CONTINUE_SEARCH);
   341 }
   342 
   343 
   344 EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand =
   345     { 0, SDL_Main_ExceptionHandler };
   346 
   347 /* The main DLL entry for DLL Initialization and Uninitialization: */
   348 unsigned _System
   349 LibMain(unsigned hmod, unsigned termination)
   350 {
   351     if (termination) {
   352 #ifdef DEBUG_BUILD
   353 /*    printf("[SDL DLL Unintialization] : Removing exception handler\n"); */
   354 #endif
   355         DosUnsetExceptionHandler(&SDL_Main_xcpthand);
   356         return 1;
   357     } else {
   358 #ifdef DEBUG_BUILD
   359         /* Make stdout and stderr unbuffered! */
   360         setbuf(stdout, NULL);
   361         setbuf(stderr, NULL);
   362 #endif
   363         /* Fire up exception handler */
   364 #ifdef DEBUG_BUILD
   365 /*    printf("[SDL DLL Initialization] : Setting exception handler\n"); */
   366 #endif
   367         /* Set exception handler */
   368         DosSetExceptionHandler(&SDL_Main_xcpthand);
   369 
   370         return 1;
   371     }
   372 }
   373 #endif /* __WATCOMC__ */
   374 
   375 #elif defined(__WIN32__)
   376 
   377 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   378 /* Need to include DllMain() on Watcom C for some reason.. */
   379 #define WIN32_LEAN_AND_MEAN
   380 #include <windows.h>
   381 
   382 BOOL APIENTRY
   383 _DllMainCRTStartup(HANDLE hModule,
   384                    DWORD ul_reason_for_call, LPVOID lpReserved)
   385 {
   386     switch (ul_reason_for_call) {
   387     case DLL_PROCESS_ATTACH:
   388     case DLL_THREAD_ATTACH:
   389     case DLL_THREAD_DETACH:
   390     case DLL_PROCESS_DETACH:
   391         break;
   392     }
   393     return TRUE;
   394 }
   395 #endif /* building DLL with Watcom C */
   396 
   397 #endif /* OS/2 elif __WIN32__ */
   398 
   399 /* vi: set ts=4 sw=4 expandtab: */