src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1190 173c063d4f55
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@0
    22
slouken@0
    23
/* Initialization code for SDL */
slouken@0
    24
slouken@0
    25
#include <stdlib.h>		/* For getenv() */
slouken@557
    26
#ifdef ENABLE_PTH
slouken@557
    27
#include <pth.h>
slouken@557
    28
#endif
slouken@0
    29
slouken@0
    30
#include "SDL.h"
slouken@0
    31
#include "SDL_endian.h"
slouken@0
    32
#include "SDL_fatal.h"
slouken@0
    33
#ifndef DISABLE_VIDEO
slouken@0
    34
#include "SDL_leaks.h"
slouken@0
    35
#endif
slouken@0
    36
slouken@0
    37
/* Initialization/Cleanup routines */
slouken@0
    38
#ifndef DISABLE_JOYSTICK
slouken@0
    39
extern int  SDL_JoystickInit(void);
slouken@0
    40
extern void SDL_JoystickQuit(void);
slouken@0
    41
#endif
slouken@0
    42
#ifndef DISABLE_CDROM
slouken@0
    43
extern int  SDL_CDROMInit(void);
slouken@0
    44
extern void SDL_CDROMQuit(void);
slouken@0
    45
#endif
slouken@0
    46
#ifndef DISABLE_TIMERS
slouken@0
    47
extern void SDL_StartTicks(void);
slouken@0
    48
extern int  SDL_TimerInit(void);
slouken@0
    49
extern void SDL_TimerQuit(void);
slouken@0
    50
#endif
slouken@0
    51
slouken@0
    52
/* The current SDL version */
slouken@0
    53
static SDL_version version = 
slouken@0
    54
	{ SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };
slouken@0
    55
slouken@0
    56
/* The initialized subsystems */
slouken@0
    57
static Uint32 SDL_initialized = 0;
slouken@0
    58
static Uint32 ticks_started = 0;
slouken@0
    59
slouken@0
    60
#ifdef CHECK_LEAKS
slouken@0
    61
int surfaces_allocated = 0;
slouken@0
    62
#endif
slouken@0
    63
slouken@0
    64
int SDL_InitSubSystem(Uint32 flags)
slouken@0
    65
{
slouken@0
    66
#ifndef DISABLE_VIDEO
slouken@0
    67
	/* Initialize the video/event subsystem */
slouken@0
    68
	if ( (flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO) ) {
slouken@0
    69
		if ( SDL_VideoInit(getenv("SDL_VIDEODRIVER"),
slouken@0
    70
		                   (flags&SDL_INIT_EVENTTHREAD)) < 0 ) {
slouken@0
    71
			return(-1);
slouken@0
    72
		}
slouken@0
    73
		SDL_initialized |= SDL_INIT_VIDEO;
slouken@0
    74
	}
slouken@0
    75
#else
slouken@0
    76
	if ( flags & SDL_INIT_VIDEO ) {
slouken@0
    77
		SDL_SetError("SDL not built with video support");
slouken@0
    78
		return(-1);
slouken@0
    79
	}
slouken@0
    80
#endif
slouken@0
    81
slouken@0
    82
#ifndef DISABLE_AUDIO
slouken@0
    83
	/* Initialize the audio subsystem */
slouken@0
    84
	if ( (flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO) ) {
slouken@0
    85
		if ( SDL_AudioInit(getenv("SDL_AUDIODRIVER")) < 0 ) {
slouken@0
    86
			return(-1);
slouken@0
    87
		}
slouken@0
    88
		SDL_initialized |= SDL_INIT_AUDIO;
slouken@0
    89
	}
slouken@0
    90
#else
slouken@0
    91
	if ( flags & SDL_INIT_AUDIO ) {
slouken@0
    92
		SDL_SetError("SDL not built with audio support");
slouken@0
    93
		return(-1);
slouken@0
    94
	}
slouken@0
    95
#endif
slouken@0
    96
slouken@0
    97
#ifndef DISABLE_TIMERS
slouken@0
    98
	/* Initialize the timer subsystem */
slouken@0
    99
	if ( ! ticks_started ) {
slouken@0
   100
		SDL_StartTicks();
slouken@0
   101
		ticks_started = 1;
slouken@0
   102
	}
slouken@0
   103
	if ( (flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER) ) {
slouken@0
   104
		if ( SDL_TimerInit() < 0 ) {
slouken@0
   105
			return(-1);
slouken@0
   106
		}
slouken@0
   107
		SDL_initialized |= SDL_INIT_TIMER;
slouken@0
   108
	}
slouken@0
   109
#else
slouken@0
   110
	if ( flags & SDL_INIT_TIMER ) {
slouken@0
   111
		SDL_SetError("SDL not built with timer support");
slouken@0
   112
		return(-1);
slouken@0
   113
	}
slouken@0
   114
#endif
slouken@0
   115
slouken@0
   116
#ifndef DISABLE_JOYSTICK
slouken@0
   117
	/* Initialize the joystick subsystem */
slouken@0
   118
	if ( (flags & SDL_INIT_JOYSTICK) &&
slouken@0
   119
	     !(SDL_initialized & SDL_INIT_JOYSTICK) ) {
slouken@0
   120
		if ( SDL_JoystickInit() < 0 ) {
slouken@0
   121
			return(-1);
slouken@0
   122
		}
slouken@0
   123
		SDL_initialized |= SDL_INIT_JOYSTICK;
slouken@0
   124
	}
slouken@0
   125
#else
slouken@0
   126
	if ( flags & SDL_INIT_JOYSTICK ) {
slouken@0
   127
		SDL_SetError("SDL not built with joystick support");
slouken@0
   128
		return(-1);
slouken@0
   129
	}
slouken@0
   130
#endif
slouken@0
   131
slouken@0
   132
#ifndef DISABLE_CDROM
slouken@0
   133
	/* Initialize the CD-ROM subsystem */
slouken@0
   134
	if ( (flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM) ) {
slouken@0
   135
		if ( SDL_CDROMInit() < 0 ) {
slouken@0
   136
			return(-1);
slouken@0
   137
		}
slouken@0
   138
		SDL_initialized |= SDL_INIT_CDROM;
slouken@0
   139
	}
slouken@0
   140
#else
slouken@0
   141
	if ( flags & SDL_INIT_CDROM ) {
slouken@0
   142
		SDL_SetError("SDL not built with cdrom support");
slouken@0
   143
		return(-1);
slouken@0
   144
	}
slouken@0
   145
#endif
slouken@0
   146
	return(0);
slouken@0
   147
}
slouken@0
   148
