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