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