2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "./SDL_internal.h"
23 #if defined(__WIN32__)
24 #include "core/windows/SDL_windows.h"
25 #elif defined(__OS2__)
26 #include <stdlib.h> /* For _exit() */
27 #elif !defined(__WINRT__)
28 #include <unistd.h> /* For _exit(), etc. */
31 #include "core/os2/SDL_os2.h"
34 #include "thread/os2/SDL_systls_c.h"
37 #if defined(__EMSCRIPTEN__)
38 #include <emscripten.h>
41 /* Initialization code for SDL */
45 #include "SDL_revision.h"
46 #include "SDL_assert_c.h"
47 #include "events/SDL_events_c.h"
48 #include "haptic/SDL_haptic_c.h"
49 #include "joystick/SDL_joystick_c.h"
50 #include "sensor/SDL_sensor_c.h"
52 /* Initialization/Cleanup routines */
53 #if !SDL_TIMERS_DISABLED
54 # include "timer/SDL_timer_c.h"
56 #if SDL_VIDEO_DRIVER_WINDOWS
57 extern int SDL_HelperWindowCreate(void);
58 extern int SDL_HelperWindowDestroy(void);
62 /* This is not declared in any header, although it is shared between some
63 parts of SDL, because we don't want anything calling it without an
64 extremely good reason. */
65 extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
66 SDL_NORETURN void SDL_ExitProcess(int exitcode)
69 /* "if you do not know the state of all threads in your process, it is
70 better to call TerminateProcess than ExitProcess"
71 https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx */
72 TerminateProcess(GetCurrentProcess(), exitcode);
73 /* MingW doesn't have TerminateProcess marked as noreturn, so add an
74 ExitProcess here that will never be reached but make MingW happy. */
75 ExitProcess(exitcode);
76 #elif defined(__EMSCRIPTEN__)
77 emscripten_cancel_main_loop(); /* this should "kill" the app. */
78 emscripten_force_exit(exitcode); /* this should "kill" the app. */
80 #elif defined(__HAIKU__) /* Haiku has _Exit, but it's not marked noreturn. */
82 #elif defined(HAVE__EXIT) /* Upper case _Exit() */
90 /* The initialized subsystems */
91 #ifdef SDL_MAIN_NEEDED
92 static SDL_bool SDL_MainIsReady = SDL_FALSE;
94 static SDL_bool SDL_MainIsReady = SDL_TRUE;
96 static SDL_bool SDL_bInMainQuit = SDL_FALSE;
97 static Uint8 SDL_SubsystemRefCount[ 32 ];
99 /* Private helper to increment a subsystem's ref counter. */
101 SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
103 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
104 SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
105 ++SDL_SubsystemRefCount[subsystem_index];
108 /* Private helper to decrement a subsystem's ref counter. */
110 SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
112 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
113 if (SDL_SubsystemRefCount[subsystem_index] > 0) {
114 --SDL_SubsystemRefCount[subsystem_index];
118 /* Private helper to check if a system needs init. */
120 SDL_PrivateShouldInitSubsystem(Uint32 subsystem)
122 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
123 SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
124 return (SDL_SubsystemRefCount[subsystem_index] == 0) ? SDL_TRUE : SDL_FALSE;
127 /* Private helper to check if a system needs to be quit. */
129 SDL_PrivateShouldQuitSubsystem(Uint32 subsystem) {
130 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
131 if (SDL_SubsystemRefCount[subsystem_index] == 0) {
135 /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
138 return (SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit) ? SDL_TRUE : SDL_FALSE;
142 SDL_SetMainReady(void)
144 SDL_MainIsReady = SDL_TRUE;
148 SDL_InitSubSystem(Uint32 flags)
150 if (!SDL_MainIsReady) {
151 SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
156 // Initialize with null input param in Tizen
157 if ((flags & SDL_INIT_VIDEO)){
158 SDL_Log("SDL_InitSubSystem: call SDL_tizen_app_init for null argument\n");
159 SDL_tizen_app_init(0, NULL);
163 /* Clear the error message */
166 if ((flags & SDL_INIT_GAMECONTROLLER)) {
167 /* game controller implies joystick */
168 flags |= SDL_INIT_JOYSTICK;
171 if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) {
172 /* video or joystick implies events */
173 flags |= SDL_INIT_EVENTS;
177 SDL_OS2TLSAlloc(); /* thread/os2/SDL_systls.c */
180 #if SDL_VIDEO_DRIVER_WINDOWS
181 if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
182 if (SDL_HelperWindowCreate() < 0) {
188 #if !SDL_TIMERS_DISABLED
192 /* Initialize the event subsystem */
193 if ((flags & SDL_INIT_EVENTS)) {
194 #if !SDL_EVENTS_DISABLED
195 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
196 if (SDL_EventsInit() < 0) {
200 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
202 return SDL_SetError("SDL not built with events support");
206 /* Initialize the timer subsystem */
207 if ((flags & SDL_INIT_TIMER)){
208 #if !SDL_TIMERS_DISABLED
209 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
210 if (SDL_TimerInit() < 0) {
214 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
216 return SDL_SetError("SDL not built with timer support");
220 /* Initialize the video subsystem */
221 if ((flags & SDL_INIT_VIDEO)){
222 #if !SDL_VIDEO_DISABLED
223 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
224 if (SDL_VideoInit(NULL) < 0) {
228 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
230 return SDL_SetError("SDL not built with video support");
234 /* Initialize the audio subsystem */
235 if ((flags & SDL_INIT_AUDIO)){
236 #if !SDL_AUDIO_DISABLED
237 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
238 if (SDL_AudioInit(NULL) < 0) {
242 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
244 return SDL_SetError("SDL not built with audio support");
248 /* Initialize the joystick subsystem */
249 if ((flags & SDL_INIT_JOYSTICK)){
250 #if !SDL_JOYSTICK_DISABLED
251 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
252 if (SDL_JoystickInit() < 0) {
256 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
258 return SDL_SetError("SDL not built with joystick support");
262 if ((flags & SDL_INIT_GAMECONTROLLER)){
263 #if !SDL_JOYSTICK_DISABLED
264 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
265 if (SDL_GameControllerInit() < 0) {
269 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
271 return SDL_SetError("SDL not built with joystick support");
275 /* Initialize the haptic subsystem */
276 if ((flags & SDL_INIT_HAPTIC)){
277 #if !SDL_HAPTIC_DISABLED
278 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
279 if (SDL_HapticInit() < 0) {
283 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
285 return SDL_SetError("SDL not built with haptic (force feedback) support");
289 /* Initialize the sensor subsystem */
290 if ((flags & SDL_INIT_SENSOR)){
291 #if !SDL_SENSOR_DISABLED
292 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_SENSOR)) {
293 if (SDL_SensorInit() < 0) {
297 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_SENSOR);
299 return SDL_SetError("SDL not built with sensor support");
307 SDL_Init(Uint32 flags)
309 return SDL_InitSubSystem(flags);
313 SDL_QuitSubSystem(Uint32 flags)
316 SDL_OS2TLSFree(); /* thread/os2/SDL_systls.c */
322 /* Shut down requested initialized subsystems */
323 #if !SDL_SENSOR_DISABLED
324 if ((flags & SDL_INIT_SENSOR)) {
325 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_SENSOR)) {
328 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_SENSOR);
332 #if !SDL_JOYSTICK_DISABLED
333 if ((flags & SDL_INIT_GAMECONTROLLER)) {
334 /* game controller implies joystick */
335 flags |= SDL_INIT_JOYSTICK;
337 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
338 SDL_GameControllerQuit();
340 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER);
343 if ((flags & SDL_INIT_JOYSTICK)) {
344 /* joystick implies events */
345 flags |= SDL_INIT_EVENTS;
347 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
350 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK);
354 #if !SDL_HAPTIC_DISABLED
355 if ((flags & SDL_INIT_HAPTIC)) {
356 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
359 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC);
363 #if !SDL_AUDIO_DISABLED
364 if ((flags & SDL_INIT_AUDIO)) {
365 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) {
368 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO);
372 #if !SDL_VIDEO_DISABLED
373 if ((flags & SDL_INIT_VIDEO)) {
374 /* video implies events */
375 flags |= SDL_INIT_EVENTS;
377 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
380 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO);
384 #if !SDL_TIMERS_DISABLED
385 if ((flags & SDL_INIT_TIMER)) {
386 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) {
389 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
393 #if !SDL_EVENTS_DISABLED
394 if ((flags & SDL_INIT_EVENTS)) {
395 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) {
398 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS);
404 SDL_WasInit(Uint32 flags)
407 int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
408 Uint32 initialized = 0;
410 /* Fast path for checking one flag */
411 if (SDL_HasExactlyOneBitSet32(flags)) {
412 int subsystem_index = SDL_MostSignificantBitIndex32(flags);
413 return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
417 flags = SDL_INIT_EVERYTHING;
420 num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
422 /* Iterate over each bit in flags, and check the matching subsystem. */
423 for (i = 0; i < num_subsystems; ++i) {
424 if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
425 initialized |= (1 << i);
437 SDL_bInMainQuit = SDL_TRUE;
439 /* Quit all subsystems */
440 #if SDL_VIDEO_DRIVER_WINDOWS
441 SDL_HelperWindowDestroy();
443 SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
445 #if !SDL_TIMERS_DISABLED
450 SDL_AssertionsQuit();
451 SDL_LogResetPriorities();
453 /* Now that every subsystem has been quit, we reset the subsystem refcount
454 * and the list of initialized subsystems.
456 SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
458 SDL_bInMainQuit = SDL_FALSE;
461 /* Get the library version number */
463 SDL_GetVersion(SDL_version * ver)
468 /* Get the library source revision */
470 SDL_GetRevision(void)
475 /* Get the library source revision number */
477 SDL_GetRevisionNumber(void)
479 return SDL_REVISION_NUMBER;
482 /* Get the name of the platform */
509 return "MacOS Classic";
523 return "QNX Neutrino";
537 return "PlayStation Portable";
541 return "Unknown (see SDL_platform.h)";
549 extern SDL_bool SDL_IsAndroidTablet(void);
550 return SDL_IsAndroidTablet();
552 extern SDL_bool SDL_IsIPad(void);
559 #if defined(__WIN32__)
561 #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
562 /* Need to include DllMain() on Watcom C for some reason.. */
565 _DllMainCRTStartup(HANDLE hModule,
566 DWORD ul_reason_for_call, LPVOID lpReserved)
568 switch (ul_reason_for_call) {
569 case DLL_PROCESS_ATTACH:
570 case DLL_THREAD_ATTACH:
571 case DLL_THREAD_DETACH:
572 case DLL_PROCESS_DETACH:
577 #endif /* Building DLL */
579 #endif /* __WIN32__ */
581 /* vi: set sts=4 ts=4 sw=4 expandtab: */