SDL  2.0
SDL.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 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__)
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"
36 #include "sensor/SDL_sensor_c.h"
37 
38 /* Initialization/Cleanup routines */
39 #if !SDL_TIMERS_DISABLED
40 # include "timer/SDL_timer_c.h"
41 #endif
42 #if SDL_VIDEO_DRIVER_WINDOWS
43 extern int SDL_HelperWindowCreate(void);
44 extern int SDL_HelperWindowDestroy(void);
45 #endif
46 
47 
48 /* The initialized subsystems */
49 #ifdef SDL_MAIN_NEEDED
51 #else
53 #endif
56 
57 /* Private helper to increment a subsystem's ref counter. */
58 static void
60 {
61  int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
62  SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
63  ++SDL_SubsystemRefCount[subsystem_index];
64 }
65 
66 /* Private helper to decrement a subsystem's ref counter. */
67 static void
69 {
70  int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
71  if (SDL_SubsystemRefCount[subsystem_index] > 0) {
72  --SDL_SubsystemRefCount[subsystem_index];
73  }
74 }
75 
76 /* Private helper to check if a system needs init. */
77 static SDL_bool
79 {
80  int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
81  SDL_assert(SDL_SubsystemRefCount[subsystem_index] < 255);
82  return (SDL_SubsystemRefCount[subsystem_index] == 0) ? SDL_TRUE : SDL_FALSE;
83 }
84 
85 /* Private helper to check if a system needs to be quit. */
86 static SDL_bool
88  int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
89  if (SDL_SubsystemRefCount[subsystem_index] == 0) {
90  return SDL_FALSE;
91  }
92 
93  /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
94  * isn't zero.
95  */
96  return (SDL_SubsystemRefCount[subsystem_index] == 1 || SDL_bInMainQuit) ? SDL_TRUE : SDL_FALSE;
97 }
98 
99 void
101 {
103 }
104 
105 int
107 {
108  if (!SDL_MainIsReady) {
109  SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
110  return -1;
111  }
112 
113  /* Clear the error message */
114  SDL_ClearError();
115 
116  if ((flags & SDL_INIT_GAMECONTROLLER)) {
117  /* game controller implies joystick */
119  }
120 
122  /* video or joystick implies events */
124  }
125 
126 #if SDL_VIDEO_DRIVER_WINDOWS
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  /* Initialize the event subsystem */
139  if ((flags & SDL_INIT_EVENTS)) {
140 #if !SDL_EVENTS_DISABLED
142  if (SDL_StartEventLoop() < 0) {
143  return (-1);
144  }
145  SDL_QuitInit();
146  }
148 #else
149  return SDL_SetError("SDL not built with events support");
150 #endif
151  }
152 
153  /* Initialize the timer subsystem */
154  if ((flags & SDL_INIT_TIMER)){
155 #if !SDL_TIMERS_DISABLED
157  if (SDL_TimerInit() < 0) {
158  return (-1);
159  }
160  }
162 #else
163  return SDL_SetError("SDL not built with timer support");
164 #endif
165  }
166 
167  /* Initialize the video subsystem */
168  if ((flags & SDL_INIT_VIDEO)){
169 #if !SDL_VIDEO_DISABLED
171  if (SDL_VideoInit(NULL) < 0) {
172  return (-1);
173  }
174  }
176 #else
177  return SDL_SetError("SDL not built with video support");
178 #endif
179  }
180 
181  /* Initialize the audio subsystem */
182  if ((flags & SDL_INIT_AUDIO)){
183 #if !SDL_AUDIO_DISABLED
185  if (SDL_AudioInit(NULL) < 0) {
186  return (-1);
187  }
188  }
190 #else
191  return SDL_SetError("SDL not built with audio support");
192 #endif
193  }
194 
195  /* Initialize the joystick subsystem */
196  if ((flags & SDL_INIT_JOYSTICK)){
197 #if !SDL_JOYSTICK_DISABLED
199  if (SDL_JoystickInit() < 0) {
200  return (-1);
201  }
202  }
204 #else
205  return SDL_SetError("SDL not built with joystick support");
206 #endif
207  }
208 
210 #if !SDL_JOYSTICK_DISABLED
212  if (SDL_GameControllerInit() < 0) {
213  return (-1);
214  }
215  }
217 #else
218  return SDL_SetError("SDL not built with joystick support");
219 #endif
220  }
221 
222  /* Initialize the haptic subsystem */
223  if ((flags & SDL_INIT_HAPTIC)){
224 #if !SDL_HAPTIC_DISABLED
226  if (SDL_HapticInit() < 0) {
227  return (-1);
228  }
229  }
231 #else
232  return SDL_SetError("SDL not built with haptic (force feedback) support");
233 #endif
234  }
235 
236  /* Initialize the sensor subsystem */
237  if ((flags & SDL_INIT_SENSOR)){
238 #if !SDL_SENSOR_DISABLED
240  if (SDL_SensorInit() < 0) {
241  return (-1);
242  }
243  }
245 #else
246  return SDL_SetError("SDL not built with sensor support");
247 #endif
248  }
249 
250  return (0);
251 }
252 
253 int
255 {
256  return SDL_InitSubSystem(flags);
257 }
258 
259 void
261 {
262  /* Shut down requested initialized subsystems */
263 #if !SDL_SENSOR_DISABLED
264  if ((flags & SDL_INIT_SENSOR)) {
266  SDL_SensorQuit();
267  }
269  }
270 #endif
271 
272 #if !SDL_JOYSTICK_DISABLED
273  if ((flags & SDL_INIT_GAMECONTROLLER)) {
274  /* game controller implies joystick */
276 
279  }
281  }
282 
283  if ((flags & SDL_INIT_JOYSTICK)) {
284  /* joystick implies events */
286 
289  }
291  }
292 #endif
293 
294 #if !SDL_HAPTIC_DISABLED
295  if ((flags & SDL_INIT_HAPTIC)) {
297  SDL_HapticQuit();
298  }
300  }
301 #endif
302 
303 #if !SDL_AUDIO_DISABLED
304  if ((flags & SDL_INIT_AUDIO)) {
306  SDL_AudioQuit();
307  }
309  }
310 #endif
311 
312 #if !SDL_VIDEO_DISABLED
313  if ((flags & SDL_INIT_VIDEO)) {
314  /* video implies events */
316 
318  SDL_VideoQuit();
319  }
321  }
322 #endif
323 
324 #if !SDL_TIMERS_DISABLED
325  if ((flags & SDL_INIT_TIMER)) {
327  SDL_TimerQuit();
328  }
330  }
331 #endif
332 
333 #if !SDL_EVENTS_DISABLED
334  if ((flags & SDL_INIT_EVENTS)) {
336  SDL_QuitQuit();
338  }
340  }
341 #endif
342 }
343 
344 Uint32
346 {
347  int i;
348  int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
349  Uint32 initialized = 0;
350 
351  if (!flags) {
353  }
354 
355  num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
356 
357  /* Iterate over each bit in flags, and check the matching subsystem. */
358  for (i = 0; i < num_subsystems; ++i) {
359  if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
360  initialized |= (1 << i);
361  }
362 
363  flags >>= 1;
364  }
365 
366  return initialized;
367 }
368 
369 void
370 SDL_Quit(void)
371 {
373 
374  /* Quit all subsystems */
375 #if SDL_VIDEO_DRIVER_WINDOWS
376  SDL_HelperWindowDestroy();
377 #endif
379 
380 #if !SDL_TIMERS_DISABLED
381  SDL_TicksQuit();
382 #endif
383 
384  SDL_ClearHints();
387 
388  /* Now that every subsystem has been quit, we reset the subsystem refcount
389  * and the list of initialized subsystems.
390  */
392 
394 }
395 
396 /* Get the library version number */
397 void
399 {
400  SDL_VERSION(ver);
401 }
402 
403 /* Get the library source revision */
404 const char *
406 {
407  return SDL_REVISION;
408 }
409 
410 /* Get the library source revision number */
411 int
413 {
414  return SDL_REVISION_NUMBER;
415 }
416 
417 /* Get the name of the platform */
418 const char *
420 {
421 #if __AIX__
422  return "AIX";
423 #elif __ANDROID__
424  return "Android";
425 #elif __BSDI__
426  return "BSDI";
427 #elif __DREAMCAST__
428  return "Dreamcast";
429 #elif __EMSCRIPTEN__
430  return "Emscripten";
431 #elif __FREEBSD__
432  return "FreeBSD";
433 #elif __HAIKU__
434  return "Haiku";
435 #elif __HPUX__
436  return "HP-UX";
437 #elif __IRIX__
438  return "Irix";
439 #elif __LINUX__
440  return "Linux";
441 #elif __MINT__
442  return "Atari MiNT";
443 #elif __MACOS__
444  return "MacOS Classic";
445 #elif __MACOSX__
446  return "Mac OS X";
447 #elif __NACL__
448  return "NaCl";
449 #elif __NETBSD__
450  return "NetBSD";
451 #elif __OPENBSD__
452  return "OpenBSD";
453 #elif __OS2__
454  return "OS/2";
455 #elif __OSF__
456  return "OSF/1";
457 #elif __QNXNTO__
458  return "QNX Neutrino";
459 #elif __RISCOS__
460  return "RISC OS";
461 #elif __SOLARIS__
462  return "Solaris";
463 #elif __WIN32__
464  return "Windows";
465 #elif __WINRT__
466  return "WinRT";
467 #elif __TVOS__
468  return "tvOS";
469 #elif __IPHONEOS__
470  return "iOS";
471 #elif __PSP__
472  return "PlayStation Portable";
473 #else
474  return "Unknown (see SDL_platform.h)";
475 #endif
476 }
477 
478 SDL_bool
480 {
481 #if __ANDROID__
482  extern SDL_bool SDL_IsAndroidTablet(void);
483  return SDL_IsAndroidTablet();
484 #elif __IPHONEOS__
485  extern SDL_bool SDL_IsIPad(void);
486  return SDL_IsIPad();
487 #else
488  return SDL_FALSE;
489 #endif
490 }
491 
492 #if defined(__WIN32__)
493 
494 #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
495 /* Need to include DllMain() on Watcom C for some reason.. */
496 
497 BOOL APIENTRY
498 _DllMainCRTStartup(HANDLE hModule,
499  DWORD ul_reason_for_call, LPVOID lpReserved)
500 {
501  switch (ul_reason_for_call) {
502  case DLL_PROCESS_ATTACH:
503  case DLL_THREAD_ATTACH:
504  case DLL_THREAD_DETACH:
505  case DLL_PROCESS_DETACH:
506  break;
507  }
508  return TRUE;
509 }
510 #endif /* Building DLL */
511 
512 #endif /* __WIN32__ */
513 
514 /* vi: set sts=4 ts=4 sw=4 expandtab: */
SDL.h
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179
SDL_memset
#define SDL_memset
Definition: SDL_dynapi_overrides.h:386
SDL_GetRevision
const char * SDL_GetRevision(void)
Get the code revision of SDL that is linked against your program.
Definition: SDL.c:405
SDL_GetPlatform
const char * SDL_GetPlatform()
Gets the name of the platform.
Definition: SDL.c:419
SDL_GetRevisionNumber
int SDL_GetRevisionNumber(void)
Get the revision number of SDL that is linked against your program.
Definition: SDL.c:412
SDL_AudioInit
#define SDL_AudioInit
Definition: SDL_dynapi_overrides.h:75
SDL_PrivateShouldInitSubsystem
static SDL_bool SDL_PrivateShouldInitSubsystem(Uint32 subsystem)
Definition: SDL.c:78
SDL_TimerQuit
void SDL_TimerQuit(void)
Definition: SDL_timer.c:239
SDL_ClearError
#define SDL_ClearError
Definition: SDL_dynapi_overrides.h:114
SDL_HapticQuit
void SDL_HapticQuit(void)
Definition: SDL_haptic.c:390
SDL_PrivateSubsystemRefCountIncr
static void SDL_PrivateSubsystemRefCountIncr(Uint32 subsystem)
Definition: SDL.c:59
SDL_joystick_c.h
NULL
#define NULL
Definition: begin_code.h:164
SDL_JoystickInit
int SDL_JoystickInit(void)
Definition: SDL_joystick.c:114
SDL_SubsystemRefCount
static Uint8 SDL_SubsystemRefCount[32]
Definition: SDL.c:55
SDL_version
Information the version of SDL in use.
Definition: SDL_version.h:52
TRUE
#define TRUE
Definition: edid-parse.c:33
SDL_LogResetPriorities
#define SDL_LogResetPriorities
Definition: SDL_dynapi_overrides.h:238
SDL_INIT_JOYSTICK
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
SDL_VERSION
#define SDL_VERSION(x)
Macro to determine SDL version program was compiled against.
Definition: SDL_version.h:79
SDL_IsAndroidTablet
SDL_bool SDL_IsAndroidTablet(void)
SDL_INIT_EVENTS
#define SDL_INIT_EVENTS
Definition: SDL.h:83
SDL_TimerInit
int SDL_TimerInit(void)
Definition: SDL_timer.c:207
SDL_TicksInit
void SDL_TicksInit(void)
SDL_SensorInit
int SDL_SensorInit(void)
Definition: SDL_sensor.c:69
SDL_revision.h
x0
GLuint GLfloat x0
Definition: SDL_opengl_glext.h:8583
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_ClearHints
#define SDL_ClearHints
Definition: SDL_dynapi_overrides.h:194
SDL_assert_c.h
SDL_AudioQuit
#define SDL_AudioQuit
Definition: SDL_dynapi_overrides.h:76
SDL_QuitQuit
void SDL_QuitQuit(void)
Definition: SDL_quit.c:128
SDL_bits.h
SDL_GameControllerInit
int SDL_GameControllerInit(void)
Definition: SDL_gamecontroller.c:1358
SDL_SensorQuit
void SDL_SensorQuit(void)
Definition: SDL_sensor.c:440
SDL_INIT_SENSOR
#define SDL_INIT_SENSOR
Definition: SDL.h:84
SDL_PrivateSubsystemRefCountDecr
static void SDL_PrivateSubsystemRefCountDecr(Uint32 subsystem)
Definition: SDL.c:68
SDL_GetVersion
void SDL_GetVersion(SDL_version *ver)
Get the version of SDL that is linked against your program.
Definition: SDL.c:398
SDL_bInMainQuit
static SDL_bool SDL_bInMainQuit
Definition: SDL.c:54
SDL_internal.h
SDL_TicksQuit
void SDL_TicksQuit(void)
SDL_HapticInit
int SDL_HapticInit(void)
Definition: SDL_haptic.c:35
SDL_REVISION_NUMBER
#define SDL_REVISION_NUMBER
Definition: SDL_revision.h:2
SDL_JoystickQuit
void SDL_JoystickQuit(void)
Definition: SDL_joystick.c:697
SDL_VideoInit
#define SDL_VideoInit
Definition: SDL_dynapi_overrides.h:498
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
SDL_QuitInit
int SDL_QuitInit(void)
Definition: SDL_quit.c:92
SDL_PrivateShouldQuitSubsystem
static SDL_bool SDL_PrivateShouldQuitSubsystem(Uint32 subsystem)
Definition: SDL.c:87
SDL_AssertionsQuit
void SDL_AssertionsQuit(void)
Definition: SDL_assert.c:397
SDL_WasInit
Uint32 SDL_WasInit(Uint32 flags)
Definition: SDL.c:345
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDL_INIT_HAPTIC
#define SDL_INIT_HAPTIC
Definition: SDL.h:81
APIENTRY
#define APIENTRY
Definition: SDL_opengl.h:139
SDL_Quit
void SDL_Quit(void)
Definition: SDL.c:370
SDL_IsTablet
SDL_bool SDL_IsTablet()
Return true if the current device is a tablet.
Definition: SDL.c:479
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:79
SDL_haptic_c.h
SDL_REVISION
#define SDL_REVISION
Definition: SDL_revision.h:1
SDL_INIT_TIMER
#define SDL_INIT_TIMER
Definition: SDL.h:77
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_InitSubSystem
int SDL_InitSubSystem(Uint32 flags)
Definition: SDL.c:106
SDL_StopEventLoop
void SDL_StopEventLoop(void)
Definition: SDL_events.c:317
SDL_GameControllerQuit
void SDL_GameControllerQuit(void)
Definition: SDL_gamecontroller.c:1906
SDL_events_c.h
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_SetMainReady
void SDL_SetMainReady(void)
Definition: SDL.c:100
SDL_VideoQuit
#define SDL_VideoQuit
Definition: SDL_dynapi_overrides.h:499
SDL_MostSignificantBitIndex32
SDL_FORCE_INLINE int SDL_MostSignificantBitIndex32(Uint32 x)
Definition: SDL_bits.h:61
SDL_windows.h
SDL_sensor_c.h
SDL_INIT_GAMECONTROLLER
#define SDL_INIT_GAMECONTROLLER
Definition: SDL.h:82
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
flags
GLbitfield flags
Definition: SDL_opengl_glext.h:1480
SDL_INIT_EVERYTHING
#define SDL_INIT_EVERYTHING
Definition: SDL.h:86
SDL_QuitSubSystem
void SDL_QuitSubSystem(Uint32 flags)
Definition: SDL.c:260
SDL_Init
int SDL_Init(Uint32 flags)
Definition: SDL.c:254
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_MainIsReady
static SDL_bool SDL_MainIsReady
Definition: SDL.c:52
SDL_timer_c.h
SDL_StartEventLoop
int SDL_StartEventLoop(void)
Definition: SDL_events.c:391
SDL_INIT_AUDIO
#define SDL_INIT_AUDIO
Definition: SDL.h:78