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