SDL.c 22 KB

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