src/SDL.c
author Edward Rudd <urkle@outoforder.cc>
Thu, 02 May 2013 21:40:59 -0400
changeset 7139 dd722e7460c5
parent 7110 2a98852fd58d
child 7191 75360622e65f
permissions -rw-r--r--
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init

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