src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Feb 2011 10:42:51 -0800
changeset 5359 d5fe4ed3a28d
parent 5310 a9945ce48999
child 5535 96594ac5fd1a
permissions -rw-r--r--
Added a revision number for easy compile-time tests.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 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@5310
    27
#include "SDL_revision.h"
slouken@1361
    28
#include "SDL_fatal.h"
slouken@4472
    29
#include "SDL_assert_c.h"
slouken@4472
    30
#include "haptic/SDL_haptic_c.h"
slouken@4472
    31
#include "joystick/SDL_joystick_c.h"
slouken@3647
    32
slouken@0
    33
/* Initialization/Cleanup routines */
slouken@1361
    34
#if !SDL_TIMERS_DISABLED
slouken@0
    35
extern void SDL_StartTicks(void);
slouken@1895
    36
extern int SDL_TimerInit(void);
slouken@0
    37
extern void SDL_TimerQuit(void);
slouken@0
    38
#endif
slouken@5086
    39
#if defined(__WIN32__)
slouken@2713
    40
extern int SDL_HelperWindowCreate(void);
slouken@2713
    41
extern int SDL_HelperWindowDestroy(void);
slouken@2713
    42
#endif
slouken@0
    43
slouken@3647
    44
slouken@0
    45
/* The initialized subsystems */
slouken@0
    46
static Uint32 SDL_initialized = 0;
slouken@0
    47
static Uint32 ticks_started = 0;
slouken@0
    48
slouken@0
    49
slouken@1895
    50
int
slouken@1895
    51
SDL_InitSubSystem(Uint32 flags)
slouken@0
    52
{
slouken@1361
    53
#if !SDL_VIDEO_DISABLED
slouken@1895
    54
    /* Initialize the video/event subsystem */
slouken@1895
    55
    if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
slouken@5123
    56
        if (SDL_VideoInit(NULL) < 0) {
slouken@1895
    57
            return (-1);
slouken@1895
    58
        }
slouken@1895
    59
        SDL_initialized |= SDL_INIT_VIDEO;
slouken@1895
    60
    }
slouken@0
    61
#else
slouken@1895
    62
    if (flags & SDL_INIT_VIDEO) {
slouken@1895
    63
        SDL_SetError("SDL not built with video support");
slouken@1895
    64
        return (-1);
slouken@1895
    65
    }
slouken@0
    66
#endif
slouken@0
    67
slouken@1361
    68
#if !SDL_AUDIO_DISABLED
slouken@1895
    69
    /* Initialize the audio subsystem */
slouken@1895
    70
    if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1909
    71
        if (SDL_AudioInit(NULL) < 0) {
slouken@1895
    72
            return (-1);
slouken@1895
    73
        }
slouken@1895
    74
        SDL_initialized |= SDL_INIT_AUDIO;
slouken@1895
    75
    }
slouken@0
    76
#else
slouken@1895
    77
    if (flags & SDL_INIT_AUDIO) {
slouken@1895
    78
        SDL_SetError("SDL not built with audio support");
slouken@1895
    79
        return (-1);
slouken@1895
    80
    }
slouken@0
    81
#endif
slouken@0
    82
slouken@1361
    83
#if !SDL_TIMERS_DISABLED
slouken@1895
    84
    /* Initialize the timer subsystem */
slouken@1895
    85
    if (!ticks_started) {
slouken@1895
    86
        SDL_StartTicks();
slouken@1895
    87
        ticks_started = 1;
slouken@1895
    88
    }
slouken@1895
    89
    if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
    90
        if (SDL_TimerInit() < 0) {
slouken@1895
    91
            return (-1);
slouken@1895
    92
        }
slouken@1895
    93
        SDL_initialized |= SDL_INIT_TIMER;
slouken@1895
    94
    }
slouken@0
    95
#else
slouken@1895
    96
    if (flags & SDL_INIT_TIMER) {
slouken@1895
    97
        SDL_SetError("SDL not built with timer support");
slouken@1895
    98
        return (-1);
slouken@1895
    99
    }
slouken@0
   100
#endif
slouken@0
   101
slouken@1361
   102
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   103
    /* Initialize the joystick subsystem */
slouken@1895
   104
    if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   105
        if (SDL_JoystickInit() < 0) {
slouken@1895
   106
            return (-1);
slouken@1895
   107
        }
slouken@1895
   108
        SDL_initialized |= SDL_INIT_JOYSTICK;
slouken@1895
   109
    }
slouken@0
   110
#else
slouken@1895
   111
    if (flags & SDL_INIT_JOYSTICK) {
slouken@1895
   112
        SDL_SetError("SDL not built with joystick support");
slouken@1895
   113
        return (-1);
slouken@1895
   114
    }
slouken@0
   115
#endif
slouken@0
   116
slouken@2713
   117
#if !SDL_HAPTIC_DISABLED
slouken@2713
   118
    /* Initialize the haptic subsystem */