slouken@0
   149
int SDL_Init(Uint32 flags)
slouken@0
   150
{
slouken@397
   151
#if !defined(DISABLE_THREADS) && defined(ENABLE_PTH)
slouken@397
   152
	if (!pth_init()) {
slouken@397
   153
		return -1;
slouken@397
   154
	}
slouken@397
   155
#endif
slouken@397
   156
slouken@0
   157
	/* Clear the error message */
slouken@0
   158
	SDL_ClearError();
slouken@0
   159
slouken@0
   160
	/* Initialize the desired subsystems */
slouken@0
   161
	if ( SDL_InitSubSystem(flags) < 0 ) {
slouken@0
   162
		return(-1);
slouken@0
   163
	}
slouken@0
   164
slouken@0
   165
	/* Everything is initialized */
slouken@0
   166
	if ( !(flags & SDL_INIT_NOPARACHUTE) ) {
slouken@0
   167
		SDL_InstallParachute();
slouken@0
   168
	}
slouken@0
   169
	return(0);
slouken@0
   170
}
slouken@0
   171
slouken@0
   172
void SDL_QuitSubSystem(Uint32 flags)
slouken@0
   173
{
slouken@0
   174
	/* Shut down requested initialized subsystems */
slouken@0
   175
#ifndef DISABLE_CDROM
slouken@0
   176
	if ( (flags & SDL_initialized & SDL_INIT_CDROM) ) {
slouken@0
   177
		SDL_CDROMQuit();
slouken@0
   178
		SDL_initialized &= ~SDL_INIT_CDROM;
slouken@0
   179
	}
slouken@0
   180
#endif
slouken@0
   181
#ifndef DISABLE_JOYSTICK
slouken@0
   182
	if ( (flags & SDL_initialized & SDL_INIT_JOYSTICK) ) {
slouken@0
   183
		SDL_JoystickQuit();
slouken@0
   184
		SDL_initialized &= ~SDL_INIT_JOYSTICK;
slouken@0
   185
	}
slouken@0
   186
#endif
slouken@0
   187
#ifndef DISABLE_TIMERS
slouken@0
   188
	if ( (flags & SDL_initialized & SDL_INIT_TIMER) ) {
slouken@0
   189
		SDL_TimerQuit();
slouken@0
   190
		SDL_initialized &= ~SDL_INIT_TIMER;
slouken@0
   191
	}
slouken@0
   192
#endif
slouken@0
   193
#ifndef DISABLE_AUDIO
slouken@0
   194
	if ( (flags & SDL_initialized & SDL_INIT_AUDIO) ) {
slouken@0
   195
		SDL_AudioQuit();
slouken@0
   196
		SDL_initialized &= ~SDL_INIT_AUDIO;
slouken@0
   197
	}
slouken@0
   198
#endif
slouken@0
   199
#ifndef DISABLE_VIDEO
slouken@0
   200
	if ( (flags & SDL_initialized & SDL_INIT_VIDEO) ) {
slouken@0
   201
		SDL_VideoQuit();
slouken@0
   202
		SDL_initialized &= ~SDL_INIT_VIDEO;
slouken@0
   203
	}
slouken@0
   204
#endif
slouken@0
   205
}
slouken@0
   206
