src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2009 05:27:13 +0000
changeset 2983 388c5a2daeac
parent 2982 b64c1d23039b
child 2996 972a69e47cd9
permissions -rw-r--r--
Fine tuned revision code, updated testver
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@1361
    28
#if !SDL_VIDEO_DISABLED
slouken@1361
    29
#include "video/SDL_leaks.h"
slouken@1361
    30
#endif
slouken@1361
    31
slouken@1361
    32
#if SDL_THREAD_PTH
slouken@557
    33
#include <pth.h>
slouken@557
    34
#endif
slouken@0
    35
slouken@0
    36
/* Initialization/Cleanup routines */
slouken@1361
    37
#if !SDL_JOYSTICK_DISABLED
slouken@1895
    38
extern int SDL_JoystickInit(void);
slouken@0
    39
extern void SDL_JoystickQuit(void);
slouken@0
    40
#endif
slouken@2713
    41
#if !SDL_HAPTIC_DISABLED
slouken@2713
    42
extern int SDL_HapticInit(void);
slouken@2713
    43
extern int SDL_HapticQuit(void);
slouken@2713
    44
#endif
slouken@1361
    45
#if !SDL_CDROM_DISABLED
slouken@1895
    46
extern int SDL_CDROMInit(void);
slouken@0
    47
extern void SDL_CDROMQuit(void);
slouken@0
    48
#endif
slouken@1361
    49
#if !SDL_TIMERS_DISABLED
slouken@0
    50
extern void SDL_StartTicks(void);
slouken@1895
    51
extern int SDL_TimerInit(void);
slouken@0
    52
extern void SDL_TimerQuit(void);
slouken@0
    53
#endif
slouken@2713
    54
#if defined(__WIN32__)
slouken@2713
    55
extern int SDL_HelperWindowCreate(void);
slouken@2713
    56
extern int SDL_HelperWindowDestroy(void);
slouken@2713
    57
#endif
slouken@0
    58
slouken@0
    59
/* The initialized subsystems */
slouken@0
    60
static Uint32 SDL_initialized = 0;
slouken@0
    61
static Uint32 ticks_started = 0;
slouken@0
    62
slouken@0
    63
#ifdef CHECK_LEAKS
slouken@0
    64
int surfaces_allocated = 0;
slouken@0
    65
#endif
slouken@0
    66
slouken@1895
    67
int
slouken@1895
    68
SDL_InitSubSystem(Uint32 flags)
slouken@0
    69
{
slouken@1361
    70
#if !SDL_VIDEO_DISABLED
slouken@1895
    71
    /* Initialize the video/event subsystem */
slouken@1895
    72
    if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
slouken@1909
    73
        if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
slouken@1895
    74
            return (-1);
slouken@1895
    75
        }
slouken@1895
    76
        SDL_initialized |= SDL_INIT_VIDEO;
slouken@1895
    77
    }
slouken@0
    78
#else
slouken@1895
    79
    if (flags & SDL_INIT_VIDEO) {
slouken@1895
    80
        SDL_SetError("SDL not built with video support");
slouken@1895
    81
        return (-1);
slouken@1895
    82
    }
slouken@0
    83
#endif
slouken@0
    84
slouken@1361
    85
#if !SDL_AUDIO_DISABLED
slouken@1895
    86
    /* Initialize the audio subsystem */
slouken@1895
    87
    if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1909
    88
        if (SDL_AudioInit(NULL) < 0) {
slouken@1895
    89
            return (-1);
slouken@1895
    90
        }
slouken@1895
    91
        SDL_initialized |= SDL_INIT_AUDIO;
slouken@1895
    92
    }
slouken@0
    93
#else
slouken@1895
    94
    if (flags & SDL_INIT_AUDIO) {
slouken@1895
    95
        SDL_SetError("SDL not built with audio support");
slouken@1895
    96
        return (-1);
slouken@1895
    97
    }
slouken@0
    98
#endif
slouken@0
    99
slouken@1361
   100
#if !SDL_TIMERS_DISABLED
slouken@1895
   101
    /* Initialize the timer subsystem */
slouken@1895
   102
    if (!ticks_started) {
slouken@1895
   103
        SDL_StartTicks();
slouken@1895
   104
        ticks_started = 1;
slouken@1895
   105
    }
slouken@1895
   106
    if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
   107
        if (SDL_TimerInit() < 0) {
slouken@1895
   108
            return (-1);
slouken@1895
   109
        }
slouken@1895
   110
        SDL_initialized |= SDL_INIT_TIMER;
slouken@1895
   111
    }
slouken@0
   112
#else
slouken@1895
   113
    if (flags & SDL_INIT_TIMER) {
slouken@1895
   114
        SDL_SetError("SDL not built with timer support");
slouken@1895
   115
        return (-1);
slouken@1895
   116
    }
