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.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Initialization code for SDL */
slouken@0
    25
slouken@1361
    26
#include "SDL.h"
slouken@1361
    27
#include "SDL_fatal.h"
slouken@4472
    28
#include "SDL_assert_c.h"
slouken@4472
    29
#include "haptic/SDL_haptic_c.h"
slouken@4472
    30
#include "joystick/SDL_joystick_c.h"
slouken@3647
    31
slouken@0
    32
/* Initialization/Cleanup routines */
slouken@1361
    33
#if !SDL_TIMERS_DISABLED
slouken@0
    34
extern void SDL_StartTicks(void);
slouken@1895
    35
extern int SDL_TimerInit(void);
slouken@0
    36
extern void SDL_TimerQuit(void);
slouken@0
    37
#endif
slouken@5086
    38
#if defined(__WIN32__)
slouken@2713
    39
extern int SDL_HelperWindowCreate(void);
slouken@2713
    40
extern int SDL_HelperWindowDestroy(void);
slouken@2713
    41
#endif
slouken@0
    42
slouken@3647
    43
slouken@0
    44
/* The initialized subsystems */
slouken@0
    45
static Uint32 SDL_initialized = 0;
slouken@0
    46
static Uint32 ticks_started = 0;
slouken@0
    47
slouken@0
    48
slouken@1895
    49
int
slouken@1895
    50
SDL_InitSubSystem(Uint32 flags)
slouken@0
    51
{
slouken@1361
    52
#if !SDL_VIDEO_DISABLED
slouken@1895
    53
    /* Initialize the video/event subsystem */
slouken@1895
    54
    if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
slouken@5123
    55
        if (SDL_VideoInit(NULL) < 0) {
slouken@1895
    56
            return (-1);
slouken@1895
    57
        }
slouken@1895
    58
        SDL_initialized |= SDL_INIT_VIDEO;
slouken@1895
    59
    }
slouken@0
    60
#else
slouken@1895
    61
    if (flags & SDL_INIT_VIDEO) {
slouken@1895
    62
        SDL_SetError("SDL not built with video support");
slouken@1895
    63
        return (-1);
slouken@1895
    64
    }
slouken@0
    65
#endif
slouken@0
    66
slouken@1361
    67
#if !SDL_AUDIO_DISABLED
slouken@1895
    68
    /* Initialize the audio subsystem */
slouken@1895
    69
    if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1909
    70
        if (SDL_AudioInit(NULL) < 0) {
slouken@1895
    71
            return (-1);
slouken@1895
    72
        }
slouken@1895
    73
        SDL_initialized |= SDL_INIT_AUDIO;
slouken@1895
    74
    }
slouken@0
    75
#else
slouken@1895
    76
    if (flags & SDL_INIT_AUDIO) {
slouken@1895
    77
        SDL_SetError("SDL not built with audio support");
slouken@1895
    78
        return (-1);
slouken@1895
    79
    }
slouken@0
    80
#endif
slouken@0
    81
slouken@1361
    82
#if !SDL_TIMERS_DISABLED
slouken@1895
    83
    /* Initialize the timer subsystem */
slouken@1895
    84
    if (!ticks_started) {
slouken@1895
    85
        SDL_StartTicks();
slouken@1895
    86
        ticks_started = 1;
slouken@1895
    87
    }
slouken@1895
    88
    if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
    89
        if (SDL_TimerInit() < 0) {
slouken@1895
    90
            return (-1);
slouken@1895
    91
        }
slouken@1895
    92
        SDL_initialized |= SDL_INIT_TIMER;
slouken@1895
    93
    }
slouken@0
    94
#else
slouken@1895
    95
    if (flags & SDL_INIT_TIMER) {
slouken@1895
    96
        SDL_SetError("SDL not built with timer support");
slouken@1895
    97
        return (-1);
slouken@1895
    98
    }
slouken@0
    99
#endif
slouken@0
   100
slouken@1361
   101
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   102
    /* Initialize the joystick subsystem */
slouken@1895
   103
    if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   104
        if (SDL_JoystickInit() < 0) {
slouken@1895
   105
            return (-1);
slouken@1895
   106
        }
slouken@1895
   107
        SDL_initialized |= SDL_INIT_JOYSTICK;
slouken@1895
   108
    }
slouken@0
   109
#else
slouken@1895
   110
    if (flags & SDL_INIT_JOYSTICK) {
slouken@1895
   111
        SDL_SetError("SDL not built with joystick support");
slouken@1895
   112
        return (-1);
slouken@1895
   113
    }
slouken@0
   114
#endif
slouken@0
   115
slouken@2713
   116
#if !SDL_HAPTIC_DISABLED
slouken@2713
   117
    /* Initialize the haptic subsystem */
slouken@2713
   118
    if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   119
        if (SDL_HapticInit() < 0) {
slouken@2713
   120
            return (-1);
slouken@2713
   121
        }
