src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 08:47:44 -0800
changeset 6885 700f1b25f77f
parent 6867 4c899e841091
child 6924 5be9b933146a
permissions -rw-r--r--
Happy New Year!
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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"
jorgen@6866
    26
#include "SDL_bits.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 ticks_started = 0;
slouken@6690
    47
static SDL_bool SDL_bInMainQuit = SDL_FALSE;
jorgen@6866
    48
static Uint8 SDL_SubsystemRefCount[ 32 ];
slouken@0
    49
jorgen@6866
    50
/* Private helper to increment a subsystem's ref counter. */
jorgen@6867
    51
static void
jorgen@6867
    52
SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
slouken@6690
    53
{
jorgen@6866
    54
    int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
jorgen@6866
    55
    SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
jorgen@6866
    56
    ++SDL_SubsystemRefCount[subsystem_index];
jorgen@6866
    57
}
slouken@6690
    58
jorgen@6866
    59
/* Private helper to decrement a subsystem's ref counter. */
jorgen@6867
    60
static void
jorgen@6867
    61
SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
jorgen@6866
    62
{
jorgen@6866
    63
    int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
jorgen@6866
    64
    if (SDL_SubsystemRefCount[subsystem_index] > 0) {
jorgen@6866
    65
        --SDL_SubsystemRefCount[subsystem_index];
jorgen@6866
    66
    }
jorgen@6866
    67
}
jorgen@6866
    68
jorgen@6866
    69
/* Private helper to check if a system needs init. */
jorgen@6866
    70
static SDL_bool
jorgen@6866
    71
SDL_PrivateShouldInitSubsystem(Uint32 flags, Uint32 subsystem)
jorgen@6866
    72
{
jorgen@6867
    73
    int subsystem_index;
jorgen@6866
    74
    if ((flags & subsystem) == 0) {
jorgen@6866
    75
      return SDL_FALSE;
jorgen@6866
    76
    }
jorgen@6866
    77
jorgen@6867
    78
    subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
jorgen@6866
    79
    SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
jorgen@6866
    80
    return (SDL_SubsystemRefCount[subsystem_index] == 0);
jorgen@6866
    81
}
jorgen@6866
    82
jorgen@6866
    83
/* Private helper to check if a system needs to be quit. */
jorgen@6866
    84
static SDL_bool
jorgen@6866
    85
SDL_PrivateShouldQuitSubsystem(Uint32 subsystem) {
jorgen@6866
    86
    int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
jorgen@6866
    87
    if (SDL_SubsystemRefCount[subsystem_index] == 0) {
jorgen@6866
    88
      return SDL_FALSE;
jorgen@6866
    89
    }
jorgen@6866
    90
jorgen@6866
    91
    /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
jorgen@6866
    92
     * isn't zero.
jorgen@6866
    93
     */
jorgen@6866
    94
    return SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit;
slouken@6690
    95
}
slouken@0
    96
slouken@1895
    97
int
slouken@1895
    98
SDL_InitSubSystem(Uint32 flags)
slouken@0
    99
{
slouken@6128
   100
#if !SDL_TIMERS_DISABLED
slouken@6128
   101
    if (!ticks_started) {
slouken@6128
   102
        SDL_StartTicks();
slouken@6128
   103
        ticks_started = 1;
slouken@6128
   104
    }
jorgen@6866
   105
#endif
slouken@6690
   106
jorgen@6866
   107
    /* Initialize the timer subsystem */
jorgen@6866
   108
    if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_TIMER)) {
jorgen@6866
   109
#if !SDL_TIMERS_DISABLED
jorgen@6866
   110
        if (SDL_TimerInit() < 0) {
jorgen@6866
   111
            return (-1);
jorgen@6866
   112
        }
jorgen@6866
   113
        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
slouken@6128
   114
#else
slouken@6128
   115
        SDL_SetError("SDL not built with timer support");
slouken@6128
   116
        return (-1);
jorgen@6866
   117
#endif
slouken@6128
   118
    }
slouken@6128
   119
jorgen@6866
   120
    /* Initialize the video/event subsystem */
jorgen@6866
   121
    if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_VIDEO)) {
slouken@1361
   122
#if !SDL_VIDEO_DISABLED
jorgen@6866
   123
        if (SDL_VideoInit(NULL) < 0) {
jorgen@6866
   124
            return (-1);
jorgen@6866
   125
        }
jorgen@6866
   126
        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
slouken@0
   127
#else
slouken@1895
   128
        SDL_SetError("SDL not built with video support");
slouken@1895
   129
        return (-1);
jorgen@6866
   130
#endif
slouken@1895
   131
    }
slouken@0
   132
jorgen@6866
   133
    /* Initialize the audio subsystem */
