src/SDL.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Mon, 25 Feb 2013 16:52:54 -0800
changeset 6927 982b03203b73
parent 6924 5be9b933146a
child 7009 161b7b6a5303
permissions -rw-r--r--
Cleanup of SDL_PrivateShouldInitSubsystem.

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