slouken@0
   117
#endif
slouken@0
   118
slouken@1361
   119
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   120
    /* Initialize the joystick subsystem */
slouken@1895
   121
    if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   122
        if (SDL_JoystickInit() < 0) {
slouken@1895
   123
            return (-1);
slouken@1895
   124
        }
slouken@1895
   125
        SDL_initialized |= SDL_INIT_JOYSTICK;
slouken@1895
   126
    }
slouken@0
   127
#else
slouken@1895
   128
    if (flags & SDL_INIT_JOYSTICK) {
slouken@1895
   129
        SDL_SetError("SDL not built with joystick support");
slouken@1895
   130
        return (-1);
slouken@1895
   131
    }
slouken@0
   132
#endif
slouken@0
   133
slouken@2713
   134
#if !SDL_HAPTIC_DISABLED
slouken@2713
   135
    /* Initialize the haptic subsystem */
slouken@2713
   136
    if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   137
        if (SDL_HapticInit() < 0) {
slouken@2713
   138
            return (-1);
slouken@2713
   139
        }
slouken@2713
   140
        SDL_initialized |= SDL_INIT_HAPTIC;
slouken@2713
   141
    }
slouken@2713
   142
#else
slouken@2713
   143
    if (flags & SDL_INIT_HAPTIC) {
slouken@2713
   144
        SDL_SetError("SDL not built with haptic (force feedback) support");
slouken@2713
   145
        return (-1);
slouken@2713
   146
    }
slouken@2713
   147
#endif
slouken@2713
   148
slouken@2713
   149
slouken@1361
   150
#if !SDL_CDROM_DISABLED
slouken@1895
   151
    /* Initialize the CD-ROM subsystem */
slouken@1895
   152
    if ((flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM)) {
slouken@1895
   153
        if (SDL_CDROMInit() < 0) {
slouken@1895
   154
            return (-1);
slouken@1895
   155
        }
slouken@1895
   156
        SDL_initialized |= SDL_INIT_CDROM;
slouken@1895
   157
    }
slouken@0
   158
#else
slouken@1895
   159
    if (flags & SDL_INIT_CDROM) {
slouken@1895
   160
        SDL_SetError("SDL not built with cdrom support");
slouken@1895
   161
        return (-1);
slouken@1895
   162
    }
slouken@0
   163
#endif
slouken@1895
   164
    return (0);
slouken@0
   165
}
slouken@0
   166
slouken@1895
   167
int
slouken@1895
   168
SDL_Init(Uint32 flags)
slouken@0
   169
{
slouken@1361
   170
#if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
slouken@1895
   171
    if (!pth_init()) {
slouken@1895
   172
        return -1;
slouken@1895
   173
    }
slouken@397
   174
#endif
slouken@397
   175
slouken@1895
   176
    /* Clear the error message */
slouken@1895
   177
    SDL_ClearError();
slouken@0
   178
slouken@2713
   179
#if defined(__WIN32__)
slouken@2713
   180
    if (SDL_HelperWindowCreate() < 0) {
slouken@2713
   181
        return -1;
slouken@2713
   182
    }
slouken@2713
   183
#endif
slouken@2713
   184
slouken@1895
   185
    /* Initialize the desired subsystems */
slouken@1895
   186
    if (SDL_InitSubSystem(flags) < 0) {
slouken@1895
   187
        return (-1);
slouken@1895
   188
    }
slouken@0
   189
slouken@1895
   190
    /* Everything is initialized */
slouken@1895
   191
    if (!(flags & SDL_INIT_NOPARACHUTE)) {
slouken@1895
   192
        SDL_InstallParachute();
slouken@1895
   193
    }
slouken@1895
   194
    return (0);
slouken@0
   195
}
slouken@0
   196
slouken@1895
   197
void
slouken@1895
   198
SDL_QuitSubSystem(Uint32 flags)
slouken@0
   199
{
slouken@1895
   200
    /* Shut down requested initialized subsystems */
slouken@1361
   201
#if !SDL_CDROM_DISABLED
slouken@1895
   202
    if ((flags & SDL_initialized & SDL_INIT_CDROM)) {
slouken@1895
   203
        SDL_CDROMQuit();
slouken@1895
   204
        SDL_initialized &= ~SDL_INIT_CDROM;
slouken@1895
   205
    }
slouken@0
   206
#endif
slouken@1361
   207
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   208
    if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   209
        SDL_JoystickQuit();
slouken@1895
   210
        SDL_initialized &= ~SDL_INIT_JOYSTICK;
slouken@1895
   211
    }
slouken@0
   212
#endif
slouken@2713
   213
#if !SDL_HAPTIC_DISABLED
slouken@2713
   214
    if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   215
        SDL_HapticQuit();
slouken@2713
   216
        SDL_initialized &= ~SDL_INIT_HAPTIC;
slouken@2713
   217
    }
slouken@2713
   218
#endif
slouken@1361
   219
#if !SDL_TIMERS_DISABLED
slouken@1895
   220
    if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
   221
        SDL_TimerQuit();
slouken@1895
   222
        SDL_initialized &= ~SDL_INIT_TIMER;
slouken@1895
   223
    }
slouken@0
   224
#endif
slouken@1361
   225
#if !SDL_AUDIO_DISABLED
slouken@1895
   226
    if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1895
   227
        SDL_AudioQuit();
slouken@1895
   228
        SDL_initialized &= ~SDL_INIT_AUDIO;
slouken@1895
   229
    }
slouken@0
   230
#endif
slouken@1361
   231
#if !SDL_VIDEO_DISABLED
slouken@1895
   232
    if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
slouken@1895
   233
        SDL_VideoQuit();
slouken@1895
   234
        SDL_initialized &= ~SDL_INIT_VIDEO;
slouken@1895
   235
    }
slouken@0
   236
#endif
slouken@0
   237
}
slouken@0
   238
slouken@1895
   239
Uint32
slouken@1895
   240
SDL_WasInit(Uint32 flags)
slouken@0
   241
{
slouken@1895
   242
    if (!flags) {
slouken@1895
   243
        flags = SDL_INIT_EVERYTHING;
slouken@1895
   244
    }
slouken@1895
   245
    return (SDL_initialized & flags);
slouken@0
   246
}
slouken@0
   247
slouken@1895
   248
void
slouken@1895
   249
SDL_Quit(void)
slouken@0
   250
{
slouken@1895
   251
    /* Quit all subsystems */
icculus@1190
   252
#ifdef DEBUG_BUILD
slouken@1895
   253
    printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
slouken@1895
   254
    fflush(stdout);
icculus@1190
   255
#endif
slouken@2713
   256
slouken@2713
   257
#if defined(__WIN32__)
slouken@2713
   258
    SDL_HelperWindowDestroy();
slouken@2713
   259
#endif
slouken@1895
   260
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   261
slouken@0
   262
#ifdef CHECK_LEAKS
icculus@1190
   263
#ifdef DEBUG_BUILD
slouken@1895
   264
    printf("[SDL_Quit] : CHECK_LEAKS\n");
slouken@1895
   265
    fflush(stdout);
icculus@1190
   266
#endif
icculus@1190
   267
slouken@1895
   268
    /* Print the number of surfaces not freed */
slouken@1895
   269
    if (surfaces_allocated != 0) {
slouken@1895
   270
        fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
slouken@1895
   271
                surfaces_allocated);
slouken@1895
   272
    }
slouken@0
   273
#endif
icculus@1190
   274
#ifdef DEBUG_BUILD
slouken@1895
   275
    printf("[SDL_Quit] : SDL_UninstallParachute()\n");
slouken@1895
   276
    fflush(stdout);
icculus@1190
   277
#endif
slouken@0
   278
slouken@1895
   279
    /* Uninstall any parachute signal handlers */
slouken@1895
   280
    SDL_UninstallParachute();
slouken@397
   281
slouken@1361
   282
#if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
slouken@1895
   283
    pth_kill();
slouken@397
   284
#endif
icculus@1190
   285
#ifdef DEBUG_BUILD
slouken@1895
   286
    printf("[SDL_Quit] : Returning!\n");
slouken@1895
   287
    fflush(stdout);
icculus@1190
   288
#endif
icculus@1190
   289
slouken@0
   290
}
slouken@0
   291
slouken@1895
   292
/* Get the library version number */
slouken@1895
   293
void
slouken@1895
   294
SDL_GetVersion(SDL_version * ver)
slouken@0
   295
{
slouken@1895
   296
    SDL_VERSION(ver);
slouken@0
   297
}
slouken@0
   298
slouken@2982
   299
/* Get the library source revision */
slouken@2982
   300
int
slouken@2982
   301
SDL_GetRevision()
slouken@2982
   302
{
slouken@2983
   303
    return SDL_REVISION;
slouken@2982
   304
}
slouken@2982
   305
slouken@1330
   306
#if defined(__OS2__)
slouken@1487
   307
/* Building for OS/2 */
icculus@1190
   308
#ifdef __WATCOMC__
icculus@1190
   309
icculus@1190
   310
#define INCL_DOSERRORS
icculus@1190
   311
#define INCL_DOSEXCEPTIONS
icculus@1190
   312
#include <os2.h>
icculus@1190
   313
slouken@1487
   314
/* Exception handler to prevent the Audio thread hanging, making a zombie process! */
slouken@1895
   315