jorgen@6866
   134
    if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_AUDIO)) {
slouken@1361
   135
#if !SDL_AUDIO_DISABLED
jorgen@6866
   136
        if (SDL_AudioInit(NULL) < 0) {
jorgen@6866
   137
            return (-1);
jorgen@6866
   138
        }
jorgen@6866
   139
        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
slouken@0
   140
#else
slouken@1895
   141
        SDL_SetError("SDL not built with audio support");
slouken@1895
   142
        return (-1);
jorgen@6866
   143
#endif
slouken@1895
   144
    }
slouken@0
   145
jorgen@6866
   146
    if ((flags & SDL_INIT_GAMECONTROLLER)) {
jorgen@6866
   147
        // Game controller implies Joystick.
jorgen@6866
   148
        flags |= SDL_INIT_JOYSTICK;
jorgen@6866
   149
    }
jorgen@6866
   150
jorgen@6866
   151
    /* Initialize the joystick subsystem */
jorgen@6866
   152
    if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_JOYSTICK)) {
slouken@1361
   153
#if !SDL_JOYSTICK_DISABLED
jorgen@6866
   154
        if (SDL_JoystickInit() < 0) {
slouken@1895
   155
            return (-1);
slouken@1895
   156
        }
jorgen@6866
   157
        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
slouken@0
   158
#else
slouken@1895
   159
        SDL_SetError("SDL not built with joystick support");
slouken@1895
   160
        return (-1);
jorgen@6866
   161
#endif
slouken@1895
   162
    }
jorgen@6866
   163
jorgen@6866
   164
    if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_GAMECONTROLLER)) {
jorgen@6866
   165
#if !SDL_JOYSTICK_DISABLED
jorgen@6866
   166
        if (SDL_GameControllerInit() < 0) {
jorgen@6866
   167
            return (-1);
jorgen@6866
   168
        }
jorgen@6866
   169
        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
jorgen@6866
   170
#else
jorgen@6866
   171
        SDL_SetError("SDL not built with joystick support");
jorgen@6866
   172
        return (-1);
slouken@0
   173
#endif
jorgen@6866
   174
    }
slouken@0
   175
jorgen@6866
   176
    /* Initialize the haptic subsystem */
jorgen@6866
   177
    if (SDL_PrivateShouldInitSubsystem(flags, SDL_INIT_HAPTIC)) {
slouken@2713
   178
#if !SDL_HAPTIC_DISABLED
jorgen@6866
   179
        if (SDL_HapticInit() < 0) {
jorgen@6866
   180
            return (-1);
jorgen@6866
   181
        }
jorgen@6866
   182
        SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
slouken@2713
   183
#else
slouken@2713
   184
        SDL_SetError("SDL not built with haptic (force feedback) support");
slouken@2713
   185
        return (-1);
jorgen@6866
   186
#endif
slouken@2713
   187
    }
jorgen@6866
   188
slouken@1895
   189
    return (0);
slouken@0
   190
}
slouken@0
   191
slouken@1895
   192
int
slouken@1895
   193
SDL_Init(Uint32 flags)
slouken@0
   194
{
slouken@3647
   195
    if (SDL_AssertionsInit() < 0) {
slouken@3647
   196
        return -1;
slouken@3647
   197
    }
slouken@3647
   198
slouken@1895
   199
    /* Clear the error message */
slouken@1895
   200
    SDL_ClearError();
slouken@0
   201
slouken@5086
   202
#if defined(__WIN32__)
slouken@2713
   203
    if (SDL_HelperWindowCreate() < 0) {
slouken@2713
   204
        return -1;
slouken@2713
   205
    }
slouken@2713
   206
#endif
slouken@2713
   207
slouken@1895
   208
    /* Initialize the desired subsystems */
slouken@1895
   209
    if (SDL_InitSubSystem(flags) < 0) {
slouken@1895
   210
        return (-1);
slouken@1895
   211
    }
slouken@0
   212
slouken@1895
   213
    /* Everything is initialized */
slouken@1895
   214
    if (!(flags & SDL_INIT_NOPARACHUTE)) {
slouken@1895
   215
        SDL_InstallParachute();
slouken@1895
   216
    }
slouken@3647
   217
slouken@1895
   218
    return (0);
slouken@0
   219
}
slouken@0
   220
slouken@1895
   221
void
slouken@1895
   222
SDL_QuitSubSystem(Uint32 flags)
slouken@0
   223
{
slouken@1895
   224
    /* Shut down requested initialized subsystems */
slouken@1361
   225
#if !SDL_JOYSTICK_DISABLED
jorgen@6866
   226
    if ((flags & SDL_INIT_GAMECONTROLLER)) {
jorgen@6866
   227
        // Game controller implies Joystick.
jorgen@6866
   228
        flags |= SDL_INIT_JOYSTICK;
jorgen@6866
   229
jorgen@6866
   230
        if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
jorgen@6866
   231
            SDL_GameControllerQuit();
slouken@6690
   232
		}
jorgen@6866
   233
        SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER);
jorgen@6866
   234
    }
slouken@6690
   235
jorgen@6866
   236
    if ((flags & SDL_INIT_JOYSTICK)) {
jorgen@6866
   237
        if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
slouken@6690
   238
			SDL_JoystickQuit();
slouken@6690
   239
		}
jorgen@6866
   240
        SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK);
slouken@1895
   241
    }
slouken@0
   242
#endif
jorgen@6866
   243
slouken@2713
   244
#if !SDL_HAPTIC_DISABLED
jorgen@6866
   245
    if ((flags & SDL_INIT_HAPTIC)) {
jorgen@6866
   246
        if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
slouken@6690
   247
			SDL_HapticQuit();
slouken@6690
   248
		}
jorgen@6866
   249
        SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC);
slouken@2713
   250
    }
slouken@2713
   251
#endif
jorgen@6866
   252
slouken@1361
   253
#if !SDL_AUDIO_DISABLED
jorgen@6866
   254
    if ((flags & SDL_INIT_AUDIO)) {
jorgen@6866
   255
        if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) {
slouken@6690
   256
			SDL_AudioQuit();
slouken@6690
   257
		}
jorgen@6866
   258
        SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO);
slouken@1895
   259
    }
slouken@0
   260
#endif
jorgen@6866
   261
slouken@1361
   262
#if !SDL_VIDEO_DISABLED
jorgen@6866
   263
    if ((flags & SDL_INIT_VIDEO)) {
jorgen@6866
   264
        if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
slouken@6690
   265
			SDL_VideoQuit();
slouken@6690
   266
		}
jorgen@6866
   267
        SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO);
slouken@1895
   268
    }
slouken@0
   269
#endif
jorgen@6866
   270
slouken@6128
   271
#if !SDL_TIMERS_DISABLED
jorgen@6866
   272
    if ((flags & SDL_INIT_TIMER)) {
jorgen@6866
   273
        if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) {
slouken@6690
   274
			SDL_TimerQuit();
slouken@6690
   275
		}
jorgen@6866
   276
        SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
slouken@6128
   277
    }
slouken@6128
   278
#endif
slouken@0
   279
}
slouken@0
   280
slouken@1895
   281
Uint32
slouken@1895
   282
SDL_WasInit(Uint32 flags)
slouken@0
   283
{
jorgen@6866
   284
    int i;
jorgen@6866
   285
    int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
jorgen@6866
   286
    Uint32 initialized = 0;
jorgen@6866
   287
slouken@1895
   288
    if (!flags) {
slouken@1895
   289
        flags = SDL_INIT_EVERYTHING;
slouken@1895
   290
    }
jorgen@6866
   291
jorgen@6866
   292
    num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
jorgen@6866
   293
jorgen@6866
   294
    /* Iterate over each bit in flags, and check the matching subsystem. */
jorgen@6866
   295
    for (i = 0; i < num_subsystems; ++i) {
jorgen@6866
   296
        if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
jorgen@6866
   297
            initialized |= (1 << i);
jorgen@6866
   298
        }
jorgen@6866
   299
jorgen@6866
   300
        flags >>= 1;
jorgen@6866
   301
    }
jorgen@6866
   302
jorgen@6866
   303
    return initialized;
slouken@0
   304
}
slouken@0
   305
slouken@1895
   306
void
slouken@1895
   307
SDL_Quit(void)
slouken@0
   308
{
jorgen@6866
   309
    SDL_bInMainQuit = SDL_TRUE;
jorgen@6866
   310
slouken@1895
   311
    /* Quit all subsystems */
slouken@5086
   312
#if defined(__WIN32__)
slouken@2713
   313
    SDL_HelperWindowDestroy();
slouken@2713
   314
#endif
slouken@1895
   315
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   316
slouken@1895
   317
    /* Uninstall any parachute signal handlers */
slouken@1895
   318
    SDL_UninstallParachute();
slouken@397
   319
slouken@5189
   320
    SDL_ClearHints();
slouken@3647
   321
    SDL_AssertionsQuit();
slouken@5221
   322
    SDL_LogResetPriorities();
slouken@6690
   323
jorgen@6866
   324
    /* Now that every subsystem has been quit, we reset the subsystem refcount
jorgen@6866
   325
     * and the list of initialized subsystems.
jorgen@6866
   326
     */
jorgen@6866
   327
    SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
jorgen@6866
   328
jorgen@6866
   329
    SDL_bInMainQuit = SDL_FALSE;
slouken@0
   330
}
slouken@0
   331
slouken@1895
   332
/* Get the library version number */
slouken@1895
   333
void
slouken@1895
   334
