SDL.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include "SDL3/SDL_revision.h"
  20. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  21. #include "core/windows/SDL_windows.h"
  22. #elif !defined(SDL_PLATFORM_WINRT)
  23. #include <unistd.h> /* _exit(), etc. */
  24. #endif
  25. /* this checks for HAVE_DBUS_DBUS_H internally. */
  26. #include "core/linux/SDL_dbus.h"
  27. #ifdef SDL_PLATFORM_EMSCRIPTEN
  28. #include <emscripten.h>
  29. #endif
  30. /* Initialization code for SDL */
  31. #include "SDL_assert_c.h"
  32. #include "SDL_hints_c.h"
  33. #include "SDL_log_c.h"
  34. #include "SDL_properties_c.h"
  35. #include "audio/SDL_sysaudio.h"
  36. #include "camera/SDL_camera_c.h"
  37. #include "cpuinfo/SDL_cpuinfo_c.h"
  38. #include "events/SDL_events_c.h"
  39. #include "haptic/SDL_haptic_c.h"
  40. #include "joystick/SDL_gamepad_c.h"
  41. #include "joystick/SDL_joystick_c.h"
  42. #include "render/SDL_sysrender.h"
  43. #include "sensor/SDL_sensor_c.h"
  44. #include "stdlib/SDL_getenv_c.h"
  45. #include "video/SDL_video_c.h"
  46. #define SDL_INIT_EVERYTHING ~0U
  47. /* Initialization/Cleanup routines */
  48. #include "timer/SDL_timer_c.h"
  49. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  50. extern int SDL_HelperWindowCreate(void);
  51. extern int SDL_HelperWindowDestroy(void);
  52. #endif
  53. #ifdef SDL_BUILD_MAJOR_VERSION
  54. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MAJOR_VERSION,
  55. SDL_MAJOR_VERSION == SDL_BUILD_MAJOR_VERSION);
  56. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MINOR_VERSION,
  57. SDL_MINOR_VERSION == SDL_BUILD_MINOR_VERSION);
  58. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MICRO_VERSION,
  59. SDL_MICRO_VERSION == SDL_BUILD_MICRO_VERSION);
  60. #endif
  61. /* Limited by its encoding in SDL_VERSIONNUM */
  62. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_min, SDL_MAJOR_VERSION >= 0);
  63. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_max, SDL_MAJOR_VERSION <= 10);
  64. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_min, SDL_MINOR_VERSION >= 0);
  65. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_max, SDL_MINOR_VERSION <= 999);
  66. SDL_COMPILE_TIME_ASSERT(SDL_MICRO_VERSION_min, SDL_MICRO_VERSION >= 0);
  67. SDL_COMPILE_TIME_ASSERT(SDL_MICRO_VERSION_max, SDL_MICRO_VERSION <= 999);
  68. /* This is not declared in any header, although it is shared between some
  69. parts of SDL, because we don't want anything calling it without an
  70. extremely good reason. */
  71. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  72. SDL_NORETURN void SDL_ExitProcess(int exitcode)
  73. {
  74. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  75. /* "if you do not know the state of all threads in your process, it is
  76. better to call TerminateProcess than ExitProcess"
  77. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx */
  78. TerminateProcess(GetCurrentProcess(), exitcode);
  79. /* MingW doesn't have TerminateProcess marked as noreturn, so add an
  80. ExitProcess here that will never be reached but make MingW happy. */
  81. ExitProcess(exitcode);
  82. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  83. emscripten_cancel_main_loop(); /* this should "kill" the app. */
  84. emscripten_force_exit(exitcode); /* this should "kill" the app. */
  85. exit(exitcode);
  86. #elif defined(SDL_PLATFORM_HAIKU) /* Haiku has _Exit, but it's not marked noreturn. */
  87. _exit(exitcode);
  88. #elif defined(HAVE__EXIT) /* Upper case _Exit() */
  89. _Exit(exitcode);
  90. #else
  91. _exit(exitcode);
  92. #endif
  93. }
  94. /* The initialized subsystems */
  95. #ifdef SDL_MAIN_NEEDED
  96. static SDL_bool SDL_MainIsReady = SDL_FALSE;
  97. #else
  98. static SDL_bool SDL_MainIsReady = SDL_TRUE;
  99. #endif
  100. static SDL_bool SDL_bInMainQuit = SDL_FALSE;
  101. static Uint8 SDL_SubsystemRefCount[32];
  102. /* Private helper to increment a subsystem's ref counter. */
  103. static void SDL_IncrementSubsystemRefCount(Uint32 subsystem)
  104. {
  105. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  106. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  107. if (subsystem_index >= 0) {
  108. ++SDL_SubsystemRefCount[subsystem_index];
  109. }
  110. }
  111. /* Private helper to decrement a subsystem's ref counter. */
  112. static void SDL_DecrementSubsystemRefCount(Uint32 subsystem)
  113. {
  114. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  115. if ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] > 0)) {
  116. if (SDL_bInMainQuit) {
  117. SDL_SubsystemRefCount[subsystem_index] = 0;
  118. } else {
  119. --SDL_SubsystemRefCount[subsystem_index];
  120. }
  121. }
  122. }
  123. /* Private helper to check if a system needs init. */
  124. static SDL_bool SDL_ShouldInitSubsystem(Uint32 subsystem)
  125. {
  126. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  127. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  128. return ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 0));
  129. }
  130. /* Private helper to check if a system needs to be quit. */
  131. static SDL_bool SDL_ShouldQuitSubsystem(Uint32 subsystem)
  132. {
  133. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  134. if ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 0)) {
  135. return SDL_FALSE;
  136. }
  137. /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
  138. * isn't zero.
  139. */
  140. return (((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 1)) || SDL_bInMainQuit);
  141. }
  142. /* Private helper to either increment's existing ref counter,
  143. * or fully init a new subsystem. */
  144. static SDL_bool SDL_InitOrIncrementSubsystem(Uint32 subsystem)
  145. {
  146. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  147. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  148. if (subsystem_index < 0) {
  149. return SDL_FALSE;
  150. }
  151. if (SDL_SubsystemRefCount[subsystem_index] > 0) {
  152. ++SDL_SubsystemRefCount[subsystem_index];
  153. return SDL_TRUE;
  154. }
  155. return SDL_InitSubSystem(subsystem) == 0;
  156. }
  157. void SDL_SetMainReady(void)
  158. {
  159. SDL_MainIsReady = SDL_TRUE;
  160. }
  161. int SDL_InitSubSystem(Uint32 flags)
  162. {
  163. Uint32 flags_initialized = 0;
  164. if (!SDL_MainIsReady) {
  165. return SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
  166. }
  167. SDL_InitLog();
  168. SDL_InitProperties();
  169. SDL_GetGlobalProperties();
  170. /* Clear the error message */
  171. SDL_ClearError();
  172. #ifdef SDL_USE_LIBDBUS
  173. SDL_DBus_Init();
  174. #endif
  175. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  176. if (flags & (SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK)) {
  177. if (SDL_HelperWindowCreate() < 0) {
  178. goto quit_and_error;
  179. }
  180. }
  181. #endif
  182. SDL_InitTicks();
  183. /* Initialize the event subsystem */
  184. if (flags & SDL_INIT_EVENTS) {
  185. if (SDL_ShouldInitSubsystem(SDL_INIT_EVENTS)) {
  186. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  187. if (SDL_InitEvents() < 0) {
  188. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  189. goto quit_and_error;
  190. }
  191. } else {
  192. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  193. }
  194. flags_initialized |= SDL_INIT_EVENTS;
  195. }
  196. /* Initialize the timer subsystem */
  197. if (flags & SDL_INIT_TIMER) {
  198. if (SDL_ShouldInitSubsystem(SDL_INIT_TIMER)) {
  199. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  200. if (SDL_InitTimers() < 0) {
  201. SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
  202. goto quit_and_error;
  203. }
  204. } else {
  205. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  206. }
  207. flags_initialized |= SDL_INIT_TIMER;
  208. }
  209. /* Initialize the video subsystem */
  210. if (flags & SDL_INIT_VIDEO) {
  211. #ifndef SDL_VIDEO_DISABLED
  212. if (SDL_ShouldInitSubsystem(SDL_INIT_VIDEO)) {
  213. /* video implies events */
  214. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  215. goto quit_and_error;
  216. }
  217. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  218. if (SDL_VideoInit(NULL) < 0) {
  219. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  220. goto quit_and_error;
  221. }
  222. } else {
  223. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  224. }
  225. flags_initialized |= SDL_INIT_VIDEO;
  226. #else
  227. SDL_SetError("SDL not built with video support");
  228. goto quit_and_error;
  229. #endif
  230. }
  231. /* Initialize the audio subsystem */
  232. if (flags & SDL_INIT_AUDIO) {
  233. #ifndef SDL_AUDIO_DISABLED
  234. if (SDL_ShouldInitSubsystem(SDL_INIT_AUDIO)) {
  235. /* audio implies events */
  236. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  237. goto quit_and_error;
  238. }
  239. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  240. if (SDL_InitAudio(NULL) < 0) {
  241. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  242. goto quit_and_error;
  243. }
  244. } else {
  245. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  246. }
  247. flags_initialized |= SDL_INIT_AUDIO;
  248. #else
  249. SDL_SetError("SDL not built with audio support");
  250. goto quit_and_error;
  251. #endif
  252. }
  253. /* Initialize the joystick subsystem */
  254. if (flags & SDL_INIT_JOYSTICK) {
  255. #ifndef SDL_JOYSTICK_DISABLED
  256. if (SDL_ShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  257. /* joystick implies events */
  258. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  259. goto quit_and_error;
  260. }
  261. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  262. if (SDL_InitJoysticks() < 0) {
  263. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  264. goto quit_and_error;
  265. }
  266. } else {
  267. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  268. }
  269. flags_initialized |= SDL_INIT_JOYSTICK;
  270. #else
  271. SDL_SetError("SDL not built with joystick support");
  272. goto quit_and_error;
  273. #endif
  274. }
  275. if (flags & SDL_INIT_GAMEPAD) {
  276. #ifndef SDL_JOYSTICK_DISABLED
  277. if (SDL_ShouldInitSubsystem(SDL_INIT_GAMEPAD)) {
  278. /* game controller implies joystick */
  279. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_JOYSTICK)) {
  280. goto quit_and_error;
  281. }
  282. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  283. if (SDL_InitGamepads() < 0) {
  284. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  285. goto quit_and_error;
  286. }
  287. } else {
  288. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  289. }
  290. flags_initialized |= SDL_INIT_GAMEPAD;
  291. #else
  292. SDL_SetError("SDL not built with joystick support");
  293. goto quit_and_error;
  294. #endif
  295. }
  296. /* Initialize the haptic subsystem */
  297. if (flags & SDL_INIT_HAPTIC) {
  298. #ifndef SDL_HAPTIC_DISABLED
  299. if (SDL_ShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  300. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  301. if (SDL_InitHaptics() < 0) {
  302. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  303. goto quit_and_error;
  304. }
  305. } else {
  306. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  307. }
  308. flags_initialized |= SDL_INIT_HAPTIC;
  309. #else
  310. SDL_SetError("SDL not built with haptic (force feedback) support");
  311. goto quit_and_error;
  312. #endif
  313. }
  314. /* Initialize the sensor subsystem */
  315. if (flags & SDL_INIT_SENSOR) {
  316. #ifndef SDL_SENSOR_DISABLED
  317. if (SDL_ShouldInitSubsystem(SDL_INIT_SENSOR)) {
  318. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  319. if (SDL_InitSensors() < 0) {
  320. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  321. goto quit_and_error;
  322. }
  323. } else {
  324. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  325. }
  326. flags_initialized |= SDL_INIT_SENSOR;
  327. #else
  328. SDL_SetError("SDL not built with sensor support");
  329. goto quit_and_error;
  330. #endif
  331. }
  332. /* Initialize the camera subsystem */
  333. if (flags & SDL_INIT_CAMERA) {
  334. #ifndef SDL_CAMERA_DISABLED
  335. if (SDL_ShouldInitSubsystem(SDL_INIT_CAMERA)) {
  336. /* camera implies events */
  337. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  338. goto quit_and_error;
  339. }
  340. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  341. if (SDL_CameraInit(NULL) < 0) {
  342. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  343. goto quit_and_error;
  344. }
  345. } else {
  346. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  347. }
  348. flags_initialized |= SDL_INIT_CAMERA;
  349. #else
  350. SDL_SetError("SDL not built with camera support");
  351. goto quit_and_error;
  352. #endif
  353. }
  354. (void)flags_initialized; /* make static analysis happy, since this only gets used in error cases. */
  355. return SDL_ClearError();
  356. quit_and_error:
  357. SDL_QuitSubSystem(flags_initialized);
  358. return -1;
  359. }
  360. int SDL_Init(Uint32 flags)
  361. {
  362. return SDL_InitSubSystem(flags);
  363. }
  364. void SDL_QuitSubSystem(Uint32 flags)
  365. {
  366. /* Shut down requested initialized subsystems */
  367. #ifndef SDL_CAMERA_DISABLED
  368. if (flags & SDL_INIT_CAMERA) {
  369. if (SDL_ShouldQuitSubsystem(SDL_INIT_CAMERA)) {
  370. SDL_QuitCamera();
  371. /* camera implies events */
  372. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  373. }
  374. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  375. }
  376. #endif
  377. #ifndef SDL_SENSOR_DISABLED
  378. if (flags & SDL_INIT_SENSOR) {
  379. if (SDL_ShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  380. SDL_QuitSensors();
  381. }
  382. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  383. }
  384. #endif
  385. #ifndef SDL_JOYSTICK_DISABLED
  386. if (flags & SDL_INIT_GAMEPAD) {
  387. if (SDL_ShouldQuitSubsystem(SDL_INIT_GAMEPAD)) {
  388. SDL_QuitGamepads();
  389. /* game controller implies joystick */
  390. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  391. }
  392. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  393. }
  394. if (flags & SDL_INIT_JOYSTICK) {
  395. if (SDL_ShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  396. SDL_QuitJoysticks();
  397. /* joystick implies events */
  398. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  399. }
  400. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  401. }
  402. #endif
  403. #ifndef SDL_HAPTIC_DISABLED
  404. if (flags & SDL_INIT_HAPTIC) {
  405. if (SDL_ShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  406. SDL_QuitHaptics();
  407. }
  408. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  409. }
  410. #endif
  411. #ifndef SDL_AUDIO_DISABLED
  412. if (flags & SDL_INIT_AUDIO) {
  413. if (SDL_ShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  414. SDL_QuitAudio();
  415. /* audio implies events */
  416. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  417. }
  418. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  419. }
  420. #endif
  421. #ifndef SDL_VIDEO_DISABLED
  422. if (flags & SDL_INIT_VIDEO) {
  423. if (SDL_ShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  424. SDL_QuitRender();
  425. SDL_VideoQuit();
  426. /* video implies events */
  427. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  428. }
  429. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  430. }
  431. #endif
  432. if (flags & SDL_INIT_TIMER) {
  433. if (SDL_ShouldQuitSubsystem(SDL_INIT_TIMER)) {
  434. SDL_QuitTimers();
  435. }
  436. SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
  437. }
  438. if (flags & SDL_INIT_EVENTS) {
  439. if (SDL_ShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  440. SDL_QuitEvents();
  441. }
  442. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  443. }
  444. }
  445. Uint32 SDL_WasInit(Uint32 flags)
  446. {
  447. int i;
  448. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  449. Uint32 initialized = 0;
  450. /* Fast path for checking one flag */
  451. if (SDL_HasExactlyOneBitSet32(flags)) {
  452. int subsystem_index = SDL_MostSignificantBitIndex32(flags);
  453. return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
  454. }
  455. if (!flags) {
  456. flags = SDL_INIT_EVERYTHING;
  457. }
  458. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  459. /* Iterate over each bit in flags, and check the matching subsystem. */
  460. for (i = 0; i < num_subsystems; ++i) {
  461. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  462. initialized |= (1 << i);
  463. }
  464. flags >>= 1;
  465. }
  466. return initialized;
  467. }
  468. void SDL_Quit(void)
  469. {
  470. SDL_bInMainQuit = SDL_TRUE;
  471. /* Quit all subsystems */
  472. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  473. SDL_HelperWindowDestroy();
  474. #endif
  475. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  476. SDL_QuitTicks();
  477. #ifdef SDL_USE_LIBDBUS
  478. SDL_DBus_Quit();
  479. #endif
  480. SDL_SetObjectsInvalid();
  481. SDL_ClearHints();
  482. SDL_AssertionsQuit();
  483. SDL_QuitCPUInfo();
  484. SDL_QuitProperties();
  485. SDL_QuitLog();
  486. /* Now that every subsystem has been quit, we reset the subsystem refcount
  487. * and the list of initialized subsystems.
  488. */
  489. SDL_memset(SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount));
  490. SDL_CleanupTLS();
  491. SDL_FreeEnvironmentMemory();
  492. SDL_bInMainQuit = SDL_FALSE;
  493. }
  494. /* Get the library version number */
  495. int SDL_GetVersion(void)
  496. {
  497. return SDL_VERSION;
  498. }
  499. /* Get the library source revision */
  500. const char *SDL_GetRevision(void)
  501. {
  502. return SDL_REVISION; // a string literal, no need to SDL_FreeLater it.
  503. }
  504. // Get the name of the platform
  505. // (a string literal, no need to SDL_FreeLater it.)
  506. const char *SDL_GetPlatform(void)
  507. {
  508. #if defined(SDL_PLATFORM_AIX)
  509. return "AIX";
  510. #elif defined(SDL_PLATFORM_ANDROID)
  511. return "Android";
  512. #elif defined(SDL_PLATFORM_BSDI)
  513. return "BSDI";
  514. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  515. return "Emscripten";
  516. #elif defined(SDL_PLATFORM_FREEBSD)
  517. return "FreeBSD";
  518. #elif defined(SDL_PLATFORM_HAIKU)
  519. return "Haiku";
  520. #elif defined(SDL_PLATFORM_HPUX)
  521. return "HP-UX";
  522. #elif defined(SDL_PLATFORM_IRIX)
  523. return "Irix";
  524. #elif defined(SDL_PLATFORM_LINUX)
  525. return "Linux";
  526. #elif defined(__MINT__)
  527. return "Atari MiNT";
  528. #elif defined(SDL_PLATFORM_MACOS)
  529. return "macOS";
  530. #elif defined(SDL_PLATFORM_NETBSD)
  531. return "NetBSD";
  532. #elif defined(SDL_PLATFORM_OPENBSD)
  533. return "OpenBSD";
  534. #elif defined(SDL_PLATFORM_OS2)
  535. return "OS/2";
  536. #elif defined(SDL_PLATFORM_OSF)
  537. return "OSF/1";
  538. #elif defined(SDL_PLATFORM_QNXNTO)
  539. return "QNX Neutrino";
  540. #elif defined(SDL_PLATFORM_RISCOS)
  541. return "RISC OS";
  542. #elif defined(SDL_PLATFORM_SOLARIS)
  543. return "Solaris";
  544. #elif defined(SDL_PLATFORM_WIN32)
  545. return "Windows";
  546. #elif defined(SDL_PLATFORM_WINRT)
  547. return "WinRT";
  548. #elif defined(SDL_PLATFORM_WINGDK)
  549. return "WinGDK";
  550. #elif defined(SDL_PLATFORM_XBOXONE)
  551. return "Xbox One";
  552. #elif defined(SDL_PLATFORM_XBOXSERIES)
  553. return "Xbox Series X|S";
  554. #elif defined(SDL_PLATFORM_IOS)
  555. return "iOS";
  556. #elif defined(SDL_PLATFORM_TVOS)
  557. return "tvOS";
  558. #elif defined(SDL_PLATFORM_PS2)
  559. return "PlayStation 2";
  560. #elif defined(SDL_PLATFORM_PSP)
  561. return "PlayStation Portable";
  562. #elif defined(SDL_PLATFORM_VITA)
  563. return "PlayStation Vita";
  564. #elif defined(SDL_PLATFORM_NGAGE)
  565. return "Nokia N-Gage";
  566. #elif defined(SDL_PLATFORM_3DS)
  567. return "Nintendo 3DS";
  568. #elif defined(__managarm__)
  569. return "Managarm";
  570. #else
  571. return "Unknown (see SDL_platform.h)";
  572. #endif
  573. }
  574. SDL_bool SDL_IsTablet(void)
  575. {
  576. #ifdef SDL_PLATFORM_ANDROID
  577. extern SDL_bool SDL_IsAndroidTablet(void);
  578. return SDL_IsAndroidTablet();
  579. #elif defined(SDL_PLATFORM_IOS)
  580. extern SDL_bool SDL_IsIPad(void);
  581. return SDL_IsIPad();
  582. #else
  583. return SDL_FALSE;
  584. #endif
  585. }
  586. #ifdef SDL_PLATFORM_WIN32
  587. #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
  588. /* FIXME: Still need to include DllMain() on Watcom C ? */
  589. BOOL APIENTRY MINGW32_FORCEALIGN _DllMainCRTStartup(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  590. {
  591. switch (ul_reason_for_call) {
  592. case DLL_PROCESS_ATTACH:
  593. case DLL_THREAD_ATTACH:
  594. case DLL_THREAD_DETACH:
  595. case DLL_PROCESS_DETACH:
  596. break;
  597. }
  598. return TRUE;
  599. }
  600. #endif /* Building DLL */
  601. #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */