src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 4159 a1b03ba2fcd0
child 6127 73a1a4c35259
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Initialization code for SDL */
slouken@0
    25
slouken@1361
    26
#include "SDL.h"
slouken@1361
    27
#include "SDL_fatal.h"
slouken@1361
    28
#if !SDL_VIDEO_DISABLED
slouken@1361
    29
#include "video/SDL_leaks.h"
slouken@1361
    30
#endif
slouken@1361
    31
slouken@1361
    32
#if SDL_THREAD_PTH
slouken@557
    33
#include <pth.h>
slouken@557
    34
#endif
slouken@0
    35
slouken@0
    36
/* Initialization/Cleanup routines */
slouken@1361
    37
#if !SDL_JOYSTICK_DISABLED
slouken@0
    38
extern int  SDL_JoystickInit(void);
slouken@0
    39
extern void SDL_JoystickQuit(void);
slouken@0
    40
#endif
slouken@1361
    41
#if !SDL_CDROM_DISABLED
slouken@0
    42
extern int  SDL_CDROMInit(void);
slouken@0
    43
extern void SDL_CDROMQuit(void);
slouken@0
    44
#endif
slouken@1361
    45
#if !SDL_TIMERS_DISABLED
slouken@0
    46
extern void SDL_StartTicks(void);
slouken@0
    47
extern int  SDL_TimerInit(void);
slouken@0
    48
extern void SDL_TimerQuit(void);
slouken@0
    49
#endif
slouken@0
    50
slouken@0
    51
/* The current SDL version */
slouken@0
    52
static SDL_version version = 
slouken@0
    53
	{ SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };
slouken@0
    54
slouken@0
    55
/* The initialized subsystems */
slouken@0
    56
static Uint32 SDL_initialized = 0;
slouken@3976
    57
#if !SDL_TIMERS_DISABLED
slouken@0
    58
static Uint32 ticks_started = 0;
slouken@3976
    59
#endif
slouken@0
    60
slouken@0
    61
#ifdef CHECK_LEAKS
slouken@0
    62
int surfaces_allocated = 0;
slouken@0
    63
#endif
slouken@0
    64
slouken@0
    65
