src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Jan 2010 06:47:17 +0000
changeset 3647 c5925cd41955
parent 3319 63d4517fc4ac
child 3663 bc50cd16bb07
permissions -rw-r--r--
First pass at Ryan's assertion code, minor tweaks to come.
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@3647
   184
    /* brief sanity checks for the sanity checks.  :)  */
slouken@3647
   185
    SDL_assert(1);
slouken@3647
   186
    SDL_assert_release(1);
slouken@3647
   187
    SDL_assert_paranoid(1);
slouken@3647
   188
    SDL_assert(0 || 1);
slouken@3647
   189
    SDL_assert_release(0 || 1);
slouken@3647
   190
    SDL_assert_paranoid(0 || 1);
slouken@3647
   191
slouken@3647
   192
#if 0   /* enable this to test assertion failures. */
slouken@3647
   193
    SDL_assert_release(1 == 2);
slouken@3647
   194
    SDL_assert_release(5 < 4);
slouken@3647
   195
    SDL_assert_release(0 && "This is a test");
slouken@3647
   196
#endif
slouken@3647
   197
slouken@1895
   198
    return (0);
slouken@0
   199
}
slouken@0
   200
slouken@1895
   201
void
slouken@1895
   202
SDL_QuitSubSystem(Uint32 flags)
slouken@0
   203
{
slouken@1895
   204
    /* Shut down requested initialized subsystems */
slouken@1361
   205
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   206
    if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   207
        SDL_JoystickQuit();
slouken@1895
   208
        SDL_initialized &= ~SDL_INIT_JOYSTICK;
slouken@1895
   209
    }
slouken@0
   210
#endif
slouken@2713
   211
#if !SDL_HAPTIC_DISABLED
slouken@2713
   212
    if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   213
        SDL_HapticQuit();
slouken@2713
   214
        SDL_initialized &= ~SDL_INIT_HAPTIC;
slouken@2713
   215
    }
slouken@2713
   216
#endif
slouken@1361
   217
#if !SDL_TIMERS_DISABLED
slouken@1895
   218
    if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
   219
        SDL_TimerQuit();
slouken@1895
   220
        SDL_initialized &= ~SDL_INIT_TIMER;
slouken@1895
   221
    }
slouken@0
   222
#endif
slouken@1361
   223
#if !SDL_AUDIO_DISABLED
slouken@1895
   224
    if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1895
   225
        SDL_AudioQuit();
slouken@1895
   226
        SDL_initialized &= ~SDL_INIT_AUDIO;
slouken@1895
   227
    }
slouken@0
   228
#endif
slouken@1361
   229
#if !SDL_VIDEO_DISABLED
slouken@1895
   230
    if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
slouken@1895
   231
        SDL_VideoQuit();
slouken@1895
   232
        SDL_initialized &= ~SDL_INIT_VIDEO;
slouken@1895
   233
    }
slouken@0
   234
#endif
slouken@0
   235
}
slouken@0
   236
slouken@1895
   237
Uint32
slouken@1895
   238
SDL_WasInit(Uint32 flags)
slouken@0
   239
{
slouken@1895
   240
    if (!flags) {
slouken@1895
   241
        flags = SDL_INIT_EVERYTHING;
slouken@1895
   242
    }
slouken@1895
   243
    return (SDL_initialized & flags);
slouken@0
   244
}
slouken@0
   245
slouken@1895
   246
void
slouken@1895
   247
SDL_Quit(void)
slouken@0
   248
{
slouken@1895
   249
    /* Quit all subsystems */
icculus@1190
   250
#ifdef DEBUG_BUILD
slouken@1895
   251
    printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
slouken@1895
   252
    fflush(stdout);
icculus@1190
   253
#endif
slouken@2713
   254
slouken@2713
   255
#if defined(__WIN32__)
slouken@2713
   256
    SDL_HelperWindowDestroy();
slouken@2713
   257
#endif
slouken@1895
   258
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   259
slouken@0
   260
#ifdef CHECK_LEAKS
icculus@1190
   261
#ifdef DEBUG_BUILD
slouken@1895
   262
    printf("[SDL_Quit] : CHECK_LEAKS\n");
slouken@1895
   263
    fflush(stdout);
icculus@1190
   264
#endif
icculus@1190
   265
slouken@3647
   266
    /* !!! FIXME: make this an assertion. */
slouken@1895
   267
    /* Print the number of surfaces not freed */
slouken@1895
   268
    if (surfaces_allocated != 0) {
slouken@1895
   269
        fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
slouken@1895
   270
                surfaces_allocated);
slouken@1895
   271
    }
slouken@0
   272
#endif
icculus@1190
   273
#ifdef DEBUG_BUILD
slouken@1895
   274
    printf("[SDL_Quit] : SDL_UninstallParachute()\n");
slouken@1895
   275
    fflush(stdout);
icculus@1190
   276
#endif
slouken@0
   277
slouken@1895
   278
    /* Uninstall any parachute signal handlers */
slouken@1895
   279
    SDL_UninstallParachute();
slouken@397
   280
slouken@3647
   281
    SDL_AssertionsQuit();
slouken@3647
   282