slouken@2713
   119
    if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   120
        if (SDL_HapticInit() < 0) {
slouken@2713
   121
            return (-1);
slouken@2713
   122
        }
slouken@2713
   123
        SDL_initialized |= SDL_INIT_HAPTIC;
slouken@2713
   124
    }
slouken@2713
   125
#else
slouken@2713
   126
    if (flags & SDL_INIT_HAPTIC) {
slouken@2713
   127
        SDL_SetError("SDL not built with haptic (force feedback) support");
slouken@2713
   128
        return (-1);
slouken@2713
   129
    }
slouken@2713
   130
#endif
slouken@1895
   131
    return (0);
slouken@0
   132
}
slouken@0
   133
slouken@1895
   134
int
slouken@1895
   135
SDL_Init(Uint32 flags)
slouken@0
   136
{
slouken@3647
   137
    if (SDL_AssertionsInit() < 0) {
slouken@3647
   138
        return -1;
slouken@3647
   139
    }
slouken@3647
   140
slouken@1895
   141
    /* Clear the error message */
slouken@1895
   142
    SDL_ClearError();
slouken@0
   143
slouken@5086
   144
#if defined(__WIN32__)
slouken@2713
   145
    if (SDL_HelperWindowCreate() < 0) {
slouken@2713
   146
        return -1;
slouken@2713
   147
    }
slouken@2713
   148
#endif
slouken@2713
   149
slouken@1895
   150
    /* Initialize the desired subsystems */
slouken@1895
   151
    if (SDL_InitSubSystem(flags) < 0) {
slouken@1895
   152
        return (-1);
slouken@1895
   153
    }
slouken@0
   154
slouken@1895
   155
    /* Everything is initialized */
slouken@1895
   156
    if (!(flags & SDL_INIT_NOPARACHUTE)) {
slouken@1895
   157
        SDL_InstallParachute();
slouken@1895
   158
    }
slouken@3647
   159
slouken@1895
   160
    return (0);
slouken@0
   161
}
slouken@0
   162
slouken@1895
   163
void
slouken@1895
   164
SDL_QuitSubSystem(Uint32 flags)
slouken@0
   165
{
slouken@1895
   166
    /* Shut down requested initialized subsystems */
slouken@1361
   167
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   168
    if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   169
        SDL_JoystickQuit();
slouken@1895
   170
        SDL_initialized &= ~SDL_INIT_JOYSTICK;
slouken@1895
   171
    }
slouken@0
   172
#endif
slouken@2713
   173
#if !SDL_HAPTIC_DISABLED
slouken@2713
   174
    if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   175
        SDL_HapticQuit();
slouken@2713
   176
        SDL_initialized &= ~SDL_INIT_HAPTIC;
slouken@2713
   177
    }
slouken@2713
   178
#endif
slouken@1361
   179
#if !SDL_TIMERS_DISABLED
slouken@1895
   180
    if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
   181
        SDL_TimerQuit();
slouken@1895
   182
        SDL_initialized &= ~SDL_INIT_TIMER;
slouken@1895
   183
    }
slouken@0
   184
#endif
slouken@1361
   185
#if !SDL_AUDIO_DISABLED
slouken@1895
   186
    if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1895
   187
        SDL_AudioQuit();
slouken@1895
   188
        SDL_initialized &= ~SDL_INIT_AUDIO;
slouken@1895
   189
    }
slouken@0
   190
#endif
slouken@1361
   191
#if !SDL_VIDEO_DISABLED
slouken@1895
   192
    if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
slouken@1895
   193
        SDL_VideoQuit();
slouken@1895
   194
        SDL_initialized &= ~SDL_INIT_VIDEO;
slouken@1895
   195
    }
slouken@0
   196
#endif
slouken@0
   197
}
slouken@0
   198
slouken@1895
   199
Uint32
slouken@1895
   200
SDL_WasInit(Uint32 flags)
slouken@0
   201
{
slouken@1895
   202
    if (!flags) {
slouken@1895
   203
        flags = SDL_INIT_EVERYTHING;
slouken@1895
   204
    }
slouken@1895
   205
    return (SDL_initialized & flags);
slouken@0
   206
}
slouken@0
   207
slouken@1895
   208
void
slouken@1895
   209
SDL_Quit(void)
slouken@0
   210
{
slouken@1895
   211
    /* Quit all subsystems */
slouken@5086
   212
#if defined(__WIN32__)
slouken@2713
   213
    SDL_HelperWindowDestroy();
slouken@2713
   214
#endif
slouken@1895
   215
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   216
slouken@1895
   217
    /* Uninstall any parachute signal handlers */
slouken@1895
   218
    SDL_UninstallParachute();
slouken@397
   219
slouken@5189
   220
    SDL_ClearHints();
slouken@3647
   221
    SDL_AssertionsQuit();
slouken@5221
   222
    SDL_LogResetPriorities();
slouken@0
   223
}
slouken@0
   224
slouken@1895
   225
/* Get the library version number */
slouken@1895
   226
void
slouken@1895
   227
