src/SDL.c
author Darren Alton <dalton@stevens.edu>
Thu, 10 Jul 2008 23:35:01 +0000
branchgsoc2008_nds
changeset 2678 3895761db26a
parent 2674 f1d07ba2e275
permissions -rw-r--r--
Removed old touchscreen system, starting to implement mouse-based touchscreen. Also, more work on a non-software-rendering video driver
     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(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
    66             return (-1);
    67         }
    68         SDL_initialized |= SDL_INIT_VIDEO;
    69     }
    70 #else
    71     if (flags & SDL_INIT_VIDEO) {
    72         SDL_SetError("SDL not built with video support");
    73         return (-1);
    74     }
    75 #endif
    76 
    77 #if !SDL_AUDIO_DISABLED
    78     /* Initialize the audio subsystem */
    79     if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
    80         if (SDL_AudioInit(NULL) < 0) {
    81             return (-1);
    82         }
    83         SDL_initialized |= SDL_INIT_AUDIO;
    84     }
    85 #else
    86     if (flags & SDL_INIT_AUDIO) {
    87         SDL_SetError("SDL not built with audio support");
    88         return (-1);
    89     }
    90 #endif
    91 
    92 #if !SDL_TIMERS_DISABLED
    93     /* Initialize the timer subsystem */
    94     if (!ticks_started) {
    95         SDL_StartTicks();
    96         ticks_started = 1;
    97     }
    98     if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
    99         if (SDL_TimerInit() < 0) {
   100             return (-1);
   101         }
   102         SDL_initialized |= SDL_INIT_TIMER;
   103     }
   104 #else
   105     if (flags & SDL_INIT_TIMER) {
   106         SDL_SetError("SDL not built with timer support");
   107         return (-1);
   108     }
   109 #endif
   110 
   111 #if !SDL_JOYSTICK_DISABLED
   112     /* Initialize the joystick subsystem */
   113     if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
   114         if (SDL_JoystickInit() < 0) {
   115             return (-1);
   116         }
   117         SDL_initialized |= SDL_INIT_JOYSTICK;
   118     }
   119 #else
   120     if (flags & SDL_INIT_JOYSTICK) {
   121         SDL_SetError("SDL not built with joystick support");
   122         return (-1);
   123     }
   124 #endif
   125 
   126 #if !SDL_CDROM_DISABLED
   127     /* Initialize the CD-ROM subsystem */
   128     if ((flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM)) {
   129         if (SDL_CDROMInit() < 0) {
   130             return (-1);
   131         }
   132         SDL_initialized |= SDL_INIT_CDROM;
   133     }
   134 #else
   135     if (flags & SDL_INIT_CDROM) {
   136         SDL_SetError("SDL not built with cdrom support");
   137         return (-1);
   138     }
   139 #endif
   140     return (0);
   141 }
   142 
   143 int
   144 SDL_Init(Uint32 flags)
   145 {
   146 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
   147     if (!pth_init()) {
   148         return -1;
   149     }
   150 #endif
   151 
   152     /* Clear the error message */
   153     SDL_ClearError();
   154 
   155     /* Initialize the desired subsystems */
   156     if (SDL_InitSubSystem(flags) < 0) {
   157         return (-1);
   158     }
   159 
   160     /* Everything is initialized */
   161     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   162         SDL_InstallParachute();
   163     }
   164     return (0);
   165 }
   166 
   167 void
   168 SDL_QuitSubSystem(Uint32 flags)
   169 {
   170     /* Shut down requested initialized subsystems */
   171 #if !SDL_CDROM_DISABLED
   172     if ((flags & SDL_initialized & SDL_INIT_CDROM)) {
   173         SDL_CDROMQuit();
   174         SDL_initialized &= ~SDL_INIT_CDROM;
   175     }
   176 #endif
   177 #if !SDL_JOYSTICK_DISABLED
   178     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   179         SDL_JoystickQuit();
   180         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   181     }
   182 #endif
   183 #if !SDL_TIMERS_DISABLED
   184     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   185         SDL_TimerQuit();
   186         SDL_initialized &= ~SDL_INIT_TIMER;
   187     }
   188 #endif
   189 #if !SDL_AUDIO_DISABLED
   190     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   191         SDL_AudioQuit();
   192         SDL_initialized &= ~SDL_INIT_AUDIO;
   193     }
   194 #endif
   195 #if !SDL_VIDEO_DISABLED
   196     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   197         SDL_VideoQuit();
   198         SDL_initialized &= ~SDL_INIT_VIDEO;
   199     }
   200 #endif
   201 }
   202 
   203 Uint32
   204 SDL_WasInit(Uint32 flags)
   205 {
   206     if (!flags) {
   207         flags = SDL_INIT_EVERYTHING;
   208     }
   209     return (SDL_initialized & flags);
   210 }
   211 
   212 void
   213 SDL_Quit(void)
   214 {
   215     /* Quit all subsystems */
   216 #ifdef DEBUG_BUILD
   217     printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
   218     fflush(stdout);
   219 #endif
   220     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   221 
   222 #ifdef CHECK_LEAKS
   223 #ifdef DEBUG_BUILD
   224     printf("[SDL_Quit] : CHECK_LEAKS\n");
   225     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");
   236     fflush(stdout);
   237 #endif
   238 
   239     /* Uninstall any parachute signal handlers */
   240     SDL_UninstallParachute();
   241 
   242 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
   243     pth_kill();
   244 #endif
   245 #ifdef DEBUG_BUILD
   246     printf("[SDL_Quit] : Returning!\n");
   247     fflush(stdout);
   248 #endif
   249 
   250 }
   251 
   252 /* Get the library version number */
   253 void
   254 SDL_GetVersion(SDL_version * ver)
   255 {
   256     SDL_VERSION(ver);
   257 }
   258 
   259 #if defined(__OS2__)
   260 /* Building for OS/2 */
   261 #ifdef __WATCOMC__
   262 
   263 #define INCL_DOSERRORS
   264 #define INCL_DOSEXCEPTIONS
   265 #include <os2.h>
   266 
   267 /* Exception handler to prevent the Audio thread hanging, making a zombie process! */
   268 ULONG _System
   269 SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
   270                           PEXCEPTIONREGISTRATIONRECORD pERegRec,
   271                           PCONTEXTRECORD pCtxRec, PVOID p)
   272 {
   273     if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
   274         return XCPT_CONTINUE_SEARCH;
   275     if (pERepRec->fHandlerFlags & EH_UNWINDING)
   276         return XCPT_CONTINUE_SEARCH;
   277     if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
   278         return XCPT_CONTINUE_SEARCH;
   279 
   280     /* Do cleanup at every fatal exception! */
   281     if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) ==
   282          XCPT_FATAL_EXCEPTION) && (pERepRec->ExceptionNum != XCPT_BREAKPOINT)
   283         && (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)) {
   284         if (SDL_initialized & SDL_INIT_AUDIO) {
   285             /* This removes the zombie audio thread in case of emergency. */
   286 #ifdef DEBUG_BUILD
   287             printf
   288                 ("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
   289 #endif
   290             SDL_CloseAudio();
   291         }
   292     }
   293     return (XCPT_CONTINUE_SEARCH);
   294 }
   295 
   296 
   297 EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand =
   298     { 0, SDL_Main_ExceptionHandler };
   299 
   300 /* The main DLL entry for DLL Initialization and Uninitialization: */
   301 unsigned _System
   302 LibMain(unsigned hmod, unsigned termination)
   303 {
   304     if (termination) {
   305 #ifdef DEBUG_BUILD
   306 /*    printf("[SDL DLL Unintialization] : Removing exception handler\n"); */
   307 #endif
   308         DosUnsetExceptionHandler(&SDL_Main_xcpthand);
   309         return 1;
   310     } else {
   311 #ifdef DEBUG_BUILD
   312         /* Make stdout and stderr unbuffered! */
   313         setbuf(stdout, NULL);
   314         setbuf(stderr, NULL);
   315 #endif
   316         /* Fire up exception handler */
   317 #ifdef DEBUG_BUILD
   318 /*    printf("[SDL DLL Initialization] : Setting exception handler\n"); */
   319 #endif
   320         /* Set exception handler */
   321         DosSetExceptionHandler(&SDL_Main_xcpthand);
   322 
   323         return 1;
   324     }
   325 }
   326 #endif /* __WATCOMC__ */
   327 
   328 #elif defined(__WIN32__)
   329 
   330 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   331 /* Need to include DllMain() on Watcom C for some reason.. */
   332 #define WIN32_LEAN_AND_MEAN
   333 #include <windows.h>
   334 
   335 BOOL APIENTRY
   336 _DllMainCRTStartup(HANDLE hModule,
   337                    DWORD ul_reason_for_call, LPVOID lpReserved)
   338 {
   339     switch (ul_reason_for_call) {
   340     case DLL_PROCESS_ATTACH:
   341     case DLL_THREAD_ATTACH:
   342     case DLL_THREAD_DETACH:
   343     case DLL_PROCESS_DETACH:
   344         break;
   345     }
   346     return TRUE;
   347 }
   348 #endif /* building DLL with Watcom C */
   349 
   350 #endif /* OS/2 elif __WIN32__ */
   351 
   352 /* vi: set ts=4 sw=4 expandtab: */