src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 24 Jan 2010 20:47:20 +0000
changeset 3696 47d923feedb0
parent 3663 bc50cd16bb07
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #935

Patrice Mandin

Hello,

I originally added pth support for threads in SDL 1.2 because on the Atari
platform we did not have any thread library.

I think pth support could be removed from SDL 1.3 for two reasons:

- Atari platform removed

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