slouken@2713
   122
        SDL_initialized |= SDL_INIT_HAPTIC;
slouken@2713
   123
    }
slouken@2713
   124
#else
slouken@2713
   125
    if (flags & SDL_INIT_HAPTIC) {
slouken@2713
   126
        SDL_SetError("SDL not built with haptic (force feedback) support");
slouken@2713
   127
        return (-1);
slouken@2713
   128
    }
slouken@2713
   129
#endif
slouken@1895
   130
    return (0);
slouken@0
   131
}
slouken@0
   132
slouken@1895
   133
int
slouken@1895
   134
SDL_Init(Uint32 flags)
slouken@0
   135
{
slouken@3647
   136
    if (SDL_AssertionsInit() < 0) {
slouken@3647
   137
        return -1;
slouken@3647
   138
    }
slouken@3647
   139
slouken@1895
   140
    /* Clear the error message */
slouken@1895
   141
    SDL_ClearError();
slouken@0
   142
slouken@5086
   143
#if defined(__WIN32__)
slouken@2713
   144
    if (SDL_HelperWindowCreate() < 0) {
slouken@2713
   145
        return -1;
slouken@2713
   146
    }
slouken@2713
   147
#endif
slouken@2713
   148
slouken@1895
   149
    /* Initialize the desired subsystems */
slouken@1895
   150
    if (SDL_InitSubSystem(flags) < 0) {
slouken@1895
   151
        return (-1);
slouken@1895
   152
    }
slouken@0
   153
slouken@1895
   154
    /* Everything is initialized */
slouken@1895
   155
    if (!(flags & SDL_INIT_NOPARACHUTE)) {
slouken@1895
   156
        SDL_InstallParachute();
slouken@1895
   157
    }
slouken@3647
   158
slouken@1895
   159
    return (0);
slouken@0
   160
}
slouken@0
   161
slouken@1895
   162
void
slouken@1895
   163
SDL_QuitSubSystem(Uint32 flags)
slouken@0
   164
{
slouken@1895
   165
    /* Shut down requested initialized subsystems */
slouken@1361
   166
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   167
    if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   168
        SDL_JoystickQuit();
slouken@1895
   169
        SDL_initialized &= ~SDL_INIT_JOYSTICK;
slouken@1895
   170
    }
slouken@0
   171
#endif
slouken@2713
   172
#if !SDL_HAPTIC_DISABLED
slouken@2713
   173
    if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   174
        SDL_HapticQuit();
slouken@2713
   175
        SDL_initialized &= ~SDL_INIT_HAPTIC;
slouken@2713
   176
    }
slouken@2713
   177
#endif
slouken@1361
   178
#if !SDL_TIMERS_DISABLED
slouken@1895
   179
    if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
   180
        SDL_TimerQuit();
slouken@1895
   181
        SDL_initialized &= ~SDL_INIT_TIMER;
slouken@1895
   182
    }
slouken@0
   183
#endif
slouken@1361
   184
#if !SDL_AUDIO_DISABLED
slouken@1895
   185
    if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1895
   186
        SDL_AudioQuit();
slouken@1895
   187
        SDL_initialized &= ~SDL_INIT_AUDIO;
slouken@1895
   188
    }
slouken@0
   189
#endif
slouken@1361
   190
#if !SDL_VIDEO_DISABLED
slouken@1895
   191
    if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
slouken@1895
   192
        SDL_VideoQuit();
slouken@1895
   193
        SDL_initialized &= ~SDL_INIT_VIDEO;
slouken@1895
   194
    }
slouken@0
   195
#endif
slouken@0
   196
}
slouken@0
   197
slouken@1895
   198
Uint32
slouken@1895
   199
SDL_WasInit(Uint32 flags)
slouken@0
   200
{
slouken@1895
   201
    if (!flags) {
slouken@1895
   202
        flags = SDL_INIT_EVERYTHING;
slouken@1895
   203
    }
slouken@1895
   204
    return (SDL_initialized & flags);
slouken@0
   205
}
slouken@0
   206
slouken@1895
   207
void
slouken@1895
   208
SDL_Quit(void)
slouken@0
   209
{
slouken@1895
   210
    /* Quit all subsystems */
slouken@5086
   211
#if defined(__WIN32__)
slouken@2713
   212
    SDL_HelperWindowDestroy();
slouken@2713
   213
#endif
slouken@1895
   214
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   215
slouken@1895
   216
    /* Uninstall any parachute signal handlers */
slouken@1895
   217
    SDL_UninstallParachute();
slouken@397
   218
slouken@5189
   219
    SDL_ClearHints();
slouken@3647
   220
    SDL_AssertionsQuit();
slouken@5221
   221
    SDL_LogResetPriorities();
slouken@0
   222
}
slouken@0
   223
