src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Feb 2011 10:02:39 -0800
changeset 5189 6f6a9340fb93
parent 5123 dc0dfdd58f27
child 5221 2ee8112bfc6b
permissions -rw-r--r--
Added a hint system to allow configuration hints to be specified by the application.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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_c.h"
    29 #include "haptic/SDL_haptic_c.h"
    30 #include "joystick/SDL_joystick_c.h"
    31 
    32 #if !SDL_VIDEO_DISABLED
    33 #include "video/SDL_leaks.h"
    34 #endif
    35 
    36 /* Initialization/Cleanup routines */
    37 #if !SDL_TIMERS_DISABLED
    38 extern void SDL_StartTicks(void);
    39 extern int SDL_TimerInit(void);
    40 extern void SDL_TimerQuit(void);
    41 #endif
    42 #if defined(__WIN32__)
    43 extern int SDL_HelperWindowCreate(void);
    44 extern int SDL_HelperWindowDestroy(void);
    45 #endif
    46 
    47 
    48 /* The initialized subsystems */
    49 static Uint32 SDL_initialized = 0;
    50 static Uint32 ticks_started = 0;
    51 
    52 #ifdef CHECK_LEAKS
    53 int surfaces_allocated = 0;
    54 #endif
    55 
    56 int
    57 SDL_InitSubSystem(Uint32 flags)
    58 {
    59 #if !SDL_VIDEO_DISABLED
    60     /* Initialize the video/event subsystem */
    61     if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
    62         if (SDL_VideoInit(NULL) < 0) {
    63             return (-1);
    64         }
    65         SDL_initialized |= SDL_INIT_VIDEO;
    66     }
    67 #else
    68     if (flags & SDL_INIT_VIDEO) {
    69         SDL_SetError("SDL not built with video support");
    70         return (-1);
    71     }
    72 #endif
    73 
    74 #if !SDL_AUDIO_DISABLED
    75     /* Initialize the audio subsystem */
    76     if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
    77         if (SDL_AudioInit(NULL) < 0) {
    78             return (-1);
    79         }
    80         SDL_initialized |= SDL_INIT_AUDIO;
    81     }
    82 #else
    83     if (flags & SDL_INIT_AUDIO) {
    84         SDL_SetError("SDL not built with audio support");
    85         return (-1);
    86     }
    87 #endif
    88 
    89 #if !SDL_TIMERS_DISABLED
    90     /* Initialize the timer subsystem */
    91     if (!ticks_started) {
    92         SDL_StartTicks();
    93         ticks_started = 1;
    94     }
    95     if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
    96         if (SDL_TimerInit() < 0) {
    97             return (-1);
    98         }
    99         SDL_initialized |= SDL_INIT_TIMER;
   100     }
   101 #else
   102     if (flags & SDL_INIT_TIMER) {
   103         SDL_SetError("SDL not built with timer support");
   104         return (-1);
   105     }
   106 #endif
   107 
   108 #if !SDL_JOYSTICK_DISABLED
   109     /* Initialize the joystick subsystem */
   110     if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
   111         if (SDL_JoystickInit() < 0) {
   112             return (-1);
   113         }
   114         SDL_initialized |= SDL_INIT_JOYSTICK;
   115     }
   116 #else
   117     if (flags & SDL_INIT_JOYSTICK) {
   118         SDL_SetError("SDL not built with joystick support");
   119         return (-1);
   120     }
   121 #endif
   122 
   123 #if !SDL_HAPTIC_DISABLED
   124     /* Initialize the haptic subsystem */
   125     if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
   126         if (SDL_HapticInit() < 0) {
   127             return (-1);
   128         }
   129         SDL_initialized |= SDL_INIT_HAPTIC;
   130     }
   131 #else
   132     if (flags & SDL_INIT_HAPTIC) {
   133         SDL_SetError("SDL not built with haptic (force feedback) support");
   134         return (-1);
   135     }
   136 #endif
   137     return (0);
   138 }
   139 
   140 int
   141 SDL_Init(Uint32 flags)
   142 {
   143     if (SDL_AssertionsInit() < 0) {
   144         return -1;
   145     }
   146 
   147     /* Clear the error message */
   148     SDL_ClearError();
   149 
   150 #if defined(__WIN32__)
   151     if (SDL_HelperWindowCreate() < 0) {
   152         return -1;
   153     }
   154 #endif
   155 
   156     /* Initialize the desired subsystems */
   157     if (SDL_InitSubSystem(flags) < 0) {
   158         return (-1);
   159     }
   160 
   161     /* Everything is initialized */
   162     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   163         SDL_InstallParachute();
   164     }
   165 
   166     return (0);
   167 }
   168 
   169 void
   170 SDL_QuitSubSystem(Uint32 flags)
   171 {
   172     /* Shut down requested initialized subsystems */
   173 #if !SDL_JOYSTICK_DISABLED
   174     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   175         SDL_JoystickQuit();
   176         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   177     }
   178 #endif
   179 #if !SDL_HAPTIC_DISABLED
   180     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   181         SDL_HapticQuit();
   182         SDL_initialized &= ~SDL_INIT_HAPTIC;
   183     }
   184 #endif
   185 #if !SDL_TIMERS_DISABLED
   186     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   187         SDL_TimerQuit();
   188         SDL_initialized &= ~SDL_INIT_TIMER;
   189     }
   190 #endif
   191 #if !SDL_AUDIO_DISABLED
   192     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   193         SDL_AudioQuit();
   194         SDL_initialized &= ~SDL_INIT_AUDIO;
   195     }
   196 #endif
   197 #if !SDL_VIDEO_DISABLED
   198     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   199         SDL_VideoQuit();
   200         SDL_initialized &= ~SDL_INIT_VIDEO;
   201     }
   202 #endif
   203 }
   204 
   205 Uint32
   206 SDL_WasInit(Uint32 flags)
   207 {
   208     if (!flags) {
   209         flags = SDL_INIT_EVERYTHING;
   210     }
   211     return (SDL_initialized & flags);
   212 }
   213 
   214 void
   215 SDL_Quit(void)
   216 {
   217     /* Quit all subsystems */
   218 #ifdef DEBUG_BUILD
   219     printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
   220     fflush(stdout);
   221 #endif
   222 
   223 #if defined(__WIN32__)
   224     SDL_HelperWindowDestroy();
   225 #endif
   226     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   227 
   228 #ifdef CHECK_LEAKS
   229 #ifdef DEBUG_BUILD
   230     printf("[SDL_Quit] : CHECK_LEAKS\n");
   231     fflush(stdout);
   232 #endif
   233 
   234     /* !!! FIXME: make this an assertion. */
   235     /* Print the number of surfaces not freed */
   236     if (surfaces_allocated != 0) {
   237         fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
   238                 surfaces_allocated);
   239     }
   240 #endif
   241 #ifdef DEBUG_BUILD
   242     printf("[SDL_Quit] : SDL_UninstallParachute()\n");
   243     fflush(stdout);
   244 #endif
   245 
   246     /* Uninstall any parachute signal handlers */
   247     SDL_UninstallParachute();
   248 
   249     SDL_ClearHints();
   250     SDL_AssertionsQuit();
   251 
   252 #ifdef DEBUG_BUILD
   253     printf("[SDL_Quit] : Returning!\n");
   254     fflush(stdout);
   255 #endif
   256 
   257 }
   258 
   259 /* Get the library version number */
   260 void
   261 SDL_GetVersion(SDL_version * ver)
   262 {
   263     SDL_VERSION(ver);
   264 }
   265 
   266 /* Get the library source revision */
   267 const char *
   268 SDL_GetRevision(void)
   269 {
   270     return SDL_REVISION;
   271 }
   272 
   273 /* Get the name of the platform */
   274 const char *
   275 SDL_GetPlatform()
   276 {
   277 #if __AIX__
   278     return "AIX";
   279 #elif __HAIKU__
   280 /* Haiku must appear here before BeOS, since it also defines __BEOS__ */
   281     return "Haiku";
   282 #elif __BEOS__
   283     return "BeOS";
   284 #elif __BSDI__
   285     return "BSDI";
   286 #elif __DREAMCAST__
   287     return "Dreamcast";
   288 #elif __FREEBSD__
   289     return "FreeBSD";
   290 #elif __HPUX__
   291     return "HP-UX";
   292 #elif __IRIX__
   293     return "Irix";
   294 #elif __LINUX__
   295     return "Linux";
   296 #elif __MINT__
   297     return "Atari MiNT";
   298 #elif __MACOS__
   299     return "MacOS Classic";
   300 #elif __MACOSX__
   301     return "Mac OS X";
   302 #elif __NETBSD__
   303     return "NetBSD";
   304 #elif __OPENBSD__
   305     return "OpenBSD";
   306 #elif __OS2__
   307     return "OS/2";
   308 #elif __OSF__
   309     return "OSF/1";
   310 #elif __QNXNTO__
   311     return "QNX Neutrino";
   312 #elif __RISCOS__
   313     return "RISC OS";
   314 #elif __SOLARIS__
   315     return "Solaris";
   316 #elif __WIN32__
   317 #ifdef _WIN32_WCE
   318     return "Windows CE";
   319 #else
   320     return "Windows";
   321 #endif
   322 #elif __IPHONEOS__
   323     return "iPhone OS";
   324 #else
   325     return "Unknown (see SDL_platform.h)";
   326 #endif
   327 }
   328 
   329 #if defined(__WIN32__)
   330 
   331 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   332 /* Need to include DllMain() on Watcom C for some reason.. */
   333 #include "core/windows/SDL_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 /* __WIN32__ */
   351 
   352 /* vi: set ts=4 sw=4 expandtab: */