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