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