SDL.c 22 KB

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