src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 12 Feb 2011 11:36:56 -0800
changeset 5270 7aba0406c273
parent 5262 b530ef003506
child 5310 a9945ce48999
permissions -rw-r--r--
Frank Zago to sdl

The following patch fixes some of the bitrot for the Nintendo DS port.
The support is still basic at the moment, but it allows to run the "general"
test under the current head of tree (parent: 5269:11bd1585efb5 tip).
Most of the patch is mine, but I integrated a couple changes that John
Magnotti posted on Feb 1st.
     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 __NDS__
   270     return "Nintendo DS";
   271 #elif __OPENBSD__
   272     return "OpenBSD";
   273 #elif __OS2__
   274     return "OS/2";
   275 #elif __OSF__
   276     return "OSF/1";
   277 #elif __QNXNTO__
   278     return "QNX Neutrino";
   279 #elif __RISCOS__
   280     return "RISC OS";
   281 #elif __SOLARIS__
   282     return "Solaris";
   283 #elif __WIN32__
   284 #ifdef _WIN32_WCE
   285     return "Windows CE";
   286 #else
   287     return "Windows";
   288 #endif
   289 #elif __IPHONEOS__
   290     return "iPhone OS";
   291 #else
   292     return "Unknown (see SDL_platform.h)";
   293 #endif
   294 }
   295 
   296 #if defined(__WIN32__)
   297 
   298 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   299 /* Need to include DllMain() on Watcom C for some reason.. */
   300 #include "core/windows/SDL_windows.h"
   301 
   302 BOOL APIENTRY
   303 _DllMainCRTStartup(HANDLE hModule,
   304                    DWORD ul_reason_for_call, LPVOID lpReserved)
   305 {
   306     switch (ul_reason_for_call) {
   307     case DLL_PROCESS_ATTACH:
   308     case DLL_THREAD_ATTACH:
   309     case DLL_THREAD_DETACH:
   310     case DLL_PROCESS_DETACH:
   311         break;
   312     }
   313     return TRUE;
   314 }
   315 #endif /* building DLL with Watcom C */
   316 
   317 #endif /* __WIN32__ */
   318 
   319 /* vi: set ts=4 sw=4 expandtab: */