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