SDL.c 15 KB

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