2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 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"
27 /* Initialization code for SDL */
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"
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);
44 #if SDL_VIDEO_DRIVER_WINDOWS
45 extern int SDL_HelperWindowCreate(void);
46 extern int SDL_HelperWindowDestroy(void);
50 /* The initialized subsystems */
51 #ifdef SDL_MAIN_NEEDED
52 static SDL_bool SDL_MainIsReady = SDL_FALSE;
54 static SDL_bool SDL_MainIsReady = SDL_TRUE;
56 static SDL_bool SDL_bInMainQuit = SDL_FALSE;
57 static Uint8 SDL_SubsystemRefCount[ 32 ];
59 /* Private helper to increment a subsystem's ref counter. */
61 SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
63 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
64 SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
65 ++SDL_SubsystemRefCount[subsystem_index];
68 /* Private helper to decrement a subsystem's ref counter. */
70 SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
72 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
73 if (SDL_SubsystemRefCount[subsystem_index] > 0) {
74 --SDL_SubsystemRefCount[subsystem_index];
78 /* Private helper to check if a system needs init. */
80 SDL_PrivateShouldInitSubsystem(Uint32 subsystem)
82 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
83 SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
84 return (SDL_SubsystemRefCount[subsystem_index] == 0);
87 /* Private helper to check if a system needs to be quit. */
89 SDL_PrivateShouldQuitSubsystem(Uint32 subsystem) {
90 int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
91 if (SDL_SubsystemRefCount[subsystem_index] == 0) {
95 /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
98 return SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit;
102 SDL_SetMainReady(void)
104 SDL_MainIsReady = SDL_TRUE;
108 SDL_InitSubSystem(Uint32 flags)
110 if (!SDL_MainIsReady) {
111 SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
115 /* Clear the error message */
118 #if SDL_VIDEO_DRIVER_WINDOWS
119 if ((flags & (SDL_INIT_HAPTIC|SDL_INIT_JOYSTICK))) {
120 if (SDL_HelperWindowCreate() < 0) {
126 #if !SDL_TIMERS_DISABLED
130 if ((flags & SDL_INIT_GAMECONTROLLER)) {
131 /* game controller implies joystick */
132 flags |= SDL_INIT_JOYSTICK;
135 if ((flags & (SDL_INIT_VIDEO|SDL_INIT_JOYSTICK))) {
136 /* video or joystick implies events */
137 flags |= SDL_INIT_EVENTS;
140 /* Initialize the event subsystem */
141 if ((flags & SDL_INIT_EVENTS)) {
142 #if !SDL_EVENTS_DISABLED
143 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_EVENTS)) {
144 if (SDL_StartEventLoop() < 0) {
149 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_EVENTS);
151 return SDL_SetError("SDL not built with events support");
155 /* Initialize the timer subsystem */
156 if ((flags & SDL_INIT_TIMER)){
157 #if !SDL_TIMERS_DISABLED
158 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_TIMER)) {
159 if (SDL_TimerInit() < 0) {
163 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_TIMER);
165 return SDL_SetError("SDL not built with timer support");
169 /* Initialize the video subsystem */
170 if ((flags & SDL_INIT_VIDEO)){
171 #if !SDL_VIDEO_DISABLED
172 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_VIDEO)) {
173 if (SDL_VideoInit(NULL) < 0) {
177 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_VIDEO);
179 return SDL_SetError("SDL not built with video support");
183 /* Initialize the audio subsystem */
184 if ((flags & SDL_INIT_AUDIO)){
185 #if !SDL_AUDIO_DISABLED
186 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_AUDIO)) {
187 if (SDL_AudioInit(NULL) < 0) {
191 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_AUDIO);
193 return SDL_SetError("SDL not built with audio support");
197 /* Initialize the joystick subsystem */
198 if ((flags & SDL_INIT_JOYSTICK)){
199 #if !SDL_JOYSTICK_DISABLED
200 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
201 if (SDL_JoystickInit() < 0) {
205 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_JOYSTICK);
207 return SDL_SetError("SDL not built with joystick support");
211 if ((flags & SDL_INIT_GAMECONTROLLER)){
212 #if !SDL_JOYSTICK_DISABLED
213 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_GAMECONTROLLER)) {
214 if (SDL_GameControllerInit() < 0) {
218 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_GAMECONTROLLER);
220 return SDL_SetError("SDL not built with joystick support");
224 /* Initialize the haptic subsystem */
225 if ((flags & SDL_INIT_HAPTIC)){
226 #if !SDL_HAPTIC_DISABLED
227 if (SDL_PrivateShouldInitSubsystem(SDL_INIT_HAPTIC)) {
228 if (SDL_HapticInit() < 0) {
232 SDL_PrivateSubsystemRefCountIncr(SDL_INIT_HAPTIC);
234 return SDL_SetError("SDL not built with haptic (force feedback) support");
242 SDL_Init(Uint32 flags)
244 return SDL_InitSubSystem(flags);
248 SDL_QuitSubSystem(Uint32 flags)
250 /* Shut down requested initialized subsystems */
251 #if !SDL_JOYSTICK_DISABLED
252 if ((flags & SDL_INIT_GAMECONTROLLER)) {
253 /* game controller implies joystick */
254 flags |= SDL_INIT_JOYSTICK;
256 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_GAMECONTROLLER)) {
257 SDL_GameControllerQuit();
259 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_GAMECONTROLLER);
262 if ((flags & SDL_INIT_JOYSTICK)) {
263 /* joystick implies events */
264 flags |= SDL_INIT_EVENTS;
266 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
269 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_JOYSTICK);
273 #if !SDL_HAPTIC_DISABLED
274 if ((flags & SDL_INIT_HAPTIC)) {
275 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
278 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_HAPTIC);
282 #if !SDL_AUDIO_DISABLED
283 if ((flags & SDL_INIT_AUDIO)) {
284 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_AUDIO)) {
287 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_AUDIO);
291 #if !SDL_VIDEO_DISABLED
292 if ((flags & SDL_INIT_VIDEO)) {
293 /* video implies events */
294 flags |= SDL_INIT_EVENTS;
296 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_VIDEO)) {
299 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_VIDEO);
303 #if !SDL_TIMERS_DISABLED
304 if ((flags & SDL_INIT_TIMER)) {
305 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_TIMER)) {
308 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_TIMER);
312 #if !SDL_EVENTS_DISABLED
313 if ((flags & SDL_INIT_EVENTS)) {
314 if (SDL_PrivateShouldQuitSubsystem(SDL_INIT_EVENTS)) {
318 SDL_PrivateSubsystemRefCountDecr(SDL_INIT_EVENTS);
324 SDL_WasInit(Uint32 flags)
327 int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
328 Uint32 initialized = 0;
331 flags = SDL_INIT_EVERYTHING;
334 num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
336 /* Iterate over each bit in flags, and check the matching subsystem. */
337 for (i = 0; i < num_subsystems; ++i) {
338 if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
339 initialized |= (1 << i);
351 SDL_bInMainQuit = SDL_TRUE;
353 /* Quit all subsystems */
354 #if SDL_VIDEO_DRIVER_WINDOWS
355 SDL_HelperWindowDestroy();
357 SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
359 #if !SDL_TIMERS_DISABLED
364 SDL_AssertionsQuit();
365 SDL_LogResetPriorities();
367 /* Now that every subsystem has been quit, we reset the subsystem refcount
368 * and the list of initialized subsystems.
370 SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
372 SDL_bInMainQuit = SDL_FALSE;
375 /* Get the library version number */
377 SDL_GetVersion(SDL_version * ver)
382 /* Get the library source revision */
384 SDL_GetRevision(void)
389 /* Get the library source revision number */
391 SDL_GetRevisionNumber(void)
393 return SDL_REVISION_NUMBER;
396 /* Get the name of the platform */
423 return "MacOS Classic";
437 return "QNX Neutrino";
449 return "PlayStation Portable";
453 return "Unknown (see SDL_platform.h)";
457 #if defined(__WIN32__)
459 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
460 /* Need to include DllMain() on Watcom C for some reason.. */
463 _DllMainCRTStartup(HANDLE hModule,
464 DWORD ul_reason_for_call, LPVOID lpReserved)
466 switch (ul_reason_for_call) {
467 case DLL_PROCESS_ATTACH:
468 case DLL_THREAD_ATTACH:
469 case DLL_THREAD_DETACH:
470 case DLL_PROCESS_DETACH:
475 #endif /* building DLL with Watcom C */
477 #endif /* __WIN32__ */
479 /* vi: set sts=4 ts=4 sw=4 expandtab: */