SDL.c 25 KB

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