src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Jan 2010 09:48:42 +0000
changeset 3663 bc50cd16bb07
parent 3647 c5925cd41955
child 3696 47d923feedb0
permissions -rw-r--r--
Moved the assertion tests to testplatform
     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     return (0);
   185 }
   186 
   187 void
   188 SDL_QuitSubSystem(Uint32 flags)
   189 {
   190     /* Shut down requested initialized subsystems */
   191 #if !SDL_JOYSTICK_DISABLED
   192     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   193         SDL_JoystickQuit();
   194         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   195     }
   196 #endif
   197 #if !SDL_HAPTIC_DISABLED
   198     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   199         SDL_HapticQuit();
   200         SDL_initialized &= ~SDL_INIT_HAPTIC;
   201     }
   202 #endif
   203 #if !SDL_TIMERS_DISABLED
   204     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   205         SDL_TimerQuit();
   206         SDL_initialized &= ~SDL_INIT_TIMER;
   207     }
   208 #endif
   209 #if !SDL_AUDIO_DISABLED
   210     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   211         SDL_AudioQuit();
   212         SDL_initialized &= ~SDL_INIT_AUDIO;
   213     }
   214 #endif
   215 #if !SDL_VIDEO_DISABLED
   216     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   217         SDL_VideoQuit();
   218         SDL_initialized &= ~SDL_INIT_VIDEO;
   219     }
   220 #endif
   221 }
   222 
   223 Uint32
   224 SDL_WasInit(Uint32 flags)
   225 {
   226     if (!flags) {
   227         flags = SDL_INIT_EVERYTHING;
   228     }
   229     return (SDL_initialized & flags);
   230 }
   231 
   232 void
   233 SDL_Quit(void)
   234 {
   235     /* Quit all subsystems */
   236 #ifdef DEBUG_BUILD
   237     printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
   238     fflush(stdout);
   239 #endif
   240 
   241 #if defined(__WIN32__)
   242     SDL_HelperWindowDestroy();
   243 #endif
   244     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   245 
   246 #ifdef CHECK_LEAKS
   247 #ifdef DEBUG_BUILD
   248     printf("[SDL_Quit] : CHECK_LEAKS\n");
   249     fflush(stdout);
   250 #endif
   251 
   252     /* !!! FIXME: make this an assertion. */
   253     /* Print the number of surfaces not freed */
   254     if (surfaces_allocated != 0) {
   255         fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
   256                 surfaces_allocated);
   257     }
   258 #endif
   259 #ifdef DEBUG_BUILD
   260     printf("[SDL_Quit] : SDL_UninstallParachute()\n");
   261     fflush(stdout);
   262 #endif
   263 
   264     /* Uninstall any parachute signal handlers */
   265     SDL_UninstallParachute();
   266 
   267     SDL_AssertionsQuit();
   268 
   269 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
   270     pth_kill();
   271 #endif
   272 #ifdef DEBUG_BUILD
   273     printf("[SDL_Quit] : Returning!\n");
   274     fflush(stdout);
   275 #endif
   276 
   277 }
   278 
   279 /* Get the library version number */
   280 void
   281 SDL_GetVersion(SDL_version * ver)
   282 {
   283     SDL_VERSION(ver);
   284 }
   285 
   286 /* Get the library source revision */
   287 int
   288 SDL_GetRevision(void)
   289 {
   290     return SDL_REVISION;
   291 }
   292 
   293 /* Get the name of the platform */
   294 const char *
   295 SDL_GetPlatform()
   296 {
   297 #if __AIX__
   298     return "AIX";
   299 #elif __HAIKU__
   300 /* Haiku must appear here before BeOS, since it also defines __BEOS__ */
   301     return "Haiku";
   302 #elif __BEOS__
   303     return "BeOS";
   304 #elif __BSDI__
   305     return "BSDI";
   306 #elif __DREAMCAST__
   307     return "Dreamcast";
   308 #elif __FREEBSD__
   309     return "FreeBSD";
   310 #elif __HPUX__
   311     return "HP-UX";
   312 #elif __IRIX__
   313     return "Irix";
   314 #elif __LINUX__
   315     return "Linux";
   316 #elif __MINT__
   317     return "Atari MiNT";
   318 #elif __MACOS__
   319     return "MacOS Classic";
   320 #elif __MACOSX__
   321     return "Mac OS X";
   322 #elif __NETBSD__
   323     return "NetBSD";
   324 #elif __OPENBSD__
   325     return "OpenBSD";
   326 #elif __OS2__
   327     return "OS/2";
   328 #elif __OSF__
   329     return "OSF/1";
   330 #elif __QNXNTO__
   331     return "QNX Neutrino";
   332 #elif __RISCOS__
   333     return "RISC OS";
   334 #elif __SOLARIS__
   335     return "Solaris";
   336 #elif __WIN32__
   337 #ifdef _WIN32_WCE
   338     return "Windows CE";
   339 #else
   340     return "Windows";
   341 #endif
   342 #elif __IPHONEOS__
   343     return "iPhone OS";
   344 #else
   345     return "Unknown (see SDL_platform.h)";
   346 #endif
   347 }
   348 
   349 #if defined(__WIN32__)
   350 
   351 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   352 /* Need to include DllMain() on Watcom C for some reason.. */
   353 #define WIN32_LEAN_AND_MEAN
   354 #include <windows.h>
   355 
   356 BOOL APIENTRY
   357 _DllMainCRTStartup(HANDLE hModule,
   358                    DWORD ul_reason_for_call, LPVOID lpReserved)
   359 {
   360     switch (ul_reason_for_call) {
   361     case DLL_PROCESS_ATTACH:
   362     case DLL_THREAD_ATTACH:
   363     case DLL_THREAD_DETACH:
   364     case DLL_PROCESS_DETACH:
   365         break;
   366     }
   367     return TRUE;
   368 }
   369 #endif /* building DLL with Watcom C */
   370 
   371 #endif /* __WIN32__ */
   372 
   373 /* vi: set ts=4 sw=4 expandtab: */