SDL_GetVersion(SDL_version * ver)
slouken@0
   335
{
slouken@1895
   336
    SDL_VERSION(ver);
slouken@0
   337
}
slouken@0
   338
slouken@2982
   339
/* Get the library source revision */
icculus@4419
   340
const char *
slouken@2996
   341
SDL_GetRevision(void)
slouken@2982
   342
{
slouken@2983
   343
    return SDL_REVISION;
slouken@2982
   344
}
slouken@2982
   345
slouken@5359
   346
/* Get the library source revision number */
slouken@5359
   347
int
slouken@5359
   348
SDL_GetRevisionNumber(void)
slouken@5359
   349
{
slouken@5359
   350
    return SDL_REVISION_NUMBER;
slouken@5359
   351
}
slouken@5359
   352
slouken@3319
   353
/* Get the name of the platform */
slouken@3319
   354
const char *
slouken@3319
   355
SDL_GetPlatform()
slouken@3319
   356
{
slouken@3319
   357
#if __AIX__
slouken@3319
   358
    return "AIX";
slouken@6441
   359
#elif __ANDROID__
slouken@6441
   360
    return "Android";
slouken@3319
   361
#elif __HAIKU__
slouken@3319
   362
/* Haiku must appear here before BeOS, since it also defines __BEOS__ */
slouken@3319
   363
    return "Haiku";
slouken@3319
   364
#elif __BEOS__
slouken@3319
   365
    return "BeOS";
slouken@3319
   366
#elif __BSDI__
slouken@3319
   367
    return "BSDI";
slouken@3319
   368
#elif __DREAMCAST__
slouken@3319
   369
    return "Dreamcast";
slouken@3319
   370
#elif __FREEBSD__
slouken@3319
   371
    return "FreeBSD";
slouken@3319
   372
#elif __HPUX__
slouken@3319
   373
    return "HP-UX";
slouken@3319
   374
#elif __IRIX__
slouken@3319
   375
    return "Irix";
slouken@3319
   376
#elif __LINUX__
slouken@3319
   377
    return "Linux";
slouken@3319
   378
#elif __MINT__
slouken@3319
   379
    return "Atari MiNT";
slouken@3319
   380
#elif __MACOS__
slouken@3319
   381
    return "MacOS Classic";
slouken@3319
   382
#elif __MACOSX__
slouken@3319
   383
    return "Mac OS X";
slouken@3319
   384
#elif __NETBSD__
slouken@3319
   385
    return "NetBSD";
slouken@5270
   386
#elif __NDS__
slouken@5270
   387
    return "Nintendo DS";
slouken@3319
   388
#elif __OPENBSD__
slouken@3319
   389
    return "OpenBSD";
slouken@3319
   390
#elif __OS2__
slouken@3319
   391
    return "OS/2";
slouken@3319
   392
#elif __OSF__
slouken@3319
   393
    return "OSF/1";
slouken@3319
   394
#elif __QNXNTO__
slouken@3319
   395
    return "QNX Neutrino";
slouken@3319
   396
#elif __RISCOS__
slouken@3319
   397
    return "RISC OS";
slouken@3319
   398
#elif __SOLARIS__
slouken@3319
   399
    return "Solaris";
slouken@5086
   400
#elif __WIN32__
slouken@3319
   401
    return "Windows";
slouken@3319
   402
#elif __IPHONEOS__
slouken@3319
   403
    return "iPhone OS";
slouken@3319
   404
#else
slouken@3319
   405
    return "Unknown (see SDL_platform.h)";
slouken@3319
   406
#endif
slouken@3319
   407
}
slouken@3319
   408
slouken@5086
   409
#if defined(__WIN32__)
slouken@1330
   410
slouken@1465
   411
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@1465
   412
/* Need to include DllMain() on Watcom C for some reason.. */
slouken@5090
   413
#include "core/windows/SDL_windows.h"
slouken@1330
   414
slouken@1895
   415
BOOL APIENTRY
slouken@1895
   416
_DllMainCRTStartup(HANDLE hModule,
slouken@1895
   417
                   DWORD ul_reason_for_call, LPVOID lpReserved)
slouken@1330
   418
{
slouken@1895
   419
    switch (ul_reason_for_call) {
slouken@1895
   420
    case DLL_PROCESS_ATTACH:
slouken@1895
   421
    case DLL_THREAD_ATTACH:
slouken@1895
   422
    case DLL_THREAD_DETACH:
slouken@1895
   423
    case DLL_PROCESS_DETACH:
slouken@1895
   424
        break;
slouken@1895
   425
    }
slouken@1895
   426
    return TRUE;
slouken@1330
   427
}
slouken@1465
   428
#endif /* building DLL with Watcom C */
slouken@1330
   429
slouken@5086
   430
#endif /* __WIN32__ */
slouken@1895
   431
slouken@1895
   432
/* vi: set ts=4 sw=4 expandtab: */