src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 24 Jan 2010 20:47:20 +0000
changeset 3696 47d923feedb0
parent 3663 bc50cd16bb07
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #935

Patrice Mandin

Hello,

I originally added pth support for threads in SDL 1.2 because on the Atari
platform we did not have any thread library.

I think pth support could be removed from SDL 1.3 for two reasons:

- Atari platform removed

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