SDL.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 "video/SDL_video_c.h"
  37. #include "events/SDL_events_c.h"
  38. #include "haptic/SDL_haptic_c.h"
  39. #include "joystick/SDL_gamepad_c.h"
  40. #include "joystick/SDL_joystick_c.h"
  41. #include "sensor/SDL_sensor_c.h"
  42. #include "camera/SDL_camera_c.h"
  43. #define SDL_INIT_EVERYTHING ~0U
  44. /* Initialization/Cleanup routines */
  45. #include "time/SDL_time_c.h"
  46. #include "timer/SDL_timer_c.h"
  47. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  48. extern int SDL_HelperWindowCreate(void);
  49. extern int SDL_HelperWindowDestroy(void);
  50. #endif
  51. #ifdef SDL_BUILD_MAJOR_VERSION
  52. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MAJOR_VERSION,
  53. SDL_MAJOR_VERSION == SDL_BUILD_MAJOR_VERSION);
  54. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MINOR_VERSION,
  55. SDL_MINOR_VERSION == SDL_BUILD_MINOR_VERSION);
  56. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MICRO_VERSION,
  57. SDL_PATCHLEVEL == SDL_BUILD_MICRO_VERSION);
  58. #endif
  59. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_min, SDL_MAJOR_VERSION >= 0);
  60. /* Limited only by the need to fit in SDL_Version */
  61. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_max, SDL_MAJOR_VERSION <= 255);
  62. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_min, SDL_MINOR_VERSION >= 0);
  63. /* Limited only by the need to fit in SDL_Version */
  64. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_max, SDL_MINOR_VERSION <= 255);
  65. SDL_COMPILE_TIME_ASSERT(SDL_PATCHLEVEL_min, SDL_PATCHLEVEL >= 0);
  66. /* Limited by its encoding in SDL_VERSIONNUM and in the ABI versions */
  67. SDL_COMPILE_TIME_ASSERT(SDL_PATCHLEVEL_max, SDL_PATCHLEVEL <= 99);
  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_InitTime();
  183. SDL_InitTicks();
  184. /* Initialize the event subsystem */
  185. if (flags & SDL_INIT_EVENTS) {
  186. if (SDL_ShouldInitSubsystem(SDL_INIT_EVENTS)) {
  187. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  188. if (SDL_InitEvents() < 0) {
  189. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  190. goto quit_and_error;
  191. }
  192. } else {
  193. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  194. }
  195. flags_initialized |= SDL_INIT_EVENTS;
  196. }
  197. /* Initialize the timer subsystem */
  198. if (flags & SDL_INIT_TIMER) {
  199. if (SDL_ShouldInitSubsystem(SDL_INIT_TIMER)) {
  200. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  201. if (SDL_InitTimers() < 0) {
  202. SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
  203. goto quit_and_error;
  204. }
  205. } else {
  206. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  207. }
  208. flags_initialized |= SDL_INIT_TIMER;
  209. }
  210. /* Initialize the video subsystem */
  211. if (flags & SDL_INIT_VIDEO) {
  212. #ifndef SDL_VIDEO_DISABLED
  213. if (SDL_ShouldInitSubsystem(SDL_INIT_VIDEO)) {
  214. /* video implies events */
  215. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  216. goto quit_and_error;
  217. }
  218. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  219. if (SDL_VideoInit(NULL) < 0) {
  220. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  221. goto quit_and_error;
  222. }
  223. } else {
  224. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  225. }
  226. flags_initialized |= SDL_INIT_VIDEO;
  227. #else
  228. SDL_SetError("SDL not built with video support");
  229. goto quit_and_error;
  230. #endif
  231. }
  232. /* Initialize the audio subsystem */
  233. if (flags & SDL_INIT_AUDIO) {
  234. #ifndef SDL_AUDIO_DISABLED
  235. if (SDL_ShouldInitSubsystem(SDL_INIT_AUDIO)) {
  236. /* audio implies events */
  237. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  238. goto quit_and_error;
  239. }
  240. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  241. if (SDL_InitAudio(NULL) < 0) {
  242. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  243. goto quit_and_error;
  244. }
  245. } else {
  246. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  247. }
  248. flags_initialized |= SDL_INIT_AUDIO;
  249. #else
  250. SDL_SetError("SDL not built with audio support");
  251. goto quit_and_error;
  252. #endif
  253. }
  254. /* Initialize the joystick subsystem */
  255. if (flags & SDL_INIT_JOYSTICK) {
  256. #ifndef SDL_JOYSTICK_DISABLED
  257. if (SDL_ShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  258. /* joystick implies events */
  259. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  260. goto quit_and_error;
  261. }
  262. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  263. if (SDL_InitJoysticks() < 0) {
  264. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  265. goto quit_and_error;
  266. }
  267. } else {
  268. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  269. }
  270. flags_initialized |= SDL_INIT_JOYSTICK;
  271. #else
  272. SDL_SetError("SDL not built with joystick support");
  273. goto quit_and_error;
  274. #endif
  275. }
  276. if (flags & SDL_INIT_GAMEPAD) {
  277. #ifndef SDL_JOYSTICK_DISABLED
  278. if (SDL_ShouldInitSubsystem(SDL_INIT_GAMEPAD)) {
  279. /* game controller implies joystick */
  280. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_JOYSTICK)) {
  281. goto quit_and_error;
  282. }
  283. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  284. if (SDL_InitGamepads() < 0) {
  285. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  286. goto quit_and_error;
  287. }
  288. } else {
  289. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  290. }
  291. flags_initialized |= SDL_INIT_GAMEPAD;
  292. #else
  293. SDL_SetError("SDL not built with joystick support");
  294. goto quit_and_error;
  295. #endif
  296. }
  297. /* Initialize the haptic subsystem */
  298. if (flags & SDL_INIT_HAPTIC) {
  299. #ifndef SDL_HAPTIC_DISABLED
  300. if (SDL_ShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  301. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  302. if (SDL_InitHaptics() < 0) {
  303. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  304. goto quit_and_error;
  305. }
  306. } else {
  307. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  308. }
  309. flags_initialized |= SDL_INIT_HAPTIC;
  310. #else
  311. SDL_SetError("SDL not built with haptic (force feedback) support");
  312. goto quit_and_error;
  313. #endif
  314. }
  315. /* Initialize the sensor subsystem */
  316. if (flags & SDL_INIT_SENSOR) {
  317. #ifndef SDL_SENSOR_DISABLED
  318. if (SDL_ShouldInitSubsystem(SDL_INIT_SENSOR)) {
  319. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  320. if (SDL_InitSensors() < 0) {
  321. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  322. goto quit_and_error;
  323. }
  324. } else {
  325. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  326. }
  327. flags_initialized |= SDL_INIT_SENSOR;
  328. #else
  329. SDL_SetError("SDL not built with sensor support");
  330. goto quit_and_error;
  331. #endif
  332. }
  333. /* Initialize the camera subsystem */
  334. if (flags & SDL_INIT_CAMERA) {
  335. #ifndef SDL_CAMERA_DISABLED
  336. if (SDL_ShouldInitSubsystem(SDL_INIT_CAMERA)) {
  337. /* camera implies events */
  338. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  339. goto quit_and_error;
  340. }
  341. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  342. if (SDL_CameraInit(NULL) < 0) {
  343. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  344. goto quit_and_error;
  345. }
  346. } else {
  347. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  348. }
  349. flags_initialized |= SDL_INIT_CAMERA;
  350. #else
  351. SDL_SetError("SDL not built with camera support");
  352. goto quit_and_error;
  353. #endif
  354. }
  355. (void)flags_initialized; /* make static analysis happy, since this only gets used in error cases. */
  356. return 0;
  357. quit_and_error:
  358. SDL_QuitSubSystem(flags_initialized);
  359. return -1;
  360. }
  361. int SDL_Init(Uint32 flags)
  362. {
  363. return SDL_InitSubSystem(flags);
  364. }
  365. void SDL_QuitSubSystem(Uint32 flags)
  366. {
  367. /* Shut down requested initialized subsystems */
  368. #ifndef SDL_CAMERA_DISABLED
  369. if (flags & SDL_INIT_CAMERA) {
  370. if (SDL_ShouldQuitSubsystem(SDL_INIT_CAMERA)) {
  371. SDL_QuitCamera();
  372. /* camera implies events */
  373. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  374. }
  375. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  376. }
  377. #endif
  378. #ifndef SDL_SENSOR_DISABLED
  379. if (flags & SDL_INIT_SENSOR) {
  380. if (SDL_ShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  381. SDL_QuitSensors();
  382. }
  383. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  384. }
  385. #endif
  386. #ifndef SDL_JOYSTICK_DISABLED
  387. if (flags & SDL_INIT_GAMEPAD) {
  388. if (SDL_ShouldQuitSubsystem(SDL_INIT_GAMEPAD)) {
  389. SDL_QuitGamepads();
  390. /* game controller implies joystick */
  391. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  392. }
  393. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  394. }
  395. if (flags & SDL_INIT_JOYSTICK) {
  396. if (SDL_ShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  397. SDL_QuitJoysticks();
  398. /* joystick implies events */
  399. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  400. }
  401. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  402. }
  403. #endif
  404. #ifndef SDL_HAPTIC_DISABLED
  405. if (flags & SDL_INIT_HAPTIC) {
  406. if (SDL_ShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  407. SDL_QuitHaptics();
  408. }
  409. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  410. }
  411. #endif
  412. #ifndef SDL_AUDIO_DISABLED
  413. if (flags & SDL_INIT_AUDIO) {
  414. if (SDL_ShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  415. SDL_QuitAudio();
  416. /* audio implies events */
  417. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  418. }
  419. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  420. }
  421. #endif
  422. #ifndef SDL_VIDEO_DISABLED
  423. if (flags & SDL_INIT_VIDEO) {
  424. if (SDL_ShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  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. SDL_QuitTime();
  478. #ifdef SDL_USE_LIBDBUS
  479. SDL_DBus_Quit();
  480. #endif
  481. SDL_ClearHints();
  482. SDL_AssertionsQuit();
  483. SDL_QuitProperties();
  484. SDL_QuitLog();
  485. /* Now that every subsystem has been quit, we reset the subsystem refcount
  486. * and the list of initialized subsystems.
  487. */
  488. SDL_memset(SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount));
  489. SDL_CleanupTLS();
  490. SDL_bInMainQuit = SDL_FALSE;
  491. }
  492. /* Assume we can wrap SDL_AtomicInt values and cast to Uint32 */
  493. SDL_COMPILE_TIME_ASSERT(sizeof_object_id, sizeof(int) == sizeof(Uint32));
  494. Uint32 SDL_GetNextObjectID(void)
  495. {
  496. static SDL_AtomicInt last_id;
  497. Uint32 id = (Uint32)SDL_AtomicIncRef(&last_id) + 1;
  498. if (id == 0) {
  499. id = (Uint32)SDL_AtomicIncRef(&last_id) + 1;
  500. }
  501. return id;
  502. }
  503. /* Get the library version number */
  504. int SDL_GetVersion(SDL_Version *ver)
  505. {
  506. static SDL_bool check_hint = SDL_TRUE;
  507. static SDL_bool legacy_version = SDL_FALSE;
  508. if (!ver) {
  509. return SDL_InvalidParamError("ver");
  510. }
  511. SDL_VERSION(ver);
  512. if (check_hint) {
  513. check_hint = SDL_FALSE;
  514. legacy_version = SDL_GetHintBoolean("SDL_LEGACY_VERSION", SDL_FALSE);
  515. }
  516. if (legacy_version) {
  517. /* Prior to SDL 2.24.0, the patch version was incremented with every release */
  518. ver->patch = ver->minor;
  519. ver->minor = 0;
  520. }
  521. return 0;
  522. }
  523. /* Get the library source revision */
  524. const char *SDL_GetRevision(void)
  525. {
  526. return SDL_REVISION;
  527. }
  528. /* Get the name of the platform */
  529. const char *SDL_GetPlatform(void)
  530. {
  531. #if defined(SDL_PLATFORM_AIX)
  532. return "AIX";
  533. #elif defined(SDL_PLATFORM_ANDROID)
  534. return "Android";
  535. #elif defined(SDL_PLATFORM_BSDI)
  536. return "BSDI";
  537. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  538. return "Emscripten";
  539. #elif defined(SDL_PLATFORM_FREEBSD)
  540. return "FreeBSD";
  541. #elif defined(SDL_PLATFORM_HAIKU)
  542. return "Haiku";
  543. #elif defined(SDL_PLATFORM_HPUX)
  544. return "HP-UX";
  545. #elif defined(SDL_PLATFORM_IRIX)
  546. return "Irix";
  547. #elif defined(SDL_PLATFORM_LINUX)
  548. return "Linux";
  549. #elif defined(__MINT__)
  550. return "Atari MiNT";
  551. #elif defined(SDL_PLATFORM_MACOS)
  552. return "macOS";
  553. #elif defined(SDL_PLATFORM_NETBSD)
  554. return "NetBSD";
  555. #elif defined(SDL_PLATFORM_OPENBSD)
  556. return "OpenBSD";
  557. #elif defined(SDL_PLATFORM_OS2)
  558. return "OS/2";
  559. #elif defined(SDL_PLATFORM_OSF)
  560. return "OSF/1";
  561. #elif defined(SDL_PLATFORM_QNXNTO)
  562. return "QNX Neutrino";
  563. #elif defined(SDL_PLATFORM_RISCOS)
  564. return "RISC OS";
  565. #elif defined(SDL_PLATFORM_SOLARIS)
  566. return "Solaris";
  567. #elif defined(SDL_PLATFORM_WIN32)
  568. return "Windows";
  569. #elif defined(SDL_PLATFORM_WINRT)
  570. return "WinRT";
  571. #elif defined(SDL_PLATFORM_WINGDK)
  572. return "WinGDK";
  573. #elif defined(SDL_PLATFORM_XBOXONE)
  574. return "Xbox One";
  575. #elif defined(SDL_PLATFORM_XBOXSERIES)
  576. return "Xbox Series X|S";
  577. #elif defined(SDL_PLATFORM_IOS)
  578. return "iOS";
  579. #elif defined(SDL_PLATFORM_TVOS)
  580. return "tvOS";
  581. #elif defined(SDL_PLATFORM_PS2)
  582. return "PlayStation 2";
  583. #elif defined(SDL_PLATFORM_PSP)
  584. return "PlayStation Portable";
  585. #elif defined(SDL_PLATFORM_VITA)
  586. return "PlayStation Vita";
  587. #elif defined(SDL_PLATFORM_NGAGE)
  588. return "Nokia N-Gage";
  589. #elif defined(SDL_PLATFORM_3DS)
  590. return "Nintendo 3DS";
  591. #elif defined(__managarm__)
  592. return "Managarm";
  593. #else
  594. return "Unknown (see SDL_platform.h)";
  595. #endif
  596. }
  597. SDL_bool SDL_IsTablet(void)
  598. {
  599. #ifdef SDL_PLATFORM_ANDROID
  600. extern SDL_bool SDL_IsAndroidTablet(void);
  601. return SDL_IsAndroidTablet();
  602. #elif defined(SDL_PLATFORM_IOS)
  603. extern SDL_bool SDL_IsIPad(void);
  604. return SDL_IsIPad();
  605. #else
  606. return SDL_FALSE;
  607. #endif
  608. }
  609. #ifdef SDL_PLATFORM_WIN32
  610. #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
  611. /* FIXME: Still need to include DllMain() on Watcom C ? */
  612. BOOL APIENTRY MINGW32_FORCEALIGN _DllMainCRTStartup(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  613. {
  614. switch (ul_reason_for_call) {
  615. case DLL_PROCESS_ATTACH:
  616. case DLL_THREAD_ATTACH:
  617. case DLL_THREAD_DETACH:
  618. case DLL_PROCESS_DETACH:
  619. break;
  620. }
  621. return TRUE;
  622. }
  623. #endif /* Building DLL */
  624. #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */