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