SDL.c 23 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_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 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. 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 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) == 0;
  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_InitHints();
  228. }
  229. static void SDL_QuitMainThread(void)
  230. {
  231. SDL_QuitHints();
  232. SDL_QuitProperties();
  233. SDL_QuitLog();
  234. SDL_QuitFilesystem();
  235. SDL_QuitTicks();
  236. SDL_QuitTLSData();
  237. }
  238. int SDL_InitSubSystem(SDL_InitFlags flags)
  239. {
  240. Uint32 flags_initialized = 0;
  241. if (!SDL_MainIsReady) {
  242. return SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
  243. }
  244. SDL_InitMainThread();
  245. #ifdef SDL_USE_LIBDBUS
  246. SDL_DBus_Init();
  247. #endif
  248. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  249. if (flags & (SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK)) {
  250. if (SDL_HelperWindowCreate() < 0) {
  251. goto quit_and_error;
  252. }
  253. }
  254. #endif
  255. // Initialize the event subsystem
  256. if (flags & SDL_INIT_EVENTS) {
  257. if (SDL_ShouldInitSubsystem(SDL_INIT_EVENTS)) {
  258. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  259. if (SDL_InitEvents() < 0) {
  260. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  261. goto quit_and_error;
  262. }
  263. } else {
  264. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  265. }
  266. flags_initialized |= SDL_INIT_EVENTS;
  267. }
  268. // Initialize the timer subsystem
  269. if (flags & SDL_INIT_TIMER) {
  270. if (SDL_ShouldInitSubsystem(SDL_INIT_TIMER)) {
  271. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  272. if (SDL_InitTimers() < 0) {
  273. SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
  274. goto quit_and_error;
  275. }
  276. } else {
  277. SDL_IncrementSubsystemRefCount(SDL_INIT_TIMER);
  278. }
  279. flags_initialized |= SDL_INIT_TIMER;
  280. }
  281. // Initialize the video subsystem
  282. if (flags & SDL_INIT_VIDEO) {
  283. #ifndef SDL_VIDEO_DISABLED
  284. if (SDL_ShouldInitSubsystem(SDL_INIT_VIDEO)) {
  285. // video implies events
  286. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  287. goto quit_and_error;
  288. }
  289. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  290. if (SDL_VideoInit(NULL) < 0) {
  291. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  292. goto quit_and_error;
  293. }
  294. } else {
  295. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  296. }
  297. flags_initialized |= SDL_INIT_VIDEO;
  298. #else
  299. SDL_SetError("SDL not built with video support");
  300. goto quit_and_error;
  301. #endif
  302. }
  303. // Initialize the audio subsystem
  304. if (flags & SDL_INIT_AUDIO) {
  305. #ifndef SDL_AUDIO_DISABLED
  306. if (SDL_ShouldInitSubsystem(SDL_INIT_AUDIO)) {
  307. // audio implies events
  308. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  309. goto quit_and_error;
  310. }
  311. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  312. if (SDL_InitAudio(NULL) < 0) {
  313. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  314. goto quit_and_error;
  315. }
  316. } else {
  317. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  318. }
  319. flags_initialized |= SDL_INIT_AUDIO;
  320. #else
  321. SDL_SetError("SDL not built with audio support");
  322. goto quit_and_error;
  323. #endif
  324. }
  325. // Initialize the joystick subsystem
  326. if (flags & SDL_INIT_JOYSTICK) {
  327. #ifndef SDL_JOYSTICK_DISABLED
  328. if (SDL_ShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  329. // joystick implies events
  330. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  331. goto quit_and_error;
  332. }
  333. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  334. if (SDL_InitJoysticks() < 0) {
  335. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  336. goto quit_and_error;
  337. }
  338. } else {
  339. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  340. }
  341. flags_initialized |= SDL_INIT_JOYSTICK;
  342. #else
  343. SDL_SetError("SDL not built with joystick support");
  344. goto quit_and_error;
  345. #endif
  346. }
  347. if (flags & SDL_INIT_GAMEPAD) {
  348. #ifndef SDL_JOYSTICK_DISABLED
  349. if (SDL_ShouldInitSubsystem(SDL_INIT_GAMEPAD)) {
  350. // game controller implies joystick
  351. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_JOYSTICK)) {
  352. goto quit_and_error;
  353. }
  354. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  355. if (SDL_InitGamepads() < 0) {
  356. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  357. goto quit_and_error;
  358. }
  359. } else {
  360. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  361. }
  362. flags_initialized |= SDL_INIT_GAMEPAD;
  363. #else
  364. SDL_SetError("SDL not built with joystick support");
  365. goto quit_and_error;
  366. #endif
  367. }
  368. // Initialize the haptic subsystem
  369. if (flags & SDL_INIT_HAPTIC) {
  370. #ifndef SDL_HAPTIC_DISABLED
  371. if (SDL_ShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  372. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  373. if (SDL_InitHaptics() < 0) {
  374. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  375. goto quit_and_error;
  376. }
  377. } else {
  378. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  379. }
  380. flags_initialized |= SDL_INIT_HAPTIC;
  381. #else
  382. SDL_SetError("SDL not built with haptic (force feedback) support");
  383. goto quit_and_error;
  384. #endif
  385. }
  386. // Initialize the sensor subsystem
  387. if (flags & SDL_INIT_SENSOR) {
  388. #ifndef SDL_SENSOR_DISABLED
  389. if (SDL_ShouldInitSubsystem(SDL_INIT_SENSOR)) {
  390. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  391. if (SDL_InitSensors() < 0) {
  392. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  393. goto quit_and_error;
  394. }
  395. } else {
  396. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  397. }
  398. flags_initialized |= SDL_INIT_SENSOR;
  399. #else
  400. SDL_SetError("SDL not built with sensor support");
  401. goto quit_and_error;
  402. #endif
  403. }
  404. // Initialize the camera subsystem
  405. if (flags & SDL_INIT_CAMERA) {
  406. #ifndef SDL_CAMERA_DISABLED
  407. if (SDL_ShouldInitSubsystem(SDL_INIT_CAMERA)) {
  408. // camera implies events
  409. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  410. goto quit_and_error;
  411. }
  412. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  413. if (SDL_CameraInit(NULL) < 0) {
  414. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  415. goto quit_and_error;
  416. }
  417. } else {
  418. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  419. }
  420. flags_initialized |= SDL_INIT_CAMERA;
  421. #else
  422. SDL_SetError("SDL not built with camera support");
  423. goto quit_and_error;
  424. #endif
  425. }
  426. (void)flags_initialized; // make static analysis happy, since this only gets used in error cases.
  427. return SDL_ClearError();
  428. quit_and_error:
  429. SDL_QuitSubSystem(flags_initialized);
  430. return -1;
  431. }
  432. int SDL_Init(SDL_InitFlags flags)
  433. {
  434. return SDL_InitSubSystem(flags);
  435. }
  436. void SDL_QuitSubSystem(SDL_InitFlags flags)
  437. {
  438. // Shut down requested initialized subsystems
  439. #ifndef SDL_CAMERA_DISABLED
  440. if (flags & SDL_INIT_CAMERA) {
  441. if (SDL_ShouldQuitSubsystem(SDL_INIT_CAMERA)) {
  442. SDL_QuitCamera();
  443. // camera implies events
  444. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  445. }
  446. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  447. }
  448. #endif
  449. #ifndef SDL_SENSOR_DISABLED
  450. if (flags & SDL_INIT_SENSOR) {
  451. if (SDL_ShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  452. SDL_QuitSensors();
  453. }
  454. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  455. }
  456. #endif
  457. #ifndef SDL_JOYSTICK_DISABLED
  458. if (flags & SDL_INIT_GAMEPAD) {
  459. if (SDL_ShouldQuitSubsystem(SDL_INIT_GAMEPAD)) {
  460. SDL_QuitGamepads();
  461. // game controller implies joystick
  462. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  463. }
  464. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  465. }
  466. if (flags & SDL_INIT_JOYSTICK) {
  467. if (SDL_ShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  468. SDL_QuitJoysticks();
  469. // joystick implies events
  470. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  471. }
  472. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  473. }
  474. #endif
  475. #ifndef SDL_HAPTIC_DISABLED
  476. if (flags & SDL_INIT_HAPTIC) {
  477. if (SDL_ShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  478. SDL_QuitHaptics();
  479. }
  480. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  481. }
  482. #endif
  483. #ifndef SDL_AUDIO_DISABLED
  484. if (flags & SDL_INIT_AUDIO) {
  485. if (SDL_ShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  486. SDL_QuitAudio();
  487. // audio implies events
  488. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  489. }
  490. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  491. }
  492. #endif
  493. #ifndef SDL_VIDEO_DISABLED
  494. if (flags & SDL_INIT_VIDEO) {
  495. if (SDL_ShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  496. SDL_QuitRender();
  497. SDL_VideoQuit();
  498. // video implies events
  499. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  500. }
  501. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  502. }
  503. #endif
  504. if (flags & SDL_INIT_TIMER) {
  505. if (SDL_ShouldQuitSubsystem(SDL_INIT_TIMER)) {
  506. SDL_QuitTimers();
  507. }
  508. SDL_DecrementSubsystemRefCount(SDL_INIT_TIMER);
  509. }
  510. if (flags & SDL_INIT_EVENTS) {
  511. if (SDL_ShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  512. SDL_QuitEvents();
  513. }
  514. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  515. }
  516. }
  517. Uint32 SDL_WasInit(SDL_InitFlags flags)
  518. {
  519. int i;
  520. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  521. Uint32 initialized = 0;
  522. // Fast path for checking one flag
  523. if (SDL_HasExactlyOneBitSet32(flags)) {
  524. int subsystem_index = SDL_MostSignificantBitIndex32(flags);
  525. return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
  526. }
  527. if (!flags) {
  528. flags = SDL_INIT_EVERYTHING;
  529. }
  530. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  531. // Iterate over each bit in flags, and check the matching subsystem.
  532. for (i = 0; i < num_subsystems; ++i) {
  533. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  534. initialized |= (1 << i);
  535. }
  536. flags >>= 1;
  537. }
  538. return initialized;
  539. }
  540. void SDL_Quit(void)
  541. {
  542. SDL_bInMainQuit = true;
  543. // Quit all subsystems
  544. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  545. SDL_HelperWindowDestroy();
  546. #endif
  547. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  548. #ifdef SDL_USE_LIBDBUS
  549. SDL_DBus_Quit();
  550. #endif
  551. SDL_SetObjectsInvalid();
  552. SDL_AssertionsQuit();
  553. SDL_QuitPixelFormatDetails();
  554. SDL_QuitCPUInfo();
  555. /* Now that every subsystem has been quit, we reset the subsystem refcount
  556. * and the list of initialized subsystems.
  557. */
  558. SDL_memset(SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount));
  559. SDL_QuitMainThread();
  560. SDL_bInMainQuit = false;
  561. }
  562. // Get the library version number
  563. int SDL_GetVersion(void)
  564. {
  565. return SDL_VERSION;
  566. }
  567. // Get the library source revision
  568. const char *SDL_GetRevision(void)
  569. {
  570. return SDL_REVISION;
  571. }
  572. // Get the name of the platform
  573. const char *SDL_GetPlatform(void)
  574. {
  575. #if defined(SDL_PLATFORM_AIX)
  576. return "AIX";
  577. #elif defined(SDL_PLATFORM_ANDROID)
  578. return "Android";
  579. #elif defined(SDL_PLATFORM_BSDI)
  580. return "BSDI";
  581. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  582. return "Emscripten";
  583. #elif defined(SDL_PLATFORM_FREEBSD)
  584. return "FreeBSD";
  585. #elif defined(SDL_PLATFORM_HAIKU)
  586. return "Haiku";
  587. #elif defined(SDL_PLATFORM_HPUX)
  588. return "HP-UX";
  589. #elif defined(SDL_PLATFORM_IRIX)
  590. return "Irix";
  591. #elif defined(SDL_PLATFORM_LINUX)
  592. return "Linux";
  593. #elif defined(__MINT__)
  594. return "Atari MiNT";
  595. #elif defined(SDL_PLATFORM_MACOS)
  596. return "macOS";
  597. #elif defined(SDL_PLATFORM_NETBSD)
  598. return "NetBSD";
  599. #elif defined(SDL_PLATFORM_OPENBSD)
  600. return "OpenBSD";
  601. #elif defined(SDL_PLATFORM_OS2)
  602. return "OS/2";
  603. #elif defined(SDL_PLATFORM_OSF)
  604. return "OSF/1";
  605. #elif defined(SDL_PLATFORM_QNXNTO)
  606. return "QNX Neutrino";
  607. #elif defined(SDL_PLATFORM_RISCOS)
  608. return "RISC OS";
  609. #elif defined(SDL_PLATFORM_SOLARIS)
  610. return "Solaris";
  611. #elif defined(SDL_PLATFORM_WIN32)
  612. return "Windows";
  613. #elif defined(SDL_PLATFORM_WINRT)
  614. return "WinRT";
  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) || defined(SDL_PLATFORM_GDK)