slouken@1895
   224
/* Get the library version number */
slouken@1895
   225
void
slouken@1895
   226
SDL_GetVersion(SDL_version * ver)
slouken@0
   227
{
slouken@1895
   228
    SDL_VERSION(ver);
slouken@0
   229
}
slouken@0
   230
slouken@2982
   231
/* Get the library source revision */
icculus@4419
   232
const char *
slouken@2996
   233
SDL_GetRevision(void)
slouken@2982
   234
{
slouken@2983
   235
    return SDL_REVISION;
slouken@2982
   236
}
slouken@2982
   237
slouken@3319
   238
/* Get the name of the platform */
slouken@3319
   239
const char *
slouken@3319
   240
SDL_GetPlatform()
slouken@3319
   241
{
slouken@3319
   242
#if __AIX__
slouken@3319
   243
    return "AIX";
slouken@3319
   244
#elif __HAIKU__
slouken@3319
   245
/* Haiku must appear here before BeOS, since it also defines __BEOS__ */
slouken@3319
   246
    return "Haiku";
slouken@3319
   247
#elif __BEOS__
slouken@3319
   248
    return "BeOS";
slouken@3319
   249
#elif __BSDI__
slouken@3319
   250
    return "BSDI";
slouken@3319
   251
#elif __DREAMCAST__
slouken@3319
   252
    return "Dreamcast";
slouken@3319
   253
#elif __FREEBSD__
slouken@3319
   254
    return "FreeBSD";
slouken@3319
   255
#elif __HPUX__
slouken@3319
   256
    return "HP-UX";
slouken@3319
   257
#elif __IRIX__
slouken@3319
   258
    return "Irix";
slouken@3319
   259
#elif __LINUX__
slouken@3319
   260
    return "Linux";
slouken@3319
   261
#elif __MINT__
slouken@3319
   262
    return "Atari MiNT";
slouken@3319
   263
#elif __MACOS__
slouken@3319
   264
    return "MacOS Classic";
slouken@3319
   265
#elif __MACOSX__
slouken@3319
   266
    return "Mac OS X";
slouken@3319
   267
#elif __NETBSD__
slouken@3319
   268
    return "NetBSD";
slouken@5270
   269
#elif __NDS__
slouken@5270
   270
    return "Nintendo DS";
slouken@3319
   271
#elif __OPENBSD__
slouken@3319
   272
    return "OpenBSD";
slouken@3319
   273
#elif __OS2__
slouken@3319
   274
    return "OS/2";
slouken@3319
   275
#elif __OSF__
slouken@3319
   276
    return "OSF/1";
slouken@3319
   277
#elif __QNXNTO__
slouken@3319
   278
    return "QNX Neutrino";
slouken@3319
   279
#elif __RISCOS__
slouken@3319
   280
    return "RISC OS";
slouken@3319
   281
#elif __SOLARIS__
slouken@3319
   282
    return "Solaris";
slouken@5086
   283
#elif __WIN32__
slouken@3319
   284
#ifdef _WIN32_WCE
slouken@3319
   285
    return "Windows CE";
slouken@3319
   286
#else
slouken@3319
   287
    return "Windows";
slouken@3319
   288
#endif
slouken@3319
   289
#elif __IPHONEOS__
slouken@3319
   290
    return "iPhone OS";
slouken@3319
   291
#else
slouken@3319
   292
    return "Unknown (see SDL_platform.h)";
slouken@3319
   293
#endif
slouken@3319
   294
}
slouken@3319
   295
slouken@5086
   296
#if defined(__WIN32__)
slouken@1330
   297
slouken@1465
   298
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@1465
   299
/* Need to include DllMain() on Watcom C for some reason.. */
slouken@5090
   300
#include "core/windows/SDL_windows.h"
slouken@1330
   301
slouken@1895
   302
BOOL APIENTRY
slouken@1895
   303
_DllMainCRTStartup(HANDLE hModule,
slouken@1895
   304
                   DWORD ul_reason_for_call, LPVOID lpReserved)
slouken@1330
   305
{
slouken@1895
   306
    switch (ul_reason_for_call) {
slouken@1895
   307
    case DLL_PROCESS_ATTACH:
slouken@1895
   308
    case DLL_THREAD_ATTACH:
slouken@1895
   309
    case DLL_THREAD_DETACH:
slouken@1895
   310
    case DLL_PROCESS_DETACH:
slouken@1895
   311
        break;
slouken@1895
   312
    }
slouken@1895
   313
    return TRUE;
slouken@1330
   314
}
slouken@1465
   315
#endif /* building DLL with Watcom C */
slouken@1330
   316
slouken@5086
   317
#endif /* __WIN32__ */
slouken@1895
   318
slouken@1895
   319
/* vi: set ts=4 sw=4 expandtab: */