int SDL_InitSubSystem(Uint32 flags)
slouken@0
    66
{
slouken@1361
    67
#if !SDL_VIDEO_DISABLED
slouken@0
    68
	/* Initialize the video/event subsystem */
slouken@0
    69
	if ( (flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO) ) {
slouken@1336
    70
		if ( SDL_VideoInit(SDL_getenv("SDL_VIDEODRIVER"),
slouken@0
    71
		                   (flags&SDL_INIT_EVENTTHREAD)) < 0 ) {
slouken@0
    72
			return(-1);
slouken@0
    73
		}
slouken@0
    74
		SDL_initialized |= SDL_INIT_VIDEO;
slouken@0
    75
	}
slouken@0
    76
#else
slouken@0
    77
	if ( flags & SDL_INIT_VIDEO ) {
slouken@0
    78
		SDL_SetError("SDL not built with video support");
slouken@0
    79
		return(-1);
slouken@0
    80
	}
slouken@0
    81
#endif
slouken@0
    82
slouken@1361
    83
#if !SDL_AUDIO_DISABLED
slouken@0
    84
	/* Initialize the audio subsystem */
slouken@0
    85
	if ( (flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO) ) {
slouken@1336
    86
		if ( SDL_AudioInit(SDL_getenv("SDL_AUDIODRIVER")) < 0 ) {
slouken@0
    87
			return(-1);
slouken@0
    88
		}
slouken@0
    89
		SDL_initialized |= SDL_INIT_AUDIO;
slouken@0
    90
	}
slouken@0
    91
#else
slouken@0
    92
	if ( flags & SDL_INIT_AUDIO ) {
slouken@0
    93
		SDL_SetError("SDL not built with audio support");
slouken@0
    94
		return(-1);
slouken@0
    95
	}
slouken@0
    96
#endif
slouken@0
    97
slouken@1361
    98
#if !SDL_TIMERS_DISABLED
slouken@0
    99
	/* Initialize the timer subsystem */
slouken@0
   100
	if ( ! ticks_started ) {
slouken@0
   101
		SDL_StartTicks();
slouken@0
   102
		ticks_started = 1;
slouken@0
   103
	}
slouken@0
   104
	if ( (flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER) ) {
slouken@0
   105
		if ( SDL_TimerInit() < 0 ) {
slouken@0
   106
			return(-1);
slouken@0
   107
		}
slouken@0
   108
		SDL_initialized |= SDL_INIT_TIMER;
slouken@0
   109
	}
slouken@0
   110
#else
slouken@0
   111
	if ( flags & SDL_INIT_TIMER ) {
slouken@0
   112
		SDL_SetError("SDL not built with timer support");
slouken@0
   113
		return(-1);
slouken@0
   114
	}
slouken@0
   115
#endif
slouken@0
   116
slouken@1361
   117
#if !SDL_JOYSTICK_DISABLED
slouken@0
   118
	/* Initialize the joystick subsystem */
slouken@0
   119
	if ( (flags & SDL_INIT_JOYSTICK) &&
slouken@0
   120
	     !(SDL_initialized & SDL_INIT_JOYSTICK) ) {
slouken@0
   121
		if ( SDL_JoystickInit() < 0 ) {
slouken@0
   122
			return(-1);
slouken@0
   123
		}
slouken@0
   124
		SDL_initialized |= SDL_INIT_JOYSTICK;
slouken@0
   125
	}
slouken@0
   126
#else
slouken@0
   127
	if ( flags & SDL_INIT_JOYSTICK ) {
slouken@0
   128
		SDL_SetError("SDL not built with joystick support");
slouken@0
   129
		return(-1);
slouken@0
   130
	}
slouken@0
   131
#endif
slouken@0
   132
slouken@1361
   133
#if !SDL_CDROM_DISABLED
slouken@0
   134
	/* Initialize the CD-ROM subsystem */
slouken@0
   135
	if ( (flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM) ) {
slouken@0
   136
		if ( SDL_CDROMInit() < 0 ) {
slouken@0
   137
			return(-1);
slouken@0
   138
		}
slouken@0
   139
		SDL_initialized |= SDL_INIT_CDROM;
slouken@0
   140
	}
slouken@0
   141
#else
slouken@0
   142
	if ( flags & SDL_INIT_CDROM ) {
slouken@0
   143
		SDL_SetError("SDL not built with cdrom support");
slouken@0
   144
		return(-1);
slouken@0
   145
	}
slouken@0
   146
#endif
slouken@0
   147
	return(0);
slouken@0
   148
}
slouken@0
   149
slouken@0
   150
int SDL_Init(Uint32 flags)
slouken@0
   151
{
slouken@1361
   152
#if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
slouken@397
   153
	if (!pth_init()) {
slouken@397
   154
		return -1;
slouken@397
   155
	}
slouken@397
   156
#endif
slouken@397
   157
slouken@0
   158
	/* Clear the error message */
slouken@0
   159
	SDL_ClearError();
slouken@0
   160
slouken@0
   161
	/* Initialize the desired subsystems */
slouken@0
   162
	if ( SDL_InitSubSystem(flags) < 0 ) {
slouken@0
   163
		return(-1);
slouken@0
   164
	}
slouken@0
   165
slouken@0
   166
	/* Everything is initialized */
slouken@0
   167
	if ( !(flags & SDL_INIT_NOPARACHUTE) ) {
slouken@0
   168
		SDL_InstallParachute();
slouken@0
   169
	}
slouken@0
   170
	return(0);
slouken@0
   171
}
slouken@0
   172
slouken@0
   173
