src/SDL.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 26 May 2015 21:13:27 -0400
changeset 9648 e6bf740c92f8
parent 9619 b94b6d0bff0f
child 9649 d7762e30ba24
permissions -rw-r--r--
Added a hint to specify new thread stack size (thanks, Gabriel!).

Fixes Bugzilla #2019.

(we'll do a better fix when we break the API in SDL 2.1.)
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "./SDL_internal.h"
    22 
    23 #if defined(__WIN32__)
    24 #include "core/windows/SDL_windows.h"
    25 #endif
    26 
    27 /* Initialization code for SDL */
    28 
    29 #include "SDL.h"
    30 #include "SDL_bits.h"
    31 #include "SDL_revision.h"
    32 #include "SDL_assert_c.h"
    33 #include "events/SDL_events_c.h"
    34 #include "haptic/SDL_haptic_c.h"
    35 #include "joystick/SDL_joystick_c.h"
    36 
    37 /* Initialization/Cleanup routines */
    38 #if !SDL_TIMERS_DISABLED
    39 extern int SDL_TimerInit(void);
    40 extern void SDL_TimerQuit(void);
    41 extern void SDL_TicksInit(void);
    42 extern void SDL_TicksQuit(void);
    43 #endif
    44 #if SDL_VIDEO_DRIVER_WINDOWS
    45 extern int SDL_HelperWindowCreate(void);
    46 extern int SDL_HelperWindowDestroy(void);
    47 #endif
    48 
    49 
    50 /* The initialized subsystems */
    51 #ifdef SDL_MAIN_NEEDED
    52 static SDL_bool SDL_MainIsReady = SDL_FALSE;
    53 #else
    54 static SDL_bool SDL_MainIsReady = SDL_TRUE;
    55 #endif
    56 static SDL_bool SDL_bInMainQuit = SDL_FALSE;
    57 static Uint8 SDL_SubsystemRefCount[ 32 ];
    58 
    59 /* Private helper to increment a subsystem's ref counter. */
    60 static void
    61 SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
    62 {
    63     int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
    64     SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
    65     ++SDL_SubsystemRefCount[subsystem_index];
    66 }
    67 
    68 /* Private helper to decrement a subsystem's ref counter. */
    69 static void
    70 SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
    71 {
    72     int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
    73     if (SDL_SubsystemRefCount[subsystem_index] > 0) {
    74         --SDL_SubsystemRefCount[subsystem_index];
    75     }
    76 }
    77 
    78 /* Private helper to check if a system needs init. */
    79 static SDL_bool
    80 SDL_PrivateShouldInitSubsystem(Uint32 subsystem)
    81 {
    82     int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
    83     SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
    84     return (SDL_SubsystemRefCount[subsystem_index] == 0);
    85 }
    86 
    87 /* Private helper to check if a system needs to be quit. */
    88 static SDL_bool
    89 SDL_PrivateShouldQuitSubsystem(Uint32 subsystem) {
    90     int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
    91     if (SDL_SubsystemRefCount[subsystem_index] == 0) {
    92       return SDL_FALSE;
    93     }
    94 
    95     /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
    96      * isn't zero.
    97      */
    98     return SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit;
    99 }
   100 
   101 void
   102 SDL_SetMainReady(void)
   103 {
   104     SDL_MainIsReady = SDL_TRUE;
   105 }
   106 
   107 int
   108 SDL_InitSubSystem(Uint32 flags)
   109 {
   110     static Uint32 hints_initialized = SDL_FALSE;
   111     if (!SDL_MainIsReady) {
   112         SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
   113         return -1;
   114     }
   115 
   116     /* Clear the error message */
   117     SDL_ClearError();
   118     
   119     if (hints_initialized == SDL_FALSE) {
   120         /* Set a default of -1 for SDL_HINT_THREAD_STACK_SIZE to prevent the
   121            end user from interfering it's value with environment variables */
   122         SDL_SetHintWithPriority(SDL_HINT_THREAD_STACK_SIZE, "-1", SDL_HINT_OVERRIDE);
   123         hints_initialized = SDL_TRUE;
   124     }
   125 
   126 #if SDL_VIDEO_DRIVER_WINDOWS
   127 	if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
   128 		if (SDL_HelperWindowCreate() < 0) {
   129 			return -1;
   130 		}
   131 	}
   132 #endif
   133 
   134 #if !SDL_TIMERS_DISABLED
   135     SDL_TicksInit();
   136 #endif
   137 
   138     if ((flags & SDL_INIT_GAMECONTROLLER)) {
   139         /* game controller implies joystick */
   140         flags |= SDL_INIT_JOYSTICK;
   141     }
   142 
   143     if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) {
   144         /* video or joystick implies events */
   145         flags |= SDL_INIT_EVENTS;
   146     }
   147 
   148     /* Initialize the event subsystem */
   149     if ((flags & SDL_INIT_EVENTS)) {
   150 #if !SDL_EVENTS_DISABLED
   151         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
   152             if (SDL_StartEventLoop() < 0) {
   153                 return (-1);
   154             }
   155             SDL_QuitInit();
   156         }
   157         SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
   158 #else
   159         return SDL_SetError("SDL not built with events support");
   160 #endif
   161     }
   162 
   163     /* Initialize the timer subsystem */
   164     if ((flags & SDL_INIT_TIMER)){
   165 #if !SDL_TIMERS_DISABLED
   166         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
   167             if (SDL_TimerInit() < 0) {
   168                 return (-1);
   169             }
   170         }
   171         SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
   172 #else
   173         return SDL_SetError("SDL not built with timer support");
   174 #endif
   175     }
   176 
   177     /* Initialize the video subsystem */
   178     if ((flags & SDL_INIT_VIDEO)){
   179 #if !SDL_VIDEO_DISABLED
   180         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
   181             if (SDL_VideoInit(NULL) < 0) {
   182                 return (-1);
   183             }
   184         }
   185         SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
   186 #else
   187         return SDL_SetError("SDL not built with video support");
   188 #endif
   189     }
   190 
   191     /* Initialize the audio subsystem */
   192     if ((flags & SDL_INIT_AUDIO)){
   193 #if !SDL_AUDIO_DISABLED
   194         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
   195             if (SDL_AudioInit(NULL) < 0) {
   196                 return (-1);
   197             }
   198         }
   199         SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
   200 #else
   201         return SDL_SetError("SDL not built with audio support");
   202 #endif
   203     }
   204 
   205     /* Initialize the joystick subsystem */
   206     if ((flags & SDL_INIT_JOYSTICK)){
   207 #if !SDL_JOYSTICK_DISABLED
   208         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
   209            if (SDL_JoystickInit() < 0) {
   210                return (-1);
   211            }
   212         }
   213         SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
   214 #else
   215         return SDL_SetError("SDL not built with joystick support");
   216 #endif
   217     }
   218 
   219     if ((flags & SDL_INIT_GAMECONTROLLER)){
   220 #if !SDL_JOYSTICK_DISABLED
   221         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
   222             if (SDL_GameControllerInit() < 0) {
   223                 return (-1);
   224             }
   225         }
   226         SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
   227 #else
   228         return SDL_SetError("SDL not built with joystick support");
   229 #endif
   230     }
   231 
   232     /* Initialize the haptic subsystem */
   233     if ((flags & SDL_INIT_HAPTIC)){
   234 #if !SDL_HAPTIC_DISABLED
   235         if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
   236             if (SDL_HapticInit() < 0) {
   237                 return (-1);
   238             }
   239         }
   240         SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
   241 #else
   242         return SDL_SetError("SDL not built with haptic (force feedback) support");
   243 #endif
   244     }
   245 
   246     return (0);
   247 }
   248 
   249 int
   250 SDL_Init(Uint32 flags)
   251 {
   252     return SDL_InitSubSystem(flags);
   253 }
   254 
   255 void
   256 SDL_QuitSubSystem(Uint32 flags)
   257 {
   258     /* Shut down requested initialized subsystems */
   259 #if !SDL_JOYSTICK_DISABLED
   260     if ((flags & SDL_INIT_GAMECONTROLLER)) {
   261         /* game controller implies joystick */
   262         flags |= SDL_INIT_JOYSTICK;
   263 
   264         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
   265             SDL_GameControllerQuit();
   266         }
   267         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER);
   268     }
   269 
   270     if ((flags & SDL_INIT_JOYSTICK)) {
   271         /* joystick implies events */
   272         flags |= SDL_INIT_EVENTS;
   273 
   274         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
   275             SDL_JoystickQuit();
   276         }
   277         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK);
   278     }
   279 #endif
   280 
   281 #if !SDL_HAPTIC_DISABLED
   282     if ((flags & SDL_INIT_HAPTIC)) {
   283         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
   284             SDL_HapticQuit();
   285         }
   286         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC);
   287     }
   288 #endif
   289 
   290 #if !SDL_AUDIO_DISABLED
   291     if ((flags & SDL_INIT_AUDIO)) {
   292         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) {
   293             SDL_AudioQuit();
   294         }
   295         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO);
   296     }
   297 #endif
   298 
   299 #if !SDL_VIDEO_DISABLED
   300     if ((flags & SDL_INIT_VIDEO)) {
   301         /* video implies events */
   302         flags |= SDL_INIT_EVENTS;
   303 
   304         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
   305             SDL_VideoQuit();
   306         }
   307         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO);
   308     }
   309 #endif
   310 
   311 #if !SDL_TIMERS_DISABLED
   312     if ((flags & SDL_INIT_TIMER)) {
   313         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) {
   314             SDL_TimerQuit();
   315         }
   316         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
   317     }
   318 #endif
   319 
   320 #if !SDL_EVENTS_DISABLED
   321     if ((flags & SDL_INIT_EVENTS)) {
   322         if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) {
   323             SDL_QuitQuit();
   324             SDL_StopEventLoop();
   325         }
   326         SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS);
   327     }
   328 #endif
   329 }
   330 
   331 Uint32
   332 SDL_WasInit(Uint32 flags)
   333 {
   334     int i;
   335     int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
   336     Uint32 initialized = 0;
   337 
   338     if (!flags) {
   339         flags = SDL_INIT_EVERYTHING;
   340     }
   341 
   342     num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
   343 
   344     /* Iterate over each bit in flags, and check the matching subsystem. */
   345     for (i = 0; i < num_subsystems; ++i) {
   346         if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
   347             initialized |= (1 << i);
   348         }
   349 
   350         flags >>= 1;
   351     }
   352 
   353     return initialized;
   354 }
   355 
   356 void
   357 SDL_Quit(void)
   358 {
   359     SDL_bInMainQuit = SDL_TRUE;
   360 
   361     /* Quit all subsystems */
   362 #if SDL_VIDEO_DRIVER_WINDOWS
   363     SDL_HelperWindowDestroy();
   364 #endif
   365     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   366 
   367 #if !SDL_TIMERS_DISABLED
   368     SDL_TicksQuit();
   369 #endif
   370 
   371     SDL_ClearHints();
   372     SDL_AssertionsQuit();
   373     SDL_LogResetPriorities();
   374 
   375     /* Now that every subsystem has been quit, we reset the subsystem refcount
   376      * and the list of initialized subsystems.
   377      */
   378     SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
   379 
   380     SDL_bInMainQuit = SDL_FALSE;
   381 }
   382 
   383 /* Get the library version number */
   384 void
   385 SDL_GetVersion(SDL_version * ver)
   386 {
   387     SDL_VERSION(ver);
   388 }
   389 
   390 /* Get the library source revision */
   391 const char *
   392 SDL_GetRevision(void)
   393 {
   394     return SDL_REVISION;
   395 }
   396 
   397 /* Get the library source revision number */
   398 int
   399 SDL_GetRevisionNumber(void)
   400 {
   401     return SDL_REVISION_NUMBER;
   402 }
   403 
   404 /* Get the name of the platform */
   405 const char *
   406 SDL_GetPlatform()
   407 {
   408 #if __AIX__
   409     return "AIX";
   410 #elif __ANDROID__
   411     return "Android";
   412 #elif __BSDI__
   413     return "BSDI";
   414 #elif __DREAMCAST__
   415     return "Dreamcast";
   416 #elif __EMSCRIPTEN__
   417     return "Emscripten";
   418 #elif __FREEBSD__
   419     return "FreeBSD";
   420 #elif __HAIKU__
   421     return "Haiku";
   422 #elif __HPUX__
   423     return "HP-UX";
   424 #elif __IRIX__
   425     return "Irix";
   426 #elif __LINUX__
   427     return "Linux";
   428 #elif __MINT__
   429     return "Atari MiNT";
   430 #elif __MACOS__
   431     return "MacOS Classic";
   432 #elif __MACOSX__
   433     return "Mac OS X";
   434 #elif __NACL__
   435     return "NaCl";
   436 #elif __NETBSD__
   437     return "NetBSD";
   438 #elif __OPENBSD__
   439     return "OpenBSD";
   440 #elif __OS2__
   441     return "OS/2";
   442 #elif __OSF__
   443     return "OSF/1";
   444 #elif __QNXNTO__
   445     return "QNX Neutrino";
   446 #elif __RISCOS__
   447     return "RISC OS";
   448 #elif __SOLARIS__
   449     return "Solaris";
   450 #elif __WIN32__
   451     return "Windows";
   452 #elif __WINRT__
   453     return "WinRT";
   454 #elif __IPHONEOS__
   455     return "iOS";
   456 #elif __PSP__
   457     return "PlayStation Portable";
   458 #else
   459     return "Unknown (see SDL_platform.h)";
   460 #endif
   461 }
   462 
   463 #if defined(__WIN32__)
   464 
   465 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   466 /* Need to include DllMain() on Watcom C for some reason.. */
   467 
   468 BOOL APIENTRY
   469 _DllMainCRTStartup(HANDLE hModule,
   470                    DWORD ul_reason_for_call, LPVOID lpReserved)
   471 {
   472     switch (ul_reason_for_call) {
   473     case DLL_PROCESS_ATTACH:
   474     case DLL_THREAD_ATTACH:
   475     case DLL_THREAD_DETACH:
   476     case DLL_PROCESS_DETACH:
   477         break;
   478     }
   479     return TRUE;
   480 }
   481 #endif /* building DLL with Watcom C */
   482 
   483 #endif /* __WIN32__ */
   484 
   485 /* vi: set sts=4 ts=4 sw=4 expandtab: */