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