void SDL_QuitSubSystem(Uint32 flags)
slouken@0
   174
{
slouken@0
   175
	/* Shut down requested initialized subsystems */
slouken@1361
   176
#if !SDL_CDROM_DISABLED
slouken@0
   177
	if ( (flags & SDL_initialized & SDL_INIT_CDROM) ) {
slouken@0
   178
		SDL_CDROMQuit();
slouken@0
   179
		SDL_initialized &= ~SDL_INIT_CDROM;
slouken@0
   180
	}
slouken@0
   181
#endif
slouken@1361
   182
#if !SDL_JOYSTICK_DISABLED
slouken@0
   183
	if ( (flags & SDL_initialized & SDL_INIT_JOYSTICK) ) {
slouken@0
   184
		SDL_JoystickQuit();
slouken@0
   185
		SDL_initialized &= ~SDL_INIT_JOYSTICK;
slouken@0
   186
	}
slouken@0
   187
#endif
slouken@1361
   188
#if !SDL_TIMERS_DISABLED
slouken@0
   189
	if ( (flags & SDL_initialized & SDL_INIT_TIMER) ) {
slouken@0
   190
		SDL_TimerQuit();
slouken@0
   191
		SDL_initialized &= ~SDL_INIT_TIMER;
slouken@0
   192
	}
slouken@0
   193
#endif
slouken@1361
   194
#if !SDL_AUDIO_DISABLED
slouken@0
   195
	if ( (flags & SDL_initialized & SDL_INIT_AUDIO) ) {
slouken@0
   196
		SDL_AudioQuit();
slouken@0
   197
		SDL_initialized &= ~SDL_INIT_AUDIO;
slouken@0
   198
	}
slouken@0
   199
#endif
slouken@1361
   200
#if !SDL_VIDEO_DISABLED
slouken@0
   201
	if ( (flags & SDL_initialized & SDL_INIT_VIDEO) ) {
slouken@0
   202
		SDL_VideoQuit();
slouken@0
   203
		SDL_initialized &= ~SDL_INIT_VIDEO;
slouken@0
   204
	}
slouken@0
   205
#endif
slouken@0
   206
}
slouken@0
   207
slouken@0
   208
Uint32 SDL_WasInit(Uint32 flags)
slouken@0
   209
{
slouken@0
   210
	if ( ! flags ) {
slouken@0
   211
		flags = SDL_INIT_EVERYTHING;
slouken@0
   212
	}
slouken@0
   213
	return (SDL_initialized&flags);
slouken@0
   214
}
slouken@0
   215
slouken@0
   216
void SDL_Quit(void)
slouken@0
   217
{
slouken@0
   218
	/* Quit all subsystems */
icculus@1190
   219
#ifdef DEBUG_BUILD
icculus@1190
   220
  printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n"); fflush(stdout);
icculus@1190
   221
#endif
slouken@0
   222
	SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   223
slouken@0
   224
#ifdef CHECK_LEAKS
icculus@1190
   225
#ifdef DEBUG_BUILD
icculus@1190
   226
  printf("[SDL_Quit] : CHECK_LEAKS\n"); fflush(stdout);
icculus@1190
   227
#endif
icculus@1190
   228
slouken@0
   229
	/* Print the number of surfaces not freed */
slouken@0
   230
	if ( surfaces_allocated != 0 ) {
slouken@0
   231
		fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", 
slouken@0
   232
							surfaces_allocated);
slouken@0
   233
	}
slouken@0
   234
#endif
icculus@1190
   235
#ifdef DEBUG_BUILD
icculus@1190
   236
  printf("[SDL_Quit] : SDL_UninstallParachute()\n"); fflush(stdout);
icculus@1190
   237
#endif
slouken@0
   238
slouken@0
   239
	/* Uninstall any parachute signal handlers */
slouken@0
   240
	SDL_UninstallParachute();
slouken@397
   241
slouken@1361
   242
#if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
slouken@397
   243
	pth_kill();
slouken@397
   244
#endif
icculus@1190
   245
#ifdef DEBUG_BUILD
icculus@1190
   246
  printf("[SDL_Quit] : Returning!\n"); fflush(stdout);
icculus@1190
   247
#endif
icculus@1190
   248
slouken@0
   249
}
slouken@0
   250
slouken@0
   251
/* Return the library version number */
slouken@0
   252
const SDL_version * SDL_Linked_Version(void)
slouken@0
   253
{
slouken@0
   254
	return(&version);
slouken@0
   255
}
slouken@0
   256
slouken@1330
   257
#if defined(__OS2__)
slouken@1487
   258
/* Building for OS/2 */
icculus@1190
   259
#ifdef __WATCOMC__
icculus@1190
   260
icculus@1190
   261
#define INCL_DOSERRORS
icculus@1190
   262
#define INCL_DOSEXCEPTIONS
icculus@1190
   263
#include <os2.h>
icculus@1190
   264
slouken@1487
   265
/* Exception handler to prevent the Audio thread hanging, making a zombie process! */
icculus@1190
   266
ULONG _System SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
icculus@1190
   267
                                        PEXCEPTIONREGISTRATIONRECORD pERegRec,
icculus@1190
   268
                                        PCONTEXTRECORD pCtxRec,
icculus@1190
   269
                                        PVOID p)
