src/SDL.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 23 Nov 2005 07:29:56 +0000
changeset 1190 173c063d4f55
parent 769 b8d311d90021
child 1312 c9b51268668f
permissions -rw-r--r--
OS/2 port!

This was mostly, if not entirely, written by "Doodle" and "Caetano":
doodle@scenergy.dfmk.hu
daniel@caetano.eng.br

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