src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Jan 2010 06:47:17 +0000
changeset 3647 c5925cd41955
parent 3319 63d4517fc4ac
child 3663 bc50cd16bb07
permissions -rw-r--r--
First pass at Ryan's assertion code, minor tweaks to come.
     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 #include "SDL_assert.h"
    29 
    30 #if !SDL_VIDEO_DISABLED
    31 #include "video/SDL_leaks.h"
    32 #endif
    33 
    34 #if SDL_THREAD_PTH
    35 #include <pth.h>
    36 #endif
    37 
    38 /* Initialization/Cleanup routines */
    39 #if !SDL_JOYSTICK_DISABLED
    40 extern int SDL_JoystickInit(void);
    41 extern void SDL_JoystickQuit(void);
    42 #endif
    43 #if !SDL_HAPTIC_DISABLED
    44 extern int SDL_HapticInit(void);
    45 extern int SDL_HapticQuit(void);
    46 #endif
    47 #if !SDL_TIMERS_DISABLED
    48 extern void SDL_StartTicks(void);
    49 extern int SDL_TimerInit(void);
    50 extern void SDL_TimerQuit(void);
    51 #endif
    52 #if defined(__WIN32__)
    53 extern int SDL_HelperWindowCreate(void);
    54 extern int SDL_HelperWindowDestroy(void);
    55 #endif
    56 
    57 extern int SDL_AssertionsInit(void);
    58 extern void SDL_AssertionsQuit(void);
    59 
    60 /* The initialized subsystems */
    61 static Uint32 SDL_initialized = 0;
    62 static Uint32 ticks_started = 0;
    63 
    64 #ifdef CHECK_LEAKS
    65 int surfaces_allocated = 0;
    66 #endif
    67 
    68 int
    69 SDL_InitSubSystem(Uint32 flags)
    70 {
    71 #if !SDL_VIDEO_DISABLED
    72     /* Initialize the video/event subsystem */
    73     if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
    74         if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
    75             return (-1);
    76         }
    77         SDL_initialized |= SDL_INIT_VIDEO;
    78     }
    79 #else
    80     if (flags & SDL_INIT_VIDEO) {
    81         SDL_SetError("SDL not built with video support");
    82         return (-1);
    83     }
    84 #endif
    85 
    86 #if !SDL_AUDIO_DISABLED
    87     /* Initialize the audio subsystem */
    88     if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
    89         if (SDL_AudioInit(NULL) < 0) {
    90             return (-1);
    91         }
    92         SDL_initialized |= SDL_INIT_AUDIO;
    93     }
    94 #else
    95     if (flags & SDL_INIT_AUDIO) {
    96         SDL_SetError("SDL not built with audio support");
    97         return (-1);
    98     }
    99 #endif
   100 
   101 #if !SDL_TIMERS_DISABLED
   102     /* Initialize the timer subsystem */
   103     if (!ticks_started) {
   104         SDL_StartTicks();
   105         ticks_started = 1;
   106     }
   107     if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
   108         if (SDL_TimerInit() < 0) {
   109             return (-1);
   110         }
   111         SDL_initialized |= SDL_INIT_TIMER;
   112     }
   113 #else
   114     if (flags & SDL_INIT_TIMER) {
   115         SDL_SetError("SDL not built with timer support");
   116         return (-1);
   117     }
   118 #endif
   119 
   120 #if !SDL_JOYSTICK_DISABLED
   121     /* Initialize the joystick subsystem */
   122     if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
   123         if (SDL_JoystickInit() < 0) {
   124             return (-1);
   125         }
   126         SDL_initialized |= SDL_INIT_JOYSTICK;
   127     }
   128 #else
   129     if (flags & SDL_INIT_JOYSTICK) {
   130         SDL_SetError("SDL not built with joystick support");
   131         return (-1);
   132     }
   133 #endif
   134 
   135 #if !SDL_HAPTIC_DISABLED
   136     /* Initialize the haptic subsystem */
   137     if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
   138         if (SDL_HapticInit() < 0) {
   139             return (-1);
   140         }
   141         SDL_initialized |= SDL_INIT_HAPTIC;
   142     }
   143 #else
   144     if (flags & SDL_INIT_HAPTIC) {
   145         SDL_SetError("SDL not built with haptic (force feedback) support");
   146         return (-1);
   147     }
   148 #endif
   149     return (0);
   150 }
   151 
   152 int
   153 SDL_Init(Uint32 flags)
   154 {
   155 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
   156     if (!pth_init()) {
   157         return -1;
   158     }
   159 #endif
   160 
   161     if (SDL_AssertionsInit() < 0) {
   162         return -1;
   163     }
   164 
   165     /* Clear the error message */
   166     SDL_ClearError();
   167 
   168 #if defined(__WIN32__)
   169     if (SDL_HelperWindowCreate() < 0) {
   170         return -1;
   171     }
   172 #endif
   173 
   174     /* Initialize the desired subsystems */
   175     if (SDL_InitSubSystem(flags) < 0) {
   176         return (-1);
   177     }
   178 
   179     /* Everything is initialized */
   180     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   181         SDL_InstallParachute();
   182     }
   183 
   184     /* brief sanity checks for the sanity checks.  :)  */
   185     SDL_assert(1);
   186     SDL_assert_release(1);
   187     SDL_assert_paranoid(1);
   188     SDL_assert(0 || 1);
   189     SDL_assert_release(0 || 1);
   190     SDL_assert_paranoid(0 || 1);
   191 
   192 #if 0   /* enable this to test assertion failures. */
   193     SDL_assert_release(1 == 2);
   194     SDL_assert_release(5 < 4);
   195     SDL_assert_release(0 && "This is a test");
   196 #endif
   197 
   198     return (0);
   199 }
   200 
   201 void
   202 SDL_QuitSubSystem(Uint32 flags)
   203 {
   204     /* Shut down requested initialized subsystems */
   205 #if !SDL_JOYSTICK_DISABLED
   206     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   207         SDL_JoystickQuit();
   208         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   209     }
   210 #endif
   211 #if !SDL_HAPTIC_DISABLED
   212     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   213         SDL_HapticQuit();
   214         SDL_initialized &= ~SDL_INIT_HAPTIC;
   215     }
   216 #endif
   217 #if !SDL_TIMERS_DISABLED
   218     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   219         SDL_TimerQuit();
   220         SDL_initialized &= ~SDL_INIT_TIMER;
   221     }
   222 #endif
   223 #if !SDL_AUDIO_DISABLED
   224     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   225         SDL_AudioQuit();
   226         SDL_initialized &= ~SDL_INIT_AUDIO;
   227     }
   228 #endif
   229 #if !SDL_VIDEO_DISABLED
   230     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   231         SDL_VideoQuit();
   232         SDL_initialized &= ~SDL_INIT_VIDEO;
   233     }
   234 #endif
   235 }
   236 
   237 Uint32
   238 SDL_WasInit(Uint32 flags)
   239 {
   240     if (!flags) {
   241         flags = SDL_INIT_EVERYTHING;
   242     }
   243     return (SDL_initialized & flags);
   244 }
   245 
   246 void
   247 SDL_Quit(void)
   248 {
   249     /* Quit all subsystems */
   250 #ifdef DEBUG_BUILD
   251     printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
   252     fflush(stdout);
   253 #endif
   254 
   255 #if defined(__WIN32__)
   256     SDL_HelperWindowDestroy();
   257 #endif
   258     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   259 
   260 #ifdef CHECK_LEAKS
   261 #ifdef DEBUG_BUILD
   262     printf("[SDL_Quit] : CHECK_LEAKS\n");
   263     fflush(stdout);
   264 #endif
   265 
   266     /* !!! FIXME: make this an assertion. */
   267     /* Print the number of surfaces not freed */
   268     if (surfaces_allocated != 0) {
   269         fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
   270                 surfaces_allocated);
   271     }
   272 #endif
   273 #ifdef DEBUG_BUILD
   274     printf("[SDL_Quit] : SDL_UninstallParachute()\n");
   275     fflush(stdout);
   276 #endif
   277 
   278     /* Uninstall any parachute signal handlers */
   279     SDL_UninstallParachute();
   280 
   281     SDL_AssertionsQuit();
   282 
   283 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
   284     pth_kill();
   285 #endif
   286 #ifdef DEBUG_BUILD
   287     printf("[SDL_Quit] : Returning!\n");
   288     fflush(stdout);
   289 #endif
   290 
   291 }
   292 
   293 /* Get the library version number */
   294 void
   295 SDL_GetVersion(SDL_version * ver)
   296 {
   297     SDL_VERSION(ver);
   298 }
   299 
   300 /* Get the library source revision */
   301 int
   302 SDL_GetRevision(void)
   303 {
   304     return SDL_REVISION;
   305 }
   306 
   307 /* Get the name of the platform */
   308 const char *
   309 SDL_GetPlatform()
   310 {
   311 #if __AIX__
   312     return "AIX";
   313 #elif __HAIKU__
   314 /* Haiku must appear here before BeOS, since it also defines __BEOS__ */
   315     return "Haiku";
   316 #elif __BEOS__
   317     return "BeOS";
   318 #elif __BSDI__
   319     return "BSDI";
   320 #elif __DREAMCAST__
   321     return "Dreamcast";
   322 #elif __FREEBSD__
   323     return "FreeBSD";
   324 #elif __HPUX__
   325     return "HP-UX";
   326 #elif __IRIX__
   327     return "Irix";
   328 #elif __LINUX__
   329     return "Linux";
   330 #elif __MINT__
   331     return "Atari MiNT";
   332 #elif __MACOS__
   333     return "MacOS Classic";
   334 #elif __MACOSX__
   335     return "Mac OS X";
   336 #elif __NETBSD__
   337     return "NetBSD";
   338 #elif __OPENBSD__
   339     return "OpenBSD";
   340 #elif __OS2__
   341     return "OS/2";
   342 #elif __OSF__
   343     return "OSF/1";
   344 #elif __QNXNTO__
   345     return "QNX Neutrino";
   346 #elif __RISCOS__
   347     return "RISC OS";
   348 #elif __SOLARIS__
   349     return "Solaris";
   350 #elif __WIN32__
   351 #ifdef _WIN32_WCE
   352     return "Windows CE";
   353 #else
   354     return "Windows";
   355 #endif
   356 #elif __IPHONEOS__
   357     return "iPhone OS";
   358 #else
   359     return "Unknown (see SDL_platform.h)";
   360 #endif
   361 }
   362 
   363 #if defined(__WIN32__)
   364 
   365 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   366 /* Need to include DllMain() on Watcom C for some reason.. */
   367 #define WIN32_LEAN_AND_MEAN
   368 #include <windows.h>
   369 
   370 BOOL APIENTRY
   371 _DllMainCRTStartup(HANDLE hModule,
   372                    DWORD ul_reason_for_call, LPVOID lpReserved)
   373 {
   374     switch (ul_reason_for_call) {
   375     case DLL_PROCESS_ATTACH:
   376     case DLL_THREAD_ATTACH:
   377     case DLL_THREAD_DETACH:
   378     case DLL_PROCESS_DETACH:
   379         break;
   380     }
   381     return TRUE;
   382 }
   383 #endif /* building DLL with Watcom C */
   384 
   385 #endif /* __WIN32__ */
   386 
   387 /* vi: set ts=4 sw=4 expandtab: */