icculus@1190
   270
{
icculus@1190
   271
  if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
icculus@1190
   272
    return XCPT_CONTINUE_SEARCH;
icculus@1190
   273
  if (pERepRec->fHandlerFlags & EH_UNWINDING)
icculus@1190
   274
    return XCPT_CONTINUE_SEARCH;
icculus@1190
   275
  if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
icculus@1190
   276
    return XCPT_CONTINUE_SEARCH;
icculus@1190
   277
slouken@1487
   278
  /* Do cleanup at every fatal exception! */
icculus@1190
   279
  if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) == XCPT_FATAL_EXCEPTION) &&
icculus@1190
   280
      (pERepRec->ExceptionNum != XCPT_BREAKPOINT) &&
icculus@1190
   281
      (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)
icculus@1190
   282
     )
icculus@1190
   283
  {
icculus@1190
   284
    if (SDL_initialized & SDL_INIT_AUDIO)
icculus@1190
   285
    {
slouken@1487
   286
      /* This removes the zombie audio thread in case of emergency. */
icculus@1190
   287
#ifdef DEBUG_BUILD
icculus@1190
   288
      printf("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
icculus@1190
   289
#endif
icculus@1190
   290
      SDL_CloseAudio();
icculus@1190
   291
    }
icculus@1190
   292
  }
icculus@1190
   293
  return (XCPT_CONTINUE_SEARCH);
icculus@1190
   294
}
icculus@1190
   295
icculus@1190
   296
icculus@1190
   297
EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand = {0, SDL_Main_ExceptionHandler};
icculus@1190
   298
slouken@1487
   299
/* The main DLL entry for DLL Initialization and Uninitialization: */
icculus@1190
   300
unsigned _System LibMain(unsigned hmod, unsigned termination)
icculus@1190
   301
{
icculus@1190
   302
  if (termination)
icculus@1190
   303
  {
icculus@1190
   304
#ifdef DEBUG_BUILD
slouken@1487
   305
/*    printf("[SDL DLL Unintialization] : Removing exception handler\n"); */
icculus@1190
   306
#endif
icculus@1190
   307
    DosUnsetExceptionHandler(&SDL_Main_xcpthand);
icculus@1190
   308
    return 1;
icculus@1190
   309
  } else
icculus@1190
   310
  {
icculus@1190
   311
#ifdef DEBUG_BUILD
slouken@1487
   312
    /* Make stdout and stderr unbuffered! */
icculus@1190
   313
    setbuf(stdout, NULL);
icculus@1190
   314
    setbuf(stderr, NULL);
icculus@1190
   315
#endif
slouken@1487
   316
    /* Fire up exception handler */
icculus@1190
   317
#ifdef DEBUG_BUILD
slouken@1487
   318
/*    printf("[SDL DLL Initialization] : Setting exception handler\n"); */
icculus@1190
   319
#endif
slouken@1487
   320
    /* Set exception handler */
icculus@1190
   321
    DosSetExceptionHandler(&SDL_Main_xcpthand);
icculus@1190
   322
icculus@1190
   323
    return 1;
icculus@1190
   324
  }
icculus@1190
   325
}
slouken@1330
   326
#endif /* __WATCOMC__ */
icculus@1190
   327
icculus@3975
   328
#elif defined(__WIN32__)  && !defined(__SYMBIAN32__)
slouken@1330
   329
slouken@1465
   330
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@1465
   331
/* Need to include DllMain() on Watcom C for some reason.. */
slouken@1433
   332
#define WIN32_LEAN_AND_MEAN
slouken@1433
   333
#include <windows.h>
slouken@1330
   334
slouken@1330
   335
BOOL APIENTRY _DllMainCRTStartup( HANDLE hModule, 
slouken@1330
   336
                       DWORD  ul_reason_for_call, 
slouken@1330
   337
                       LPVOID lpReserved )
slouken@1330
   338
{
slouken@1330
   339
	switch (ul_reason_for_call) {
slouken@1330
   340
		case DLL_PROCESS_ATTACH:
slouken@1330
   341
		case DLL_THREAD_ATTACH:
slouken@1330
   342
		case DLL_THREAD_DETACH:
slouken@1330
   343
		case DLL_PROCESS_DETACH:
slouken@1330
   344
			break;
slouken@1330
   345
	}
slouken@1330
   346
	return TRUE;
slouken@1330
   347
}
slouken@1465
   348
#endif /* building DLL with Watcom C */
slouken@1330
   349
slouken@1402
   350
#endif /* OS/2 elif __WIN32__ */