SDL_GetVersion(SDL_version * ver)
slouken@0
   228
{
slouken@1895
   229
    SDL_VERSION(ver);
slouken@0
   230
}
slouken@0
   231
slouken@2982
   232
/* Get the library source revision */
icculus@4419
   233
const char *
slouken@2996
   234
SDL_GetRevision(void)
slouken@2982
   235
{
slouken@2983
   236
    return SDL_REVISION;
slouken@2982
   237
}
slouken@2982
   238
slouken@5359
   239
/* Get the library source revision number */
slouken@5359
   240
int
slouken@5359
   241
SDL_GetRevisionNumber(void)
slouken@5359
   242
{
slouken@5359
   243
    return SDL_REVISION_NUMBER;
slouken@5359
   244
}
slouken@5359
   245
slouken@3319
   246
/* Get the name of the platform */
slouken@3319
   247
const char *
slouken@3319
   248
SDL_GetPlatform()
slouken@3319
   249
{
slouken@3319
   250
#if __AIX__
slouken@3319
   251
    return "AIX";
slouken@3319
   252
#elif __HAIKU__
slouken@3319
   253
/* Haiku must appear here before BeOS, since it also defines __BEOS__ */
slouken@3319
   254
    return "Haiku";
slouken@3319
   255
#elif __BEOS__
slouken@3319
   256
    return "BeOS";
slouken@3319
   257
#elif __BSDI__
slouken@3319
   258
    return "BSDI";
slouken@3319
   259
#elif __DREAMCAST__
slouken@3319
   260
    return "Dreamcast";
slouken@3319
   261
#elif __FREEBSD__
slouken@3319
   262
    return "FreeBSD";
slouken@3319
   263
#elif __HPUX__
slouken@3319
   264
    return "HP-UX";
slouken@3319
   265
#elif __IRIX__
slouken@3319
   266
    return "Irix";
slouken@3319
   267
#elif __LINUX__
slouken@3319
   268
    return "Linux";
slouken@3319
   269
#elif __MINT__
slouken@3319
   270
    return "Atari MiNT";
slouken@3319
   271
#elif __MACOS__
slouken@3319
   272
    return "MacOS Classic";
slouken@3319
   273
#elif __MACOSX__
slouken@3319
   274
    return "Mac OS X";
slouken@3319
   275
#elif __NETBSD__
slouken@3319
   276
    return "NetBSD";
slouken@5270
   277
#elif __NDS__
slouken@5270
   278
    return "Nintendo DS";
slouken@3319
   279
#elif __OPENBSD__
slouken@3319
   280
    return "OpenBSD";
slouken@3319
   281
#elif __OS2__
slouken@3319
   282
    return "OS/2";
slouken@3319
   283
#elif __OSF__
slouken@3319
   284
    return "OSF/1";
slouken@3319
   285
#elif __QNXNTO__
slouken@3319
   286
    return "QNX Neutrino";
slouken@3319
   287
#elif __RISCOS__
slouken@3319
   288
    return "RISC OS";
slouken@3319
   289
#elif __SOLARIS__
slouken@3319
   290
    return "Solaris";
slouken@5086
   291
#elif __WIN32__
slouken@3319
   292
#ifdef _WIN32_WCE
slouken@3319
   293
    return "Windows CE";
slouken@3319
   294
#else
slouken@3319
   295
    return "Windows";
slouken@3319
   296
#endif
slouken@3319
   297
#elif __IPHONEOS__
slouken@3319
   298
    return "iPhone OS";
slouken@3319
   299
#else
slouken@3319
   300
    return "Unknown (see SDL_platform.h)";
slouken@3319
   301
#endif
slouken@3319
   302
}
slouken@3319
   303
slouken@5086
   304
#if defined(__WIN32__)
slouken@1330
   305
slouken@1465
   306
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@1465
   307
/* Need to include DllMain() on Watcom C for some reason.. */
slouken@5090
   308
#include "core/windows/SDL_windows.h"
slouken@1330
   309
slouken@1895
   310
BOOL APIENTRY
slouken@1895
   311
_DllMainCRTStartup(HANDLE hModule,
slouken@1895
   312
                   DWORD ul_reason_for_call, LPVOID lpReserved)
slouken@1330
   313
{
slouken@1895
   314
    switch (ul_reason_for_call) {
slouken@1895
   315
    case DLL_PROCESS_ATTACH:
slouken@1895
   316
    case DLL_THREAD_ATTACH:
slouken@1895
   317
    case DLL_THREAD_DETACH:
slouken@1895
   318
    case DLL_PROCESS_DETACH:
slouken@1895
   319
        break;
slouken@1895
   320
    }
slouken@1895
   321
    return TRUE;
slouken@1330
   322
}
slouken@1465
   323
#endif /* building DLL with Watcom C */
slouken@1330
   324
slouken@5086
   325
#endif /* __WIN32__ */
slouken@1895
   326
slouken@1895
   327
/* vi: set ts=4 sw=4 expandtab: */