ULONG _System
slouken@1895
   316
SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
slouken@1895
   317
                          PEXCEPTIONREGISTRATIONRECORD pERegRec,
slouken@1895
   318
                          PCONTEXTRECORD pCtxRec, PVOID p)
icculus@1190
   319
{
slouken@1895
   320
    if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
slouken@1895
   321
        return XCPT_CONTINUE_SEARCH;
slouken@1895
   322
    if (pERepRec->fHandlerFlags & EH_UNWINDING)
slouken@1895
   323
        return XCPT_CONTINUE_SEARCH;
slouken@1895
   324
    if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
slouken@1895
   325
        return XCPT_CONTINUE_SEARCH;
icculus@1190
   326
slouken@1895
   327
    /* Do cleanup at every fatal exception! */
slouken@1895
   328
    if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) ==
slouken@1895
   329
         XCPT_FATAL_EXCEPTION) && (pERepRec->ExceptionNum != XCPT_BREAKPOINT)
slouken@1895
   330
        && (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)) {
slouken@1895
   331
        if (SDL_initialized & SDL_INIT_AUDIO) {
slouken@1895
   332
            /* This removes the zombie audio thread in case of emergency. */
icculus@1190
   333
#ifdef DEBUG_BUILD
slouken@1895
   334
            printf
slouken@1895
   335
                ("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
icculus@1190
   336
#endif
slouken@1895
   337
            SDL_CloseAudio();
slouken@1895
   338
        }
icculus@1190
   339
    }
slouken@1895
   340
    return (XCPT_CONTINUE_SEARCH);
icculus@1190
   341
}
icculus@1190
   342
icculus@1190
   343
slouken@1895
   344
EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand =
slouken@1895
   345
    { 0, SDL_Main_ExceptionHandler };
icculus@1190
   346
slouken@1487
   347
/* The main DLL entry for DLL Initialization and Uninitialization: */
slouken@1895
   348
unsigned _System
slouken@1895
   349
LibMain(unsigned hmod, unsigned termination)
icculus@1190
   350
{
slouken@1895
   351
    if (termination) {
icculus@1190
   352
#ifdef DEBUG_BUILD
slouken@1487
   353
/*    printf("[SDL DLL Unintialization] : Removing exception handler\n"); */
icculus@1190
   354
#endif
slouken@1895
   355
        DosUnsetExceptionHandler(&SDL_Main_xcpthand);
slouken@1895
   356
        return 1;
slouken@1895
   357
    } else {
icculus@1190
   358
#ifdef DEBUG_BUILD
slouken@1895
   359
        /* Make stdout and stderr unbuffered! */
slouken@1895
   360
        setbuf(stdout, NULL);
slouken@1895
   361
        setbuf(stderr, NULL);
icculus@1190
   362
#endif
slouken@1895
   363
        /* Fire up exception handler */
icculus@1190
   364
#ifdef DEBUG_BUILD
slouken@1487
   365
/*    printf("[SDL DLL Initialization] : Setting exception handler\n"); */
icculus@1190
   366
#endif
slouken@1895
   367
        /* Set exception handler */
slouken@1895
   368
        DosSetExceptionHandler(&SDL_Main_xcpthand);
icculus@1190
   369
slouken@1895
   370
        return 1;
slouken@1895
   371
    }
icculus@1190
   372
}
slouken@1330
   373
#endif /* __WATCOMC__ */
icculus@1190
   374
slouken@1402
   375
#elif defined(__WIN32__)
slouken@1330
   376
slouken@1465
   377
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@1465
   378
/* Need to include DllMain() on Watcom C for some reason.. */
slouken@1433
   379
#define WIN32_LEAN_AND_MEAN
slouken@1433
   380
#include <windows.h>
slouken@1330
   381
slouken@1895
   382
BOOL APIENTRY
slouken@1895
   383
_DllMainCRTStartup(HANDLE hModule,
slouken@1895
   384
                   DWORD ul_reason_for_call, LPVOID lpReserved)
slouken@1330
   385
{
slouken@1895
   386
    switch (ul_reason_for_call) {
slouken@1895
   387
    case DLL_PROCESS_ATTACH:
slouken@1895
   388
    case DLL_THREAD_ATTACH:
slouken@1895
   389
    case DLL_THREAD_DETACH:
slouken@1895
   390
    case DLL_PROCESS_DETACH:
slouken@1895
   391
        break;
slouken@1895
   392
    }
slouken@1895
   393
    return TRUE;
slouken@1330
   394
}
slouken@1465
   395
#endif /* building DLL with Watcom C */
slouken@1330
   396
slouken@1402
   397
#endif /* OS/2 elif __WIN32__ */
slouken@1895
   398
slouken@1895
   399
/* vi: set ts=4 sw=4 expandtab: */