SDL.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #ifdef SDL_PLATFORM_ANDROID
  51. #include "core/android/SDL_android.h"
  52. #endif
  53. #define SDL_INIT_EVERYTHING ~0U
  54. // Initialization/Cleanup routines
  55. #include "timer/SDL_timer_c.h"
  56. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  57. extern bool SDL_HelperWindowCreate(void);
  58. extern void SDL_HelperWindowDestroy(void);
  59. #endif
  60. #ifdef SDL_BUILD_MAJOR_VERSION
  61. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MAJOR_VERSION,
  62. SDL_MAJOR_VERSION == SDL_BUILD_MAJOR_VERSION);
  63. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MINOR_VERSION,
  64. SDL_MINOR_VERSION == SDL_BUILD_MINOR_VERSION);
  65. SDL_COMPILE_TIME_ASSERT(SDL_BUILD_MICRO_VERSION,
  66. SDL_MICRO_VERSION == SDL_BUILD_MICRO_VERSION);
  67. #endif
  68. // Limited by its encoding in SDL_VERSIONNUM
  69. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_min, SDL_MAJOR_VERSION >= 0);
  70. SDL_COMPILE_TIME_ASSERT(SDL_MAJOR_VERSION_max, SDL_MAJOR_VERSION <= 10);
  71. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_min, SDL_MINOR_VERSION >= 0);
  72. SDL_COMPILE_TIME_ASSERT(SDL_MINOR_VERSION_max, SDL_MINOR_VERSION <= 999);
  73. SDL_COMPILE_TIME_ASSERT(SDL_MICRO_VERSION_min, SDL_MICRO_VERSION >= 0);
  74. SDL_COMPILE_TIME_ASSERT(SDL_MICRO_VERSION_max, SDL_MICRO_VERSION <= 999);
  75. /* This is not declared in any header, although it is shared between some
  76. parts of SDL, because we don't want anything calling it without an
  77. extremely good reason. */
  78. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  79. SDL_NORETURN void SDL_ExitProcess(int exitcode)
  80. {
  81. #if defined(SDL_PLATFORM_WINDOWS)
  82. /* "if you do not know the state of all threads in your process, it is
  83. better to call TerminateProcess than ExitProcess"
  84. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx */
  85. TerminateProcess(GetCurrentProcess(), exitcode);
  86. /* MingW doesn't have TerminateProcess marked as noreturn, so add an
  87. ExitProcess here that will never be reached but make MingW happy. */
  88. ExitProcess(exitcode);
  89. #elif defined(SDL_PLATFORM_EMSCRIPTEN)
  90. emscripten_cancel_main_loop(); // this should "kill" the app.
  91. emscripten_force_exit(exitcode); // this should "kill" the app.
  92. exit(exitcode);
  93. #elif defined(SDL_PLATFORM_HAIKU) // Haiku has _Exit, but it's not marked noreturn.
  94. _exit(exitcode);
  95. #elif defined(HAVE__EXIT) // Upper case _Exit()
  96. _Exit(exitcode);
  97. #else
  98. _exit(exitcode);
  99. #endif
  100. }
  101. // App metadata
  102. bool SDL_SetAppMetadata(const char *appname, const char *appversion, const char *appidentifier)
  103. {
  104. SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING, appname);
  105. SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING, appversion);
  106. SDL_SetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING, appidentifier);
  107. return true;
  108. }
  109. static bool SDL_ValidMetadataProperty(const char *name)
  110. {
  111. if (!name || !*name) {
  112. return false;
  113. }
  114. if (SDL_strcmp(name, SDL_PROP_APP_METADATA_NAME_STRING) == 0 ||
  115. SDL_strcmp(name, SDL_PROP_APP_METADATA_VERSION_STRING) == 0 ||
  116. SDL_strcmp(name, SDL_PROP_APP_METADATA_IDENTIFIER_STRING) == 0 ||
  117. SDL_strcmp(name, SDL_PROP_APP_METADATA_CREATOR_STRING) == 0 ||
  118. SDL_strcmp(name, SDL_PROP_APP_METADATA_COPYRIGHT_STRING) == 0 ||
  119. SDL_strcmp(name, SDL_PROP_APP_METADATA_URL_STRING) == 0 ||
  120. SDL_strcmp(name, SDL_PROP_APP_METADATA_TYPE_STRING) == 0) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. bool SDL_SetAppMetadataProperty(const char *name, const char *value)
  126. {
  127. if (!SDL_ValidMetadataProperty(name)) {
  128. return SDL_InvalidParamError("name");
  129. }
  130. return SDL_SetStringProperty(SDL_GetGlobalProperties(), name, value);
  131. }
  132. const char *SDL_GetAppMetadataProperty(const char *name)
  133. {
  134. if (!SDL_ValidMetadataProperty(name)) {
  135. SDL_InvalidParamError("name");
  136. return NULL;
  137. }
  138. const char *value = NULL;
  139. if (SDL_strcmp(name, SDL_PROP_APP_METADATA_NAME_STRING) == 0) {
  140. value = SDL_GetHint(SDL_HINT_APP_NAME);
  141. } else if (SDL_strcmp(name, SDL_PROP_APP_METADATA_IDENTIFIER_STRING) == 0) {
  142. value = SDL_GetHint(SDL_HINT_APP_ID);
  143. }
  144. if (!value || !*value) {
  145. value = SDL_GetStringProperty(SDL_GetGlobalProperties(), name, NULL);
  146. }
  147. if (!value || !*value) {
  148. if (SDL_strcmp(name, SDL_PROP_APP_METADATA_NAME_STRING) == 0) {
  149. value = "SDL Application";
  150. } else if (SDL_strcmp(name, SDL_PROP_APP_METADATA_TYPE_STRING) == 0) {
  151. value = "application";
  152. }
  153. }
  154. return value;
  155. }
  156. // The initialized subsystems
  157. #ifdef SDL_MAIN_NEEDED
  158. static bool SDL_MainIsReady = false;
  159. #else
  160. static bool SDL_MainIsReady = true;
  161. #endif
  162. static bool SDL_bInMainQuit = false;
  163. static Uint8 SDL_SubsystemRefCount[32];
  164. // Private helper to increment a subsystem's ref counter.
  165. static void SDL_IncrementSubsystemRefCount(Uint32 subsystem)
  166. {
  167. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  168. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  169. if (subsystem_index >= 0) {
  170. ++SDL_SubsystemRefCount[subsystem_index];
  171. }
  172. }
  173. // Private helper to decrement a subsystem's ref counter.
  174. static void SDL_DecrementSubsystemRefCount(Uint32 subsystem)
  175. {
  176. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  177. if ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] > 0)) {
  178. if (SDL_bInMainQuit) {
  179. SDL_SubsystemRefCount[subsystem_index] = 0;
  180. } else {
  181. --SDL_SubsystemRefCount[subsystem_index];
  182. }
  183. }
  184. }
  185. // Private helper to check if a system needs init.
  186. static bool SDL_ShouldInitSubsystem(Uint32 subsystem)
  187. {
  188. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  189. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  190. return ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 0));
  191. }
  192. // Private helper to check if a system needs to be quit.
  193. static bool SDL_ShouldQuitSubsystem(Uint32 subsystem)
  194. {
  195. const int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  196. if ((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 0)) {
  197. return false;
  198. }
  199. /* If we're in SDL_Quit, we shut down every subsystem, even if refcount
  200. * isn't zero.
  201. */
  202. return (((subsystem_index >= 0) && (SDL_SubsystemRefCount[subsystem_index] == 1)) || SDL_bInMainQuit);
  203. }
  204. /* Private helper to either increment's existing ref counter,
  205. * or fully init a new subsystem. */
  206. static bool SDL_InitOrIncrementSubsystem(Uint32 subsystem)
  207. {
  208. int subsystem_index = SDL_MostSignificantBitIndex32(subsystem);
  209. SDL_assert((subsystem_index < 0) || (SDL_SubsystemRefCount[subsystem_index] < 255));
  210. if (subsystem_index < 0) {
  211. return false;
  212. }
  213. if (SDL_SubsystemRefCount[subsystem_index] > 0) {
  214. ++SDL_SubsystemRefCount[subsystem_index];
  215. return true;
  216. }
  217. return SDL_InitSubSystem(subsystem);
  218. }
  219. void SDL_SetMainReady(void)
  220. {
  221. SDL_MainIsReady = true;
  222. }
  223. // Initialize all the subsystems that require initialization before threads start
  224. void SDL_InitMainThread(void)
  225. {
  226. static bool done_info = false;
  227. SDL_InitTLSData();
  228. SDL_InitEnvironment();
  229. SDL_InitTicks();
  230. SDL_InitFilesystem();
  231. if (!done_info) {
  232. const char *value;
  233. value = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_NAME_STRING);
  234. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "App name: %s", value ? value : "<unspecified>");
  235. value = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_VERSION_STRING);
  236. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "App version: %s", value ? value : "<unspecified>");
  237. value = SDL_GetAppMetadataProperty(SDL_PROP_APP_METADATA_IDENTIFIER_STRING);
  238. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "App ID: %s", value ? value : "<unspecified>");
  239. SDL_LogInfo(SDL_LOG_CATEGORY_SYSTEM, "SDL revision: %s", SDL_REVISION);
  240. done_info = true;
  241. }
  242. }
  243. static void SDL_QuitMainThread(void)
  244. {
  245. SDL_QuitFilesystem();
  246. SDL_QuitTicks();
  247. SDL_QuitEnvironment();
  248. SDL_QuitTLSData();
  249. }
  250. bool SDL_InitSubSystem(SDL_InitFlags flags)
  251. {
  252. Uint32 flags_initialized = 0;
  253. if (!SDL_MainIsReady) {
  254. return SDL_SetError("Application didn't initialize properly, did you include SDL_main.h in the file containing your main() function?");
  255. }
  256. SDL_InitMainThread();
  257. #ifdef SDL_USE_LIBDBUS
  258. SDL_DBus_Init();
  259. #endif
  260. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  261. if (flags & (SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK)) {
  262. if (!SDL_HelperWindowCreate()) {
  263. goto quit_and_error;
  264. }
  265. }
  266. #endif
  267. // Initialize the event subsystem
  268. if (flags & SDL_INIT_EVENTS) {
  269. if (SDL_ShouldInitSubsystem(SDL_INIT_EVENTS)) {
  270. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  271. if (!SDL_InitEvents()) {
  272. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  273. goto quit_and_error;
  274. }
  275. } else {
  276. SDL_IncrementSubsystemRefCount(SDL_INIT_EVENTS);
  277. }
  278. flags_initialized |= SDL_INIT_EVENTS;
  279. }
  280. // Initialize the video subsystem
  281. if (flags & SDL_INIT_VIDEO) {
  282. #ifndef SDL_VIDEO_DISABLED
  283. if (SDL_ShouldInitSubsystem(SDL_INIT_VIDEO)) {
  284. // video implies events
  285. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  286. goto quit_and_error;
  287. }
  288. SDL_IncrementSubsystemRefCount(SDL_INIT_VIDEO);
  289. if (!SDL_VideoInit(NULL)) {
  290. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  291. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  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)) {
  313. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  314. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  315. goto quit_and_error;
  316. }
  317. } else {
  318. SDL_IncrementSubsystemRefCount(SDL_INIT_AUDIO);
  319. }
  320. flags_initialized |= SDL_INIT_AUDIO;
  321. #else
  322. SDL_SetError("SDL not built with audio support");
  323. goto quit_and_error;
  324. #endif
  325. }
  326. // Initialize the joystick subsystem
  327. if (flags & SDL_INIT_JOYSTICK) {
  328. #ifndef SDL_JOYSTICK_DISABLED
  329. if (SDL_ShouldInitSubsystem(SDL_INIT_JOYSTICK)) {
  330. // joystick implies events
  331. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  332. goto quit_and_error;
  333. }
  334. SDL_IncrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  335. if (!SDL_InitJoysticks()) {
  336. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  337. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  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. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  360. goto quit_and_error;
  361. }
  362. } else {
  363. SDL_IncrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  364. }
  365. flags_initialized |= SDL_INIT_GAMEPAD;
  366. #else
  367. SDL_SetError("SDL not built with joystick support");
  368. goto quit_and_error;
  369. #endif
  370. }
  371. // Initialize the haptic subsystem
  372. if (flags & SDL_INIT_HAPTIC) {
  373. #ifndef SDL_HAPTIC_DISABLED
  374. if (SDL_ShouldInitSubsystem(SDL_INIT_HAPTIC)) {
  375. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  376. if (!SDL_InitHaptics()) {
  377. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  378. goto quit_and_error;
  379. }
  380. } else {
  381. SDL_IncrementSubsystemRefCount(SDL_INIT_HAPTIC);
  382. }
  383. flags_initialized |= SDL_INIT_HAPTIC;
  384. #else
  385. SDL_SetError("SDL not built with haptic (force feedback) support");
  386. goto quit_and_error;
  387. #endif
  388. }
  389. // Initialize the sensor subsystem
  390. if (flags & SDL_INIT_SENSOR) {
  391. #ifndef SDL_SENSOR_DISABLED
  392. if (SDL_ShouldInitSubsystem(SDL_INIT_SENSOR)) {
  393. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  394. if (!SDL_InitSensors()) {
  395. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  396. goto quit_and_error;
  397. }
  398. } else {
  399. SDL_IncrementSubsystemRefCount(SDL_INIT_SENSOR);
  400. }
  401. flags_initialized |= SDL_INIT_SENSOR;
  402. #else
  403. SDL_SetError("SDL not built with sensor support");
  404. goto quit_and_error;
  405. #endif
  406. }
  407. // Initialize the camera subsystem
  408. if (flags & SDL_INIT_CAMERA) {
  409. #ifndef SDL_CAMERA_DISABLED
  410. if (SDL_ShouldInitSubsystem(SDL_INIT_CAMERA)) {
  411. // camera implies events
  412. if (!SDL_InitOrIncrementSubsystem(SDL_INIT_EVENTS)) {
  413. goto quit_and_error;
  414. }
  415. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  416. if (!SDL_CameraInit(NULL)) {
  417. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  418. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  419. goto quit_and_error;
  420. }
  421. } else {
  422. SDL_IncrementSubsystemRefCount(SDL_INIT_CAMERA);
  423. }
  424. flags_initialized |= SDL_INIT_CAMERA;
  425. #else
  426. SDL_SetError("SDL not built with camera support");
  427. goto quit_and_error;
  428. #endif
  429. }
  430. (void)flags_initialized; // make static analysis happy, since this only gets used in error cases.
  431. return SDL_ClearError();
  432. quit_and_error:
  433. SDL_QuitSubSystem(flags_initialized);
  434. return false;
  435. }
  436. bool SDL_Init(SDL_InitFlags flags)
  437. {
  438. return SDL_InitSubSystem(flags);
  439. }
  440. void SDL_QuitSubSystem(SDL_InitFlags flags)
  441. {
  442. // Shut down requested initialized subsystems
  443. #ifndef SDL_CAMERA_DISABLED
  444. if (flags & SDL_INIT_CAMERA) {
  445. if (SDL_ShouldQuitSubsystem(SDL_INIT_CAMERA)) {
  446. SDL_QuitCamera();
  447. // camera implies events
  448. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  449. }
  450. SDL_DecrementSubsystemRefCount(SDL_INIT_CAMERA);
  451. }
  452. #endif
  453. #ifndef SDL_SENSOR_DISABLED
  454. if (flags & SDL_INIT_SENSOR) {
  455. if (SDL_ShouldQuitSubsystem(SDL_INIT_SENSOR)) {
  456. SDL_QuitSensors();
  457. }
  458. SDL_DecrementSubsystemRefCount(SDL_INIT_SENSOR);
  459. }
  460. #endif
  461. #ifndef SDL_JOYSTICK_DISABLED
  462. if (flags & SDL_INIT_GAMEPAD) {
  463. if (SDL_ShouldQuitSubsystem(SDL_INIT_GAMEPAD)) {
  464. SDL_QuitGamepads();
  465. // game controller implies joystick
  466. SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  467. }
  468. SDL_DecrementSubsystemRefCount(SDL_INIT_GAMEPAD);
  469. }
  470. if (flags & SDL_INIT_JOYSTICK) {
  471. if (SDL_ShouldQuitSubsystem(SDL_INIT_JOYSTICK)) {
  472. SDL_QuitJoysticks();
  473. // joystick implies events
  474. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  475. }
  476. SDL_DecrementSubsystemRefCount(SDL_INIT_JOYSTICK);
  477. }
  478. #endif
  479. #ifndef SDL_HAPTIC_DISABLED
  480. if (flags & SDL_INIT_HAPTIC) {
  481. if (SDL_ShouldQuitSubsystem(SDL_INIT_HAPTIC)) {
  482. SDL_QuitHaptics();
  483. }
  484. SDL_DecrementSubsystemRefCount(SDL_INIT_HAPTIC);
  485. }
  486. #endif
  487. #ifndef SDL_AUDIO_DISABLED
  488. if (flags & SDL_INIT_AUDIO) {
  489. if (SDL_ShouldQuitSubsystem(SDL_INIT_AUDIO)) {
  490. SDL_QuitAudio();
  491. // audio implies events
  492. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  493. }
  494. SDL_DecrementSubsystemRefCount(SDL_INIT_AUDIO);
  495. }
  496. #endif
  497. #ifndef SDL_VIDEO_DISABLED
  498. if (flags & SDL_INIT_VIDEO) {
  499. if (SDL_ShouldQuitSubsystem(SDL_INIT_VIDEO)) {
  500. SDL_QuitRender();
  501. SDL_VideoQuit();
  502. // video implies events
  503. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  504. }
  505. SDL_DecrementSubsystemRefCount(SDL_INIT_VIDEO);
  506. }
  507. #endif
  508. if (flags & SDL_INIT_EVENTS) {
  509. if (SDL_ShouldQuitSubsystem(SDL_INIT_EVENTS)) {
  510. SDL_QuitEvents();
  511. }
  512. SDL_DecrementSubsystemRefCount(SDL_INIT_EVENTS);
  513. }
  514. }
  515. Uint32 SDL_WasInit(SDL_InitFlags flags)
  516. {
  517. int i;
  518. int num_subsystems = SDL_arraysize(SDL_SubsystemRefCount);
  519. Uint32 initialized = 0;
  520. // Fast path for checking one flag
  521. if (SDL_HasExactlyOneBitSet32(flags)) {
  522. int subsystem_index = SDL_MostSignificantBitIndex32(flags);
  523. return SDL_SubsystemRefCount[subsystem_index] ? flags : 0;
  524. }
  525. if (!flags) {
  526. flags = SDL_INIT_EVERYTHING;
  527. }
  528. num_subsystems = SDL_min(num_subsystems, SDL_MostSignificantBitIndex32(flags) + 1);
  529. // Iterate over each bit in flags, and check the matching subsystem.
  530. for (i = 0; i < num_subsystems; ++i) {
  531. if ((flags & 1) && SDL_SubsystemRefCount[i] > 0) {
  532. initialized |= (1 << i);
  533. }
  534. flags >>= 1;
  535. }
  536. return initialized;
  537. }
  538. void SDL_Quit(void)
  539. {
  540. SDL_bInMainQuit = true;
  541. // Quit all subsystems
  542. #ifdef SDL_VIDEO_DRIVER_WINDOWS
  543. SDL_HelperWindowDestroy();
  544. #endif
  545. SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
  546. #ifdef SDL_USE_LIBDBUS
  547. SDL_DBus_Quit();
  548. #endif
  549. SDL_QuitTimers();
  550. SDL_SetObjectsInvalid();
  551. SDL_AssertionsQuit();
  552. SDL_QuitPixelFormatDetails();
  553. SDL_QuitCPUInfo();
  554. /* Now that every subsystem has been quit, we reset the subsystem refcount
  555. * and the list of initialized subsystems.
  556. */
  557. SDL_memset(SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount));
  558. SDL_QuitLog();
  559. SDL_QuitHints();
  560. SDL_QuitProperties();
  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. bool SDL_IsTablet(void)
  642. {
  643. #ifdef SDL_PLATFORM_ANDROID
  644. return SDL_IsAndroidTablet();
  645. #elif defined(SDL_PLATFORM_IOS)
  646. extern bool SDL_IsIPad(void);
  647. return SDL_IsIPad();
  648. #else
  649. return false;
  650. #endif
  651. }
  652. bool SDL_IsTV(void)
  653. {
  654. #ifdef SDL_PLATFORM_ANDROID
  655. return SDL_IsAndroidTV();
  656. #elif defined(SDL_PLATFORM_IOS)
  657. extern bool SDL_IsAppleTV(void);
  658. return SDL_IsAppleTV();
  659. #else
  660. return false;
  661. #endif
  662. }
  663. #ifdef SDL_PLATFORM_WIN32
  664. #if (!defined(HAVE_LIBC) || defined(__WATCOMC__)) && !defined(SDL_STATIC_LIB)
  665. // FIXME: Still need to include DllMain() on Watcom C ?
  666. BOOL APIENTRY MINGW32_FORCEALIGN _DllMainCRTStartup(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
  667. {
  668. switch (ul_reason_for_call) {
  669. case DLL_PROCESS_ATTACH:
  670. case DLL_THREAD_ATTACH:
  671. case DLL_THREAD_DETACH:
  672. case DLL_PROCESS_DETACH:
  673. break;
  674. }
  675. return TRUE;
  676. }
  677. #endif // Building DLL
  678. #endif // defined(SDL_PLATFORM_WIN32)