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