comparison src/SDL.c @ 1895:c121d94672cb

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