src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 16 Sep 2002 07:09:04 +0000
changeset 496 864a66f028d8
parent 397 283d348cb624
child 543 522e5202014d
permissions -rw-r--r--
Don't be fooled by unclutter - the mouse isn't really leaving our window
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* Initialization code for SDL */
    29 
    30 #include <stdlib.h>		/* For getenv() */
    31 
    32 #include "SDL.h"
    33 #include "SDL_endian.h"
    34 #include "SDL_fatal.h"
    35 #ifndef DISABLE_VIDEO
    36 #include "SDL_leaks.h"
    37 #endif
    38 
    39 /* Initialization/Cleanup routines */
    40 #ifndef DISABLE_JOYSTICK
    41 extern int  SDL_JoystickInit(void);
    42 extern void SDL_JoystickQuit(void);
    43 #endif
    44 #ifndef DISABLE_CDROM
    45 extern int  SDL_CDROMInit(void);
    46 extern void SDL_CDROMQuit(void);
    47 #endif
    48 #ifndef DISABLE_TIMERS
    49 extern void SDL_StartTicks(void);
    50 extern int  SDL_TimerInit(void);
    51 extern void SDL_TimerQuit(void);
    52 #endif
    53 
    54 /* The current SDL version */
    55 static SDL_version version = 
    56 	{ SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };
    57 
    58 /* The initialized subsystems */
    59 static Uint32 SDL_initialized = 0;
    60 static Uint32 ticks_started = 0;
    61 
    62 #ifdef CHECK_LEAKS
    63 int surfaces_allocated = 0;
    64 #endif
    65 
    66 int SDL_InitSubSystem(Uint32 flags)
    67 {
    68 #ifndef DISABLE_VIDEO
    69 	/* Initialize the video/event subsystem */
    70 	if ( (flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO) ) {
    71 		if ( SDL_VideoInit(getenv("SDL_VIDEODRIVER"),
    72 		                   (flags&SDL_INIT_EVENTTHREAD)) < 0 ) {
    73 			return(-1);
    74 		}
    75 		SDL_initialized |= SDL_INIT_VIDEO;
    76 	}
    77 #else
    78 	if ( flags & SDL_INIT_VIDEO ) {
    79 		SDL_SetError("SDL not built with video support");
    80 		return(-1);
    81 	}
    82 #endif
    83 
    84 #ifndef DISABLE_AUDIO
    85 	/* Initialize the audio subsystem */
    86 	if ( (flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO) ) {
    87 		if ( SDL_AudioInit(getenv("SDL_AUDIODRIVER")) < 0 ) {
    88 			return(-1);
    89 		}
    90 		SDL_initialized |= SDL_INIT_AUDIO;
    91 	}
    92 #else
    93 	if ( flags & SDL_INIT_AUDIO ) {
    94 		SDL_SetError("SDL not built with audio support");
    95 		return(-1);
    96 	}
    97 #endif
    98 
    99 #ifndef DISABLE_TIMERS
   100 	/* Initialize the timer subsystem */
   101 	if ( ! ticks_started ) {
   102 		SDL_StartTicks();
   103 		ticks_started = 1;
   104 	}
   105 	if ( (flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER) ) {
   106 		if ( SDL_TimerInit() < 0 ) {
   107 			return(-1);
   108 		}
   109 		SDL_initialized |= SDL_INIT_TIMER;
   110 	}
   111 #else
   112 	if ( flags & SDL_INIT_TIMER ) {
   113 		SDL_SetError("SDL not built with timer support");
   114 		return(-1);
   115 	}
   116 #endif
   117 
   118 #ifndef DISABLE_JOYSTICK
   119 	/* Initialize the joystick subsystem */
   120 	if ( (flags & SDL_INIT_JOYSTICK) &&
   121 	     !(SDL_initialized & SDL_INIT_JOYSTICK) ) {
   122 		if ( SDL_JoystickInit() < 0 ) {
   123 			return(-1);
   124 		}
   125 		SDL_initialized |= SDL_INIT_JOYSTICK;
   126 	}
   127 #else
   128 	if ( flags & SDL_INIT_JOYSTICK ) {
   129 		SDL_SetError("SDL not built with joystick support");
   130 		return(-1);
   131 	}
   132 #endif
   133 
   134 #ifndef DISABLE_CDROM
   135 	/* Initialize the CD-ROM subsystem */
   136 	if ( (flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM) ) {
   137 		if ( SDL_CDROMInit() < 0 ) {
   138 			return(-1);
   139 		}
   140 		SDL_initialized |= SDL_INIT_CDROM;
   141 	}
   142 #else
   143 	if ( flags & SDL_INIT_CDROM ) {
   144 		SDL_SetError("SDL not built with cdrom support");
   145 		return(-1);
   146 	}
   147 #endif
   148 	return(0);
   149 }
   150 
   151 int SDL_Init(Uint32 flags)
   152 {
   153 #if !defined(DISABLE_THREADS) && defined(ENABLE_PTH)
   154 	if (!pth_init()) {
   155 		return -1;
   156 	}
   157 #endif
   158 
   159 	/* Clear the error message */
   160 	SDL_ClearError();
   161 
   162 	/* Initialize the desired subsystems */
   163 	if ( SDL_InitSubSystem(flags) < 0 ) {
   164 		return(-1);
   165 	}
   166 
   167 	/* Everything is initialized */
   168 	if ( !(flags & SDL_INIT_NOPARACHUTE) ) {
   169 		SDL_InstallParachute();
   170 	}
   171 	return(0);
   172 }
   173 
   174 void SDL_QuitSubSystem(Uint32 flags)
   175 {
   176 	/* Shut down requested initialized subsystems */
   177 #ifndef DISABLE_CDROM
   178 	if ( (flags & SDL_initialized & SDL_INIT_CDROM) ) {
   179 		SDL_CDROMQuit();
   180 		SDL_initialized &= ~SDL_INIT_CDROM;
   181 	}
   182 #endif
   183 #ifndef DISABLE_JOYSTICK
   184 	if ( (flags & SDL_initialized & SDL_INIT_JOYSTICK) ) {
   185 		SDL_JoystickQuit();
   186 		SDL_initialized &= ~SDL_INIT_JOYSTICK;
   187 	}
   188 #endif
   189 #ifndef DISABLE_TIMERS
   190 	if ( (flags & SDL_initialized & SDL_INIT_TIMER) ) {
   191 		SDL_TimerQuit();
   192 		SDL_initialized &= ~SDL_INIT_TIMER;
   193 	}
   194 #endif
   195 #ifndef DISABLE_AUDIO
   196 	if ( (flags & SDL_initialized & SDL_INIT_AUDIO) ) {
   197 		SDL_AudioQuit();
   198 		SDL_initialized &= ~SDL_INIT_AUDIO;
   199 	}
   200 #endif
   201 #ifndef DISABLE_VIDEO
   202 	if ( (flags & SDL_initialized & SDL_INIT_VIDEO) ) {
   203 		SDL_VideoQuit();
   204 		SDL_initialized &= ~SDL_INIT_VIDEO;
   205 	}
   206 #endif
   207 }
   208 
   209 Uint32 SDL_WasInit(Uint32 flags)
   210 {
   211 	if ( ! flags ) {
   212 		flags = SDL_INIT_EVERYTHING;
   213 	}
   214 	return (SDL_initialized&flags);
   215 }
   216 
   217 void SDL_Quit(void)
   218 {
   219 	/* Quit all subsystems */
   220 	SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   221 
   222 #ifdef CHECK_LEAKS
   223 	/* Print the number of surfaces not freed */
   224 	if ( surfaces_allocated != 0 ) {
   225 		fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", 
   226 							surfaces_allocated);
   227 	}
   228 #endif
   229 
   230 	/* Uninstall any parachute signal handlers */
   231 	SDL_UninstallParachute();
   232 
   233 #if !defined(DISABLE_THREADS) && defined(ENABLE_PTH)
   234 	pth_kill();
   235 #endif
   236 }
   237 
   238 /* Return the library version number */
   239 const SDL_version * SDL_Linked_Version(void)
   240 {
   241 	return(&version);
   242 }
   243 
   244 #if defined(_WIN32_WCE)
   245 /* Need to include DllMain() on Windows CE for some reason.. */
   246 #include <windows.h>
   247 
   248 BOOL APIENTRY DllMain( HANDLE hModule, 
   249                        DWORD  ul_reason_for_call, 
   250                        LPVOID lpReserved )
   251 {
   252 	switch (ul_reason_for_call) {
   253 		case DLL_PROCESS_ATTACH:
   254 		case DLL_THREAD_ATTACH:
   255 		case DLL_THREAD_DETACH:
   256 		case DLL_PROCESS_DETACH:
   257 			break;
   258 	}
   259 	return TRUE;
   260 }
   261 #endif /* _WIN32_WCE */