SDL.c 22 KB

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