src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 Aug 2008 09:55:03 +0000
changeset 2713 0906692aa6a4
parent 1909 8f1ab2f7c722
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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