slouken@0
   207
Uint32 SDL_WasInit(Uint32 flags)
slouken@0
   208
{
slouken@0
   209
	if ( ! flags ) {
slouken@0
   210
		flags = SDL_INIT_EVERYTHING;
slouken@0
   211
	}
slouken@0
   212
	return (SDL_initialized&flags);
slouken@0
   213
}
slouken@0
   214
slouken@0
   215
void SDL_Quit(void)
slouken@0
   216
{
slouken@0
   217
	/* Quit all subsystems */
icculus@1190
   218
#ifdef DEBUG_BUILD
icculus@1190
   219
  printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n"); fflush(stdout);
icculus@1190
   220
#endif
slouken@0
   221
	SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   222
slouken@0
   223
#ifdef CHECK_LEAKS
icculus@1190
   224
#ifdef DEBUG_BUILD
icculus@1190
   225
  printf("[SDL_Quit] : CHECK_LEAKS\n"); fflush(stdout);
icculus@1190
   226
#endif
icculus@1190
   227
slouken@0
   228
	/* Print the number of surfaces not freed */
slouken@0
   229
	if ( surfaces_allocated != 0 ) {
slouken@0
   230
		fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", 
slouken@0
   231
							surfaces_allocated);
slouken@0
   232
	}
slouken@0
   233
#endif
icculus@1190
   234
#ifdef DEBUG_BUILD
icculus@1190
   235
  printf("[SDL_Quit] : SDL_UninstallParachute()\n"); fflush(stdout);
icculus@1190
   236
#endif
slouken@0
   237
slouken@0
   238
	/* Uninstall any parachute signal handlers */
slouken@0
   239
	SDL_UninstallParachute();
slouken@397
   240
slouken@397
   241
#if !defined(DISABLE_THREADS) && defined(ENABLE_PTH)
slouken@397
   242
	pth_kill();
slouken@397
   243
#endif
icculus@1190
   244
#ifdef DEBUG_BUILD
icculus@1190
   245
  printf("[SDL_Quit] : Returning!\n"); fflush(stdout);
icculus@1190
   246
#endif
icculus@1190
   247
slouken@0
   248
}
slouken@0
   249
slouken@0
   250
/* Return the library version number */
slouken@0
   251
const SDL_version * SDL_Linked_Version(void)
slouken@0
   252
{
slouken@0
   253
	return(&version);
slouken@0
   254
}
slouken@0
   255
icculus@1190
   256
#ifndef __OS2__
slouken@543
   257
#if defined(_WIN32_WCE) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@543
   258
/* Need to include DllMain() on Windows CE and Watcom C for some reason.. */
slouken@36
   259
#include <windows.h>
slouken@36
   260
slouken@36
   261
BOOL APIENTRY DllMain( HANDLE hModule, 
slouken@36
   262
                       DWORD  ul_reason_for_call, 
slouken@36
   263
                       LPVOID lpReserved )
slouken@36
   264
{
slouken@36
   265
	switch (ul_reason_for_call) {
slouken@36
   266
		case DLL_PROCESS_ATTACH:
slouken@36
   267
		case DLL_THREAD_ATTACH:
slouken@36
   268
		case DLL_THREAD_DETACH:
slouken@36
   269
		case DLL_PROCESS_DETACH:
slouken@36
   270
			break;
slouken@36
   271
	}
slouken@36
   272
	return TRUE;
slouken@36
   273
}
slouken@543
   274
#endif /* _WIN32_WCE and building DLL with Watcom C */
icculus@1190
   275
