src/SDL.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 12:48:50 -0400
changeset 7037 3fedf1f25b94
parent 7011 82bbfbbc720e
child 7110 2a98852fd58d
permissions -rw-r--r--
Make SDL_SetError and friends unconditionally return -1.

This lets us change things like this...

if (Failed) {
SDL_SetError("We failed");
return -1;
}

...into this...

if (Failed) {
return SDL_SetError("We failed");
}


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