slouken@1361
   283
#if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
slouken@1895
   284
    pth_kill();
slouken@397
   285
#endif
icculus@1190
   286
#ifdef DEBUG_BUILD
slouken@1895
   287
    printf("[SDL_Quit] : Returning!\n");
slouken@1895
   288
    fflush(stdout);
icculus@1190
   289
#endif
icculus@1190
   290
slouken@0
   291
}
slouken@0
   292
slouken@1895
   293
/* Get the library version number */
slouken@1895
   294
void
slouken@1895
   295
SDL_GetVersion(SDL_version * ver)
slouken@0
   296
{
slouken@1895
   297
    SDL_VERSION(ver);
slouken@0
   298
}
slouken@0
   299
slouken@2982
   300
/* Get the library source revision */
slouken@2982
   301
int
slouken@2996
   302
SDL_GetRevision(void)
slouken@2982
   303
{
slouken@2983
   304
    return SDL_REVISION;
slouken@2982
   305
}
slouken@2982
   306
slouken@3319
   307
/* Get the name of the platform */
slouken@3319
   308
const char *
slouken@3319
   309
SDL_GetPlatform()
slouken@3319
   310
{
slouken@3319
   311
#if __AIX__
slouken@3319
   312
    return "AIX";
slouken@3319
   313
#elif __HAIKU__
slouken@3319
   314
/* Haiku must appear here before BeOS, since it also defines __BEOS__ */
slouken@3319
   315
    return "Haiku";
slouken@3319
   316
#elif __BEOS__
slouken@3319
   317
    return "BeOS";
slouken@3319
   318
#elif __BSDI__
slouken@3319
   319
    return "BSDI";
slouken@3319
   320
#elif __DREAMCAST__
slouken@3319
   321
    return "Dreamcast";
slouken@3319
   322
#elif __FREEBSD__
slouken@3319
   323
    return "FreeBSD";
slouken@3319
   324
#elif __HPUX__
slouken@3319
   325
    return "HP-UX";
slouken@3319
   326
#elif __IRIX__
slouken@3319
   327
    return "Irix";
slouken@3319
   328
#elif __LINUX__
slouken@3319
   329
    return "Linux";
slouken@3319
   330
#elif __MINT__
slouken@3319
   331
    return "Atari MiNT";
slouken@3319
   332
#elif __MACOS__
slouken@3319
   333
    return "MacOS Classic";
slouken@3319
   334
#elif __MACOSX__
slouken@3319
   335
    return "Mac OS X";
slouken@3319
   336
#elif __NETBSD__
slouken@3319
   337
    return "NetBSD";
slouken@3319
   338
#elif __OPENBSD__
slouken@3319
   339
    return "OpenBSD";
slouken@3319
   340
#elif __OS2__
slouken@3319
   341
    return "OS/2";
slouken@3319
   342
#elif __OSF__
slouken@3319
   343
    return "OSF/1";
slouken@3319
   344
#elif __QNXNTO__
slouken@3319
   345
    return "QNX Neutrino";
slouken@3319
   346
#elif __RISCOS__
slouken@3319
   347
    return "RISC OS";
slouken@3319
   348
#elif __SOLARIS__
slouken@3319
   349
    return "Solaris";
slouken@3319
   350
#elif __WIN32__
slouken@3319
   351
#ifdef _WIN32_WCE
slouken@3319
   352
    return "Windows CE";
slouken@3319
   353
#else
slouken@3319
   354
    return "Windows";
slouken@3319
   355
#endif
slouken@3319
   356
#elif __IPHONEOS__
slouken@3319
   357
    return "iPhone OS";
slouken@3319
   358
#else
slouken@3319
   359
    return "Unknown (see SDL_platform.h)";
slouken@3319
   360
#endif
slouken@3319
   361
}
slouken@3319
   362
slouken@3269
   363
#if defined(__WIN32__)
slouken@1330
   364
slouken@1465
   365
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@1465
   366
/* Need to include DllMain() on Watcom C for some reason.. */
slouken@1433
   367
#define WIN32_LEAN_AND_MEAN
slouken@1433
   368
#include <windows.h>
slouken@1330
   369
slouken@1895
   370
BOOL APIENTRY
slouken@1895
   371
_DllMainCRTStartup(HANDLE hModule,
slouken@1895
   372
                   DWORD ul_reason_for_call, LPVOID lpReserved)
slouken@1330
   373
{
slouken@1895
   374
    switch (ul_reason_for_call) {
slouken@1895
   375
    case DLL_PROCESS_ATTACH:
slouken@1895
   376
    case DLL_THREAD_ATTACH:
slouken@1895
   377
    case DLL_THREAD_DETACH:
slouken@1895
   378
    case DLL_PROCESS_DETACH:
slouken@1895
   379
        break;
slouken@1895
   380
    }
slouken@1895
   381
    return TRUE;
slouken@1330
   382
}
slouken@1465
   383
#endif /* building DLL with Watcom C */
slouken@1330
   384
slouken@3269
   385
#endif /* __WIN32__ */
slouken@1895
   386
slouken@1895
   387
/* vi: set ts=4 sw=4 expandtab: */