SDL.c 25 KB

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