#else
icculus@1190
   276
// Building for OS/2
icculus@1190
   277
#ifdef __WATCOMC__
icculus@1190
   278
icculus@1190
   279
#define INCL_DOSERRORS
icculus@1190
   280
#define INCL_DOSEXCEPTIONS
icculus@1190
   281
#include <os2.h>
icculus@1190
   282
icculus@1190
   283
// Exception handler to prevent the Audio thread hanging, making a zombie process!
icculus@1190
   284
ULONG _System SDL_Main_ExceptionHandler(PEXCEPTIONREPORTRECORD pERepRec,
icculus@1190
   285
                                        PEXCEPTIONREGISTRATIONRECORD pERegRec,
icculus@1190
   286
                                        PCONTEXTRECORD pCtxRec,
icculus@1190
   287
                                        PVOID p)
icculus@1190
   288
{
icculus@1190
   289
  if (pERepRec->fHandlerFlags & EH_EXIT_UNWIND)
icculus@1190
   290
    return XCPT_CONTINUE_SEARCH;
icculus@1190
   291
  if (pERepRec->fHandlerFlags & EH_UNWINDING)
icculus@1190
   292
    return XCPT_CONTINUE_SEARCH;
icculus@1190
   293
  if (pERepRec->fHandlerFlags & EH_NESTED_CALL)
icculus@1190
   294
    return XCPT_CONTINUE_SEARCH;
icculus@1190
   295
icculus@1190
   296
  // Do cleanup at every fatal exception!
icculus@1190
   297
  if (((pERepRec->ExceptionNum & XCPT_SEVERITY_CODE) == XCPT_FATAL_EXCEPTION) &&
icculus@1190
   298
      (pERepRec->ExceptionNum != XCPT_BREAKPOINT) &&
icculus@1190
   299
      (pERepRec->ExceptionNum != XCPT_SINGLE_STEP)
icculus@1190
   300
     )
icculus@1190
   301
  {
icculus@1190
   302
    if (SDL_initialized & SDL_INIT_AUDIO)
icculus@1190
   303
    {
icculus@1190
   304
      // This removes the zombie audio thread in case of emergency.
icculus@1190
   305
#ifdef DEBUG_BUILD
icculus@1190
   306
      printf("[SDL_Main_ExceptionHandler] : Calling SDL_CloseAudio()!\n");
icculus@1190
   307
#endif
icculus@1190
   308
      SDL_CloseAudio();
icculus@1190
   309
    }
icculus@1190
   310
  }
icculus@1190
   311
  return (XCPT_CONTINUE_SEARCH);
icculus@1190
   312
}
icculus@1190
   313
icculus@1190
   314
icculus@1190
   315
EXCEPTIONREGISTRATIONRECORD SDL_Main_xcpthand = {0, SDL_Main_ExceptionHandler};
icculus@1190
   316
icculus@1190
   317
// The main DLL entry for DLL Initialization and Uninitialization:
icculus@1190
   318
unsigned _System LibMain(unsigned hmod, unsigned termination)
icculus@1190
   319
{
icculus@1190
   320
  if (termination)
icculus@1190
   321
  {
icculus@1190
   322
#ifdef DEBUG_BUILD
icculus@1190
   323
//    printf("[SDL DLL Unintialization] : Removing exception handler\n");
icculus@1190
   324
#endif
icculus@1190
   325
    DosUnsetExceptionHandler(&SDL_Main_xcpthand);
icculus@1190
   326
    return 1;
icculus@1190
   327
  } else
icculus@1190
   328
  {
icculus@1190
   329
#ifdef DEBUG_BUILD
icculus@1190
   330
    // Make stdout and stderr unbuffered!
icculus@1190
   331
    setbuf(stdout, NULL);
icculus@1190
   332
    setbuf(stderr, NULL);
icculus@1190
   333
#endif
icculus@1190
   334
    // Fire up exception handler
icculus@1190
   335
#ifdef DEBUG_BUILD
icculus@1190
   336
//    printf("[SDL DLL Initialization] : Setting exception handler\n");
icculus@1190
   337
#endif
icculus@1190
   338
    // Set exception handler
icculus@1190
   339
    DosSetExceptionHandler(&SDL_Main_xcpthand);
icculus@1190
   340
icculus@1190
   341
    return 1;
icculus@1190
   342
  }
icculus@1190
   343
}
icculus@1190
   344
icculus@1190
   345
#endif
icculus@1190
   346
#endif